webRemoteControlMobileShell.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import {
  5. WebRemoteControlMobileShell,
  6. isWebRemoteControlMobileChatHistoryState,
  7. } from "@/WebRemoteControlMobileShell.js";
  8. vi.mock("@/i18n/IntlProvider.js", () => ({
  9. useZCodeIntl: () => ({
  10. intl: {
  11. formatMessage: ({ id }: { id: string }) => id,
  12. },
  13. }),
  14. }));
  15. vi.mock("@/logger.js", () => ({
  16. logger: {
  17. error: vi.fn(),
  18. info: vi.fn(),
  19. warn: vi.fn(),
  20. },
  21. }));
  22. vi.mock("@/store/StoreProvider.js", () => ({
  23. useZCodeStore: (
  24. selector: (state: { theme: string; setTheme: (theme: string) => void }) => unknown,
  25. ) => selector({ theme: "dark", setTheme: vi.fn() }),
  26. }));
  27. vi.mock("@/components/ui/resizable.js", () => ({
  28. ResizablePanelGroup: ({
  29. children,
  30. className,
  31. layoutId,
  32. }: {
  33. children: ReactNode;
  34. className?: string;
  35. layoutId: string;
  36. }) =>
  37. createElement(
  38. "div",
  39. {
  40. className,
  41. "data-layout-id": layoutId,
  42. },
  43. children,
  44. ),
  45. }));
  46. function renderMobileShell(initialNavigationIntent?: "chat") {
  47. return renderToStaticMarkup(
  48. createElement(WebRemoteControlMobileShell, {
  49. activeTaskId: "task-1",
  50. activeWorkspacePath: "/workspace/app",
  51. chatContent: createElement("main", { "data-testid": "chat-content" }),
  52. initialNavigationIntent,
  53. renderTaskHome: () => createElement("aside", { "data-testid": "project-home" }),
  54. renderChatHeader: () => createElement("header", { "data-testid": "chat-header" }),
  55. switcher: {
  56. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  57. switchWorkspace: vi.fn(async () => {}),
  58. },
  59. }),
  60. );
  61. }
  62. function renderMobileShellWithSidePane(isSidePaneOpen: boolean, chatOverlay?: ReactNode) {
  63. return renderToStaticMarkup(
  64. createElement(WebRemoteControlMobileShell, {
  65. activeTaskId: "task-1",
  66. activeWorkspacePath: "/workspace/app",
  67. chatContent: createElement("main", { "data-testid": "chat-content" }),
  68. chatOverlay,
  69. initialNavigationIntent: "chat",
  70. isSidePaneOpen,
  71. onCloseSidePane: vi.fn(),
  72. renderTaskHome: () => createElement("aside", { "data-testid": "project-home" }),
  73. renderChatHeader: () => createElement("header", { "data-testid": "chat-header" }),
  74. sidePaneContent: createElement("section", { "data-testid": "side-pane-content" }),
  75. switcher: {
  76. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  77. switchWorkspace: vi.fn(async () => {}),
  78. },
  79. }),
  80. );
  81. }
  82. function renderMobileShellWithTransportState(terminalTransportState: "paired" | "reconnecting") {
  83. return renderToStaticMarkup(
  84. createElement(WebRemoteControlMobileShell, {
  85. activeTaskId: "task-1",
  86. activeWorkspacePath: "/workspace/app",
  87. chatContent: createElement("main", { "data-testid": "chat-content" }),
  88. initialNavigationIntent: "chat",
  89. renderTaskHome: () => createElement("aside", { "data-testid": "project-home" }),
  90. renderChatHeader: () => createElement("header", { "data-testid": "chat-header" }),
  91. switcher: {
  92. listWorkspaces: vi.fn(async () => ({ workspaces: [] })),
  93. switchWorkspace: vi.fn(async () => {}),
  94. },
  95. webRemoteControlTerminalTransportState: terminalTransportState,
  96. }),
  97. );
  98. }
  99. describe("WebRemoteControlMobileShell", () => {
  100. afterEach(() => {
  101. vi.unstubAllGlobals();
  102. });
  103. it("keeps scan bootstrap on the mobile home page without a chat intent", () => {
  104. const html = renderMobileShell();
  105. expect(html).toContain('data-testid="project-home"');
  106. expect(html).not.toContain('data-mobile-page="chat"');
  107. expect(html).not.toContain('data-testid="chat-content"');
  108. });
  109. it("starts on chat when a cross-workspace click provides a chat intent", () => {
  110. const html = renderMobileShell("chat");
  111. expect(html).toContain('data-mobile-page="chat"');
  112. expect(html).toContain('data-testid="chat-content"');
  113. });
  114. it("renders a theme menu trigger in the mobile chat header", () => {
  115. const html = renderMobileShell("chat");
  116. expect(html).toContain("webRemoteControl.themeMenu.trigger");
  117. });
  118. it("restores chat from browser history state after refreshing on the chat page", () => {
  119. vi.stubGlobal("window", {
  120. history: {
  121. state: { zcodeMobilePage: "chat" },
  122. back: vi.fn(),
  123. pushState: vi.fn(),
  124. },
  125. addEventListener: vi.fn(),
  126. removeEventListener: vi.fn(),
  127. });
  128. const html = renderMobileShell();
  129. expect(html).toContain('data-mobile-page="chat"');
  130. expect(html).toContain('data-testid="chat-content"');
  131. });
  132. it("renders side pane content as a right-side overlay over the mobile chat page", () => {
  133. const html = renderMobileShellWithSidePane(true);
  134. expect(html).toContain('data-mobile-side-pane-overlay="true"');
  135. expect(html).toContain("translate-x-0");
  136. expect(html).toContain('data-testid="side-pane-content"');
  137. });
  138. it("removes the covered chat surface from interaction while the mobile side pane is open", () => {
  139. const openHtml = renderMobileShellWithSidePane(true);
  140. const closedHtml = renderMobileShellWithSidePane(false);
  141. expect(openHtml).toContain('data-mobile-chat-surface="inert"');
  142. expect(openHtml).toContain('aria-hidden="true"');
  143. expect(openHtml).toContain('inert=""');
  144. expect(closedHtml).not.toContain('data-mobile-chat-surface="inert"');
  145. });
  146. it("keeps the chat overlay outside the inert surface while the side pane is open", () => {
  147. const html = renderMobileShellWithSidePane(
  148. true,
  149. createElement(
  150. "div",
  151. { "data-testid": "task-find-overlay" },
  152. createElement("input", { placeholder: "Search file changes..." }),
  153. ),
  154. );
  155. const inertSurfaceContent = html.match(/<main[^>]*inert=""[^>]*>([\s\S]*?)<\/main>/)?.[1];
  156. expect(html).toContain('data-mobile-chat-overlay="true"');
  157. expect(html).toContain("pointer-events-none absolute inset-0 z-40");
  158. expect(html).toContain('data-testid="task-find-overlay"');
  159. expect(inertSurfaceContent).toBeDefined();
  160. expect(inertSurfaceContent).not.toContain('data-testid="task-find-overlay"');
  161. expect(html.indexOf('data-mobile-chat-overlay="true"')).toBeGreaterThan(
  162. html.indexOf('inert=""'),
  163. );
  164. });
  165. it("shows a compact reconnecting notice over the existing mobile UI", () => {
  166. const html = renderMobileShellWithTransportState("reconnecting");
  167. expect(html).toContain('data-web-remote-control-reconnect-notice="true"');
  168. expect(html).toContain("webRemoteControl.mobileShell.reconnecting");
  169. expect(html).toContain('data-testid="chat-content"');
  170. });
  171. it("does not show the reconnecting notice while the transport is paired", () => {
  172. const html = renderMobileShellWithTransportState("paired");
  173. expect(html).not.toContain('data-web-remote-control-reconnect-notice="true"');
  174. });
  175. it("recognizes only the mobile chat browser history state", () => {
  176. expect(isWebRemoteControlMobileChatHistoryState({ zcodeMobilePage: "chat" })).toBe(true);
  177. expect(isWebRemoteControlMobileChatHistoryState({ zcodeMobilePage: "home" })).toBe(false);
  178. expect(isWebRemoteControlMobileChatHistoryState(null)).toBe(false);
  179. });
  180. });