import { z } from 'zod' import { buildComponent } from '../../../components/utils/buildComponent' import ForgeRegistryDuplicateError from '../../errors/ForgeRegistryDuplicateError' import ForgeRegistryValidationError from '../../errors/ForgeRegistryValidationError' import type { BlockDefinition, EvaluatedBlock } from '../../../components/types/structures.type' import ComponentRegistry from './ComponentRegistry' describe('ComponentRegistry', () => { let registry: ComponentRegistry beforeEach(() => { registry = new ComponentRegistry() }) describe('registerMany', () => { it('should compile component input schemas while preserving their parsed output', () => { // Arrange const inputSchema = z.object({ value: z.string().transform(value => value.length) }) const component = buildComponent('text', () => '', { inputSchema }) // Act registry.registerMany([component]) const registeredSchema = registry.get('text')?.inputSchema // Assert expect(registeredSchema).not.toBe(inputSchema) expect(registeredSchema?.safeParse({ value: 'Ada' })).toEqual({ success: true, data: { value: 3 } }) }) it('should register a single component successfully', () => { const mockComponent = buildComponent('text-input', (_block: EvaluatedBlock) => { return `` }) expect(() => registry.registerMany([mockComponent])).not.toThrow() expect(registry.has('text-input')).toBe(true) }) it('should register multiple components successfully', () => { const comp1 = buildComponent('text', () => '') const comp2 = buildComponent('radio', () => '') const comp3 = buildComponent('checkbox', () => '') registry.registerMany([comp1, comp2, comp3]) expect(registry.has('text')).toBe(true) expect(registry.has('radio')).toBe(true) expect(registry.has('checkbox')).toBe(true) expect(registry.size()).toBe(3) }) it('should handle empty array without throwing', () => { expect(() => registry.registerMany([])).not.toThrow() expect(registry.size()).toBe(0) }) it('should handle null/undefined gracefully', () => { expect(() => registry.registerMany(null as any)).not.toThrow() expect(() => registry.registerMany(undefined as any)).not.toThrow() expect(registry.size()).toBe(0) }) describe('duplicate registration', () => { it('should throw ForgeRegistryDuplicateError for duplicate component', () => { const comp1 = buildComponent('text', () => '') const comp2 = buildComponent('text', () => '