import { describe, expect, it, mock } from 'bun:test';
import { createResearchBridge } from './researchBridge';

describe('createResearchBridge model commands', () => {
  it('passes financial model command payloads to Tauri', async () => {
    const invoke = mock(
      async <T>(command: string, args?: Record<string, unknown>): Promise<T> => {
        switch (command) {
          case 'list_financial_models':
            expect(args).toEqual({ request: { workspaceId: 'workspace-1' } });
            return [] as T;
          case 'create_financial_model':
            expect(args).toEqual({
              request: {
                workspaceId: 'workspace-1',
                name: 'Base DCF',
                ticker: 'NVDA',
                templateType: 'dcf',
              },
            });
            return { id: 'model-1' } as T;
          case 'save_financial_model':
            expect(args).toEqual({
              request: {
                modelId: 'model-1',
                workbookJson: { version: 1, sheets: [] },
                annotations: [],
              },
            });
            return { id: 'model-1' } as T;
          case 'evaluate_financial_model':
            expect(args).toEqual({ request: { modelId: 'model-1' } });
            return {
              evaluatedCells: [],
              formulaErrors: [],
              dependencyGraph: [],
            } as T;
          case 'export_financial_model_xlsx':
            expect(args).toEqual({ request: { modelId: 'model-1' } });
            return {
              filePath: '/tmp/model.xlsx',
              modelId: 'model-1',
              sheetCount: 1,
              exportedAt: '2026-04-30T10:00:00Z',
            } as T;
          default:
            throw new Error(`unexpected command ${command}`);
        }
      },
    );
    const bridge = createResearchBridge(invoke);

    await bridge.listFinancialModels({ workspaceId: 'workspace-1' });
    await bridge.createFinancialModel({
      workspaceId: 'workspace-1',
      name: 'Base DCF',
      ticker: 'NVDA',
      templateType: 'dcf',
    });
    await bridge.saveFinancialModel({
      modelId: 'model-1',
      workbookJson: { version: 1, sheets: [] },
      annotations: [],
    });
    await bridge.evaluateFinancialModel({ modelId: 'model-1' });
    await bridge.exportFinancialModelXlsx({ modelId: 'model-1' });

    expect(invoke).toHaveBeenCalledTimes(5);
  });
});
