|
| 1 | +import 'dotenv/config' |
| 2 | +import postgres from 'postgres' |
| 3 | +import { afterAll, beforeAll, expect, it } from 'vitest' |
| 4 | +import { factoryForDomain } from '@/eql/v3/domain-registry' |
| 5 | +import { introspect } from '@/supabase/introspect' |
| 6 | +import { |
| 7 | + assertModelledDomains, |
| 8 | + synthesizeTables, |
| 9 | +} from '@/supabase/schema-builder' |
| 10 | +import { installEqlV3IfNeeded } from './helpers/eql-v3' |
| 11 | +import { describeLivePgOnly, LIVE_PG_ENABLED } from './helpers/live-gate' |
| 12 | + |
| 13 | +const databaseUrl = process.env.DATABASE_URL |
| 14 | +const sql = LIVE_PG_ENABLED |
| 15 | + ? postgres(databaseUrl as string, { prepare: false }) |
| 16 | + : (undefined as unknown as postgres.Sql) |
| 17 | + |
| 18 | +const MODELLED = 'protect_ci_v3_introspect' |
| 19 | +const UNMODELLED = 'protect_ci_v3_unmodelled' |
| 20 | + |
| 21 | +beforeAll(async () => { |
| 22 | + if (!LIVE_PG_ENABLED) return |
| 23 | + await installEqlV3IfNeeded(sql) |
| 24 | + await sql.unsafe(`DROP TABLE IF EXISTS ${MODELLED}`) |
| 25 | + await sql.unsafe(`DROP TABLE IF EXISTS ${UNMODELLED}`) |
| 26 | + await sql.unsafe(` |
| 27 | + CREATE TABLE ${MODELLED} ( |
| 28 | + id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY, |
| 29 | + email public.text_search NOT NULL, |
| 30 | + amount public.integer_ord NOT NULL, |
| 31 | + note TEXT, |
| 32 | + meta jsonb |
| 33 | + ) |
| 34 | + `) |
| 35 | + // Columns typed with EQL domains that have NO types factory. |
| 36 | + await sql.unsafe(` |
| 37 | + CREATE TABLE ${UNMODELLED} ( |
| 38 | + id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY, |
| 39 | + score public.integer_ord_ope NOT NULL, |
| 40 | + doc public.json |
| 41 | + ) |
| 42 | + `) |
| 43 | +}, 30000) |
| 44 | + |
| 45 | +afterAll(async () => { |
| 46 | + if (!LIVE_PG_ENABLED) return |
| 47 | + await sql.unsafe(`DROP TABLE IF EXISTS ${MODELLED}`) |
| 48 | + await sql.unsafe(`DROP TABLE IF EXISTS ${UNMODELLED}`) |
| 49 | + await sql.end() |
| 50 | +}, 30000) |
| 51 | + |
| 52 | +describeLivePgOnly('eql_v3 supabase introspection', () => { |
| 53 | + it('detects EQL v3 domains and classifies plaintext columns', async () => { |
| 54 | + const { tables } = await introspect(databaseUrl as string) |
| 55 | + const table = tables.find((t) => t.tableName === MODELLED) |
| 56 | + expect(table).toBeDefined() |
| 57 | + const domains = Object.fromEntries( |
| 58 | + table!.columns.map((c) => [c.columnName, c.domainName]), |
| 59 | + ) |
| 60 | + expect(domains.email).toBe('text_search') |
| 61 | + expect(domains.amount).toBe('integer_ord') |
| 62 | + // Plaintext + plain jsonb → NULL (udt_name is jsonb for all of these). |
| 63 | + expect(domains.id).toBeNull() |
| 64 | + expect(domains.note).toBeNull() |
| 65 | + expect(domains.meta).toBeNull() |
| 66 | + }, 30000) |
| 67 | + |
| 68 | + it('round-trips the domain → builder mapping via synthesizeTables', async () => { |
| 69 | + const { tables, eqlDomains } = await introspect(databaseUrl as string) |
| 70 | + // Sanity: the modelled domains are recognised as EQL domains (by COMMENT). |
| 71 | + expect(eqlDomains.has('text_search')).toBe(true) |
| 72 | + expect(eqlDomains.has('integer_ord')).toBe(true) |
| 73 | + |
| 74 | + const { tables: synth, allColumns } = synthesizeTables(tables) |
| 75 | + const table = synth.get(MODELLED) |
| 76 | + expect(Object.keys(table!.columnBuilders).sort()).toEqual([ |
| 77 | + 'amount', |
| 78 | + 'email', |
| 79 | + ]) |
| 80 | + expect(table!.columnBuilders.email.getEqlType()).toBe('public.text_search') |
| 81 | + expect(table!.columnBuilders.amount.getEqlType()).toBe('public.integer_ord') |
| 82 | + expect(allColumns.get(MODELLED)).toEqual([ |
| 83 | + 'id', |
| 84 | + 'email', |
| 85 | + 'amount', |
| 86 | + 'note', |
| 87 | + 'meta', |
| 88 | + ]) |
| 89 | + }, 30000) |
| 90 | + |
| 91 | + it('detects unmodelled EQL domains (by COMMENT) and the guard rejects them', async () => { |
| 92 | + const { tables, eqlDomains } = await introspect(databaseUrl as string) |
| 93 | + |
| 94 | + // The COMMENT predicate recognises these as EQL domains... |
| 95 | + expect(eqlDomains.has('integer_ord_ope')).toBe(true) |
| 96 | + expect(eqlDomains.has('json')).toBe(true) |
| 97 | + // ...and they have no types factory (genuinely unmodelled)... |
| 98 | + expect(factoryForDomain('integer_ord_ope')).toBeUndefined() |
| 99 | + expect(factoryForDomain('json')).toBeUndefined() |
| 100 | + |
| 101 | + // Scope the guard to THIS test's tables. `introspect` returns every table in |
| 102 | + // `public`, and a developer's DATABASE_URL may already carry an unmodelled |
| 103 | + // EQL column of its own — the assertion would then pass for the wrong |
| 104 | + // reason, or fail naming a domain this test never created. |
| 105 | + const modelledOnly = tables.filter((t) => t.tableName === MODELLED) |
| 106 | + const scoped = tables.filter( |
| 107 | + (t) => t.tableName === MODELLED || t.tableName === UNMODELLED, |
| 108 | + ) |
| 109 | + expect(modelledOnly).toHaveLength(1) |
| 110 | + expect(scoped).toHaveLength(2) |
| 111 | + |
| 112 | + // The modelled table alone must NOT trip the guard. |
| 113 | + expect(() => assertModelledDomains(modelledOnly, eqlDomains)).not.toThrow() |
| 114 | + |
| 115 | + // ...and the unmodelled one throws, naming the offending column/domain. |
| 116 | + expect(() => assertModelledDomains(scoped, eqlDomains)).toThrow( |
| 117 | + /integer_ord_ope|public\.json/, |
| 118 | + ) |
| 119 | + }, 30000) |
| 120 | +}) |
0 commit comments