|
| 1 | +/** |
| 2 | + * Binds `__fixtures__/scaffold/*.ts` to what `generatePlaceholderClient` |
| 3 | + * actually emits. |
| 4 | + * |
| 5 | + * The fixtures are the only thing in the repo that COMPILES the file |
| 6 | + * `stash init` writes into a user's project — `tsconfig.scaffold.json` and the |
| 7 | + * CI step that runs it exist for that. But a typechecked fixture is worthless |
| 8 | + * if the generator can drift away from it, and the existing codegen tests only |
| 9 | + * `toContain`-match fragments while `build-schema.test.ts` stubs the generator |
| 10 | + * out entirely. That combination is how `Encryption({ schemas: [] })` shipped: |
| 11 | + * every test was green and no compiler ever saw the output (#772 review, |
| 12 | + * finding 6). |
| 13 | + * |
| 14 | + * So: byte-for-byte, both directions. Change a template, regenerate the |
| 15 | + * fixture; the compiler then has an opinion about it. |
| 16 | + */ |
| 17 | +import { readFileSync } from 'node:fs' |
| 18 | +import path from 'node:path' |
| 19 | +import { fileURLToPath } from 'node:url' |
| 20 | +import { describe, expect, it } from 'vitest' |
| 21 | +import { PLACEHOLDER_TABLE_NAME } from '@/config/index.js' |
| 22 | +import { generatePlaceholderClient } from '../utils.js' |
| 23 | + |
| 24 | +const FIXTURE_DIR = path.resolve( |
| 25 | + fileURLToPath(import.meta.url), |
| 26 | + '../../../../../__fixtures__/scaffold', |
| 27 | +) |
| 28 | + |
| 29 | +const fixture = (name: string) => |
| 30 | + readFileSync(path.join(FIXTURE_DIR, name), 'utf-8') |
| 31 | + |
| 32 | +describe('the scaffolded client fixtures match the generator', () => { |
| 33 | + // Named `.generated.ts` so biome.json's existing exclusion leaves them alone: |
| 34 | + // they are template OUTPUT, and reformatting them would break the byte-for-byte |
| 35 | + // comparison below (which is the only thing tying the compiler to the generator). |
| 36 | + it.each([ |
| 37 | + ['generic.generated.ts', 'postgresql'], |
| 38 | + ['drizzle.generated.ts', 'drizzle'], |
| 39 | + ] as const)('%s', (file, integration) => { |
| 40 | + expect(fixture(file)).toBe(generatePlaceholderClient(integration)) |
| 41 | + }) |
| 42 | + |
| 43 | + // `supabase` shares the generic template; pin that so a future split does not |
| 44 | + // silently leave the supabase path ungated. |
| 45 | + it('supabase reuses the generic template', () => { |
| 46 | + expect(generatePlaceholderClient('supabase')).toBe( |
| 47 | + fixture('generic.generated.ts'), |
| 48 | + ) |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +describe('the scaffold compiles because it declares a table', () => { |
| 53 | + it.each([ |
| 54 | + 'generic.generated.ts', |
| 55 | + 'drizzle.generated.ts', |
| 56 | + ])('%s passes a non-empty schema set', (file) => { |
| 57 | + const body = fixture(file) |
| 58 | + // The empty form is a hard TS2769 against both overloads — `Encryption` |
| 59 | + // requires at least one table by design (S-6), so the scaffold cannot go |
| 60 | + // back to `schemas: []` without breaking every project it is written into. |
| 61 | + expect(body).not.toContain('Encryption({ schemas: [] })') |
| 62 | + expect(body).toContain('schemas: [placeholderTable]') |
| 63 | + }) |
| 64 | + |
| 65 | + it.each([ |
| 66 | + 'generic.generated.ts', |
| 67 | + 'drizzle.generated.ts', |
| 68 | + ])('%s uses the sentinel name the config loader refuses', (file) => { |
| 69 | + // `loadEncryptConfig` exits 1 when this is the only table left, so the |
| 70 | + // two must agree — otherwise the user gets a confusing "table not found" |
| 71 | + // from whichever command runs next instead of "you never replaced this". |
| 72 | + expect(fixture(file)).toContain(`'${PLACEHOLDER_TABLE_NAME}'`) |
| 73 | + }) |
| 74 | +}) |
0 commit comments