import assert from 'node:assert/strict' import { renderToStaticMarkup } from 'react-dom/server' import { ArtifactContentRenderer } from '../src/renderer/src/workbuddy/artifacts/ArtifactContentRenderer' import { loadCustomArtifactContent, loadDiffContent } from '../src/renderer/src/workbuddy/artifacts/content-loader' import { selectArtifactView, isHtmlPreviewSizeOversized, HTML_PREVIEW_SIZE_LIMIT } from '../src/renderer/src/workbuddy/artifacts/routing' import { createWriteArtifact } from '../src/renderer/src/workbuddy/artifacts/tool-artifact' import { ToolCallRenderer } from '../src/renderer/src/workbuddy/conversation/ToolCallRenderer' import { createSourceWriteFileOpenRequest } from '../src/renderer/src/workbuddy/conversation/SourceWriteFileView' import { artifactImageSource } from '../src/renderer/src/workbuddy/artifacts/image-path' async function main(): Promise { assert.equal(artifactImageSource('../image.png', 'file:///C:/work/with%20space/notes/readme.md', 'C:/other'), 'file:///C:/work/with%20space/image.png') assert.equal(artifactImageSource('image.png', 'C:\\work\\notes\\readme.md', 'C:/other'), 'C:/work/notes/image.png') assert.equal(artifactImageSource('image.png', 'notes/readme.md', 'C:/work'), 'C:/work/notes/image.png') assert.equal(artifactImageSource('../image.png', 'https://example.com/notes/readme.md', 'C:/work'), 'https://example.com/image.png') assert.equal(artifactImageSource('data:image/png;base64,AA==', 'notes/readme.md', 'C:/work'), 'data:image/png;base64,AA==') const view = (type: string, extra = {}) => selectArtifactView({ id: 'a', type, ...extra }) assert.equal(view('implementation-plan'), 'plan') assert.equal(view('overview', { contentType: 'code', content: 'x' }), 'overview') assert.equal(view('task', { contentType: 'markdown', content: 'x' }), 'empty') assert.equal(view('custom-artifact', { category: 'agents', customType: 'wb/app' }), 'agent-member') assert.equal(view('custom-artifact', { customType: 'mcp/app', content: 'x' }), 'extension') assert.equal(view('custom-artifact', { customType: 'other', contentType: 'code' }), 'custom') assert.equal(view('media-artifact', { category: 'artifact', url: 'agent://files/page.HTM', contentType: 'code' }), 'html') assert.equal(view('media-artifact', { category: 'other', url: 'page.html', contentType: 'code' }), 'media') assert.equal(view('file-changes', { fileChangeInfo: { uri: 'a.ts' } }), 'file-diff') assert.equal(view('other', { contentType: 'diff', originalContent: '', modifiedContent: 'new' }), 'empty', 'retain the captured direct-diff truthiness gate') assert.equal(isHtmlPreviewSizeOversized(HTML_PREVIEW_SIZE_LIMIT - 1), false) assert.equal(isHtmlPreviewSizeOversized(HTML_PREVIEW_SIZE_LIMIT), true) assert.equal(isHtmlPreviewSizeOversized(NaN), false) let reads = 0 const read = async (uri: string) => { reads++; assert.equal(uri, 'a.md'); return '# live' } assert.equal(await loadCustomArtifactContent('a.md', '# cached', read), '# cached') assert.equal(reads, 0) assert.equal(await loadCustomArtifactContent('a.md', '# cached', read, true), '# live') assert.equal(reads, 1) assert.equal(await loadCustomArtifactContent('a.md', '', async () => ''), '') await assert.rejects(loadCustomArtifactContent(undefined, '# cached', read), /No file URI/) await assert.rejects(loadCustomArtifactContent('a.md'), /No readFile/) const diff = '@@ -1 +1 @@\n-old\n+new' const cached = await loadDiffContent({ uri: 'a.md', diff, cachedContent: 'new' }, read) assert.equal(cached.original, 'old') assert.equal(cached.exactDiff, diff) assert.equal(reads, 1, 'cached full file must bypass host reads') const live = await loadDiffContent({ uri: 'a.md', diff }, async () => 'new') assert.deepEqual(live, cached) const fallback = await loadDiffContent({ uri: 'missing', diff }, async () => { throw Error('missing') }) assert.deepEqual(fallback, { original: 'old', modified: 'new', fallback: true }) for (const [before, after] of [['old', 'new'], ['', 'created\n'], ['removed\n', ''], ['a\r\nb\r\n', 'a\r\nB\r\n'], ['', ''], ['a\n\n', 'b\n\n']]) { const artifact = createWriteArtifact('write', 'a.txt', { oldContent: before, newContent: after })! const sides = await loadDiffContent(artifact.fileChangeInfo!) assert.equal(sides.original, before, 'complete pre-write content should survive the adapter') assert.equal(sides.modified, after, 'complete post-write content should survive the adapter') } assert.equal(createWriteArtifact('write', 'a.txt', { oldText: 'fragment', newText: 'fragment' }), undefined) const plan = renderToStaticMarkup( {content}} />) assert.match(plan, /artifact-content/) assert.match(plan, /plan-content/) assert.match(plan, /data-header="true"/) const code = renderToStaticMarkup( null} renderCode={(content, language) => {content}} />) assert.match(code, /data-language="typescript"/) const written = renderToStaticMarkup( undefined} tool={{ id: 'write', name: 'edit_file', status: 'success', args: { path: 'actual.ts', oldText: 'fragment', newText: 'replacement' }, result: { result: { path: 'actual.ts', oldContent: 'full before', newContent: 'full after' } } }} />) assert.match(written, /actual.ts/) assert.match(written, /role="link"/) assert.match(written, /full before/) assert.match(written, /full after/) const writeRequest = createSourceWriteFileOpenRequest({ id: 'write', name: 'edit_file', status: 'success', args: { path: 'actual.ts', oldText: 'fragment', newText: 'replacement' }, result: { result: { path: 'actual.ts', oldContent: 'full before', newContent: 'full after' } } }) assert.equal(writeRequest?.content, 'full after') assert.ok(writeRequest?.artifact?.fileChangeInfo) const openedWriteSides = await loadDiffContent(writeRequest.artifact.fileChangeInfo) assert.equal(openedWriteSides.original, 'full before') assert.equal(openedWriteSides.modified, 'full after') console.log('Artifact routing, content loading, full write projection and renderer regressions passed') } void main().catch((error) => { console.error(error); process.exitCode = 1 })