|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { encryptedTable, types } from '@/eql/v3' |
| 3 | +import type { IntrospectionResult } from '@/supabase/introspect' |
| 4 | +import { |
| 5 | + assertModelledDomains, |
| 6 | + mergeDeclaredTables, |
| 7 | + synthesizeTables, |
| 8 | +} from '@/supabase/schema-builder' |
| 9 | + |
| 10 | +const introspection: IntrospectionResult = [ |
| 11 | + { |
| 12 | + tableName: 'users', |
| 13 | + columns: [ |
| 14 | + { columnName: 'id', domainName: null }, |
| 15 | + { columnName: 'email', domainName: 'text_search' }, |
| 16 | + { columnName: 'amount', domainName: 'integer_ord' }, |
| 17 | + { columnName: 'note', domainName: null }, |
| 18 | + { columnName: 'weird', domainName: 'not_a_domain' }, // unknown → plaintext |
| 19 | + ], |
| 20 | + }, |
| 21 | +] |
| 22 | + |
| 23 | +describe('synthesizeTables', () => { |
| 24 | + it('builds an EncryptedTable with only the recognised domain columns', () => { |
| 25 | + const { tables } = synthesizeTables(introspection) |
| 26 | + const users = tables.get('users') |
| 27 | + expect(users).toBeDefined() |
| 28 | + expect(Object.keys(users!.columnBuilders).sort()).toEqual([ |
| 29 | + 'amount', |
| 30 | + 'email', |
| 31 | + ]) |
| 32 | + }) |
| 33 | + |
| 34 | + it('records the FULL column list (encrypted + plaintext) for select(*)', () => { |
| 35 | + const { allColumns } = synthesizeTables(introspection) |
| 36 | + expect(allColumns.get('users')).toEqual([ |
| 37 | + 'id', |
| 38 | + 'email', |
| 39 | + 'amount', |
| 40 | + 'note', |
| 41 | + 'weird', |
| 42 | + ]) |
| 43 | + }) |
| 44 | + |
| 45 | + it('synthesizes a domain column byte-identically to a declared column', () => { |
| 46 | + const { tables } = synthesizeTables(introspection) |
| 47 | + const synthesized = tables.get('users')!.build() |
| 48 | + const declared = encryptedTable('users', { |
| 49 | + email: types.TextSearch('email'), |
| 50 | + amount: types.IntegerOrd('amount'), |
| 51 | + }).build() |
| 52 | + expect(synthesized.columns.email).toEqual(declared.columns.email) |
| 53 | + expect(synthesized.columns.amount).toEqual(declared.columns.amount) |
| 54 | + }) |
| 55 | + |
| 56 | + it('treats an Object.prototype-named domain as plaintext, not a domain', () => { |
| 57 | + // `DOMAIN_REGISTRY['constructor']` would be truthy on a plain object; the |
| 58 | + // null prototype + Object.hasOwn guard keep this column plaintext. |
| 59 | + const prototypeNamed: IntrospectionResult = [ |
| 60 | + { |
| 61 | + tableName: 'weird', |
| 62 | + columns: [ |
| 63 | + { columnName: 'a', domainName: 'constructor' }, |
| 64 | + { columnName: 'b', domainName: '__proto__' }, |
| 65 | + { columnName: 'c', domainName: 'toString' }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + ] |
| 69 | + const { tables, allColumns } = synthesizeTables(prototypeNamed) |
| 70 | + expect(Object.keys(tables.get('weird')!.columnBuilders)).toEqual([]) |
| 71 | + expect(allColumns.get('weird')).toEqual(['a', 'b', 'c']) |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe('mergeDeclaredTables', () => { |
| 76 | + it('lets a declared TextSearch column keep its tuned match options', () => { |
| 77 | + const synth = synthesizeTables(introspection) |
| 78 | + const declaredTable = encryptedTable('users', { |
| 79 | + email: types |
| 80 | + .TextSearch('email') |
| 81 | + .freeTextSearch({ include_original: false }), |
| 82 | + }) |
| 83 | + const merged = mergeDeclaredTables(synth, { users: declaredTable }) |
| 84 | + const built = merged.tables.get('users')!.build() |
| 85 | + expect(built.columns.email.indexes.match?.include_original).toBe(false) |
| 86 | + expect(built.columns.amount).toBeDefined() |
| 87 | + expect(merged.allColumns.get('users')).toEqual( |
| 88 | + synth.allColumns.get('users'), |
| 89 | + ) |
| 90 | + }) |
| 91 | + |
| 92 | + it('preserves a declared property→DB-name rename', () => { |
| 93 | + const renamed: IntrospectionResult = [ |
| 94 | + { |
| 95 | + tableName: 'events', |
| 96 | + columns: [ |
| 97 | + { columnName: 'id', domainName: null }, |
| 98 | + { columnName: 'created_on', domainName: 'timestamp_ord' }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + ] |
| 102 | + const synth = synthesizeTables(renamed) |
| 103 | + const declaredTable = encryptedTable('events', { |
| 104 | + createdAt: types.TimestampOrd('created_on'), |
| 105 | + }) |
| 106 | + const merged = mergeDeclaredTables(synth, { events: declaredTable }) |
| 107 | + const table = merged.tables.get('events')! |
| 108 | + expect(table.buildColumnKeyMap()).toEqual({ createdAt: 'created_on' }) |
| 109 | + expect(Object.keys(table.columnBuilders)).toEqual(['createdAt']) |
| 110 | + }) |
| 111 | +}) |
| 112 | + |
| 113 | +describe('assertModelledDomains', () => { |
| 114 | + it('passes when every EQL domain in use is modelled', () => { |
| 115 | + const eqlDomains = new Set(['text_search', 'integer_ord']) |
| 116 | + expect(() => assertModelledDomains(introspection, eqlDomains)).not.toThrow() |
| 117 | + }) |
| 118 | + |
| 119 | + it('throws naming the column + domain for a recognised-but-unmodelled domain', () => { |
| 120 | + const withOpe: IntrospectionResult = [ |
| 121 | + { |
| 122 | + tableName: 'metrics', |
| 123 | + columns: [ |
| 124 | + { columnName: 'id', domainName: null }, |
| 125 | + { columnName: 'score', domainName: 'integer_ord_ope' }, |
| 126 | + ], |
| 127 | + }, |
| 128 | + ] |
| 129 | + // integer_ord_ope IS an EQL v3 domain, but has no types factory. |
| 130 | + const eqlDomains = new Set(['integer_ord_ope']) |
| 131 | + expect(() => assertModelledDomains(withOpe, eqlDomains)).toThrow( |
| 132 | + /metrics\.score.*integer_ord_ope|integer_ord_ope.*metrics\.score/, |
| 133 | + ) |
| 134 | + }) |
| 135 | + |
| 136 | + it('does NOT throw for a user jsonb domain that is not an EQL domain', () => { |
| 137 | + const withUserDomain: IntrospectionResult = [ |
| 138 | + { |
| 139 | + tableName: 'docs', |
| 140 | + columns: [{ columnName: 'body', domainName: 'my_json' }], |
| 141 | + }, |
| 142 | + ] |
| 143 | + // my_json is NOT in eqlDomains → plaintext passthrough, no throw. |
| 144 | + expect(() => assertModelledDomains(withUserDomain, new Set())).not.toThrow() |
| 145 | + }) |
| 146 | +}) |
0 commit comments