import { BackgroundJobWorker } from '../jobs/background-job.worker';
import { XiaobaoAiWorker } from './xiaobao-ai.worker';

describe('XiaobaoAiWorker', () => {
  it('registers the AI interpretation handler on module init', async () => {
    const worker = {
      registerHandler: jest.fn(),
    };
    const service = {
      runInterpretation: jest.fn().mockResolvedValue(undefined),
    };
    const aiWorker = new XiaobaoAiWorker(worker as unknown as BackgroundJobWorker, service as any);

    aiWorker.onModuleInit();

    expect(worker.registerHandler).toHaveBeenCalledWith('xiaobao.ai.interpret', expect.any(Function));
    const handler = worker.registerHandler.mock.calls[0][1];
    await handler({ versionId: 'version-1', riskSignature: 'sig-1' }, { id: 'job-1' });
    expect(service.runInterpretation).toHaveBeenCalledWith({ versionId: 'version-1', riskSignature: 'sig-1' });
  });

  it('rejects malformed interpretation payloads', async () => {
    const worker = {
      registerHandler: jest.fn(),
    };
    const service = {
      runInterpretation: jest.fn(),
    };
    const aiWorker = new XiaobaoAiWorker(worker as unknown as BackgroundJobWorker, service as any);

    aiWorker.onModuleInit();
    const handler = worker.registerHandler.mock.calls[0][1];

    await expect(handler({ versionId: 'version-1' }, { id: 'job-1' }))
      .rejects.toThrow('xiaobao.ai.interpret requires payload.versionId and payload.riskSignature');
    expect(service.runInterpretation).not.toHaveBeenCalled();
  });
});
