From 7bd4f2b28e03a8206ab30124ce1e555c6909d138 Mon Sep 17 00:00:00 2001 From: simoleo89 Date: Sat, 27 Jun 2026 14:37:13 +0200 Subject: [PATCH] test(db): add env-gated integration smoke test for real-schema reads --- packages/db/src/integration/reads.test.ts | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 packages/db/src/integration/reads.test.ts diff --git a/packages/db/src/integration/reads.test.ts b/packages/db/src/integration/reads.test.ts new file mode 100644 index 00000000..6fc8cde6 --- /dev/null +++ b/packages/db/src/integration/reads.test.ts @@ -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; 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"); + }); +});