|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +// Renders the real predicate against the real drizzle dialect and schema: the |
| 6 | +// bug this guards against is a SQL *rendering* bug, so the global drizzle-orm |
| 7 | +// and 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 { LIVE_PAUSED_REFERENCE_STATUSES, unreferencedLargeValuePredicate } = await import( |
| 18 | + '@/lib/execution/payloads/large-value-metadata' |
| 19 | +) |
| 20 | + |
| 21 | +describe('unreferencedLargeValuePredicate SQL', () => { |
| 22 | + const { sql: text, params } = new PgDialect().sqlToQuery(unreferencedLargeValuePredicate()) |
| 23 | + |
| 24 | + it('compares paused status against an IN list', () => { |
| 25 | + expect(text).toMatch(/\.status IN \(\$\d+, \$\d+, \$\d+\)/) |
| 26 | + expect(params).toEqual(expect.arrayContaining([...LIVE_PAUSED_REFERENCE_STATUSES])) |
| 27 | + }) |
| 28 | + |
| 29 | + it('never emits ANY() over drizzle-expanded statuses', () => { |
| 30 | + // Drizzle renders a JS array as `($1, $2, $3)` — correct for IN, but |
| 31 | + // `ANY((...)::text[])` makes Postgres reject the whole statement with |
| 32 | + // "cannot cast type record to text[]", which killed cleanup-logs for two months. |
| 33 | + expect(text).not.toMatch(/ANY\(\(\$\d+/) |
| 34 | + }) |
| 35 | + |
| 36 | + it('never emits a dangling comparison operator before IN', () => { |
| 37 | + // `status = IN (...)` is a syntax error Postgres only reports at execution time. |
| 38 | + expect(text).not.toMatch(/=\s*IN\s*\(/) |
| 39 | + }) |
| 40 | +}) |
0 commit comments