import { describe, it, expect, vi, beforeEach } from 'vitest';

const { runWorkflowMock, generateMorningReportMock } = vi.hoisted(() => ({
  runWorkflowMock: vi.fn(),
  generateMorningReportMock: vi.fn(),
}));

vi.mock('../run-workflow.ts', () => ({
  runWorkflow: runWorkflowMock,
}));

vi.mock('../morning-report.ts', () => ({
  generateMorningReport: generateMorningReportMock,
}));

import { runScheduledWorkflow } from '../schedule-workflow.ts';

describe('schedule-workflow', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('runs workflow in resume mode and then generates morning report', async () => {
    runWorkflowMock.mockResolvedValue(undefined);
    generateMorningReportMock.mockResolvedValue(undefined);

    await runScheduledWorkflow();

    expect(runWorkflowMock).toHaveBeenCalledWith({ mode: 'resume' });
    expect(generateMorningReportMock).toHaveBeenCalledTimes(1);
  });

  it('propagates workflow failures and does not run report generation', async () => {
    const boom = new Error('workflow exploded');
    runWorkflowMock.mockRejectedValue(boom);

    await expect(runScheduledWorkflow()).rejects.toThrow('workflow exploded');
    expect(generateMorningReportMock).not.toHaveBeenCalled();
  });
});
