* fix(recipes): corrige plusieurs bugs d'import TheMealDB
- Les instructions TheMealDB numérotées sur leur propre ligne ("1\n\ntexte...\n\n2\n\ntexte...") créaient des étapes parasites ne contenant qu'un chiffre — filtrées désormais (#52).
- Un ingrédient compté sans mot d'unité dans le texte source (ex. "4 Egg Yolks") laissait l'import bloqué sur "Importer" indéfiniment, sans indication visuelle de la ligne en cause — matchUnit retombe maintenant sur l'unité générique "piece" quand une quantité a été extraite, et RecipeImportForm/RecipeFormPage surlignent désormais toute ligne dont l'unité manque, avec un message explicite (#53).
- Ajout de INGREDIENT_LABEL_SYNONYMS_EN pour reconnaître des formulations alternatives fréquentes chez les sources anglophones ("vanilla pod" en plus de "vanilla bean") sans élargir INGREDIENT_LABELS_EN à un tableau pour ses ~550 entrées (#54).
- Effet de bord découvert en vérifiant #53 de bout en bout : deux lignes source résolues vers le même ingrédient catalogue (ex. "Egg Yolks"/"Eggs" -> "Œuf") faisaient planter la création en 500 (contrainte unique recipe_id+ingredient_id) au lieu d'un 400 propre. createRecipeSchema rejette maintenant les ingredientId en double, et le formulaire d'import surligne les doublons avant même de soumettre.
Vérifié de bout en bout dans le navigateur (import réel de la recette "Flan" depuis TheMealDB, jusqu'au planning) en plus des tests ajoutés.
Closes #52, #53, #54
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fix(layout): la sidebar réduite écrasait la barre mobile
`isCollapsed` (rail icône seule sur desktop) persiste dans localStorage
indépendamment de la largeur de fenêtre — un utilisateur ayant réduit la
sidebar sur desktop puis ouvrant la même session sur mobile (ou réduisant
la fenêtre sous 640px) gardait `.app-sidebar.collapsed` (spécificité
0,2,0 : width 4.25rem, flex-direction column), qui l'emportait sur la
règle mobile `@media (max-width: 640px)` (spécificité 0,1,0) censée passer
la sidebar en barre horizontale pleine largeur.
Le bloc `&.collapsed` est maintenant scopé sous `@media (min-width: 641px)`
— le complément exact du breakpoint mobile — donc il ne s'applique plus du
tout en dessous.
Vérifié dans le navigateur : sidebar collapsed=true dans localStorage,
viewport 375px — la sidebar calcule bien width: 375px / flex-direction:
row (barre horizontale pleine largeur) au lieu de 4.25rem/column.
Closes #27
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* docs(readme): documente GET /planning?date=, plus /planning/current
Le README documentait encore `GET /planning/current` (401 sans session,
couvre "aujourd'hui"), une route qui n'existe plus — `planning.routes.ts`
ne définit que `GET /planning?date=YYYY-MM-DD` depuis l'introduction de la
grille de semaine complète. Sans session, `/planning/current` renvoie un
404 générique (route inexistante), pas le 401 documenté.
Documente aussi POST/DELETE /planning/items au passage, absents jusqu'ici.
Closes #55
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* docs: met à jour README et specs/ avec l'état réel du code
Le code avait beaucoup évolué depuis la dernière mise à jour de la
documentation (sources externes, import de recettes, planning en
grille, pages de paramètres, thème, tests Cucumber...) sans que
README.md/specs/*.md ne suivent. Tour complet du code (backend +
frontend) et réécriture :
- specs/batch-cooking-modele.md : schéma de données réécrit depuis
schema.prisma (foyer/admin/invitation, sources, catalogue
ingrédients/unités, techniques détectées, visibilité des recettes).
- specs/backend-architecture.md : foyer, préférences/goûts, planning,
référence, sources externes (adaptateurs/registre/sync), matching
ingrédients/techniques, isolation base de test, suppression de compte.
- specs/frontend-architecture.md : routing complet, sidebar/paramètres,
thème, planning + picker, catalogue + import, composants UI partagés,
tests Cypress+Cucumber.
- specs/batch-cooking-architecture.md : module Import passe de TODO à
implémenté.
- specs/error-handling.md : liste complète des ~19 codes d'erreur.
- README.md : réécriture pour refléter tout ce qui précède, plus la
note (dangereusement obsolète) sur le partage base de test/dev — le
fix existe déjà (apps/api/.env.test), la doc décrivait encore le bug.
* feat(ingredients): ajoute jaune/blanc d'oeuf, coriandre en poudre, viandes hachées
Complète le catalogue d'ingrédients de référence (seed data) :
- jaune d'oeuf / blanc d'oeuf (dairyAndCheese/eggs, aux côtés d'"egg")
- coriandre en poudre (condimentsAndSpices/spices, aux côtés de
corianderSeeds/freshCilantro déjà présents)
- viandes hachées manquantes : veau, porc, agneau (meatAndSeafood/meats,
aux côtés de groundBeef déjà présent), dinde et poulet
(meatAndSeafood/poultry)
Libellés ajoutés dans apps/web/src/locales/fr/translation.json (source
d'affichage) et packages/shared/src/data/catalog-labels-en.ts (matching
anglais pour l'import de recettes depuis des sources comme TheMealDB).
Aucune icône ni régime dédiés : héritent des défauts de leur groupe
(EGG/SPICE/MEAT/POULTRY, mêmes dietUids que leurs groupes respectifs).
282 tests apps/api toujours au vert (resetDatabase() reseed le
catalogue à chaque test).
* fix(i18n): retire le œ ligaturé des libellés français de l'œuf
"Œuf"/"Œufs" (ingrédient, sous-catégorie, allergène) et "Jaune/Blanc
d'œuf" (ajoutés par #60) s'écrivaient avec le œ ligaturé — remplacé par
"oe" (deux lettres) partout où le mot apparaît. Ne touche pas "bœuf"
(mot différent, non concerné).
Le scénario Cucumber recipe-form.feature qui sélectionne l'ingrédient
par son libellé affiché est mis à jour en conséquence.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fix(recipes): concatène les ingrédients dupliqués à l'import
Suite au retour utilisateur sur #53 (follow-up) : au lieu de bloquer
l'import et de demander à l'utilisateur de retirer une ligne en double
à la main, deux lignes source qui résolvent vers le même ingrédient
catalogue sont désormais fusionnées automatiquement, quantité
concaténée (sommée), avant même que l'écran de revue ne s'affiche.
- mergeDuplicateIngredients (recipe-translation.ts) : même unité des
deux côtés -> somme directe. Unité différente mais même UnitType
(MASS/VOLUME) -> conversion via toBaseFactor avant de sommer, exprimée
dans l'unité de la première ligne. UnitType différent, ou COUNT des
deux côtés (une "pincée" n'est pas une fraction fixe d'une "gousse",
cf. le commentaire de UnitView) -> jamais fusionnées, laissées en
double (createRecipeSchema/RecipeImportForm continuent de les
signaler, filet de sécurité déjà en place). Les lignes non résolues
(ingredientId: null) ne sont jamais fusionnées entre elles.
- rawText concaténé ("100g Sugar + 45g Sugar") pour la traçabilité.
- Branché dans previewSourceItem (sources.service.ts), juste après
translateRecipeIngredients — c'est le seul endroit où des doublons
peuvent apparaître (la création manuelle ne peut pas en produire,
IngredientPicker exclut déjà les ingrédients déjà sélectionnés).
Vérifié via l'API en local (import réel de "Flan" depuis TheMealDB) :
"100g Sugar"/"45g Sugar" -> une seule ligne Sucre, 145g.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* chore(lint): upgrade Biome vers 2.x, active noExplicitAny/noConsole/noFloatingPromises
`@biomejs/biome` passe de 1.9.4 à 2.5.9 (config migrée via `biome migrate
--write`) — nécessaire pour noFloatingPromises, une règle type-aware
apparue en 2.0 (nursery).
- noExplicitAny : déjà "recommended", actif depuis toujours, aucun changement.
- noConsole (biome.json) : bloque tout `console.*` sauf error/warn/info/
debug/table/assert — équivalent à "pas de console.log" sans interdire
les niveaux nommés (voir le nouveau log service dans le prochain commit,
qui centralise justement ces appels).
- noFloatingPromises (nursery) activé explicitement sous `rules.nursery`
sans avoir besoin d'activer le domaine "types" au sens large (ça aurait
aussi allumé des dizaines d'autres règles type-aware type
noUnresolvedImports/noUnnecessaryConditions, hors scope ici).
Le reste du diff, c'est soit du reformatage automatique (import sort, 2.x
ordonne différemment de 1.9.4 — `biome check --write --unsafe`), soit les
corrections des ~20 promesses flottantes que la nouvelle règle a fait
remonter :
- La plupart sont des `navigate(...)` non attendus (react-router v7 type
`navigate` en `void | Promise<void>`) — préfixés `void navigate(...)`,
aucun changement de comportement.
- Trois chargements initiaux en useEffect (OnboardingAllergensPage,
OnboardingDietPage, OnboardingHouseholdPage, HouseholdSettingsPage)
n'avaient jamais de `.catch()` du tout — ajouté (dégradation silencieuse
vers un état vide/par défaut, même raisonnement que le `.catch()` déjà
présent dans OnboardingSourcesPage).
- HouseholdSettingsPage : `loadHouse` était une fonction déclarée à chaque
render (donc une référence différente à chaque fois) utilisée comme
dépendance de useEffect ET passée en callback à des enfants — le
useEffect se re-déclenchait donc à chaque re-render provoqué par son
propre fetch, un vrai bug de boucle infinie de requêtes que
noFloatingPromises a fait remonter indirectement (via
useExhaustiveDependencies). Corrigé avec useCallback([]).
- RecipeDetailPanel : une clé de liste `${index}-...}` sur une liste
statique (draft.steps, sans id stable — DraftRecipeStepView n'en a pas)
— biome-ignore justifié, pas de bug réel.
- recipe.test.ts : variable `agent` non utilisée, retirée.
Vérifié : `pnpm --filter api test` (295/295), `pnpm lint` et `pnpm build`
clean sur tout le repo.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* feat(api): ajoute un log service pour les logs de fonctionnement côté serveur
Jusqu'ici, rien ne journalisait quoi que ce soit côté serveur : aucune
trace au démarrage à part un console.log ad hoc, et surtout aucune trace
des requêtes ni des erreurs gérées par ErrorHandlerService — un 500 en
production n'aurait laissé aucune trace exploitable.
- LoggerService (apps/api/src/lib/logger.service.ts) — classe (public
debug/info/warn/error, private emit), même convention que
ErrorHandlerService (packages/error-tools) : instance unique partagée
exportée (`export const logger = new LoggerService()`). Émet une ligne
JSON structurée par appel (timestamp/level/message + meta), filtrée par
seuil selon NODE_ENV (debug complet en dev, warn+ pendant les tests
pour ne pas alourdir la sortie de Mocha, info+ en production). Seul
endroit du code autorisé à toucher `console` directement (biome-ignore
justifié), toujours via une méthode nommée — jamais un console.log nu.
- requestLogger (middlewares/request-logger.ts) — une ligne par requête
terminée (méthode/chemin/statut/durée), montée en tout premier dans
app.ts, avant même setupCore (CORS/JSON/cookies), pour englober tout le
pipeline. Niveau déduit du statut (info/warn/error).
- errorLogger (middlewares/error-logger.ts) — monté juste avant
createErrorMiddleware : réutilise errorHandlerService.handle() (pur/
sans effet de bord) pour classifier l'erreur avant que la vraie réponse
ne soit construite, log en warn les 4xx routiniers (validation, 404,
401...) et en error les 5xx/exceptions non prévues (avec la stack).
- error-handler.service.ts : retire le `console.error(error)` ad hoc de
fromUnknownError — errorLogger voit désormais chaque erreur avant que
ce service ne la mappe, donc ce console.error faisait doublon (et
loggait en texte brut, pas en JSON structuré).
- server.ts : le console.log de démarrage passe par logger.info.
Vérifié : pnpm --filter api test (303/303, dont 8 nouveaux tests sur
LoggerService), pnpm lint/build clean, testé en live (pnpm dev:api +
curl) — logs JSON corrects pour un 200, un 404, un 401.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* style: préfixe tous les membres private/protected par _
Convention demandée par l'utilisateur : `emit` -> `_emit`, sur toutes les
classes du repo, pas seulement le nouveau code. `public` reste sans
préfixe.
- LoggerService (apps/api) : _minSeverity, _emit.
- ApiClient (apps/web) : _request (39 sites d'appel mis à jour).
- ErrorHandlerService (packages/error-tools) : _fromZodError,
_fromHttpError, _fromUnknownError.
- ExpressServer (packages/express-tools) : _app, _registeredRoutes.
Aucun changement de comportement — pur renommage interne, aucune méthode
private/protected n'était appelée depuis l'extérieur de sa classe.
Vérifié : pnpm --filter api test (303/303), pnpm lint/build clean sur
tout le repo (apps/api, apps/web, packages/*).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* docs(specs): documente les conventions de développement du repo
Nouveau specs/dev-conventions.md — jusqu'ici ces règles n'existaient que
dans l'historique de commits/PR (classes vs objets littéraux pour la
logique de service, préfixe _ sur private/protected, règles Biome
actives, log service, tests sans mocks de la DB, conventions git/PR...),
rien de centralisé pour un futur contributeur (humain ou Claude Code).
Référencé depuis README.md, section "Qualité / Tests".
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* refactor(web): regroupe pages/ par section au lieu d'un dossier à plat
pages/ mélangeait 8 fichiers directement à sa racine (LoginPage,
SignupPage, PlanningPage+scss, RecipesPage, RecipeFormPage,
ImportRecipePage, ShoppingListPage, ComingSoonPage+scss) à côté de deux
sous-dossiers déjà groupés (onboarding/, settings/) — incohérent, et
difficile à parcourir une fois le nombre de pages monté. Un sous-dossier
par section routée, même règle que onboarding/settings existants :
- pages/auth/ — LoginPage, SignupPage
- pages/planning/ — PlanningPage + planning-page.scss
- pages/recipes/ — RecipesPage, RecipeFormPage, ImportRecipePage
- pages/shopping-list/ — ShoppingListPage
ComingSoonPage (+ .scss) déménage vers components/ui/ — ce n'est pas une
page routée elle-même (ShoppingListPage l'enveloppe), c'est un composant
UI générique réutilisable, sa place est aux côtés de Dialog/Tooltip/etc.,
pas dans pages/.
Chemins relatifs internes de chaque fichier déplacé mis à jour (un niveau
de profondeur en plus), imports dans App.tsx repointés, tri Biome
réappliqué. specs/frontend-architecture.md mis à jour (arborescence +
références de chemin).
Vérifié : pnpm build clean (apps/web, 1952 modules), pnpm lint clean sur
tout le repo, testé en live dans le navigateur (login/signup, planning,
recettes, nouvelle recette, liste de courses, paramètres) — aucune route
cassée.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* refactor(web): regroupe features/recipes/ par sous-domaine au lieu d'un dossier à plat
20 fichiers à plat -> badges/ (DietTagSelect, DietBadges, AllergenBadges,
ReproducibleBadge, FavoriteStarButton), ingredients/ (IngredientPicker,
IngredientRow, ingredient-icons), steps/ (StepListEditor, StepDescription,
highlight-tech-steps), sources/ (RecipeSourcesPanel, SourceItemTable,
RecipeImportForm, recipe-import-draft, useEnabledSources).
RecipeTable/RecipeTabs/RecipeDetailPanel et recipes.scss restent à la
racine (composants transverses aux sous-dossiers, partagés par plusieurs
d'entre eux). Chemins relatifs corrigés dans les fichiers déplacés et chez
tous leurs importeurs externes (pages/recipes/*, features/planning/
RecipePickerDialog.tsx, features/profile/DislikedIngredientsField.tsx),
doc mise à jour (specs/frontend-architecture.md, specs/batch-cooking-
modele.md).
Vérifié : tsc --noEmit, biome check, build complet, 303 tests API,
vérification live navigateur (planning, /recettes, /recettes/nouvelle).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* refactor(api): regroupe lib/ par sous-domaine au lieu d'un dossier à plat
9 fichiers à plat -> recipe-sources/ (recipe-source-adapter, recipe-source-
errors, recipe-source-registry) et recipe-matching/ (recipe-translation,
ingredient-matcher, tech-step-matcher). jwt.ts, safe-profile.ts et
logger.service.ts restent à la racine de lib/ (pas de sous-domaine
partagé avec les autres).
Chemins relatifs corrigés dans les fichiers déplacés (profondeur +1 vers
db/) et chez tous leurs importeurs (modules/sources, modules/recipe,
sources/*, db/recipe-source-sync.ts, 12 fichiers de test), doc mise à
jour (specs/backend-architecture.md, specs/batch-cooking-architecture.md).
Vérifié : tsc --noEmit, biome check, build complet, 303 tests API.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* refactor(api): regroupe test/ par sous-domaine, miroir de src/lib/
18 fichiers à plat -> recipe-matching/ (ingredient-matcher, recipe-
translation, tech-step-matcher — miroir de lib/recipe-matching/),
recipe-sources/ (json-ld-recipe, recipe-source, recipe-source-sync,
the-meal-db — miroir de lib/recipe-sources/), sources/ (sources,
sources-index — module + registration src/sources/index.ts).
Les tests par domaine API sans regroupement naturel (auth, health,
house, logger.service, planning, preferences, profile, recipe,
reference) restent à la racine de test/, un fichier par domaine — même
logique que jwt.ts/safe-profile.ts restés à la racine de lib/.
Chemins relatifs corrigés (../src/ -> ../../src/, ../test-support/ ->
../../test-support/ dans les fichiers déplacés qui appellent
resetDatabase). .mocharc.json ("test/**/*.test.ts") couvre déjà les
sous-dossiers, aucun changement de config nécessaire.
Vérifié : biome check, 303 tests API.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* feat(convention): impose try/catch autour de chaque await/corps async
Nouvelle règle de dev : aucun await nu, et un corps de fonction/méthode
async doit intégralement vivre dans un try/catch (pas seulement la ou
les lignes qui awaitent). Documentée dans specs/dev-conventions.md avec
son périmètre (code applicatif — services/hooks/composants/middlewares
— routes *.routes.ts exemptées car déjà couvertes par
wrapAsyncHandler ; tests et scripts one-off exemptés aussi).
Appliqué rétroactivement à tout le code applicatif qui ne l'était pas
déjà :
- api : auth/house/profile/preferences/planning/reference/recipe/
sources .service.ts, recipe-source-sync.ts, recipe-translation.ts,
ingredient-matcher.ts, tech-step-matcher.ts, json-ld-recipe.ts,
the-meal-db.ts — un try/catch par fonction async, rethrow simple
(le middleware d'erreur logge déjà tout centralement, voir
error-logger.ts) sauf quand un catch avait déjà une logique propre
(ex. le retry de createHouse).
- web : api/client.ts (_request), AuthContext.tsx, ThemeContext.tsx,
AppLayout.tsx (handleLogout), HouseholdSettingsPage.tsx (handleCopy/
handleRemove/handleDelete/handleLeave) — la plupart des handlers de
formulaire avaient déjà ce pattern, seuls ceux qui laissaient un
await nu ont été corrigés.
lint/complexity/noUselessCatch désactivé dans biome.json (interdisait
justement le catch-qui-rethrow que cette convention impose).
Vérifié : tsc --noEmit (api+web), biome check (0 erreur, repo entier),
build complet, 303 tests API, vérification live navigateur (thème,
déconnexion, copie du code d'invitation).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fix(web): corrige l'import cassé de highlight-tech-steps.cy.tsx
Oubli lors du regroupement de features/recipes/ par sous-domaine
(refactor(web): regroupe features/recipes/...) : le déplacement de
highlight-tech-steps.ts vers features/recipes/steps/ n'avait pas été
répercuté dans ce test composant Cypress (hors de apps/web/src, donc
raté par la recherche de référence externe à l'époque) — faisait
planter le job e2e en CI ("Failed to fetch dynamically imported
module").
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
1020 lines
39 KiB
TypeScript
1020 lines
39 KiB
TypeScript
import type { SignupInput } from "@batch-cooking/shared";
|
|
import { ErrorCode } from "@batch-cooking/shared";
|
|
import { faker } from "@faker-js/faker";
|
|
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 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): RecipeSourceAdapter {
|
|
return {
|
|
key,
|
|
name,
|
|
official: false,
|
|
iconUrl: null,
|
|
locale: "fr",
|
|
async list() {
|
|
return { items: [], nextCursor: null };
|
|
},
|
|
async fetchDetail() {
|
|
throw new Error("not implemented");
|
|
},
|
|
parse() {
|
|
throw new Error("not implemented");
|
|
},
|
|
};
|
|
}
|
|
|
|
/** See `auth.test.ts` — generated rather than hardcoded, no test fixture looks like a real person's data. */
|
|
function buildSignupPayload(): SignupInput {
|
|
const firstName = faker.person.firstName();
|
|
const lastName = faker.person.lastName();
|
|
return {
|
|
firstName,
|
|
lastName,
|
|
email: faker.internet.email({ firstName, lastName }).toLowerCase(),
|
|
password: faker.internet.password({ length: 16 }),
|
|
};
|
|
}
|
|
|
|
/** Resolves a reference ingredient's id by its `reference-seed-data.ts` uid (also its DB `key`). */
|
|
async function ingredientId(key: string): Promise<number> {
|
|
const ingredient = await prisma.ingredient.findFirstOrThrow({ where: { key } });
|
|
return ingredient.id;
|
|
}
|
|
|
|
/** Resolves a reference unit's id by its `reference-seed-data.ts` uid (also its DB `key`) — same reasoning as {@link ingredientId}. */
|
|
async function unitId(key: string): Promise<number> {
|
|
const unit = await prisma.unit.findFirstOrThrow({ where: { key } });
|
|
return unit.id;
|
|
}
|
|
|
|
/** Resolves a reference tech step's id by its `reference-seed-data.ts` uid (also its DB `key`) — same reasoning as {@link ingredientId}. */
|
|
async function techStepId(key: string): Promise<number> {
|
|
const techStep = await prisma.techStep.findFirstOrThrow({ where: { key } });
|
|
return techStep.id;
|
|
}
|
|
|
|
/** A step's detected technique sequence, in order — mirrors `matchTechSteps`' return shape (`../src/lib/recipe-matching/tech-step-matcher.js`) so tests can assert on it directly. */
|
|
async function stepTechStepIds(stepId: number): Promise<number[]> {
|
|
const links = await prisma.stepTechStep.findMany({
|
|
where: { stepId },
|
|
orderBy: { order: "asc" },
|
|
select: { techStepId: true },
|
|
});
|
|
return links.map((link) => link.techStepId);
|
|
}
|
|
|
|
describe("Recipes", () => {
|
|
const app = createApp();
|
|
|
|
/** Signs up a fresh profile and returns both its session `agent` and profile id — most tests below need the id for `authorId` on directly-created fixture rows. */
|
|
async function signup(): Promise<{ agent: ReturnType<typeof request.agent>; profileId: number }> {
|
|
const agent = request.agent(app);
|
|
const res = await agent.post("/auth/signup").send(buildSignupPayload());
|
|
return { agent, profileId: res.body.id };
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
await resetDatabase();
|
|
});
|
|
|
|
after(async () => {
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
describe("GET /recipes", () => {
|
|
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
|
|
const res = await request(app).get("/recipes").query({ tab: "publique" });
|
|
|
|
expect(res.status).to.equal(401);
|
|
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
|
|
});
|
|
|
|
it("rejects a missing ?tab= with 400 VALIDATION_ERROR", async () => {
|
|
const { agent } = await signup();
|
|
|
|
const res = await agent.get("/recipes");
|
|
|
|
expect(res.status).to.equal(400);
|
|
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
|
|
});
|
|
|
|
it("returns an empty catalog when no recipe exists yet", async () => {
|
|
const { agent } = await signup();
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "publique" });
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body).to.deep.equal([]);
|
|
});
|
|
|
|
it("filters the catalog by name when ?search= is given", async () => {
|
|
const { agent, profileId } = await signup();
|
|
await prisma.recipe.create({
|
|
data: { name: "Ratatouille", authorId: profileId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
await prisma.recipe.create({
|
|
data: { name: "Tarte aux pommes", authorId: profileId, visibility: "PUBLIC", portions: 6 },
|
|
});
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "publique", search: "rata" });
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body.map((r: { name: string }) => r.name)).to.deep.equal(["Ratatouille"]);
|
|
});
|
|
|
|
it("perso tab only returns the viewer's own PERSONAL recipes", async () => {
|
|
const { agent, profileId } = await signup();
|
|
const { profileId: otherId } = await signup();
|
|
await prisma.recipe.create({ data: { name: "La mienne", authorId: profileId, portions: 4 } });
|
|
await prisma.recipe.create({
|
|
data: { name: "Pas la mienne", authorId: otherId, portions: 4 },
|
|
});
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "perso" });
|
|
|
|
expect(res.body.map((r: { name: string }) => r.name)).to.deep.equal(["La mienne"]);
|
|
});
|
|
|
|
it("foyer tab only returns HOUSE recipes authored within the viewer's current house", async () => {
|
|
const { agent, profileId } = await signup();
|
|
const houseRes = await agent.post("/house").send({ name: "Chez moi" });
|
|
const { profileId: otherId } = await signup();
|
|
const otherHouseRes = await request.agent(app).post("/house").send({ name: "Chez un autre" });
|
|
|
|
await prisma.recipe.create({
|
|
data: {
|
|
name: "Recette du foyer",
|
|
authorId: profileId,
|
|
visibility: "HOUSE",
|
|
authorHouseId: houseRes.body.id,
|
|
portions: 4,
|
|
},
|
|
});
|
|
await prisma.recipe.create({
|
|
data: {
|
|
name: "Recette d'un autre foyer",
|
|
authorId: otherId,
|
|
visibility: "HOUSE",
|
|
authorHouseId: otherHouseRes.body.id,
|
|
portions: 4,
|
|
},
|
|
});
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "foyer" });
|
|
|
|
expect(res.body.map((r: { name: string }) => r.name)).to.deep.equal(["Recette du foyer"]);
|
|
});
|
|
|
|
it("foyer tab is empty when the viewer has no household", async () => {
|
|
const { agent } = await signup();
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "foyer" });
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body).to.deep.equal([]);
|
|
});
|
|
|
|
it("favoris tab only returns recipes the viewer has favorited", async () => {
|
|
const { agent, profileId } = await signup();
|
|
const favorited = await prisma.recipe.create({
|
|
data: { name: "Favorite", authorId: profileId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
await prisma.recipe.create({
|
|
data: { name: "Pas favorite", authorId: profileId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
await agent.post(`/recipes/${favorited.id}/favorite`);
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "favoris" });
|
|
|
|
expect(res.body.map((r: { name: string }) => r.name)).to.deep.equal(["Favorite"]);
|
|
});
|
|
|
|
it("a PERSONAL recipe from another author is invisible in the publique tab", async () => {
|
|
const { agent } = await signup();
|
|
const { profileId: otherId } = await signup();
|
|
await prisma.recipe.create({ data: { name: "Secrète", authorId: otherId, portions: 4 } });
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "publique" });
|
|
|
|
expect(res.body).to.deep.equal([]);
|
|
});
|
|
|
|
describe("source visibility", () => {
|
|
afterEach(() => {
|
|
clearRecipeSources();
|
|
});
|
|
|
|
/** Registers+syncs a throwaway adapter and returns the `Source` row `syncRecipeSources` created for it. */
|
|
async function registerAndSyncSource(key: string) {
|
|
registerRecipeSource(buildFakeAdapter(key, key));
|
|
await syncRecipeSources(prisma);
|
|
return prisma.source.findUniqueOrThrow({ where: { key } });
|
|
}
|
|
|
|
it("always shows a manually-authored recipe, even for a household with nothing enabled", async () => {
|
|
const { agent, profileId } = await signup();
|
|
await agent.post("/house").send({ name: "Chez moi" });
|
|
await prisma.recipe.create({
|
|
data: { name: "Maison", authorId: profileId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "publique" });
|
|
expect(res.body.map((r: { name: string }) => r.name)).to.deep.equal(["Maison"]);
|
|
});
|
|
|
|
it("hides a sourced recipe until the viewer's household enables that source, then shows it", async () => {
|
|
const { agent, profileId } = await signup();
|
|
await agent.post("/house").send({ name: "Chez moi" });
|
|
const source = await registerAndSyncSource("fakeSource");
|
|
await prisma.recipe.create({
|
|
data: {
|
|
name: "Importée",
|
|
authorId: profileId,
|
|
visibility: "PUBLIC",
|
|
portions: 4,
|
|
sourceId: source.id,
|
|
externalId: "1",
|
|
},
|
|
});
|
|
|
|
const hidden = await agent.get("/recipes").query({ tab: "publique" });
|
|
expect(hidden.body).to.deep.equal([]);
|
|
|
|
const patchRes = await agent
|
|
.patch("/house/current/sources")
|
|
.send({ sourceIds: [source.id] });
|
|
expect(patchRes.status).to.equal(200);
|
|
|
|
const visible = await agent.get("/recipes").query({ tab: "publique" });
|
|
expect(visible.body.map((r: { name: string }) => r.name)).to.deep.equal(["Importée"]);
|
|
});
|
|
|
|
it("hides a disabled-source recipe in every tab, including the viewer's own perso and favoris", async () => {
|
|
const { agent, profileId } = await signup();
|
|
await agent.post("/house").send({ name: "Chez moi" });
|
|
const source = await registerAndSyncSource("fakeSource");
|
|
const recipe = await prisma.recipe.create({
|
|
data: {
|
|
name: "Ma recette importée",
|
|
authorId: profileId,
|
|
portions: 4,
|
|
sourceId: source.id,
|
|
externalId: "1",
|
|
},
|
|
});
|
|
await prisma.recipeFavorite.create({
|
|
data: { userProfileId: profileId, recipeId: recipe.id },
|
|
});
|
|
|
|
expect((await agent.get("/recipes").query({ tab: "perso" })).body).to.deep.equal([]);
|
|
expect((await agent.get("/recipes").query({ tab: "favoris" })).body).to.deep.equal([]);
|
|
|
|
await agent.patch("/house/current/sources").send({ sourceIds: [source.id] });
|
|
|
|
const persoAfter = await agent.get("/recipes").query({ tab: "perso" });
|
|
expect(persoAfter.body.map((r: { name: string }) => r.name)).to.deep.equal([
|
|
"Ma recette importée",
|
|
]);
|
|
const favorisAfter = await agent.get("/recipes").query({ tab: "favoris" });
|
|
expect(favorisAfter.body.map((r: { name: string }) => r.name)).to.deep.equal([
|
|
"Ma recette importée",
|
|
]);
|
|
});
|
|
|
|
it("hides every sourced recipe for a viewer with no household at all", async () => {
|
|
const { agent, profileId } = await signup();
|
|
const source = await registerAndSyncSource("fakeSource");
|
|
await prisma.recipe.create({
|
|
data: {
|
|
name: "Importée",
|
|
authorId: profileId,
|
|
visibility: "PUBLIC",
|
|
portions: 4,
|
|
sourceId: source.id,
|
|
externalId: "1",
|
|
},
|
|
});
|
|
|
|
const res = await agent.get("/recipes").query({ tab: "publique" });
|
|
expect(res.body).to.deep.equal([]);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("POST /recipes", () => {
|
|
it("creates a recipe with its ingredients, ordered steps and diet tags", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const oeuf = await ingredientId("egg");
|
|
const piece = await unitId("piece");
|
|
const vegetarien = await prisma.diet.findFirstOrThrow({
|
|
where: { key: "vegetarian" },
|
|
});
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Omelette provençale",
|
|
description: "Rapide et savoureuse",
|
|
portions: 2,
|
|
dietIds: [vegetarien.id],
|
|
ingredients: [
|
|
{ ingredientId: tomate, quantity: 2, unitId: piece },
|
|
{ ingredientId: oeuf, quantity: 3, unitId: piece },
|
|
],
|
|
steps: [{ description: "Battre les œufs" }, { description: "Ajouter les tomates" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(201);
|
|
expect(res.body.name).to.equal("Omelette provençale");
|
|
expect(res.body.portions).to.equal(2);
|
|
expect(res.body.ingredients).to.have.length(2);
|
|
expect(res.body.ingredients[0].unit.key).to.equal("piece");
|
|
expect(
|
|
res.body.steps.map((s: { description: string; order: number }) => s.order),
|
|
).to.deep.equal([0, 1]);
|
|
// Allergens aggregated across ingredients — "Œuf" carries "Œufs".
|
|
expect(res.body.allergens.map((a: { key: string }) => a.key)).to.include("eggs");
|
|
expect(res.body.diets.map((d: { key: string }) => d.key)).to.deep.equal(["vegetarian"]);
|
|
});
|
|
|
|
it("defaults to PERSONAL visibility, and stamps the author's current household", async () => {
|
|
const { agent } = await signup();
|
|
const houseRes = await agent.post("/house").send({ name: "Chez moi" });
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Test",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
|
|
expect(res.body.visibility).to.equal("PERSONAL");
|
|
// authorHouseId isn't in the API response, but the "foyer" tab
|
|
// proves it was stamped — a HOUSE recipe created next should show up.
|
|
const houseRecipe = await agent.post("/recipes").send({
|
|
name: "Foyer",
|
|
visibility: "HOUSE",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
const foyerRes = await agent.get("/recipes").query({ tab: "foyer" });
|
|
expect(foyerRes.body.map((r: { id: number }) => r.id)).to.include(houseRecipe.body.id);
|
|
expect(houseRes.body.id).to.be.a("number"); // house exists, sanity check
|
|
});
|
|
|
|
it("auto-detects a step's technique from its description and persists it", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const simmer = await techStepId("simmer");
|
|
const description = "Faire mijoter à feu doux pendant 30 minutes";
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Ragoût",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description }],
|
|
});
|
|
|
|
expect(res.status).to.equal(201);
|
|
const step = await prisma.step.findFirstOrThrow({ where: { recipeId: res.body.id } });
|
|
expect(await stepTechStepIds(step.id)).to.deep.equal([simmer]);
|
|
// Exposed via StepView too — the whole point of persisting start/end
|
|
// (see tech-step-matcher.ts's matchTechStepSpans) is that the API
|
|
// response itself carries exactly what to highlight, not just the id.
|
|
const resStep = res.body.steps[0];
|
|
expect(resStep.techSteps).to.have.length(1);
|
|
expect(resStep.techSteps[0].techStep).to.deep.equal({ id: simmer, key: "simmer" });
|
|
const { start, end } = resStep.techSteps[0];
|
|
expect(description.slice(start, end).toLowerCase()).to.equal("mijoter");
|
|
});
|
|
|
|
it("leaves a step's technique sequence empty when its description matches no known technique", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Test",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Servir immédiatement" }],
|
|
});
|
|
|
|
const step = await prisma.step.findFirstOrThrow({ where: { recipeId: res.body.id } });
|
|
expect(await stepTechStepIds(step.id)).to.deep.equal([]);
|
|
});
|
|
|
|
it("detects each step's technique(s) independently, preserving order", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const simmer = await techStepId("simmer");
|
|
const chop = await techStepId("chop");
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Ragoût",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [
|
|
{ description: "Hacher les oignons" },
|
|
{ description: "Servir immédiatement" },
|
|
{ description: "Faire mijoter à feu doux" },
|
|
],
|
|
});
|
|
|
|
expect(res.status).to.equal(201);
|
|
const steps = await prisma.step.findMany({
|
|
where: { recipeId: res.body.id },
|
|
orderBy: { order: "asc" },
|
|
});
|
|
expect(await Promise.all(steps.map((s) => stepTechStepIds(s.id)))).to.deep.equal([
|
|
[chop],
|
|
[],
|
|
[simmer],
|
|
]);
|
|
});
|
|
|
|
it("picks the more specific technique end-to-end when a description matches more than one", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const bake = await techStepId("bake");
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Gratin",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
// Matches both `cook` (weight 10) and `bake` (weight 25, "au four").
|
|
steps: [{ description: "Cuire au four pendant 30 minutes" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(201);
|
|
const step = await prisma.step.findFirstOrThrow({ where: { recipeId: res.body.id } });
|
|
expect(await stepTechStepIds(step.id)).to.deep.equal([bake]);
|
|
});
|
|
|
|
it("detects a sequence of several distinct techniques within a single step, in reading order", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const preheat = await techStepId("preheat");
|
|
const melt = await techStepId("melt");
|
|
const description = "Préchauffer la poêle, puis faire fondre le beurre";
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Poêlée",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
// The case that motivated the sequence model: one instruction, two techniques.
|
|
steps: [{ description }],
|
|
});
|
|
|
|
expect(res.status).to.equal(201);
|
|
const step = await prisma.step.findFirstOrThrow({ where: { recipeId: res.body.id } });
|
|
expect(await stepTechStepIds(step.id)).to.deep.equal([preheat, melt]);
|
|
// Each entry's span, sliced back out of the description, is exactly
|
|
// the word(s) that triggered that particular match.
|
|
const resTechSteps = res.body.steps[0].techSteps;
|
|
expect(resTechSteps.map((t: { techStep: { key: string } }) => t.techStep.key)).to.deep.equal([
|
|
"preheat",
|
|
"melt",
|
|
]);
|
|
expect(description.slice(resTechSteps[0].start, resTechSteps[0].end).toLowerCase()).to.equal(
|
|
"préchauffer",
|
|
);
|
|
expect(description.slice(resTechSteps[1].start, resTechSteps[1].end).toLowerCase()).to.equal(
|
|
"faire fondre",
|
|
);
|
|
});
|
|
|
|
it("rejects an unknown ingredientId with 404 INGREDIENT_NOT_FOUND", async () => {
|
|
const { agent } = await signup();
|
|
const piece = await unitId("piece");
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Test",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: 999_999, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.INGREDIENT_NOT_FOUND);
|
|
});
|
|
|
|
it("rejects an unknown unitId with 404 UNIT_NOT_FOUND", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Test",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: 999_999 }],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.UNIT_NOT_FOUND);
|
|
});
|
|
|
|
it("rejects an unknown dietId with 404 DIET_NOT_FOUND", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Test",
|
|
portions: 4,
|
|
dietIds: [999_999],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.DIET_NOT_FOUND);
|
|
});
|
|
|
|
it("rejects an empty ingredients or steps list with 400 VALIDATION_ERROR", async () => {
|
|
const { agent } = await signup();
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Test",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(400);
|
|
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
|
|
});
|
|
|
|
it("rejects the same ingredientId listed twice with 400 VALIDATION_ERROR, not a 500 (issue #53 follow-up)", async () => {
|
|
// `RecipeIngredient`'s primary key is `(recipeId, ingredientId)` — a
|
|
// manual creation can't reach this via the web UI (`IngredientPicker`
|
|
// hides an already-picked ingredient), but nothing stops a raw
|
|
// request (or a source import, whose lines aren't deduplicated) from
|
|
// sending it — must fail cleanly instead of crashing on the DB's
|
|
// unique-constraint violation.
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
|
|
const res = await agent.post("/recipes").send({
|
|
name: "Test",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [
|
|
{ ingredientId: tomate, quantity: 1, unitId: piece },
|
|
{ ingredientId: tomate, quantity: 2, unitId: piece },
|
|
],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(400);
|
|
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
|
|
});
|
|
|
|
it("rejects a missing or non-positive portions with 400 VALIDATION_ERROR", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const basePayload = {
|
|
name: "Test",
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Étape" }],
|
|
};
|
|
|
|
const missing = await agent.post("/recipes").send(basePayload);
|
|
const zero = await agent.post("/recipes").send({ ...basePayload, portions: 0 });
|
|
|
|
expect(missing.status).to.equal(400);
|
|
expect(missing.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
|
|
expect(zero.status).to.equal(400);
|
|
expect(zero.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
|
|
});
|
|
});
|
|
|
|
describe("GET /recipes/:id", () => {
|
|
it("returns 404 RECIPE_NOT_FOUND for an unknown id", async () => {
|
|
const { agent } = await signup();
|
|
|
|
const res = await agent.get("/recipes/999999");
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.RECIPE_NOT_FOUND);
|
|
});
|
|
|
|
it("returns the full recipe detail", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const created = await agent.post("/recipes").send({
|
|
name: "Salade",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Couper" }],
|
|
});
|
|
|
|
const res = await agent.get(`/recipes/${created.body.id}`);
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body.name).to.equal("Salade");
|
|
expect(res.body.portions).to.equal(4);
|
|
expect(res.body.ingredients[0].ingredient.key).to.equal("tomato");
|
|
expect(res.body.ingredients[0].unit.key).to.equal("piece");
|
|
expect(res.body.isFavorite).to.equal(false);
|
|
});
|
|
|
|
it("returns 404 for a PERSONAL recipe belonging to someone else", async () => {
|
|
const { agent } = await signup();
|
|
const { profileId: otherId } = await signup();
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "Secrète", authorId: otherId, portions: 4 },
|
|
});
|
|
|
|
const res = await agent.get(`/recipes/${recipe.id}`);
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.RECIPE_NOT_FOUND);
|
|
});
|
|
|
|
it("returns 200 for a PUBLIC recipe belonging to someone else", async () => {
|
|
const { agent } = await signup();
|
|
const { profileId: otherId } = await signup();
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "Ouverte", authorId: otherId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
|
|
const res = await agent.get(`/recipes/${recipe.id}`);
|
|
|
|
expect(res.status).to.equal(200);
|
|
});
|
|
|
|
it("returns 200 for a HOUSE recipe shared with the viewer's household, 404 otherwise", async () => {
|
|
const { agent } = await signup();
|
|
const houseRes = await agent.post("/house").send({ name: "Chez moi" });
|
|
const { profileId: otherId } = await signup();
|
|
const inHouse = await prisma.recipe.create({
|
|
data: {
|
|
name: "Du foyer",
|
|
authorId: otherId,
|
|
visibility: "HOUSE",
|
|
authorHouseId: houseRes.body.id,
|
|
portions: 4,
|
|
},
|
|
});
|
|
const otherHouseId = (
|
|
await prisma.house.create({
|
|
data: { name: "Autre", adminId: otherId, inviteCode: "TESTHOUS" },
|
|
})
|
|
).id;
|
|
const outsideHouse = await prisma.recipe.create({
|
|
data: {
|
|
name: "D'un autre foyer",
|
|
authorId: otherId,
|
|
visibility: "HOUSE",
|
|
authorHouseId: otherHouseId,
|
|
portions: 4,
|
|
},
|
|
});
|
|
|
|
const inHouseRes = await agent.get(`/recipes/${inHouse.id}`);
|
|
const outsideHouseRes = await agent.get(`/recipes/${outsideHouse.id}`);
|
|
|
|
expect(inHouseRes.status).to.equal(200);
|
|
expect(outsideHouseRes.status).to.equal(404);
|
|
});
|
|
});
|
|
|
|
describe("PATCH /recipes/:id", () => {
|
|
it("replaces the recipe's whole content", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const oignon = await ingredientId("onion");
|
|
const piece = await unitId("piece");
|
|
const gram = await unitId("gram");
|
|
const created = await agent.post("/recipes").send({
|
|
name: "Salade",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Couper" }],
|
|
});
|
|
|
|
const res = await agent.patch(`/recipes/${created.body.id}`).send({
|
|
name: "Salade composée",
|
|
portions: 6,
|
|
visibility: "PUBLIC",
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: oignon, quantity: 2, unitId: gram }],
|
|
steps: [{ description: "Émincer" }, { description: "Mélanger" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body.name).to.equal("Salade composée");
|
|
expect(res.body.portions).to.equal(6);
|
|
expect(res.body.visibility).to.equal("PUBLIC");
|
|
expect(res.body.ingredients).to.have.length(1);
|
|
expect(res.body.ingredients[0].ingredient.key).to.equal("onion");
|
|
expect(res.body.ingredients[0].unit.key).to.equal("gram");
|
|
expect(res.body.steps).to.have.length(2);
|
|
});
|
|
|
|
it("recomputes each replaced step's technique sequence", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const mince = await techStepId("mince");
|
|
const created = await agent.post("/recipes").send({
|
|
name: "Salade",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Servir immédiatement" }],
|
|
});
|
|
|
|
const res = await agent.patch(`/recipes/${created.body.id}`).send({
|
|
name: "Salade",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Émincer les tomates" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(200);
|
|
const step = await prisma.step.findFirstOrThrow({ where: { recipeId: created.body.id } });
|
|
expect(await stepTechStepIds(step.id)).to.deep.equal([mince]);
|
|
});
|
|
|
|
it("recomputes techniques from scratch on every edit — modifying, adding, and removing a step all take effect, nothing stale survives", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const chop = await techStepId("chop");
|
|
const _mince = await techStepId("mince");
|
|
const _melt = await techStepId("melt");
|
|
const simmer = await techStepId("simmer");
|
|
const bake = await techStepId("bake");
|
|
|
|
const created = await agent.post("/recipes").send({
|
|
name: "Ragoût",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [
|
|
{ description: "Hacher les oignons" }, // chop
|
|
{ description: "Faire mijoter à feu doux" }, // simmer
|
|
{ description: "Cuire au four" }, // bake
|
|
],
|
|
});
|
|
expect(created.status).to.equal(201);
|
|
const originalStepIds = (
|
|
await prisma.step.findMany({ where: { recipeId: created.body.id } })
|
|
).map((s) => s.id);
|
|
expect(originalStepIds).to.have.length(3);
|
|
// Sanity check before the edit — each original step really did get a
|
|
// techStepId chop/simmer/bake (proves the later assertions are
|
|
// actually about recomputation, not about it never having matched).
|
|
expect((await Promise.all(originalStepIds.map(stepTechStepIds))).flat().sort()).to.deep.equal(
|
|
[chop, simmer, bake].sort(),
|
|
);
|
|
|
|
// Edit: step 1's description changes (chop -> mince), a brand new
|
|
// step 2 is added (-> melt), and the old steps 2/3 (simmer/bake) are
|
|
// dropped entirely — the three cases the recompute guarantee has to
|
|
// cover (see StepTechStep's schema doc comment).
|
|
const editedDescription = "Émincer les tomates";
|
|
const addedDescription = "Faire fondre le beurre";
|
|
const res = await agent.patch(`/recipes/${created.body.id}`).send({
|
|
name: "Ragoût",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: editedDescription }, { description: addedDescription }],
|
|
});
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body.steps).to.have.length(2);
|
|
expect(
|
|
res.body.steps.map((s: { techSteps: { techStep: { key: string } }[] }) =>
|
|
s.techSteps.map((t) => t.techStep.key),
|
|
),
|
|
).to.deep.equal([["mince"], ["melt"]]);
|
|
// Modified step's span reflects the NEW text, not a stale one from
|
|
// "Hacher les oignons" (which doesn't even contain "émincer").
|
|
const editedTechStep = res.body.steps[0].techSteps[0];
|
|
expect(
|
|
editedDescription.slice(editedTechStep.start, editedTechStep.end).toLowerCase(),
|
|
).to.equal("émincer");
|
|
const addedTechStep = res.body.steps[1].techSteps[0];
|
|
expect(addedDescription.slice(addedTechStep.start, addedTechStep.end).toLowerCase()).to.equal(
|
|
"faire fondre",
|
|
);
|
|
|
|
// The dropped steps' old rows are actually gone (cascade), not just
|
|
// invisible in the response — confirms "delete" really deletes rather
|
|
// than orphaning StepTechStep rows nothing references any more.
|
|
const remainingSteps = await prisma.step.findMany({ where: { recipeId: created.body.id } });
|
|
expect(remainingSteps).to.have.length(2);
|
|
const orphanedTechSteps = await prisma.stepTechStep.findMany({
|
|
where: { stepId: { in: originalStepIds } },
|
|
});
|
|
expect(orphanedTechSteps).to.deep.equal([]);
|
|
});
|
|
|
|
it("returns 404 RECIPE_NOT_FOUND for an unknown id", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
|
|
const res = await agent.patch("/recipes/999999").send({
|
|
name: "Test",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.RECIPE_NOT_FOUND);
|
|
});
|
|
|
|
it("rejects an unknown dietId with 404 DIET_NOT_FOUND", async () => {
|
|
const { agent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const created = await agent.post("/recipes").send({
|
|
name: "Salade",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Couper" }],
|
|
});
|
|
|
|
const res = await agent.patch(`/recipes/${created.body.id}`).send({
|
|
name: "Salade",
|
|
portions: 4,
|
|
dietIds: [999_999],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Couper" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.DIET_NOT_FOUND);
|
|
});
|
|
|
|
it("rejects an edit from anyone other than the recipe's author with 403 NOT_RECIPE_AUTHOR", async () => {
|
|
const { profileId } = await signup();
|
|
const { agent: otherAgent } = await signup();
|
|
const tomate = await ingredientId("tomato");
|
|
const piece = await unitId("piece");
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "Publique", authorId: profileId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
|
|
const res = await otherAgent.patch(`/recipes/${recipe.id}`).send({
|
|
name: "Hack",
|
|
portions: 4,
|
|
dietIds: [],
|
|
ingredients: [{ ingredientId: tomate, quantity: 1, unitId: piece }],
|
|
steps: [{ description: "Étape" }],
|
|
});
|
|
|
|
expect(res.status).to.equal(403);
|
|
expect(res.body.code).to.equal(ErrorCode.NOT_RECIPE_AUTHOR);
|
|
});
|
|
});
|
|
|
|
describe("DELETE /recipes/:id", () => {
|
|
it("deletes a recipe not referenced by any planning item", async () => {
|
|
const { agent, profileId } = await signup();
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "À supprimer", authorId: profileId, portions: 4 },
|
|
});
|
|
|
|
const res = await agent.delete(`/recipes/${recipe.id}`);
|
|
expect(res.status).to.equal(204);
|
|
|
|
const getRes = await agent.get(`/recipes/${recipe.id}`);
|
|
expect(getRes.status).to.equal(404);
|
|
});
|
|
|
|
it("rejects deleting a recipe still used by a planning item with 409 RECIPE_IN_USE", async () => {
|
|
const { agent, profileId } = await signup();
|
|
const houseRes = await agent.post("/house").send({ name: "Chez moi" });
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "Ratatouille", authorId: profileId, portions: 4 },
|
|
});
|
|
const planning = await prisma.planning.create({
|
|
data: {
|
|
houseId: houseRes.body.id,
|
|
startDate: new Date(Date.UTC(2026, 0, 1)),
|
|
finishDate: new Date(Date.UTC(2026, 0, 7)),
|
|
},
|
|
});
|
|
await prisma.planningItem.create({
|
|
data: {
|
|
planningId: planning.id,
|
|
weekDay: "lundi",
|
|
meal: "diner",
|
|
recipeId: recipe.id,
|
|
portions: 4,
|
|
},
|
|
});
|
|
|
|
const res = await agent.delete(`/recipes/${recipe.id}`);
|
|
|
|
expect(res.status).to.equal(409);
|
|
expect(res.body.code).to.equal(ErrorCode.RECIPE_IN_USE);
|
|
});
|
|
|
|
it("returns 404 RECIPE_NOT_FOUND for an unknown id", async () => {
|
|
const { agent } = await signup();
|
|
|
|
const res = await agent.delete("/recipes/999999");
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.RECIPE_NOT_FOUND);
|
|
});
|
|
|
|
it("rejects deleting someone else's recipe with 403 NOT_RECIPE_AUTHOR", async () => {
|
|
const { profileId } = await signup();
|
|
const { agent: otherAgent } = await signup();
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "Publique", authorId: profileId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
|
|
const res = await otherAgent.delete(`/recipes/${recipe.id}`);
|
|
|
|
expect(res.status).to.equal(403);
|
|
expect(res.body.code).to.equal(ErrorCode.NOT_RECIPE_AUTHOR);
|
|
});
|
|
});
|
|
|
|
describe("POST/DELETE /recipes/:id/favorite", () => {
|
|
it("adds and removes a recipe from the viewer's favorites", async () => {
|
|
const { agent, profileId } = await signup();
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "Recette", authorId: profileId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
|
|
const addRes = await agent.post(`/recipes/${recipe.id}/favorite`);
|
|
expect(addRes.status).to.equal(204);
|
|
expect((await agent.get(`/recipes/${recipe.id}`)).body.isFavorite).to.equal(true);
|
|
|
|
const removeRes = await agent.delete(`/recipes/${recipe.id}/favorite`);
|
|
expect(removeRes.status).to.equal(204);
|
|
expect((await agent.get(`/recipes/${recipe.id}`)).body.isFavorite).to.equal(false);
|
|
});
|
|
|
|
it("is idempotent — favoriting an already-favorited recipe doesn't error", async () => {
|
|
const { agent, profileId } = await signup();
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "Recette", authorId: profileId, visibility: "PUBLIC", portions: 4 },
|
|
});
|
|
|
|
await agent.post(`/recipes/${recipe.id}/favorite`);
|
|
const res = await agent.post(`/recipes/${recipe.id}/favorite`);
|
|
|
|
expect(res.status).to.equal(204);
|
|
});
|
|
|
|
it("rejects favoriting a recipe the viewer can't see with 404 RECIPE_NOT_FOUND", async () => {
|
|
const { agent } = await signup();
|
|
const { profileId: otherId } = await signup();
|
|
const recipe = await prisma.recipe.create({
|
|
data: { name: "Secrète", authorId: otherId, portions: 4 },
|
|
});
|
|
|
|
const res = await agent.post(`/recipes/${recipe.id}/favorite`);
|
|
|
|
expect(res.status).to.equal(404);
|
|
expect(res.body.code).to.equal(ErrorCode.RECIPE_NOT_FOUND);
|
|
});
|
|
});
|
|
});
|