batchCooking/apps/api/test/reference.test.ts
Nicolas b49ef5bb40
Some checks failed
CI / lint (push) Successful in 1m37s
CI / intent-service-test (push) Successful in 5m11s
CI / build (push) Successful in 1m9s
CI / e2e (push) Failing after 7m13s
CI / test (push) Failing after 18m46s
CI / build-android (push) Has been skipped
feat(recipes): signaler une technique absente du catalogue (#130)
Calque le pattern « ingrédient hors-catalogue » sur `TechStep` : cinq
colonnes additives (`isPlaceholder`/`displayName`/`createdById`/`createdAt`/
`reviewedAt`), migration purement additive, FK `createdById → user_profiles`
en `SET NULL`.

Correction d'étape : `submitTechStepCorrectionSchema` accepte
`correctedTechStepName` (mutuellement exclusif de `correctedTechStepId`) ;
`submitTechStepCorrection` crée une `TechStep` placeholder dans la
transaction et poursuit comme si son id avait été fourni. La correction
s'applique immédiatement (`source: manual`) ; nouvel event analytics
`tech_step.placeholder_proposed`.

`TechStepView` gagne `isPlaceholder`/`displayName` ; `GET
/reference/tech-steps` exclut les placeholders. Nouveau helper web
`techStepLabel` (`displayName ?? t(catalog.techSteps.<key>)`), utilisé par
`StepDescription` et le popover. Le popover a une échappatoire « Ma
technique n'est pas dans la liste » (champ libre).

Admin : `admin-tech-step-catalog.service.ts` + routes
`GET/PATCH /admin/api/catalog/tech-step-placeholders` (liste plate — les
propositions de technique sont rares) ; `CatalogPage` gagne un sélecteur
Ingrédients / Techniques.

Tests : Mocha (proposition placeholder, rejet id+nom, exclusion catalogue,
liste + mark-reviewed admin) + Cypress (composant popover, e2e admin
catalogue). Assertions `.techStep` existantes mises à jour pour les 2
nouveaux champs.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-09 15:40:35 +02:00

301 lines
11 KiB
TypeScript

import { expect } from "chai";
import request from "supertest";
import { createApp } from "../src/app.js";
import { prisma } from "../src/db/prisma.js";
import { syncRecipeSources } from "../src/db/recipe-source-sync.js";
import {
seedReferenceData,
TECH_STEPS,
TOUR_SCENARIOS,
UTENSILS,
} from "../src/db/reference-seed-data.js";
import type { RecipeSourceAdapter } from "../src/lib/recipe-sources/recipe-source-adapter.js";
import {
clearRecipeSources,
registerRecipeSource,
} from "../src/lib/recipe-sources/recipe-source-registry.js";
import { resetDatabase } from "../test-support/reset-db.js";
/** A minimal `RecipeSourceAdapter` — only `key`/`name`/`official`/`iconUrl` matter for `syncRecipeSources` fixtures here. */
function buildFakeAdapter(
key: string,
name: string,
official: boolean,
iconUrl: string | null = null,
): RecipeSourceAdapter {
return {
key,
name,
official,
iconUrl,
locale: "fr",
async list() {
return { items: [], nextCursor: null };
},
async fetchDetail() {
throw new Error("not implemented");
},
parse() {
throw new Error("not implemented");
},
};
}
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("vegetarian");
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("peanuts");
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 = (key: string) => res.body.find((a: { key: string }) => a.key === key);
expect(byKey("gluten").kind).to.equal("INTOLERANCE");
expect(byKey("sulfites").kind).to.equal("INTOLERANCE");
expect(byKey("peanuts").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("tomato");
expect(res.body[0]).to.have.keys([
"id",
"key",
"icon",
"category",
"subcategory",
"reproducible",
"allergens",
"diets",
"isPlaceholder",
"displayName",
]);
});
it("resolves each ingredient's linked allergens, empty for one with none", async () => {
const res = await request(app).get("/reference/ingredients");
const byKey = (key: string) => res.body.find((i: { key: string }) => i.key === key);
expect(byKey("egg").allergens.map((a: { key: string }) => a.key)).to.include("eggs");
expect(byKey("tomato").allergens).to.deep.equal([]);
});
});
describe("GET /reference/units", () => {
it("returns the seeded units, no session required", async () => {
const res = await request(app).get("/reference/units");
expect(res.status).to.equal(200);
expect(res.body).to.have.length(17);
expect(res.body.map((u: { key: string }) => u.key)).to.include("gram");
expect(res.body[0]).to.have.keys(["id", "key", "type", "toBaseFactor"]);
});
it("resolves MASS/VOLUME toBaseFactor against their type's base unit, COUNT units all at 1", async () => {
const res = await request(app).get("/reference/units");
const byKey = (key: string) => res.body.find((u: { key: string }) => u.key === key);
expect(byKey("gram")).to.include({ type: "MASS", toBaseFactor: 1 });
expect(byKey("kilogram")).to.include({ type: "MASS", toBaseFactor: 1000 });
expect(byKey("liter")).to.include({ type: "VOLUME", toBaseFactor: 1000 });
expect(byKey("piece")).to.include({ type: "COUNT", toBaseFactor: 1 });
expect(byKey("pinch")).to.include({ type: "COUNT", toBaseFactor: 1 });
expect(byKey("cup")).to.include({ type: "VOLUME", toBaseFactor: 236.5882 });
expect(byKey("ounce")).to.include({ type: "MASS", toBaseFactor: 28.3495 });
expect(byKey("pound")).to.include({ type: "MASS", toBaseFactor: 453.5924 });
});
});
describe("GET /reference/tech-steps", () => {
it("returns the seeded techniques, no session required", async () => {
const res = await request(app).get("/reference/tech-steps");
expect(res.status).to.equal(200);
// `TECH_STEPS.length` (reference-seed-data.ts), not a hardcoded
// number — this catalog has grown since (26 -> 74) and will again.
expect(res.body).to.have.length(TECH_STEPS.length);
expect(res.body.map((t: { key: string }) => t.key)).to.include("simmer");
expect(res.body[0]).to.have.keys(["id", "key", "isPlaceholder", "displayName"]);
expect(res.body.every((t: { isPlaceholder: boolean }) => t.isPlaceholder === false)).to.equal(
true,
);
});
it("excludes placeholder techniques users proposed while correcting a step (issue #130)", async () => {
await prisma.techStep.create({
data: {
key: "placeholder:test-1",
isPlaceholder: true,
displayName: "Flamber au chalumeau",
},
});
const res = await request(app).get("/reference/tech-steps");
expect(res.status).to.equal(200);
expect(res.body).to.have.length(TECH_STEPS.length);
expect(
res.body.some(
(t: { displayName: string | null }) => t.displayName === "Flamber au chalumeau",
),
).to.equal(false);
});
it("orders techniques alphabetically by key", async () => {
const res = await request(app).get("/reference/tech-steps");
const keys = res.body.map((t: { key: string }) => t.key);
expect(keys).to.deep.equal([...keys].sort());
});
it("reseeding is idempotent — no duplicate techniques", async () => {
// resetDatabase already seeded once in beforeEach; seed a second time
// on top of that without truncating, the way a redeploy would.
await seedReferenceData(prisma);
const res = await request(app).get("/reference/tech-steps");
expect(res.body).to.have.length(TECH_STEPS.length);
});
});
describe("GET /reference/utensils", () => {
it("returns the seeded utensils, no session required", async () => {
const res = await request(app).get("/reference/utensils");
expect(res.status).to.equal(200);
expect(res.body).to.have.length(UTENSILS.length);
expect(res.body.map((u: { key: string }) => u.key)).to.include("pan");
expect(res.body[0]).to.have.keys(["id", "key"]);
});
it("orders utensils alphabetically by key", async () => {
const res = await request(app).get("/reference/utensils");
const keys = res.body.map((u: { key: string }) => u.key);
expect(keys).to.deep.equal([...keys].sort());
});
it("reseeding is idempotent — no duplicate utensils", async () => {
await seedReferenceData(prisma);
const res = await request(app).get("/reference/utensils");
expect(res.body).to.have.length(UTENSILS.length);
});
});
describe("GET /reference/sources", () => {
afterEach(() => {
clearRecipeSources();
});
it("is empty until a concrete adapter is registered", async () => {
const res = await request(app).get("/reference/sources");
expect(res.status).to.equal(200);
expect(res.body).to.deep.equal([]);
});
it("returns every synced adapter, official flag and icon included, no session required", async () => {
registerRecipeSource(buildFakeAdapter("fakeSource", "Fake Source", false));
registerRecipeSource(
buildFakeAdapter(
"officialSource",
"Official Source",
true,
"https://example.test/icon.svg",
),
);
await syncRecipeSources(prisma);
const res = await request(app).get("/reference/sources");
expect(res.status).to.equal(200);
expect(res.body).to.have.length(2);
expect(res.body[0]).to.have.keys(["id", "key", "name", "official", "iconUrl"]);
const byKey = (key: string) => res.body.find((s: { key: string }) => s.key === key);
expect(byKey("fakeSource").official).to.equal(false);
expect(byKey("fakeSource").iconUrl).to.equal(null);
expect(byKey("officialSource").official).to.equal(true);
expect(byKey("officialSource").iconUrl).to.equal("https://example.test/icon.svg");
});
it("orders sources alphabetically by name", async () => {
registerRecipeSource(buildFakeAdapter("bSource", "Bravo", false));
registerRecipeSource(buildFakeAdapter("aSource", "Alpha", false));
await syncRecipeSources(prisma);
const res = await request(app).get("/reference/sources");
expect(res.body.map((s: { name: string }) => s.name)).to.deep.equal(["Alpha", "Bravo"]);
});
});
describe("GET /reference/tours", () => {
it("returns every seeded scenario with its steps in order", async () => {
const res = await request(app).get("/reference/tours");
expect(res.status).to.equal(200);
expect(res.body.map((t: { key: string }) => t.key)).to.have.members(
TOUR_SCENARIOS.map((s) => s.key),
);
const general = res.body.find((t: { key: string }) => t.key === "general");
expect(general.name).to.be.a("string").and.not.empty;
expect(general.steps).to.have.length(TOUR_SCENARIOS[0].steps.length);
expect(general.steps[0]).to.have.keys(["order", "route", "targetKey", "title", "content"]);
expect(general.steps.map((s: { order: number }) => s.order)).to.deep.equal(
general.steps.map((_: unknown, i: number) => i),
);
// First step of the general tour navigates to "/" and points at the
// planning nav link (see TOUR_SCENARIOS).
expect(general.steps[0].route).to.equal("/");
expect(general.steps[0].targetKey).to.equal("nav-planning");
});
it("excludes a disabled scenario", async () => {
await prisma.tourScenario.update({
where: { key: "techStepMetadata" },
data: { enabled: false },
});
const res = await request(app).get("/reference/tours");
const keys = res.body.map((t: { key: string }) => t.key);
expect(keys).to.include("general");
expect(keys).to.not.include("techStepMetadata");
});
});
});