import assert from "node:assert/strict"; import { Then, When } from "@cucumber/cucumber"; import { prisma } from "../../src/db/prisma.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 (Category.name) to their Allergy id — see reference.service.ts for why the name 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 match = allergies.find((allergy) => allergy.category.name === name); 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: { name: 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: { name: 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); });