|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +// Renders the real prune statements against the real drizzle dialect and |
| 6 | +// schema. These are raw `sql` templates, so a rendering bug only surfaces at |
| 7 | +// execution time — the global drizzle/schema mocks would hide it entirely. |
| 8 | +import { describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +vi.unmock('drizzle-orm') |
| 11 | +vi.unmock('@sim/db') |
| 12 | +vi.unmock('@sim/db/schema') |
| 13 | + |
| 14 | +process.env.DATABASE_URL ??= 'postgresql://user:pass@localhost:5432/test' |
| 15 | + |
| 16 | +const { PgDialect } = await import('drizzle-orm/pg-core') |
| 17 | +const { pruneLargeValueMetadata } = await import('@/lib/execution/payloads/large-value-metadata') |
| 18 | + |
| 19 | +/** Captures the raw statements `pruneLargeValueMetadata` issues, without a database. */ |
| 20 | +async function renderPruneStatements(workspaceIds: string[]): Promise<string[]> { |
| 21 | + const dialect = new PgDialect() |
| 22 | + const rendered: string[] = [] |
| 23 | + const capturingClient = { |
| 24 | + execute: async (query: Parameters<PgDialect['sqlToQuery']>[0]) => { |
| 25 | + rendered.push(dialect.sqlToQuery(query).sql) |
| 26 | + return [{ count: 0 }] |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + await pruneLargeValueMetadata({ |
| 31 | + workspaceIds, |
| 32 | + tombstonesDeletedBefore: new Date('2026-01-01T00:00:00Z'), |
| 33 | + dbClient: capturingClient as unknown as Parameters< |
| 34 | + typeof pruneLargeValueMetadata |
| 35 | + >[0]['dbClient'], |
| 36 | + }) |
| 37 | + |
| 38 | + return rendered |
| 39 | +} |
| 40 | + |
| 41 | +describe('pruneLargeValueMetadata SQL', () => { |
| 42 | + it('binds workspace ids as one array parameter, not a value list', async () => { |
| 43 | + const statements = await renderPruneStatements(['ws-1', 'ws-2']) |
| 44 | + |
| 45 | + expect(statements.length).toBeGreaterThan(0) |
| 46 | + for (const statement of statements) { |
| 47 | + // `ANY(($1, $2)::text[])` is what interpolating the raw JS array yields — |
| 48 | + // Postgres rejects it ("cannot cast type record to text[]"), and with a |
| 49 | + // single id `ANY(($1)::text[])` fails as 22P02 instead. |
| 50 | + expect(statement).not.toMatch(/ANY\(\(\$\d+/) |
| 51 | + expect(statement).toMatch(/ANY\(\$\d+::text\[\]\)/) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + it('renders identically for a single workspace id', async () => { |
| 56 | + const statements = await renderPruneStatements(['ws-only']) |
| 57 | + |
| 58 | + expect(statements.length).toBeGreaterThan(0) |
| 59 | + for (const statement of statements) { |
| 60 | + expect(statement).toMatch(/ANY\(\$\d+::text\[\]\)/) |
| 61 | + } |
| 62 | + }) |
| 63 | +}) |
0 commit comments