webRemoteControlMobileShell.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import { createElement, type ReactNode } from "react";
  2. import { readFileSync } from "node:fs";
  3. import { resolve } from "node:path";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import {
  7. WebRemoteControlMobileShell,
  8. isWebRemoteControlMobileChatHistoryState,
  9. } from "@/WebRemoteControlMobileShell.js";
  10. import { persistWebRemoteControlMobileTaskHomePreferences } from "@/WebRemoteControlMobileTaskHome.js";
  11. import type {
  12. WebRemoteControlTaskTarget,
  13. WebRemoteControlWorkspaceListResult,
  14. } from "@zcode/shared";
  15. vi.mock("@/i18n/IntlProvider.js", () => ({
  16. useZCodeIntl: () => ({
  17. intl: {
  18. formatMessage: ({ id }: { id: string }) => id,
  19. },
  20. }),
  21. }));
  22. vi.mock("@/logger.js", () => ({
  23. logger: {
  24. error: vi.fn(),
  25. info: vi.fn(),
  26. warn: vi.fn(),
  27. },
  28. }));
  29. vi.mock("@/store/StoreProvider.js", () => ({
  30. useZCodeStore: (selector: (state: { theme: string; setTheme: (theme: string) => void }) => unknown) =>
  31. selector({ theme: "dark", setTheme: vi.fn() }),
  32. }));
  33. vi.mock("@/components/ui/resizable.js", () => ({
  34. ResizablePanelGroup: ({
  35. children,
  36. className,
  37. layoutId,
  38. }: {
  39. children: ReactNode;
  40. className?: string;
  41. layoutId: string;
  42. }) =>
  43. createElement(
  44. "div",
  45. {
  46. className,
  47. "data-layout-id": layoutId,
  48. },
  49. children,
  50. ),
  51. }));
  52. function createStorageMock(initial: Record<string, string> = {}) {
  53. const state = new Map(Object.entries(initial));
  54. return {
  55. getItem: (key: string) => state.get(key) ?? null,
  56. setItem: (key: string, value: string) => {
  57. state.set(key, value);
  58. },
  59. removeItem: (key: string) => {
  60. state.delete(key);
  61. },
  62. };
  63. }
  64. function renderMobileShell(initialNavigationIntent?: "chat") {
  65. return renderToStaticMarkup(
  66. createElement(WebRemoteControlMobileShell, {
  67. activeTaskId: "task-1",
  68. activeWorkspacePath: "/workspace/app",
  69. chatContent: createElement("main", { "data-testid": "chat-content" }),
  70. initialNavigationIntent,
  71. onSelectTask: vi.fn(),
  72. renderChatHeader: () => createElement("header", { "data-testid": "chat-header" }),
  73. switcher: {
  74. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  75. switchWorkspace: vi.fn(async () => {}),
  76. },
  77. }),
  78. );
  79. }
  80. function renderMobileShellWithSidePane(
  81. isSidePaneOpen: boolean,
  82. chatOverlay?: ReactNode,
  83. ) {
  84. return renderToStaticMarkup(
  85. createElement(WebRemoteControlMobileShell, {
  86. activeTaskId: "task-1",
  87. activeWorkspacePath: "/workspace/app",
  88. chatContent: createElement("main", { "data-testid": "chat-content" }),
  89. chatOverlay,
  90. initialNavigationIntent: "chat",
  91. isSidePaneOpen,
  92. onCloseSidePane: vi.fn(),
  93. onSelectTask: vi.fn(),
  94. renderChatHeader: () => createElement("header", { "data-testid": "chat-header" }),
  95. sidePaneContent: createElement("section", { "data-testid": "side-pane-content" }),
  96. switcher: {
  97. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  98. switchWorkspace: vi.fn(async () => {}),
  99. },
  100. }),
  101. );
  102. }
  103. function renderMobileShellWithTransportState(terminalTransportState: "paired" | "reconnecting") {
  104. return renderToStaticMarkup(
  105. createElement(WebRemoteControlMobileShell, {
  106. activeTaskId: "task-1",
  107. activeWorkspacePath: "/workspace/app",
  108. chatContent: createElement("main", { "data-testid": "chat-content" }),
  109. initialNavigationIntent: "chat",
  110. onSelectTask: vi.fn(),
  111. renderChatHeader: () => createElement("header", { "data-testid": "chat-header" }),
  112. switcher: {
  113. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  114. switchWorkspace: vi.fn(async () => {}),
  115. },
  116. webRemoteControlTerminalTransportState: terminalTransportState,
  117. }),
  118. );
  119. }
  120. const workspaceList: WebRemoteControlWorkspaceListResult = {
  121. workspaces: [
  122. {
  123. workspacePath: "/workspace/app",
  124. label: "App",
  125. kind: "local",
  126. },
  127. ],
  128. tasks: [
  129. {
  130. taskId: "older-created",
  131. title: "Older created",
  132. workspacePath: "/workspace/app",
  133. workspaceLabel: "App",
  134. workspaceKind: "local",
  135. createdAt: 10,
  136. updatedAt: 100,
  137. provider: "codex",
  138. },
  139. {
  140. taskId: "newer-created",
  141. title: "Newer created",
  142. workspacePath: "/workspace/app",
  143. workspaceLabel: "App",
  144. workspaceKind: "local",
  145. createdAt: 20,
  146. updatedAt: 1,
  147. provider: "codex",
  148. },
  149. ] satisfies WebRemoteControlTaskTarget[],
  150. };
  151. function renderMobileShellWithInitialWorkspaceList() {
  152. return renderToStaticMarkup(
  153. createElement(WebRemoteControlMobileShell, {
  154. activeTaskId: null,
  155. activeWorkspacePath: "/workspace/app",
  156. chatContent: createElement("main", { "data-testid": "chat-content" }),
  157. initialWorkspaceList: workspaceList,
  158. onSelectTask: vi.fn(),
  159. renderChatHeader: () => createElement("header", { "data-testid": "chat-header" }),
  160. switcher: {
  161. listWorkspaces: vi.fn(async () => workspaceList),
  162. switchWorkspace: vi.fn(async () => {}),
  163. },
  164. }),
  165. );
  166. }
  167. describe("WebRemoteControlMobileShell", () => {
  168. afterEach(() => {
  169. vi.unstubAllGlobals();
  170. });
  171. it("keeps scan bootstrap on the mobile home page without a chat intent", () => {
  172. const html = renderMobileShell();
  173. expect(html).toContain("webRemoteControl.mobileHome.title");
  174. expect(html).not.toContain('data-mobile-page="chat"');
  175. expect(html).not.toContain('data-testid="chat-content"');
  176. });
  177. it("requests a workspace list refresh when returning from chat to the task list", () => {
  178. const source = readFileSync(
  179. resolve(process.cwd(), "packages/ui/src/WebRemoteControlMobileShell.tsx"),
  180. "utf8",
  181. );
  182. expect(source).toContain("homeRefreshKey");
  183. expect(source).toContain("refreshKey={homeRefreshKey}");
  184. });
  185. it("starts on chat when a cross-workspace click provides a chat intent", () => {
  186. const html = renderMobileShell("chat");
  187. expect(html).toContain('data-mobile-page="chat"');
  188. expect(html).toContain('data-testid="chat-content"');
  189. });
  190. it("renders a theme menu trigger in the mobile chat header", () => {
  191. const html = renderMobileShell("chat");
  192. expect(html).toContain("webRemoteControl.themeMenu.trigger");
  193. });
  194. it("restores chat from browser history state after refreshing on the chat page", () => {
  195. vi.stubGlobal("window", {
  196. history: {
  197. state: { zcodeMobilePage: "chat" },
  198. back: vi.fn(),
  199. pushState: vi.fn(),
  200. },
  201. addEventListener: vi.fn(),
  202. removeEventListener: vi.fn(),
  203. });
  204. const html = renderMobileShell();
  205. expect(html).toContain('data-mobile-page="chat"');
  206. expect(html).toContain('data-testid="chat-content"');
  207. });
  208. it("restores mobile task home preferences from localStorage after refreshing the page", () => {
  209. const storage = createStorageMock();
  210. vi.stubGlobal("window", {
  211. localStorage: storage,
  212. history: {
  213. state: null,
  214. },
  215. });
  216. persistWebRemoteControlMobileTaskHomePreferences(
  217. { organizeBy: "timeline", sortBy: "created" },
  218. storage,
  219. );
  220. const html = renderMobileShellWithInitialWorkspaceList();
  221. expect(html.indexOf("Newer created")).toBeLessThan(html.indexOf("Older created"));
  222. expect(html).not.toContain("webRemoteControl.mobileHome.workspaceKind.local");
  223. });
  224. it("constrains chat content in a flex column so the conversation area can scroll", () => {
  225. const html = renderMobileShell("chat");
  226. expect(html).toContain(
  227. '<main class="flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden">',
  228. );
  229. });
  230. it("keeps the mobile chat and side-pane content shrinkable at narrow widths", () => {
  231. const html = renderMobileShell("chat");
  232. const shellSource = readFileSync(
  233. resolve(process.cwd(), "packages/ui/src/WebRemoteControlMobileShell.tsx"),
  234. "utf8",
  235. );
  236. const sidePaneSource = readFileSync(
  237. resolve(process.cwd(), "packages/ui/src/app-shell/AnimatedSidePanePanel.tsx"),
  238. "utf8",
  239. );
  240. const workspaceShellSource = readFileSync(
  241. resolve(process.cwd(), "packages/ui/src/app-shell/WorkspaceShellLayout.tsx"),
  242. "utf8",
  243. );
  244. const sessionPaneSource = readFileSync(
  245. resolve(process.cwd(), "packages/ui/src/v4/SessionPane.tsx"),
  246. "utf8",
  247. );
  248. const timelineSource = readFileSync(
  249. resolve(process.cwd(), "packages/ui/src/v4/ConversationTimeline.tsx"),
  250. "utf8",
  251. );
  252. // 回归原因:长 subagent 输出或进程条目让 flex 子项按 min-content 宽度撑开,
  253. // 窄屏远控抽屉随后会裁掉每一行的开头。
  254. expect(html).toContain(
  255. '<main class="flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden">',
  256. );
  257. expect(shellSource).toContain(
  258. 'className="relative flex h-full min-h-0 min-w-0 flex-col bg-background"',
  259. );
  260. expect(sidePaneSource).toContain('mobileOverlay && "min-w-0"');
  261. expect(sidePaneSource).not.toContain('className={cn("h-full min-w-0"');
  262. expect(workspaceShellSource).toContain(
  263. 'className="relative flex h-full min-h-0 min-w-0 flex-1 flex-col"',
  264. );
  265. expect(sessionPaneSource).toContain('compactForRemoteControl && "min-w-0"');
  266. expect(timelineSource).toContain('compactForRemoteControl && "min-w-0"');
  267. });
  268. it("renders side pane content as a right-side overlay over the mobile chat page", () => {
  269. const html = renderMobileShellWithSidePane(true);
  270. expect(html).toContain('data-mobile-side-pane-overlay="true"');
  271. expect(html).toContain("absolute top-0 right-0 h-full w-[min(88vw,28rem)]");
  272. expect(html).toContain("translate-x-0");
  273. expect(html).toContain('data-testid="side-pane-content"');
  274. expect(html).not.toContain("web-remote-control-mobile-side-pane-layout");
  275. });
  276. it("removes the covered chat surface from interaction while the mobile side pane is open", () => {
  277. const openHtml = renderMobileShellWithSidePane(true);
  278. const closedHtml = renderMobileShellWithSidePane(false);
  279. expect(openHtml).toContain('data-mobile-chat-surface="inert"');
  280. expect(openHtml).toContain('aria-hidden="true"');
  281. expect(openHtml).toContain('inert=""');
  282. expect(closedHtml).not.toContain('data-mobile-chat-surface="inert"');
  283. });
  284. it("keeps the chat overlay outside the inert surface while the side pane is open", () => {
  285. const html = renderMobileShellWithSidePane(
  286. true,
  287. createElement(
  288. "div",
  289. { "data-testid": "task-find-overlay" },
  290. createElement("input", { placeholder: "Search file changes..." }),
  291. ),
  292. );
  293. const inertSurfaceContent = html.match(
  294. /<main[^>]*inert=""[^>]*>([\s\S]*?)<\/main>/,
  295. )?.[1];
  296. expect(html).toContain('data-mobile-chat-overlay="true"');
  297. expect(html).toContain("pointer-events-none absolute inset-0 z-40");
  298. expect(html).toContain('data-testid="task-find-overlay"');
  299. expect(inertSurfaceContent).toBeDefined();
  300. expect(inertSurfaceContent).not.toContain('data-testid="task-find-overlay"');
  301. expect(html.indexOf('data-mobile-chat-overlay="true"')).toBeGreaterThan(
  302. html.indexOf('inert=""'),
  303. );
  304. });
  305. it("shows a compact reconnecting notice over the existing mobile UI", () => {
  306. const html = renderMobileShellWithTransportState("reconnecting");
  307. expect(html).toContain('data-web-remote-control-reconnect-notice="true"');
  308. expect(html).toContain("webRemoteControl.mobileShell.reconnecting");
  309. expect(html).toContain('data-testid="chat-content"');
  310. });
  311. it("does not show the reconnecting notice while the transport is paired", () => {
  312. const html = renderMobileShellWithTransportState("paired");
  313. expect(html).not.toContain('data-web-remote-control-reconnect-notice="true"');
  314. });
  315. it("recognizes only the mobile chat browser history state", () => {
  316. expect(isWebRemoteControlMobileChatHistoryState({ zcodeMobilePage: "chat" })).toBe(true);
  317. expect(isWebRemoteControlMobileChatHistoryState({ zcodeMobilePage: "home" })).toBe(false);
  318. expect(isWebRemoteControlMobileChatHistoryState(null)).toBe(false);
  319. });
  320. });