import { asFunction, asValue, createContainer as createDiContainer } from 'awilix'; import { expect, test } from 'bun:test'; import { ServiceContainerBuilder, type ServiceCradle } from '@stock-bot/di'; import type { StockAppConfig } from '@stock-bot/stock-config'; import { registerOrchestratorServices } from '../../apps/stock/orchestrator/src/container'; import type { PaperConfigurationInput } from '../../apps/stock/orchestrator/src/contracts/paper-runtime'; import { createContainer } from '../../apps/stock/orchestrator/src/simple-container'; import type { OrchestratorServices } from '../../apps/stock/orchestrator/src/types'; import { logger } from '../fixtures/execution'; import { paperFeedConfiguration } from '../fixtures/paper-feed'; test('canonical paper rejects invalid role/configuration before resolving services', async () => { let resolved = 0; const container = createDiContainer(); container.register({ serviceContainer: asFunction(() => { resolved++; throw new Error('Unexpected service resolution'); }), }); const config = paperFeedConfiguration(); await expect( registerOrchestratorServices(container, { role: 'research-only', canonicalPaper: config, }) ).rejects.toThrow('requires a paper composition'); config.matching.series[0]!.mode = 'Open'; await expect( registerOrchestratorServices(container, { role: 'paper', canonicalPaper: config, }) ).rejects.toThrow('Quote execution only'); expect(resolved).toBe(0); }); test('live defaults, research canonical configuration and missing custody fail before connections', async () => { const config = (orchestrator: unknown) => ({ services: { orchestrator } }) as StockAppConfig; await expect(createContainer(config({ defaultMode: 'live' }))).rejects.toThrow( 'Live trading is disabled' ); await expect( createContainer(config({ defaultMode: 'backtest', canonicalPaper: paperFeedConfiguration() })) ).rejects.toThrow('requires a paper composition'); await expect( createContainer(config({ defaultMode: 'paper', canonicalPaper: paperFeedConfiguration() })) ).rejects.toThrow('requires durable PostgreSQL'); }); test('reviewed deployment host is explicit, exclusive, durable and never enabled for research', async () => { const container = createDiContainer(); let resolved = 0; container.register({ serviceContainer: asFunction(() => { resolved++; throw new Error('Unexpected service resolution'); }), }); await expect( registerOrchestratorServices(container, { role: 'research-only', paperDeploymentHost: { enabled: true }, }) ).rejects.toThrow('requires a paper composition'); await expect( registerOrchestratorServices(container, { role: 'paper', canonicalPaper: paperFeedConfiguration(), paperDeploymentHost: { enabled: true }, }) ).rejects.toThrow('never two account owners'); await expect( registerOrchestratorServices(container, { role: 'paper', paperDeploymentHost: { enabled: true, deploymentId: 'not-a-uuid' }, }) ).rejects.toThrow(); await expect( registerOrchestratorServices(container, { role: 'paper', canonicalPaper: { ...paperFeedConfiguration(), version: 2 } as PaperConfigurationInput, }) ).rejects.toThrow('reviewed deployment host'); expect(resolved).toBe(0); const config = { services: { orchestrator: { defaultMode: 'paper', paperDeploymentHost: { enabled: true } } }, } as StockAppConfig; await expect(createContainer(config)).rejects.toThrow('requires durable PostgreSQL'); }); test('explicit deployment host initializes unbound without account reads, writes or activation', async () => { const container = await new ServiceContainerBuilder() .withOptions({ enableCache: false, enableQueue: false, enableMongoDB: false, enablePostgres: false, enableQuestDB: false, enableBrowser: false, enableProxy: false, }) .build(); let transactions = 0; const services: OrchestratorServices = { logger, postgres: { transaction() { transactions++; throw new Error('Unbound host must not access account state'); }, } as unknown as NonNullable, }; container.register({ serviceContainer: asValue(services) }); try { await registerOrchestratorServices(container, { role: 'paper', paperDeploymentHost: { enabled: true }, }); expect(services.custom?.PaperDeploymentHost).toBeDefined(); expect(services.custom?.CanonicalPaperRuntime).toBeUndefined(); expect(() => services.custom!.PaperDeploymentHost!.runtime()).toThrow('not bound'); expect(transactions).toBe(0); } finally { await services.custom?.PaperDeploymentHost?.shutdown(); await services.custom?.BacktestRunner?.shutdown(); await container.dispose(); } });