Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,128 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
__resetDbClientSingletonsForTests,
|
||||
createDbClient,
|
||||
getDbClient,
|
||||
getExtendedDbClient,
|
||||
getReadDbClient,
|
||||
getWriteDbClient,
|
||||
} from "./client.server.js";
|
||||
|
||||
const clientServerSourcePath = path.join(path.dirname(fileURLToPath(import.meta.url)), "./client.server.ts");
|
||||
const clientServerSource = () => readFileSync(clientServerSourcePath, "utf8");
|
||||
|
||||
describe("@toolctrl/db server client boundary", () => {
|
||||
it("exports a createDbClient factory from the package-owned generated client", () => {
|
||||
expect(createDbClient).toEqual(expect.any(Function));
|
||||
});
|
||||
|
||||
it("reuses a singleton across runtime environments", () => {
|
||||
const existingClient = {} as ReturnType<typeof createDbClient>;
|
||||
global.__db = existingClient;
|
||||
|
||||
expect(getDbClient({ nodeEnv: "development" })).toBe(existingClient);
|
||||
expect(getDbClient({ nodeEnv: "production" })).toBe(existingClient);
|
||||
|
||||
global.__db = undefined;
|
||||
});
|
||||
|
||||
it("keeps explicit client options isolated from the process singleton", () => {
|
||||
const existingClient = {} as ReturnType<typeof createDbClient>;
|
||||
global.__db = existingClient;
|
||||
|
||||
expect(getDbClient({ connectionString: "postgresql://user:pass@example.test:5432/db" })).not.toBe(existingClient);
|
||||
|
||||
global.__db = undefined;
|
||||
});
|
||||
|
||||
it("wires database error diagnostics through the structured logger while preserving custom callbacks", () => {
|
||||
const source = clientServerSource();
|
||||
|
||||
expect(source).toContain('import { createChildLogger, defaultLogger } from "@toolctrl/logger";');
|
||||
expect(source).toContain('const dbLogger = createChildLogger(defaultLogger, { component: "db" });');
|
||||
expect(source).toContain('dbLogger.error({ error: event }, "database error");');
|
||||
expect(source).toContain("options.onError ?? logDatabaseError");
|
||||
});
|
||||
|
||||
it("does not leave active console diagnostics in the server database client", () => {
|
||||
const source = clientServerSource();
|
||||
const withoutLineComments = source.replaceAll(/\/\/.*$/gm, "");
|
||||
|
||||
expect(withoutLineComments).not.toMatch(/\bconsole\.(?:log|error|warn|info)\s*\(/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("R/W-split singletons", () => {
|
||||
const originalReadUrl = process.env.DATABASE_READ_URL;
|
||||
|
||||
afterEach(() => {
|
||||
if (originalReadUrl === undefined) {
|
||||
delete process.env.DATABASE_READ_URL;
|
||||
} else {
|
||||
process.env.DATABASE_READ_URL = originalReadUrl;
|
||||
}
|
||||
__resetDbClientSingletonsForTests();
|
||||
});
|
||||
|
||||
it("falls back to the write singleton when DATABASE_READ_URL is unset", () => {
|
||||
delete process.env.DATABASE_READ_URL;
|
||||
const existingWriter = {} as ReturnType<typeof createDbClient>;
|
||||
global.__db = existingWriter;
|
||||
global.__dbRead = undefined;
|
||||
|
||||
const read = getReadDbClient();
|
||||
const write = getWriteDbClient();
|
||||
|
||||
expect(read).toBe(existingWriter);
|
||||
expect(read).toBe(write);
|
||||
});
|
||||
|
||||
it("falls back to the write singleton when DATABASE_READ_URL is an empty string", () => {
|
||||
process.env.DATABASE_READ_URL = "";
|
||||
const existingWriter = {} as ReturnType<typeof createDbClient>;
|
||||
global.__db = existingWriter;
|
||||
global.__dbRead = undefined;
|
||||
|
||||
expect(getReadDbClient()).toBe(existingWriter);
|
||||
});
|
||||
|
||||
it("creates a distinct read client when DATABASE_READ_URL is set", () => {
|
||||
process.env.DATABASE_READ_URL = "postgresql://reader:pw@replica.test:5432/db";
|
||||
const existingWriter = {} as ReturnType<typeof createDbClient>;
|
||||
global.__db = existingWriter;
|
||||
global.__dbRead = undefined;
|
||||
|
||||
const read = getReadDbClient();
|
||||
|
||||
expect(read).not.toBe(existingWriter);
|
||||
expect(getReadDbClient()).toBe(read); // memoised
|
||||
});
|
||||
|
||||
it("exposes .read and .write accessors via $extends and lets default API flow to the write client", () => {
|
||||
delete process.env.DATABASE_READ_URL;
|
||||
const existingWriter = createDbClient({ connectionString: "postgresql://user:pass@primary.test:5432/db" });
|
||||
global.__db = existingWriter;
|
||||
global.__dbRead = undefined;
|
||||
|
||||
const extended = getExtendedDbClient();
|
||||
|
||||
expect(extended.write).toBe(existingWriter);
|
||||
expect(extended.read).toBe(existingWriter); // fallback semantics
|
||||
// Default API (e.g. extended.user) must still be present — $extends preserves it.
|
||||
expect(extended).toHaveProperty("user");
|
||||
});
|
||||
|
||||
it("routes .read and .write to different underlying clients when a replica URL is configured", () => {
|
||||
process.env.DATABASE_READ_URL = "postgresql://reader:pw@replica.test:5432/db";
|
||||
global.__db = undefined;
|
||||
global.__dbRead = undefined;
|
||||
|
||||
const extended = getExtendedDbClient();
|
||||
|
||||
expect(extended.read).not.toBe(extended.write);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user