test(db): add env-gated integration smoke test for real-schema reads

This commit is contained in:
Simo committed 2026-06-27 14:37:13 +02:00
1 parent 2a304c94a4
commit 7bd4f2b28e
1 file changed
+47
+47
View File
@@ -0,0 +1,47 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import type { Pool } from "mysql2/promise";
import { createDb, type Database } from "../client.js";
import { makeUsersRepo } from "../repositories/users-repo.js";
import { makeCurrencyRepo } from "../repositories/currency-repo.js";
import { makeSettingsRepo } from "../repositories/settings-repo.js";
import { SettingsService } from "@atomcms/settings";
const url = process.env.DATABASE_URL;
// Skips cleanly when no real database is configured (e.g. local dev without DB).
// The connection is built in beforeAll so it is NOT created when the suite is
// skipped (a skipped describe still executes its body during collection).
describe.skipIf(!url)("DB reads against the real schema", () => {
let db: Database;
let pool: Pool;
beforeAll(() => {
const created = createDb(url as string);
db = created.db;
pool = created.pool;
});
afterAll(async () => {
await pool.end();
});
it("reads a user by username", async () => {
const user = await makeUsersRepo(db).findByUsername("Systemaccount");
expect(user).not.toBeNull();
expect(user?.username).toBe("Systemaccount");
});
it("reads composite-key currency rows for a user", async () => {
const user = await makeUsersRepo(db).findByUsername("Systemaccount");
const balances = await makeCurrencyRepo(db).forUser(user!.id);
// Map<type, amount>; presence is enough to prove composite-key reads work.
expect(balances).toBeInstanceOf(Map);
});
it("loads settings through SettingsService", async () => {
const repo = makeSettingsRepo(db);
const svc = new SettingsService(() => repo.all());
const name = await svc.get("hotel_name", "unknown");
expect(typeof name).toBe("string");
});
});