|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { selectRunOpsTopology } from "~/db.server"; |
| 3 | +import { computeRunOpsSplitReadEnabled } from "~/v3/runOpsMigration/runOpsSplitReadGate"; |
| 4 | + |
| 5 | +// Glue test: nothing previously asserted that selectRunOpsTopology's ACTUAL output, fed into |
| 6 | +// computeRunOpsSplitReadEnabled, yields the right gate. Each unit was only tested against |
| 7 | +// hand-rolled objects, so a refactor that made the NEW client alias a control-plane client |
| 8 | +// (even with correct URLs) would silently disable read fan-out with zero test failure. |
| 9 | +const cp = { writer: { __tag: "cp-writer" } as any, replica: { __tag: "cp-replica" } as any }; |
| 10 | + |
| 11 | +describe("selectRunOpsTopology -> computeRunOpsSplitReadEnabled (seam)", () => { |
| 12 | + it("split-configured with a genuinely distinct NEW client: gate TRUE, no warn", () => { |
| 13 | + const dedicatedNew = { __tag: "dedicated-new" } as any; |
| 14 | + const topo = selectRunOpsTopology( |
| 15 | + { splitEnabled: true, legacyUrl: "postgres://legacy", newUrl: "postgres://new" }, |
| 16 | + { |
| 17 | + controlPlane: cp, |
| 18 | + buildNewWriter: vi.fn().mockReturnValue(dedicatedNew), |
| 19 | + buildNewReplica: vi.fn(), |
| 20 | + } |
| 21 | + ); |
| 22 | + const warn = vi.fn(); |
| 23 | + |
| 24 | + const enabled = computeRunOpsSplitReadEnabled({ |
| 25 | + newReplica: topo.newRunOps.replica, |
| 26 | + controlPlaneWriter: topo.controlPlane.writer, |
| 27 | + controlPlaneReplica: topo.controlPlane.replica, |
| 28 | + hasNewUrl: true, |
| 29 | + hasLegacyUrl: true, |
| 30 | + logger: { warn }, |
| 31 | + }); |
| 32 | + |
| 33 | + expect(enabled).toBe(true); |
| 34 | + expect(warn).not.toHaveBeenCalled(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("regression: both URLs set but the client factory aliases the control-plane instance -> gate FALSE, warn fires", () => { |
| 38 | + // Stand-in for the bug this test guards against: a builder refactor that accidentally |
| 39 | + // returns the shared control-plane client instead of opening a dedicated connection. |
| 40 | + const topo = selectRunOpsTopology( |
| 41 | + { splitEnabled: true, legacyUrl: "postgres://legacy", newUrl: "postgres://new" }, |
| 42 | + { |
| 43 | + controlPlane: cp, |
| 44 | + buildNewWriter: vi.fn().mockReturnValue(cp.replica), |
| 45 | + buildNewReplica: vi.fn(), |
| 46 | + } |
| 47 | + ); |
| 48 | + const warn = vi.fn(); |
| 49 | + |
| 50 | + const enabled = computeRunOpsSplitReadEnabled({ |
| 51 | + newReplica: topo.newRunOps.replica, |
| 52 | + controlPlaneWriter: topo.controlPlane.writer, |
| 53 | + controlPlaneReplica: topo.controlPlane.replica, |
| 54 | + hasNewUrl: true, |
| 55 | + hasLegacyUrl: true, |
| 56 | + logger: { warn }, |
| 57 | + }); |
| 58 | + |
| 59 | + expect(enabled).toBe(false); |
| 60 | + expect(warn).toHaveBeenCalledTimes(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it("single mode (URLs unset): topology naturally collapses to control-plane refs -> gate FALSE, no warn", () => { |
| 64 | + const topo = selectRunOpsTopology( |
| 65 | + { splitEnabled: false }, |
| 66 | + { |
| 67 | + controlPlane: cp, |
| 68 | + buildNewWriter: vi.fn(), |
| 69 | + buildNewReplica: vi.fn(), |
| 70 | + } |
| 71 | + ); |
| 72 | + const warn = vi.fn(); |
| 73 | + |
| 74 | + const enabled = computeRunOpsSplitReadEnabled({ |
| 75 | + newReplica: topo.newRunOps.replica, |
| 76 | + controlPlaneWriter: topo.controlPlane.writer, |
| 77 | + controlPlaneReplica: topo.controlPlane.replica, |
| 78 | + hasNewUrl: false, |
| 79 | + hasLegacyUrl: false, |
| 80 | + logger: { warn }, |
| 81 | + }); |
| 82 | + |
| 83 | + expect(enabled).toBe(false); |
| 84 | + expect(warn).not.toHaveBeenCalled(); |
| 85 | + expect(topo.newRunOps.replica).toBe(cp.replica); // sanity: genuinely the shared instance |
| 86 | + }); |
| 87 | +}); |
0 commit comments