4f85ebe472
- Added `CodeFrameMarker` and `formatCodeFrameLine()` to centralize code-frame gutter formatting. - Extended diff rendering to preserve `|` and `│` separators, aligning gutter markers and line numbers. - Reworked AST, grep, hashline, Vim, and diff renderers to use shared line formatting with computed `lineNumberWidth`. - Updated atom editing flow and tests, including `resolveAtomEntryPaths` migration and new loc-based/edge-case coverage. - Adjusted benchmark runner early-stop configuration by passing `buildEarlyStop` through prompt collection.
169 lines
5.4 KiB
TypeScript
169 lines
5.4 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import * as fs from "node:fs/promises";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { ToolExecutionComponent } from "@oh-my-pi/pi-coding-agent/modes/components/tool-execution";
|
|
import * as themeModule from "@oh-my-pi/pi-coding-agent/modes/theme/theme";
|
|
import { toolRenderers } from "@oh-my-pi/pi-coding-agent/tools/renderers";
|
|
import type { TUI } from "@oh-my-pi/pi-tui";
|
|
|
|
async function getUiTheme() {
|
|
await themeModule.initTheme(false, undefined, undefined, "dark", "light");
|
|
const theme = await themeModule.getThemeByName("dark");
|
|
expect(theme).toBeDefined();
|
|
return theme!;
|
|
}
|
|
|
|
describe("apply_patch rendering", () => {
|
|
it("registers apply_patch to use the edit renderer", () => {
|
|
expect(toolRenderers.apply_patch).toBe(toolRenderers.edit);
|
|
});
|
|
|
|
it("renders apply_patch results through edit UI instead of generic fallback", async () => {
|
|
await getUiTheme();
|
|
const uiStub = { requestRender() {} } as unknown as TUI;
|
|
|
|
const component = new ToolExecutionComponent(
|
|
"apply_patch",
|
|
{
|
|
input: "*** Begin Patch\n*** Update File: src/demo.ts\n@@\n-old\n+new\n*** End Patch",
|
|
},
|
|
{},
|
|
undefined,
|
|
uiStub,
|
|
);
|
|
|
|
component.updateResult(
|
|
{
|
|
content: [{ type: "text", text: "" }],
|
|
details: {
|
|
path: "src/demo.ts",
|
|
op: "update",
|
|
diff: "@@\n-old\n+new",
|
|
},
|
|
},
|
|
false,
|
|
);
|
|
|
|
const rendered = Bun.stripANSI(component.render(140).join("\n"));
|
|
expect(rendered).toContain("src/demo.ts");
|
|
expect(rendered).toContain("+new");
|
|
expect(rendered).not.toContain("(no output)");
|
|
});
|
|
|
|
it("derives call path, operation, and file-count hints from apply_patch input", async () => {
|
|
const uiTheme = await getUiTheme();
|
|
const input = [
|
|
"*** Begin Patch",
|
|
"*** Update File: src/first.ts",
|
|
"@@",
|
|
"-before",
|
|
"+after",
|
|
"*** Add File: src/new.ts",
|
|
"+hello",
|
|
"*** End Patch",
|
|
].join("\n");
|
|
|
|
const component = toolRenderers.apply_patch.renderCall({ input }, { expanded: false, isPartial: true }, uiTheme);
|
|
const rendered = Bun.stripANSI(component.render(160).join("\n"));
|
|
|
|
expect(rendered).toContain("src/first.ts");
|
|
expect(rendered).toContain("Edit");
|
|
expect(rendered).toContain("(+1 more)");
|
|
});
|
|
|
|
it("does not show missing end-marker errors while apply_patch input is streaming", async () => {
|
|
const uiTheme = await getUiTheme();
|
|
const input = ["*** Begin Patch", "*** Update File: src/streaming.ts", "@@", "-before", "+after"].join("\n");
|
|
|
|
const component = toolRenderers.apply_patch.renderCall({ input }, { expanded: false, isPartial: true }, uiTheme);
|
|
const rendered = Bun.stripANSI(component.render(160).join("\n"));
|
|
|
|
expect(rendered).toContain("src/streaming.ts");
|
|
expect(rendered).not.toContain("The last line of the patch must be");
|
|
});
|
|
|
|
it("shows an apply_patch parse error preview for malformed input", async () => {
|
|
const uiTheme = await getUiTheme();
|
|
const malformedInput = ["*** Begin Patch", "*** Update File: src/bad.ts", "*** End Patch"].join("\n");
|
|
|
|
const component = toolRenderers.apply_patch.renderCall(
|
|
{ input: malformedInput },
|
|
{ expanded: false, isPartial: true },
|
|
uiTheme,
|
|
);
|
|
const rendered = Bun.stripANSI(component.render(160).join("\n"));
|
|
|
|
expect(rendered).toContain("src/bad.ts");
|
|
expect(rendered).toContain("is empty");
|
|
});
|
|
|
|
it("shows apply_patch preview diffs after args complete", async () => {
|
|
await getUiTheme();
|
|
const uiStub = { requestRender() {} } as unknown as TUI;
|
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "apply-patch-preview-"));
|
|
try {
|
|
await Bun.write(path.join(tmpDir, "preview.ts"), "const value = 1;\n");
|
|
const input = [
|
|
"*** Begin Patch",
|
|
"*** Update File: preview.ts",
|
|
"@@",
|
|
"-const value = 1;",
|
|
"+const value = 2;",
|
|
"*** End Patch",
|
|
].join("\n");
|
|
|
|
const component = new ToolExecutionComponent("apply_patch", { input }, {}, undefined, uiStub, tmpDir);
|
|
const before = Bun.stripANSI(component.render(160).join("\n"));
|
|
expect(before).not.toContain("(preview)");
|
|
|
|
component.setArgsComplete();
|
|
await Bun.sleep(50);
|
|
|
|
const after = Bun.stripANSI(component.render(160).join("\n"));
|
|
expect(after).toContain("(preview)");
|
|
expect(after).toContain("const value = 2;");
|
|
} finally {
|
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("aligns rendered edit diff separators", async () => {
|
|
await getUiTheme();
|
|
const uiStub = { requestRender() {} } as unknown as TUI;
|
|
const component = new ToolExecutionComponent(
|
|
"edit",
|
|
{ path: "packages/coding-agent/src/tools/image-gen.ts" },
|
|
{},
|
|
undefined,
|
|
uiStub,
|
|
);
|
|
|
|
component.updateResult(
|
|
{
|
|
content: [{ type: "text", text: "" }],
|
|
details: {
|
|
path: "packages/coding-agent/src/tools/image-gen.ts",
|
|
op: "update",
|
|
diff: [
|
|
" 10|}",
|
|
'+11|import { CODEX_INSTRUCTIONS } from "@oh-my-pi/pi-ai/providers/openai-codex-responses";',
|
|
" 12|\t$env,",
|
|
" 228|\toutput_format: typeof OPENAI_IMAGE_OUTPUT_FORMAT;",
|
|
"+235|\tinstructions?: string;",
|
|
' 234|\tinput: Array<{ role: "user"; content: OpenAIInputContent[] }>;',
|
|
].join("\n"),
|
|
},
|
|
},
|
|
false,
|
|
);
|
|
|
|
const rendered = Bun.stripANSI(component.render(220).join("\n"));
|
|
expect(rendered).toContain(" 10│}");
|
|
expect(rendered).toContain(" +11│import");
|
|
expect(rendered).toContain(" 228│");
|
|
expect(rendered).toContain("+235│");
|
|
expect(rendered).toContain(" 234│");
|
|
});
|
|
});
|