repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/web/__tests__/harness.test.ts
RawFile outline
- theme line 6
- renderLines line 16
- renderToolResult line 20
- web extension harness line 34
- t line 35
- extension line 36
- beforeAll line 38
- afterAll line 44
- registers web_fetch, web_map, web_search, and understand_image tools line 46
- activates its tools when a host session starts with a narrower set line 54
- keeps web_fetch preview hidden until tool output is expanded line 70
- shows web_fetch visible content on expand when no preview detail exists line 96
- keeps web_search result titles hidden until tool output is expanded line 125
- keeps understand_image analysis hidden until tool output is expanded line 155
import { initTheme } from "@earendil-works/pi-coding-agent";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { createTestSession, type TestSession } from "../../../test/harness";
import webExtension from "../index";
function theme() {
return {
fg: (_style: string, text: string) => text,
bg: (_style: string, text: string) => text,
bold: (text: string) => text,
italic: (text: string) => text,
strikethrough: (text: string) => text,
} as any;
}
function renderLines(component: { render(width: number): string[] }): string {
return component.render(120).join("\n");
}
function renderToolResult(
tool: { definition: { renderResult?: Function } },
result: unknown,
expanded: boolean,
): string {
const component = tool.definition.renderResult?.(
result,
{ expanded, isPartial: false },
theme(),
{},
);
return component ? renderLines(component) : "";
}
describe("web extension harness", () => {
let t: TestSession;
let extension: TestSession["session"]["extensionRunner"]["extensions"][number];
beforeAll(async () => {
initTheme("default", false);
t = await createTestSession({ extensionFactories: [webExtension] });
[extension] = t.session.extensionRunner.extensions;
});
afterAll(() => t.dispose());
it("registers web_fetch, web_map, web_search, and understand_image tools", async () => {
expect(extension).toBeDefined();
expect([...extension.tools.keys()]).toContain("web_fetch");
expect([...extension.tools.keys()]).toContain("web_map");
expect([...extension.tools.keys()]).toContain("web_search");
expect([...extension.tools.keys()]).toContain("understand_image");
});
it("activates its tools when a host session starts with a narrower set", async () => {
t.session.setActiveToolsByName(["read"]);
await extension.handlers.get("session_start")[0](
{ type: "session_start", reason: "startup" },
t.session.extensionRunner.createCommandContext(),
);
expect(t.session.getActiveToolNames()).toEqual([
"read",
"web_fetch",
"web_map",
"web_search",
"understand_image",
]);
});
it("keeps web_fetch preview hidden until tool output is expanded", async () => {
const tool = extension.tools.get("web_fetch");
const result = {
content: [{ type: "text", text: "full saved content" }],
details: {
kind: "html",
format: "markdown",
contentType: "text/html",
bodySize: 261_200,
chars: 10_000,
visibleChars: 2_270,
responseId: "40144e5a-0f06-4e47-b8d9-4a59855239d0",
preview: "# Quick Select\nVerbose page body",
},
};
const collapsed = renderToolResult(tool, result, false);
const expanded = renderToolResult(tool, result, true);
expect(collapsed).toContain("Agent sees:");
expect(collapsed).not.toContain("Quick Select");
expect(collapsed).not.toContain("Verbose page body");
expect(expanded).toContain("Quick Select");
expect(expanded).toContain("Verbose page body");
});
it("shows web_fetch visible content on expand when no preview detail exists", async () => {
const tool = extension.tools.get("web_fetch");
const result = {
content: [
{
type: "text",
text: "https://wezterm.org/quickselect.html\n\n# Quick Select Mode\nExpanded markdown body",
},
],
details: {
kind: "html",
format: "markdown",
contentType: "text/html",
bodySize: 261_200,
chars: 10_000,
visibleChars: 1_014,
responseId: "68a87f48-82c6-4182-b101-4eb07fd62118",
},
};
const collapsed = renderToolResult(tool, result, false);
const expanded = renderToolResult(tool, result, true);
expect(collapsed).toContain("to expand");
expect(collapsed).not.toContain("Quick Select Mode");
expect(expanded).toContain("Quick Select Mode");
expect(expanded).toContain("Expanded markdown body");
});
it("keeps web_search result titles hidden until tool output is expanded", async () => {
const tool = extension.tools.get("web_search");
const result = {
content: [
{
type: "text",
text: "Search: wezterm\n\n[1] Quick Select Mode\n https://wezterm.org/quickselect.html",
},
],
details: {
query: "wezterm",
source: "codex",
results: [
{
title: "Quick Select Mode",
url: "https://wezterm.org/quickselect.html",
},
],
},
};
const collapsed = renderToolResult(tool, result, false);
const expanded = renderToolResult(tool, result, true);
expect(collapsed).toContain("1 result");
expect(collapsed).toContain("codex");
expect(collapsed).not.toContain("Quick Select Mode");
expect(expanded).toContain("Quick Select Mode");
});
it("keeps understand_image analysis hidden until tool output is expanded", async () => {
const tool = extension.tools.get("understand_image");
const result = {
content: [
{
type: "text",
text: "The image shows a black cat on a blue terminal background.",
},
],
details: { prompt: "describe", image_source: "cat.png" },
};
const collapsed = renderToolResult(tool, result, false);
const expanded = renderToolResult(tool, result, true);
expect(collapsed).toContain("Image analyzed");
expect(collapsed).not.toContain("black cat");
expect(expanded).toContain("black cat");
});
});