import { describe, expect, it } from 'vitest';
import { detectPaperResearchIntent, extractionMatrixToCsv, extractionMatrixToLatex, requestedExtractionFields } from './research';
import type { PaperExtractionMatrix } from './types';

describe('Paper Memory structured research routing', () => {
  it('infers advanced operations from natural language without a permanent mode', () => {
    expect(detectPaperResearchIntent('Compare these papers.').kind).toBe('compare');
    expect(detectPaperResearchIntent('Which papers disagree?').kind).toBe('contradictions');
    expect(detectPaperResearchIntent('Show the evidence balance for whether treatment improves survival.').kind).toBe('evidence-balance');
    expect(detectPaperResearchIntent('Create a Study Snapshot for this paper.').kind).toBe('snapshot');
    expect(detectPaperResearchIntent('What does this paper conclude?').kind).toBe('ask');
  });

  it('extracts a bounded user-defined field list', () => {
    expect(requestedExtractionFields('Extract sample size, dataset, method, accuracy and limitations from these papers.')).toEqual([
      'sample size', 'dataset', 'method', 'accuracy', 'limitations',
    ]);
  });

  it('exports provenance-backed matrices without changing the source artifact', () => {
    const matrix: PaperExtractionMatrix = {
      kind: 'extraction-matrix',
      fields: ['sample size', 'method'],
      rows: [{
        paperId: 'p1',
        paperTitle: 'Study & One',
        cells: {
          'sample size': { value: '318', evidence: [] },
          method: { value: 'RCT', evidence: [] },
        },
      }],
    };
    expect(extractionMatrixToCsv(matrix)).toContain('"318"');
    expect(extractionMatrixToLatex(matrix)).toContain('Study \\& One');
    expect(matrix.rows[0]?.paperTitle).toBe('Study & One');
  });
});
