useWebRemoteControlTaskOpen.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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("marks a same-workspace unread task as read before selecting it", async () => {
  19. const task = createTask({ taskId: "task-unread", unreadAt: 123 });
  20. const events: string[] = [];
  21. const markTaskRead = vi.fn(async () => {
  22. events.push("mark-read");
  23. });
  24. const onSelectTask = vi.fn(() => {
  25. events.push("select");
  26. });
  27. await openWebRemoteControlTask({
  28. activeWorkspacePath: "/workspace/app",
  29. activeTaskId: null,
  30. markTaskReadOnOpen: true,
  31. onSelectTask,
  32. switcher: {
  33. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  34. markTaskRead,
  35. switchWorkspace: vi.fn(async () => {}),
  36. },
  37. task,
  38. });
  39. expect(markTaskRead).toHaveBeenCalledWith(task);
  40. expect(events).toEqual(["mark-read", "select"]);
  41. });
  42. it("keeps non-mobile task-open callers on their existing unread path", async () => {
  43. const markTaskRead = vi.fn(async () => {});
  44. const onSelectTask = vi.fn();
  45. await openWebRemoteControlTask({
  46. activeWorkspacePath: "/workspace/app",
  47. activeTaskId: null,
  48. onSelectTask,
  49. switcher: {
  50. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  51. markTaskRead,
  52. switchWorkspace: vi.fn(async () => {}),
  53. },
  54. task: createTask({ taskId: "task-unread", unreadAt: 123 }),
  55. });
  56. expect(markTaskRead).not.toHaveBeenCalled();
  57. expect(onSelectTask).toHaveBeenCalledWith("/workspace/app", "task-unread", undefined);
  58. });
  59. it("does not wait for the mobile read mutation before opening the task", async () => {
  60. let finishMarkingRead!: () => void;
  61. const markTaskRead = vi.fn(
  62. () =>
  63. new Promise<void>((resolve) => {
  64. finishMarkingRead = resolve;
  65. }),
  66. );
  67. const onSelectTask = vi.fn();
  68. const opening = openWebRemoteControlTask({
  69. activeWorkspacePath: "/workspace/app",
  70. activeTaskId: null,
  71. markTaskReadOnOpen: true,
  72. onSelectTask,
  73. switcher: {
  74. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  75. markTaskRead,
  76. switchWorkspace: vi.fn(async () => {}),
  77. },
  78. task: createTask({ taskId: "task-unread", unreadAt: 123 }),
  79. });
  80. expect(onSelectTask).toHaveBeenCalledWith("/workspace/app", "task-unread", undefined);
  81. finishMarkingRead();
  82. await opening;
  83. });
  84. it("selects same-workspace tasks, updates mobile view state, and navigates to chat", async () => {
  85. const onSelectTask = vi.fn();
  86. const onNavigateToChat = vi.fn();
  87. const updateMobileViewState = vi.fn(async () => {});
  88. await openWebRemoteControlTask({
  89. activeWorkspacePath: "/workspace/app",
  90. activeTaskId: null,
  91. onNavigateToChat,
  92. onSelectTask,
  93. switcher: {
  94. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  95. switchWorkspace: vi.fn(async () => {}),
  96. updateMobileViewState,
  97. },
  98. task: createTask({ taskId: "task-1" }),
  99. });
  100. expect(onSelectTask).toHaveBeenCalledWith("/workspace/app", "task-1", undefined);
  101. expect(updateMobileViewState).toHaveBeenCalledWith("/workspace/app", "task-1");
  102. expect(onNavigateToChat).toHaveBeenCalledTimes(1);
  103. });
  104. it("switches cross-workspace tasks with a mobile chat navigation intent", async () => {
  105. const switchWorkspace = vi.fn(async () => {});
  106. const onSwitchingChange = vi.fn();
  107. await openWebRemoteControlTask({
  108. activeWorkspacePath: "/workspace/current",
  109. activeTaskId: null,
  110. markTaskReadOnOpen: true,
  111. mobileNavigationIntent: "chat",
  112. onSelectTask: vi.fn(),
  113. onSwitchingChange,
  114. setSwitchingTaskKey: vi.fn(),
  115. switcher: {
  116. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  117. switchWorkspace,
  118. },
  119. task: createTask({
  120. taskId: "remote-task",
  121. workspacePath: "/workspace/remote",
  122. workspaceIdentity: "ssh://host/workspace/remote",
  123. workspaceKind: "remote",
  124. }),
  125. });
  126. expect(onSwitchingChange).toHaveBeenCalledWith(true);
  127. expect(switchWorkspace).toHaveBeenCalledWith("ssh://host/workspace/remote", {
  128. taskId: "remote-task",
  129. mobileNavigationIntent: "chat",
  130. });
  131. });
  132. it("carries an unread-clear intent to the target bridge even from a home-only root", async () => {
  133. const switchWorkspace = vi.fn(async () => {});
  134. await openWebRemoteControlTask({
  135. activeWorkspacePath: "/workspace/current",
  136. activeTaskId: null,
  137. markTaskReadOnOpen: true,
  138. mobileNavigationIntent: "chat",
  139. onSelectTask: vi.fn(),
  140. switcher: {
  141. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  142. switchWorkspace,
  143. },
  144. task: createTask({
  145. taskId: "remote-unread-task",
  146. unreadAt: 123,
  147. workspacePath: "/workspace/remote",
  148. workspaceIdentity: "ssh://host/workspace/remote",
  149. workspaceKind: "remote",
  150. }),
  151. });
  152. expect(switchWorkspace).toHaveBeenCalledWith("ssh://host/workspace/remote", {
  153. taskId: "remote-unread-task",
  154. mobileNavigationIntent: "chat",
  155. markTaskReadExpectedUnreadAt: 123,
  156. });
  157. });
  158. it("navigates home when a remote workspace bridge fails because it is disconnected", async () => {
  159. const setError = vi.fn();
  160. const onNavigateHome = vi.fn();
  161. await openWebRemoteControlTask({
  162. activeWorkspacePath: "/workspace/current",
  163. activeTaskId: null,
  164. mobileNavigationIntent: "chat",
  165. onNavigateHome,
  166. onSelectTask: vi.fn(),
  167. setError,
  168. switcher: {
  169. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  170. switchWorkspace: vi.fn(async () => {
  171. throw new Error("目标远程工作区尚未连接,无法创建 bridge,请先重连。");
  172. }),
  173. },
  174. task: createTask({
  175. taskId: "remote-task",
  176. workspacePath: "/workspace/remote",
  177. workspaceIdentity: "ssh://host/workspace/remote",
  178. workspaceKind: "remote",
  179. }),
  180. });
  181. expect(onNavigateHome).toHaveBeenCalledTimes(1);
  182. expect(setError).not.toHaveBeenCalledWith("目标远程工作区尚未连接,无法创建 bridge,请先重连。");
  183. });
  184. it("clears switching state and stores the error when cross-workspace switching fails", async () => {
  185. const setError = vi.fn();
  186. const setSwitchingTaskKey = vi.fn();
  187. const onSwitchingChange = vi.fn();
  188. await openWebRemoteControlTask({
  189. activeWorkspacePath: "/workspace/current",
  190. activeTaskId: null,
  191. mobileNavigationIntent: "chat",
  192. onSelectTask: vi.fn(),
  193. onSwitchingChange,
  194. setError,
  195. setSwitchingTaskKey,
  196. switcher: {
  197. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  198. switchWorkspace: vi.fn(async () => {
  199. throw new Error("bridge failed");
  200. }),
  201. },
  202. task: createTask({
  203. taskId: "remote-task",
  204. workspacePath: "/workspace/remote",
  205. }),
  206. });
  207. expect(setSwitchingTaskKey).toHaveBeenNthCalledWith(1, "/workspace/remote:remote-task");
  208. expect(setSwitchingTaskKey).toHaveBeenNthCalledWith(2, null);
  209. expect(setError).toHaveBeenCalledWith("bridge failed");
  210. expect(onSwitchingChange).toHaveBeenNthCalledWith(1, true);
  211. expect(onSwitchingChange).toHaveBeenNthCalledWith(2, false);
  212. });
  213. });