repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/loop/__tests__/progress.test.ts
Rawimport type { Theme } from "@earendil-works/pi-coding-agent";
import { visibleWidth } from "@earendil-works/pi-tui";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { LoopSnapshot } from "../src/controller.ts";
import { createProgress } from "../src/progress.ts";
const themes = await import(
new URL(
"./modes/interactive/theme/theme.js",
import.meta.resolve("@earendil-works/pi-coding-agent"),
).href
);
const theme: Theme = themes.getThemeByName("dark");
const plain = (lines: string[]) =>
lines.join("\n").replace(/\x1b\[[0-9;]*m/gu, "");
function run(): LoopSnapshot {
return {
id: "test",
goal: "Done",
gates: [{ name: "accept", command: "unused" }],
cwd: ".",
budgets: { minutes: null, turns: null, attempts: null },
status: "active",
reason: "",
turns: 0,
attempts: 1,
createdAt: 0,
updatedAt: 0,
deadline: null,
lastPass: null,
runningGate: 0,
runningSince: 0,
liveOutput: null,
pending: true,
pendingAudit: 0,
storageError: null,
};
}
afterEach(() => vi.useRealTimers());
describe("loop progress widget", () => {
it("advances spinner and elapsed time during silence; reflects cancellation and disposes refresh", () => {
vi.useFakeTimers();
vi.setSystemTime(0);
const current = { run: run(), baseline: true, agentRunning: false };
const refresh = vi.fn();
const widget = createProgress(
{ requestRender: refresh },
theme,
() => current,
);
const first = plain(widget.render(100));
expect(first).toContain("Baseline · accept [1/1] · 00:00 elapsed");
expect(first).toContain("no output yet");
expect(first).toContain("Esc / /loop pause");
vi.advanceTimersByTime(1250);
const later = plain(widget.render(100));
expect(later).toContain("00:01 elapsed");
expect(later[0]).not.toBe(first[0]);
expect(refresh).toHaveBeenCalled();
current.run.status = "paused";
expect(plain(widget.render(100))).toContain("paused · cancelling gate");
current.run.pending = false;
expect(plain(widget.render(100))).toBe("Loop paused");
const count = refresh.mock.calls.length;
vi.advanceTimersByTime(1000);
expect(refresh).toHaveBeenCalledTimes(count);
current.run.status = "active";
current.run.pending = true;
widget.render(100);
widget.dispose();
vi.advanceTimersByTime(1000);
expect(refresh).toHaveBeenCalledTimes(count);
expect(widget.render(100)).toEqual([]);
});
it("bounds and sanitizes gate output, adapts to width, and shows phase transitions", () => {
vi.useFakeTimers();
const current = { run: run(), baseline: false, agentRunning: false };
current.run.gates[0]!.name = "accept\u001b[2J\nmalicious";
current.run.liveOutput = {
stdout: "old\n",
stderr: "\u001b[31mLatest diagnostic\u001b[0m\u0007\n",
truncated: false,
};
const widget = createProgress({ requestRender() {} }, theme, () => current);
const text = plain(widget.render(100));
expect(text).toContain("Checking gates");
expect(text).toContain("Output: Latest diagnostic");
current.run.liveOutput = {
stdout: "usable\n",
stderr: "\u001b[31m\n\u001b[0m\u0007",
truncated: false,
};
expect(plain(widget.render(100))).toContain("Output: usable");
expect(text).not.toMatch(/[\x00-\x09\x0b-\x1f\x7f]/u);
for (const width of [1, 20, 50])
expect(
widget.render(width).every((line) => visibleWidth(line) <= width),
).toBe(true);
expect(widget.render(0)).toEqual([]);
current.run.pending = false;
expect(plain(widget.render(100))).toBe("Waiting for agent");
current.agentRunning = true;
expect(plain(widget.render(100))).toBe("Agent working");
current.run.status = "succeeded";
expect(plain(widget.render(100))).toBe("Loop passed");
current.run.status = "stopped";
expect(plain(widget.render(100))).toBe("Loop stopped");
widget.dispose();
});
});