/** * TASK-0135: routing-helper unit tests. The PrismaClient is LAZY (no * connection at construction), so these assert the ROUTING without a DB: * which client `db.read()` / `db.write()` return, and the hasReplica flag. * The live behavior (a write-then-read-your-write on the same request * always hits the primary) is covered by * tests/integration/replica-routing.integration.test.ts. */ import { describe, expect, it } from "vitest"; import { createDatabaseClients } from "./index.js"; const PRIMARY = "postgresql://factory:factory@localhost:5432/factory"; const REPLICA = "postgresql://factory:factory@localhost:5433/factory"; describe("createDatabaseClients routing (TASK-0135)", () => { it("with a replica URL: read() → replica, write() → primary, hasReplica true", () => { const db = createDatabaseClients({ databaseUrl: PRIMARY, databaseReplicaUrl: REPLICA }); try { expect(db.hasReplica).toBe(true); expect(db.write()).toBe(db.primary); expect(db.read()).toBe(db.reader); expect(db.reader).not.toBe(db.primary); // a distinct replica client } finally { void db.disconnect(); } }); it("without a replica URL: the reader IS the primary (single-node dev/CI)", () => { const db = createDatabaseClients({ databaseUrl: PRIMARY }); try { expect(db.hasReplica).toBe(false); expect(db.read()).toBe(db.primary); expect(db.write()).toBe(db.primary); expect(db.reader).toBe(db.primary); } finally { void db.disconnect(); } }); it("write() is ALWAYS the primary, regardless of replica config", () => { for (const replica of [REPLICA, undefined, ""]) { const db = createDatabaseClients({ databaseUrl: PRIMARY, databaseReplicaUrl: replica }); try { expect(db.write()).toBe(db.primary); } finally { void db.disconnect(); } } }); });