// Calls that change a file show as a diff, in the approval card and the transcript. import assert from 'node:assert/strict' import { test } from 'node:test' import vm from 'node:vm' import { callChange, changeHtml, CHAT_JS, diffText, esc, type DiffLine } from '../src/web.ts' const text = (l: DiffLine) => (l[0] === '…' ? `… ${l[1]}` : l[0] + l[1].map(([t]) => t).join('')) test('an edit that adds lines after a kept line shows only the added lines as changed', () => { // The case from the web chat: newText is oldText with lines after it, and oldText has no newline. const lines = diffText(`'.md pre{a}',`, `'.md pre{a}',\n '.md .code-block{b}',\n '.md .code-toolbar{c}',`) assert.deepEqual(lines.map(text), [` '.md pre{a}',`, `+ '.md .code-block{b}',`, `+ '.md .code-toolbar{c}',`]) }) test('a changed line marks the changed words; a rewritten one is shown whole', () => { const [del, add] = diffText('const answer = 41', 'const answer = 42') assert.deepEqual(del, ['-', [['const answer = ', false], ['41', true]]]) assert.deepEqual(add, ['+', [['const answer = ', false], ['42', true]]]) // Too little in common to be worth marking. assert.deepEqual(diffText('alpha beta', 'gamma delta'), [['-', [['alpha beta', false]]], ['+', [['gamma delta', false]]]]) }) test('long runs of unchanged lines fold, keeping three next to each change', () => { const ctx = Array.from({ length: 10 }, (_, i) => `l${i}`) const lines = diffText(['x', ...ctx, 'y'].join('\n'), ['X', ...ctx, 'Y'].join('\n')) assert.deepEqual(lines.map(text), ['-x', '+X', ' l0', ' l1', ' l2', '… 4', ' l7', ' l8', ' l9', '-y', '+Y']) // At the edges only the side next to the change is kept. assert.deepEqual(diffText([...ctx, 'a'].join('\n'), [...ctx, 'b'].join('\n')).map(text), ['… 7', ' l7', ' l8', ' l9', '-a', '+b']) }) test('edit, write and block_replace calls become changes; others do not', () => { const edit = callChange('edit', JSON.stringify({ call_intention: 'Fix', path: 'src/a.ts', edits: [{ oldText: 'a', newText: 'b' }, { oldText: 'c', newText: 'c\nd' }] })) assert.equal(edit?.path, 'src/a.ts') assert.equal(edit?.hunks.length, 2) assert.deepEqual([edit?.added, edit?.removed], [2, 1]) // Edits as pi's edit tool also accepts them: a JSON string, a lone object, legacy oldText/newText. assert.equal(callChange('edit', JSON.stringify({ path: 'a', edits: JSON.stringify([{ oldText: 'a', newText: 'b' }]) }))?.hunks.length, 1) assert.equal(callChange('edit', JSON.stringify({ path: 'a', edits: { oldText: 'a', newText: 'b' } }))?.hunks.length, 1) assert.equal(callChange('edit', JSON.stringify({ path: 'a', oldText: 'a', newText: 'b' }))?.hunks.length, 1) const write = callChange('write', JSON.stringify({ path: '/tmp/n.txt', content: 'one\ntwo\n' })) assert.deepEqual(write?.hunks[0].map(text), ['+one', '+two']) assert.equal(callChange('block_replace', JSON.stringify({ label: 'human', oldText: 'a', newText: 'b' }))?.path, 'block human') assert.equal(callChange('bash', JSON.stringify({ command: 'ls' })), undefined) assert.equal(callChange('edit', JSON.stringify({ path: 'a', edits: [] })), undefined) assert.equal(callChange('edit', 'not json'), undefined) }) test('chat.js draws a change exactly as the page does', () => { // Just enough DOM to build nodes and print them. type Node = { tag: string; className: string; kids: (Node | string)[]; append: (...n: (Node | string)[]) => void; appendChild: (n: Node) => Node; textContent: string } const node = (tag: string): Node => { const n: Node = { tag, className: '', kids: [], append: (...k) => void n.kids.push(...k), appendChild: (k) => (n.kids.push(k), k), set textContent(t: string) { n.kids = [t] }, get textContent() { return '' }, } return n } const html = (n: Node | string): string => typeof n === 'string' ? esc(n) : `<${n.tag}${n.className ? ` class="${n.className}"` : ''}>${n.kids.map(html).join('')}` const el = (tag: string, cls?: string | null, t?: string | null) => { const e = node(tag) if (cls) e.className = cls if (t !== undefined && t !== null) e.textContent = t return e } const source = CHAT_JS.slice(CHAT_JS.indexOf(' const DIFF_KIND'), CHAT_JS.indexOf(' const agentName')) const changeNode = vm.runInNewContext(`${source}\nchangeNode`, { el, detail: () => '' }) const ctx = Array.from({ length: 8 }, (_, i) => `keep ${i} <&>`) const change = callChange('edit', JSON.stringify({ path: '/w/src/web.ts', edits: [{ oldText: ['let x = 1', ...ctx, 'end'].join('\n'), newText: ['let x = 2', ...ctx, 'end', '', 'more'].join('\n') }] }))! assert.equal(html(changeNode(change)), changeHtml(change)) })