feat(14-06): convert all 9 service test files to async PGlite

- All beforeEach now use async/await createTestDb()
- All service calls in tests now awaited
- All direct DB calls (.run()/.all()) replaced with await
- All test callbacks made async
- Fixed PostgreSQL GROUP BY strictness in totals.service.ts (categories.name and categories.icon added to groupBy)
- db type changed to 'any' to accommodate PGlite type differences

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-04 13:11:52 +02:00
parent cb2a192cb5
commit 458b33f1c7
9 changed files with 406 additions and 408 deletions

View File

@@ -15,10 +15,10 @@ import {
import { createTestDb } from "../helpers/db.ts";
describe("Auth Service", () => {
let db: ReturnType<typeof createTestDb>;
let db: any;
beforeEach(() => {
db = createTestDb();
beforeEach(async () => {
db = await createTestDb();
});
describe("User Management", () => {
@@ -48,12 +48,12 @@ describe("Auth Service", () => {
});
it("getUserCount returns 0 then 1", async () => {
const countBefore = getUserCount(db);
const countBefore = await getUserCount(db);
expect(countBefore).toBe(0);
await createUser(db, "admin", "secret123");
const countAfter = getUserCount(db);
const countAfter = await getUserCount(db);
expect(countAfter).toBe(1);
});
@@ -86,33 +86,33 @@ describe("Auth Service", () => {
describe("Session Management", () => {
it("creates and retrieves a session (id length is 64 hex chars)", async () => {
const user = await createUser(db, "admin", "secret123");
const session = createSession(db, user.id);
const session = await createSession(db, user.id);
expect(session).toBeDefined();
expect(session.id).toHaveLength(64);
expect(session.userId).toBe(user.id);
expect(session.expiresAt).toBeInstanceOf(Date);
const retrieved = getSession(db, session.id);
const retrieved = await getSession(db, session.id);
expect(retrieved).not.toBeNull();
expect(retrieved!.id).toBe(session.id);
});
it("returns null for expired session (expiryDays = -1)", async () => {
const user = await createUser(db, "admin", "secret123");
const session = createSession(db, user.id, -1);
const session = await createSession(db, user.id, -1);
const retrieved = getSession(db, session.id);
const retrieved = await getSession(db, session.id);
expect(retrieved).toBeNull();
});
it("deletes a session", async () => {
const user = await createUser(db, "admin", "secret123");
const session = createSession(db, user.id);
const session = await createSession(db, user.id);
deleteSession(db, session.id);
await deleteSession(db, session.id);
const retrieved = getSession(db, session.id);
const retrieved = await getSession(db, session.id);
expect(retrieved).toBeNull();
});
});
@@ -144,7 +144,7 @@ describe("Auth Service", () => {
it("deletes key so it is no longer valid", async () => {
const result = await createApiKey(db, "test-key");
deleteApiKey(db, result.id);
await deleteApiKey(db, result.id);
const isValid = await verifyApiKey(db, result.rawKey);
expect(isValid).toBe(false);
@@ -154,7 +154,7 @@ describe("Auth Service", () => {
await createApiKey(db, "key-one");
await createApiKey(db, "key-two");
const keys = listApiKeys(db);
const keys = await listApiKeys(db);
expect(keys).toHaveLength(2);
expect(keys[0].name).toBe("key-one");
expect(keys[1].name).toBe("key-two");