"use strict";
// src/test_chunker.ts
// v1.13.0 — RAG Chunker regression suite.
Object.defineProperty(exports, "__esModule", { value: true });
const Chunker_1 = require("./orchestrator/rag/Chunker");
function assert(cond, msg) {
    if (!cond)
        throw new Error(`assertion failed: ${msg}`);
}
function tsBoundarySample() {
    return [
        '// header comment',
        'import { foo } from "x";',
        '',
        'export function alpha(x: number): number {',
        '  return x * 2;',
        '}',
        '',
        'export class Beta {',
        '  do(x: number) { return x + 1; }',
        '}',
        '',
        'export const GAMMA = 42;',
        '',
        'export interface Delta { id: string }',
        '',
        'export type Epsilon = "a" | "b";',
    ].join('\n');
}
function scenarioBoundaryChunks() {
    const chunks = (0, Chunker_1.chunkFile)('/x/foo.ts', tsBoundarySample());
    assert(chunks.length >= 4, `expected ≥4 TS boundary chunks, got ${chunks.length}`);
    for (const c of chunks) {
        assert(c.startLine >= 1, 'startLine 1-indexed');
        assert(c.endLine >= c.startLine, 'endLine ≥ startLine');
        assert(c.content.length > 0, 'non-empty content');
        assert(c.language === 'ts', 'language tagged as ts');
    }
    console.log(`✓ scenario 1: TS boundary chunking produced ${chunks.length} segments`);
}
function scenarioSlidingWindowDefault() {
    const lines = [];
    for (let i = 0; i < 120; i++)
        lines.push(`line ${i}: ${'x'.repeat(20)}`);
    const chunks = (0, Chunker_1.chunkFile)('/x/plain.txt', lines.join('\n'));
    assert(chunks.length > 1, `expected multiple chunks for 120-line file, got ${chunks.length}`);
    let prevStart = 0;
    for (const c of chunks) {
        assert(c.startLine > prevStart || c.startLine === 1, `monotonic-ish window start (got ${c.startLine})`);
        prevStart = c.startLine;
    }
    console.log(`✓ scenario 2: sliding window produced ${chunks.length} chunks for 120-line file`);
}
function scenarioOverlap() {
    const lines = [];
    for (let i = 0; i < 100; i++)
        lines.push(`line ${i}`);
    const chunks = (0, Chunker_1.chunkFile)('/x/plain.txt', lines.join('\n'), { windowLines: 20, overlapLines: 5 });
    let overlapSeen = false;
    for (let i = 1; i < chunks.length; i++) {
        if (chunks[i].startLine <= chunks[i - 1].endLine) {
            overlapSeen = true;
            break;
        }
    }
    assert(overlapSeen, 'sliding window should produce overlapping chunks when overlap > 0');
    console.log(`✓ scenario 3: overlap parameter produces overlapping windows`);
}
function scenarioTokenCap() {
    const huge = 'A'.repeat(40 * 1000);
    const chunks = (0, Chunker_1.chunkFile)('/x/huge.txt', huge);
    // huge single-line files still produce at least one chunk (or none if it
    // can't fit even when halved). Either way, no chunk should bust the cap.
    for (const c of chunks) {
        assert((0, Chunker_1.approxTokens)(c.content) <= 512, `chunk over token cap: ${(0, Chunker_1.approxTokens)(c.content)} tokens`);
    }
    console.log(`✓ scenario 4: token cap enforced (produced ${chunks.length} chunks for 40k-char line)`);
}
function scenarioLanguageInference() {
    assert((0, Chunker_1.inferLanguage)('a.ts') === 'ts', 'ts');
    assert((0, Chunker_1.inferLanguage)('a.tsx') === 'ts', 'tsx → ts');
    assert((0, Chunker_1.inferLanguage)('a.js') === 'js', 'js');
    assert((0, Chunker_1.inferLanguage)('a.py') === 'py', 'py');
    assert((0, Chunker_1.inferLanguage)('a.go') === 'go', 'go');
    assert((0, Chunker_1.inferLanguage)('a.md') === 'md', 'md');
    assert((0, Chunker_1.inferLanguage)('a.bin') === undefined, 'unknown ext → undefined');
    console.log(`✓ scenario 5: language inference table is sane`);
}
function scenarioEmptyContent() {
    const out = (0, Chunker_1.chunkFile)('/x/empty.ts', '');
    assert(out.length === 0, 'empty file → zero chunks');
    const blank = (0, Chunker_1.chunkFile)('/x/blank.txt', '\n\n\n\n');
    assert(blank.length === 0 || blank.every((c) => c.content.trim().length >= 0), 'blank input handled gracefully');
    console.log(`✓ scenario 6: empty/blank input is handled`);
}
function main() {
    scenarioBoundaryChunks();
    scenarioSlidingWindowDefault();
    scenarioOverlap();
    scenarioTokenCap();
    scenarioLanguageInference();
    scenarioEmptyContent();
    console.log('TEST_CHUNKER_OK');
}
try {
    main();
}
catch (err) {
    console.error(`TEST_CHUNKER_FAIL: ${err instanceof Error ? err.message : String(err)}`);
    process.exit(1);
}
