import assert from "node:assert/strict"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test from "node:test"; import { createCodemodeExtension, type CodemodeToolDetails, type ToolRendererResolver, type ToolRenderers, } from "@selesai/code"; import { registerToolDisplayOverrides, } from "../src/tool-overrides.ts"; import { DEFAULT_TOOL_DISPLAY_CONFIG, type ToolDisplayConfig } from "../src/types.ts"; import { createExtensionApiHarness, createTestTheme, renderComponentText, runEvent, } from "./tool-renderer-harness.ts"; type NestedCall = CodemodeToolDetails["calls"][number]; const THEME = createTestTheme() as never; function buildConfig(overrides: Partial = {}): ToolDisplayConfig { return { ...DEFAULT_TOOL_DISPLAY_CONFIG, ...overrides, boxedToolCalls: overrides.boxedToolCalls ?? false, registerToolOverrides: { ...DEFAULT_TOOL_DISPLAY_CONFIG.registerToolOverrides, ...(overrides.registerToolOverrides ?? {}), }, customToolOverrides: overrides.customToolOverrides ?? {}, }; } function createHostCodemodeRenderers(): ToolRenderers { let renderers: ToolRenderers | undefined; const factory = createCodemodeExtension({ models: false }); factory({ registerTool(tool: ToolRenderers): void { renderers = tool; }, getSettings: () => ({}), getAllTools: () => [], appendEntry(): void {}, } as never); assert.ok(renderers, "codemode extension registered its definition"); return renderers; } const HOST_CODEMODE = createHostCodemodeRenderers(); function createDisplayResolver(config: ToolDisplayConfig): ToolRendererResolver { const harness = createExtensionApiHarness(); registerToolDisplayOverrides(harness.api, () => config); assert.equal(harness.resolvers.length, 1, "pi-tool-display registers one resolver"); assert.equal(harness.registeredTools.length, 0, "pi-tool-display registers no tools"); return harness.resolvers[0]!; } function createHarness(config: ToolDisplayConfig = buildConfig()) { const resolver = createDisplayResolver(config); const codemode = resolver("codemode", () => HOST_CODEMODE); assert.ok(codemode, "codemode has renderers"); return { config, resolver, codemode }; } function renderCall( renderers: ToolRenderers, args: unknown, context: Record = {}, ): string { assert.ok(renderers.renderCall); return renderComponentText(renderers.renderCall(args, THEME, context as never), 120); } function renderResult( renderers: ToolRenderers, result: { content: Array<{ type: string; text?: string }>; details?: unknown; isError?: boolean }, options: { expanded?: boolean; isPartial?: boolean } = {}, context: Record = {}, ): string { assert.ok(renderers.renderResult); return renderComponentText( renderers.renderResult( result as never, { expanded: options.expanded ?? false, isPartial: options.isPartial ?? false }, THEME, context as never, ), 120, ); } function call(overrides: Partial & Pick): NestedCall { return { id: `call-${overrides.name}-${Math.random().toString(16).slice(2)}`, args: "{}", status: "ok", durationMs: 12, ...overrides, } as NestedCall; } function scriptContent(output = "script output"): Array<{ type: "text"; text: string }> { return [ { type: "text", text: "Script completed\nWall time 0.1 seconds\nOutput:\n" }, { type: "text", text: output }, ]; } function readPreview(text: string, extra: Partial> = {}) { return { text, textLines: text ? text.split("\n").length : 0, textBytes: Buffer.byteLength(text, "utf8"), truncated: false, images: 0, isError: false, ...extra, } as NonNullable; } // ─── Every recorded call, collapsed and expanded ──────────────────────────── test("every nested call is listed collapsed and expanded, with no last-8 cap", () => { const { codemode } = createHarness(); const calls = Array.from({ length: 12 }, (_, index) => call({ name: "read", args: JSON.stringify({ path: `src/file-${index}.ts` }) }), ); const details = { calls } as CodemodeToolDetails; for (const expanded of [false, true]) { const text = renderResult(codemode, { content: scriptContent(), details }, { expanded }); for (const entry of calls) { assert.ok(text.includes(JSON.parse(entry.args).path), `missing ${entry.args} when expanded=${expanded}\n${text}`); } } }); test("nested calls use the direct per-tool call formatters", () => { const { codemode } = createHarness(); const calls = [ call({ name: "read", args: JSON.stringify({ path: "src/a.ts" }) }), call({ name: "bash", args: JSON.stringify({ command: "echo hi" }) }), call({ name: "powershell", args: JSON.stringify({ command: "Get-ChildItem" }) }), call({ name: "edit", args: JSON.stringify({ path: "src/a.ts", oldText: "old", newText: "new" }) }), call({ name: "write", args: JSON.stringify({ path: "src/a.ts", content: "one\ntwo" }) }), call({ name: "grep", args: JSON.stringify({ pattern: "TODO", path: "src" }) }), call({ name: "find", args: JSON.stringify({ pattern: "*.ts", path: "src" }) }), call({ name: "ls", args: JSON.stringify({ path: "src" }) }), call({ name: "mcp__docs__search", args: JSON.stringify({ query: "hooks" }) }), ]; const text = renderResult(codemode, { content: scriptContent(), details: { calls } }); assert.match(text, /read src\/a\.ts/); assert.match(text, /\$ echo hi/); assert.match(text, /\$ Get-ChildItem/); assert.match(text, /edit src\/a\.ts/); assert.match(text, /write src\/a\.ts/); assert.match(text, /grep \/TODO\/ in src/); assert.match(text, /find \*\.ts in src/); assert.match(text, /ls src/); assert.match(text, /MCP docs\/search \(1 arg\)/); }); test("unknown tools and invalid or truncated args fall back to the saved raw text", () => { const { codemode } = createHarness(); const calls = [ call({ name: "mystery_tool", args: '{"alpha":1}' }), call({ name: "read", args: "{not json" }), call({ name: "write", args: '{"path":"src/big.ts","content":"unterminated', argsBytes: 24 * 1024, argsTruncated: true, }), ]; const collapsed = renderResult(codemode, { content: scriptContent(), details: { calls } }); assert.match(collapsed, /mystery_tool \{"alpha":1\}/); assert.match(collapsed, /read \{not json/); assert.match(collapsed, /write \{"path":"src\/big\.ts","content":"unterminated/); const expanded = renderResult(codemode, { content: scriptContent(), details: { calls } }, { expanded: true }); assert.match(expanded, /args truncated: saved 44B of 24\.0KB/); }); test("expanded calls show the full saved args instead of an 80-character summary", () => { const { codemode } = createHarness(); const longValue = "x".repeat(400); const entry = call({ name: "write", args: JSON.stringify({ path: "src/long.ts", content: longValue }) }); const details = { calls: [entry] } as CodemodeToolDetails; const collapsed = renderResult(codemode, { content: scriptContent(), details }); assert.equal(collapsed.includes(longValue), false, "collapsed call line does not dump the args"); const expanded = renderResult(codemode, { content: scriptContent(), details }, { expanded: true }); assert.ok( expanded.replace(/\s+/g, "").includes(longValue), "expanded args keep every saved character (normal terminal wrapping is allowed)", ); assert.match(expanded, /"path": "src\/long\.ts"/); }); test("expanded args shows the truncation marker when the saved string is empty", () => { const { codemode } = createHarness(); const entry = call({ name: "read", args: "", argsTruncated: true, argsBytes: 180_000, }); const text = renderResult(codemode, { content: scriptContent(), details: { calls: [entry] } }, { expanded: true }); assert.match(text, /args truncated: saved 0B of 175\.8KB/); }); // ─── Nested results: same renderer and output mode as direct calls ───────── function directResult( resolver: ToolRendererResolver, toolName: string, result: { content: Array<{ type: string; text?: string }>; details?: unknown; isError?: boolean }, options: { expanded?: boolean }, args: unknown, config: ToolDisplayConfig, ): string { const renderers = resolver(toolName, () => undefined); assert.ok(renderers?.renderResult, `direct ${toolName} renderer`); return renderResult(renderers, result, options, { args, cwd: process.cwd(), toolCallId: `direct-${toolName}`, state: {}, isError: result.isError ?? false, outputPad: 1, }); } test("nested read results use readOutputMode through the direct read renderer", () => { for (const mode of ["summary", "preview", "hidden"] as const) { const config = buildConfig({ readOutputMode: mode, previewLines: 2 }); const { resolver, codemode } = createHarness(config); const args = { path: "src/a.ts" }; const result = { content: [{ type: "text" as const, text: "one\ntwo\nthree\n" }], details: { truncation: { truncated: false, totalLines: 3 } }, isError: false, }; const entry = call({ name: "read", args: JSON.stringify(args), result: readPreview(result.content[0]!.text, { textLines: 3, details: result.details, }), }); const details = { calls: [entry] } as CodemodeToolDetails; for (const expanded of [false, true]) { const direct = directResult(resolver, "read", result, { expanded }, args, config); const nested = renderResult(codemode, { content: scriptContent(), details }, { expanded }); if (direct) { assert.ok(nested.includes(direct), `read ${mode} expanded=${expanded}\nexpected:\n${direct}\nnested:\n${nested}`); } else if (mode === "hidden" && expanded) { // Hidden output draws nothing collapsed; expanding the row reveals the bounded saved preview. assert.match(nested, /saved result preview:/); assert.ok(nested.includes("one")); } else { assert.equal(nested.includes("one"), false, `read ${mode} expanded=${expanded} stays hidden\n${nested}`); } } } }); test("nested search results use searchOutputMode through the direct search renderers", () => { for (const mode of ["count", "preview", "hidden"] as const) { const config = buildConfig({ searchOutputMode: mode, previewLines: 2 }); const { resolver, codemode } = createHarness(config); const args = { pattern: "TODO", path: "src" }; const result = { content: [{ type: "text" as const, text: "match one\nmatch two\n" }], details: {}, isError: false, }; const entry = call({ name: "grep", args: JSON.stringify(args), result: readPreview(result.content[0]!.text, { textLines: 2 }), }); const details = { calls: [entry] } as CodemodeToolDetails; for (const expanded of [false, true]) { const direct = directResult(resolver, "grep", result, { expanded }, args, config); const nested = renderResult(codemode, { content: scriptContent(), details }, { expanded }); if (direct) { assert.ok(nested.includes(direct), `grep ${mode} expanded=${expanded}\nexpected:\n${direct}\nnested:\n${nested}`); } else if (expanded) { assert.match(nested, /saved result preview:/); assert.ok(nested.includes("match one")); } else { assert.equal(nested.includes("match one"), false, `grep ${mode} collapsed stays hidden\n${nested}`); } } } }); test("nested bash results use bashOutputMode through the direct shell renderer", () => { for (const mode of ["summary", "preview", "opencode"] as const) { const config = buildConfig({ bashOutputMode: mode, previewLines: 2, bashCollapsedLines: 1 }); const { resolver, codemode } = createHarness(config); const args = { command: "printf 'a\\nb\\n'" }; const result = { content: [{ type: "text" as const, text: "a\nb\n" }], details: {}, isError: false, }; const entry = call({ name: "bash", args: JSON.stringify(args), result: readPreview("a\nb", { textLines: 2 }), }); const details = { calls: [entry] } as CodemodeToolDetails; for (const expanded of [false, true]) { const direct = directResult(resolver, "bash", result, { expanded }, args, config); const nested = renderResult(codemode, { content: scriptContent(), details }, { expanded }); assert.ok(nested.includes(direct), `bash ${mode} expanded=${expanded}\nexpected:\n${direct}\nnested:\n${nested}`); } } }); test("nested powershell results use the direct shell renderer", () => { const config = buildConfig({ bashOutputMode: "preview", previewLines: 2 }); const { resolver, codemode } = createHarness(config); const args = { command: "Get-ChildItem" }; const result = { content: [{ type: "text" as const, text: "one\ntwo\n" }], details: {}, isError: false, }; const entry = call({ name: "powershell", args: JSON.stringify(args), result: readPreview("one\ntwo", { textLines: 2 }), }); const details = { calls: [entry] } as CodemodeToolDetails; const direct = directResult(resolver, "powershell", result, { expanded: true }, args, config); const nested = renderResult(codemode, { content: scriptContent(), details }, { expanded: true }); assert.ok(nested.includes(direct), `powershell result differs\nexpected:\n${direct}\nnested:\n${nested}`); }); test("nested edit and write results use the direct diff renderer and diff settings", () => { const config = buildConfig({ diffViewMode: "unified", diffIndicatorMode: "classic" }); const { resolver, codemode } = createHarness(config); const editArgs = { path: "src/a.ts", oldText: "old line", newText: "new line" }; const editDetails = { diff: "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1 +1 @@\n-old line\n+new line\n", patch: "", }; const editEntry = call({ name: "edit", args: JSON.stringify(editArgs), result: readPreview("Edited src/a.ts", { details: editDetails }), }); const writeArgs = { path: "src/new.ts", content: "hello\nworld\n" }; const writeDetails = { toolDisplay: { fileExistedBeforeWrite: true, previousContent: "before\n" } }; const writeEntry = call({ name: "write", args: JSON.stringify(writeArgs), result: readPreview("Wrote src/new.ts", { details: writeDetails }), }); const details = { calls: [editEntry, writeEntry] } as CodemodeToolDetails; const nested = renderResult(codemode, { content: scriptContent(), details }, { expanded: true }); const directEdit = directResult( resolver, "edit", { content: [{ type: "text", text: "Edited src/a.ts" }], details: editDetails, isError: false }, { expanded: true }, editArgs, config, ); const directWrite = directResult( resolver, "write", { content: [{ type: "text", text: "Wrote src/new.ts" }], details: writeDetails, isError: false }, { expanded: true }, writeArgs, config, ); assert.ok(nested.includes(directEdit), `nested edit diff differs\nexpected:\n${directEdit}\nnested:\n${nested}`); assert.ok(nested.includes(directWrite), `nested write diff differs\nexpected:\n${directWrite}\nnested:\n${nested}`); }); test("nested MCP and custom-tool results use mcpOutputMode/customToolOverrides", () => { const config = buildConfig({ mcpOutputMode: "summary", customToolOverrides: { my_tool: { enabled: true, kind: "generic", outputMode: "summary" } }, }); const { resolver, codemode } = createHarness(config); const resultText = "alpha\nbeta\n"; const mcpArgs = { query: "hooks" }; const mcpEntry = call({ name: "mcp__docs__search", args: JSON.stringify(mcpArgs), result: readPreview(resultText, { textLines: 2 }), }); const customArgs = { query: "x" }; const customEntry = call({ name: "my_tool", args: JSON.stringify(customArgs), result: readPreview(resultText, { textLines: 2 }), }); const details = { calls: [mcpEntry, customEntry] } as CodemodeToolDetails; const nested = renderResult(codemode, { content: scriptContent(), details }); for (const [toolName, args] of [["mcp__docs__search", mcpArgs], ["my_tool", customArgs]] as const) { const direct = directResult( resolver, toolName, { content: [{ type: "text", text: resultText }], details: {}, isError: false }, { expanded: false }, args, config, ); assert.ok(nested.includes(direct), `${toolName} differs\nexpected:\n${direct}\nnested:\n${nested}`); } }); test("a disabled built-in falls back to the raw nested line instead of its formatter", () => { const config = buildConfig({ registerToolOverrides: { read: false } as ToolDisplayConfig["registerToolOverrides"], }); const { codemode } = createHarness(config); const entry = call({ name: "read", args: JSON.stringify({ path: "src/a.ts" }) }); const text = renderResult(codemode, { content: scriptContent(), details: { calls: [entry] } }); assert.match(text, /read \{"path":"src\/a\.ts"\}/); assert.equal(text.includes("read src/a.ts"), false); }); // ─── Saved bounds, omissions, old sessions, errors ───────────────────────── test("script-budget, truncated previews, omitted details and images render explicit markers", () => { const { codemode } = createHarness(); const calls = [ call({ id: "budget", name: "read", args: JSON.stringify({ path: "src/a.ts" }), result: { omitted: true, reason: "script-budget" }, }), call({ id: "truncated", name: "bash", args: JSON.stringify({ command: "yes" }), result: readPreview("line-1\nline-2", { textLines: 4000, textBytes: 80_000, truncated: true, images: 2, details: { omitted: true, bytes: 52_000 }, }), }), ]; const text = renderResult(codemode, { content: scriptContent(), details: { calls } }, { expanded: true }); assert.match(text, /result omitted \(script budget\)/); assert.match(text, /preview truncated: showing 2\/4000 lines, 78\.1KB full/); assert.match(text, /2 images not saved/); assert.match(text, /details omitted \(50\.8KB\)/); }); test("old details without result still list calls and script output", () => { const { codemode } = createHarness(); const details = { calls: [ { id: "old-1", name: "read", args: '{"path":"src/a.ts"}', status: "ok", durationMs: 5 }, ], } as CodemodeToolDetails; const text = renderResult(codemode, { content: scriptContent("old output"), details }, { expanded: true }); assert.match(text, /read src\/a\.ts/); assert.match(text, /old output/); assert.match(text, /"path": "src\/a\.ts"/); }); test("an error result stays visible even when its output mode is hidden", () => { const config = buildConfig({ readOutputMode: "hidden" }); const { codemode } = createHarness(config); const entry = call({ name: "read", args: JSON.stringify({ path: "missing.ts" }), status: "error", result: readPreview("ENOENT: no such file", { isError: true }), }); const text = renderResult(codemode, { content: scriptContent(), details: { calls: [entry] } }); assert.match(text, /✗ read missing\.ts/); assert.match(text, /ENOENT: no such file/); }); test("a write without previous content does not fabricate a full-overwrite diff", () => { const { codemode } = createHarness(); const entry = call({ name: "write", args: JSON.stringify({ path: "src/new.ts", content: "hello\n" }), result: readPreview("Wrote src/new.ts", { details: { omitted: true, bytes: 9000 }, }), }); const text = renderResult(codemode, { content: scriptContent(), details: { calls: [entry] } }, { expanded: true }); assert.match(text, /write diff unavailable: previous content was not saved/); assert.match(text, /Wrote src\/new\.ts/); assert.equal(text.includes("before"), false); }); test("empty or unrelated saved details do not fabricate a write diff", () => { const { codemode } = createHarness(); for (const details of [{}, { foo: "bar" }]) { const entry = call({ name: "write", args: JSON.stringify({ path: "src/new.ts", content: "hello\n" }), result: readPreview("Wrote src/new.ts", { details }), }); const text = renderResult( codemode, { content: scriptContent(), details: { calls: [entry] } }, { expanded: true }, ); assert.match(text, /write diff unavailable: previous content was not saved/, JSON.stringify(details)); assert.match(text, /Wrote src\/new\.ts/); assert.doesNotMatch(text, /↳ created/, `no fabricated write diff for ${JSON.stringify(details)}`); assert.equal(/^\s*\+\s*hello/m.test(text), false, `no additions diff for ${JSON.stringify(details)}`); } }); test("a valid new-file write record still renders the created diff", () => { const { codemode } = createHarness(); const entry = call({ name: "write", args: JSON.stringify({ path: "src/new.ts", content: "hello\n" }), result: readPreview("Wrote src/new.ts", { details: { toolDisplay: { fileExistedBeforeWrite: false } }, }), }); const text = renderResult(codemode, { content: scriptContent(), details: { calls: [entry] } }, { expanded: true }); assert.doesNotMatch(text, /write diff unavailable/); assert.match(text, /↳ created/); assert.match(text, /1 │ hello/); }); test("a valid previous-content-omitted write record still renders its omission notice", () => { const { codemode } = createHarness(); const entry = call({ name: "write", args: JSON.stringify({ path: "src/big.ts", content: "hello\n" }), result: readPreview("Wrote src/big.ts", { details: { toolDisplay: { fileExistedBeforeWrite: true, previousContentOmitted: true, previousBytes: 12_000, }, }, }), }); const text = renderResult(codemode, { content: scriptContent(), details: { calls: [entry] } }, { expanded: true }); assert.doesNotMatch(text, /write diff unavailable/); assert.match(text, /previous content omitted \(11\.7KB\); showing new content only/); assert.match(text, /hello/); }); test("live write capture supplies the diff when the saved details are omitted", async () => { const dir = mkdtempSync(join(tmpdir(), "codemode-write-")); try { writeFileSync(join(dir, "a.ts"), "before\n", "utf8"); const config = buildConfig(); const harness = createExtensionApiHarness(); registerToolDisplayOverrides(harness.api, () => config); const resolver = harness.resolvers[0]!; await runEvent( harness, "tool_call", { toolName: "write", toolCallId: "live-write", input: { path: "a.ts", content: "after\n" }, }, { cwd: dir }, ); const codemode = resolver("codemode", () => HOST_CODEMODE); assert.ok(codemode); const entry = call({ id: "live-write", name: "write", args: JSON.stringify({ path: "a.ts", content: "after\n" }), result: readPreview("Wrote a.ts", { details: { omitted: true, bytes: 9000 } }), }); const details = { calls: [entry] } as CodemodeToolDetails; const context = { state: {} }; for (let render = 0; render < 2; render++) { const text = renderResult(codemode, { content: scriptContent(), details }, { expanded: true }, context); assert.match(text, /before/, `live previous content render ${render}`); assert.match(text, /after/, `live new content render ${render}`); } } finally { rmSync(dir, { recursive: true, force: true }); } }); // ─── Script output, timer, cost and live updates ─────────────────────────── test("script output, full-output path, timer and model cost total match codemode", () => { const { codemode } = createHarness(); const state = {}; const context = { state, cwd: process.cwd(), executionStarted: true, durationMs: 1500 }; renderCall(codemode, { code: "const x = 1;" }, context); const calls = [ call({ name: "read", cost: 0.001, result: readPreview("a") }), call({ name: "bash", cost: 0.002, result: readPreview("b") }), ]; const details = { calls, fullOutputPath: "/tmp/codemode-full.txt" } as CodemodeToolDetails; const text = renderResult( codemode, { content: scriptContent("script output"), details }, {}, { ...context, state }, ); assert.equal(text.match(/script output/g)?.length, 1, "script output is not duplicated by the host renderer"); assert.match(text, /Full output: \/tmp\/codemode-full\.txt/); assert.match(text, /Took 1\.5s/); assert.match(text, /Model calls: \$0\.0030/); }); test("partial to final updates reuse state without duplicate frames or timers", () => { const { codemode } = createHarness(buildConfig({ readOutputMode: "preview" })); const originalSetInterval = globalThis.setInterval; const originalClearInterval = globalThis.clearInterval; const intervals: Array> = []; globalThis.setInterval = ((fn: (...args: unknown[]) => unknown, ms?: number) => { const id = originalSetInterval(fn as never, ms); intervals.push(id); return id; }) as typeof globalThis.setInterval; try { const state = {}; const context = { state, cwd: process.cwd(), executionStarted: true, durationMs: undefined }; renderCall(codemode, { code: "const x = 1;" }, context); const running = [ call({ id: "run-a", name: "read", args: '{"path":"a.ts"}', status: "running", durationMs: undefined }), call({ id: "run-b", name: "bash", args: '{"command":"sleep 1"}', status: "running", durationMs: undefined }), ]; const first = renderResult( codemode, { content: [], details: { calls: running } }, { isPartial: true }, { ...context, state, isPartial: true }, ); const second = renderResult( codemode, { content: [], details: { calls: running } }, { isPartial: true }, { ...context, state, isPartial: true }, ); assert.match(first, /read a\.ts/); assert.match(second, /read a\.ts/); assert.match(second, /\$ sleep 1/); assert.equal(intervals.length, 1, "one script timer across partial updates"); const final = renderResult( codemode, { content: scriptContent("done"), details: { calls: [ call({ id: "final-a", name: "read", args: '{"path":"a.ts"}', result: readPreview("A") }), call({ id: "final-b", name: "bash", args: '{"command":"echo b"}', result: readPreview("B") }), ], }, }, {}, { ...context, state, durationMs: 1500 }, ); assert.match(final, /✓/); assert.match(final, /A/); assert.match(final, /B/); assert.equal((final.match(/^✓/gm) ?? []).length, 2, "one summary row per final call"); } finally { for (const interval of intervals) originalClearInterval(interval); globalThis.setInterval = originalSetInterval; globalThis.clearInterval = originalClearInterval; } }); test("a running call keeps its id state when its result arrives", () => { const { codemode } = createHarness(buildConfig({ readOutputMode: "preview" })); const state = {}; const context = { state, cwd: process.cwd(), executionStarted: true }; const running = call({ id: "running-read", name: "read", args: '{"path":"a.ts"}', status: "running", durationMs: undefined, }); const partial = renderResult( codemode, { content: [], details: { calls: [running] } }, { isPartial: true }, { ...context, isPartial: true }, ); assert.match(partial, /… read a\.ts/); const final = renderResult( codemode, { content: scriptContent("done"), details: { calls: [{ ...running, status: "ok", durationMs: 7, result: readPreview("the result") }], }, }, {}, { ...context, durationMs: 7 }, ); assert.match(final, /✓ read a\.ts 7ms/); assert.match(final, /the result/); }); test("a nested codemode call never re-enters the codemode display", () => { const { codemode } = createHarness(); const entry = call({ name: "codemode", args: JSON.stringify({ code: "return 1" }), result: readPreview("nested script output"), }); const text = renderResult(codemode, { content: scriptContent(), details: { calls: [entry] } }); assert.match(text, /codemode \{"code":"return 1"\}/); assert.match(text, /nested script output/); }); // ─── Resolver gating and boxing ──────────────────────────────────────────── test("codemodeDisplay false and enabled false both keep the host renderer", () => { const host = { renderShell: "default" } as ToolRenderers; const disabled = createDisplayResolver(buildConfig({ codemodeDisplay: false })); assert.equal(disabled("codemode", () => host), host); const extensionOff = createDisplayResolver(buildConfig({ enabled: false })); assert.equal(extensionOff("codemode", () => host), host); }); test("codemode follows boxedToolCalls and outputPad", () => { const { codemode } = createHarness(buildConfig({ boxedToolCalls: true })); assert.equal(codemode.renderShell, "self"); const text = renderCall(codemode, { code: "const x = 1;" }, { outputPad: 3, cwd: process.cwd() }); assert.match(text.split("\n")[0]!, /^╭/); assert.ok(text.split("\n").some((line) => /^│ {3}codemode/.test(line)), text); }); test("rendering a codemode result does not mutate the model-visible content", () => { const { codemode } = createHarness(); const result = { content: scriptContent("immutable"), details: { calls: [call({ name: "read", args: '{"path":"a.ts"}', result: readPreview("A") })] }, }; const before = structuredClone(result); renderResult(codemode, result, { expanded: true }); assert.deepEqual(result, before); });