repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
pi/agent/extensions/ghost/__tests__/overlay-render.test.ts
RawFile outline
- fitLine line 16
- truncates and pads to exact visible width line 17
- GhostOverlayComponent rendering line 24
- never emits a line wider than render(width), including brutal tiny widths line 25
- keeps all lines fitted after long transcript, tool, status, and input churn line 37
- invalidates transcript cache on assistant streaming updates line 57
- invalidates transcript cache on partial tool output line 70
- submits trimmed input, hides on ctrl+s, closes on escape line 80
- message/cwd helpers line 94
- extracts only text content line 95
- formats home cwd compactly line 105
import { describe, expect, it } from "bun:test";
import { visibleWidth } from "@mariozechner/pi-tui";
import { extractMessageText, fitLine, formatGhostCwd } from "../overlay.ts";
import {
assistantEnd,
assistantStart,
assistantUpdate,
createOverlay,
expectLinesFit,
toolEnd,
toolStart,
toolUpdate,
userStart,
} from "./overlay-harness.ts";
describe("fitLine", () => {
it("truncates and pads to exact visible width", () => {
expect(visibleWidth(fitLine("abcdef", 3))).toBe(3);
expect(visibleWidth(fitLine("x", 3))).toBe(3);
expect(fitLine("x", 0)).toBe("");
});
});
describe("GhostOverlayComponent rendering", () => {
it("never emits a line wider than render(width), including brutal tiny widths", () => {
for (let rows = 1; rows <= 24; rows++) {
for (let width = 0; width <= 120; width++) {
const { overlay } = createOverlay(rows, width);
overlay.setStatus("status ".repeat(80));
const lines = overlay.render(width);
expectLinesFit(lines, width);
expect(lines.length).toBeLessThanOrEqual(Math.max(1, rows));
}
}
});
it("keeps all lines fitted after long transcript, tool, status, and input churn", () => {
const { overlay } = createOverlay(18, 60);
overlay.setStatus("S".repeat(500));
for (let i = 0; i < 30; i++) {
overlay.handleSessionEvent(userStart(`user ${i} ${"u".repeat(120)}`));
overlay.handleSessionEvent(assistantStart(`assistant ${i} ${"a".repeat(120)}`));
overlay.handleSessionEvent(assistantUpdate(`assistant update ${i} ${"b".repeat(120)}`));
overlay.handleSessionEvent(assistantEnd(`assistant end ${i} ${"c".repeat(120)}`));
}
overlay.handleSessionEvent(toolStart({ huge: "x".repeat(200) }));
overlay.handleSessionEvent(toolUpdate("partial".repeat(80)));
overlay.handleSessionEvent(toolEnd("done".repeat(80)));
for (const width of [4, 5, 8, 13, 20, 40, 80]) {
expectLinesFit(overlay.render(width), width);
}
});
it("invalidates transcript cache on assistant streaming updates", () => {
const { overlay } = createOverlay(24, 90);
overlay.handleSessionEvent(assistantStart("first token"));
expect(overlay.render(90).join("\n")).toContain("first token");
overlay.handleSessionEvent(assistantUpdate("second token"));
const rendered = overlay.render(90).join("\n");
expect(rendered).toContain("second token");
expect(rendered).not.toContain("first token");
});
it("invalidates transcript cache on partial tool output", () => {
const { overlay } = createOverlay(24, 90);
overlay.handleSessionEvent(toolStart());
expect(overlay.render(90).join("\n")).toContain("ghost_tool");
overlay.handleSessionEvent(toolUpdate("partial-output-visible"));
expect(overlay.render(90).join("\n")).toContain("partial-output-visible");
});
it("submits trimmed input, hides on ctrl+s, closes on escape", () => {
const harness = createOverlay(20, 80);
for (const char of " hello ghost ") harness.overlay.handleInput(char);
harness.overlay.handleInput("\r");
harness.overlay.handleInput("\x13");
harness.overlay.handleInput("\x1b");
expect(harness.submitted).toEqual(["hello ghost"]);
expect(harness.hiddenCount()).toBe(1);
expect(harness.closedCount()).toBe(1);
});
});
describe("message/cwd helpers", () => {
it("extracts only text content", () => {
expect(extractMessageText({
content: [
{ type: "image" },
{ type: "text", text: " a " },
{ type: "text", text: "b" },
],
})).toBe("a \nb");
});
it("formats home cwd compactly", () => {
const home = process.env.HOME;
if (!home) return;
expect(formatGhostCwd(home)).toBe("~");
expect(formatGhostCwd(`${home}/repo`)).toBe("~/repo");
});
});