| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- // @vitest-environment jsdom
- import { createElement, type ReactNode } from "react";
- import { act, renderHook } from "@testing-library/react";
- import type { IServiceAccessor } from "@zcode/services";
- import { beforeEach, describe, expect, it, vi } from "vitest";
- import { ServiceProvider } from "@/hooks/useServices.js";
- import { useWorkspaceTaskNavigation } from "@/app-shell/useWorkspaceTaskNavigation.js";
- import { createTaskNavigationHistory } from "@/lib/taskNavigationHistory.js";
- import { buildTaskEntityKey } from "@/lib/taskQueryCache.js";
- import { useTaskQueryCacheStore } from "@/store/taskQueryCacheStore.js";
- import { useRemoteWorkspaceSessionStore } from "@/store/remoteWorkspaceSessionStore.js";
- import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
- const tabStore = vi.hoisted(() => {
- let state: {
- activeTabId: string | null;
- tabs: Array<{
- id: string;
- kind: "workspace";
- label: string;
- workspacePath: string;
- workspaceIdentity?: string;
- remoteSessionId?: string;
- }>;
- } = { activeTabId: null, tabs: [] };
- return {
- api: { getState: () => state },
- setState: (next: typeof state) => {
- state = next;
- },
- };
- });
- vi.mock("@/store/TabStoreProvider.js", () => ({
- useTabStoreApi: () => tabStore.api,
- }));
- const localWorkspacePath = "/work/local";
- const taskId = "task-local";
- function taskMeta(params: { workspacePath?: string; workspaceIdentity?: string } = {}) {
- const workspacePath = params.workspacePath ?? localWorkspacePath;
- return {
- taskId,
- traceId: "trace-local",
- title: "local task",
- workspacePath,
- ...(params.workspaceIdentity ? { workspaceIdentity: params.workspaceIdentity } : {}),
- createdAt: 1,
- updatedAt: 2,
- mode: "default" as const,
- unreadAt: 2,
- };
- }
- function servicesWithSetTaskUnread(setTaskUnread: ReturnType<typeof vi.fn>): IServiceAccessor {
- return { zcodeTaskService: { setTaskUnread } } as unknown as IServiceAccessor;
- }
- function renderNavigation(params: {
- ambientServices: IServiceAccessor;
- activateTabByPath: (
- workspacePath: string,
- options?: { workspaceIdentity?: string },
- ) => boolean;
- workspacePath?: string;
- workspaceIdentity?: string;
- onNavigateToAutomations?: ReturnType<typeof vi.fn>;
- }) {
- const wrapper = ({ children }: { children: ReactNode }) =>
- createElement(ServiceProvider, { services: params.ambientServices }, children);
- return renderHook(
- () =>
- useWorkspaceTaskNavigation({
- intl: { formatMessage: ({ id }) => id },
- workspaceAbsPath: params.workspacePath ?? "/home/dev",
- workspaceIdentity:
- params.workspaceIdentity ?? "remote:ssh:dev:22:user:/home/dev",
- activateTabByPath: params.activateTabByPath,
- onNavigateToAutomations: params.onNavigateToAutomations,
- }),
- { wrapper },
- );
- }
- describe("useWorkspaceTaskNavigation", () => {
- beforeEach(() => {
- vi.clearAllMocks();
- tabStore.setState({ activeTabId: null, tabs: [] });
- useTaskQueryCacheStore.getState().clearAll();
- useRemoteWorkspaceSessionStore.setState({
- baseServices: null,
- sessionsById: {},
- sessionIdByWorkspacePath: {},
- sessionIdByWorkspaceIdentity: {},
- });
- useZCodeSessionStore.setState({ taskNavHistory: createTaskNavigationHistory() });
- });
- it("把 Idle-time intent 写入 Automations 导航目标和历史", () => {
- const onNavigateToAutomations = vi.fn();
- const hook = renderNavigation({
- ambientServices: servicesWithSetTaskUnread(vi.fn()),
- activateTabByPath: vi.fn(() => true),
- onNavigateToAutomations,
- });
- act(() => {
- hook.result.current.handleOpenAutomations(undefined, "idle");
- });
- expect(onNavigateToAutomations).toHaveBeenCalledWith({
- workspacePath: "/home/dev",
- workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
- automationTab: "idle",
- });
- expect(useZCodeSessionStore.getState().taskNavHistory.entries).toEqual([
- {
- kind: "automations",
- workspacePath: "/home/dev",
- workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
- automationTab: "idle",
- },
- ]);
- hook.unmount();
- });
- it("前进回放时恢复 Idle-time Automations intent", () => {
- const onNavigateToAutomations = vi.fn();
- const activateTabByPath = vi.fn(() => true);
- useZCodeSessionStore.setState({
- taskNavHistory: {
- entries: [
- {
- kind: "task",
- workspacePath: "/home/dev",
- workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
- taskId: "task-before-automations",
- },
- {
- kind: "automations",
- workspacePath: "/home/dev",
- workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
- automationTab: "idle",
- },
- ],
- cursor: 0,
- },
- });
- const hook = renderNavigation({
- ambientServices: servicesWithSetTaskUnread(vi.fn()),
- activateTabByPath,
- onNavigateToAutomations,
- });
- act(() => {
- hook.result.current.handleTaskNavForward();
- });
- expect(activateTabByPath).toHaveBeenCalledWith("/home/dev", {
- workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
- });
- expect(onNavigateToAutomations).toHaveBeenCalledWith({
- workspacePath: "/home/dev",
- workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
- automationTab: "idle",
- });
- hook.unmount();
- });
- it("从 remote workspace 选择本地 task 时通过 window base services 清除未读", async () => {
- const baseSetTaskUnread = vi.fn(async () => taskMeta());
- const staleRemoteSetTaskUnread = vi.fn(async () => taskMeta());
- const baseServices = servicesWithSetTaskUnread(baseSetTaskUnread);
- const staleRemoteServices = servicesWithSetTaskUnread(staleRemoteSetTaskUnread);
- useRemoteWorkspaceSessionStore.setState({ baseServices });
- useTaskQueryCacheStore.getState().upsertTaskMeta(taskMeta());
- tabStore.setState({
- activeTabId: "remote-a",
- tabs: [
- {
- id: "remote-a",
- kind: "workspace",
- label: "remote",
- workspacePath: "/home/dev",
- workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
- remoteSessionId: "remote-a",
- },
- ],
- });
- const activateTabByPath = vi.fn(() => {
- tabStore.setState({
- activeTabId: "local",
- tabs: [
- {
- id: "local",
- kind: "workspace",
- label: "local",
- workspacePath: localWorkspacePath,
- },
- ],
- });
- return true;
- });
- const hook = renderNavigation({
- ambientServices: staleRemoteServices,
- activateTabByPath,
- });
- await act(async () => {
- hook.result.current.handleSelectTask(localWorkspacePath, taskId);
- await Promise.resolve();
- });
- expect(baseSetTaskUnread).toHaveBeenCalledWith({
- taskId,
- workspacePath: localWorkspacePath,
- unread: false,
- expectedUnreadAt: 2,
- });
- expect(staleRemoteSetTaskUnread).not.toHaveBeenCalled();
- hook.unmount();
- });
- it("任务行未读但 query cache 尚未水合时仍按点击版本清除未读", async () => {
- const baseSetTaskUnread = vi.fn(async () => ({
- ...taskMeta(),
- unreadAt: undefined,
- }));
- const baseServices = servicesWithSetTaskUnread(baseSetTaskUnread);
- useRemoteWorkspaceSessionStore.setState({ baseServices });
- tabStore.setState({ activeTabId: null, tabs: [] });
- const activateTabByPath = vi.fn(() => {
- tabStore.setState({
- activeTabId: "local",
- tabs: [
- {
- id: "local",
- kind: "workspace",
- label: "local",
- workspacePath: localWorkspacePath,
- },
- ],
- });
- return true;
- });
- const hook = renderNavigation({
- ambientServices: baseServices,
- activateTabByPath,
- });
- await act(async () => {
- hook.result.current.handleSelectTask(
- localWorkspacePath,
- taskId,
- undefined,
- 321,
- );
- await Promise.resolve();
- });
- expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey).toEqual({});
- expect(baseSetTaskUnread).toHaveBeenCalledWith({
- taskId,
- workspacePath: localWorkspacePath,
- unread: false,
- expectedUnreadAt: 321,
- });
- hook.unmount();
- });
- it("选择 remote task 时只使用目标 tab 的精确 remoteSessionId attachment", async () => {
- const targetWorkspacePath = "/home/shared";
- const targetWorkspaceIdentity = "remote:ssh:host-b:22:user:/home/shared";
- const targetSetTaskUnread = vi.fn(async () =>
- taskMeta({ workspacePath: targetWorkspacePath, workspaceIdentity: targetWorkspaceIdentity }),
- );
- const baseSetTaskUnread = vi.fn();
- const staleRemoteSetTaskUnread = vi.fn();
- const baseServices = servicesWithSetTaskUnread(baseSetTaskUnread);
- const targetServices = servicesWithSetTaskUnread(targetSetTaskUnread);
- const staleRemoteServices = servicesWithSetTaskUnread(staleRemoteSetTaskUnread);
- useRemoteWorkspaceSessionStore.setState({
- baseServices,
- sessionsById: {
- "remote-b": { sessionId: "remote-b", services: targetServices },
- },
- });
- useTaskQueryCacheStore
- .getState()
- .upsertTaskMeta(
- taskMeta({ workspacePath: targetWorkspacePath, workspaceIdentity: targetWorkspaceIdentity }),
- );
- tabStore.setState({
- activeTabId: "remote-a",
- tabs: [
- {
- id: "remote-a",
- kind: "workspace",
- label: "remote-a",
- workspacePath: "/home/shared",
- workspaceIdentity: "remote:ssh:host-a:22:user:/home/shared",
- remoteSessionId: "remote-a",
- },
- ],
- });
- const activateTabByPath = vi.fn(() => {
- tabStore.setState({
- activeTabId: "remote-b",
- tabs: [
- {
- id: "remote-b",
- kind: "workspace",
- label: "remote-b",
- workspacePath: targetWorkspacePath,
- workspaceIdentity: targetWorkspaceIdentity,
- remoteSessionId: "remote-b",
- },
- ],
- });
- return true;
- });
- const hook = renderNavigation({
- ambientServices: staleRemoteServices,
- activateTabByPath,
- workspacePath: "/home/shared",
- workspaceIdentity: "remote:ssh:host-a:22:user:/home/shared",
- });
- await act(async () => {
- hook.result.current.handleSelectTask(
- targetWorkspacePath,
- taskId,
- targetWorkspaceIdentity,
- );
- await Promise.resolve();
- });
- expect(targetSetTaskUnread).toHaveBeenCalledWith({
- taskId,
- workspacePath: targetWorkspacePath,
- workspaceIdentity: targetWorkspaceIdentity,
- unread: false,
- expectedUnreadAt: 2,
- });
- expect(baseSetTaskUnread).not.toHaveBeenCalled();
- expect(staleRemoteSetTaskUnread).not.toHaveBeenCalled();
- hook.unmount();
- });
- it("目标 remote attachment 已断开时回滚未读且不回退到 base", async () => {
- const targetWorkspacePath = "/home/shared";
- const targetWorkspaceIdentity = "remote:ssh:host-b:22:user:/home/shared";
- const baseSetTaskUnread = vi.fn();
- const staleRemoteSetTaskUnread = vi.fn();
- const baseServices = servicesWithSetTaskUnread(baseSetTaskUnread);
- const staleRemoteServices = servicesWithSetTaskUnread(staleRemoteSetTaskUnread);
- useRemoteWorkspaceSessionStore.setState({ baseServices });
- const remoteTask = taskMeta({
- workspacePath: targetWorkspacePath,
- workspaceIdentity: targetWorkspaceIdentity,
- });
- useTaskQueryCacheStore.getState().upsertTaskMeta(remoteTask);
- tabStore.setState({ activeTabId: null, tabs: [] });
- const activateTabByPath = vi.fn(() => {
- tabStore.setState({
- activeTabId: "remote-b",
- tabs: [
- {
- id: "remote-b",
- kind: "workspace",
- label: "remote-b",
- workspacePath: targetWorkspacePath,
- workspaceIdentity: targetWorkspaceIdentity,
- remoteSessionId: "remote-b-disconnected",
- },
- ],
- });
- return true;
- });
- const hook = renderNavigation({
- ambientServices: staleRemoteServices,
- activateTabByPath,
- workspacePath: "/home/shared",
- workspaceIdentity: "remote:ssh:host-a:22:user:/home/shared",
- });
- act(() => {
- hook.result.current.handleSelectTask(
- targetWorkspacePath,
- taskId,
- targetWorkspaceIdentity,
- );
- });
- expect(baseSetTaskUnread).not.toHaveBeenCalled();
- expect(staleRemoteSetTaskUnread).not.toHaveBeenCalled();
- expect(
- useTaskQueryCacheStore.getState().taskMetaByEntityKey[buildTaskEntityKey(remoteTask)]?.unreadAt,
- ).toBe(2);
- hook.unmount();
- });
- });
|