batchCooking/apps/api/test/house.test.ts
Nicolas 766d48eaa5 feat(recipes): préférences de sources par foyer + distinction officielle/non-officielle
Répond à deux besoins : permettre à chaque foyer de choisir quelles
sources apparaissent dans ses onglets de recettes, et distinguer les
sources à API officielle des sources scrapées.

- RecipeSourceAdapter.official (booléen, sans défaut — chaque
  adaptateur doit le déclarer explicitement) synchronisé sur
  Source.official par syncRecipeSources.
- HouseSource : table de jointure opt-in (House <-> Source) — aucune
  ligne = source masquée. Un foyer nouvellement créé ne voit aucune
  source tant qu'il ne les active pas explicitement.
- GET /reference/sources (catalogue des sources implémentées, avec le
  flag officiel).
- GET/PATCH /house/current/sources (lecture/remplacement complet des
  sources activées par le foyer courant).
- recipe.service.ts : sourceVisibilityWhere() filtre désormais TOUS
  les onglets (perso/foyer/publique/favoris) — une recette sans
  source reste toujours visible ; une recette importée ne l'est que
  si sa source est activée pour le foyer du viewer. Un viewer sans
  foyer ne voit aucune recette sourcée.

Côté web :
- Nouvelle étape /onboarding/sources dans le wizard d'inscription,
  atteinte uniquement si un foyer vient d'être créé/rejoint (sinon on
  saute direct aux allergènes) ; s'auto-saute aussi si aucune source
  n'est encore implémentée (catalogue vide aujourd'hui).
- Nouvelle section « Sources de recettes » dans /parametres/foyer
  (masquée dans les mêmes conditions), avec sauvegarde à la volée
  (même pattern que les autres préférences hot-saved).
- SourceSelect (features/house/), grille de cases à cocher avec badge
  officiel/non-officielle, sur le même principe qu'AllergySelect.

172 tests backend passent (dont 25 nouveaux). Build et lint propres.
Vérifié manuellement en navigateur : le parcours d'onboarding saute
bien l'étape sources (catalogue vide) et affiche « 4 sur 4 » quand un
foyer a été créé ; la section paramètres reste invisible tant
qu'aucune source n'existe.

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

436 lines
17 KiB
TypeScript

import { ErrorCode, type SignupInput } from "@batch-cooking/shared";
import { faker } from "@faker-js/faker";
import { expect } from "chai";
import type { Express } from "express";
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-source-adapter.js";
import { clearRecipeSources, registerRecipeSource } from "../src/lib/recipe-source-registry.js";
import { resetDatabase } from "../test-support/reset-db.js";
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 }),
};
}
/** A minimal `RecipeSourceAdapter` — only `key`/`name`/`official` matter for `syncRecipeSources` fixtures here. */
function buildFakeAdapter(key: string, name: string): RecipeSourceAdapter {
return {
key,
name,
official: false,
async list() {
return { items: [], nextCursor: null };
},
async fetchDetail() {
throw new Error("not implemented");
},
parse() {
throw new Error("not implemented");
},
};
}
/** Signs up a fresh profile on a brand new agent (its own cookie jar) and returns both. */
async function signupAgent(app: Express) {
const agent = request.agent(app);
const res = await agent.post("/auth/signup").send(buildSignupPayload());
return { agent, profile: res.body };
}
describe("Household", () => {
const app = createApp();
beforeEach(async () => {
await resetDatabase();
});
after(async () => {
await prisma.$disconnect();
});
describe("GET /house/current", () => {
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
const res = await request(app).get("/house/current");
expect(res.status).to.equal(401);
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
});
it("returns null when the profile has no household yet", async () => {
const { agent } = await signupAgent(app);
const res = await agent.get("/house/current");
expect(res.status).to.equal(200);
expect(res.body).to.equal(null);
});
it("returns the household with its admin and member list, once created", async () => {
const { agent, profile } = await signupAgent(app);
await agent.post("/house").send({ name: "Chez Alice" });
const res = await agent.get("/house/current");
expect(res.status).to.equal(200);
expect(res.body.name).to.equal("Chez Alice");
expect(res.body.adminId).to.equal(profile.id);
expect(res.body.inviteCode).to.match(/^[A-Z2-9]{8}$/);
expect(res.body.members).to.deep.equal([
{ id: profile.id, firstName: profile.firstName, lastName: profile.lastName },
]);
});
});
describe("PATCH /house/current", () => {
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
const res = await request(app).patch("/house/current").send({ name: "Chez nous" });
expect(res.status).to.equal(401);
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
});
it("rejects renaming when the profile has no household yet with 404 HOUSE_NOT_FOUND", async () => {
const { agent } = await signupAgent(app);
const res = await agent.patch("/house/current").send({ name: "Chez nous" });
expect(res.status).to.equal(404);
expect(res.body.code).to.equal(ErrorCode.HOUSE_NOT_FOUND);
});
it("renames the household", async () => {
const { agent } = await signupAgent(app);
await agent.post("/house").send({ name: "Chez Alice" });
const res = await agent.patch("/house/current").send({ name: "Chez les Dupont" });
expect(res.status).to.equal(200);
expect(res.body.name).to.equal("Chez les Dupont");
const refetch = await agent.get("/house/current");
expect(refetch.body.name).to.equal("Chez les Dupont");
});
it("rejects an empty name with 400 VALIDATION_ERROR", async () => {
const { agent } = await signupAgent(app);
await agent.post("/house").send({ name: "Chez Alice" });
const res = await agent.patch("/house/current").send({ name: "" });
expect(res.status).to.equal(400);
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
});
});
describe("POST /house", () => {
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
const res = await request(app).post("/house").send({ name: "Chez nous" });
expect(res.status).to.equal(401);
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
});
it("creates a household with the caller as its admin", async () => {
const { agent, profile } = await signupAgent(app);
const res = await agent.post("/house").send({ name: "Chez Alice" });
expect(res.status).to.equal(201);
expect(res.body.name).to.equal("Chez Alice");
expect(res.body.adminId).to.equal(profile.id);
const me = await agent.get("/auth/me");
expect(me.body.houseId).to.equal(res.body.id);
});
it("rejects an empty name with 400 VALIDATION_ERROR", async () => {
const { agent } = await signupAgent(app);
const res = await agent.post("/house").send({ name: "" });
expect(res.status).to.equal(400);
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
});
it("rejects creating a second household with 409 ALREADY_HAS_HOUSE", async () => {
const { agent } = await signupAgent(app);
await agent.post("/house").send({ name: "Chez Alice" });
const res = await agent.post("/house").send({ name: "Chez Alice bis" });
expect(res.status).to.equal(409);
expect(res.body.code).to.equal(ErrorCode.ALREADY_HAS_HOUSE);
});
});
describe("POST /house/join", () => {
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
const res = await request(app).post("/house/join").send({ inviteCode: "ABCDEFGH" });
expect(res.status).to.equal(401);
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
});
it("joins an existing household by invite code", async () => {
const { agent: adminAgent } = await signupAgent(app);
const created = await adminAgent.post("/house").send({ name: "Chez Alice" });
const { agent: joinerAgent, profile: joiner } = await signupAgent(app);
const res = await joinerAgent
.post("/house/join")
.send({ inviteCode: created.body.inviteCode });
expect(res.status).to.equal(200);
expect(res.body.id).to.equal(created.body.id);
expect(res.body.members.map((m: { id: number }) => m.id)).to.include(joiner.id);
});
it("rejects an unknown invite code with 404 INVITE_CODE_NOT_FOUND", async () => {
const { agent } = await signupAgent(app);
const res = await agent.post("/house/join").send({ inviteCode: "ZZZZZZZZ" });
expect(res.status).to.equal(404);
expect(res.body.code).to.equal(ErrorCode.INVITE_CODE_NOT_FOUND);
});
it("rejects joining when the profile already belongs to a household with 409 ALREADY_HAS_HOUSE", async () => {
const { agent: adminAgent } = await signupAgent(app);
const created = await adminAgent.post("/house").send({ name: "Chez Alice" });
const { agent } = await signupAgent(app);
await agent.post("/house").send({ name: "Chez Bob" });
const res = await agent.post("/house/join").send({ inviteCode: created.body.inviteCode });
expect(res.status).to.equal(409);
expect(res.body.code).to.equal(ErrorCode.ALREADY_HAS_HOUSE);
});
});
describe("POST /house/leave", () => {
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
const res = await request(app).post("/house/leave");
expect(res.status).to.equal(401);
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
});
it("rejects leaving when the profile has no household with 404 HOUSE_NOT_FOUND", async () => {
const { agent } = await signupAgent(app);
const res = await agent.post("/house/leave");
expect(res.status).to.equal(404);
expect(res.body.code).to.equal(ErrorCode.HOUSE_NOT_FOUND);
});
it("deletes the household when its sole member leaves", async () => {
const { agent } = await signupAgent(app);
const created = await agent.post("/house").send({ name: "Chez Alice" });
const res = await agent.post("/house/leave");
expect(res.status).to.equal(204);
expect(await prisma.house.findUnique({ where: { id: created.body.id } })).to.equal(null);
});
it("transfers adminship to the remaining member when the admin leaves", async () => {
const { agent: adminAgent } = await signupAgent(app);
const created = await adminAgent.post("/house").send({ name: "Chez Alice" });
const { agent: memberAgent, profile: member } = await signupAgent(app);
await memberAgent.post("/house/join").send({ inviteCode: created.body.inviteCode });
const res = await adminAgent.post("/house/leave");
expect(res.status).to.equal(204);
const house = await prisma.house.findUnique({ where: { id: created.body.id } });
expect(house?.adminId).to.equal(member.id);
});
});
describe("DELETE /house/current", () => {
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
const res = await request(app).delete("/house/current");
expect(res.status).to.equal(401);
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
});
it("rejects deleting when the profile has no household with 404 HOUSE_NOT_FOUND", async () => {
const { agent } = await signupAgent(app);
const res = await agent.delete("/house/current");
expect(res.status).to.equal(404);
expect(res.body.code).to.equal(ErrorCode.HOUSE_NOT_FOUND);
});
it("rejects a non-admin member with 403 NOT_HOUSE_ADMIN", async () => {
const { agent: adminAgent } = await signupAgent(app);
const created = await adminAgent.post("/house").send({ name: "Chez Alice" });
const { agent: memberAgent } = await signupAgent(app);
await memberAgent.post("/house/join").send({ inviteCode: created.body.inviteCode });
const res = await memberAgent.delete("/house/current");
expect(res.status).to.equal(403);
expect(res.body.code).to.equal(ErrorCode.NOT_HOUSE_ADMIN);
});
it("deletes the household for every member, cascading its plannings", async () => {
const { agent: adminAgent } = await signupAgent(app);
const created = await adminAgent.post("/house").send({ name: "Chez Alice" });
const { agent: memberAgent, profile: member } = await signupAgent(app);
await memberAgent.post("/house/join").send({ inviteCode: created.body.inviteCode });
const planning = await prisma.planning.create({
data: {
houseId: created.body.id,
startDate: new Date(Date.UTC(2000, 0, 1)),
finishDate: new Date(Date.UTC(2000, 0, 7)),
},
});
const res = await adminAgent.delete("/house/current");
expect(res.status).to.equal(204);
expect(await prisma.house.findUnique({ where: { id: created.body.id } })).to.equal(null);
expect(await prisma.planning.findUnique({ where: { id: planning.id } })).to.equal(null);
const memberProfile = await prisma.userProfile.findUniqueOrThrow({
where: { id: member.id },
});
expect(memberProfile.houseId).to.equal(null);
});
});
describe("DELETE /house/members/:memberId", () => {
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
const res = await request(app).delete("/house/members/1");
expect(res.status).to.equal(401);
expect(res.body.code).to.equal(ErrorCode.NOT_AUTHENTICATED);
});
it("rejects a non-admin member with 403 NOT_HOUSE_ADMIN", async () => {
const { agent: adminAgent, profile: admin } = await signupAgent(app);
const created = await adminAgent.post("/house").send({ name: "Chez Alice" });
const { agent: memberAgent } = await signupAgent(app);
await memberAgent.post("/house/join").send({ inviteCode: created.body.inviteCode });
const res = await memberAgent.delete(`/house/members/${admin.id}`);
expect(res.status).to.equal(403);
expect(res.body.code).to.equal(ErrorCode.NOT_HOUSE_ADMIN);
});
it("rejects the admin trying to remove themselves with 400 VALIDATION_ERROR", async () => {
const { agent: adminAgent, profile: admin } = await signupAgent(app);
await adminAgent.post("/house").send({ name: "Chez Alice" });
const res = await adminAgent.delete(`/house/members/${admin.id}`);
expect(res.status).to.equal(400);
expect(res.body.code).to.equal(ErrorCode.VALIDATION_ERROR);
});
it("removes the targeted member from the household", async () => {
const { agent: adminAgent } = await signupAgent(app);
const created = await adminAgent.post("/house").send({ name: "Chez Alice" });
const { agent: memberAgent, profile: member } = await signupAgent(app);
await memberAgent.post("/house/join").send({ inviteCode: created.body.inviteCode });
const res = await adminAgent.delete(`/house/members/${member.id}`);
expect(res.status).to.equal(200);
expect(res.body.members.map((m: { id: number }) => m.id)).to.not.include(member.id);
const memberProfile = await prisma.userProfile.findUniqueOrThrow({
where: { id: member.id },
});
expect(memberProfile.houseId).to.equal(null);
});
});
describe("GET /house/current/sources + PATCH /house/current/sources", () => {
beforeEach(() => {
clearRecipeSources();
});
afterEach(() => {
clearRecipeSources();
});
it("rejects requests without a session cookie with 401 NOT_AUTHENTICATED", async () => {
const getRes = await request(app).get("/house/current/sources");
const patchRes = await request(app).patch("/house/current/sources").send({ sourceIds: [] });
expect(getRes.status).to.equal(401);
expect(patchRes.status).to.equal(401);
});
it("rejects reading/writing with 404 HOUSE_NOT_FOUND when the profile has no household yet", async () => {
const { agent } = await signupAgent(app);
const getRes = await agent.get("/house/current/sources");
const patchRes = await agent.patch("/house/current/sources").send({ sourceIds: [] });
expect(getRes.status).to.equal(404);
expect(getRes.body.code).to.equal(ErrorCode.HOUSE_NOT_FOUND);
expect(patchRes.status).to.equal(404);
expect(patchRes.body.code).to.equal(ErrorCode.HOUSE_NOT_FOUND);
});
it("starts with nothing enabled, opt-in — not just an empty array by coincidence", async () => {
const { agent } = await signupAgent(app);
await agent.post("/house").send({ name: "Chez Alice" });
const res = await agent.get("/house/current/sources");
expect(res.status).to.equal(200);
expect(res.body).to.deep.equal([]);
});
it("replaces (not merges) the enabled-source set, and it's readable back", async () => {
registerRecipeSource(buildFakeAdapter("fakeSource", "Fake Source"));
registerRecipeSource(buildFakeAdapter("otherSource", "Other Source"));
await syncRecipeSources(prisma);
const fakeSource = await prisma.source.findUniqueOrThrow({ where: { key: "fakeSource" } });
const otherSource = await prisma.source.findUniqueOrThrow({ where: { key: "otherSource" } });
const { agent } = await signupAgent(app);
await agent.post("/house").send({ name: "Chez Alice" });
const firstPatch = await agent
.patch("/house/current/sources")
.send({ sourceIds: [fakeSource.id, otherSource.id] });
expect(firstPatch.status).to.equal(200);
expect(firstPatch.body.sort()).to.deep.equal([fakeSource.id, otherSource.id].sort());
const secondPatch = await agent
.patch("/house/current/sources")
.send({ sourceIds: [fakeSource.id] });
expect(secondPatch.status).to.equal(200);
expect(secondPatch.body).to.deep.equal([fakeSource.id]);
const res = await agent.get("/house/current/sources");
expect(res.body).to.deep.equal([fakeSource.id]);
});
it("rejects an unknown sourceId with 404 SOURCE_NOT_FOUND", async () => {
const { agent } = await signupAgent(app);
await agent.post("/house").send({ name: "Chez Alice" });
const res = await agent.patch("/house/current/sources").send({ sourceIds: [999999] });
expect(res.status).to.equal(404);
expect(res.body.code).to.equal(ErrorCode.SOURCE_NOT_FOUND);
});
});
});