|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 2 | +import { mkdirSync, writeFileSync, rmSync } from 'node:fs' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import cookbookBundler from './cookbook-bundler' |
| 6 | + |
| 7 | +function createTempCookbook() { |
| 8 | + const dir = join(tmpdir(), `cookbook-test-${Date.now()}`) |
| 9 | + mkdirSync(join(dir, 'patterns', 'energy-settlement'), { recursive: true }) |
| 10 | + mkdirSync(join(dir, 'ui', 'activity-feed'), { recursive: true }) |
| 11 | + return dir |
| 12 | +} |
| 13 | + |
| 14 | +describe('cookbook-bundler', () => { |
| 15 | + let tempDir: string |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + tempDir = createTempCookbook() |
| 19 | + }) |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + rmSync(tempDir, { recursive: true, force: true }) |
| 23 | + }) |
| 24 | + |
| 25 | + it('resolves the virtual module ID', () => { |
| 26 | + const plugin = cookbookBundler({ cookbookDir: tempDir }) |
| 27 | + const resolveId = plugin.resolveId as (id: string) => string | undefined |
| 28 | + expect(resolveId('virtual:cookbook-data')).toBe('\0virtual:cookbook-data') |
| 29 | + expect(resolveId('some-other-module')).toBeUndefined() |
| 30 | + }) |
| 31 | + |
| 32 | + it('loads empty items when registry.json is missing', () => { |
| 33 | + const plugin = cookbookBundler({ cookbookDir: tempDir }) |
| 34 | + const load = plugin.load as (id: string) => string | undefined |
| 35 | + const result = load('\0virtual:cookbook-data') |
| 36 | + expect(result).toContain('export default') |
| 37 | + const data = JSON.parse(result!.replace('export default ', '').replace(';', '')) |
| 38 | + expect(data.items).toEqual([]) |
| 39 | + }) |
| 40 | + |
| 41 | + it('loads registry items and merges pattern.json metadata', () => { |
| 42 | + writeFileSync( |
| 43 | + join(tempDir, 'registry.json'), |
| 44 | + JSON.stringify({ |
| 45 | + name: 'test-cookbook', |
| 46 | + items: [ |
| 47 | + { name: 'energy-settlement', type: 'registry:pattern', title: 'Energy' }, |
| 48 | + { name: 'activity-feed', type: 'registry:ui', title: 'Activity Feed' }, |
| 49 | + ], |
| 50 | + }), |
| 51 | + ) |
| 52 | + |
| 53 | + writeFileSync( |
| 54 | + join(tempDir, 'patterns', 'energy-settlement', 'pattern.json'), |
| 55 | + JSON.stringify({ |
| 56 | + name: 'energy-settlement', |
| 57 | + type: 'registry:pattern', |
| 58 | + title: 'Energy', |
| 59 | + description: 'Converts kWh into value', |
| 60 | + categories: ['energy'], |
| 61 | + meta: { complexity: 3, design_pattern: 'cross-instrument-valuation' }, |
| 62 | + files: [{ path: 'patterns/energy-settlement/manifest-fragment.yaml', type: 'registry:file' }], |
| 63 | + }), |
| 64 | + ) |
| 65 | + |
| 66 | + writeFileSync( |
| 67 | + join(tempDir, 'ui', 'activity-feed', 'component.json'), |
| 68 | + JSON.stringify({ |
| 69 | + name: 'activity-feed', |
| 70 | + type: 'registry:ui', |
| 71 | + title: 'Activity Feed', |
| 72 | + description: 'Displays events', |
| 73 | + meta: { feature_module: 'dashboard' }, |
| 74 | + }), |
| 75 | + ) |
| 76 | + |
| 77 | + const plugin = cookbookBundler({ cookbookDir: tempDir }) |
| 78 | + const load = plugin.load as (id: string) => string | undefined |
| 79 | + const result = load('\0virtual:cookbook-data') |
| 80 | + const data = JSON.parse(result!.replace('export default ', '').replace(';', '')) |
| 81 | + |
| 82 | + expect(data.name).toBe('test-cookbook') |
| 83 | + expect(data.items).toHaveLength(2) |
| 84 | + |
| 85 | + const pattern = data.items[0] |
| 86 | + expect(pattern.description).toBe('Converts kWh into value') |
| 87 | + expect(pattern.meta.complexity).toBe(3) |
| 88 | + expect(pattern.files).toHaveLength(1) |
| 89 | + |
| 90 | + const ui = data.items[1] |
| 91 | + expect(ui.description).toBe('Displays events') |
| 92 | + expect(ui.meta.feature_module).toBe('dashboard') |
| 93 | + }) |
| 94 | + |
| 95 | + it('handles missing pattern.json gracefully', () => { |
| 96 | + writeFileSync( |
| 97 | + join(tempDir, 'registry.json'), |
| 98 | + JSON.stringify({ |
| 99 | + name: 'test-cookbook', |
| 100 | + items: [ |
| 101 | + { name: 'no-detail', type: 'registry:pattern', title: 'No Detail' }, |
| 102 | + ], |
| 103 | + }), |
| 104 | + ) |
| 105 | + |
| 106 | + const plugin = cookbookBundler({ cookbookDir: tempDir }) |
| 107 | + const load = plugin.load as (id: string) => string | undefined |
| 108 | + const result = load('\0virtual:cookbook-data') |
| 109 | + const data = JSON.parse(result!.replace('export default ', '').replace(';', '')) |
| 110 | + |
| 111 | + expect(data.items).toHaveLength(1) |
| 112 | + expect(data.items[0].name).toBe('no-detail') |
| 113 | + expect(data.items[0].meta).toBeUndefined() |
| 114 | + }) |
| 115 | +}) |
0 commit comments