feat: add extensive test suite

This commit is contained in:
2026-08-30 11:33:53 +07:00 Unverified
parent 061cce00dc
commit 8359c7272e
131 changed files with 8717 additions and 2200 deletions
+57
View File
@@ -0,0 +1,57 @@
import { describe, expect, mock, test } from "bun:test";
import { getSettingsService, updateSettingService } from "./service";
describe("settings services", () => {
test("requires admin before reading settings", async () => {
const read = mock(async () => ({ enka: false }));
expect(
getSettingsService(
{ enka: true },
{ adminCheck: async () => false, read },
),
).rejects.toBe("Unauthorized");
expect(read).not.toHaveBeenCalled();
});
test("uses defaults only when no settings row exists", async () => {
const defaults = { enka: true };
expect(
await getSettingsService(defaults, {
adminCheck: async () => true,
read: async () => undefined,
}),
).toBe(defaults);
expect(
await getSettingsService(defaults, {
adminCheck: async () => true,
read: async () => ({ enka: false }),
}),
).toEqual({ enka: false });
});
test("blocks unauthorized updates before persistence", async () => {
const persist = mock(async (_value: boolean) => {});
expect(
updateSettingService(false, {
adminCheck: async () => null,
persist,
afterPersist: async () => {},
}),
).rejects.toBe("Unauthorized");
expect(persist).not.toHaveBeenCalled();
});
test("persists the exact state before logging and revalidation", async () => {
const calls: string[] = [];
await updateSettingService(false, {
adminCheck: async () => true,
persist: async (value) => {
calls.push(`persist:${value}`);
},
afterPersist: async (value) => {
calls.push(`after:${value}`);
},
});
expect(calls).toEqual(["persist:false", "after:false"]);
});
});