repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/ctx/__tests__/harness.test.ts
RawFile outline
- t line 8
- ctxPath line 10
- textOf line 14
- text line 16
- emitRead line 20
- beforeEach line 35
- afterEach line 39
- ctx harness integration line 44
- loads upstream pi-test-harness from npm line 45
- registers no tools and no commands line 50
- compacts huge read output before it reaches the transcript line 56
- does not compact bounded read windows line 71
- does not compact read errors line 80
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import path from "node:path";
import {
createTestSession,
type TestSession,
} from "@marcfargas/pi-test-harness";
let t: TestSession | undefined;
function ctxPath(): string {
return path.resolve(import.meta.dir, "../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.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();
});
});