"use strict";
// src/test_handoff_artifact.ts
// v1.17.0 — HandoffArtifact regression.
Object.defineProperty(exports, "__esModule", { value: true });
const HandoffArtifact_1 = require("./orchestrator/story/HandoffArtifact");
function assert(cond, msg) {
    if (!cond)
        throw new Error(`assertion failed: ${msg}`);
}
function makeInput(overrides = {}) {
    return {
        fromAgent: 'builder',
        toAgent: 'sentinel',
        story: {
            id: '1.1',
            path: '/repo/docs/stories/epic-x/1.1.story.md',
            status: 'InProgress',
            title: '[BUG] Fix gateway',
            openACCount: 1,
            totalACCount: 4,
            branch: 'story-1.1-bug-fix-gateway',
        },
        decisions: ['Patched Gateway.shutdown', 'Added stale state cleanup'],
        filesModified: ['src/orchestrator/Gateway.ts', 'src/cli/StatusCommand.ts'],
        blockers: [],
        nextAction: 'Run npm run test:all and review',
        ...overrides,
    };
}
function scenarioBuildBasic() {
    const rec = (0, HandoffArtifact_1.buildHandoffArtifact)(makeInput());
    assert(rec.handoff.from_agent === 'builder', 'from');
    assert(rec.handoff.to_agent === 'sentinel', 'to');
    assert(rec.handoff.story_context.story_id === '1.1', 'story id');
    assert(rec.handoff.story_context.open_ac === 1, 'open ac');
    assert(rec.handoff.decisions.length === 2, 'decisions');
    assert(rec.handoff.files_modified.length === 2, 'files');
    assert(typeof rec.handoff.created_at === 'string' && rec.handoff.created_at.length > 0, 'created_at');
    console.log('✅ scenario 1: buildHandoffArtifact populates all fields');
}
function scenarioCaps() {
    // Inject more than max decisions/files/blockers to test slicing.
    const manyDecisions = Array.from({ length: 20 }, (_, i) => `decision ${i}`);
    const manyFiles = Array.from({ length: 30 }, (_, i) => `src/foo/file${i}.ts`);
    const manyBlockers = Array.from({ length: 10 }, (_, i) => `blocker ${i}`);
    const rec = (0, HandoffArtifact_1.buildHandoffArtifact)(makeInput({
        decisions: manyDecisions,
        filesModified: manyFiles,
        blockers: manyBlockers,
    }));
    assert(rec.handoff.decisions.length === HandoffArtifact_1._limits.MAX_DECISIONS, `decisions capped to ${HandoffArtifact_1._limits.MAX_DECISIONS}, got ${rec.handoff.decisions.length}`);
    assert(rec.handoff.files_modified.length === HandoffArtifact_1._limits.MAX_FILES, `files capped to ${HandoffArtifact_1._limits.MAX_FILES}`);
    assert(rec.handoff.blockers.length === HandoffArtifact_1._limits.MAX_BLOCKERS, `blockers capped to ${HandoffArtifact_1._limits.MAX_BLOCKERS}`);
    // Last N kept (slice(-N)).
    assert(rec.handoff.decisions[0] === 'decision 15', `last 5 kept, got ${rec.handoff.decisions[0]}`);
    console.log('✅ scenario 2: slice caps applied (last-N kept)');
}
function scenarioSerializeRoundTrip() {
    const rec = (0, HandoffArtifact_1.buildHandoffArtifact)(makeInput());
    const yamlStr = (0, HandoffArtifact_1.serializeHandoff)(rec);
    assert(yamlStr.length < HandoffArtifact_1._limits.MAX_CHARS, `yaml char count: ${yamlStr.length} < ${HandoffArtifact_1._limits.MAX_CHARS}`);
    const parsed = (0, HandoffArtifact_1.parseHandoff)(yamlStr);
    assert(parsed !== null, 'parses');
    assert(parsed.handoff.from_agent === rec.handoff.from_agent, 'from_agent round-trip');
    assert(parsed.handoff.story_context.story_id === rec.handoff.story_context.story_id, 'story_id round-trip');
    assert(parsed.handoff.decisions.length === rec.handoff.decisions.length, 'decisions round-trip');
    console.log('✅ scenario 3: serialize → parse round-trip preserves shape');
}
function scenarioMalformedParse() {
    assert((0, HandoffArtifact_1.parseHandoff)('') === null, 'empty');
    assert((0, HandoffArtifact_1.parseHandoff)('not yaml: {{{') === null, 'malformed');
    assert((0, HandoffArtifact_1.parseHandoff)('foo: bar') === null, 'missing handoff key');
    console.log('✅ scenario 4: parseHandoff rejects malformed input');
}
function scenarioCharCap() {
    // Force oversized input — extremely long decisions strings.
    const huge = Array.from({ length: 5 }, () => 'x'.repeat(1000));
    const rec = (0, HandoffArtifact_1.buildHandoffArtifact)(makeInput({ decisions: huge }));
    const yamlStr = (0, HandoffArtifact_1.serializeHandoff)(rec);
    assert(yamlStr.length <= HandoffArtifact_1._limits.MAX_CHARS * 1.1, `oversized inputs trigger trim: yaml=${yamlStr.length} cap=${HandoffArtifact_1._limits.MAX_CHARS}`);
    console.log(`✅ scenario 5: char cap (~${HandoffArtifact_1._limits.MAX_CHARS}) enforced via trim — got ${yamlStr.length}`);
}
try {
    scenarioBuildBasic();
    scenarioCaps();
    scenarioSerializeRoundTrip();
    scenarioMalformedParse();
    scenarioCharCap();
    console.log('TEST_HANDOFF_ARTIFACT_OK');
}
catch (err) {
    console.error('TEST_HANDOFF_ARTIFACT_FAIL', err);
    process.exit(1);
}
