Files
sofa/lib/services/settings.test.ts
T
jake 609f984aa0 Move test utilities from lib/ to test/ directory
- Rename `lib/test-preload.ts` → `test/preload.ts` and
  `lib/test-utils.ts` → `test/sqlite.ts`
- Update `bunfig.toml` preload path to `./test/preload.ts`
- Update all service test imports from `@/lib/test-utils` to
  `@/test/sqlite`
2026-03-08 12:18:21 -04:00

71 lines
2.1 KiB
TypeScript

import { beforeEach, describe, expect, test } from "bun:test";
import { clearAllTables, insertUser } from "@/test/sqlite";
import {
getSetting,
getUserCount,
isRegistrationOpen,
setSetting,
} from "./settings";
beforeEach(() => {
clearAllTables();
});
// ── getSetting / setSetting ─────────────────────────────────────────
describe("getSetting / setSetting", () => {
test("returns null for missing key", () => {
expect(getSetting("nonexistent")).toBeNull();
});
test("stores and retrieves a value", () => {
setSetting("theme", "dark");
expect(getSetting("theme")).toBe("dark");
});
test("upserts: overwrites existing value", () => {
setSetting("theme", "dark");
setSetting("theme", "light");
expect(getSetting("theme")).toBe("light");
});
});
// ── getUserCount ────────────────────────────────────────────────────
describe("getUserCount", () => {
test("returns 0 when no users", () => {
expect(getUserCount()).toBe(0);
});
test("returns correct count", () => {
insertUser("user-1");
insertUser("user-2");
expect(getUserCount()).toBe(2);
});
});
// ── isRegistrationOpen ──────────────────────────────────────────────
describe("isRegistrationOpen", () => {
test("returns true when no users exist (first-run)", () => {
expect(isRegistrationOpen()).toBe(true);
});
test("returns false when users exist and setting is not set", () => {
insertUser();
expect(isRegistrationOpen()).toBe(false);
});
test("returns true when users exist and setting is 'true'", () => {
insertUser();
setSetting("registrationOpen", "true");
expect(isRegistrationOpen()).toBe(true);
});
test("returns false when users exist and setting is 'false'", () => {
insertUser();
setSetting("registrationOpen", "false");
expect(isRegistrationOpen()).toBe(false);
});
});