summaryrefslogtreecommitdiff
path: root/plugins/code-modernization/tests/register.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/code-modernization/tests/register.test.ts')
-rw-r--r--plugins/code-modernization/tests/register.test.ts428
1 files changed, 428 insertions, 0 deletions
diff --git a/plugins/code-modernization/tests/register.test.ts b/plugins/code-modernization/tests/register.test.ts
new file mode 100644
index 00000000..188fac41
--- /dev/null
+++ b/plugins/code-modernization/tests/register.test.ts
@@ -0,0 +1,428 @@
+import { describe, expect, mock, test } from 'claude-code/testing'
+
+import { parseBrief } from '../hooks/reader/brief'
+import { BAND, command, HINT, MAIN_SCREEN_HINT, PANE, SESSION, SIGN_PANE } from './fixtures/inputs'
+import { elementsOf, rowsOf, stringsOf, textOf, worldOf } from './fixtures/world'
+import { BRIEF_SIGNED, FULL, UNSIGNED } from './fixtures/workspace'
+
+const NAME = 'code-modernization'
+const BRIEF = 'analysis/billing/MODERNIZATION_BRIEF.md'
+
+describe('start and pane', () => {
+ test('the start registers the three commands and opens nothing by itself', async ($, on) => {
+ const world = worldOf(on, FULL)
+
+ mock.clock(on)
+ await $.session.start(SESSION)
+
+ expect(world.commands.sort()).toEqual(['modernize-panel', 'modernize-review', 'modernize-sign'])
+ expect(world.opened).toEqual([])
+ })
+
+ test('under the fullscreen layout the pane opens once the hint has drawn; on the main screen it waits to be asked', async ($, on) => {
+ const world = worldOf(on, FULL)
+ const clock = mock.clock(on)
+
+ on('ui.render', () => ({ type: 'Text', children: [''] }))
+ await $.session.start(SESSION)
+ await $.ui.render(MAIN_SCREEN_HINT)
+ await clock.advance(200)
+
+ expect(world.opened, 'not fullscreen: nothing opens unasked').toEqual([])
+
+ await $.ui.render(HINT)
+ await clock.advance(200)
+
+ expect(world.opened).toEqual([{ id: 'modernize', focus: false }])
+
+ await $.ui.render(HINT)
+ await clock.advance(200)
+
+ expect(world.opened.length, 'opened once').toBe(1)
+ })
+
+ test('a build that never draws the prompt hint still opens the pane, from the band\'s render', async ($, on) => {
+ const world = worldOf(on, FULL)
+ const clock = mock.clock(on)
+
+ on('ui.render', () => ({ type: 'Text', children: [''] }))
+ await $.session.start(SESSION)
+ // As 2.1.273 draws it: a viewport with no `isFullscreen`, and a band shorter than the screen.
+ await $.ui.render({ ...BAND, viewport: { columns: 160, rows: 45 }, props: { ...BAND.props, maxRows: 17 } })
+ await clock.advance(200)
+
+ expect(world.opened).toEqual([{ id: 'modernize', focus: false }])
+ })
+
+ test('on the main screen, where the band may be as tall as the terminal, nothing opens unasked', async ($, on) => {
+ const world = worldOf(on, FULL)
+ const clock = mock.clock(on)
+
+ on('ui.render', () => ({ type: 'Text', children: [''] }))
+ await $.session.start(SESSION)
+ await $.ui.render({ ...BAND, viewport: { columns: 160, rows: 45 }, props: { ...BAND.props, maxRows: 45 } })
+ await clock.advance(200)
+
+ expect(world.opened).toEqual([])
+ })
+
+ test('the pane opens when the first artifact appears, not only at the start', async ($, on) => {
+ const world = worldOf(on, { 'legacy/billing/app/cbl/INTCALC.cbl': 'x' })
+ const clock = mock.clock(on)
+
+ on('ui.render', () => ({ type: 'Text', children: [''] }))
+ on('tool.call', () => {
+ world.put('analysis/billing/PREFLIGHT.md', '# Preflight')
+
+ return { result: 'ok', text: 'ok' }
+ })
+
+ await $.session.start(SESSION)
+ await $.ui.render(BAND)
+ await clock.advance(200)
+ expect(world.opened, 'nothing to show yet').toEqual([])
+
+ await $.tool.call({ tool: 'Write', file_path: '/work/analysis/billing/PREFLIGHT.md', content: '# Preflight' })
+ await clock.advance(1000)
+
+ expect(world.opened).toEqual([{ id: 'modernize', focus: false }])
+ })
+
+ test('a workspace with no analysis opens no pane and says what to run', async ($, on) => {
+ const world = worldOf(on, { 'README.md': 'hi' })
+ const clock = mock.clock(on)
+
+ on('ui.render', () => ({ type: 'Text', children: [''] }))
+ await $.session.start(SESSION)
+ await $.ui.render(HINT)
+ await clock.advance(200)
+
+ expect(world.opened).toEqual([])
+ expect(textOf(await $.ui.render(PANE))).toContain('no system found under legacy/ or analysis/')
+ })
+
+ test('the pane draws the system, its stages, phases, the transformed module and the next step', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+ await $.session.start(SESSION)
+
+ const tree = await $.ui.render(PANE)
+ const text = textOf(tree)
+
+ expect(text).toContain('billing → Java / Spring')
+ expect(text).toContain('✓ preflight')
+ expect(text).toContain('✓ brief')
+ expect(text).toContain('✓ transform')
+ expect(text).toContain('· harden')
+ expect(text).toContain('P1 Interest pilot (D1)')
+ expect(text).toContain('INTCALC')
+ expect(text).toContain('12 green')
+ expect(text).toContain('/code-modernization:modernize-status billing')
+
+ const raster = elementsOf(tree, 'Raster')[0] as { props: { columns: number; rows: number; cells: string } } | undefined
+
+ expect(raster?.props.columns).toBe(71)
+ expect(atob(raster?.props.cells ?? '').length).toBe(71 * (raster?.props.rows ?? 0) * 12)
+ })
+
+ test('/modernize-panel toggles the pane and says which, and json prints the reading', async ($, on) => {
+ const world = worldOf(on, FULL)
+
+ mock.clock(on)
+ await $.session.start(SESSION)
+
+ expect(await $.command.run(command('modernize-panel'))).toEqual({ text: 'Modernization pane shown' })
+ expect(world.opened.at(-1)?.id).toBe('modernize')
+ expect(await $.command.run(command('modernize-panel'))).toEqual({ text: 'Modernization pane hidden' })
+ expect(world.closed).toEqual(['modernize'])
+
+ const { text } = await $.command.run(command('modernize-panel', 'json'))
+
+ expect(JSON.parse((text ?? '').replace(/```json\n|\n```/g, '')).modules[0]).toEqual({
+ dir: 'INTCALC',
+ state: 'reviewed',
+ tests: { tests: 12, failures: 0, errors: 0, skipped: 0, reports: 1 },
+ reviewDate: '2026-09-15',
+ })
+ })
+
+ test('the next button puts the next command in the prompt box, never runs it', async ($, on) => {
+ const world = worldOf(on, { ...UNSIGNED, [BRIEF]: BRIEF_SIGNED })
+
+ mock.clock(on)
+ await $.session.start(SESSION)
+ await $.ui.render(PANE)
+ await $.ui.press({ plugin: NAME, key: 'next' })
+
+ expect(world.fills).toEqual(['/code-modernization:modernize-transform billing INTCALC java-spring'])
+ })
+})
+
+describe('pane budget', () => {
+ test('the pane never draws more rows than its body has, busy or idle, tall or short', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+ on('tool.call', ($, e) => (e.tool === 'Bash' ? { isError: true as const, result: 'x', text: 'error: the same thing broke here' } : { result: 'x', text: 'x' }))
+ await $.session.start(SESSION)
+ await $.turn.start({ text: '/code-modernization:modernize-transform billing INTCALC java', turnId: 't1' })
+
+ for (const agentId of ['w1', 'w2', 'w3', 'w4']) {
+ await $.tool.call({ tool: 'Read', file_path: '/work/legacy/billing/app/cbl/INTCALC.cbl', agentId } as never)
+ await $.tool.call({ tool: 'Bash', command: 'mvn -q test', agentId } as never)
+ }
+
+ await $.tool.call({ tool: 'Read', file_path: '/work/legacy/billing/app/cbl/INTCALC.cbl' })
+ await $.tool.call({ tool: 'Bash', command: 'mvn -q test' })
+
+ for (const bodyRows of [24, 30, 36, 41, 44, 52, 60]) {
+ const tree = await $.ui.render({ ...PANE, props: { ...PANE.props, scroll: { offset: 0, bodyRows } } })
+
+ expect(rowsOf(tree) <= bodyRows, `pane fits ${bodyRows} rows (drew ${rowsOf(tree)})`).toBe(true)
+ expect(textOf(tree), 'the session block is always in view').toContain('Session')
+ expect(textOf(tree)).toContain('agents active')
+ }
+
+ // A dialog or another panel can take nearly all of the pane's rows, and a body is not always a whole number.
+ for (const bodyRows of [0, 1, 3, 8, 12, 16.5, 20]) {
+ const tree = await $.ui.render({ ...PANE, props: { ...PANE.props, scroll: { offset: 0, bodyRows } } })
+
+ for (const raster of elementsOf(tree, 'Raster') as { props: { rows: number; columns: number } }[]) {
+ expect(Number.isInteger(raster.props.rows) && raster.props.rows >= 1, `a map of ${raster.props.rows} rows in a body of ${bodyRows}`).toBe(true)
+ expect(Number.isInteger(raster.props.columns) && raster.props.columns >= 1).toBe(true)
+ }
+
+ expect(textOf(tree), `a body of ${bodyRows} rows still draws the pane`).toContain('billing')
+ }
+ })
+})
+
+describe('x-ray and the estate', () => {
+ test('a read of a legacy file carries what the analysis knows, narrowed to the lines read', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+ on('tool.call', () => ({ result: 'file text', text: 'file text' }))
+ await $.session.start(SESSION)
+
+ const whole = await $.tool.call({ tool: 'Read', file_path: '/work/legacy/billing/app/cbl/INTCALC.cbl' })
+ const note = (whole.context ?? []).join('\n')
+
+ expect(note).toContain('x-ray for app/cbl/INTCALC.cbl · INTCALC · D1 Interest · 600 lines')
+ expect(note).toContain('Reached from: ACCTUPD.')
+ expect(note).toContain('Reads: RATES.')
+ expect(note).toContain('Business rules citing this file: 2 (2 P0)')
+ expect(note).toContain('P0-001 Monthly interest is truncated, not rounded (L462-470) [suspected defect]')
+ expect(note).toContain('Already transformed at modernized/billing/INTCALC: reviewed, 12/12 tests passing.')
+
+ const window = await $.tool.call({ tool: 'Read', file_path: 'legacy/billing/app/cbl/INTCALC.cbl', offset: 410, limit: 20 })
+ const narrowed = (window.context ?? []).join('\n')
+
+ expect(narrowed).toContain('In the lines just read (410-429):')
+ expect(narrowed).toContain('P0-002')
+ expect(narrowed.includes('P0-001')).toBe(false)
+ })
+
+ test('a read outside the legacy tree, or of a file nothing knows, carries nothing', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+ on('tool.call', () => ({ result: 'x', text: 'x' }))
+ await $.session.start(SESSION)
+
+ expect((await $.tool.call({ tool: 'Read', file_path: '/work/analysis/billing/ASSESSMENT.md' })).context).toBe(undefined)
+ expect((await $.tool.call({ tool: 'Read', file_path: '/work/legacy/billing/README.md' })).context).toBe(undefined)
+ })
+
+ test('a read that misses is told where the file is, when it is under the legacy root', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+
+ on('tool.call', () => ({
+ isError: true as const,
+ result: 'x',
+ text: 'File does not exist. Note: your current working directory is /work.',
+ }))
+
+ await $.session.start(SESSION)
+
+ // A citation as the rules file writes it, read from the workspace root.
+ const cited = await $.tool.call({ tool: 'Read', file_path: '/work/app/cbl/INTCALC.cbl' })
+
+ expect(cited.context?.join('\n')).toContain('legacy/billing/app/cbl/INTCALC.cbl exists')
+
+ // A path that is nowhere, and one already under the legacy tree, get no such note.
+ expect((await $.tool.call({ tool: 'Read', file_path: '/work/app/cbl/NOPE.cbl' })).context).toBe(undefined)
+ expect((await $.tool.call({ tool: 'Read', file_path: '/work/legacy/billing/app/cbl/NOPE.cbl' })).context).toBe(undefined)
+ })
+
+ test('a touched tile is repainted while it fades and the repainting stops once it has', async ($, on) => {
+ const world = worldOf(on, FULL)
+ const clock = mock.clock(on, { now: 0 })
+
+ on('tool.call', () => ({ result: 'x', text: 'x' }))
+ await $.session.start(SESSION)
+ await $.ui.render(PANE)
+
+ expect(world.blits).toBe(0)
+
+ await $.tool.call({ tool: 'Read', file_path: '/work/legacy/billing/app/cbl/ACCTUPD.cbl' })
+ await clock.advance(450)
+
+ expect(world.blits > 0, 'frames are painted while the touch fades').toBe(true)
+ })
+})
+
+describe('fleet', () => {
+ test('three agents failing the same way raise one alert', async ($, on) => {
+ const world = worldOf(on, FULL)
+
+ mock.clock(on)
+
+ on('tool.call', ($, e) => ({
+ isError: true as const,
+ result: 'x',
+ text: `error CS0246: The type 'NUnit' could not be found in /src/${String(e.agentId)}/A.cs(12,3)`,
+ }))
+
+ await $.session.start(SESSION)
+
+ for (const agentId of ['w1', 'w2', 'w3', 'w4']) {
+ // A subagent's call carries the loop it runs in; a test's call says so the same way.
+ await $.tool.call({ tool: 'Bash', command: 'dotnet build', agentId } as never)
+ }
+
+ expect(world.toasts.filter(text => text.startsWith('Fleet:')).length).toBe(1)
+ expect(world.logs.some(text => text.includes('3 agents hit the same failure'))).toBe(true)
+
+ const text = textOf(await $.ui.render(PANE))
+
+ expect(text).toContain('agents active')
+ expect(text).toContain('4 agents: error CS0246')
+ })
+})
+
+describe('review deck', () => {
+ test('/modernize-review draws the first flagged card in the band, and a digit decides it', async ($, on) => {
+ const world = worldOf(on, FULL)
+ const clock = mock.clock(on)
+
+ on('ui.render', () => ({ type: 'Text', children: ['engine'] }))
+ await $.session.start(SESSION)
+
+ expect(textOf(await $.ui.render(BAND)), 'closed: the band holds only the bar that shows the pane').toContain('show pane')
+ expect(await $.command.run(command('modernize-review'))).toEqual({ text: 'Reviewing 2 rules.' })
+
+ const card = await $.ui.render(BAND)
+ const text = textOf(card)
+
+ expect(text).toContain('1/2')
+ expect(text).toContain('P0-002 · Missing rate row aborts the run')
+ expect(text).toContain('For an SME')
+ expect(stringsOf(card).filter(part => part === 'confirm').length).toBe(1)
+
+ await $.ui.press({ plugin: NAME, key: 'wrong' })
+ await clock.settle()
+
+ const ledger = world.writes.find(write => write.path === 'analysis/billing/RULE_REVIEWS.json')
+
+ expect(JSON.parse(ledger?.text ?? '{}').reviews['P0-002'].verdict).toBe('wrong')
+ expect(world.writes.some(write => write.path === 'analysis/billing/RULE_REVIEWS.md' && write.text.includes('| P0-002 | Wrong |'))).toBe(true)
+ expect(world.files.get('analysis/billing/BUSINESS_RULES.md'), 'the rules file is never edited').toBe(FULL['analysis/billing/BUSINESS_RULES.md'])
+
+ const next = textOf(await $.ui.render(BAND))
+
+ expect(next).toContain('2/2')
+ expect(next).toContain('P0-001')
+ expect(next).toContain('✗ 1')
+ })
+
+ test('the card never draws more rows than the band has, source shown or not', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+ await $.session.start(SESSION)
+ await $.command.run(command('modernize-review', 'all'))
+
+ for (const maxRows of [12, 16, 20, 30]) {
+ const input = { ...BAND, props: { ...BAND.props, maxRows } }
+
+ expect(rowsOf(await $.ui.render(input)) <= maxRows, `card fits ${maxRows} rows`).toBe(true)
+
+ await $.ui.press({ plugin: NAME, key: 'source' })
+ expect(rowsOf(await $.ui.render(input)) <= maxRows, `card with source fits ${maxRows} rows`).toBe(true)
+ await $.ui.press({ plugin: NAME, key: 'source' })
+ }
+ })
+
+ test('close hands the band back', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+ on('ui.render', () => ({ type: 'Text', children: ['engine'] }))
+ await $.session.start(SESSION)
+ await $.command.run(command('modernize-review', 'p0'))
+ await $.ui.render(BAND)
+ await $.ui.press({ plugin: NAME, key: 'close' })
+
+ expect(textOf(await $.ui.render(BAND)), 'the bar that shows the pane is back').toContain('show pane')
+ })
+})
+
+describe('signing', () => {
+ test('a person signs through the dialog: the block is filled and nothing else changes', async ($, on) => {
+ const world = worldOf(on, UNSIGNED)
+ const clock = mock.clock(on)
+
+ on('tool.call', () => ({ result: 'ok', text: 'ok' }))
+ await $.session.start(SESSION)
+
+ expect(textOf(await $.ui.render(PANE))).toContain('approve the brief')
+ expect(await $.command.run(command('modernize-sign'))).toEqual({})
+ expect(world.opened.at(-1)).toEqual({ id: 'modernize-sign', focus: true })
+
+ const dialog = await $.ui.render(SIGN_PANE)
+
+ expect(textOf(dialog)).toContain('treat an unsigned brief as not approved')
+ expect(textOf(dialog)).toContain('1 open question')
+
+ await $.ui.press({ plugin: NAME, key: 'do-sign' })
+ await clock.settle()
+ expect(textOf(await $.ui.render(SIGN_PANE)), 'no name, no signature').toContain('Type the approver')
+
+ await $.ui.press({ plugin: NAME, key: 'cancel' })
+ expect(await $.command.run(command('modernize-sign', 'Ana Lopez, VP Eng')), 'the name can ride the command').toEqual({})
+ expect(textOf(await $.ui.render(SIGN_PANE))).toContain('Ana Lopez, VP Eng')
+ await $.ui.press({ plugin: NAME, key: 'full' })
+ await $.ui.press({ plugin: NAME, key: 'do-sign' })
+ await clock.settle()
+
+ const signed = world.files.get(BRIEF) ?? ''
+
+ expect(parseBrief(signed).approval).toEqual({ isSigned: true, by: 'Ana Lopez, VP Eng', date: parseBrief(signed).approval.date, covers: 'full' })
+ expect(world.closed).toContain('modernize-sign')
+ expect(world.logs.some(text => text.includes('brief signed by Ana Lopez, VP Eng (full plan)'))).toBe(true)
+
+ const after = await $.tool.call({ tool: 'Bash', command: 'ls' })
+
+ expect(after.context, 'the person\'s signature is not a violation').toBe(undefined)
+ expect(world.files.get(BRIEF)).toBe(signed)
+ })
+
+ test('a signed brief, or a running turn, opens no dialog', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+ await $.session.start(SESSION)
+
+ expect((await $.command.run(command('modernize-sign'))).text).toContain('already signed by VP Engineering')
+ })
+})
+
+describe('prompt context', () => {
+ test('the one-line state rides a typed prompt once per change', async ($, on) => {
+ worldOf(on, FULL)
+ mock.clock(on)
+ await $.session.start(SESSION)
+
+ const first = await $.prompt.submit({ text: 'hello', wait: false, origin: { kind: 'composer' } })
+ const second = await $.prompt.submit({ text: 'again', wait: false, origin: { kind: 'composer' } })
+
+ expect((first.context ?? []).join(' ')).toContain('billing: discovery 5/5 · brief approved · 1/3 modules reviewed')
+ expect(second.context, 'unchanged: not attached again').toBe(undefined)
+ })
+})