|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { setTimeout as sleep } from 'node:timers/promises'; |
| 4 | + |
| 5 | +import type { PersonaSelection, PersonaSpec } from '@agentworkforce/workload-router'; |
| 6 | +import { |
| 7 | + buildBurnEnrichment, |
| 8 | + burnIngestHarness, |
| 9 | + canonicalJson, |
| 10 | + personaVersionHash, |
| 11 | + shouldEnableBurnAttribution, |
| 12 | + startBurnAttribution, |
| 13 | + type BurnIngestOptions, |
| 14 | + type BurnPendingStampOptions |
| 15 | +} from './burn-attribution.js'; |
| 16 | + |
| 17 | +function fakeSelection(): Pick<PersonaSelection, 'personaId' | 'tier' | 'runtime'> { |
| 18 | + return { |
| 19 | + personaId: 'code-reviewer', |
| 20 | + tier: 'best', |
| 21 | + runtime: { |
| 22 | + harness: 'codex', |
| 23 | + model: 'openai-codex/gpt-5.3-codex', |
| 24 | + systemPrompt: 'Review the diff.', |
| 25 | + harnessSettings: { |
| 26 | + reasoning: 'high', |
| 27 | + timeoutSeconds: 1200 |
| 28 | + } |
| 29 | + } |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function fakeSpec(overrides: Partial<PersonaSpec> = {}): PersonaSpec { |
| 34 | + return { |
| 35 | + id: 'code-reviewer', |
| 36 | + intent: 'review', |
| 37 | + tags: ['review'], |
| 38 | + description: 'Reviews code.', |
| 39 | + skills: [], |
| 40 | + tiers: { |
| 41 | + best: fakeSelection().runtime, |
| 42 | + 'best-value': { |
| 43 | + harness: 'opencode', |
| 44 | + model: 'opencode/gpt-5-nano', |
| 45 | + systemPrompt: 'Review concisely.', |
| 46 | + harnessSettings: { |
| 47 | + reasoning: 'medium', |
| 48 | + timeoutSeconds: 900 |
| 49 | + } |
| 50 | + }, |
| 51 | + minimum: { |
| 52 | + harness: 'opencode', |
| 53 | + model: 'opencode/minimax-m2.5-free', |
| 54 | + systemPrompt: 'Review blockers.', |
| 55 | + harnessSettings: { |
| 56 | + reasoning: 'low', |
| 57 | + timeoutSeconds: 600 |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + ...overrides |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +test('personaVersionHash canonicalizes object keys and changes with effective content', () => { |
| 66 | + assert.equal(canonicalJson({ b: 1, a: { d: 4, c: 3 } }), '{"a":{"c":3,"d":4},"b":1}'); |
| 67 | + |
| 68 | + const left = { |
| 69 | + id: 'p', |
| 70 | + tiers: { |
| 71 | + best: { model: 'm', harness: 'codex' } |
| 72 | + }, |
| 73 | + tags: ['review'] |
| 74 | + }; |
| 75 | + const right = { |
| 76 | + tags: ['review'], |
| 77 | + tiers: { |
| 78 | + best: { harness: 'codex', model: 'm' } |
| 79 | + }, |
| 80 | + id: 'p' |
| 81 | + }; |
| 82 | + assert.equal(personaVersionHash(left), personaVersionHash(right)); |
| 83 | + assert.notEqual(personaVersionHash(left), personaVersionHash({ ...right, tags: ['testing'] })); |
| 84 | +}); |
| 85 | + |
| 86 | +test('buildBurnEnrichment emits the required AgentWorkforce tags', () => { |
| 87 | + const spec = fakeSpec(); |
| 88 | + const tags = buildBurnEnrichment({ |
| 89 | + selection: fakeSelection(), |
| 90 | + personaSpec: spec, |
| 91 | + personaSource: 'dir:1' |
| 92 | + }); |
| 93 | + |
| 94 | + assert.deepEqual(tags, { |
| 95 | + agentworkforce: '1', |
| 96 | + persona: 'code-reviewer', |
| 97 | + personaTier: 'best', |
| 98 | + personaVersion: personaVersionHash(spec), |
| 99 | + personaSource: 'dir:1' |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +test('startBurnAttribution writes a pending stamp and runs periodic plus final ingest', async () => { |
| 104 | + const stamps: BurnPendingStampOptions[] = []; |
| 105 | + const ingests: BurnIngestOptions[] = []; |
| 106 | + const run = await startBurnAttribution({ |
| 107 | + selection: fakeSelection(), |
| 108 | + personaSpec: fakeSpec(), |
| 109 | + personaSource: 'cwd', |
| 110 | + cwd: '/tmp/project', |
| 111 | + intervalMs: 5, |
| 112 | + now: () => new Date('2026-05-08T12:00:00.000Z'), |
| 113 | + sdk: { |
| 114 | + writePendingStamp: (opts) => { |
| 115 | + stamps.push(opts); |
| 116 | + }, |
| 117 | + ingest: (opts) => { |
| 118 | + if (opts) ingests.push(opts); |
| 119 | + } |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + assert.equal(run.enabled, true); |
| 124 | + await sleep(25); |
| 125 | + await run.stop(); |
| 126 | + |
| 127 | + assert.equal(stamps.length, 1); |
| 128 | + assert.equal(stamps[0]?.harness, 'codex'); |
| 129 | + assert.equal(stamps[0]?.cwd, '/tmp/project'); |
| 130 | + assert.equal(stamps[0]?.spawnStartTs, '2026-05-08T12:00:00.000Z'); |
| 131 | + assert.equal(stamps[0]?.enrichment.persona, 'code-reviewer'); |
| 132 | + assert.ok(ingests.length >= 2, 'expected at least one periodic ingest plus final ingest'); |
| 133 | + assert.deepEqual(ingests.at(-1), { harness: 'codex' }); |
| 134 | +}); |
| 135 | + |
| 136 | +test('startBurnAttribution skips SDK loading and ingest when opted out', async () => { |
| 137 | + let loads = 0; |
| 138 | + const run = await startBurnAttribution({ |
| 139 | + selection: fakeSelection(), |
| 140 | + personaSpec: fakeSpec(), |
| 141 | + personaSource: 'library', |
| 142 | + cwd: '/tmp/project', |
| 143 | + noBurn: true, |
| 144 | + sdk: async () => { |
| 145 | + loads += 1; |
| 146 | + throw new Error('should not load'); |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + await run.stop(); |
| 151 | + assert.equal(run.enabled, false); |
| 152 | + assert.equal(loads, 0); |
| 153 | + assert.equal( |
| 154 | + shouldEnableBurnAttribution({ env: { AGENTWORKFORCE_BURN: '0' } }), |
| 155 | + false |
| 156 | + ); |
| 157 | +}); |
| 158 | + |
| 159 | +test('burnIngestHarness maps AgentWorkforce claude to Burn claude-code', () => { |
| 160 | + assert.equal(burnIngestHarness('claude'), 'claude-code'); |
| 161 | + assert.equal(burnIngestHarness('codex'), 'codex'); |
| 162 | + assert.equal(burnIngestHarness('opencode'), 'opencode'); |
| 163 | +}); |
0 commit comments