|
| 1 | +/** @vitest-environment node */ |
| 2 | +import { execFileSync } from 'node:child_process' |
| 3 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { dirname, join } from 'node:path' |
| 6 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 7 | +import { readBlockRegistryAtRef } from '@/scripts/block-registry-snapshot' |
| 8 | + |
| 9 | +let root: string |
| 10 | + |
| 11 | +function write(path: string, content: string) { |
| 12 | + const target = join(root, path) |
| 13 | + mkdirSync(dirname(target), { recursive: true }) |
| 14 | + writeFileSync(target, content) |
| 15 | +} |
| 16 | + |
| 17 | +function git(...args: string[]) { |
| 18 | + return execFileSync('git', args, { cwd: root, encoding: 'utf8', stdio: 'pipe' }).trim() |
| 19 | +} |
| 20 | + |
| 21 | +function commit() { |
| 22 | + git('add', '.') |
| 23 | + git( |
| 24 | + '-c', |
| 25 | + 'user.name=Test', |
| 26 | + '-c', |
| 27 | + 'commit.gpgsign=false', |
| 28 | + '-c', |
| 29 | + 'core.hooksPath=/dev/null', |
| 30 | + '-c', |
| 31 | + 'user.email=test@example.test', |
| 32 | + 'commit', |
| 33 | + '-m', |
| 34 | + 'Baseline fixture' |
| 35 | + ) |
| 36 | + return git('rev-parse', 'HEAD') |
| 37 | +} |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + root = mkdtempSync(join(tmpdir(), 'registry-snapshot-test-')) |
| 41 | + git('init', '--quiet') |
| 42 | + write('.gitignore', 'node_modules\n') |
| 43 | + write('apps/sim/package.json', JSON.stringify({ name: '@sim/app', type: 'module' })) |
| 44 | + write( |
| 45 | + 'apps/sim/tsconfig.json', |
| 46 | + JSON.stringify({ compilerOptions: { paths: { '@/*': ['./*'] } } }) |
| 47 | + ) |
| 48 | +}) |
| 49 | + |
| 50 | +afterEach(() => rmSync(root, { recursive: true, force: true })) |
| 51 | + |
| 52 | +describe('readBlockRegistryAtRef', () => { |
| 53 | + it('reads effective IDs from spreads, local arrays, helpers, and derived blocks at the base revision', () => { |
| 54 | + write( |
| 55 | + 'apps/sim/blocks/registry.ts', |
| 56 | + ` |
| 57 | +import { sharedFields } from '@sim/fields' |
| 58 | +import { triggerFields } from '@/triggers/fields' |
| 59 | +const localFields = [{ id: 'operation', options: [{ id: 'nested-option' }] }, { id: 'encoding' }] |
| 60 | +const LegacyBlock = { type: 'legacy', subBlocks: localFields } satisfies { type: string; subBlocks: { id: string }[] } |
| 61 | +const CurrentBlock = { ...LegacyBlock, type: 'current', subBlocks: LegacyBlock.subBlocks.filter(field => field.id !== 'encoding') } |
| 62 | +const makeFields = () => [...sharedFields, ...triggerFields] |
| 63 | +const SpreadBlock = { type: 'spread', subBlocks: [...localFields, ...makeFields()] } |
| 64 | +export const getBlockRegistry = () => ({ legacy: LegacyBlock, current: CurrentBlock, spread: SpreadBlock }) |
| 65 | +` |
| 66 | + ) |
| 67 | + write('apps/sim/triggers/fields.ts', "export const triggerFields = [{ id: 'trigger' }]\n") |
| 68 | + write( |
| 69 | + 'packages/fields/package.json', |
| 70 | + JSON.stringify({ name: '@sim/fields', type: 'module', exports: './index.ts' }) |
| 71 | + ) |
| 72 | + write('packages/fields/index.ts', "export const sharedFields = [{ id: 'shared' }]\n") |
| 73 | + const base = commit() |
| 74 | + mkdirSync(join(root, 'node_modules/@sim'), { recursive: true }) |
| 75 | + symlinkSync(join(root, 'packages/fields'), join(root, 'node_modules/@sim/fields'), 'dir') |
| 76 | + write( |
| 77 | + 'packages/fields/index.ts', |
| 78 | + "export const sharedFields = [{ id: 'changed-after-base' }]\n" |
| 79 | + ) |
| 80 | + write('apps/sim/triggers/fields.ts', 'export const triggerFields = []\n') |
| 81 | + const statusBefore = git('status', '--porcelain') |
| 82 | + |
| 83 | + expect(readBlockRegistryAtRef(root, base)).toEqual({ |
| 84 | + legacy: ['operation', 'encoding'], |
| 85 | + current: ['operation'], |
| 86 | + spread: ['operation', 'encoding', 'shared', 'trigger'], |
| 87 | + }) |
| 88 | + expect(git('status', '--porcelain')).toBe(statusBefore) |
| 89 | + expect(readFileSync(join(root, 'packages/fields/index.ts'), 'utf8')).toContain( |
| 90 | + 'changed-after-base' |
| 91 | + ) |
| 92 | + }) |
| 93 | + |
| 94 | + it('keeps installed third-party dependencies available without treating their output as registry JSON', () => { |
| 95 | + write( |
| 96 | + 'apps/sim/blocks/registry.ts', |
| 97 | + ` |
| 98 | +import { field } from 'fixture-provider' |
| 99 | +console.log('Registry initialization diagnostic') |
| 100 | +export const getBlockRegistry = () => ({ block: { type: 'block', subBlocks: [field] } }) |
| 101 | +` |
| 102 | + ) |
| 103 | + const base = commit() |
| 104 | + write( |
| 105 | + 'node_modules/fixture-provider/package.json', |
| 106 | + JSON.stringify({ name: 'fixture-provider', type: 'module', exports: './index.js' }) |
| 107 | + ) |
| 108 | + write( |
| 109 | + 'node_modules/fixture-provider/index.js', |
| 110 | + "export const field = { id: 'installed-field' }\n" |
| 111 | + ) |
| 112 | + |
| 113 | + expect(readBlockRegistryAtRef(root, base)).toEqual({ block: ['installed-field'] }) |
| 114 | + }) |
| 115 | + |
| 116 | + it('fails instead of returning partial IDs when a derived definition cannot load', () => { |
| 117 | + write( |
| 118 | + 'apps/sim/blocks/registry.ts', |
| 119 | + ` |
| 120 | +import { missingFields } from './missing' |
| 121 | +export const getBlockRegistry = () => ({ block: { type: 'block', subBlocks: missingFields } }) |
| 122 | +` |
| 123 | + ) |
| 124 | + const base = commit() |
| 125 | + expect(() => readBlockRegistryAtRef(root, base)).toThrow() |
| 126 | + }) |
| 127 | + |
| 128 | + it('fails when the requested base revision is unavailable', () => { |
| 129 | + write('apps/sim/blocks/registry.ts', 'export const getBlockRegistry = () => ({})\n') |
| 130 | + commit() |
| 131 | + expect(() => readBlockRegistryAtRef(root, 'missing-base')).toThrow() |
| 132 | + }) |
| 133 | +}) |
0 commit comments