import { readFile } from "node:fs/promises"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { assertPortalLegacyCommerceRecutEnvironment } from "./assert-portal-legacy-commerce-recut-db-push.mjs"; const root = process.cwd(); const sqlPath = join(root, "prisma/rls/portal-legacy-commerce-recut-pre-db-push.sql"); const expectedTables = [ "PortalCheckoutSessionStatus", "PortalSubscriptionFeature", "PortalSubscriptionPrice", "PortalSubscriptionProduct", "PortalUserSubscription", "PortalUserSubscriptionProduct", "PortalUserSubscriptionProductPrice", ]; const expectedPolicies = expectedTables.map((table) => `hg_${table}_all`); function extractSqlArray(sql, variableName) { const match = sql.match( new RegExp(`${variableName} CONSTANT text\\[\\] := ARRAY\\[([\\s\\S]*?)\\];`), ); expect(match, `${variableName} must be declared as an explicit SQL array`).not.toBeNull(); return [...match[1].matchAll(/'([^']+)'/g)].map((entry) => entry[1]); } describe("VA-P26a legacy-commerce feature-branch recut", () => { it("drops exactly the seven reviewed historical policies and fails on unknown policy dependencies", async () => { const sql = await readFile(sqlPath, "utf8"); expect(extractSqlArray(sql, "legacy_tables")).toEqual(expectedTables); expect(extractSqlArray(sql, "legacy_policies")).toEqual(expectedPolicies); expect(sql).toContain("DROP POLICY IF EXISTS %I ON %I.%I"); expect(sql).toContain("FROM pg_policy AS policy"); expect(sql).toContain("found non-allowlisted policies"); expect(sql).toMatch(/^BEGIN;[\s\S]*COMMIT;\s*$/m); expect(sql).not.toMatch(/\bCASCADE\b/i); expect(sql).not.toContain("20260713190000_appwide_rls_cutover/migration.sql"); }); it("is wired immediately before live feature-schema db push but not into the merge parity gate", async () => { const [packageJson, liveGate, parityGate] = await Promise.all([ readFile(join(root, "package.json"), "utf8"), readFile(join(root, "scripts/verify-rls-live.sh"), "utf8"), readFile(join(root, "scripts/verify-migrations-schema-parity.sh"), "utf8"), ]); const scripts = JSON.parse(packageJson).scripts; expect(scripts["db:prepare:portal-legacy-commerce-recut"]).toBe( "node scripts/assert-portal-legacy-commerce-recut-db-push.mjs && pnpm exec prisma db execute --file prisma/rls/portal-legacy-commerce-recut-pre-db-push.sql --config prisma.config.ts", ); const preparationIndex = liveGate.indexOf( "pnpm run db:prepare:portal-legacy-commerce-recut", ); const dbPushIndex = liveGate.indexOf("pnpm exec prisma db push --accept-data-loss"); expect(preparationIndex).toBeGreaterThan(-1); expect(dbPushIndex).toBeGreaterThan(preparationIndex); expect(liveGate).toContain('CREATE POLICY "va_p26a_unreviewed_probe"'); expect(liveGate).toContain("RECUT_ROLLBACK_POLICY_COUNT"); expect(parityGate).not.toContain("db:prepare:portal-legacy-commerce-recut"); }); it("requires an explicit opt-in and a loopback non-production database", () => { expect(() => assertPortalLegacyCommerceRecutEnvironment({ HIVEGRID_ALLOW_PORTAL_LEGACY_RECUT_DB_PUSH: "1", DIRECT_URL: "postgresql://user:secret@127.0.0.1:5432/rls_live", }), ).not.toThrow(); expect(() => assertPortalLegacyCommerceRecutEnvironment({ DIRECT_URL: "postgresql://user:secret@127.0.0.1:5432/rls_live", }), ).toThrow(/explicit local\/CI feature-branch recut/); expect(() => assertPortalLegacyCommerceRecutEnvironment({ HIVEGRID_ALLOW_PORTAL_LEGACY_RECUT_DB_PUSH: "1", DIRECT_URL: "postgresql://user:secret@db.internal:5432/hivegrid", }), ).toThrow(/loopback/); expect(() => assertPortalLegacyCommerceRecutEnvironment({ HIVEGRID_ALLOW_PORTAL_LEGACY_RECUT_DB_PUSH: "1", DIRECT_URL: "postgresql://user:secret@localhost:5432/hivegrid", SENTRY_ENVIRONMENT: "production", }), ).toThrow(/forbidden/); expect(() => assertPortalLegacyCommerceRecutEnvironment({ HIVEGRID_ALLOW_PORTAL_LEGACY_RECUT_DB_PUSH: "1", DIRECT_URL: "postgresql://user:secret@localhost:5432/hivegrid", NODE_ENV: "staging", }), ).toThrow(/forbidden/); }); it("keeps the merge migration and inverse-SQL obligation explicit", async () => { const ticket = await readFile(join(root, ".planning/ticket-docs/VA-P26a.md"), "utf8"); expect(ticket).toContain("Legacy-Recut DB-Push-/Merge-Vertrag"); expect(ticket).toContain("kein Migrationsersatz"); expect(ticket).toContain("forward-only P26-Migration"); expect(ticket).toContain("Inverse-SQL-Kommentar"); expect(ticket).toContain("verify:migrations-parity` bleibt"); }); });