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", () => { const sql = readArtifact("prisma/rls/booking-constraints.sql"); expect(sql).toContain("CREATE EXTENSION IF NOT EXISTS btree_gist"); expect(sql).toContain("appointment_no_overlap"); expect(sql).toContain('EXCLUDE USING gist'); expect(sql).toContain('"providerUserId" WITH ='); expect(sql).toContain('tstzrange("startAt", "endAt", \'\'[)\'\') WITH &&'); expect(sql).toContain("WHERE (status IN (''scheduled'', ''held''))"); expect(sql).toContain("23P01"); }); 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"); }); });