diff options
| author | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 18:07:14 -0700 |
|---|---|---|
| committer | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 18:11:07 -0700 |
| commit | c9e990089c6c87cb96e227e998edbada1df82861 (patch) | |
| tree | 623eb2b217930fd1e56558946443cd2ddeb5b849 /plugins/code-modernization/tests/logic.test.ts | |
| parent | 70caddcb673ac8995d6a5191e02ac51de579d6c3 (diff) | |
code-modernization: add an optional live progress pane
Adds a hooks module, off unless the CLI's early-access function hooks
are enabled, that draws a pane beside the transcript while the commands
run: the system, which steps are done, a map of its code, each unit of
work and where it stands, what needs attention, and the next command.
It reads only the artifacts the commands write and the legacy tree, so
it works for any language and for a rewrite, a same-stack uplift or a
greenfield rebuild. The map comes from topology.json when there is one
and from the source tree when there is not. In an uplift each module is
set against the baseline; tests are read from JUnit XML, .trx reports or
the runs a session sees; a button shows and hides the pane, and
/modernize-panel does the same from the keyboard.
The same module adds x-ray context on legacy reads, a review deck for
business rules (/modernize-review), a sign-off dialog (/modernize-sign)
and a fleet view that spots one failure repeating across agents. It
only watches: it never blocks, rewrites or restores an edit, and it runs
no program inside the trees it reads. An uplift's changed modules are
found by comparing the working copy with the tree by file name and size
through the file API. Its options are the plugin's userConfig. Without
the flag nothing loads and the commands, agents and workflows behave as
before.
91 tests in tests/ run with "claude plugin test"; plugin validate
passes on the release build with the flag on and off.
Diffstat (limited to 'plugins/code-modernization/tests/logic.test.ts')
| -rw-r--r-- | plugins/code-modernization/tests/logic.test.ts | 570 |
1 files changed, 570 insertions, 0 deletions
diff --git a/plugins/code-modernization/tests/logic.test.ts b/plugins/code-modernization/tests/logic.test.ts new file mode 100644 index 00000000..f4c258e9 --- /dev/null +++ b/plugins/code-modernization/tests/logic.test.ts @@ -0,0 +1,570 @@ +import { describe, expect, test } from 'claude-code/testing' + +import { + announceThresholdOf, + lineOf, + missingPathOf, + newFleet, + normalizeFailure, + noteCall, + noteDone, + noteFailure, + prune, + subjectOf, + tallyOf, +} from '../hooks/fleet/fleet' +import { paint, tilesOf } from '../hooks/map/estate' +import { base64Of, codePointOf, mix, packCells } from '../hooks/map/raster' +import { layout, layoutGroups } from '../hooks/map/treemap' +import { absOf, isUnder, relTo, resolveDots } from '../hooks/paths' +import { criterionTextOf, parseBrief } from '../hooks/reader/brief' +import { readNotes, stateOf, totalsOfJunitXml } from '../hooks/reader/modernized' +import { citationsIn, idOfTitle, needsReview, parseRules } from '../hooks/reader/rules' +import { nodeOfFile, parseTopology } from '../hooks/reader/topology' +import { decide, ledgerMarkdown, nextUnreviewed, queueOf, undecide } from '../hooks/review/deck' +import { signBrief } from '../hooks/sign' +import { readTestRun, TEST_COMMAND } from '../hooks/tests-run' +import { wrapLines } from '../hooks/views/deck' +import { BRIEF_SIGNED, BRIEF_UNSIGNED, RULES, RULES_BR, RULES_LABELLED, TOPOLOGY } from './fixtures/workspace' + +describe('paths', () => { + test('relative and absolute paths resolve against the working directory', () => { + expect(absOf('/work', 'a/../b/./c')).toBe('/work/b/c') + expect(resolveDots('/a/b/../../c')).toBe('/c') + expect(relTo('/work', '/work/legacy/x.cbl')).toBe('legacy/x.cbl') + expect(relTo('/work', 'legacy/x.cbl')).toBe('legacy/x.cbl') + expect(relTo('/work', '/elsewhere/x')).toBe(null) + expect(relTo('/private/tmp/ws', '/tmp/ws/a')).toBe('a') + }) + + test('isUnder does not confuse a sibling whose name starts the same', () => { + expect(isUnder('legacy/a.cbl', 'legacy')).toBe(true) + expect(isUnder('legacy-old/a.cbl', 'legacy')).toBe(false) + expect(isUnder('legacy', 'legacy')).toBe(true) + }) +}) + +describe('topology', () => { + test('reads modules, domains, edges and flows, and maps a file to its node', () => { + const topo = parseTopology(TOPOLOGY) + + expect(topo?.modules.map(node => node.id)).toEqual(['INTCALC', 'ACCTUPD', 'ACCTVIEW']) + expect(topo?.domains.map(domain => `${domain.name}:${domain.modules.length}`)).toEqual(['D1 Interest:1', 'D2 Accounts:2']) + expect(topo?.edges.length).toBe(3) + expect(topo && nodeOfFile(topo, 'app/cbl/INTCALC.cbl')?.id).toBe('INTCALC') + expect(topo && nodeOfFile(topo, 'INTCALC.cbl')?.id).toBe('INTCALC') + expect(topo && nodeOfFile(topo, 'app/cbl/NOPE.cbl')).toBe(null) + }) + + test('garbage is no topology, never a throw', () => { + expect(parseTopology('not json')).toBe(null) + expect(parseTopology('[]')).toBe(null) + expect(parseTopology('{"root": 7}')).toBe(null) + }) +}) + +describe('rules', () => { + test('cards and table rows parse, with priorities from ids and section headings', () => { + const rules = parseRules(RULES) + + expect(rules.rules.map(rule => `${rule.id.replace(/^R-[0-9a-f]{6}$/, 'R-hash')}:${rule.priority}`)).toEqual([ + 'P0-001:P0', + 'P0-002:P0', + 'P0-003:P0', + 'R-hash:P1', + ]) + expect(idOfTitle('View shows masked id!'), 'the id is of the title, whatever its punctuation').toBe(rules.rules[3]?.id) + + const first = rules.byId.get('P0-001') + + expect(first?.title).toBe('Monthly interest is truncated, not rounded') + expect(first?.confidence).toBe('High') + expect(first?.category).toBe('Calculation') + expect(first?.given).toContain('$1,250.00') + expect(first?.then).toContain('$19.27') + expect(first?.defect).toContain('Truncation') + expect(first?.edgeCases).toEqual(['A zero rate skips the account']) + expect(first?.citations[0]).toEqual({ path: 'legacy/billing/app/cbl/INTCALC.cbl', base: 'intcalc.cbl', from: 462, to: 470 }) + expect(first?.domain).toBe('D1 Interest') + }) + + test('the appendix is skipped, and rules index by the file they cite', () => { + const rules = parseRules(RULES) + + expect(rules.rules.some(rule => rule.title === 'Not a rule')).toBe(false) + expect(rules.byFileBase.get('intcalc.cbl')?.map(rule => rule.id)).toEqual(['P0-001', 'P0-002']) + }) + + test('needsReview picks the defect, the SME note and the lower confidence', () => { + const rules = parseRules(RULES) + + expect(rules.rules.filter(needsReview).map(rule => rule.id)).toEqual(['P0-001', 'P0-002']) + }) + + test('a card whose id carries a domain, with the priority in its header line and list-style Given/When/Then, reads whole', () => { + const rules = parseRules(RULES_BR) + + expect(rules.rules.map(rule => `${rule.id}:${rule.priority}`)).toEqual(['BR-D7-09:P0', 'BR-D7-10:P2']) + + const interest = rules.byId.get('BR-D7-09') + + expect(interest?.category).toBe('Calculation') + expect(interest?.confidence).toBe('High') + expect(interest?.given).toBe('A balance of 1250.00 at rate 15.00') + expect(interest?.when).toBe('The interest batch processes that row') + expect(interest?.then).toBe('Interest is 15.62 (truncated, NOT rounded)') + expect(interest?.citations[0]).toEqual({ path: 'app/cbl/CBACT04C.cbl', base: 'cbact04c.cbl', from: 462, to: 470 }) + expect(interest?.defect).toContain('Truncation') + expect(rules.byId.get('BR-D7-10')?.sme).toBe('Should zero interest print?') + }) + + test('the legend and appendix tables of that layout are not rules', () => { + expect(parseRules(RULES_BR).rules.map(rule => rule.title)).toEqual([ + 'Monthly interest per transaction-category balance', + 'Interest statement line', + ]) + }) + + test('the labelled-fields layout reads whole: statement, fenced Given/When/Then, edge cases, defect and SME question', () => { + const rules = parseRules(RULES_LABELLED) + + expect(rules.rules.map(rule => `${rule.id}:${rule.priority}`), 'the summary table and the appendix are not rules').toEqual([ + 'RULE-007:P0', + 'RULE-004:P0', + 'RULE-090:P2', + ]) + + const interest = rules.byId.get('RULE-007') + + expect(interest?.title).toBe('Monthly interest per transaction category using disclosure-group rate') + expect(interest?.category).toBe('Calculation') + expect(interest?.confidence).toBe('High') + expect(interest?.statement).toContain('divided by 1200') + expect(interest?.given).toContain("Account in group 'GOLD'") + expect(interest?.when).toBe('CBACT04C processes that category record') + expect(interest?.then).toContain('truncated not rounded') + expect(interest?.then, 'an And line continues the Then').toContain("read from 'DEFAULT'") + expect(interest?.edgeCases).toEqual(['No ROUNDED clause on the COMPUTE; result is truncated to cents', 'Negative balances produce negative interest']) + expect(interest?.citations[0]).toEqual({ path: 'app/cbl/CBACT04C.cbl', base: 'cbact04c.cbl', from: 188, to: 222 }) + expect(interest?.domain).toBe('Calculation rules') + expect(interest?.defect).toBeUndefined() + expect(interest?.sme).toBeUndefined() + + const counters = rules.byId.get('RULE-004') + + expect(counters?.defect).toBe('Line 821 adds a stale amount to the declined total.') + expect(counters?.sme).toBe("Should declined-amount accumulate the current request's amount?") + expect(counters?.confidence).toBe('Medium') + expect(rules.rules.filter(needsReview).map(rule => rule.id)).toEqual(['RULE-004']) + }) + + test('citations are found bare, ranged and with an en dash', () => { + expect(citationsIn('see A.cbl:10 and app/cpy/B.cpy:4–9, not http://x:80').map(c => `${c.base}:${c.from}-${c.to}`)).toEqual([ + 'a.cbl:10-10', + 'b.cpy:4-9', + ]) + }) +}) + +describe('brief', () => { + test('phases, the modules they name, criteria and the unsigned block', () => { + const brief = parseBrief(BRIEF_UNSIGNED, ['INTCALC', 'ACCTUPD', 'ACCTVIEW']) + + expect(brief.target).toBe('Java / Spring') + expect(brief.phases.map(phase => `${phase.number}:${phase.size}:${phase.modules.join('+')}:${phase.criteria.length}`)).toEqual([ + '1:M:INTCALC:2', + '2:L:ACCTUPD+ACCTVIEW:1', + ]) + expect(brief.approval).toEqual({ isSigned: false }) + expect(brief.openQuestions).toBe(1) + }) + + test('a phase lists its pilot first and the rest in the order it names them, not in topology order', () => { + const text = [ + '### Phase 1 — Harness plus one job · **Size M** · Risk **High**', + '', + 'Scope: `ACCTUPD`, `INTCALC`.', + '', + '**🎯 Pilot unit — `INTCALC`.** One program, taken end to end.', + '', + '| Risk | Mitigation |', + '| --- | --- |', + '| The online path is not proven | Phase 2 names `ACCTVIEW` |', + ].join('\n') + + expect(parseBrief(text, ['ACCTVIEW', 'ACCTUPD', 'INTCALC']).phases[0]?.modules).toEqual(['INTCALC', 'ACCTUPD', 'ACCTVIEW']) + }) + + test('a signed block reads who, when and what it covers', () => { + expect(parseBrief(BRIEF_SIGNED).approval).toEqual({ + isSigned: true, + by: 'VP Engineering', + date: '2026-09-15', + covers: 'phase-1', + }) + }) + + test('a module id is matched as a whole word only', () => { + const brief = parseBrief('### Phase 1 — X\n\nScope: INTCALC2 and XINTCALC.\n', ['INTCALC']) + + expect(brief.phases[0]?.modules).toEqual([]) + }) + + test('a tick annotation is not part of a criterion text', () => { + expect(criterionTextOf('- [x] Diff is clean (Ana, 2026-09-16)')).toBe('Diff is clean') + expect(criterionTextOf('not a box')).toBe(null) + }) +}) + +describe('modernized', () => { + test('junit totals from a suite and from a suites wrapper', () => { + expect(totalsOfJunitXml('<testsuite tests="9" failures="1" errors="2" skipped="3">')).toEqual({ tests: 9, failures: 1, errors: 2, skipped: 3, reports: 1 }) + expect(totalsOfJunitXml('<testsuites tests="4" failures="0" errors="0"><testsuite tests="4"/></testsuites>')?.tests).toBe(4) + expect(totalsOfJunitXml('<html/>')).toBe(null) + }) + + test('notes yield the review date, follow-ups and the switch state', () => { + expect(readNotes('## Architecture review\nReviewed 2026-09-15.\n## Follow-ups\n- a\n- b\n')).toEqual({ + hasReviewSection: true, + reviewDate: '2026-09-15', + isPortedNotSwitched: false, + isSwitched: false, + followUps: 2, + }) + expect(readNotes('Status: ported, not switched').isPortedNotSwitched).toBe(true) + expect(readNotes('Route switched 2026-09-10.').isSwitched).toBe(true) + }) + + test('states follow the facts, furthest first', () => { + const base = { dir: 'm', path: 'p', hasMain: true, hasTests: true, hasNotes: true, hasReviewSection: false, isPortedNotSwitched: false, isSwitched: false, followUps: 0, mtimeMs: 0 } + const green = { tests: 5, failures: 0, errors: 0, skipped: 0, reports: 1 } + + expect(stateOf({ ...base, hasTests: false, tests: null })).toBe('scaffolded') + expect(stateOf({ ...base, tests: null })).toBe('tests-written') + expect(stateOf({ ...base, tests: { ...green, failures: 1 } })).toBe('tests-red') + expect(stateOf({ ...base, tests: green })).toBe('tests-green') + expect(stateOf({ ...base, tests: green, hasReviewSection: true })).toBe('reviewed') + expect(stateOf({ ...base, tests: green, hasReviewSection: true, isPortedNotSwitched: true })).toBe('ported') + expect(stateOf({ ...base, tests: green, hasReviewSection: true, isSwitched: true })).toBe('switched') + expect(stateOf({ ...base, tests: { ...green, tests: 0 } }), 'reports with zero cases are not green').toBe('tests-written') + }) +}) + +describe('sign', () => { + test('fills the block in place and nothing else', () => { + const signed = signBrief(BRIEF_UNSIGNED, ' Ana Lopez ', '2026-09-16', 'phase-1') + + expect(signed).toContain('Approved by: Ana Lopez') + expect(signed).toContain('Date: 2026-09-16') + expect(signed).toContain('[X] Phase 1 only [ ] Full plan') + expect(parseBrief(signed ?? '').approval).toEqual({ isSigned: true, by: 'Ana Lopez', date: '2026-09-16', covers: 'phase-1' }) + }) + + test('the one-line and the worded forms are filled too', () => { + const oneLine = '## 8. Approval Block\n```\nApproved by: ________________ Date: __________\nApproval covers: Phase 1 only | Full plan\n```\n' + const signed = signBrief(oneLine, 'Ana', '2026-09-16', 'full') + + expect(signed).toContain('Approved by: Ana Date: 2026-09-16') + expect(signed).toContain('Approval covers: Full plan') + expect(parseBrief(signed ?? '').approval.covers).toBe('full') + expect(signBrief('no block here', 'Ana', '2026-09-16', 'full')).toBe(null) + }) +}) + +describe('test runs', () => { + test('commands that run a suite are recognised, others are not', () => { + for (const command of ['mvn -q test', './mvnw verify', 'cd x && gradle test', 'pytest -q', 'npm test', 'npm run test', 'dotnet test', 'go test ./...', 'cargo test', 'JAVA_HOME=/x mvn -q -o test 2>&1 | tail -40', 'cd modernized/x; time ./gradlew check']) { + expect(TEST_COMMAND.test(command), command).toBe(true) + } + + for (const command of ['mvn compile', 'ls test', 'git status', 'cat pytest.ini', 'grep -rn pytest .', 'echo "run npm test later"']) { + expect(TEST_COMMAND.test(command), command).toBe(false) + } + }) + + test('totals are read per runner, and a run of nothing is zero executed', () => { + expect(readTestRun('Tests run: 3, Failures: 0, Errors: 0, Skipped: 0\nTests run: 174, Failures: 1, Errors: 0, Skipped: 2')).toEqual({ executed: 172, failed: 1, skipped: 2 }) + expect(readTestRun('Tests run: 12, Failures: 0, Errors: 0, Skipped: 12\nBUILD SUCCESS')).toEqual({ executed: 0, failed: 0, skipped: 12 }) + expect(readTestRun('==== 3 passed, 1 skipped in 0.12s ====')).toEqual({ executed: 3, failed: 0, skipped: 1 }) + expect(readTestRun('collected 0 items')).toEqual({ executed: 0, failed: 0, skipped: 0 }) + expect(readTestRun('Tests: 1 failed, 2 skipped, 9 passed, 12 total')).toEqual({ executed: 10, failed: 1, skipped: 2 }) + expect(readTestRun('Passed! - Failed: 0, Passed: 12, Skipped: 1, Total: 13')).toEqual({ executed: 12, failed: 0, skipped: 1 }) + expect(readTestRun('test result: ok. 7 passed; 0 failed; 1 ignored')).toEqual({ executed: 7, failed: 0, skipped: 1 }) + expect(readTestRun('compiled fine')).toBe(null) + }) +}) + +describe('treemap and raster', () => { + test('every item is placed without overlap and the area is covered', () => { + const items = [500, 300, 120, 80, 40, 20, 5, 1].map((size, index) => ({ item: index, size })) + const placed = layout(items, { x: 0, y: 0, w: 40, h: 10 }) + const owner = new Array<number>(400).fill(-1) + let overlaps = 0 + + for (const entry of placed) { + for (let row = 0; row < entry.rect.h; row += 1) { + for (let col = 0; col < entry.rect.w; col += 1) { + const key = (entry.rect.y + row) * 40 + entry.rect.x + col + + overlaps += owner[key] === -1 ? 0 : 1 + owner[key] = entry.item + } + } + } + + expect(placed.length).toBe(8) + expect(overlaps).toBe(0) + expect(owner.filter(id => id === -1).length).toBe(0) + }) + + test('groups keep their items inside their own rectangle', () => { + const placed = layoutGroups( + [ + { name: 'a', items: [{ item: 'a1', size: 10 }, { item: 'a2', size: 5 }] }, + { name: 'b', items: [{ item: 'b1', size: 30 }] }, + { name: 'empty', items: [] }, + ], + { x: 0, y: 0, w: 30, h: 8 }, + ) + + expect(placed.map(group => group.name).sort()).toEqual(['a', 'b']) + + for (const group of placed) { + for (const entry of group.items) { + expect(entry.rect.x >= group.rect.x && entry.rect.x + entry.rect.w <= group.rect.x + group.rect.w).toBe(true) + expect(entry.rect.y >= group.rect.y && entry.rect.y + entry.rect.h <= group.rect.y + group.rect.h).toBe(true) + } + } + }) + + test('cells pack to twelve bytes each, base64 is standard, odd glyphs become spaces', () => { + expect(base64Of(new Uint8Array([77, 97, 110]))).toBe('TWFu') + expect(base64Of(new Uint8Array([77, 97]))).toBe('TWE=') + expect(base64Of(new Uint8Array([77]))).toBe('TQ==') + expect(packCells([{ glyph: 'A', fg: 0x112233, bg: 0x01000000 }]).length).toBe(16) + expect(codePointOf('\u{1F600}')).toBe(0x20) + expect(codePointOf('漢')).toBe(0x20) + expect(codePointOf('▸')).toBe(0x25b8) + expect(mix(0x000000, 0xffffff, 0.5)).toBe(0x808080) + }) +}) + +describe('fleet', () => { + test('a failure shared by three agents is announced once', () => { + const fleet = newFleet() + const error = (n: number) => `error CS0246: The type 'NUnit${n}' could not be found in /src/proj${n}/A.cs(12,3)` + + for (const id of ['a', 'b', 'c', 'd']) { + noteCall(fleet, id, 'Bash', 'dotnet build', 1) + } + + expect(normalizeFailure(error(1))).toBe(normalizeFailure(error(2))) + expect(noteFailure(fleet, 'a', error(1), 10)).toBe(null) + expect(noteFailure(fleet, 'a', error(1), 11), 'the same agent twice is still one agent').toBe(null) + expect(noteFailure(fleet, 'b', error(2), 12)).toBe(null) + expect(noteFailure(fleet, 'c', error(3), 13)?.agents.size).toBe(3) + expect(noteFailure(fleet, 'd', error(4), 14), 'announced once').toBe(null) + expect(tallyOf(fleet, 20).shared.length).toBe(1) + }) + + test('what names no cause is not a signature', () => { + // A search that found nothing, and a malformed call: neither is something agents share. + expect(normalizeFailure('Exit code 1\napp/data/ASCII/:\nacctdata.txt carddata.txt')).toBe('') + expect(normalizeFailure('Exit code 1')).toBe('') + expect(normalizeFailure('<tool_use_error>InputValidationError: Read was called with input that could not be parsed as JSON.')).toBe('') + expect(normalizeFailure('Exit code 2\n12 "x"'), 'only placeholders left').toBe('') + }) + + test('one cause worded per command or per call is one signature', () => { + const missing = [ + 'File does not exist. Note: your current working directory is /Users/x/workspace.', + 'Exit code 2\nugrep: warning: /Users/x/workspace/app/cbl/CBTRN02C.cbl: No such file or directory', + 'Exit code 1\nwc: /Users/x/workspace/app/cbl/COPAUA0C.cbl: open: No such file or directory', + 'Exit code 1 ls: /Users/x/workspace/app/cbl/: No such file or directory', + ] + + expect(new Set(missing.map(normalizeFailure))).toEqual(new Set(['a path that does not exist'])) + expect(missingPathOf(missing[2] ?? '')).toBe('/Users/x/workspace/app/cbl/COPAUA0C.cbl') + expect(missingPathOf(missing[0] ?? '')).toBe(undefined) + + expect( + normalizeFailure("Permission to use Bash with command cd /Users/x/legacy/carddemo && sed -n '/^ 5000-PROCESS/,/^ 5000-EXIT/p' app/cbl/A.cbl has been denied."), + ).toBe(normalizeFailure("Permission to use Bash with command sed -n '/1245-EDIT\\./,/1245-EXIT\\./p' /Users/x/legacy/carddemo/app/cbl/B.cbl has been denied.")) + + // Which property broke which rule survives; which element of the array does not. + const schema = (n: number, twice: boolean) => + `Output does not match required schema: /rules/${n}/category: must be equal to one of the allowed values: ["Calculation","Validation"]` + + (twice ? `, /rules/${n + 6}/category: must be equal to one of the allowed values: ["Calculation","Validation"]` : '') + + expect(normalizeFailure(schema(13, true))).toBe(normalizeFailure(schema(27, false))) + expect(normalizeFailure(schema(13, true))).toBe( + 'Output does not match required schema: /rules/<n>/category: must be equal to one of the allowed values', + ) + + expect(normalizeFailure('Exit code 1\n2575: PERFORM 9000-READ\n(eval):1: == not found')).toBe( + normalizeFailure('Exit code 1\n(eval):3: ==DATA=== not found'), + ) + }) + + test('the bar for calling a failure out rises with the fleet, and it is called out again only once it has spread', () => { + const fleet = newFleet() + const miss = 'File does not exist. Note: your current working directory is /w.' + + for (let index = 0; index < 200; index += 1) { + noteCall(fleet, `agent-${index}`, 'Read', 'A.cbl', 1) + } + + expect(announceThresholdOf(fleet)).toBe(10) + + const hits: number[] = [] + + for (let index = 0; index < 120; index += 1) { + // One failure a second: the quiet minute has long passed by the time it has spread fivefold. + const hit = noteFailure(fleet, `agent-${index}`, miss, 1000 + index * 1000, `app/cbl/P${index}.cbl`) + + if (hit !== null) { + hits.push(hit.agents.size) + } + } + + expect(hits, 'at the bar, then once it has spread fivefold and a minute has passed').toEqual([10, 70]) + + const shared = tallyOf(fleet, 0).shared[0] + + expect(shared !== undefined ? lineOf(shared) : '').toBe( + '120 agents hit the same failure: a path that does not exist (e.g. app/cbl/P0.cbl, app/cbl/P1.cbl)', + ) + expect(shared !== undefined ? lineOf(shared, 'short') : '').toBe('120 agents: a path that does not exist (e.g. app/cbl/P0.cbl)') + }) + + test('a fan-out that fails all at once is not called out twice in a minute', () => { + const fleet = newFleet() + + for (let index = 0; index < 40; index += 1) { + noteCall(fleet, `agent-${index}`, 'Read', 'A.cbl', 1) + } + + const hits: number[] = [] + + for (let index = 0; index < 40; index += 1) { + const hit = noteFailure(fleet, `agent-${index}`, 'File does not exist.', 1000 + index * 100) + + if (hit !== null) { + hits.push(hit.agents.size) + } + } + + expect(hits).toEqual([3]) + }) + + test('the widest-spread failure is listed first', () => { + const fleet = newFleet() + + for (let index = 0; index < 8; index += 1) { + noteCall(fleet, `agent-${index}`, 'Bash', 'x', 1) + } + + for (let index = 0; index < 3; index += 1) { + noteFailure(fleet, `agent-${index}`, 'error TS2304: Cannot find name', 10) + } + + for (let index = 0; index < 6; index += 1) { + noteFailure(fleet, `agent-${index}`, 'File does not exist.', 20) + } + + expect(tallyOf(fleet, 30).shared.map(signature => signature.agents.size)).toEqual([6, 3]) + }) + + test('a shell call is labelled by what runs, not where', () => { + expect(subjectOf('Bash', { command: 'cd /very/long/path 2>/dev/null; ls -la' })).toBe('ls -la') + expect(subjectOf('Bash', { command: 'cd /very/long/path/to/module && mvn -q test' })).toBe('mvn -q test') + expect(subjectOf('Bash', { command: 'W=/tmp/x; cd "$W/analysis" && export A=1 && cat RULES.md' })).toBe('cat RULES.md') + expect(subjectOf('Read', { file_path: '/a/b/C.cbl' })).toBe('C.cbl') + }) + + test('totals survive pruning', () => { + const fleet = newFleet() + + for (let index = 0; index < 20; index += 1) { + noteCall(fleet, `agent-${index}`, 'Read', 'a.cbl', index, 'MOD') + noteDone(fleet, `agent-${index}`, index + 1) + } + + prune(fleet, 100, 0, 5) + + const tally = tallyOf(fleet, 100) + + expect(fleet.agents.size <= 5).toBe(true) + expect(tally.total).toBe(20) + expect(tally.done).toBe(20) + expect(tally.calls).toBe(20) + }) +}) + +describe('deck', () => { + test('flagged rules come first by weight, and verdicts move the cursor on', () => { + const rules = parseRules(RULES) + const queue = queueOf(rules, 'flagged') + + expect(queue.map(rule => rule.id)).toEqual(['P0-002', 'P0-001']) + expect(queueOf(rules, 'p0').length).toBe(3) + expect(queueOf(rules, 'all').length).toBe(4) + expect(queueOf(rules, 'all', 'acctview').map(rule => rule.title)).toEqual(['View shows masked id']) + expect(queueOf(rules, 'p0', 'truncat').map(rule => rule.id), 'the filter reads what the rule says, not only its title').toEqual(['P0-001']) + + const first = queue[0] + + if (first === undefined) { + throw new Error('empty queue') + } + + const ledger = decide({}, first, 'wrong', '2026-09-16T00:00:00Z') + + expect(nextUnreviewed(queue, ledger, 0)).toBe(1) + expect(nextUnreviewed(queue, undecide(ledger, first.id), 0)).toBe(0) + expect(ledgerMarkdown('billing', ledger)).toContain('| P0-002 | Wrong | 2026-09-16 |') + }) + + test('cards wrap into counted rows', () => { + expect(wrapLines('one two three four five', 9, 5)).toEqual(['one two', 'three', 'four five']) + expect(wrapLines('one two three four five', 9, 2)).toEqual(['one two', 'three…']) + expect(wrapLines('', 9, 2)).toEqual([]) + expect(wrapLines('abcdefghijklmnop', 8, 3)).toEqual(['abcdefgh', 'ijklmnop']) + }) +}) + +describe('estate', () => { + test('tiles take their state from the transformed modules and flash when touched', () => { + const topology = parseTopology(TOPOLOGY) + + if (topology === null) { + throw new Error('no topology') + } + + const snapshot = { + system: 'billing', + topology, + byNode: new Map([['INTCALC', { state: 'reviewed' }]]), + next: { text: '/x:modernize-transform billing ACCTUPD java', isByHand: false, reason: '' }, + } as never + + const tiles = tilesOf(snapshot, 40, 8) + + expect(tiles.map(tile => `${tile.id}:${tile.state}:${tile.isNext}`).sort()).toEqual([ + 'ACCTUPD:untouched:true', + 'ACCTVIEW:untouched:false', + 'INTCALC:reviewed:false', + ]) + + const still = paint(tiles, 40, 8, new Map(), 10_000) + const hot = paint(tiles, 40, 8, new Map([['INTCALC', { atMs: 9_900, kind: 'write' as const }]]), 10_000) + const cold = paint(tiles, 40, 8, new Map([['INTCALC', { atMs: 1_000, kind: 'write' as const }]]), 10_000) + + expect(still.isAnimating).toBe(false) + expect(hot.isAnimating).toBe(true) + expect(hot.cells === still.cells).toBe(false) + expect(cold.isAnimating).toBe(false) + expect(cold.cells).toBe(still.cells) + expect(atob(still.cells).length).toBe(40 * 8 * 12) + }) +}) |
