|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +// @testcontainers/postgresql resolves because Task 0 declared it in apps/webapp/package.json. |
| 3 | +import { PostgreSqlContainer } from "@testcontainers/postgresql"; |
| 4 | +import { computeSplitEnabled } from "~/v3/runOpsMigration/splitMode.server"; |
| 5 | +import { probeDistinctDatabases } from "~/v3/runOpsMigration/distinctDbSentinel.server"; |
| 6 | + |
| 7 | +describe("computeSplitEnabled (pure)", () => { |
| 8 | + it("is OFF by default and never probes when the flag is off", async () => { |
| 9 | + const probe = vi.fn(); |
| 10 | + const result = await computeSplitEnabled( |
| 11 | + { flagEnabled: false, legacyUrl: "postgres://a", newUrl: "postgres://b" }, |
| 12 | + { probe } |
| 13 | + ); |
| 14 | + expect(result).toBe(false); |
| 15 | + expect(probe).not.toHaveBeenCalled(); // self-host opens no second connection |
| 16 | + }); |
| 17 | + |
| 18 | + it("stays single-DB when flag is on but URLs are missing", async () => { |
| 19 | + const probe = vi.fn(); |
| 20 | + expect(await computeSplitEnabled({ flagEnabled: true }, { probe })).toBe(false); |
| 21 | + expect(probe).not.toHaveBeenCalled(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("enables split only when flag is on AND sentinel confirms distinct", async () => { |
| 25 | + const probe = vi.fn().mockResolvedValue({ distinct: true }); |
| 26 | + expect( |
| 27 | + await computeSplitEnabled( |
| 28 | + { flagEnabled: true, legacyUrl: "postgres://a", newUrl: "postgres://b" }, |
| 29 | + { probe } |
| 30 | + ) |
| 31 | + ).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it("stays single-DB when sentinel reports NOT distinct", async () => { |
| 35 | + const probe = vi.fn().mockResolvedValue({ distinct: false, reason: "same DB" }); |
| 36 | + expect( |
| 37 | + await computeSplitEnabled( |
| 38 | + { flagEnabled: true, legacyUrl: "postgres://a", newUrl: "postgres://b" }, |
| 39 | + { probe } |
| 40 | + ) |
| 41 | + ).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + // Migration-family unreachability proof: with the flag off the gate returns false and |
| 45 | + // no probe runs. Downstream migration-family units are required to early-return on |
| 46 | + // !isSplitEnabled(); this unit proves the gate's value, each downstream unit's own test |
| 47 | + // proves it honors the gate. (W0-FND-08's "split OFF collapses to a single prisma/$replica |
| 48 | + // pair with no second connection opened" depends on this no-probe behavior.) |
| 49 | + it("is provably unreachable (no probe) when the flag is off", async () => { |
| 50 | + const probe = vi.fn(); |
| 51 | + expect( |
| 52 | + await computeSplitEnabled( |
| 53 | + { flagEnabled: false, legacyUrl: "postgres://a", newUrl: "postgres://b" }, |
| 54 | + { probe } |
| 55 | + ) |
| 56 | + ).toBe(false); |
| 57 | + expect(probe).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("distinct-DB sentinel (real Postgres)", () => { |
| 62 | + it("reports NOT distinct when both URLs hit the same physical cluster", async () => { |
| 63 | + const pg = await new PostgreSqlContainer("docker.io/postgres:14").start(); |
| 64 | + try { |
| 65 | + const url = pg.getConnectionUri(); |
| 66 | + const result = await probeDistinctDatabases(url, url); |
| 67 | + expect(result.distinct).toBe(false); // identical URL -> false-split prevented |
| 68 | + } finally { |
| 69 | + await pg.stop(); |
| 70 | + } |
| 71 | + }, 60_000); |
| 72 | + |
| 73 | + it("reports distinct when URLs hit two separate clusters (PG14 legacy + PG17 new)", async () => { |
| 74 | + const legacy = await new PostgreSqlContainer("docker.io/postgres:14").start(); |
| 75 | + const next = await new PostgreSqlContainer("docker.io/postgres:17").start(); |
| 76 | + try { |
| 77 | + const result = await probeDistinctDatabases( |
| 78 | + legacy.getConnectionUri(), |
| 79 | + next.getConnectionUri() |
| 80 | + ); |
| 81 | + expect(result.distinct).toBe(true); |
| 82 | + } finally { |
| 83 | + await legacy.stop(); |
| 84 | + await next.stop(); |
| 85 | + } |
| 86 | + }, 120_000); |
| 87 | + |
| 88 | + it("fails closed (single-DB) when a DB is unreachable", async () => { |
| 89 | + const result = await probeDistinctDatabases( |
| 90 | + "postgresql://nouser:nopass@127.0.0.1:1/none", |
| 91 | + "postgresql://nouser:nopass@127.0.0.1:2/none" |
| 92 | + ); |
| 93 | + expect(result.distinct).toBe(false); |
| 94 | + }, 30_000); |
| 95 | +}); |
0 commit comments