Files
oh-my-pi/packages/coding-agent/test/interactive-mode-todo-clear.test.ts
T
roboomp 8a2c90a48c fix(coding-agent): anchored todo HUD with borders and progress header
The sticky panel above the editor rendered as ambient text:

  Todos

  └ I. Foundation

  └ ☐ ...

so users perceived the bordered tool-result block in chat as the

only todo display. Once that result scrolled into history they

concluded the list 'isn't anchored'.

Bracket the panel with dim horizontal rules (matching BtwPanel /

OmfgPanel) and inline progress + active-phase pointer in the header

(`Todos · 2/7 done · I/III Foundation`) so the persistent HUD reads

as a real panel and stays self-describing without scrolling back.

Fixes #3213
2026-06-21 22:38:20 +00:00

203 lines
6.4 KiB
TypeScript

import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "bun:test";
import * as path from "node:path";
import { Agent } from "@oh-my-pi/pi-agent-core";
import { ModelRegistry } from "@oh-my-pi/pi-coding-agent/config/model-registry";
import { resetSettingsForTest, Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
import { InteractiveMode } from "@oh-my-pi/pi-coding-agent/modes/interactive-mode";
import { initTheme } from "@oh-my-pi/pi-coding-agent/modes/theme/theme";
import { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session";
import { AuthStorage } from "@oh-my-pi/pi-coding-agent/session/auth-storage";
import { SessionManager } from "@oh-my-pi/pi-coding-agent/session/session-manager";
import type { TodoPhase } from "@oh-my-pi/pi-coding-agent/tools/todo";
import { TempDir } from "@oh-my-pi/pi-utils";
function renderTodos(mode: InteractiveMode): string {
return Bun.stripANSI(mode.todoContainer.render(120).join("\n"));
}
describe("InteractiveMode todo auto-clear", () => {
let tempDir: TempDir;
let authStorage: AuthStorage;
let session: AgentSession;
let mode: InteractiveMode;
beforeAll(async () => {
await initTheme();
});
beforeEach(async () => {
resetSettingsForTest();
tempDir = TempDir.createSync("@pi-todo-clear-");
});
afterEach(async () => {
mode?.stop();
await session?.dispose();
authStorage?.close();
tempDir?.removeSync();
vi.useRealTimers();
vi.restoreAllMocks();
resetSettingsForTest();
});
async function createMode(todoClearDelay: number): Promise<void> {
await Settings.init({
inMemory: true,
cwd: tempDir.path(),
overrides: { "tasks.todoClearDelay": todoClearDelay },
});
authStorage = await AuthStorage.create(path.join(tempDir.path(), "testauth.db"));
const modelRegistry = new ModelRegistry(authStorage);
const model = modelRegistry.find("anthropic", "claude-sonnet-4-5");
if (!model) throw new Error("Expected claude-sonnet-4-5 to exist in registry");
session = new AgentSession({
agent: new Agent({
initialState: {
model,
systemPrompt: ["Test"],
tools: [],
messages: [],
},
}),
sessionManager: SessionManager.create(tempDir.path(), tempDir.path()),
settings: Settings.isolated({ "tasks.todoClearDelay": todoClearDelay }),
modelRegistry,
});
mode = new InteractiveMode(session, "test");
}
it("clears closed todos from the panel instantly without mutating session history", async () => {
await createMode(0);
const phases: TodoPhase[] = [
{
name: "Implementation",
tasks: [
{ content: "done task", status: "completed" },
{ content: "abandoned task", status: "abandoned" },
],
},
];
session.setTodoPhases(phases);
mode.setTodos(session.getTodoPhases());
expect(renderTodos(mode)).not.toContain("done task");
expect(renderTodos(mode)).not.toContain("abandoned task");
expect(session.getTodoPhases()).toEqual(phases);
});
it("leaves closed todos visible when auto-clear is disabled", async () => {
await createMode(-1);
mode.setTodos([{ name: "Implementation", tasks: [{ content: "done task", status: "completed" }] }]);
expect(renderTodos(mode)).toContain("done task");
});
it("clears closed todos after the configured delay", async () => {
await createMode(1);
vi.useFakeTimers();
mode.setTodos([{ name: "Implementation", tasks: [{ content: "done task", status: "completed" }] }]);
expect(renderTodos(mode)).toContain("done task");
vi.advanceTimersByTime(999);
expect(renderTodos(mode)).toContain("done task");
vi.advanceTimersByTime(1);
expect(renderTodos(mode)).not.toContain("done task");
});
});
describe("InteractiveMode todo HUD anchor", () => {
let tempDir: TempDir;
let authStorage: AuthStorage;
let session: AgentSession;
let mode: InteractiveMode;
beforeAll(async () => {
await initTheme();
});
beforeEach(async () => {
resetSettingsForTest();
tempDir = TempDir.createSync("@pi-todo-hud-");
await Settings.init({ inMemory: true, cwd: tempDir.path() });
authStorage = await AuthStorage.create(path.join(tempDir.path(), "testauth.db"));
const modelRegistry = new ModelRegistry(authStorage);
const model = modelRegistry.find("anthropic", "claude-sonnet-4-5");
if (!model) throw new Error("Expected claude-sonnet-4-5 to exist in registry");
session = new AgentSession({
agent: new Agent({
initialState: { model, systemPrompt: ["Test"], tools: [], messages: [] },
}),
sessionManager: SessionManager.create(tempDir.path(), tempDir.path()),
settings: Settings.isolated({}),
modelRegistry,
});
mode = new InteractiveMode(session, "test");
});
afterEach(async () => {
mode?.stop();
await session?.dispose();
authStorage?.close();
tempDir?.removeSync();
vi.useRealTimers();
vi.restoreAllMocks();
resetSettingsForTest();
});
it("brackets the panel with horizontal rules and a progress header", () => {
mode.setTodos([
{
name: "Foundation",
tasks: [
{ content: "first task", status: "completed" },
{ content: "second task", status: "in_progress" },
{ content: "third task", status: "pending" },
],
},
{
name: "Verification",
tasks: [{ content: "run tests", status: "pending" }],
},
]);
const lines = mode.todoContainer.render(80).flatMap(line => line.split("\n"));
const stripped = lines.map(line => Bun.stripANSI(line));
// Top + bottom rules — full-width horizontal rules render as ─ repeated.
expect(stripped[0]).toBe("─".repeat(80));
expect(stripped[stripped.length - 1]).toBe("─".repeat(80));
// Header shows progress and active-phase pointer so the HUD is
// self-describing without scrolling back to the tool result.
const header = stripped[1] ?? "";
expect(header).toContain("Todos");
expect(header).toContain("1/4 done");
expect(header).toContain("I/II Foundation");
});
it("renders nothing when there are no todos", () => {
mode.setTodos([]);
expect(mode.todoContainer.render(80)).toHaveLength(0);
});
it("omits the phase pointer in the header for a single-phase list", () => {
mode.setTodos([
{
name: "Tasks",
tasks: [
{ content: "alpha", status: "pending" },
{ content: "beta", status: "pending" },
],
},
]);
const header = Bun.stripANSI(mode.todoContainer.render(80)[1] ?? "");
expect(header).toContain("Todos");
expect(header).toContain("0/2 done");
expect(header).toContain("Tasks");
expect(header).not.toContain("/I ");
});
});