webRemoteControlTaskViewReporter.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { act, cleanup, render } from "@testing-library/react";
  4. import { afterEach, beforeEach, expect, it, vi } from "vitest";
  5. import type { WebRemoteControlWorkspaceSwitcherApi } from "@/root/types.js";
  6. import { WebRemoteControlTaskViewReporter } from "@/web-remote-control/WebRemoteControlTaskViewReporter.js";
  7. import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
  8. import { WebRemoteControlMobileShell } from "@/WebRemoteControlMobileShell.js";
  9. vi.mock("@/i18n/IntlProvider.js", () => ({
  10. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  11. }));
  12. vi.mock("@/WebRemoteControlThemeMenu.js", () => ({ WebRemoteControlThemeMenu: () => null }));
  13. const workspacePath = "/repo";
  14. const workspaceIdentity = "remote:server:source:/repo";
  15. let visibility: DocumentVisibilityState = "visible";
  16. beforeEach(() => {
  17. visibility = "visible";
  18. vi.spyOn(document, "visibilityState", "get").mockImplementation(() => visibility);
  19. useZCodeSessionStore.setState({ workspaces: {}, taskNavHistory: { entries: [], cursor: -1 } });
  20. window.history.replaceState({}, "");
  21. });
  22. afterEach(() => {
  23. cleanup();
  24. vi.restoreAllMocks();
  25. });
  26. function fixture(activeTaskId: string | null = "task") {
  27. if (activeTaskId)
  28. useZCodeSessionStore.getState().setActiveTaskId(workspacePath, activeTaskId, workspaceIdentity);
  29. const updateMobileTaskView = vi.fn<
  30. NonNullable<WebRemoteControlWorkspaceSwitcherApi["updateMobileTaskView"]>
  31. >(async () => {});
  32. const updateMobileViewState = vi.fn<
  33. NonNullable<WebRemoteControlWorkspaceSwitcherApi["updateMobileViewState"]>
  34. >(async () => {});
  35. const props = {
  36. enabled: true,
  37. activeTaskId,
  38. workspacePath,
  39. workspaceIdentity,
  40. switcher: { updateMobileTaskView, updateMobileViewState },
  41. };
  42. const ui = render(createElement(WebRemoteControlTaskViewReporter, props));
  43. return { ...ui, props, updateMobileTaskView, updateMobileViewState };
  44. }
  45. it("reports draft promotion once from the committed full address and updates navigation in either viewport", () => {
  46. const f = fixture(null);
  47. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith(null);
  48. act(() =>
  49. useZCodeSessionStore.getState().setActiveTaskId(workspacePath, "task", workspaceIdentity),
  50. );
  51. f.rerender(createElement(WebRemoteControlTaskViewReporter, { ...f.props, activeTaskId: "task" }));
  52. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith({
  53. taskId: "task",
  54. workspacePath,
  55. workspaceIdentity,
  56. });
  57. expect(f.updateMobileViewState).toHaveBeenLastCalledWith(workspaceIdentity, "task");
  58. const count = f.updateMobileTaskView.mock.calls.length;
  59. f.rerender(createElement(WebRemoteControlTaskViewReporter, { ...f.props, activeTaskId: "task" }));
  60. expect(f.updateMobileTaskView).toHaveBeenCalledTimes(count);
  61. f.unmount();
  62. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith(null);
  63. });
  64. it("clears a disabled task view once and restores it when enabled", () => {
  65. const f = fixture();
  66. f.rerender(createElement(WebRemoteControlTaskViewReporter, { ...f.props, enabled: false }));
  67. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith(null);
  68. const count = f.updateMobileTaskView.mock.calls.length;
  69. f.rerender(createElement(WebRemoteControlTaskViewReporter, { ...f.props, enabled: false }));
  70. expect(f.updateMobileTaskView).toHaveBeenCalledTimes(count);
  71. f.rerender(createElement(WebRemoteControlTaskViewReporter, f.props));
  72. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith({
  73. taskId: "task",
  74. workspacePath,
  75. workspaceIdentity,
  76. });
  77. });
  78. it("clears on page background/lifecycle and restores only while visible", () => {
  79. const f = fixture();
  80. act(() => {
  81. visibility = "hidden";
  82. document.dispatchEvent(new Event("visibilitychange"));
  83. });
  84. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith(null);
  85. const count = f.updateMobileTaskView.mock.calls.length;
  86. act(() => window.dispatchEvent(new Event("pageshow")));
  87. expect(f.updateMobileTaskView).toHaveBeenCalledTimes(count);
  88. act(() => {
  89. visibility = "visible";
  90. document.dispatchEvent(new Event("visibilitychange"));
  91. });
  92. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith({
  93. taskId: "task",
  94. workspacePath,
  95. workspaceIdentity,
  96. });
  97. act(() => window.dispatchEvent(new Event("pagehide")));
  98. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith(null);
  99. });
  100. it("does not infer viewing from a cached task before its complete navigation is committed", () => {
  101. const f = fixture("cached");
  102. f.rerender(createElement(WebRemoteControlTaskViewReporter, { ...f.props, enabled: false }));
  103. act(() =>
  104. useZCodeSessionStore.getState().setActiveTaskId(workspacePath, "target", workspaceIdentity),
  105. );
  106. // 切换屏障结束前缓存 task 仍可能作为旧 props 存在,不能上报它已被查看。
  107. f.rerender(createElement(WebRemoteControlTaskViewReporter, { ...f.props, enabled: true }));
  108. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith(null);
  109. f.rerender(
  110. createElement(WebRemoteControlTaskViewReporter, { ...f.props, activeTaskId: "target" }),
  111. );
  112. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith({
  113. taskId: "target",
  114. workspacePath,
  115. workspaceIdentity,
  116. });
  117. });
  118. it("does not turn a callback reference change into a new viewing event", () => {
  119. const f = fixture();
  120. const updateMobileTaskView = vi.fn<
  121. NonNullable<WebRemoteControlWorkspaceSwitcherApi["updateMobileTaskView"]>
  122. >(async () => {});
  123. f.rerender(
  124. createElement(WebRemoteControlTaskViewReporter, {
  125. ...f.props,
  126. switcher: { ...f.props.switcher, updateMobileTaskView },
  127. }),
  128. );
  129. expect(f.updateMobileTaskView).toHaveBeenCalledTimes(1);
  130. expect(updateMobileTaskView).not.toHaveBeenCalled();
  131. act(() => {
  132. visibility = "hidden";
  133. document.dispatchEvent(new Event("visibilitychange"));
  134. });
  135. expect(updateMobileTaskView).toHaveBeenLastCalledWith(null);
  136. });
  137. it("uses the mobile shell's real Home, Root visibility and inert overlay gates", () => {
  138. useZCodeSessionStore.getState().setActiveTaskId(workspacePath, "task", workspaceIdentity);
  139. const updateMobileTaskView = vi.fn<
  140. NonNullable<WebRemoteControlWorkspaceSwitcherApi["updateMobileTaskView"]>
  141. >(async () => {});
  142. const props = {
  143. activeTaskId: "task",
  144. activeWorkspacePath: workspacePath,
  145. activeWorkspaceIdentity: workspaceIdentity,
  146. taskViewEnabled: true,
  147. chatContent: createElement("main", { "data-testid": "task-content" }),
  148. renderChatHeader: () => null,
  149. renderTaskHome: ({ navigateToChat }: { navigateToChat(): void }) =>
  150. createElement("button", { onClick: navigateToChat, "data-testid": "enter-chat" }, "Enter"),
  151. switcher: {
  152. listWorkspaces: async () => ({ workspaces: [] }),
  153. switchWorkspace: async () => {},
  154. updateMobileTaskView,
  155. },
  156. };
  157. const ui = render(createElement(WebRemoteControlMobileShell, props));
  158. expect(updateMobileTaskView).toHaveBeenLastCalledWith(null);
  159. act(() => ui.getByTestId("enter-chat").click());
  160. expect(updateMobileTaskView).toHaveBeenLastCalledWith({
  161. taskId: "task",
  162. workspacePath,
  163. workspaceIdentity,
  164. });
  165. ui.rerender(createElement(WebRemoteControlMobileShell, { ...props, taskViewEnabled: false }));
  166. expect(updateMobileTaskView).toHaveBeenLastCalledWith(null);
  167. ui.rerender(createElement(WebRemoteControlMobileShell, props));
  168. expect(updateMobileTaskView).toHaveBeenLastCalledWith({
  169. taskId: "task",
  170. workspacePath,
  171. workspaceIdentity,
  172. });
  173. ui.rerender(
  174. createElement(WebRemoteControlMobileShell, {
  175. ...props,
  176. sidePaneContent: createElement("aside"),
  177. isSidePaneOpen: true,
  178. }),
  179. );
  180. expect(updateMobileTaskView).toHaveBeenLastCalledWith(null);
  181. ui.rerender(createElement(WebRemoteControlMobileShell, props));
  182. act(() => window.dispatchEvent(new PopStateEvent("popstate", { state: {} })));
  183. expect(updateMobileTaskView).toHaveBeenLastCalledWith(null);
  184. });
  185. it("MRO03:freeze 撤销查看,resume 只重读当前真正可见目标", () => {
  186. const f = fixture();
  187. act(() => document.dispatchEvent(new Event("freeze")));
  188. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith(null);
  189. const count = f.updateMobileTaskView.mock.calls.length;
  190. act(() => {
  191. visibility = "hidden";
  192. document.dispatchEvent(new Event("resume"));
  193. });
  194. expect(f.updateMobileTaskView).toHaveBeenCalledTimes(count);
  195. act(() => {
  196. visibility = "visible";
  197. document.dispatchEvent(new Event("resume"));
  198. });
  199. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith({
  200. workspacePath,
  201. workspaceIdentity,
  202. taskId: "task",
  203. });
  204. act(() => document.dispatchEvent(new Event("freeze")));
  205. f.rerender(createElement(WebRemoteControlTaskViewReporter, { ...f.props, enabled: false }));
  206. act(() => document.dispatchEvent(new Event("resume")));
  207. expect(f.updateMobileTaskView).toHaveBeenLastCalledWith(null);
  208. });
  209. it("MRO03:连续聊天跨宽窄布局不撤销查看,真正返回 Home 才撤销", () => {
  210. useZCodeSessionStore.getState().setActiveTaskId(workspacePath, "task", workspaceIdentity);
  211. const updateMobileTaskView = vi.fn(async () => {});
  212. const props = {
  213. activeTaskId: "task",
  214. activeWorkspacePath: workspacePath,
  215. activeWorkspaceIdentity: workspaceIdentity,
  216. initialNavigationIntent: "chat" as const,
  217. chatContent: createElement("main", { "data-testid": "narrow-chat" }),
  218. renderChatHeader: () => null,
  219. renderTaskHome: () => createElement("aside", { "data-testid": "task-home" }),
  220. switcher: {
  221. listWorkspaces: async () => ({ workspaces: [] }),
  222. switchWorkspace: async () => {},
  223. updateMobileTaskView,
  224. },
  225. children: createElement("main", { "data-testid": "wide-chat" }),
  226. };
  227. const ui = render(createElement(WebRemoteControlMobileShell, props));
  228. expect(ui.getByTestId("narrow-chat")).toBeTruthy();
  229. const firstView = updateMobileTaskView.mock.calls.slice();
  230. for (const isMobileViewport of [false, true, false, true]) {
  231. ui.rerender(createElement(WebRemoteControlMobileShell, { ...props, isMobileViewport }));
  232. expect(ui.getByTestId(isMobileViewport ? "narrow-chat" : "wide-chat")).toBeTruthy();
  233. expect(updateMobileTaskView.mock.calls).toEqual(firstView);
  234. }
  235. act(() => window.dispatchEvent(new PopStateEvent("popstate", { state: {} })));
  236. expect(ui.getByTestId("task-home")).toBeTruthy();
  237. expect(updateMobileTaskView).toHaveBeenLastCalledWith(null);
  238. ui.rerender(createElement(WebRemoteControlMobileShell, { ...props, isMobileViewport: false }));
  239. expect(updateMobileTaskView).toHaveBeenLastCalledWith({
  240. taskId: "task",
  241. workspacePath,
  242. workspaceIdentity,
  243. });
  244. // 返回窄屏应保留用户选过的 Home,不能再次消费旧的初始 chat 意图。
  245. ui.rerender(createElement(WebRemoteControlMobileShell, { ...props, isMobileViewport: true }));
  246. expect(ui.getByTestId("task-home")).toBeTruthy();
  247. expect(updateMobileTaskView).toHaveBeenLastCalledWith(null);
  248. ui.unmount();
  249. expect(updateMobileTaskView).toHaveBeenLastCalledWith(null);
  250. });