repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/ctx/__tests__/harness.test.ts
RawFile outline
- t line 5
- ctxPath line 7
- textOf line 11
- text line 13
- emitRead line 17
- beforeEach line 32
- afterEach line 36
- ctx harness integration line 41
- loads upstream pi-test-harness from npm line 42
- registers no tools and no commands line 47
- compacts huge read output before it reaches the transcript line 54
- does not compact bounded read windows line 69
- does not compact read errors line 78
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createTestSession, type TestSession } from "../../../test/harness";
let t: TestSession | undefined;
function ctxPath(): string {
return path.resolve(import.meta.dirname, "../index.ts");
}
function textOf(result: any): string {
return result.content
.map((part: { text?: string }) => part.text ?? "")
.join("\n");
}
async function emitRead(params: {
text: string;
input?: Record<string, unknown>;
isError?: boolean;
}) {
return await t?.session.extensionRunner.emitToolResult({
type: "tool_result",
toolName: "read",
input: params.input ?? { path: "big.txt" },
content: [{ type: "text", text: params.text }],
details: {},
isError: params.isError ?? false,
});
}
beforeEach(async () => {
t = await createTestSession({ extensions: [ctxPath()] });
});
afterEach(() => {
t?.dispose();
t = undefined;
});
describe("ctx harness integration", () => {
it("loads upstream pi-test-harness from npm", async () => {
const pkg = await import("@marcfargas/pi-test-harness");
expect(typeof pkg.createTestSession).toBe("function");
});
it("registers no tools and no commands", () => {
const [extension] = t?.session.extensionRunner.extensions ?? [];
expect(extension).toBeDefined();
expect([...(extension?.tools.keys() ?? [])]).toEqual([]);
expect([...(extension?.commands.keys() ?? [])]).toEqual([]);
});
it("compacts huge read output before it reaches the transcript", async () => {
const raw = Array.from(
{ length: 600 },
(_, i) => `line ${i + 1} ${"x".repeat(80)}`,
).join("\n");
const patched = await emitRead({ text: raw });
const text = textOf(patched);
expect(text).toContain("line 1");
expect(text).toContain("line 600");
expect(text).toContain("[ctx compacted read:");
expect(text).toContain("Use grep or read offset/limit");
expect(text).not.toContain("line 200");
expect(patched.details.ctxCompactedRead).toBe(true);
});
it("does not compact bounded read windows", async () => {
const raw = "x".repeat(25_000);
const patched = await emitRead({
text: raw,
input: { path: "big.txt", offset: 10, limit: 5 },
});
expect(patched).toBeUndefined();
});
it("does not compact read errors", async () => {
const raw = "x".repeat(25_000);
const patched = await emitRead({ text: raw, isError: true });
expect(patched).toBeUndefined();
});
});