import fs from "node:fs"; import path from "node:path"; import { describe, expect, test } from "vitest"; import { resolveRepoPath } from "~/utils/testing/repoPaths"; const repoRoot = resolveRepoPath("."); function readArtifact(relativePath: string): string { return fs.readFileSync(path.join(repoRoot, relativePath), "utf8"); } describe("booking DB rollout artifacts", () => { test("hard double-booking guard uses an idempotent exclusion constraint for active appointments", () => { // tsrange, not tstzrange: startAt/endAt are TIMESTAMP(3) (UTC by // convention) and the timestamp->timestamptz cast is only STABLE, which // Postgres rejects inside index expressions (42P17). const reviewedArtifact = readArtifact("prisma/rls/booking-constraints.sql"); const baseline = readArtifact("prisma/migrations/00000000000000_hivegrid_baseline/migration.sql"); expect(reviewedArtifact).toContain("CREATE EXTENSION IF NOT EXISTS btree_gist"); expect(reviewedArtifact).toContain("23P01"); expect(baseline).toContain("CREATE EXTENSION IF NOT EXISTS btree_gist"); expect(baseline).toContain("appointment_no_overlap"); expect(baseline).toContain("EXCLUDE USING gist"); // Tenant-scoped on purpose: the slot engine is tenant-scoped and demo // tenants share provider users — a tenant-agnostic constraint leaked // cross-tenant busy state and broke the 2026-07-17 staging rollout. expect(baseline).toContain('"tenantId" WITH ='); expect(baseline).toContain('"providerUserId" WITH ='); expect(baseline).toContain('tsrange("startAt", "endAt", \'[)\'::text) WITH &&'); expect(baseline).not.toContain("tstzrange("); expect(baseline).toContain("status = ANY (ARRAY['scheduled'::public.\"AppointmentStatus\", 'held'::public.\"AppointmentStatus\"])"); }); test("booking RLS artifact covers every tenant-scoped booking table and fails closed without tenant GUC", () => { const sql = readArtifact("prisma/rls/booking-isolation.sql"); const expectedTables = ["BookingPage", "BookingPageProvider", "AvailabilityRule", "AvailabilityException", "Appointment"]; for (const table of expectedTables) { expect(sql).toContain(`'${table}'`); } expect(sql).toContain("ENABLE ROW LEVEL SECURITY"); expect(sql).toContain("FORCE ROW LEVEL SECURITY"); expect(sql).toContain('"tenantId" = current_setting(\'\'app.tenant_id\'\', true)'); expect(sql).toContain("GRANT SELECT, INSERT, UPDATE, DELETE"); expect(sql).toContain("hivegrid_app"); }); });