batchCooking/apps/api/test/recipe-translation.test.ts
Nicolas 89722ba790 feat(recipes): matching anglais pour les tech steps et les ingrédients
- Ajoute une expression régulière anglaise à chacun des 26 TechStepMapping
  du catalogue (locale "en"), en plus du "fr" existant — les recettes en
  anglais (TheMealDB, etc.) peuvent désormais matcher leurs étapes.
- Nouveau apps/api/src/lib/ingredient-matcher.ts : moteur de matching pur
  (nom d'ingrédient, unité, quantité) contre les catalogues Ingredient/Unit,
  à partir de labels anglais écrits à la main (packages/shared/src/data/
  catalog-labels-en.ts — 546 INGREDIENT_LABELS_EN + 17 UNIT_LABELS_EN avec
  synonymes/abréviations). Tokenise et stem naïvement les deux côtés pour
  tolérer pluriels et mots descriptifs superflus ; la correspondance la
  plus spécifique (le plus de mots) l'emporte en cas de recoupement.
- extractQuantity() : lit un nombre en tête de texte libre (entier,
  décimal, fraction simple ou nombre mixte) pour déduire la quantité et
  l'unité quand la source ne les fournit pas séparément.
- Étend recipe-translation.ts : translateRecipe(recipe, locale) résout
  aussi ingredientId/unitId/quantity de chaque ligne d'ingrédient — mais
  uniquement pour locale "en" (seules langue avec des labels), pour ne pas
  interroger la base inutilement ni halluciner un match dans une autre
  langue.
- Ajoute cup/ounce/pound au catalogue Unit (toBaseFactor réel), absents
  jusqu'ici alors que très fréquents dans les recettes anglaises.
- Vérifié en conditions réelles contre TheMealDB (Teriyaki Chicken
  Casserole) : 8/9 ingrédients résolus avec la bonne quantité/unité, le
  seul raté ("stir-fry vegetables") étant un mélange sans entrée dédiée au
  catalogue — dégradation gracieuse (unitId/ingredientId: null) comme prévu.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-20 13:10:00 +02:00

326 lines
12 KiB
TypeScript

import { expect } from "chai";
import { prisma } from "../src/db/prisma.js";
import type { IngredientMatchEntry, UnitMatchEntry } from "../src/lib/ingredient-matcher.js";
import type { ParsedRecipe, ParsedRecipeIngredient } from "../src/lib/recipe-source-adapter.js";
import {
translateRecipe,
translateRecipeIngredients,
translateRecipeSteps,
} from "../src/lib/recipe-translation.js";
import type { TechStepMappingRule } from "../src/lib/tech-step-matcher.js";
import { resetDatabase } from "../test-support/reset-db.js";
/** A minimal fixture `ParsedRecipe` — only `steps` (built from `descriptions`) matters for most tests here, the rest is filler to prove it survives translation untouched. */
function buildParsedRecipe(descriptions: string[]): ParsedRecipe {
return {
name: "Test Recipe",
description: "A recipe for testing",
picture: "https://example.test/recipe.jpg",
portions: 4,
sourceUrl: "https://example.test/recipes/1",
ingredients: [{ rawText: "1 egg", quantity: null, unit: null, name: "egg" }],
steps: descriptions.map((description, i) => ({
description,
picture: i === 0 ? "https://example.test/step1.jpg" : null,
})),
};
}
describe("recipe-translation", () => {
describe("translateRecipeSteps", () => {
const simmer: TechStepMappingRule = {
techStepId: 1,
expression: "\\bmijot(er|ez|e|ant|é)\\b",
weight: 15,
};
const preheat: TechStepMappingRule = {
techStepId: 2,
expression: "\\bpr[ée]chauff(er|ez|é|ée)\\b",
weight: 20,
};
const melt: TechStepMappingRule = {
techStepId: 3,
expression: "\\bfaire fondre\\b|\\bfaites fondre\\b",
weight: 15,
};
it("declares each step's technique sequence, preserving order", () => {
const recipe = buildParsedRecipe([
"Préchauffer la poêle, puis faire fondre le beurre",
"Servir immédiatement",
"Faire mijoter à feu doux",
]);
const translated = translateRecipeSteps(recipe, [simmer, preheat, melt]);
expect(translated.steps.map((step) => step.techStepIds)).to.deep.equal([[2, 3], [], [1]]);
});
it("leaves description/picture untouched on each step", () => {
const recipe = buildParsedRecipe(["Faire mijoter à feu doux", "Servir"]);
const translated = translateRecipeSteps(recipe, [simmer]);
expect(translated.steps[0]).to.deep.equal({
description: "Faire mijoter à feu doux",
picture: "https://example.test/step1.jpg",
techStepIds: [1],
});
expect(translated.steps[1]).to.deep.equal({
description: "Servir",
picture: null,
techStepIds: [],
});
});
it("passes every other field through unchanged", () => {
const recipe = buildParsedRecipe(["Servir"]);
const translated = translateRecipeSteps(recipe, []);
expect(translated.name).to.equal(recipe.name);
expect(translated.description).to.equal(recipe.description);
expect(translated.picture).to.equal(recipe.picture);
expect(translated.portions).to.equal(recipe.portions);
expect(translated.sourceUrl).to.equal(recipe.sourceUrl);
});
it("stubs every ingredient's ingredientId/unitId to null, leaving the rest of it untouched — actual ingredient matching is translateRecipeIngredients' job", () => {
const recipe = buildParsedRecipe(["Servir"]);
const translated = translateRecipeSteps(recipe, []);
expect(translated.ingredients).to.deep.equal([
{ ...recipe.ingredients[0], ingredientId: null, unitId: null },
]);
});
it("gives every step an empty sequence when there are no mappings at all", () => {
const recipe = buildParsedRecipe(["Faire mijoter à feu doux", "Préchauffer le four"]);
const translated = translateRecipeSteps(recipe, []);
expect(translated.steps.map((step) => step.techStepIds)).to.deep.equal([[], []]);
});
it("handles a recipe with no steps without error", () => {
const recipe = buildParsedRecipe([]);
const translated = translateRecipeSteps(recipe, [simmer]);
expect(translated.steps).to.deep.equal([]);
});
});
describe("translateRecipeIngredients", () => {
const tomato: IngredientMatchEntry = { ingredientId: 1, label: "Tomato" };
const chicken: IngredientMatchEntry = { ingredientId: 2, label: "Chicken" };
const chickenBreast: IngredientMatchEntry = { ingredientId: 3, label: "Chicken breast" };
const onion: IngredientMatchEntry = { ingredientId: 4, label: "Onion" };
const gram: UnitMatchEntry = { unitId: 10, synonyms: ["g", "gram", "grams"] };
const cup: UnitMatchEntry = { unitId: 11, synonyms: ["cup", "cups"] };
function buildIngredient(
overrides: Partial<ParsedRecipeIngredient> & { rawText: string; name: string },
): ParsedRecipeIngredient {
return { quantity: null, unit: null, ...overrides };
}
it("resolves ingredientId from free-text name, tolerating extra descriptive words and plurals", () => {
const translated = translateRecipeIngredients(
[
buildIngredient({
rawText: "2 large diced yellow onions",
name: "large diced yellow onions",
}),
],
[tomato, chicken, chickenBreast, onion],
[],
);
expect(translated[0].ingredientId).to.equal(onion.ingredientId);
});
it("prefers the more specific multi-word label over a shorter one it contains", () => {
const translated = translateRecipeIngredients(
[
buildIngredient({
rawText: "2 boneless chicken breasts",
name: "boneless chicken breasts",
}),
],
[tomato, chicken, chickenBreast, onion],
[],
);
expect(translated[0].ingredientId).to.equal(chickenBreast.ingredientId);
});
it("returns a null ingredientId when nothing in the catalog matches", () => {
const translated = translateRecipeIngredients(
[buildIngredient({ rawText: "1 mango", name: "mango" })],
[tomato, chicken, chickenBreast, onion],
[],
);
expect(translated[0].ingredientId).to.equal(null);
});
it("extracts a mixed-number quantity and unit from rawText when the source left them null", () => {
const translated = translateRecipeIngredients(
[buildIngredient({ rawText: "1 1/2 cups chicken breast", name: "chicken breast" })],
[chickenBreast],
[cup],
);
expect(translated[0].quantity).to.equal(1.5);
expect(translated[0].unitId).to.equal(cup.unitId);
});
it("trusts the source's own quantity/unit over re-deriving them from rawText", () => {
const translated = translateRecipeIngredients(
[
buildIngredient({
rawText: "some raw text that happens to mention cups",
name: "tomato",
quantity: 3,
unit: "g",
}),
],
[tomato],
[gram, cup],
);
expect(translated[0].quantity).to.equal(3);
expect(translated[0].unitId).to.equal(gram.unitId);
});
it("leaves quantity/unitId null when rawText has neither a leading number nor a recognizable unit", () => {
const translated = translateRecipeIngredients(
[buildIngredient({ rawText: "salt to taste", name: "salt" })],
[],
[],
);
expect(translated[0].quantity).to.equal(null);
expect(translated[0].unitId).to.equal(null);
});
it("leaves rawText/name/description untouched", () => {
const ingredient = buildIngredient({ rawText: "1 cup onions", name: "onions" });
const translated = translateRecipeIngredients([ingredient], [onion], [cup]);
expect(translated[0].rawText).to.equal(ingredient.rawText);
expect(translated[0].name).to.equal(ingredient.name);
});
});
describe("translateRecipe", () => {
beforeEach(async () => {
await resetDatabase();
});
after(async () => {
await prisma.$disconnect();
});
it("resolves real TechStep ids from the seeded French catalog", async () => {
const simmer = await prisma.techStep.findFirstOrThrow({ where: { key: "simmer" } });
const chop = await prisma.techStep.findFirstOrThrow({ where: { key: "chop" } });
const recipe = buildParsedRecipe(["Hacher les oignons", "Faire mijoter à feu doux"]);
const translated = await translateRecipe(recipe, "fr");
expect(translated.steps.map((step) => step.techStepIds)).to.deep.equal([
[chop.id],
[simmer.id],
]);
});
it("finds nothing for English text against the French catalog — locales are separate rule sets, never mixed", async () => {
// A step lifted verbatim from a real TheMealDB recipe.
const recipe = buildParsedRecipe([
"Bring a large saucepan of salted water to the boil",
"Chop the onions finely",
]);
const translated = await translateRecipe(recipe, "fr");
expect(translated.steps.map((step) => step.techStepIds)).to.deep.equal([[], []]);
});
it("resolves real TechStep ids from the seeded English catalog", async () => {
const boil = await prisma.techStep.findFirstOrThrow({ where: { key: "boil" } });
const chop = await prisma.techStep.findFirstOrThrow({ where: { key: "chop" } });
// Same two steps as the French/English mismatch test above, this
// time matched against the matching-language catalog.
const recipe = buildParsedRecipe([
"Bring a large saucepan of salted water to the boil",
"Chop the onions finely",
]);
const translated = await translateRecipe(recipe, "en");
expect(translated.steps.map((step) => step.techStepIds)).to.deep.equal([
[boil.id],
[chop.id],
]);
});
it("finds nothing for a locale with no mappings at all", async () => {
const recipe = buildParsedRecipe(["Faire mijoter à feu doux"]);
const translated = await translateRecipe(recipe, "de");
expect(translated.steps.map((step) => step.techStepIds)).to.deep.equal([[]]);
});
it("resolves real Ingredient/Unit ids from the seeded English catalog for an 'en' translation", async () => {
const onion = await prisma.ingredient.findFirstOrThrow({ where: { key: "onion" } });
const cup = await prisma.unit.findFirstOrThrow({ where: { key: "cup" } });
const recipe: ParsedRecipe = {
...buildParsedRecipe(["Chop the onions finely"]),
ingredients: [
{ rawText: "1 cup onions, chopped", quantity: null, unit: null, name: "onions" },
],
};
const translated = await translateRecipe(recipe, "en");
expect(translated.ingredients).to.deep.equal([
{
rawText: "1 cup onions, chopped",
quantity: 1,
unit: null,
name: "onions",
ingredientId: onion.id,
unitId: cup.id,
},
]);
});
it("leaves ingredients untouched (no quantity extraction either) for a non-English locale — no matching data exists yet, and the DB isn't even queried for it", async () => {
const recipe: ParsedRecipe = {
...buildParsedRecipe(["Faire mijoter à feu doux"]),
ingredients: [
{ rawText: "1 cup onions, chopped", quantity: null, unit: null, name: "onions" },
],
};
const translated = await translateRecipe(recipe, "fr");
expect(translated.ingredients).to.deep.equal([
{
rawText: "1 cup onions, chopped",
quantity: null,
unit: null,
name: "onions",
ingredientId: null,
unitId: null,
},
]);
});
});
});