workspaceWebRemoteControlTrigger.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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: ({ children, ...props }: { children: unknown; [key: string]: unknown }) =>
  31. createElement("button", props, children),
  32. }));
  33. vi.mock("@/components/ui/lucide.js", () => ({
  34. Smartphone: () => createElement("span", { "data-testid": "smartphone-icon" }),
  35. }));
  36. vi.mock("@/i18n/IntlProvider.js", () => ({
  37. useZCodeIntl: () => ({
  38. intl: {
  39. formatMessage: ({ id }: { id: string }) => id,
  40. },
  41. }),
  42. }));
  43. vi.mock("@/logger.js", () => ({
  44. logger: {
  45. info: vi.fn(),
  46. warn: vi.fn(),
  47. error: vi.fn(),
  48. },
  49. }));
  50. vi.mock("@/hooks/useResolvedRemoteWorkspaceSessionId.js", () => ({
  51. useResolvedRemoteWorkspaceSessionId: resolvedRemoteSessionIdMock,
  52. }));
  53. vi.mock("@/hooks/useWebRemoteControlStatus.js", () => ({
  54. useWebRemoteControlStatus: () => ({ status: "idle" }),
  55. }));
  56. vi.mock("@/web-remote-control/index.js", () => ({
  57. WebRemoteControlDialog: ({
  58. initialTaskId,
  59. remoteSessionId,
  60. }: {
  61. initialTaskId?: string;
  62. remoteSessionId?: string;
  63. }) =>
  64. createElement("div", {
  65. "data-testid": "web-remote-control-dialog",
  66. "data-initial-task-id": initialTaskId ?? "",
  67. "data-remote-session-id": remoteSessionId ?? "",
  68. }),
  69. }));
  70. async function renderTriggerWithEnv(
  71. env: "test" | "production",
  72. options?: {
  73. initialTaskId?: string;
  74. featureEnabled?: boolean;
  75. workspaceIdentity?: string;
  76. remoteSessionId?: string;
  77. },
  78. ) {
  79. vi.resetModules();
  80. vi.doMock("@zcode/shared", () => ({ ZCODE_ENV: env }));
  81. const { WorkspaceWebRemoteControlTrigger } =
  82. await import("../src/WorkspaceWebRemoteControlTrigger.js");
  83. const { WebRemoteControlFeatureProvider } =
  84. await import("../src/WebRemoteControlFeatureProvider.js");
  85. const trigger = createElement(WorkspaceWebRemoteControlTrigger, {
  86. workspacePath: "/tmp/project",
  87. workspaceIdentity: options?.workspaceIdentity,
  88. remoteSessionId: options?.remoteSessionId,
  89. initialTaskId: options?.initialTaskId,
  90. });
  91. return renderToStaticMarkup(
  92. typeof options?.featureEnabled === "boolean"
  93. ? createElement(WebRemoteControlFeatureProvider, { enabled: options.featureEnabled }, trigger)
  94. : trigger,
  95. );
  96. }
  97. afterEach(() => {
  98. resolvedRemoteSessionIdMock.mockReset();
  99. vi.resetModules();
  100. });
  101. describe("WorkspaceWebRemoteControlTrigger", () => {
  102. it("默认展示 Web 远程控制入口", async () => {
  103. const html = await renderTriggerWithEnv("production");
  104. expect(html).toContain("webRemoteControl.trigger");
  105. });
  106. it("允许 root wiring 显式注入 Web 远程控制入口能力", async () => {
  107. const html = await renderTriggerWithEnv("production", {
  108. featureEnabled: true,
  109. });
  110. expect(html).toContain("webRemoteControl.trigger");
  111. });
  112. it("把当前 active task 作为 Web 远程控制初始 task 传给弹层", async () => {
  113. const html = await renderTriggerWithEnv("test", {
  114. initialTaskId: "task-from-desktop",
  115. });
  116. expect(html).toContain('data-initial-task-id="task-from-desktop"');
  117. });
  118. it("用 workspaceIdentity 精确解析远控 session,并只向弹层传递已验证结果", async () => {
  119. resolvedRemoteSessionIdMock.mockReturnValue(null);
  120. const html = await renderTriggerWithEnv("test", {
  121. workspaceIdentity: "remote:ssh:localhost:2222:root:/tmp/project",
  122. remoteSessionId: "stale-session",
  123. });
  124. expect(resolvedRemoteSessionIdMock).toHaveBeenCalledWith(
  125. "/tmp/project",
  126. "stale-session",
  127. "remote:ssh:localhost:2222:root:/tmp/project",
  128. );
  129. expect(html).toContain('data-remote-session-id=""');
  130. expect(html).not.toContain('data-remote-session-id="stale-session"');
  131. });
  132. it("把入口名称和手机连接状态合并到同一个 tooltip", async () => {
  133. const html = await renderTriggerWithEnv("test");
  134. expect(html).toContain('data-tooltip-title="webRemoteControl.trigger"');
  135. expect(html).toContain('data-tooltip-description="webRemoteControl.triggerStatus.idle"');
  136. expect(html).not.toContain('data-tooltip-title="webRemoteControl.triggerStatus.idle"');
  137. });
  138. });