diff --git a/.github/workflows/build-preview-cloudflare.yml b/.github/workflows/build-preview-cloudflare.yml index c9f60f3282..88359f70f0 100644 --- a/.github/workflows/build-preview-cloudflare.yml +++ b/.github/workflows/build-preview-cloudflare.yml @@ -111,7 +111,7 @@ jobs: continue-on-error: true run: >- pnpm exec tsx scripts/snapshot-diff-page.ts --base HEAD^1 --head HEAD - --out apps/web/dist/snapshot-diff/index.html + --out apps/web/dist/snapshot-diff --preview-manifest apps/web/dist/preview/fixtures.json - name: Upload to Cloudflare Pages diff --git a/package.json b/package.json index 37b86b60d5..a3104a5330 100644 --- a/package.json +++ b/package.json @@ -263,6 +263,7 @@ "@vitest/coverage-v8": "^4.1.11", "@yao-pkg/pkg": "6.21.0", "@yarnpkg/cli-dist": "4.18.0", + "diff": "^9.0.0", "eslint-plugin-sonarjs": "^4.2.0", "execa": "^10.0.1", "zod": "4.6.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ca94685474..c594b3b2f9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -186,6 +186,9 @@ importers: '@yarnpkg/cli-dist': specifier: 4.18.0 version: 4.18.0 + diff: + specifier: ^9.0.0 + version: 9.0.0 eslint-plugin-sonarjs: specifier: ^4.2.0 version: 4.2.0(eslint@10.5.0(jiti@2.7.0)(supports-color@9.4.0)) diff --git a/scripts/preview-workflow.spec.ts b/scripts/preview-workflow.spec.ts index b7c851c3eb..8df68dd11d 100644 --- a/scripts/preview-workflow.spec.ts +++ b/scripts/preview-workflow.spec.ts @@ -91,7 +91,7 @@ describe('PR preview workflow', () => { const steps = preview.steps.map(step => step.name) const page = preview.steps.find(step => step.name === 'Render the snapshot diff page')! expect(page.run).toBe('pnpm exec tsx scripts/snapshot-diff-page.ts --base HEAD^1 --head HEAD' - + ' --out apps/web/dist/snapshot-diff/index.html --preview-manifest apps/web/dist/preview/fixtures.json') + + ' --out apps/web/dist/snapshot-diff --preview-manifest apps/web/dist/preview/fixtures.json') expect(steps.indexOf('Render the snapshot diff page')).toBeGreaterThan(steps.indexOf('Shape the upload')) expect(steps.indexOf('Render the snapshot diff page')).toBeLessThan(steps.indexOf('Upload to Cloudflare Pages')) }) diff --git a/scripts/snapshot-diff-page.spec.ts b/scripts/snapshot-diff-page.spec.ts index 84ffd1dfff..d037d1bce7 100644 --- a/scripts/snapshot-diff-page.spec.ts +++ b/scripts/snapshot-diff-page.spec.ts @@ -1,66 +1,80 @@ import { describe, expect, it } from 'vitest' -import { addPreviewLink, catalogFiles, groupDiffs, limitDiffs, parseUnifiedDiff, renderSnapshotDiffPage } from './snapshot-diff-page.ts' +import { addPreviewLink, catalogFiles, groupDiffs, readableUnits, renderSnapshotDiffPage, sideBySide } from './snapshot-diff-page.ts' -const diff = `diff --git a/snapshots/sdk/text-turn/system-prompt.expected.md b/snapshots/sdk/text-turn/system-prompt.expected.md -index 1111111..2222222 100644 ---- a/snapshots/sdk/text-turn/system-prompt.expected.md -+++ b/snapshots/sdk/text-turn/system-prompt.expected.md -@@ -1,2 +1,2 @@ - You are an agent. --Old rule. -+New rule. -diff --git a/snapshots/web/ptc-round/session.jsonl b/snapshots/web/ptc-round/session.jsonl -deleted file mode 100644 -index 3333333..0000000 ---- a/snapshots/web/ptc-round/session.jsonl -+++ /dev/null -@@ -1 +0,0 @@ --{"type":"header"} -diff --git a/snapshots/web/ptc-round/image.png b/snapshots/web/ptc-round/image.png -new file mode 100644 -index 0000000..4444444 -Binary files /dev/null and b/snapshots/web/ptc-round/image.png differ -diff --git a/snapshots/web/present/workspace.expected/a b/说明.txt b/snapshots/web/present/workspace.expected/a b/说明.txt -old mode 100644 -new mode 100755 -` - -const file = (path: string, hunks = '') => ({ path, hunks, binary: false, omitted: 0 }) +const limits = { context: 1, rows: 100, wordDiffChars: 100, timeoutMs: 5000 } +const text = (...lines: string[]) => [lines.map(line => `${line}\n`).join('')] describe('snapshot diff page', () => { - it('splits git diff output into files with their hunks', () => { - expect(parseUnifiedDiff(diff)).toEqual([ - file('snapshots/sdk/text-turn/system-prompt.expected.md', '@@ -1,2 +1,2 @@\n You are an agent.\n-Old rule.\n+New rule.'), - file('snapshots/web/ptc-round/session.jsonl', '@@ -1 +0,0 @@\n-{"type":"header"}'), - { ...file('snapshots/web/ptc-round/image.png'), binary: true }, - file('snapshots/web/present/workspace.expected/a b/说明.txt'), + it('renders JSONL records as labelled YAML blocks and keeps other files whole', () => { + const jsonl = [ + '{"type":"user/message","data":{"text":"a\\nb"}}', + '{"method":"session.event","params":{"event":{"type":"turn/start"}}}', + '{"method":"ping"}', + '[1]', + 'not json', + '', + ].join('\n') + expect(readableUnits('s/session.jsonl', jsonl)).toEqual([ + '# user/message\ntype: user/message\ndata:\n text: |-\n a\n b\n', + '# session.event turn/start\nmethod: session.event\nparams:\n event:\n type: turn/start\n', + '# ping\nmethod: ping\n', + '[1]\n', + 'not json\n', ]) - expect(parseUnifiedDiff('')).toEqual([]) + expect(readableUnits('s/prompt.md', 'a\nb')).toEqual(['a\nb']) + expect(readableUnits('s/prompt.md', '')).toEqual([]) }) - it.each([ - ['a quoted path', 'diff --git "a/snapshots/x\\ty" "b/snapshots/x\\ty"\n'], - ['unequal header paths', 'diff --git a/snapshots/x b/snapshots/y\n'], - ['a missing prefix', 'diff --git snapshots/x snapshots/x\n'], - ])('rejects a header with %s', (_, text) => { - expect(() => parseUnifiedDiff(text)).toThrow('unsupported git diff header') + it('collapses unchanged runs beyond the context and pairs changed lines with word highlights', () => { + const result = sideBySide(text('a', 'b', 'c', 'd', 'old word', 'e', 'f', 'g'), text('a', 'b', 'c', 'd', 'new word', 'e', 'added', 'f', 'g'), limits) + expect(result).toEqual({ + added: 2, removed: 1, omitted: 0, + rows: [ + ['skip', 1, 1, ['a', 'b', 'c']], + ['same', 4, 4, 'd'], + ['change', 5, 5, [[true, 'old'], [false, ' word']], [[true, 'new'], [false, ' word']]], + ['same', 6, 6, 'e'], + ['add', 7, 'added'], + ['same', 7, 8, 'f'], + ['same', 8, 9, 'g'], + ], + }) }) - it('truncates long sections and drops sections past the byte budget of the escaped JSON', () => { - const lines = (count: number, line = '+x') => file(`f${count}`, ['@@', ...Array.from({ length: count - 1 }, () => line)].join('\n')) - const [long, fits, over] = limitDiffs([lines(150), lines(10), lines(10)], { fileLines: 100, totalBytes: 445 }) - expect([long?.hunks.split('\n').length, long?.omitted]).toEqual([100, 50]) - expect([fits?.hunks.split('\n').length, fits?.omitted]).toEqual([10, 0]) - expect([over?.hunks, over?.omitted]).toEqual(['', 10]) - // Quotes, `@@`, and nine lines of an escaped newline, `+`, and two six-byte \u003c escapes: 139 bytes. - expect(limitDiffs([lines(10, '+<<')], { fileLines: 100, totalBytes: 138 })[0]?.omitted).toBe(10) - expect(limitDiffs([lines(10, '+<<')], { fileLines: 100, totalBytes: 139 })[0]?.omitted).toBe(0) + it('shows unmatched removed lines and leaves long lines without word highlights', () => { + const long = 'x'.repeat(101) + expect(sideBySide(text(long, 'gone', 'z'), text(`${long}!`, 'z'), limits).rows).toEqual([ + ['change', 1, 1, long, `${long}!`], + ['del', 2, 'gone'], + ['same', 3, 2, 'z'], + ]) + }) + + it('keeps an inserted JSONL record as one added block when later records also change', () => { + const record = (type: string, seq: number) => `{"type":"${type}","seq":${String(seq)},"a":1,"b":2,"c":3}` + const before = readableUnits('x.jsonl', [record('one', 1), record('three', 2)].join('\n')) + const after = readableUnits('x.jsonl', [record('one', 1), record('two', 2), record('three', 3)].join('\n')) + const rows = sideBySide(before, after, { ...limits, context: 0 }).rows ?? [] + expect(rows.filter(row => row[0] === 'add').map(row => row[2])).toEqual(['# two', 'type: two', 'seq: 2', 'a: 1', 'b: 2', 'c: 3']) + expect(rows.filter(row => row[0] === 'change')).toEqual([['change', 9, 15, [[false, 'seq: '], [true, '2']], [[false, 'seq: '], [true, '3']]]]) + }) + + it('reports omitted rows past the limit and no rows when the diff times out', () => { + const many = Array.from({ length: 10 }, (_, index) => `line ${String(index)}`) + expect(sideBySide([], text(...many), { ...limits, rows: 4 })).toMatchObject({ added: 10, removed: 0, omitted: 6 }) + expect(sideBySide([], text(...many), { ...limits, rows: 4 }).rows).toHaveLength(4) + // The skip row holds lines 1–7 and costs 8, so only it and the next row fit in 9. + const collapsed = sideBySide(text('1', '2', '3', '4', '5', '6', '7', '8', 'old', '9'), text('1', '2', '3', '4', '5', '6', '7', '8', 'new', '9'), { ...limits, rows: 9 }) + expect([collapsed.rows?.map(row => row[0]), collapsed.omitted]).toEqual([['skip', 'same'], 2]) + const big = Array.from({ length: 4000 }, (_, index) => String(index)) + expect(sideBySide(text(...big), text(...big.toReversed()), { ...limits, timeoutMs: 0 })) + .toEqual({ added: 4000, removed: 4000, omitted: 0 }) }) it('opens on the catalog, then groups scenarios and other files by directory in code point order', () => { const catalog = ['snapshots/sdk/text-turn/system-prompt.expected.md', 'snapshots/session/text-turn/tool-schemas.expected.json'] const paths = ['snapshots/web/b/x', 'snapshots/sdk/sdk.snapshot.ts', 'snapshots/AGENTS.md', 'snapshots/web/B/x', 'snapshots/web/b/a/y'] - expect(groupDiffs([...paths, 'snapshots/web/b/x'].map(path => file(path)), catalog)).toEqual([ + expect(groupDiffs(paths, catalog)).toEqual([ { title: 'SDK blank Session catalog', paths: catalog }, { title: 'snapshots', paths: ['snapshots/AGENTS.md'] }, { title: 'sdk', paths: ['snapshots/sdk/sdk.snapshot.ts'] }, @@ -73,8 +87,8 @@ describe('snapshot diff page', () => { ['owns both sidecars', 'profile: sdk\n', 'sdk/text-turn', 'sdk/text-turn'], ['names a source in its own suite directory', 'profile: sdk-minimal\nheader:\n toolSchemasSource: other\n', 'sdk/text-turn', 'sdk/other'], ['names sources in another suite', 'profile: sdk\nheader:\n systemPromptSource: session/a\n toolSchemasSource: session/b\n', 'session/a', 'session/b'], - ])('resolves catalog sidecars when the manifest %s', (_, text, prompt, schemas) => { - expect(catalogFiles(text)).toEqual({ + ])('resolves catalog sidecars when the manifest %s', (_, manifest, prompt, schemas) => { + expect(catalogFiles(manifest)).toEqual({ systemPrompt: `snapshots/${prompt}/system-prompt.expected.md`, toolSchemas: `snapshots/${schemas}/tool-schemas.expected.json`, }) @@ -83,16 +97,17 @@ describe('snapshot diff page', () => { it.each([ ['a non-string source', 'profile: sdk\nheader:\n toolSchemasSource: 1\n', 'header.toolSchemasSource must be a non-empty string'], ['an empty source', 'header:\n systemPromptSource: ""\n', 'header.systemPromptSource must be a non-empty string'], - ])('rejects %s', (_, text, message) => { - expect(() => catalogFiles(text)).toThrow(message) + ])('rejects %s', (_, manifest, message) => { + expect(() => catalogFiles(manifest)).toThrow(message) }) - it('embeds the data so diff text cannot close the script element', () => { - const files = [file('p', '+')] - const page = renderSnapshotDiffPage(files, [{ title: 't', paths: ['p'] }], 'a → b') + it('embeds the index so a path cannot close the script element', () => { + const files = [{ path: 'p', added: 1, removed: 0 }] + const groups = [{ title: 't', paths: ['p'] }] + const page = renderSnapshotDiffPage(files, groups, 'a → b') expect(page).not.toContain('') const data = /