workspaceWebRemoteControlTrigger.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. const { resolvedRemoteSessionIdMock } = vi.hoisted(() => ({
  5. resolvedRemoteSessionIdMock: vi.fn<() => string | null>(),
  6. }));
  7. vi.mock("@/ControlHintTooltip.js", () => ({
  8. ControlHintTooltip: ({
  9. children,
  10. title,
  11. description,
  12. side,
  13. }: {
  14. children: unknown;
  15. title: string;
  16. description?: string;
  17. side?: string;
  18. }) =>
  19. createElement(
  20. "span",
  21. {
  22. "data-tooltip-title": title,
  23. "data-tooltip-description": description ?? "",
  24. "data-tooltip-side": side ?? "",
  25. },
  26. children,
  27. ),
  28. }));
  29. vi.mock("@/components/ui/button.js", () => ({
  30. Button: ({
  31. children,
  32. ...props
  33. }: {
  34. children: unknown;
  35. [key: string]: unknown;
  36. }) => createElement("button", props, children),
  37. }));
  38. vi.mock("@/components/ui/lucide.js", () => ({
  39. Smartphone: () => createElement("span", { "data-testid": "smartphone-icon" }),
  40. }));
  41. vi.mock("@/i18n/IntlProvider.js", () => ({
  42. useZCodeIntl: () => ({
  43. intl: {
  44. formatMessage: ({ id }: { id: string }) => id,
  45. },
  46. }),
  47. }));
  48. vi.mock("@/logger.js", () => ({
  49. logger: {
  50. info: vi.fn(),
  51. warn: vi.fn(),
  52. error: vi.fn(),
  53. },
  54. }));
  55. vi.mock("@/hooks/useResolvedRemoteWorkspaceSessionId.js", () => ({
  56. useResolvedRemoteWorkspaceSessionId: resolvedRemoteSessionIdMock,
  57. }));
  58. vi.mock("@/hooks/useWebRemoteControlStatus.js", () => ({
  59. useWebRemoteControlStatus: () => ({ status: "idle" }),
  60. }));
  61. vi.mock("@/WebRemoteControlDialog.js", () => ({
  62. WebRemoteControlDialog: ({
  63. initialTaskId,
  64. remoteSessionId,
  65. }: {
  66. initialTaskId?: string;
  67. remoteSessionId?: string;
  68. }) =>
  69. createElement("div", {
  70. "data-testid": "web-remote-control-dialog",
  71. "data-initial-task-id": initialTaskId ?? "",
  72. "data-remote-session-id": remoteSessionId ?? "",
  73. }),
  74. }));
  75. async function renderTriggerWithEnv(
  76. env: "test" | "production",
  77. options?: {
  78. initialTaskId?: string;
  79. featureEnabled?: boolean;
  80. workspaceIdentity?: string;
  81. remoteSessionId?: string;
  82. },
  83. ) {
  84. vi.resetModules();
  85. vi.doMock("@zcode/shared", () => ({ ZCODE_ENV: env }));
  86. const { WorkspaceWebRemoteControlTrigger } =
  87. await import("../src/WorkspaceWebRemoteControlTrigger.js");
  88. const { WebRemoteControlFeatureProvider } =
  89. await import("../src/WebRemoteControlFeatureProvider.js");
  90. const trigger = createElement(WorkspaceWebRemoteControlTrigger, {
  91. workspacePath: "/tmp/project",
  92. workspaceIdentity: options?.workspaceIdentity,
  93. remoteSessionId: options?.remoteSessionId,
  94. initialTaskId: options?.initialTaskId,
  95. });
  96. return renderToStaticMarkup(
  97. typeof options?.featureEnabled === "boolean"
  98. ? createElement(
  99. WebRemoteControlFeatureProvider,
  100. { enabled: options.featureEnabled },
  101. trigger,
  102. )
  103. : trigger,
  104. );
  105. }
  106. afterEach(() => {
  107. resolvedRemoteSessionIdMock.mockReset();
  108. vi.resetModules();
  109. });
  110. describe("WorkspaceWebRemoteControlTrigger", () => {
  111. it("在 test 环境展示 Web 远程控制入口", async () => {
  112. const html = await renderTriggerWithEnv("test");
  113. expect(html).toContain("webRemoteControl.trigger");
  114. });
  115. it("在 production 环境展示 Web 远程控制入口", async () => {
  116. const html = await renderTriggerWithEnv("production");
  117. expect(html).toContain("webRemoteControl.trigger");
  118. });
  119. it("允许 root wiring 显式注入 Web 远程控制入口能力", async () => {
  120. const html = await renderTriggerWithEnv("production", {
  121. featureEnabled: true,
  122. });
  123. expect(html).toContain("webRemoteControl.trigger");
  124. });
  125. it("把当前 active task 作为 Web 远程控制初始 task 传给弹层", async () => {
  126. const html = await renderTriggerWithEnv("test", {
  127. initialTaskId: "task-from-desktop",
  128. });
  129. expect(html).toContain('data-initial-task-id="task-from-desktop"');
  130. });
  131. it("用 workspaceIdentity 精确解析远控 session,并只向弹层传递已验证结果", async () => {
  132. resolvedRemoteSessionIdMock.mockReturnValue(null);
  133. const html = await renderTriggerWithEnv("test", {
  134. workspaceIdentity: "remote:ssh:localhost:2222:root:/tmp/project",
  135. remoteSessionId: "stale-session",
  136. });
  137. expect(resolvedRemoteSessionIdMock).toHaveBeenCalledWith(
  138. "/tmp/project",
  139. "stale-session",
  140. "remote:ssh:localhost:2222:root:/tmp/project",
  141. );
  142. expect(html).toContain('data-remote-session-id=""');
  143. expect(html).not.toContain('data-remote-session-id="stale-session"');
  144. });
  145. it("把入口名称和手机连接状态合并到同一个 tooltip", async () => {
  146. const html = await renderTriggerWithEnv("test");
  147. expect(html).toContain('data-tooltip-title="webRemoteControl.trigger"');
  148. expect(html).toContain(
  149. 'data-tooltip-description="webRemoteControl.triggerStatus.idle"',
  150. );
  151. expect(html).not.toContain(
  152. 'data-tooltip-title="webRemoteControl.triggerStatus.idle"',
  153. );
  154. });
  155. it("在按钮上方展示远程控制 tooltip", async () => {
  156. const html = await renderTriggerWithEnv("test");
  157. expect(html).toContain('data-tooltip-side="top"');
  158. });
  159. });