const PATH_METADATA = 'path';
import { describe, expect, it, vi } from 'vitest';
import { InteractionCoordinationController } from './interaction-coordination.controller.js';

const user = { id: 'operator-1', tenantId: 'tenant-a' };

describe('InteractionCoordinationController', () => {
  it('exposes the neutral canonical route and Mos compatibility alias over identical handlers', () => {
    expect(Reflect.getMetadata(PATH_METADATA, InteractionCoordinationController)).toEqual([
      'api/coord/interaction',
      'api/coord/mos',
    ]);
    expect(InteractionCoordinationController.prototype.handoff).toBeTypeOf('function');
    expect(InteractionCoordinationController.prototype.observe).toBeTypeOf('function');
    expect(InteractionCoordinationController.prototype.result).toBeTypeOf('function');
  });

  it('derives actor and tenant from the authenticated user rather than handoff input', async () => {
    const coordination = {
      handoff: vi.fn(async () => ({ handoffId: 'handoff-1' })),
      observe: vi.fn(),
      result: vi.fn(),
    };
    const controller = new InteractionCoordinationController(coordination as never);

    await controller.handoff({ idempotencyKey: 'request-1', summary: 'Implement' }, user, 'corr-1');

    expect(coordination.handoff).toHaveBeenCalledWith(
      { idempotencyKey: 'request-1', summary: 'Implement' },
      expect.objectContaining({
        actorScope: { userId: 'operator-1', tenantId: 'tenant-a' },
        channelId: 'cli',
        correlationId: 'corr-1',
      }),
    );
  });

  it('requires a correlation header before invoking the coordination service', async () => {
    const coordination = { handoff: vi.fn(), observe: vi.fn(), result: vi.fn() };
    const controller = new InteractionCoordinationController(coordination as never);

    await expect(
      controller.handoff({ idempotencyKey: 'request-1', summary: 'Implement' }, user, undefined),
    ).rejects.toThrow('X-Correlation-Id is required');
    expect(coordination.handoff).not.toHaveBeenCalled();
  });
});
