Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

34.6 KB
Raw Blame
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import {
  fireEvent,
  render,
  screen,
  waitFor,
  within,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { DEFAULT_VISIBLE_APPS } from "@/config/appConfig";
import { UNKNOWN_PROJECT_DIR_KEY } from "@/components/sessions/utils";
import { SessionManagerPage } from "@/components/sessions/SessionManagerPage";
import { piApi } from "@/lib/api/pi";
import { sessionsApi } from "@/lib/api/sessions";
import { settingsApi } from "@/lib/api/settings";
import type { SessionMessage, SessionMeta } from "@/types";
import { setSessionFixtures, setSettings } from "../msw/state";

const toastSuccessMock = vi.fn();
const toastErrorMock = vi.fn();
const platform = vi.hoisted(() => ({ mac: false }));

vi.mock("sonner", () => ({
  toast: {
    success: (...args: unknown[]) => toastSuccessMock(...args),
    error: (...args: unknown[]) => toastErrorMock(...args),
  },
}));

vi.mock("@/lib/platform", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@/lib/platform")>();
  return { ...actual, isMac: () => platform.mac };
});

// jsdom 没有布局,虚拟列表一条都不渲染;这里让它把全部条目都画出来
vi.mock("@tanstack/react-virtual", () => ({
  useVirtualizer: ({ count }: { count: number }) => ({
    getTotalSize: () => count * 100,
    getVirtualItems: () =>
      Array.from({ length: count }, (_, index) => ({
        index,
        key: index,
        start: index * 100,
      })),
    measure: () => undefined,
    measureElement: () => undefined,
    scrollToIndex: () => undefined,
  }),
}));

const HOUR = 3600 * 1000;

const renderPage = (
  appId = "codex",
  props: Partial<Parameters<typeof SessionManagerPage>[0]> = {},
) => {
  const client = new QueryClient({
    defaultOptions: {
      queries: { retry: false },
      mutations: { retry: false },
    },
  });

  return {
    client,
    ...render(
      <QueryClientProvider client={client}>
        <SessionManagerPage appId={appId} {...props} />
      </QueryClientProvider>,
    ),
  };
};

const sessionList = () => screen.getByRole("region", { name: "会话列表" });

const openRow = (title: string) =>
  fireEvent.click(within(sessionList()).getByRole("button", { name: title }));

const openAppMenu = async () =>
  userEvent.click(screen.getByRole("button", { name: /^应用:/ }));

const EXPANDED_KEY = "cc-switch.sessionManager.expandedProjects";

const setupZetaSession = (word = "zeta") => {
  const source = "/mock/codex/needle.jsonl";
  setSessionFixtures(
    [
      {
        providerId: "codex",
        sessionId: "needle-session",
        title: "Needle Session",
        projectDir: "/mock/codex",
        createdAt: Date.now() - HOUR,
        lastActiveAt: Date.now() - HOUR,
        sourcePath: source,
      },
    ],
    {
      [`codex:${source}`]: [
        {
          role: "user",
          content: "",
          turnId: "t1",
          blocks: [{ type: "text", text: `${word} first` }],
        },
        {
          role: "assistant",
          content: "",
          turnId: "t1",
          blocks: [{ type: "text", text: "reply" }],
        },
        {
          role: "user",
          content: "",
          turnId: "t2",
          blocks: [{ type: "text", text: "again" }],
        },
        {
          role: "assistant",
          content: "",
          turnId: "t2",
          blocks: [{ type: "text", text: `${word} second` }],
        },
      ],
    },
  );
  // 后端认为最相关的是第 4 条消息(下标 3)
  const searchSpy = vi.spyOn(sessionsApi, "searchContent").mockResolvedValue({
    hits: [
      {
        providerId: "codex",
        sourcePath: source,
        snippets: [
          {
            messageIndex: 3,
            text: `${word} second`,
            highlights: [{ start: 0, end: word.length }],
          },
          {
            messageIndex: 0,
            text: `${word} first`,
            highlights: [{ start: 0, end: word.length }],
          },
        ],
        matchCount: 2,
      },
    ],
    status: { running: false, processed: 1, total: 1 },
  });

  return searchSpy;
};

describe("SessionManagerPage", () => {
  beforeEach(() => {
    // 固定在本地时间的中午:会话时间都是「现在往前几小时」,凌晨跑时会跨到昨天,
    // 「按时间」分组就不稳定。只假 Date 且照常走时,计时器保持真实。
    vi.useFakeTimers({ toFake: ["Date"], shouldAdvanceTime: true });
    vi.setSystemTime(new Date(2026, 5, 15, 12, 0, 0));
    toastSuccessMock.mockReset();
    toastErrorMock.mockReset();
    platform.mac = false;
    window.localStorage.clear();
    // 项目分组默认收起;其余用例要直接看到会话,先把模拟项目都记成已展开
    window.localStorage.setItem(
      EXPANDED_KEY,
      JSON.stringify(["/mock/codex", "/mock/claude", UNKNOWN_PROJECT_DIR_KEY]),
    );
    Object.assign(navigator, {
      clipboard: { writeText: vi.fn().mockResolvedValue(undefined) },
    });

    const now = Date.now();
    const sessions: SessionMeta[] = [
      {
        providerId: "codex",
        sessionId: "codex-session-1",
        title: "Alpha Session",
        summary: "Alpha summary",
        projectDir: "/mock/codex",
        createdAt: now - 3 * HOUR,
        lastActiveAt: now - HOUR,
        sourcePath: "/mock/codex/session-1.jsonl",
        resumeCommand: "codex resume codex-session-1",
      },
      {
        providerId: "codex",
        sessionId: "codex-session-2",
        title: "Beta Session",
        summary: "Beta summary",
        projectDir: "/mock/codex",
        createdAt: now - 5 * HOUR,
        lastActiveAt: now - 2 * HOUR,
        sourcePath: "/mock/codex/session-2.jsonl",
        resumeCommand: "codex resume codex-session-2",
      },
      {
        providerId: "claude",
        sessionId: "claude-session-1",
        title: "Claude Session",
        summary: "Claude summary",
        projectDir: "/mock/claude",
        createdAt: now - 2 * HOUR,
        lastActiveAt: now - 30 * 60 * 1000,
        sourcePath: "/mock/claude/session-1.jsonl",
        resumeCommand: "claude --resume claude-session-1",
      },
      {
        providerId: "codex",
        sessionId: "codex-session-3",
        title: "Gamma Session",
        summary: "Gamma summary",
        projectDir: null,
        createdAt: now - 30 * 24 * HOUR,
        lastActiveAt: now - 30 * 24 * HOUR,
        sourcePath: "/mock/codex/archived_sessions/session-3.jsonl",
        resumeCommand: "codex resume codex-session-3",
      },
      {
        providerId: "hermes",
        sessionId: "hermes-1",
        title: "Hermes Session",
        projectDir: null,
        createdAt: now - 4 * HOUR,
        lastActiveAt: now - 4 * HOUR,
        sourcePath: "/mock/hermes/state.db",
      },
    ];
    const messages: Record<string, SessionMessage[]> = {
      "codex:/mock/codex/session-1.jsonl": [
        { role: "user", content: "alpha question", ts: now - 3 * HOUR },
        { role: "assistant", content: "[Tool: shell]" },
        { role: "tool", content: "output line" },
        {
          role: "assistant",
          content: "Here:\n```ts\nconst a = 1;\n```",
          ts: now - HOUR,
        },
      ],
      "codex:/mock/codex/session-2.jsonl": [
        { role: "user", content: "beta", ts: now - 2 * HOUR },
      ],
      "codex:/mock/codex/archived_sessions/session-3.jsonl": [
        { role: "user", content: "gamma", ts: now - 30 * 24 * HOUR },
      ],
      "claude:/mock/claude/session-1.jsonl": [
        { role: "user", content: "claude", ts: now },
      ],
      "hermes:/mock/hermes/state.db": [],
    };

    setSessionFixtures(sessions, messages);
    // 正文搜索默认关闭;大多数用例要连正文一起搜,先打开,默认关闭的行为单独测
    setSettings({ sessionContentSearchEnabled: true });
  });

  afterEach(() => {
    vi.useRealTimers();
  });

  it("shows Oh My Pi sessions from its app and in the all-apps filter", async () => {
    setSessionFixtures(
      [
        {
          providerId: "omp",
          sessionId: "omp-native-session",
          title: "Native OMP conversation",
          sourcePath: "/mock/omp/sessions/project/native.jsonl",
          lastActiveAt: Date.now(),
        },
      ],
      {},
    );
    setSettings({ visibleApps: { ...DEFAULT_VISIBLE_APPS, omp: true } });
    renderPage("omp");
    expect(
      await screen.findByText("Native OMP conversation"),
    ).toBeInTheDocument();
    await openAppMenu();
    const menu = await screen.findByRole("menu");
    expect(
      within(menu).getByRole("menuitemradio", { name: /Oh My Pi/ }),
    ).toBeInTheDocument();
    await userEvent.click(
      within(menu).getByRole("menuitemradio", { name: /全部应用/ }),
    );
    expect(
      await screen.findByText("Native OMP conversation"),
    ).toBeInTheDocument();
  });

  it("surfaces a relative Pi sessionDir instead of presenting an empty scan as authoritative", async () => {
    const discovery = vi.spyOn(piApi, "getSessionDiscovery").mockResolvedValue({
      status: "requires_project_context",
      configuredPath: ".pi/sessions",
    });

    renderPage("pi");

    expect(await screen.findByText(".pi/sessions")).toBeInTheDocument();
    expect(discovery).toHaveBeenCalledTimes(1);
    discovery.mockRestore();
  });

  it("lists the current app's sessions grouped by project with unknown last", async () => {
    renderPage("codex");

    await screen.findByRole("button", { name: "Alpha Session" });
    const headings = within(sessionList())
      .getAllByRole("heading", { level: 3 })
      .map((heading) => heading.textContent);
    expect(headings[0]).toContain("codex");
    expect(headings[1]).toContain("未知目录");
    expect(screen.queryByText("Claude Session")).not.toBeInTheDocument();
    // 归档的 Codex 会话带「已归档」
    expect(screen.getByText("已归档")).toBeInTheDocument();
    // 应用按钮写全称和数量
    expect(screen.getByRole("button", { name: /^应用:/ })).toHaveTextContent(
      "Codex3",
    );
  });

  it("collapses a project group and remembers it", async () => {
    renderPage("codex");

    const toggle = await screen.findByRole("button", { name: /^codex/ });
    expect(toggle).toHaveAttribute("aria-expanded", "true");
    fireEvent.click(toggle);

    expect(toggle).toHaveAttribute("aria-expanded", "false");
    expect(screen.queryByText("Alpha Session")).not.toBeInTheDocument();
    expect(window.localStorage.getItem(EXPANDED_KEY)).not.toContain(
      "/mock/codex",
    );
  });

  it("starts with project groups collapsed and remembers expanded ones", async () => {
    window.localStorage.removeItem(EXPANDED_KEY);
    renderPage("codex");

    const toggle = await screen.findByRole("button", { name: /^codex/ });
    expect(toggle).toHaveAttribute("aria-expanded", "false");
    expect(screen.queryByText("Alpha Session")).not.toBeInTheDocument();

    fireEvent.click(toggle);
    expect(toggle).toHaveAttribute("aria-expanded", "true");
    expect(screen.getByText("Alpha Session")).toBeInTheDocument();
    expect(window.localStorage.getItem(EXPANDED_KEY)).toContain("/mock/codex");
  });

  it("opens collapsed groups while searching", async () => {
    window.localStorage.removeItem(EXPANDED_KEY);
    renderPage("codex");

    await screen.findByRole("button", { name: /^codex/ });
    expect(screen.queryByText("Alpha Session")).not.toBeInTheDocument();

    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "Alpha" },
    });
    // 标题里的匹配部分会被高亮拆开,按高亮的那段找
    expect(await screen.findByText("Alpha")).toBeInTheDocument();
  });

  it("finds sessions by message text and opens the reader at the match", async () => {
    renderPage("codex");
    await screen.findByText("Alpha Session");

    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "const a" },
    });

    // 标题和摘要里都没有这个词,只有正文命中
    expect(await screen.findByText(/^正文:/)).toBeInTheDocument();
    expect(screen.queryByText("Beta Session")).not.toBeInTheDocument();

    openRow("Alpha Session");
    expect(
      await screen.findByRole("textbox", { name: "查找内容" }),
    ).toHaveValue("const a");
  });

  it("jumps to the message behind the content hit, not the first match", async () => {
    const searchSpy = setupZetaSession();
    renderPage("codex");
    await screen.findByText("Needle Session");
    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "zeta" },
    });
    expect(await screen.findByText(/2 条消息命中/)).toBeInTheDocument();

    openRow("Needle Session");
    expect(await screen.findByText("2 / 2")).toBeInTheDocument();
    searchSpy.mockRestore();
  });

  it("opens the reader at an extra snippet's message when it is clicked", async () => {
    const searchSpy = setupZetaSession();
    renderPage("codex");
    await screen.findByText("Needle Session");
    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "zeta" },
    });

    const extras = await screen.findByRole("list", { name: "更多正文命中" });
    fireEvent.click(within(extras).getByRole("button"));
    expect(await screen.findByText("1 / 2")).toBeInTheDocument();
    searchSpy.mockRestore();
  });

  it.each([false, true])(
    "keeps Unicode snippet highlights and clicked message positions aligned (extra: %s)",
    async (extra) => {
      const searchSpy = setupZetaSession("İZMİR");
      renderPage("codex");
      await screen.findByText("Needle Session");
      fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
        target: { value: "i\u0307zmi\u0307r" },
      });
      expect(await screen.findByText(/2 条消息命中/)).toBeInTheDocument();
      expect(
        Array.from(
          sessionList().querySelectorAll("mark"),
          (mark) => mark.textContent,
        ),
      ).toEqual(["İZMİR", "İZMİR"]);

      if (extra) {
        const extras = screen.getByRole("list", { name: "更多正文命中" });
        fireEvent.click(within(extras).getByRole("button"));
      } else {
        openRow("Needle Session");
      }
      expect(
        await screen.findByText(extra ? "1 / 2" : "2 / 2"),
      ).toBeInTheDocument();
      const reader = screen.getByRole("region", { name: "对话内容" });
      expect(
        Array.from(reader.querySelectorAll("mark"), (mark) => mark.textContent),
      ).toEqual(["İZMİR", "İZMİR"]);
      searchSpy.mockRestore();
    },
  );

  it("uses full-source ranges when cropping changes contextual sigma lowercase", async () => {
    const source = "A" + "\u0301".repeat(100) + "Σ";
    const snippet = "…" + "\u0301".repeat(60) + "Σ";
    const searchSpy = setupZetaSession(source);
    searchSpy.mockResolvedValue({
      hits: [
        {
          providerId: "codex",
          sourcePath: "/mock/codex/needle.jsonl",
          snippets: [3, 0].map((messageIndex) => ({
            messageIndex,
            text: snippet,
            highlights: [{ start: 61, end: 62 }],
          })),
          matchCount: 2,
        },
      ],
      status: { running: false, processed: 1, total: 1 },
    });
    renderPage("codex");
    await screen.findByText("Needle Session");
    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "ς" },
    });

    expect(source.toLowerCase()).toContain("ς");
    expect(snippet.toLowerCase()).not.toContain("ς");
    expect(await screen.findByText(/2 条消息命中/)).toBeInTheDocument();
    expect(
      Array.from(
        sessionList().querySelectorAll("mark"),
        (mark) => mark.textContent,
      ),
    ).toEqual(["Σ", "Σ"]);
    searchSpy.mockRestore();
  });

  it("syncs the text index again when an unchanged list is refreshed", async () => {
    const sync = vi.spyOn(sessionsApi, "syncContentIndex");
    renderPage("codex");
    await screen.findByText("Alpha Session");
    await waitFor(() => expect(sync).toHaveBeenCalledTimes(1));

    // 列表内容不变(正文可能变了):刷新后仍要再同步一次
    await userEvent.click(
      screen.getByRole("button", { name: "会话的更多操作" }),
    );
    await userEvent.click(
      await screen.findByRole("menuitem", { name: "刷新列表" }),
    );
    await waitFor(() => expect(sync).toHaveBeenCalledTimes(2));
    sync.mockRestore();
  });

  it("leaves message text out when content search is turned off", async () => {
    setSettings({ sessionContentSearchEnabled: false });
    renderPage("codex");
    await screen.findByText("Alpha Session");

    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "const a" },
    });

    expect(
      await screen.findByText("没有标题、目录或首末消息匹配“const a”的会话"),
    ).toBeInTheDocument();
  });

  it("reports a text index failure instead of claiming there are no matches", async () => {
    const searchSpy = vi
      .spyOn(sessionsApi, "searchContent")
      .mockRejectedValue(
        "Session index error: database disk image is malformed",
      );
    renderPage("codex");
    await screen.findByText("Alpha Session");

    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "const a" },
    });

    // 索引读不出来时不能声称「没有会话包含…」
    expect(
      await screen.findByText(
        "正文搜索失败,暂时只能按标题、目录和首末消息查找",
      ),
    ).toBeInTheDocument();
    expect(
      screen.queryByText("没有会话的标题、目录或消息正文包含“const a”"),
    ).not.toBeInTheDocument();
    expect(screen.getByRole("button", { name: "重试" })).toBeInTheDocument();
    searchSpy.mockRestore();
  });

  it("keeps title matches and explains the partial result when only text search fails", async () => {
    // 标题命中照常列出:正文索引坏了不该把整个搜索变成失败
    const searchSpy = vi
      .spyOn(sessionsApi, "searchContent")
      .mockRejectedValue(
        "Session index error: database disk image is malformed",
      );
    renderPage("codex");
    await screen.findByText("Alpha Session");

    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "Alpha" },
    });

    expect(
      await screen.findByText(
        "正文搜索失败,暂时只能按标题、目录和首末消息查找",
      ),
    ).toBeInTheDocument();
    // 标题命中仍在列表里(不是空状态),提示里带上具体原因
    expect(
      within(sessionList()).getAllByRole("button", { name: /Alpha Session/ })
        .length,
    ).toBeGreaterThan(0);
    expect(screen.getByText(/malformed/)).toBeInTheDocument();
    searchSpy.mockRestore();
  });

  it("keeps message text search off by default and offers it from an empty search", async () => {
    setSettings({ sessionContentSearchEnabled: undefined });
    const sync = vi.spyOn(sessionsApi, "syncContentIndex");
    const save = vi.spyOn(settingsApi, "save");
    renderPage("codex");
    await screen.findByText("Alpha Session");

    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "const a" },
    });
    expect(
      await screen.findByText("没有标题、目录或首末消息匹配“const a”的会话"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("开启后会在本地为消息正文建立索引"),
    ).toBeInTheDocument();
    // 没开启前不读正文、不建索引
    expect(sync).not.toHaveBeenCalled();

    await userEvent.click(screen.getByRole("button", { name: "搜索消息正文" }));
    await waitFor(() =>
      expect(save).toHaveBeenCalledWith(
        expect.objectContaining({ sessionContentSearchEnabled: true }),
      ),
    );
    await waitFor(() => expect(sync).toHaveBeenCalled());
    // 开启后不用重新输入,当前关键词直接搜到正文命中
    expect(await screen.findByText(/^正文:/)).toBeInTheDocument();
    expect(
      screen.queryByRole("button", { name: "搜索消息正文" }),
    ).not.toBeInTheDocument();
    sync.mockRestore();
    save.mockRestore();
  });

  it("switches to all apps and to time buckets", async () => {
    renderPage("codex");
    await screen.findByText("Alpha Session");

    await openAppMenu();
    const menu = await screen.findByRole("menu");
    const items = within(menu).getAllByRole("menuitemradio");
    // 全部应用 + 18 个来源,按会话数从多到少
    expect(items).toHaveLength(19);
    expect(items[0]).toHaveTextContent("全部应用5");
    expect(items[1]).toHaveTextContent("Codex3");
    expect(items[items.length - 1]).toHaveTextContent("无会话");
    await userEvent.click(items[0]);

    expect(await screen.findByText("Claude Session")).toBeInTheDocument();
    expect(screen.getByText("Codex 2")).toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "按时间" }));
    expect(
      within(sessionList()).getByRole("heading", { name: "今天" }),
    ).toBeInTheDocument();
    expect(
      within(sessionList()).getByRole("heading", { name: "更早" }),
    ).toBeInTheDocument();
  });

  it("only offers apps that are shown in the sidebar", async () => {
    setSettings({
      visibleApps: {
        claude: true,
        "claude-desktop": true,
        codex: true,
        gemini: false,
        grokbuild: false,
        opencode: false,
        openclaw: false,
        hermes: false,
        pi: false,
        mcode: false,
        kimi: false,
        dsh: false,
        agy: false,
        omp: false,
        copilot: false,
        workbuddy: false,
        codebuddy: false,
        zcode: false,
        qoder: false,
      },
    });
    renderPage("codex");
    await screen.findByText("Alpha Session");
    await waitFor(async () => {
      await openAppMenu();
      const menu = await screen.findByRole("menu");
      expect(within(menu).getAllByRole("menuitemradio")).toHaveLength(3);
    });
    const menu = screen.getByRole("menu");
    expect(within(menu).queryByText("Hermes")).not.toBeInTheDocument();
    // 隐藏应用的会话也不计入「全部应用」
    expect(within(menu).getAllByRole("menuitemradio")[0]).toHaveTextContent(
      "全部应用4",
    );
  });

  it("shows the Claude Desktop note when coming from Claude Desktop", async () => {
    renderPage("claude", { fromApp: "claude-desktop" });
    expect(
      await screen.findByText(
        "Claude Desktop 没有单独的会话记录,这里显示 Claude Code 的会话。",
      ),
    ).toBeInTheDocument();
  });

  it("filters by search and offers to clear an empty search", async () => {
    renderPage("codex");
    await screen.findByText("Alpha Session");

    const search = screen.getByRole("textbox", { name: "搜索会话" });
    fireEvent.change(search, { target: { value: "Beta" } });
    await waitFor(() =>
      expect(screen.queryByText("Alpha Session")).not.toBeInTheDocument(),
    );
    expect(screen.getByText("Beta")).toBeInTheDocument();

    fireEvent.change(search, { target: { value: "zzz" } });
    expect(
      await screen.findByText("没有会话的标题、目录或消息正文包含“zzz”"),
    ).toBeInTheDocument();
    // 已开启正文搜索时不再提供开启入口
    expect(
      screen.queryByRole("button", { name: "搜索消息正文" }),
    ).not.toBeInTheDocument();
    expect(
      screen.queryByText("开启后会在本地为消息正文建立索引"),
    ).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole("button", { name: "清除搜索" }));
    expect(await screen.findByText("Alpha Session")).toBeInTheDocument();
  });

  it("drills into a session, merges tool calls and steps between sessions", async () => {
    renderPage("codex");
    await screen.findByText("Alpha Session");

    openRow("Alpha Session");

    expect(
      await screen.findByRole("heading", { level: 1, name: "Alpha Session" }),
    ).toBeInTheDocument();
    // 返回和会话名并进页头:只剩一层页头,「会话」那条不再单独出现
    expect(screen.getAllByRole("heading", { level: 1 })).toHaveLength(1);
    expect(screen.getByRole("button", { name: "返回会话列表" })).toBeVisible();
    // 正文和右侧对话目录各出现一次
    expect(await screen.findAllByText("alpha question")).toHaveLength(2);
    expect(
      within(screen.getByRole("navigation", { name: "对话目录" })).getByText(
        "alpha question",
      ),
    ).toBeInTheDocument();
    expect(screen.getByText("const a = 1;")).toBeInTheDocument();
    // 工具调用和输出合并进执行过程,默认折叠成一行摘要;展开后能看到
    expect(screen.queryByText("shell")).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole("button", { name: /^展开执行过程/ }));
    expect(screen.getByText("shell")).toBeInTheDocument();
    // 列表藏起来了
    expect(screen.queryByRole("region", { name: "会话列表" })).toBeNull();

    // 只看对话:工具调用隐藏
    fireEvent.click(screen.getByRole("button", { name: "对话" }));
    expect(screen.queryByText("shell")).not.toBeInTheDocument();
    expect(screen.getAllByText("alpha question")).toHaveLength(2);

    fireEvent.click(screen.getByRole("button", { name: "下一个会话" }));
    expect(
      await screen.findByRole("heading", { level: 1, name: "Beta Session" }),
    ).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "上一个会话" })).toBeEnabled();

    fireEvent.click(screen.getByRole("button", { name: "返回会话列表" }));
    expect(await screen.findByText("Alpha Session")).toBeInTheDocument();
  });

  it("explains why a Hermes session cannot be resumed", async () => {
    renderPage("hermes");
    await screen.findByText("Hermes Session");
    openRow("Hermes Session");

    expect(
      await screen.findByText("暂不支持从命令行恢复 Hermes 会话"),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("button", { name: "复制恢复命令" }),
    ).toHaveAttribute("aria-disabled", "true");
    expect(
      screen.getByText("Hermes 只显示最近 500 个会话"),
    ).toBeInTheDocument();
  });

  it("shows the read error instead of an empty session", async () => {
    const spy = vi
      .spyOn(sessionsApi, "getMessages")
      .mockRejectedValue(new Error("no such column: created_at"));
    renderPage("codex");
    await screen.findByText("Alpha Session");
    openRow("Alpha Session");

    expect(await screen.findByText("无法读取这个会话")).toBeInTheDocument();
    expect(screen.getByText("no such column: created_at")).toBeInTheDocument();
    spy.mockRestore();
  });

  it("launches the resume command in the preferred terminal on macOS", async () => {
    platform.mac = true;
    setSettings({ preferredTerminal: "iterm2" });
    const launch = vi
      .spyOn(sessionsApi, "launchTerminal")
      .mockResolvedValue(true);
    renderPage("codex");
    await screen.findByText("Alpha Session");
    openRow("Alpha Session");

    const resume = await screen.findByRole("button", {
      name: /中恢复$/,
    });
    fireEvent.click(resume);

    await waitFor(() =>
      expect(launch).toHaveBeenCalledWith({
        command: "codex resume codex-session-1",
        cwd: "/mock/codex",
      }),
    );
    expect(toastSuccessMock).toHaveBeenCalled();
    launch.mockRestore();
  });

  it("copies the resume command where one-click resume is unavailable", async () => {
    renderPage("codex");
    await screen.findByText("Alpha Session");
    openRow("Alpha Session");

    fireEvent.click(
      await screen.findByRole("button", { name: "复制恢复命令" }),
    );
    await waitFor(() =>
      expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
        "codex resume codex-session-1",
      ),
    );
  });

  it("deletes the open session after confirming and returns to the list", async () => {
    renderPage("codex");
    await screen.findByText("Alpha Session");
    openRow("Alpha Session");
    await screen.findByRole("heading", { level: 1, name: "Alpha Session" });

    await userEvent.click(
      screen.getByRole("button", { name: "Alpha Session 的更多操作" }),
    );
    await userEvent.click(
      await screen.findByRole("menuitem", { name: "删除…" }),
    );

    const dialog = await screen.findByRole("alertdialog");
    expect(dialog).toHaveTextContent("删除会话「Alpha Session」?");
    expect(dialog).toHaveTextContent("只删除这一个会话文件");
    fireEvent.click(within(dialog).getByRole("button", { name: "删除会话" }));

    await waitFor(() =>
      expect(
        screen.queryByRole("heading", { level: 1, name: "Alpha Session" }),
      ).not.toBeInTheDocument(),
    );
    await waitFor(() =>
      expect(screen.queryByText("Alpha Session")).not.toBeInTheDocument(),
    );
    expect(screen.getByText("Beta Session")).toBeInTheDocument();
  });

  it("selects several sessions and deletes them in one batch", async () => {
    const deleteMany = vi.spyOn(sessionsApi, "deleteMany");
    renderPage("codex");
    await screen.findByText("Alpha Session");

    await userEvent.click(
      screen.getByRole("button", { name: "会话的更多操作" }),
    );
    await userEvent.click(
      await screen.findByRole("menuitem", { name: "选择多个…" }),
    );

    fireEvent.click(
      screen.getByRole("checkbox", { name: "选择 Alpha Session" }),
    );
    fireEvent.click(
      screen.getByRole("checkbox", { name: "选择 Beta Session" }),
    );
    expect(screen.getByText("已选 2 个会话")).toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "删除 2 个会话…" }));
    const dialog = await screen.findByRole("alertdialog");
    expect(dialog).toHaveTextContent("删除 2 个会话?");
    expect(dialog).toHaveTextContent("Codex 2 个");
    fireEvent.click(
      within(dialog).getByRole("button", { name: "删除 2 个会话" }),
    );

    await waitFor(() => expect(deleteMany).toHaveBeenCalledTimes(1));
    await waitFor(() =>
      expect(screen.queryByText("Alpha Session")).not.toBeInTheDocument(),
    );
    expect(screen.queryByText("Beta Session")).not.toBeInTheDocument();
    expect(screen.getByText("Gamma Session")).toBeInTheDocument();
    // 删完退出选择
    expect(screen.queryByText(/已选/)).not.toBeInTheDocument();
    deleteMany.mockRestore();
  });

  it("keeps the selection when the batch delete request fails", async () => {
    const deleteMany = vi
      .spyOn(sessionsApi, "deleteMany")
      .mockRejectedValue(new Error("boom"));
    renderPage("codex");
    await screen.findByText("Alpha Session");

    await userEvent.click(
      screen.getByRole("button", { name: "会话的更多操作" }),
    );
    await userEvent.click(
      await screen.findByRole("menuitem", { name: "选择多个…" }),
    );
    fireEvent.click(screen.getByRole("button", { name: "全选匹配结果" }));
    expect(screen.getByText("已选 3 个会话")).toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "删除 3 个会话…" }));
    fireEvent.click(
      within(await screen.findByRole("alertdialog")).getByRole("button", {
        name: "删除 3 个会话",
      }),
    );

    await waitFor(() => expect(toastErrorMock).toHaveBeenCalled());
    expect(screen.getByText("已选 3 个会话")).toBeInTheDocument();
    expect(
      screen.getByRole("button", { name: "删除 3 个会话…" }),
    ).toBeEnabled();
    deleteMany.mockRestore();
  });

  it("scopes search, selection and delete confirmation to the Codex archive status", async () => {
    const scrollIntoView = Element.prototype.scrollIntoView;
    Element.prototype.scrollIntoView ??= vi.fn();
    const bodyHit = vi.spyOn(sessionsApi, "searchContent").mockResolvedValue({
      hits: [
        {
          providerId: "codex",
          sourcePath: "/mock/codex/session-1.jsonl",
          matchCount: 1,
          snippets: [
            { messageIndex: 0, text: "archived_sessions", highlights: [] },
          ],
        },
      ],
      status: { running: false, processed: 3, total: 3 },
    });
    try {
      renderPage("codex");
      await screen.findByText("Alpha Session");
      fireEvent.click(
        screen.getAllByRole("checkbox", { name: "选择 Alpha Session" })[0],
      );
      const filter = screen.getByRole("combobox", { name: "归档状态" });
      await userEvent.click(filter);
      await userEvent.click(
        await screen.findByRole("option", { name: "未归档" }),
      );
      expect(screen.queryByText("Gamma Session")).not.toBeInTheDocument();
      expect(screen.getByText("已选 1 个会话")).toBeInTheDocument();
      await userEvent.click(filter);
      await userEvent.click(
        await screen.findByRole("option", { name: "已归档" }),
      );
      expect(screen.getByText("还没有选择会话")).toBeInTheDocument();
      fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
        target: { value: "archived_sessions" },
      });
      await waitFor(() => expect(bodyHit).toHaveBeenCalled());
      await screen.findByText("匹配 1 个会话");
      expect(
        within(sessionList()).queryByText("Alpha Session"),
      ).not.toBeInTheDocument();
      expect(
        within(sessionList()).getByText("Gamma Session"),
      ).toBeInTheDocument();
      fireEvent.click(screen.getByRole("button", { name: "全选匹配结果" }));
      fireEvent.click(screen.getByRole("button", { name: "删除 1 个会话…" }));
      const dialog = within(await screen.findByRole("alertdialog"));
      expect(
        dialog.getByText("archived_sessions").parentElement,
      ).toHaveTextContent("已归档 · archived_sessions · 已选 1 个会话");
      expect(dialog.getByRole("heading")).toHaveTextContent("Gamma Session");
    } finally {
      bodyHit.mockRestore();
      if (!scrollIntoView)
        Reflect.deleteProperty(Element.prototype, "scrollIntoView");
    }
  });

  it("drops hidden selections from the count and keeps search usable while selecting", async () => {
    renderPage("codex");
    await screen.findByText("Alpha Session");

    // 行首的复选框直接进入选择态
    fireEvent.click(
      screen.getAllByRole("checkbox", { name: "选择 Alpha Session" })[0],
    );
    expect(screen.getByText("已选 1 个会话")).toBeInTheDocument();

    fireEvent.change(screen.getByRole("textbox", { name: "搜索会话" }), {
      target: { value: "Beta" },
    });
    expect(await screen.findByText("匹配 1 个会话")).toBeInTheDocument();
    expect(screen.getByText(/其中 1 个不在当前结果里/)).toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "退出选择" }));
    expect(screen.queryByText(/已选/)).not.toBeInTheDocument();
  });
});