webRemoteControlAuthGateFixture.test.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { expect, it } from "vitest";
  5. import { mapClientScenesToDraftSuggestedPromptItems } from "@/v4/draftSuggestedPromptItems.js";
  6. import { prepareWebRemoteControlAuthGateFixture } from "#e2e/helpers/web-remote-control-auth-gate-fixture";
  7. it("keeps cached-account business requests inside the auth-gate fixture and restores its environment", async () => {
  8. const root = await mkdtemp(join(tmpdir(), "remote-auth-fixture-"));
  9. const credentialsFile = join(root, "credentials.json");
  10. const settingsFile = join(root, "setting.json");
  11. const previous = process.env.ZAI_BUSINESS_BASE_URL;
  12. process.env.ZAI_BUSINESS_BASE_URL = "https://business.example.invalid";
  13. let close: (() => Promise<void>) | null = null;
  14. try {
  15. await writeFile(settingsFile, "{}");
  16. close = await prepareWebRemoteControlAuthGateFixture({
  17. specs: ["web-remote-control-auth-gate.test.ts"],
  18. credentialsFile,
  19. settingsFile,
  20. workspacePath: root,
  21. });
  22. expect(close).not.toBeNull();
  23. expect(process.env.ZAI_BUSINESS_BASE_URL).toBe(process.env.ZCODE_E2E_REMOTE_AUTH_GATE_URL);
  24. await close!();
  25. close = null;
  26. expect(process.env.ZAI_BUSINESS_BASE_URL).toBe("https://business.example.invalid");
  27. } finally {
  28. await close?.();
  29. if (previous === undefined) delete process.env.ZAI_BUSINESS_BASE_URL;
  30. else process.env.ZAI_BUSINESS_BASE_URL = previous;
  31. await rm(root, { recursive: true, force: true });
  32. }
  33. });
  34. it.each([
  35. "conversation-session-virtual-project-mobile.test.ts",
  36. "conversation-session-web-remote-project-directory.test.ts",
  37. "conversation-session-web-remote-unread.test.ts",
  38. ])(
  39. "provides %s with an isolated authenticated relay and the current Web frontend",
  40. async (spec) => {
  41. const root = await mkdtemp(join(tmpdir(), "remote-mobile-fixture-"));
  42. const credentialsFile = join(root, "credentials.json");
  43. const settingsFile = join(root, "setting.json");
  44. const previousUrl = process.env.ZCODE_WEB_REMOTE_CONTROL_URL;
  45. const previousForceColor = process.env.FORCE_COLOR;
  46. // wdio CLI 会给 worker 注入 FORCE_COLOR(非 TTY 为 "0",TTY 为 "1"),fixture 必须在该环境下识别 Web 就绪。
  47. process.env.FORCE_COLOR = "0";
  48. let close: (() => Promise<void>) | null = null;
  49. try {
  50. await writeFile(settingsFile, "{}");
  51. close = await prepareWebRemoteControlAuthGateFixture({
  52. specs: [spec],
  53. credentialsFile,
  54. settingsFile,
  55. workspacePath: root,
  56. });
  57. expect(close).not.toBeNull();
  58. expect(process.env.ZCODE_WEB_REMOTE_CONTROL_URL).toBe("http://127.0.0.1:5173/remote/v4");
  59. const origin = process.env.ZCODE_E2E_REMOTE_AUTH_GATE_URL!;
  60. expect(new URL(origin).hostname).toBe("127.0.0.1");
  61. expect(process.env.ZAI_BUSINESS_BASE_URL).toBe(origin);
  62. const credentials = JSON.parse(await readFile(credentialsFile, "utf8"));
  63. expect(credentials["oauth:active_provider"]).toBe("zai");
  64. const token = await fetch(new URL("/api/v1/remote/token", origin), {
  65. method: "POST",
  66. headers: {
  67. Authorization: `Bearer ${credentials.zcodejwttoken}`,
  68. "Content-Type": "application/json",
  69. },
  70. body: JSON.stringify({ role: "terminal" }),
  71. });
  72. expect(token.status).toBe(200);
  73. expect((await token.json()).data.token).toEqual(expect.any(String));
  74. const scenes = await (await fetch(new URL("/api/v1/client/scenes", origin))).json();
  75. expect(mapClientScenesToDraftSuggestedPromptItems(scenes.data)).toEqual([]);
  76. await close!();
  77. close = null;
  78. expect(process.env.ZCODE_WEB_REMOTE_CONTROL_URL).toBe(previousUrl);
  79. await expect(fetch(new URL("/api/v1/remote/device", origin))).rejects.toThrow();
  80. } finally {
  81. await close?.();
  82. if (previousForceColor === undefined) delete process.env.FORCE_COLOR;
  83. else process.env.FORCE_COLOR = previousForceColor;
  84. await rm(root, { recursive: true, force: true });
  85. }
  86. },
  87. );