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
+48 -22
View File
@@ -2,30 +2,48 @@
import { revalidatePath } from "next/cache";
import { actionLog } from "@/lib/api";
import { adminCheck } from "@/lib/auth";
import { db } from "@/lib/db";
import { settings } from "@/lib/db/schema";
import { adminCheck } from "@/lib/auth";
import { sse } from "@/lib/db/sse-endpoints";
import { syncAmber as syncAmberRaw } from "@/util/sync";
import { getSettingsService, updateSettingService } from "./service";
export async function getSettongs() {
if (!(await adminCheck())) throw "Unauthorized";
const [
settangs = { enka: true, donatePromptpay: true, donateTruemoney: true },
] = await db.select().from(settings).limit(1);
return settangs;
return getSettingsService(
{ enka: true, donatePromptpay: true, donateTruemoney: true },
{
adminCheck,
read: async () =>
(
await db
.select({
enka: settings.enka,
donatePromptpay: settings.donatePromptpay,
donateTruemoney: settings.donateTruemoney,
})
.from(settings)
.limit(1)
)[0],
},
);
}
export async function toggleEnka(state: boolean) {
if (!(await adminCheck())) throw "Unauthorized";
await db
.insert(settings)
.values({ enka: state })
.onConflictDoUpdate({ target: settings.id, set: { enka: state } });
await actionLog("Changed a settings", { enka: state });
revalidatePath("/admin/settings");
revalidatePath("/artifact");
return updateSettingService(state, {
adminCheck,
persist: async (value) => {
await db
.insert(settings)
.values({ enka: value })
.onConflictDoUpdate({ target: settings.id, set: { enka: value } });
},
afterPersist: async (value) => {
await actionLog("Changed a settings", { enka: value });
revalidatePath("/admin/settings");
revalidatePath("/artifact");
},
});
}
export async function syncAmber() {
@@ -48,11 +66,19 @@ export async function toggleDonatePaymentMethod(
method: "donatePromptpay" | "donateTruemoney",
state: boolean,
) {
if (!(await adminCheck())) throw "Unauthorized";
await db
.insert(settings)
.values({ [method]: state })
.onConflictDoUpdate({ target: settings.id, set: { [method]: state } });
await actionLog("Changed a settings", { [method]: state });
return updateSettingService(state, {
adminCheck,
persist: async (value) => {
await db
.insert(settings)
.values({ [method]: value })
.onConflictDoUpdate({
target: settings.id,
set: { [method]: value },
});
},
afterPersist: async (value) => {
await actionLog("Changed a settings", { [method]: value });
},
});
}
+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"]);
});
});
+25
View File
@@ -0,0 +1,25 @@
export interface SettingsDependencies {
adminCheck: () => Promise<unknown>;
persist: (value: boolean) => Promise<void>;
afterPersist: (value: boolean) => Promise<void> | void;
}
export async function updateSettingService(
value: boolean,
dependencies: SettingsDependencies,
) {
if (!(await dependencies.adminCheck())) throw "Unauthorized";
await dependencies.persist(value);
await dependencies.afterPersist(value);
}
export async function getSettingsService<T>(
defaults: T,
dependencies: {
adminCheck: () => Promise<unknown>;
read: () => Promise<T | undefined>;
},
) {
if (!(await dependencies.adminCheck())) throw "Unauthorized";
return (await dependencies.read()) ?? defaults;
}