Le matching par regex ne généralisait jamais au-delà de son propre vocabulaire — une étape décrivant la fonte du beurre comme "jusqu'à ce que le beurre ait disparu dans la poêle" ne contient aucun verbe sur lequel une regex pourrait s'ancrer, alors que le sens est sans ambiguïté. Nouveau pipeline en 3 étapes (TechStepClassifierService, node-nlp 4.27.0 — la 5.x est encore alpha, non retenue) : 1. NER (entités enum) trouve les mentions candidates + leur position exacte, à partir de listes de synonymes (tech-step-training-data.ts) plutôt que de regex écrites à la main. ner.threshold: 1 (exact, après normalisation) — le défaut à 0.8 faisait matcher "faire" (verbe auxiliaire omniprésent) contre "frire" par pure proximité de chaîne. 2. La description est découpée en clauses autour de ces candidats (splitIntoClauses, pure/testable sans modèle). 3. Le NlpManager classe chaque clause individuellement, entraîné sur des phrases qui n'emploient jamais le verbe de la technique — c'est ce qui apporte la compréhension du sens. En dessous de CONFIDENCE_THRESHOLD (0.65, ajusté empiriquement), retombe sur la technique impliquée par l'ancre NER plutôt que d'abandonner un match clairement ancré sur un mot-clé. TechStepMapping (table de regex par technique/locale) supprimée — migration 20260821130000_drop_tech_step_mapping — plus aucune table n'est interrogée à l'exécution, les données de matching vivent en code. TECH_STEPS (reference-seed-data.ts) simplifié en simple liste de uid, les mappings ayant disparu. Deux pièges trouvés en construisant ce pipeline, corrigés à la source : - db/prisma.ts construisait PrismaClient sans importer config/env.ts — un run de test isolé pouvait faire gagner la course au .env interne de Prisma (dev) contre .env.test. Fixé en important config/env.js en tout premier, pour effet de bord. - NlpManager a autoSave/autoLoad: true par défaut — persiste le modèle entraîné dans model.nlp et le recharge au lieu de ré-entraîner au prochain démarrage. Les deux désactivés explicitement (sinon un modèle obsolète masquerait silencieusement toute mise à jour du corpus/seuil) ; model.nlp ajouté au .gitignore en garde-fou. apps/api/src/db/prisma.ts, recipe.service.ts, sources.service.ts et recipe-translation.ts adaptés à la matching async (le classifieur entraîné remplace le couple loadTechStepMappingRules+matchTechStepSpans synchrone) ; server.ts appelle techStepClassifier.warmUp() avant d'accepter du trafic (le tout premier appel réel à NlpManager.process() charge les ressources par langue de node-nlp, plusieurs secondes). Vérifié : tsc --noEmit, biome check (0 erreur), build complet des 6 packages, 308 tests API (dont un test-support/reset-db.ts corrigé — référençait encore tech_step_mapping dans son TRUNCATE). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
208 lines
7.7 KiB
TypeScript
208 lines
7.7 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 } 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",
|
|
]);
|
|
});
|
|
|
|
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);
|
|
expect(res.body).to.have.length(26);
|
|
expect(res.body.map((t: { key: string }) => t.key)).to.include("simmer");
|
|
expect(res.body[0]).to.have.keys(["id", "key"]);
|
|
});
|
|
|
|
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(26);
|
|
});
|
|
});
|
|
|
|
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"]);
|
|
});
|
|
});
|
|
});
|