import { describe, expect, test } from 'vitest';
import { chunkScientificBlocks, estimateScientificTokens } from './chunking';
import { scientificBlocksFromPages } from './structure';

describe('paper memory scientific structure', () => {
  test('preserves page provenance and semantic captions/headings', async () => {
    const blocks = scientificBlocksFromPages([
      {
        pageNumber: 1,
        width: 600,
        lines: [
          { text: '1 Introduction', x: 40, y: 760, width: 120, height: 14, fontSize: 14 },
          { text: 'We study robust training under distribution shift.', x: 40, y: 730, width: 350, height: 10, fontSize: 10 },
          { text: 'Figure 1: Accuracy under shift.', x: 40, y: 100, width: 250, height: 10, fontSize: 10 },
        ],
      },
    ]);
    expect(blocks.some((block) => block.kind === 'heading')).toBe(true);
    expect(blocks.some((block) => block.kind === 'figure-caption')).toBe(true);
    const chunks = await chunkScientificBlocks('paper-1', blocks);
    expect(chunks.every((chunk) => chunk.pageStart === 1 && chunk.pageEnd === 1)).toBe(true);
    expect(chunks.every((chunk) => /^[a-f0-9]{64}$/u.test(chunk.contentHash))).toBe(true);
  });

  test('keeps oversized text below the target maximum through sentence splitting', async () => {
    const sentence = 'Robustness improves when the training distribution contains representative perturbations. ';
    const chunks = await chunkScientificBlocks('paper-2', [{
      id: 'long', kind: 'paragraph', text: sentence.repeat(220), pageStart: 3, pageEnd: 3, section: 'Results',
    }]);
    expect(chunks.length).toBeGreaterThan(1);
    expect(chunks.every((chunk) => estimateScientificTokens(chunk.text) <= 950)).toBe(true);
    expect(chunks.every((chunk) => chunk.pageStart === 3)).toBe(true);
  });
});

describe('paper memory scan detection', () => {
  test('requires OCR for multi-page image-like PDFs but not a short single-page note', async () => {
    const { isLikelyScannedPdf } = await import('./structure');
    expect(isLikelyScannedPdf([{ text: '' }, { text: '  ' }, { text: '' }])).toBe(true);
    expect(isLikelyScannedPdf([{ text: '' }])).toBe(false);
    expect(isLikelyScannedPdf([{ text: 'A'.repeat(200) }, { text: 'B'.repeat(200) }])).toBe(false);
  });
});
