useWorkspaceTaskNavigation.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // @vitest-environment jsdom
  2. import { createElement, type ReactNode } from "react";
  3. import { act, renderHook } from "@testing-library/react";
  4. import type { IServiceAccessor } from "@zcode/services";
  5. import { beforeEach, describe, expect, it, vi } from "vitest";
  6. import { ServiceProvider } from "@/hooks/useServices.js";
  7. import { useWorkspaceTaskNavigation } from "@/app-shell/useWorkspaceTaskNavigation.js";
  8. import { createTaskNavigationHistory } from "@/lib/taskNavigationHistory.js";
  9. import { buildTaskEntityKey } from "@/lib/taskQueryCache.js";
  10. import { useTaskQueryCacheStore } from "@/store/taskQueryCacheStore.js";
  11. import { useRemoteWorkspaceSessionStore } from "@/store/remoteWorkspaceSessionStore.js";
  12. import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
  13. const tabStore = vi.hoisted(() => {
  14. let state: {
  15. activeTabId: string | null;
  16. tabs: Array<{
  17. id: string;
  18. kind: "workspace";
  19. label: string;
  20. workspacePath: string;
  21. workspaceIdentity?: string;
  22. remoteSessionId?: string;
  23. }>;
  24. } = { activeTabId: null, tabs: [] };
  25. return {
  26. api: { getState: () => state },
  27. setState: (next: typeof state) => {
  28. state = next;
  29. },
  30. };
  31. });
  32. vi.mock("@/store/TabStoreProvider.js", () => ({
  33. useTabStoreApi: () => tabStore.api,
  34. }));
  35. const localWorkspacePath = "/work/local";
  36. const taskId = "task-local";
  37. function taskMeta(params: { workspacePath?: string; workspaceIdentity?: string } = {}) {
  38. const workspacePath = params.workspacePath ?? localWorkspacePath;
  39. return {
  40. taskId,
  41. traceId: "trace-local",
  42. title: "local task",
  43. workspacePath,
  44. ...(params.workspaceIdentity ? { workspaceIdentity: params.workspaceIdentity } : {}),
  45. createdAt: 1,
  46. updatedAt: 2,
  47. mode: "default" as const,
  48. unreadAt: 2,
  49. };
  50. }
  51. function servicesWithSetTaskUnread(setTaskUnread: ReturnType<typeof vi.fn>): IServiceAccessor {
  52. return { zcodeTaskService: { setTaskUnread } } as unknown as IServiceAccessor;
  53. }
  54. function renderNavigation(params: {
  55. ambientServices: IServiceAccessor;
  56. activateTabByPath: (
  57. workspacePath: string,
  58. options?: { workspaceIdentity?: string },
  59. ) => boolean;
  60. workspacePath?: string;
  61. workspaceIdentity?: string;
  62. onNavigateToAutomations?: ReturnType<typeof vi.fn>;
  63. }) {
  64. const wrapper = ({ children }: { children: ReactNode }) =>
  65. createElement(ServiceProvider, { services: params.ambientServices }, children);
  66. return renderHook(
  67. () =>
  68. useWorkspaceTaskNavigation({
  69. intl: { formatMessage: ({ id }) => id },
  70. workspaceAbsPath: params.workspacePath ?? "/home/dev",
  71. workspaceIdentity:
  72. params.workspaceIdentity ?? "remote:ssh:dev:22:user:/home/dev",
  73. activateTabByPath: params.activateTabByPath,
  74. onNavigateToAutomations: params.onNavigateToAutomations,
  75. }),
  76. { wrapper },
  77. );
  78. }
  79. describe("useWorkspaceTaskNavigation", () => {
  80. beforeEach(() => {
  81. vi.clearAllMocks();
  82. tabStore.setState({ activeTabId: null, tabs: [] });
  83. useTaskQueryCacheStore.getState().clearAll();
  84. useRemoteWorkspaceSessionStore.setState({
  85. baseServices: null,
  86. sessionsById: {},
  87. sessionIdByWorkspacePath: {},
  88. sessionIdByWorkspaceIdentity: {},
  89. });
  90. useZCodeSessionStore.setState({ taskNavHistory: createTaskNavigationHistory() });
  91. });
  92. it("把 Idle-time intent 写入 Automations 导航目标和历史", () => {
  93. const onNavigateToAutomations = vi.fn();
  94. const hook = renderNavigation({
  95. ambientServices: servicesWithSetTaskUnread(vi.fn()),
  96. activateTabByPath: vi.fn(() => true),
  97. onNavigateToAutomations,
  98. });
  99. act(() => {
  100. hook.result.current.handleOpenAutomations(undefined, "idle");
  101. });
  102. expect(onNavigateToAutomations).toHaveBeenCalledWith({
  103. workspacePath: "/home/dev",
  104. workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
  105. automationTab: "idle",
  106. });
  107. expect(useZCodeSessionStore.getState().taskNavHistory.entries).toEqual([
  108. {
  109. kind: "automations",
  110. workspacePath: "/home/dev",
  111. workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
  112. automationTab: "idle",
  113. },
  114. ]);
  115. hook.unmount();
  116. });
  117. it("前进回放时恢复 Idle-time Automations intent", () => {
  118. const onNavigateToAutomations = vi.fn();
  119. const activateTabByPath = vi.fn(() => true);
  120. useZCodeSessionStore.setState({
  121. taskNavHistory: {
  122. entries: [
  123. {
  124. kind: "task",
  125. workspacePath: "/home/dev",
  126. workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
  127. taskId: "task-before-automations",
  128. },
  129. {
  130. kind: "automations",
  131. workspacePath: "/home/dev",
  132. workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
  133. automationTab: "idle",
  134. },
  135. ],
  136. cursor: 0,
  137. },
  138. });
  139. const hook = renderNavigation({
  140. ambientServices: servicesWithSetTaskUnread(vi.fn()),
  141. activateTabByPath,
  142. onNavigateToAutomations,
  143. });
  144. act(() => {
  145. hook.result.current.handleTaskNavForward();
  146. });
  147. expect(activateTabByPath).toHaveBeenCalledWith("/home/dev", {
  148. workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
  149. });
  150. expect(onNavigateToAutomations).toHaveBeenCalledWith({
  151. workspacePath: "/home/dev",
  152. workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
  153. automationTab: "idle",
  154. });
  155. hook.unmount();
  156. });
  157. it("从 remote workspace 选择本地 task 时通过 window base services 清除未读", async () => {
  158. const baseSetTaskUnread = vi.fn(async () => taskMeta());
  159. const staleRemoteSetTaskUnread = vi.fn(async () => taskMeta());
  160. const baseServices = servicesWithSetTaskUnread(baseSetTaskUnread);
  161. const staleRemoteServices = servicesWithSetTaskUnread(staleRemoteSetTaskUnread);
  162. useRemoteWorkspaceSessionStore.setState({ baseServices });
  163. useTaskQueryCacheStore.getState().upsertTaskMeta(taskMeta());
  164. tabStore.setState({
  165. activeTabId: "remote-a",
  166. tabs: [
  167. {
  168. id: "remote-a",
  169. kind: "workspace",
  170. label: "remote",
  171. workspacePath: "/home/dev",
  172. workspaceIdentity: "remote:ssh:dev:22:user:/home/dev",
  173. remoteSessionId: "remote-a",
  174. },
  175. ],
  176. });
  177. const activateTabByPath = vi.fn(() => {
  178. tabStore.setState({
  179. activeTabId: "local",
  180. tabs: [
  181. {
  182. id: "local",
  183. kind: "workspace",
  184. label: "local",
  185. workspacePath: localWorkspacePath,
  186. },
  187. ],
  188. });
  189. return true;
  190. });
  191. const hook = renderNavigation({
  192. ambientServices: staleRemoteServices,
  193. activateTabByPath,
  194. });
  195. await act(async () => {
  196. hook.result.current.handleSelectTask(localWorkspacePath, taskId);
  197. await Promise.resolve();
  198. });
  199. expect(baseSetTaskUnread).toHaveBeenCalledWith({
  200. taskId,
  201. workspacePath: localWorkspacePath,
  202. unread: false,
  203. expectedUnreadAt: 2,
  204. });
  205. expect(staleRemoteSetTaskUnread).not.toHaveBeenCalled();
  206. hook.unmount();
  207. });
  208. it("任务行未读但 query cache 尚未水合时仍按点击版本清除未读", async () => {
  209. const baseSetTaskUnread = vi.fn(async () => ({
  210. ...taskMeta(),
  211. unreadAt: undefined,
  212. }));
  213. const baseServices = servicesWithSetTaskUnread(baseSetTaskUnread);
  214. useRemoteWorkspaceSessionStore.setState({ baseServices });
  215. tabStore.setState({ activeTabId: null, tabs: [] });
  216. const activateTabByPath = vi.fn(() => {
  217. tabStore.setState({
  218. activeTabId: "local",
  219. tabs: [
  220. {
  221. id: "local",
  222. kind: "workspace",
  223. label: "local",
  224. workspacePath: localWorkspacePath,
  225. },
  226. ],
  227. });
  228. return true;
  229. });
  230. const hook = renderNavigation({
  231. ambientServices: baseServices,
  232. activateTabByPath,
  233. });
  234. await act(async () => {
  235. hook.result.current.handleSelectTask(
  236. localWorkspacePath,
  237. taskId,
  238. undefined,
  239. 321,
  240. );
  241. await Promise.resolve();
  242. });
  243. expect(useTaskQueryCacheStore.getState().taskMetaByEntityKey).toEqual({});
  244. expect(baseSetTaskUnread).toHaveBeenCalledWith({
  245. taskId,
  246. workspacePath: localWorkspacePath,
  247. unread: false,
  248. expectedUnreadAt: 321,
  249. });
  250. hook.unmount();
  251. });
  252. it("选择 remote task 时只使用目标 tab 的精确 remoteSessionId attachment", async () => {
  253. const targetWorkspacePath = "/home/shared";
  254. const targetWorkspaceIdentity = "remote:ssh:host-b:22:user:/home/shared";
  255. const targetSetTaskUnread = vi.fn(async () =>
  256. taskMeta({ workspacePath: targetWorkspacePath, workspaceIdentity: targetWorkspaceIdentity }),
  257. );
  258. const baseSetTaskUnread = vi.fn();
  259. const staleRemoteSetTaskUnread = vi.fn();
  260. const baseServices = servicesWithSetTaskUnread(baseSetTaskUnread);
  261. const targetServices = servicesWithSetTaskUnread(targetSetTaskUnread);
  262. const staleRemoteServices = servicesWithSetTaskUnread(staleRemoteSetTaskUnread);
  263. useRemoteWorkspaceSessionStore.setState({
  264. baseServices,
  265. sessionsById: {
  266. "remote-b": { sessionId: "remote-b", services: targetServices },
  267. },
  268. });
  269. useTaskQueryCacheStore
  270. .getState()
  271. .upsertTaskMeta(
  272. taskMeta({ workspacePath: targetWorkspacePath, workspaceIdentity: targetWorkspaceIdentity }),
  273. );
  274. tabStore.setState({
  275. activeTabId: "remote-a",
  276. tabs: [
  277. {
  278. id: "remote-a",
  279. kind: "workspace",
  280. label: "remote-a",
  281. workspacePath: "/home/shared",
  282. workspaceIdentity: "remote:ssh:host-a:22:user:/home/shared",
  283. remoteSessionId: "remote-a",
  284. },
  285. ],
  286. });
  287. const activateTabByPath = vi.fn(() => {
  288. tabStore.setState({
  289. activeTabId: "remote-b",
  290. tabs: [
  291. {
  292. id: "remote-b",
  293. kind: "workspace",
  294. label: "remote-b",
  295. workspacePath: targetWorkspacePath,
  296. workspaceIdentity: targetWorkspaceIdentity,
  297. remoteSessionId: "remote-b",
  298. },
  299. ],
  300. });
  301. return true;
  302. });
  303. const hook = renderNavigation({
  304. ambientServices: staleRemoteServices,
  305. activateTabByPath,
  306. workspacePath: "/home/shared",
  307. workspaceIdentity: "remote:ssh:host-a:22:user:/home/shared",
  308. });
  309. await act(async () => {
  310. hook.result.current.handleSelectTask(
  311. targetWorkspacePath,
  312. taskId,
  313. targetWorkspaceIdentity,
  314. );
  315. await Promise.resolve();
  316. });
  317. expect(targetSetTaskUnread).toHaveBeenCalledWith({
  318. taskId,
  319. workspacePath: targetWorkspacePath,
  320. workspaceIdentity: targetWorkspaceIdentity,
  321. unread: false,
  322. expectedUnreadAt: 2,
  323. });
  324. expect(baseSetTaskUnread).not.toHaveBeenCalled();
  325. expect(staleRemoteSetTaskUnread).not.toHaveBeenCalled();
  326. hook.unmount();
  327. });
  328. it("目标 remote attachment 已断开时回滚未读且不回退到 base", async () => {
  329. const targetWorkspacePath = "/home/shared";
  330. const targetWorkspaceIdentity = "remote:ssh:host-b:22:user:/home/shared";
  331. const baseSetTaskUnread = vi.fn();
  332. const staleRemoteSetTaskUnread = vi.fn();
  333. const baseServices = servicesWithSetTaskUnread(baseSetTaskUnread);
  334. const staleRemoteServices = servicesWithSetTaskUnread(staleRemoteSetTaskUnread);
  335. useRemoteWorkspaceSessionStore.setState({ baseServices });
  336. const remoteTask = taskMeta({
  337. workspacePath: targetWorkspacePath,
  338. workspaceIdentity: targetWorkspaceIdentity,
  339. });
  340. useTaskQueryCacheStore.getState().upsertTaskMeta(remoteTask);
  341. tabStore.setState({ activeTabId: null, tabs: [] });
  342. const activateTabByPath = vi.fn(() => {
  343. tabStore.setState({
  344. activeTabId: "remote-b",
  345. tabs: [
  346. {
  347. id: "remote-b",
  348. kind: "workspace",
  349. label: "remote-b",
  350. workspacePath: targetWorkspacePath,
  351. workspaceIdentity: targetWorkspaceIdentity,
  352. remoteSessionId: "remote-b-disconnected",
  353. },
  354. ],
  355. });
  356. return true;
  357. });
  358. const hook = renderNavigation({
  359. ambientServices: staleRemoteServices,
  360. activateTabByPath,
  361. workspacePath: "/home/shared",
  362. workspaceIdentity: "remote:ssh:host-a:22:user:/home/shared",
  363. });
  364. act(() => {
  365. hook.result.current.handleSelectTask(
  366. targetWorkspacePath,
  367. taskId,
  368. targetWorkspaceIdentity,
  369. );
  370. });
  371. expect(baseSetTaskUnread).not.toHaveBeenCalled();
  372. expect(staleRemoteSetTaskUnread).not.toHaveBeenCalled();
  373. expect(
  374. useTaskQueryCacheStore.getState().taskMetaByEntityKey[buildTaskEntityKey(remoteTask)]?.unreadAt,
  375. ).toBe(2);
  376. hook.unmount();
  377. });
  378. });