import { buildComponent } from '../components/utils/buildComponent' import { createForgePackage } from '../authoring/builders' import { StructureType } from '../authoring/types/enums' import type { JourneyDefinition } from '../authoring/types/structures.type' import type { CompiledPackage } from './chassis/contracts/plans/compilationArtefacts.type' import CompilationPipeline from './chassis/compilation/pipeline/CompilationPipeline' import ComponentRegistry from './chassis/registries/ComponentRegistry' import FunctionRegistry from './chassis/registries/FunctionRegistry' import ScopedComponentRegistry from './chassis/registries/ScopedComponentRegistry' import ScopedFunctionRegistry from './chassis/registries/ScopedFunctionRegistry' import ForgeTraceSinkDispatcher from './chassis/tracing/ForgeTraceSinkDispatcher' import PackageInstance from './PackageInstance' describe('PackageInstance', () => { describe('constructor()', () => { beforeEach(() => { vi.restoreAllMocks() }) it('should use global dependencies when package has no scoped registrations', () => { // Arrange const functionRegistry = new FunctionRegistry() const componentRegistry = new ComponentRegistry() mockCompilation() // Act const instance = new PackageInstance(createForgePackage({ journey: createJourneyDefinition() }), { functionRegistry, componentRegistry, instrumentation: new ForgeTraceSinkDispatcher(), }) // Assert expect(instance.getDependencies().functionRegistry).toBe(functionRegistry) expect(instance.getDependencies().componentRegistry).toBe(componentRegistry) }) it('should scope package functions with provided dependencies', () => { // Arrange const functionRegistry = new FunctionRegistry() const componentRegistry = new ComponentRegistry() functionRegistry.register({ GlobalFunction: { name: 'GlobalFunction', evaluate: () => true, }, }) mockCompilation() // Act const instance = new PackageInstance( createForgePackage({ journey: createJourneyDefinition(), functions: { WithPrefix: (deps: { prefix: string }) => (value: unknown) => `${deps.prefix}${String(value)}`, }, }), { functionRegistry, componentRegistry, functionDependencies: { prefix: 'case-' }, instrumentation: new ForgeTraceSinkDispatcher(), }, ) // Assert const scopedFunctionRegistry = instance.getDependencies().functionRegistry expect(scopedFunctionRegistry).toBeInstanceOf(ScopedFunctionRegistry) expect(scopedFunctionRegistry.has('GlobalFunction')).toBe(true) expect(scopedFunctionRegistry.get('WithPrefix')?.evaluate('123')).toBe('case-123') expect(functionRegistry.has('WithPrefix')).toBe(false) }) it('should scope package components without registering them globally', () => { // Arrange const functionRegistry = new FunctionRegistry() const componentRegistry = new ComponentRegistry() const globalComponent = buildComponent('global-component', () => '