Hermetiska integrationstester + auto-hushall i onboarding (size, i18n, locations)

This commit is contained in:
Sven (AAMOS AI)
2026-08-06 22:06:11 +07:00
parent dd66ebedbf
commit 3696aadf98
12 changed files with 9079 additions and 44 deletions
+52 -7
View File
@@ -1,5 +1,6 @@
import "./setup-env.js";
import { describe, expect, it, beforeAll, afterAll } from "vitest";
import { eq, inArray } from "drizzle-orm";
import { and, eq, inArray } from "drizzle-orm";
import { buildServer } from "../src/server.js";
import { loadConfig } from "../src/config.js";
import { createDatabase, closeDatabase, schema } from "@app/database";
@@ -9,22 +10,32 @@ import { createDatabase, closeDatabase, schema } from "@app/database";
* created a household yet (empty pantry, single-person context).
*/
describe("what-to-eat without household", () => {
const testDb = createDatabase(process.env.TEST_DATABASE_URL);
const config = loadConfig({
...process.env,
DATABASE_URL: process.env.TEST_DATABASE_URL!,
});
const testDb = createDatabase(process.env.TEST_DATABASE_URL!);
const config = loadConfig();
let app: Awaited<ReturnType<typeof buildServer>>;
let accessToken: string;
const userEmail = "what-to-eat-repro@example.invalid";
async function cleanup() {
const emails = [userEmail, "goals-multi@example.invalid"];
const emails = [userEmail, "goals-multi@example.invalid", "auto-household@example.invalid"];
const existing = await testDb.db
.select({ id: schema.users.id })
.from(schema.users)
.where(inArray(schema.users.email, emails));
for (const u of existing) {
await testDb.db.delete(schema.householdMembers).where(eq(schema.householdMembers.userId, u.id));
const ownedHouseholds = await testDb.db
.select({ id: schema.households.id })
.from(schema.households)
.innerJoin(
schema.householdMembers,
eq(schema.householdMembers.householdId, schema.households.id),
)
.where(and(eq(schema.householdMembers.userId, u.id), eq(schema.householdMembers.role, "owner")));
for (const h of ownedHouseholds) {
await testDb.db.delete(schema.storageLocations).where(eq(schema.storageLocations.householdId, h.id));
await testDb.db.delete(schema.households).where(eq(schema.households.id, h.id));
}
await testDb.db.delete(schema.userPreferences).where(eq(schema.userPreferences.userId, u.id));
await testDb.db.delete(schema.users).where(eq(schema.users.id, u.id));
}
@@ -101,4 +112,38 @@ describe("what-to-eat without household", () => {
expect(prefs?.goals).toEqual(["cook_more", "less_waste"]);
expect(prefs?.primaryGoal).toBe("cook_more");
});
it("quick-start auto-creates a household with default storage locations", async () => {
const registerRes = await app.inject({
method: "POST",
url: "/v1/auth/register",
payload: { email: "auto-household@example.invalid", password: "Password123!", displayName: "Auto" },
});
const { accessToken: token } = JSON.parse(registerRes.body) as { accessToken: string };
const userId = (JSON.parse(atob(token.split(".")[1]!)) as { sub: string }).sub;
const quickRes = await app.inject({
method: "POST",
url: "/v1/onboarding/quick-start",
headers: { authorization: `Bearer ${token}` },
payload: { goals: ["cook_more"], persons: 2, precisionMode: "simple" },
});
expect(quickRes.statusCode).toBe(200);
const { householdId } = JSON.parse(quickRes.body) as { householdId: string };
expect(householdId).toBeTruthy();
const [household] = await testDb.db
.select()
.from(schema.households)
.where(eq(schema.households.id, householdId))
.limit(1);
expect(household).toBeTruthy();
expect(household!.size).toBe(2);
const locations = await testDb.db
.select()
.from(schema.storageLocations)
.where(eq(schema.storageLocations.householdId, householdId));
expect(locations.map((l) => l.type).sort()).toEqual(["freezer", "fridge", "pantry"]);
});
});