6ae98a1cb4
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
4.3 KiB
TypeScript
112 lines
4.3 KiB
TypeScript
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(summary.created.transactions).toBeGreaterThan(150);
|
||
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,
|
||
transactions: 0,
|
||
recurringRules: 0,
|
||
installmentPlans: 0,
|
||
});
|
||
expect(await getDb().collection("user").countDocuments()).toBe(SEED_USERS.length + 1);
|
||
expect(await HouseholdModel.countDocuments()).toBe(SEED_HOUSEHOLDS.length);
|
||
});
|
||
|
||
it("aynı aydaki transferlerin hepsi oluşur (seedRef çakışması yok)", async () => {
|
||
const tx = getDb().collection("transactions");
|
||
const months = new Set(
|
||
(await tx.find({ description: "ATM" }).toArray()).map((t) => t.seedRef.split(":")[1]),
|
||
);
|
||
expect(months.size).toBeGreaterThanOrEqual(5);
|
||
// Her ay ATM'nin yanında kart ödemesi ve birikim de var (ayın 7'si geçmişse).
|
||
expect(await tx.countDocuments({ description: "Kart borcu ödemesi" })).toBeGreaterThanOrEqual(
|
||
months.size - 1,
|
||
);
|
||
expect(await tx.countDocuments({ description: "Aylık birikim" })).toBeGreaterThanOrEqual(
|
||
months.size - 1,
|
||
);
|
||
});
|
||
|
||
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);
|
||
});
|
||
});
|