useWebRemoteControlTaskOpen.test.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { WebRemoteControlTaskTarget } from "@zcode/shared";
  3. import { openWebRemoteControlTask } from "@/hooks/useWebRemoteControlTaskOpen.js";
  4. function createTask(overrides: Partial<WebRemoteControlTaskTarget>): WebRemoteControlTaskTarget {
  5. return {
  6. taskId: overrides.taskId ?? "task-1",
  7. title: overrides.title ?? "Task 1",
  8. workspacePath: overrides.workspacePath ?? "/workspace/app",
  9. workspaceIdentity: overrides.workspaceIdentity,
  10. workspaceLabel: overrides.workspaceLabel ?? "App",
  11. workspaceKind: overrides.workspaceKind ?? "local",
  12. createdAt: overrides.createdAt ?? 1,
  13. updatedAt: overrides.updatedAt ?? 2,
  14. unreadAt: overrides.unreadAt,
  15. };
  16. }
  17. describe("openWebRemoteControlTask", () => {
  18. it("MRO01:结构化 bridge 失败展示 message,保留原任务并解除等待", async () => {
  19. const failure = { reason: "unexpected-error", message: "project.task-directory-mismatch" };
  20. const setError = vi.fn();
  21. const onSelectTask = vi.fn();
  22. const onSwitchingChange = vi.fn();
  23. await openWebRemoteControlTask({
  24. activeWorkspacePath: "/workspace/local",
  25. activeTaskId: "local-task",
  26. task: createTask({
  27. taskId: "remote-task",
  28. workspacePath: "/workspace/remote",
  29. workspaceIdentity: "remote:server:source:/workspace/remote",
  30. workspaceKind: "remote",
  31. }),
  32. onSelectTask,
  33. onSwitchingChange,
  34. setError,
  35. switcher: {
  36. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  37. switchWorkspace: vi.fn(async () => {
  38. throw failure;
  39. }),
  40. },
  41. });
  42. expect(setError).toHaveBeenLastCalledWith(failure.message);
  43. expect(onSelectTask).not.toHaveBeenCalled();
  44. expect(onSwitchingChange.mock.calls).toEqual([[true], [false]]);
  45. expect(failure).toEqual({
  46. reason: "unexpected-error",
  47. message: "project.task-directory-mismatch",
  48. });
  49. });
  50. it("marks a same-workspace unread task as read before selecting it", async () => {
  51. const task = createTask({ taskId: "task-unread", unreadAt: 123 });
  52. const events: string[] = [];
  53. const markTaskRead = vi.fn(async () => {
  54. events.push("mark-read");
  55. });
  56. const onSelectTask = vi.fn(() => {
  57. events.push("select");
  58. });
  59. await openWebRemoteControlTask({
  60. activeWorkspacePath: "/workspace/app",
  61. activeTaskId: null,
  62. markTaskReadOnOpen: true,
  63. onSelectTask,
  64. switcher: {
  65. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  66. markTaskRead,
  67. switchWorkspace: vi.fn(async () => {}),
  68. },
  69. task,
  70. });
  71. expect(markTaskRead).toHaveBeenCalledWith(task);
  72. expect(events).toEqual(["mark-read", "select"]);
  73. });
  74. it("keeps non-mobile task-open callers on their existing unread path", async () => {
  75. const markTaskRead = vi.fn(async () => {});
  76. const onSelectTask = vi.fn();
  77. await openWebRemoteControlTask({
  78. activeWorkspacePath: "/workspace/app",
  79. activeTaskId: null,
  80. onSelectTask,
  81. switcher: {
  82. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  83. markTaskRead,
  84. switchWorkspace: vi.fn(async () => {}),
  85. },
  86. task: createTask({ taskId: "task-unread", unreadAt: 123 }),
  87. });
  88. expect(markTaskRead).not.toHaveBeenCalled();
  89. expect(onSelectTask).toHaveBeenCalledWith("/workspace/app", "task-unread", undefined);
  90. });
  91. it("does not wait for the mobile read mutation before opening the task", async () => {
  92. let finishMarkingRead!: () => void;
  93. const markTaskRead = vi.fn(
  94. () =>
  95. new Promise<void>((resolve) => {
  96. finishMarkingRead = resolve;
  97. }),
  98. );
  99. const onSelectTask = vi.fn();
  100. const opening = openWebRemoteControlTask({
  101. activeWorkspacePath: "/workspace/app",
  102. activeTaskId: null,
  103. markTaskReadOnOpen: true,
  104. onSelectTask,
  105. switcher: {
  106. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  107. markTaskRead,
  108. switchWorkspace: vi.fn(async () => {}),
  109. },
  110. task: createTask({ taskId: "task-unread", unreadAt: 123 }),
  111. });
  112. expect(onSelectTask).toHaveBeenCalledWith("/workspace/app", "task-unread", undefined);
  113. finishMarkingRead();
  114. await opening;
  115. });
  116. it("selects same-workspace tasks, updates mobile view state, and navigates to chat", async () => {
  117. const onSelectTask = vi.fn();
  118. const onNavigateToChat = vi.fn();
  119. const updateMobileViewState = vi.fn(async () => {});
  120. await openWebRemoteControlTask({
  121. activeWorkspacePath: "/workspace/app",
  122. activeTaskId: null,
  123. onNavigateToChat,
  124. onSelectTask,
  125. switcher: {
  126. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  127. switchWorkspace: vi.fn(async () => {}),
  128. updateMobileViewState,
  129. },
  130. task: createTask({ taskId: "task-1" }),
  131. });
  132. expect(onSelectTask).toHaveBeenCalledWith("/workspace/app", "task-1", undefined);
  133. expect(updateMobileViewState).toHaveBeenCalledWith("/workspace/app", "task-1");
  134. expect(onNavigateToChat).toHaveBeenCalledTimes(1);
  135. });
  136. it("switches cross-workspace tasks with a mobile chat navigation intent", async () => {
  137. const switchWorkspace = vi.fn(async () => {});
  138. const onSwitchingChange = vi.fn();
  139. await openWebRemoteControlTask({
  140. activeWorkspacePath: "/workspace/current",
  141. activeTaskId: null,
  142. markTaskReadOnOpen: true,
  143. mobileNavigationIntent: "chat",
  144. onSelectTask: vi.fn(),
  145. onSwitchingChange,
  146. setSwitchingTaskKey: vi.fn(),
  147. switcher: {
  148. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  149. switchWorkspace,
  150. },
  151. task: createTask({
  152. taskId: "remote-task",
  153. workspacePath: "/workspace/remote",
  154. workspaceIdentity: "ssh://host/workspace/remote",
  155. workspaceKind: "remote",
  156. }),
  157. });
  158. expect(onSwitchingChange).toHaveBeenCalledWith(true);
  159. expect(switchWorkspace).toHaveBeenCalledWith("ssh://host/workspace/remote", {
  160. taskId: "remote-task",
  161. mobileNavigationIntent: "chat",
  162. });
  163. });
  164. it("carries an unread-clear intent to the target bridge even from a home-only root", async () => {
  165. const switchWorkspace = vi.fn(async () => {});
  166. await openWebRemoteControlTask({
  167. activeWorkspacePath: "/workspace/current",
  168. activeTaskId: null,
  169. markTaskReadOnOpen: true,
  170. mobileNavigationIntent: "chat",
  171. onSelectTask: vi.fn(),
  172. switcher: {
  173. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  174. switchWorkspace,
  175. },
  176. task: createTask({
  177. taskId: "remote-unread-task",
  178. unreadAt: 123,
  179. workspacePath: "/workspace/remote",
  180. workspaceIdentity: "ssh://host/workspace/remote",
  181. workspaceKind: "remote",
  182. }),
  183. });
  184. expect(switchWorkspace).toHaveBeenCalledWith("ssh://host/workspace/remote", {
  185. taskId: "remote-unread-task",
  186. mobileNavigationIntent: "chat",
  187. markTaskReadExpectedUnreadAt: 123,
  188. });
  189. });
  190. it("navigates home when a remote workspace bridge fails because it is disconnected", async () => {
  191. const setError = vi.fn();
  192. const onNavigateHome = vi.fn();
  193. await openWebRemoteControlTask({
  194. activeWorkspacePath: "/workspace/current",
  195. activeTaskId: null,
  196. mobileNavigationIntent: "chat",
  197. onNavigateHome,
  198. onSelectTask: vi.fn(),
  199. setError,
  200. switcher: {
  201. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  202. switchWorkspace: vi.fn(async () => {
  203. throw new Error("目标远程工作区尚未连接,无法创建 bridge,请先重连。");
  204. }),
  205. },
  206. task: createTask({
  207. taskId: "remote-task",
  208. workspacePath: "/workspace/remote",
  209. workspaceIdentity: "ssh://host/workspace/remote",
  210. workspaceKind: "remote",
  211. }),
  212. });
  213. expect(onNavigateHome).toHaveBeenCalledTimes(1);
  214. expect(setError).not.toHaveBeenCalledWith(
  215. "目标远程工作区尚未连接,无法创建 bridge,请先重连。",
  216. );
  217. });
  218. it("clears switching state and stores the error when cross-workspace switching fails", async () => {
  219. const setError = vi.fn();
  220. const setSwitchingTaskKey = vi.fn();
  221. const onSwitchingChange = vi.fn();
  222. await openWebRemoteControlTask({
  223. activeWorkspacePath: "/workspace/current",
  224. activeTaskId: null,
  225. mobileNavigationIntent: "chat",
  226. onSelectTask: vi.fn(),
  227. onSwitchingChange,
  228. setError,
  229. setSwitchingTaskKey,
  230. switcher: {
  231. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  232. switchWorkspace: vi.fn(async () => {
  233. throw new Error("bridge failed");
  234. }),
  235. },
  236. task: createTask({
  237. taskId: "remote-task",
  238. workspacePath: "/workspace/remote",
  239. }),
  240. });
  241. expect(setSwitchingTaskKey).toHaveBeenNthCalledWith(1, "/workspace/remote:remote-task");
  242. expect(setSwitchingTaskKey).toHaveBeenNthCalledWith(2, null);
  243. expect(setError).toHaveBeenCalledWith("bridge failed");
  244. expect(onSwitchingChange).toHaveBeenNthCalledWith(1, true);
  245. expect(onSwitchingChange).toHaveBeenNthCalledWith(2, false);
  246. });
  247. });