Files
oh-my-pi/packages/coding-agent/test/modes/controllers/session-selector-delete.test.ts
T
can1357 24c8bb24c6 feat(session): added modular session APIs and rebuilt listing/persistence behavior
- Added session-domain modules and exports for session-entries, context, listing, loader, and migrations.
- Changed persistence to async append writes plus writeTextAtomic, removing sync line APIs.
- Added compaction-aware session context rebuild with dangling tool-call cleanup.
- Added resumable session resolution with status inference, id/stem/suffix matching, and backup recovery.
2026-06-14 02:02:53 +02:00

94 lines
2.8 KiB
TypeScript

import { afterEach, beforeAll, describe, expect, it, vi } from "bun:test";
import { SessionSelectorComponent } from "@oh-my-pi/pi-coding-agent/modes/components/session-selector";
import { initTheme } from "@oh-my-pi/pi-coding-agent/modes/theme/theme";
import type { SessionInfo } from "@oh-my-pi/pi-coding-agent/session/session-listing";
beforeAll(() => {
initTheme();
});
afterEach(() => {
vi.restoreAllMocks();
});
function createSession(id: string, title: string): SessionInfo {
return {
path: `/tmp/${id}.jsonl`,
id,
cwd: "/tmp",
title,
created: new Date("2024-01-01T00:00:00Z"),
modified: new Date("2024-01-02T00:00:00Z"),
messageCount: 1,
size: 0,
firstMessage: `${title} first message`,
allMessagesText: `${title} first message`,
};
}
function createSelector(onDelete: (session: SessionInfo) => Promise<boolean>): SessionSelectorComponent {
return new SessionSelectorComponent(
[createSession("session-a", "Alpha"), createSession("session-b", "Beta")],
() => {},
() => {},
() => {},
{ onDelete },
);
}
function renderText(selector: SessionSelectorComponent): string {
return selector.render(120).join("\n");
}
describe("SessionSelectorComponent delete confirmation", () => {
it("keeps the session visible and shows the error when delete fails after confirmation", async () => {
const onDelete = vi.fn(async () => {
throw new Error("disk failed");
});
const selector = createSelector(onDelete);
selector.handleInput("\x1b[3~");
expect(renderText(selector)).toContain("Delete session?");
expect(renderText(selector)).toContain("Alpha");
selector.handleInput("\n");
await Bun.sleep(0);
const rendered = renderText(selector);
expect(onDelete).toHaveBeenCalledTimes(1);
expect(rendered).toContain("Error: disk failed");
expect(rendered).toContain("Alpha");
expect(rendered).toContain("Beta");
expect(rendered).not.toContain("Delete session?");
});
it("keeps the session visible when delete is canceled upstream", async () => {
const onDelete = vi.fn(async () => false);
const selector = createSelector(onDelete);
selector.handleInput("\x1b[3~");
selector.handleInput("\n");
await Bun.sleep(0);
const rendered = renderText(selector);
expect(onDelete).toHaveBeenCalledTimes(1);
expect(rendered).toContain("Alpha");
expect(rendered).toContain("Beta");
expect(rendered).not.toContain("Error:");
});
it("removes the session row after a successful delete", async () => {
const onDelete = vi.fn(async () => true);
const selector = createSelector(onDelete);
selector.handleInput("\x1b[3~");
selector.handleInput("\n");
await Bun.sleep(0);
const rendered = renderText(selector);
expect(onDelete).toHaveBeenCalledTimes(1);
expect(rendered).not.toContain("Alpha");
expect(rendered).toContain("Beta");
});
});