import { afterEach, describe, expect, it, vi } from 'vitest'; import { InteractionController } from './interaction.controller.js'; describe('InteractionController', (): void => { afterEach(() => vi.restoreAllMocks()); it('honors a differently named configured instance without a code change', async () => { const prior = process.env['MOSAIC_AGENT_NAME']; process.env['MOSAIC_AGENT_NAME'] = 'Nova'; const runtime = { listSessions: vi.fn().mockResolvedValue([]) }; const controller = new InteractionController(runtime as never, {} as never); await expect( controller.sessions('Nova', 'fleet', { id: 'owner', tenantId: 'team' }, 'corr-1'), ).resolves.toEqual([]); await expect( controller.sessions('Other', 'fleet', { id: 'owner', tenantId: 'team' }, 'corr-1'), ).rejects.toThrow('Interaction agent is not configured'); if (prior === undefined) delete process.env['MOSAIC_AGENT_NAME']; else process.env['MOSAIC_AGENT_NAME'] = prior; }); it('rejects a request without the non-simple correlation header', async () => { process.env['MOSAIC_AGENT_NAME'] = 'Nova'; const controller = new InteractionController({ listSessions: vi.fn() } as never, {} as never); await expect(controller.sessions('Nova', 'fleet', { id: 'owner' })).rejects.toThrow( 'X-Correlation-Id is required', ); }); it('rejects an invalid attach mode before invoking a provider', async () => { process.env['MOSAIC_AGENT_NAME'] = 'Nova'; const controller = new InteractionController({ attach: vi.fn() } as never, {} as never); await expect( controller.attach('Nova', 'durable-1', { mode: 'write' as never }, { id: 'owner' }, 'corr-1'), ).rejects.toThrow('Interaction attach mode is invalid'); }); it('uses the durable session identity and runtime registry for an approved stop', async () => { process.env['MOSAIC_AGENT_NAME'] = 'Nova'; const runtime = { terminate: vi.fn().mockResolvedValue(undefined) }; const durable = { getSnapshot: vi.fn().mockResolvedValue({ identity: { agentName: 'Nova', providerId: 'fleet', runtimeSessionId: 'runtime-1' }, }), }; const controller = new InteractionController(runtime as never, durable as never); await controller.stop( 'Nova', 'durable-1', { approvalRef: 'approval-1' }, { id: 'owner' }, 'corr-1', ); expect(runtime.terminate).toHaveBeenCalledWith( 'fleet', 'runtime-1', 'approval-1', expect.objectContaining({ correlationId: 'corr-1', actorScope: { userId: 'owner', tenantId: 'owner' }, }), ); }); });