Remplace l'unité texte libre de RecipeIngredient (max 20 caractères, "g"/"grammes"/"G"... jamais fiable à additionner) par une référence vers un nouveau catalogue Unit (id/key/type/toBaseFactor), même traitement que Diet/Allergy/Ingredient : GET /reference/units, seedé par reference-seed-data.ts (14 unités : gram/kilogram/milliliter/ centiliter/liter/tablespoon/teaspoon/piece/pinch/slice/clove/bunch/ sachet/sprig), sélectionnable uniquement via un <select> dans le formulaire recette (plus de saisie libre). `toBaseFactor` (combien d'unités de base — gramme pour MASS, millilitre pour VOLUME — vaut une unité) pose les bases d'une future fonctionnalité de conversion (ex. liste de courses additionnant "500g" + "0.5kg") sans construire cette fonctionnalité elle-même — les unités COUNT restent à toBaseFactor=1, non convertibles entre elles (une "pincée" n'est pas une fraction fixe d'une "gousse"). Migration : recipe_ingredient.unit → unit_id (FK), breaking change sans backfill assumé (pas de recette réelle en prod actuellement, voir commentaire de migration) — mêmes garde-fous service-side que ingredientId (404 UNIT_NOT_FOUND) et mêmes tests de couverture.
99 lines
3.8 KiB
TypeScript
99 lines
3.8 KiB
TypeScript
import { expect } from "chai";
|
|
import request from "supertest";
|
|
import { createApp } from "../src/app.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("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",
|
|
]);
|
|
});
|
|
|
|
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(14);
|
|
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 });
|
|
});
|
|
});
|
|
});
|