import assert from "node:assert/strict"; import { Then, When } from "@cucumber/cucumber"; import { prisma } from "../../src/db/prisma.js"; import { slugify } from "../../src/utils/slugify.js"; import type { CustomWorld } from "../support/world.js"; /** Splits a comma-separated list of names from a `.feature` string into trimmed, non-empty parts. */ function splitNames(names: string): string[] { return names .split(",") .map((name) => name.trim()) .filter(Boolean); } /** * Resolves allergen names (as written in a `.feature` file, e.g. * "Arachides") to their Allergy id — scenarios still name allergens by their * French label for readability, so this slugifies before matching against * `Category.key` (see reference.service.ts for why the key lives on * Category, not Allergy). */ async function allergyIdsFor(names: string[]): Promise { const allergies = await prisma.allergy.findMany({ include: { category: true } }); return names.map((name) => { const key = slugify(name); const match = allergies.find((allergy) => allergy.category.key === key); if (!match) throw new Error(`No seeded allergen named "${name}"`); return match.id; }); } When("I set my regime to {string}", async function (this: CustomWorld, dietName: string) { const diet = await prisma.diet.findFirstOrThrow({ where: { key: slugify(dietName) } }); this.response = await this.agent.patch("/profile/diet").send({ dietId: diet.id }); }); Then( "my profile's regime should be {string}", async function (this: CustomWorld, dietName: string) { const diet = await prisma.diet.findFirstOrThrow({ where: { key: slugify(dietName) } }); assert.equal(this.response.body.dietId, diet.id); }, ); When("I set my allergens to {string}", async function (this: CustomWorld, names: string) { const allergyIds = await allergyIdsFor(splitNames(names)); this.response = await this.agent.patch("/profile/allergies").send({ allergyIds }); }); Then("my selected allergens should be {string}", async function (this: CustomWorld, names: string) { const expected = (await allergyIdsFor(splitNames(names))).sort(); const actual = [...this.response.body].sort(); assert.deepEqual(actual, expected); });