import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { getAuth } from "@/lib/auth"; import { authErrorCode } from "@/lib/auth-errors"; import { getDb, getMongoClient } from "@/lib/db/mongo"; import { HouseholdModel } from "@/models/household"; import { HouseholdMemberModel } from "@/models/household-member"; import { closeSeedConnections, runSeed } from "./run"; import { SEED_HOUSEHOLDS, SEED_USERS } from "./fixtures"; const silent = () => {}; beforeAll(async () => { expect(getDb().databaseName).toBe("butcem_test"); await getDb().dropDatabase(); }); afterAll(async () => { await getDb().dropDatabase(); await closeSeedConnections(); await getMongoClient().close(); }); describe("seed", () => { it("ilk çalıştırmada admin, örnek kullanıcılar, haneler ve üyelikleri oluşturur", async () => { const summary = await runSeed({ log: silent }); const memberCount = SEED_HOUSEHOLDS.reduce((n, h) => n + h.members.length, 0); expect(summary.created).toMatchObject({ users: SEED_USERS.length + 1, households: SEED_HOUSEHOLDS.length, memberships: memberCount, }); expect(summary.created.categories).toBeGreaterThan(40); expect(summary.created.accounts).toBe(7); expect(await getDb().collection("user").countDocuments()).toBe(SEED_USERS.length + 1); expect(await HouseholdMemberModel.countDocuments()).toBe(memberCount); }); it("idempotenttir: ikinci çalıştırma kopya oluşturmaz", async () => { const summary = await runSeed({ log: silent }); expect(summary.created).toEqual({ users: 0, households: 0, memberships: 0, categories: 0, accounts: 0, }); expect(await getDb().collection("user").countDocuments()).toBe(SEED_USERS.length + 1); expect(await HouseholdModel.countDocuments()).toBe(SEED_HOUSEHOLDS.length); }); it("roller ve askı durumu doğru", async () => { const users = await getDb() .collection("user") .find({}, { projection: { username: 1, role: 1, banned: 1 } }) .toArray(); const by = new Map(users.map((u) => [u.username as string, u])); expect(by.get("admin")?.role).toBe("admin"); expect(by.get("burak")?.role).toBe("admin"); expect(by.get("ozan")?.banned).toBe(true); expect(users.filter((u) => u.banned)).toHaveLength(1); // Bir kullanıcı birden çok hanede, farklı rollerle. const ayse = by.get("ayse")!; const roles = await HouseholdMemberModel.find({ userId: ayse._id }).lean(); expect(roles.map((m) => m.role).sort()).toEqual(["owner", "viewer"]); // Hanesi olmayan admin. expect(await HouseholdMemberModel.countDocuments({ userId: by.get("burak")!._id })).toBe(0); }); it("e-postası olmayan kullanıcı kullanıcı adıyla girer; askıdaki kullanıcı giremez", async () => { const api = getAuth().api; const emre = await api.signInUsername({ body: { username: "emre", password: "Test-User-12345" }, }); expect(emre.user.name).toBe("Emre Çakır"); const error = await api .signInUsername({ body: { username: "ozan", password: "Test-User-12345" } }) .then( () => null, (e: unknown) => e, ); expect(authErrorCode(error)).toBe("BANNED_USER"); }); it("reset koleksiyonları temizleyip yeniden doldurur", async () => { const summary = await runSeed({ reset: true, log: silent }); expect(summary.created.users).toBe(SEED_USERS.length + 1); expect(await getDb().collection("user").countDocuments()).toBe(SEED_USERS.length + 1); }); });