repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/pulse/__tests__/harness.test.ts
RawFile outline
- t line 9
- afterEach line 11
- pulsePath line 16
- pulseCommand line 20
- ctx line 25
- configPath line 29
- writeConfig line 33
- notifyText line 38
- statusText line 42
- waitFor line 49
- pulse pi-test-harness integration line 61
- loads through real Pi extension runtime line 62
- runs init/list/status/pause/resume via real command context line 72
- handles add/add-dir/add-glob/remove completions with real runtime line 105
- starts cleanly on session_start without notification spam and reports status line 144
- marks workspace changes from real tool events and keeps force feedback line 163
- runs periodic idle checks from the timer line 192
import { afterEach, describe, expect, it } from "bun:test";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import {
createTestSession,
type TestSession,
} from "@marcfargas/pi-test-harness";
let t: TestSession | undefined;
afterEach(() => {
t?.dispose();
t = undefined;
});
function pulsePath(): string {
return path.resolve(import.meta.dir, "../index.ts");
}
function pulseCommand() {
const [extension] = t!.session.extensionRunner.extensions;
return extension.commands.get("pulse");
}
function ctx(): any {
return t!.session.extensionRunner.createCommandContext();
}
function configPath(): string {
return path.join(t!.cwd, ".pi/pulse.json");
}
function writeConfig(config: unknown): void {
mkdirSync(path.dirname(configPath()), { recursive: true });
writeFileSync(configPath(), JSON.stringify(config, null, 2), "utf8");
}
function notifyText(): string[] {
return t!.events.uiCallsFor("notify").map((c) => String(c.args[0]));
}
function statusText(): string[] {
return t!.events
.uiCallsFor("setStatus")
.filter((c) => c.args[0] === "pulse")
.map((c) => String(c.args[1]));
}
async function waitFor(
fn: () => boolean,
setTimeoutFn: typeof setTimeout,
nowFn: () => number,
timeoutMs = 2000,
): Promise<void> {
const started = nowFn();
while (!fn() && nowFn() - started < timeoutMs) {
await new Promise((resolve) => setTimeoutFn(resolve, 25));
}
}
describe("pulse pi-test-harness integration", () => {
it("loads through real Pi extension runtime", async () => {
t = await createTestSession({ extensions: [pulsePath()] });
const [extension] = t.session.extensionRunner.extensions;
expect(extension.path).toBe(pulsePath());
expect([...extension.commands.keys()]).toEqual(["pulse"]);
expect([...extension.tools.keys()]).toEqual([]);
expect(typeof pulseCommand().handler).toBe("function");
});
it("runs init/list/status/pause/resume via real command context", async () => {
t = await createTestSession({ extensions: [pulsePath()] });
writeFileSync(path.join(t.cwd, "README.md"), "# Test\n", "utf8");
await pulseCommand().handler("init", ctx());
expect(existsSync(configPath())).toBe(true);
expect(JSON.parse(readFileSync(configPath(), "utf8")).documents).toEqual([
{ path: "README.md" },
]);
await pulseCommand().handler("help", ctx());
await pulseCommand().handler("list", ctx());
await pulseCommand().handler("status", ctx());
await pulseCommand().handler("pause", ctx());
await pulseCommand().handler("resume", ctx());
await pulseCommand().handler("config", ctx());
expect(notifyText()).toContain("Pulse: initialized 1 doc(s)");
expect(
notifyText().some((m) => m.startsWith("Pulse — auto-sync tracked docs")),
).toBe(true);
expect(notifyText().some((m) => m.startsWith("Pulse: 1 doc(s)"))).toBe(
true,
);
expect(notifyText().some((m) => m.startsWith("Pulse\ndocs: 1"))).toBe(true);
expect(notifyText()).toContain("Pulse: paused");
expect(notifyText()).toContain("Pulse: resumed");
expect(statusText().some((s) => s === " paused")).toBe(true);
expect(
t.events.uiCallsFor("editor").some((c) => c.args[0] === "pulse-config"),
).toBe(true);
});
it("handles add/add-dir/add-glob/remove completions with real runtime", async () => {
t = await createTestSession({ extensions: [pulsePath()] });
writeFileSync(path.join(t.cwd, "my file.md"), "doc", "utf8");
writeFileSync(path.join(t.cwd, "README.md"), "doc", "utf8");
mkdirSync(path.join(t.cwd, "docs"), { recursive: true });
writeFileSync(path.join(t.cwd, "docs/a.md"), "doc", "utf8");
mkdirSync(path.join(t.cwd, "adr"), { recursive: true });
writeFileSync(path.join(t.cwd, "adr/001.md"), "adr", "utf8");
writeConfig({ documents: [] });
await pulseCommand().handler('add "my file.md" note here', ctx());
await pulseCommand().handler("add-dir docs **/*.md docs note", ctx());
await pulseCommand().handler("add-glob adr/**/*.md adr note", ctx());
expect(JSON.parse(readFileSync(configPath(), "utf8")).documents).toEqual([
{ path: "my file.md", instruction: "note here" },
{ path: "docs", type: "dir", glob: "**/*.md", instruction: "docs note" },
{ path: "adr/**/*.md", type: "glob", instruction: "adr note" },
]);
const addCompletions = pulseCommand().getArgumentCompletions("add my");
expect(addCompletions).toContainEqual({
value: 'add "my file.md"',
label: "my file.md",
});
const removeCompletions =
pulseCommand().getArgumentCompletions("remove my");
expect(removeCompletions).toContainEqual({
value: 'remove "my file.md"',
label: "my file.md",
});
await pulseCommand().handler('remove "my file.md"', ctx());
expect(
JSON.parse(readFileSync(configPath(), "utf8")).documents.map(
(d: { path: string }) => d.path,
),
).toEqual(["docs", "adr/**/*.md"]);
});
it("starts cleanly on session_start without notification spam and reports status", async () => {
t = await createTestSession({ extensions: [pulsePath()] });
writeFileSync(path.join(t.cwd, "README.md"), "doc", "utf8");
writeConfig({ documents: [{ path: "README.md" }] });
await t.session.extensionRunner.emit({
type: "session_start",
reason: "startup",
});
expect(notifyText()).toEqual([]);
expect(statusText()).toContain(" 1");
await t.session.extensionRunner.emit({
type: "session_shutdown",
reason: "quit",
});
});
it("marks workspace changes from real tool events and keeps force feedback", async () => {
t = await createTestSession({ extensions: [pulsePath()] });
writeFileSync(path.join(t.cwd, "README.md"), "doc", "utf8");
writeConfig({ documents: [{ path: "README.md" }] });
await t.session.extensionRunner.emit({
type: "session_start",
reason: "startup",
});
await t.session.extensionRunner.emit({
type: "tool_execution_end",
toolCallId: "edit-1",
toolName: "edit",
input: { path: "README.md" },
content: [{ type: "text", text: "edited" }],
details: {},
isError: false,
});
expect(statusText().some((s) => s.includes("sync in"))).toBe(true);
await pulseCommand().handler("force", ctx());
expect(notifyText().some((m) => m.startsWith("Pulse: "))).toBe(true);
await t.session.extensionRunner.emit({
type: "session_shutdown",
reason: "quit",
});
});
it("runs periodic idle checks from the timer", async () => {
const realSetInterval = globalThis.setInterval;
const realClearInterval = globalThis.clearInterval;
const realNow = Date.now;
let tick: (() => void) | undefined;
let now = 0;
globalThis.setInterval = ((fn: () => void) => {
tick = fn;
return 1 as never;
}) as never;
globalThis.clearInterval = (() => {}) as never;
Date.now = () => now;
try {
t = await createTestSession({ extensions: [pulsePath()] });
writeFileSync(path.join(t.cwd, "README.md"), "old", "utf8");
writeConfig({ interval: 1, documents: [{ path: "README.md" }] });
await t.session.extensionRunner.emit({
type: "session_start",
reason: "startup",
});
expect(tick).toBeDefined();
now = 6 * 60 * 1000;
tick!();
await waitFor(
() => notifyText().some((m) => m.startsWith("Pulse: ")),
realSetInterval,
realNow,
);
expect(notifyText().some((m) => m.startsWith("Pulse: "))).toBe(true);
await t.session.extensionRunner.emit({
type: "session_shutdown",
reason: "quit",
});
} finally {
globalThis.setInterval = realSetInterval;
globalThis.clearInterval = realClearInterval;
Date.now = realNow;
}
});
});