import assert from 'node:assert/strict' import { test } from 'node:test' import { decideShip, extraCareMatches } from './policy.ts' test('extra care catches auth titles', () => { const hits = extraCareMatches({ title: 'Fix OAuth login redirect', labels: [] }) assert.ok(hits.some((h) => h.id === 'auth')) }) test('decideShip blocks when not right fix', () => { const d = decideShip({ isRightFix: false, isTwoWayDoor: true, matchesWrittenDecision: true, extraCare: [], productDirectionNeeded: false, }) assert.equal(d.mayAutopilot, false) }) import { AUTOPILOT_THRESHOLD, buildTriageRequestBody, parseChoice, TRIAGE_CHOICES, TRIAGE_RULES } from './triage-request.ts' const sampleItem = { title: 'Fix typo in README', body: 'teh -> the', labels: ['docs'], source: 'github', projectId: 'kody', needsKent: false, } as const test('triage request asks small noul questions, one per ladder rule', () => { const body = buildTriageRequestBody(sampleItem as never) assert.equal(body.model, 'typesafe/jev') const keys = Object.keys(body.input.questions) assert.deepEqual(keys, TRIAGE_RULES.map((r) => r.key)) for (const key of keys) { const q = body.input.questions[key] as Record assert.equal(q.type, 'noul') assert.match(String(q.instructions), /untrusted data/) } assert.equal(body.input.state.title, 'Fix typo in README') }) function noulAnswers(yes: Record) { return Object.fromEntries(TRIAGE_RULES.map((r) => [r.key, { type: 'noul', noul: yes[r.key] ?? 0.05 }])) } test('triage ladder: owner decision wins over a clear fix', () => { const parsed = parseChoice({ answers: noulAnswers({ asks_owner_decision: 0.9, clear_fix: 0.95 }) }) assert.deepEqual(parsed, { value: 'needs_kent', confidence: 0.9, decidedBy: 'asks_owner_decision' }) }) test('triage ladder: clear fix below the autopilot threshold is held for review', () => { const held = parseChoice({ answers: noulAnswers({ clear_fix: AUTOPILOT_THRESHOLD - 0.05 }) }) assert.equal(held?.value, 'needs_review') assert.equal(held?.decidedBy, 'default') const ok = parseChoice({ answers: noulAnswers({ clear_fix: AUTOPILOT_THRESHOLD + 0.05 }) }) assert.equal(ok?.value, 'autopilot_ok') assert.equal(ok?.decidedBy, 'clear_fix') }) test('triage ladder: unclear problem is held for review before clear fix', () => { const parsed = parseChoice({ answers: noulAnswers({ unclear_problem: 0.7, clear_fix: 0.9 }) }) assert.equal(parsed?.value, 'needs_review') assert.equal(parsed?.decidedBy, 'unclear_problem') }) test('triage ladder: a missing answer returns null (model returned nothing)', () => { const answers = noulAnswers({}) delete (answers as Record).clear_fix assert.equal(parseChoice({ answers }), null) }) test('parseChoice reads answers.autopilot.choice from async v4 envelope', () => { const parsed = parseChoice({ result: { state: 'Completed', result: { model: 'jev-1.13.0', answers: { autopilot: { type: 'choice', choice: 'needs_kent', confidence: 0.88 } }, }, }, success: true, }) assert.deepEqual(parsed, { value: 'needs_kent', confidence: 0.88 }) }) test('parseChoice accepts direct answers shape and rejects unknown labels', () => { assert.deepEqual(parseChoice({ answers: { autopilot: { choice: 'needs_review', confidence: 0.5 } } }), { value: 'needs_review', confidence: 0.5, }) assert.equal(parseChoice({ answers: { autopilot: { choice: 'ship_it' } } }), null) assert.equal(parseChoice({ result: {} }), null) })