|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { encryptedTable, types } from '@/eql/v3' |
| 3 | +import type { SupabaseClientLike } from '@/supabase' |
| 4 | +import { encryptedSupabaseV3 } from '@/supabase' |
| 5 | +import type { IntrospectionData } from '@/supabase/introspect' |
| 6 | +import { EncryptedQueryBuilderV3Impl } from '@/supabase/query-builder-v3' |
| 7 | + |
| 8 | +// --- Mocks ----------------------------------------------------------------- |
| 9 | +// |
| 10 | +// `vi.mock` factories are hoisted above every import, so they cannot close over |
| 11 | +// a plain top-level `const` (it would still be in its TDZ when the factory runs |
| 12 | +// on first import). `vi.hoisted` lifts the spies alongside them. |
| 13 | + |
| 14 | +const { introspectMock, encryptionMock, createClientMock } = vi.hoisted(() => ({ |
| 15 | + introspectMock: vi.fn<(url: string) => Promise<unknown>>(), |
| 16 | + encryptionMock: vi.fn<(cfg: unknown) => Promise<unknown>>(), |
| 17 | + createClientMock: vi.fn(() => ({ from: () => ({}) })), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/supabase/introspect', async (importActual) => { |
| 21 | + const actual = await importActual<typeof import('@/supabase/introspect')>() |
| 22 | + return { ...actual, introspect: (url: string) => introspectMock(url) } |
| 23 | +}) |
| 24 | + |
| 25 | +vi.mock('@/encryption', async (importActual) => { |
| 26 | + const actual = await importActual<typeof import('@/encryption')>() |
| 27 | + return { ...actual, Encryption: (cfg: unknown) => encryptionMock(cfg) } |
| 28 | +}) |
| 29 | + |
| 30 | +vi.mock('@supabase/supabase-js', () => ({ createClient: createClientMock })) |
| 31 | + |
| 32 | +const fakeClient = { from: () => ({}) } as unknown as SupabaseClientLike |
| 33 | + |
| 34 | +function introspectionOf(data: Partial<IntrospectionData>): IntrospectionData { |
| 35 | + return { tables: data.tables ?? [], eqlDomains: data.eqlDomains ?? new Set() } |
| 36 | +} |
| 37 | + |
| 38 | +const usersIntrospection = introspectionOf({ |
| 39 | + tables: [ |
| 40 | + { |
| 41 | + tableName: 'users', |
| 42 | + columns: [ |
| 43 | + { columnName: 'id', domainName: null }, |
| 44 | + { columnName: 'email', domainName: 'text_search' }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + ], |
| 48 | + eqlDomains: new Set(['text_search']), |
| 49 | +}) |
| 50 | + |
| 51 | +beforeEach(() => { |
| 52 | + introspectMock.mockReset().mockResolvedValue(usersIntrospection) |
| 53 | + encryptionMock.mockReset().mockResolvedValue({}) |
| 54 | + createClientMock.mockClear() |
| 55 | + delete process.env.DATABASE_URL |
| 56 | +}) |
| 57 | +afterEach(() => vi.restoreAllMocks()) |
| 58 | + |
| 59 | +describe('encryptedSupabaseV3 factory', () => { |
| 60 | + it('url+key overload builds a client and introspects the given databaseUrl', async () => { |
| 61 | + await encryptedSupabaseV3('http://sb', 'anon-key', { |
| 62 | + databaseUrl: 'postgres://x', |
| 63 | + }) |
| 64 | + expect(createClientMock).toHaveBeenCalledWith('http://sb', 'anon-key') |
| 65 | + expect(introspectMock).toHaveBeenCalledWith('postgres://x') |
| 66 | + expect(encryptionMock).toHaveBeenCalledTimes(1) |
| 67 | + }) |
| 68 | + |
| 69 | + it('client overload uses the supplied client (no createClient)', async () => { |
| 70 | + await encryptedSupabaseV3(fakeClient, { databaseUrl: 'postgres://x' }) |
| 71 | + expect(createClientMock).not.toHaveBeenCalled() |
| 72 | + }) |
| 73 | + |
| 74 | + it('falls back to process.env.DATABASE_URL', async () => { |
| 75 | + process.env.DATABASE_URL = 'postgres://env' |
| 76 | + await encryptedSupabaseV3(fakeClient) |
| 77 | + expect(introspectMock).toHaveBeenCalledWith('postgres://env') |
| 78 | + }) |
| 79 | + |
| 80 | + it('throws naming DATABASE_URL when no URL is available', async () => { |
| 81 | + await expect(encryptedSupabaseV3(fakeClient)).rejects.toThrow( |
| 82 | + /DATABASE_URL/, |
| 83 | + ) |
| 84 | + expect(introspectMock).not.toHaveBeenCalled() |
| 85 | + }) |
| 86 | + |
| 87 | + it('verifies BEFORE building the encryption client (wrong domain)', async () => { |
| 88 | + // email is text_search in the DB; declaring text_eq must fail... |
| 89 | + const users = encryptedTable('users', { email: types.TextEq('email') }) |
| 90 | + await expect( |
| 91 | + encryptedSupabaseV3(fakeClient, { |
| 92 | + databaseUrl: 'postgres://x', |
| 93 | + schemas: { users }, |
| 94 | + }), |
| 95 | + ).rejects.toThrow(/text_eq|text_search/) |
| 96 | + // ...and Encryption must never be reached. |
| 97 | + expect(encryptionMock).not.toHaveBeenCalled() |
| 98 | + }) |
| 99 | + |
| 100 | + it('passes only non-empty tables to Encryption', async () => { |
| 101 | + introspectMock.mockResolvedValue( |
| 102 | + introspectionOf({ |
| 103 | + tables: [ |
| 104 | + { |
| 105 | + tableName: 'users', |
| 106 | + columns: [{ columnName: 'email', domainName: 'text_search' }], |
| 107 | + }, |
| 108 | + { |
| 109 | + tableName: 'logs', |
| 110 | + columns: [{ columnName: 'line', domainName: null }], |
| 111 | + }, |
| 112 | + ], |
| 113 | + eqlDomains: new Set(['text_search']), |
| 114 | + }), |
| 115 | + ) |
| 116 | + await encryptedSupabaseV3(fakeClient, { databaseUrl: 'postgres://x' }) |
| 117 | + const arg = encryptionMock.mock.calls[0][0] as { schemas: unknown[] } |
| 118 | + expect(arg.schemas).toHaveLength(1) |
| 119 | + }) |
| 120 | + |
| 121 | + it('throws a diagnosis when no modelled EQL v3 columns exist anywhere', async () => { |
| 122 | + introspectMock.mockResolvedValue( |
| 123 | + introspectionOf({ |
| 124 | + tables: [ |
| 125 | + { |
| 126 | + tableName: 'logs', |
| 127 | + columns: [{ columnName: 'line', domainName: null }], |
| 128 | + }, |
| 129 | + ], |
| 130 | + }), |
| 131 | + ) |
| 132 | + await expect( |
| 133 | + encryptedSupabaseV3(fakeClient, { databaseUrl: 'postgres://x' }), |
| 134 | + ).rejects.toThrow(/no EQL v3 encrypted columns found/) |
| 135 | + expect(encryptionMock).not.toHaveBeenCalled() |
| 136 | + }) |
| 137 | + |
| 138 | + it('throws from from() on an unknown table', async () => { |
| 139 | + const supabase = await encryptedSupabaseV3(fakeClient, { |
| 140 | + databaseUrl: 'postgres://x', |
| 141 | + }) |
| 142 | + expect(() => supabase.from('nope')).toThrow(/unknown table/) |
| 143 | + }) |
| 144 | + |
| 145 | + it('returns a v3 builder from from() on a known table', async () => { |
| 146 | + const supabase = await encryptedSupabaseV3(fakeClient, { |
| 147 | + databaseUrl: 'postgres://x', |
| 148 | + }) |
| 149 | + expect(supabase.from('users')).toBeInstanceOf(EncryptedQueryBuilderV3Impl) |
| 150 | + }) |
| 151 | + |
| 152 | + it('throws when a schemas record key ≠ its table name', async () => { |
| 153 | + const mislabelled = encryptedTable('users', { |
| 154 | + email: types.TextSearch('email'), |
| 155 | + }) |
| 156 | + await expect( |
| 157 | + encryptedSupabaseV3(fakeClient, { |
| 158 | + databaseUrl: 'postgres://x', |
| 159 | + schemas: { orders: mislabelled }, |
| 160 | + }), |
| 161 | + ).rejects.toThrow(/orders.*users|record key/) |
| 162 | + }) |
| 163 | + |
| 164 | + it('throws on a recognized-but-unmodelled EQL domain', async () => { |
| 165 | + introspectMock.mockResolvedValue( |
| 166 | + introspectionOf({ |
| 167 | + tables: [ |
| 168 | + { |
| 169 | + tableName: 'metrics', |
| 170 | + columns: [{ columnName: 'score', domainName: 'integer_ord_ope' }], |
| 171 | + }, |
| 172 | + ], |
| 173 | + eqlDomains: new Set(['integer_ord_ope']), |
| 174 | + }), |
| 175 | + ) |
| 176 | + await expect( |
| 177 | + encryptedSupabaseV3(fakeClient, { databaseUrl: 'postgres://x' }), |
| 178 | + ).rejects.toThrow(/integer_ord_ope/) |
| 179 | + expect(encryptionMock).not.toHaveBeenCalled() |
| 180 | + }) |
| 181 | +}) |
0 commit comments