import { expect } from "chai"; import request from "supertest"; import { createApp } from "../src/app.js"; import { getEnglishKey } from "../src/db/catalog-en-keys.js"; import { prisma } from "../src/db/prisma.js"; import { resetDatabase } from "../test-support/reset-db.js"; describe("Reference data", () => { const app = createApp(); beforeEach(async () => { await resetDatabase(); }); after(async () => { await prisma.$disconnect(); }); describe("GET /reference/diets", () => { it("returns the seeded regimes, no session required", async () => { const res = await request(app).get("/reference/diets"); expect(res.status).to.equal(200); expect(res.body).to.have.length(5); expect(res.body.map((d: { key: string }) => d.key)).to.include(getEnglishKey("Végétarien")); expect(res.body[0]).to.have.keys(["id", "key"]); }); }); describe("GET /reference/allergies", () => { it("returns the seeded allergens with their key resolved, no session required", async () => { const res = await request(app).get("/reference/allergies"); expect(res.status).to.equal(200); expect(res.body).to.have.length(14); expect(res.body.map((a: { key: string }) => a.key)).to.include(getEnglishKey("Arachides")); expect(res.body[0]).to.have.keys(["id", "key", "kind"]); }); it("classifies Gluten and Sulfites as intolerances, the rest as allergies", async () => { const res = await request(app).get("/reference/allergies"); const byKey = (name: string) => res.body.find((a: { key: string }) => a.key === getEnglishKey(name)); expect(byKey("Gluten").kind).to.equal("INTOLERANCE"); expect(byKey("Sulfites").kind).to.equal("INTOLERANCE"); expect(byKey("Arachides").kind).to.equal("ALLERGY"); expect(res.body.filter((a: { kind: string }) => a.kind === "INTOLERANCE")).to.have.length(2); }); }); describe("GET /reference/ingredients", () => { it("returns the seeded ingredients, no session required", async () => { const res = await request(app).get("/reference/ingredients"); expect(res.status).to.equal(200); expect(res.body.length).to.be.greaterThan(0); expect(res.body.map((i: { key: string }) => i.key)).to.include(getEnglishKey("Tomate")); expect(res.body[0]).to.have.keys([ "id", "key", "icon", "category", "subcategory", "reproducible", "allergens", "diets", ]); }); it("resolves each ingredient's linked allergens, empty for one with none", async () => { const res = await request(app).get("/reference/ingredients"); const byKey = (name: string) => res.body.find((i: { key: string }) => i.key === getEnglishKey(name)); expect(byKey("Œuf").allergens.map((a: { key: string }) => a.key)).to.include( getEnglishKey("Œufs"), ); expect(byKey("Tomate").allergens).to.deep.equal([]); }); }); });