| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { setKeybindings } from "@earendil-works/pi-tui";
- import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
- import { KeybindingsManager } from "../src/core/keybindings.ts";
- import type { SessionInfo } from "../src/core/session-manager.ts";
- import { SessionSelectorComponent } from "../src/modes/interactive/components/session-selector.ts";
- import { initTheme } from "../src/modes/interactive/theme/theme.ts";
- type Deferred<T> = {
- promise: Promise<T>;
- resolve: (value: T) => void;
- reject: (err: unknown) => void;
- };
- function createDeferred<T>(): Deferred<T> {
- let resolve: (value: T) => void = () => {};
- let reject: (err: unknown) => void = () => {};
- const promise = new Promise<T>((res, rej) => {
- resolve = res;
- reject = rej;
- });
- return { promise, resolve, reject };
- }
- async function flushPromises(): Promise<void> {
- await new Promise<void>((resolve) => {
- setImmediate(resolve);
- });
- }
- function stripAnsi(text: string): string {
- return text.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "");
- }
- function makeSession(overrides: Partial<SessionInfo> & { id: string }): SessionInfo {
- return {
- path: overrides.path ?? `/tmp/${overrides.id}.jsonl`,
- id: overrides.id,
- cwd: overrides.cwd ?? "",
- name: overrides.name,
- parentSessionPath: overrides.parentSessionPath,
- created: overrides.created ?? new Date(0),
- modified: overrides.modified ?? new Date(0),
- messageCount: overrides.messageCount ?? 1,
- firstMessage: overrides.firstMessage ?? "hello",
- allMessagesText: overrides.allMessagesText ?? "hello",
- };
- }
- function createSymlinkedSessionPaths(): {
- baseDir: string;
- parentAliasA: string;
- parentAliasB: string;
- childAliasB: string;
- } {
- const baseDir = mkdtempSync(join(tmpdir(), "pi-session-selector-"));
- const realDir = join(baseDir, "real");
- const aliasADir = join(baseDir, "alias-a");
- const aliasBDir = join(baseDir, "alias-b");
- mkdirSync(realDir, { recursive: true });
- mkdirSync(aliasADir, { recursive: true });
- mkdirSync(aliasBDir, { recursive: true });
- const sharedDir = join(realDir, "sessions");
- mkdirSync(sharedDir, { recursive: true });
- const aliasASessions = join(aliasADir, "sessions");
- const aliasBSessions = join(aliasBDir, "sessions");
- symlinkSync(sharedDir, aliasASessions);
- symlinkSync(sharedDir, aliasBSessions);
- const parentRealPath = join(sharedDir, "parent.jsonl");
- const childRealPath = join(sharedDir, "child.jsonl");
- writeFileSync(parentRealPath, "parent\n");
- writeFileSync(childRealPath, "child\n");
- return {
- baseDir,
- parentAliasA: join(aliasASessions, "parent.jsonl"),
- parentAliasB: join(aliasBSessions, "parent.jsonl"),
- childAliasB: join(aliasBSessions, "child.jsonl"),
- };
- }
- const CTRL_D = "\x04";
- const CTRL_BACKSPACE = "\x1b[127;5u";
- describe("session selector path/delete interactions", () => {
- const keybindings = new KeybindingsManager();
- const tempDirs: string[] = [];
- afterEach(() => {
- for (const dir of tempDirs.splice(0)) {
- rmSync(dir, { recursive: true, force: true });
- }
- });
- beforeEach(() => {
- // Ensure test isolation: keybindings are a global singleton
- setKeybindings(new KeybindingsManager());
- });
- beforeAll(() => {
- // session selector uses the global theme instance
- initTheme("dark");
- });
- it("does not treat Ctrl+Backspace as delete when search query is non-empty", async () => {
- const sessions = [makeSession({ id: "a" }), makeSession({ id: "b" })];
- const selector = new SessionSelectorComponent(
- async () => sessions,
- async () => [],
- () => {},
- () => {},
- () => {},
- () => {},
- { keybindings },
- );
- await flushPromises();
- const list = selector.getSessionList();
- const confirmationChanges: Array<string | null> = [];
- list.onDeleteConfirmationChange = (path) => confirmationChanges.push(path);
- list.handleInput("a");
- list.handleInput(CTRL_BACKSPACE);
- expect(confirmationChanges).toEqual([]);
- });
- it("enters confirmation mode on Ctrl+D even with a non-empty search query", async () => {
- const sessions = [makeSession({ id: "a" }), makeSession({ id: "b" })];
- const selector = new SessionSelectorComponent(
- async () => sessions,
- async () => [],
- () => {},
- () => {},
- () => {},
- () => {},
- { keybindings },
- );
- await flushPromises();
- const list = selector.getSessionList();
- const confirmationChanges: Array<string | null> = [];
- list.onDeleteConfirmationChange = (path) => confirmationChanges.push(path);
- list.handleInput("a");
- list.handleInput(CTRL_D);
- expect(confirmationChanges).toEqual([sessions[0]!.path]);
- });
- it("enters confirmation mode on Ctrl+Backspace when search query is empty", async () => {
- const sessions = [makeSession({ id: "a" }), makeSession({ id: "b" })];
- const selector = new SessionSelectorComponent(
- async () => sessions,
- async () => [],
- () => {},
- () => {},
- () => {},
- () => {},
- { keybindings },
- );
- await flushPromises();
- const list = selector.getSessionList();
- const confirmationChanges: Array<string | null> = [];
- list.onDeleteConfirmationChange = (path) => confirmationChanges.push(path);
- let deletedPath: string | null = null;
- list.onDeleteSession = async (sessionPath) => {
- deletedPath = sessionPath;
- };
- list.handleInput(CTRL_BACKSPACE);
- expect(confirmationChanges).toEqual([sessions[0]!.path]);
- list.handleInput("\r");
- expect(confirmationChanges).toEqual([sessions[0]!.path, null]);
- expect(deletedPath).toBe(sessions[0]!.path);
- });
- it("does not switch scope back to All when All load resolves after toggling back to Current", async () => {
- const currentSessions = [makeSession({ id: "current" })];
- const allDeferred = createDeferred<SessionInfo[]>();
- let allLoadCalls = 0;
- const selector = new SessionSelectorComponent(
- async () => currentSessions,
- async () => {
- allLoadCalls++;
- return allDeferred.promise;
- },
- () => {},
- () => {},
- () => {},
- () => {},
- { keybindings },
- );
- await flushPromises();
- const list = selector.getSessionList();
- list.handleInput("\t"); // current -> all (starts async load)
- list.handleInput("\t"); // all -> current
- allDeferred.resolve([makeSession({ id: "all" })]);
- await flushPromises();
- expect(allLoadCalls).toBe(1);
- const output = selector.render(120).join("\n");
- expect(output).toContain("Resume Session (Current Folder)");
- expect(output).not.toContain("Resume Session (All)");
- });
- it("does not start redundant All loads when toggling scopes while All is already loading", async () => {
- const currentSessions = [makeSession({ id: "current" })];
- const allDeferred = createDeferred<SessionInfo[]>();
- let allLoadCalls = 0;
- const selector = new SessionSelectorComponent(
- async () => currentSessions,
- async () => {
- allLoadCalls++;
- return allDeferred.promise;
- },
- () => {},
- () => {},
- () => {},
- () => {},
- { keybindings },
- );
- await flushPromises();
- const list = selector.getSessionList();
- list.handleInput("\t"); // current -> all (starts async load)
- list.handleInput("\t"); // all -> current
- list.handleInput("\t"); // current -> all again while load pending
- expect(allLoadCalls).toBe(1);
- allDeferred.resolve([makeSession({ id: "all" })]);
- await flushPromises();
- });
- it("threads sessions when parent and child paths use different symlink aliases", async () => {
- const paths = createSymlinkedSessionPaths();
- tempDirs.push(paths.baseDir);
- const sessions = [
- makeSession({
- id: "parent",
- path: paths.parentAliasB,
- name: "Parent",
- modified: new Date("2026-01-01T00:00:00.000Z"),
- }),
- makeSession({
- id: "child",
- path: paths.childAliasB,
- parentSessionPath: paths.parentAliasA,
- name: "Child",
- modified: new Date("2025-12-31T00:00:00.000Z"),
- }),
- ];
- const selector = new SessionSelectorComponent(
- async () => sessions,
- async () => [],
- () => {},
- () => {},
- () => {},
- () => {},
- { keybindings },
- );
- await flushPromises();
- const output = stripAnsi(selector.render(120).join("\n"));
- expect(output).toContain("Parent");
- expect(output).toContain("└─ Child");
- });
- it("treats the current session as active across symlink aliases", async () => {
- const paths = createSymlinkedSessionPaths();
- tempDirs.push(paths.baseDir);
- const sessions = [makeSession({ id: "parent", path: paths.parentAliasB, name: "Parent" })];
- const selector = new SessionSelectorComponent(
- async () => sessions,
- async () => [],
- () => {},
- () => {},
- () => {},
- () => {},
- { keybindings },
- paths.parentAliasA,
- );
- await flushPromises();
- const list = selector.getSessionList();
- const confirmationChanges: Array<string | null> = [];
- let errorMessage: string | undefined;
- list.onDeleteConfirmationChange = (path) => confirmationChanges.push(path);
- list.onError = (message) => {
- errorMessage = message;
- };
- list.handleInput(CTRL_D);
- expect(confirmationChanges).toEqual([]);
- expect(errorMessage).toBe("Cannot delete the currently active session");
- });
- });
|