marketing-touch-anonymous.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { readFile } from "node:fs/promises";
  2. import {
  3. TID_CONFIRM_DIALOG_CONFIRM,
  4. TID_LOGIN_TRIGGER,
  5. TID_LOGOUT_BUTTON,
  6. TID_LOGIN_USE_API_KEY_BUTTON,
  7. TID_LOGIN_API_KEY_SKIP_BUTTON,
  8. } from "@zcode/shared/test-ids";
  9. import {
  10. clearAppData,
  11. getE2EAppDataPaths,
  12. waitForDefaultWorkspaceReady,
  13. } from "#e2e/helpers/desktop-app";
  14. const byId = (id: string) => $(`[data-testid="${id}"]`);
  15. function origin() {
  16. const value = process.env.ZCODE_CODING_PLAN_UPGRADE_MOCK_BASE_URL;
  17. if (!value) throw new Error("Missing isolated marketing fixture");
  18. return value;
  19. }
  20. async function state() {
  21. return (await (await fetch(`${origin()}/__e2e/marketing/state`)).json()) as {
  22. events: Array<{ campaign_id: string; action_type: string }>;
  23. requestDetails: Array<{ method: string; path: string; headers: { authenticated: boolean } }>;
  24. };
  25. }
  26. async function reset(options: Record<string, boolean> = {}) {
  27. const response = await fetch(`${origin()}/__e2e/marketing/reset`, {
  28. method: "POST",
  29. body: JSON.stringify(options),
  30. });
  31. expect(response.ok).toBe(true);
  32. }
  33. async function expectAnonymous() {
  34. const credentials = JSON.parse(await readFile(getE2EAppDataPaths().credentialsFile, "utf8"));
  35. // 只判断凭据是否存在,失败输出也不得带出凭据内容。
  36. expect(Boolean(credentials.zcodejwttoken)).toBe(false);
  37. }
  38. describe("Marketing Touch anonymous boundaries", () => {
  39. before(async () => {
  40. await waitForDefaultWorkspaceReady();
  41. const popup = byId("cloud-content-dialog");
  42. if (await popup.isExisting()) await popup.$('[data-slot="dialog-close"]').click();
  43. await reset();
  44. const relaunch = await browser.electron.mock("app", "relaunch");
  45. await relaunch.mockResolvedValue(undefined);
  46. const quit = await browser.electron.mock("app", "quit");
  47. await quit.mockResolvedValue(undefined);
  48. await byId(TID_LOGIN_TRIGGER).click();
  49. await byId(TID_LOGOUT_BUTTON).click();
  50. await byId(TID_CONFIRM_DIALOG_CONFIRM).waitForDisplayed();
  51. await byId(TID_CONFIRM_DIALOG_CONFIRM).click();
  52. // mock 调用记录跨进程缓存,直接读取不会刷新,必须显式同步。
  53. await browser.waitUntil(
  54. async () => {
  55. await relaunch.update();
  56. await quit.update();
  57. return relaunch.mock.calls.length === 1 && quit.mock.calls.length === 1;
  58. },
  59. { timeout: 30_000 },
  60. );
  61. await expectAnonymous();
  62. await browser.electron.restoreAllMocks();
  63. });
  64. beforeEach(async function () {
  65. // 启动时先不投放,避免营销弹窗遮挡首次匿名 WelcomeScreen。
  66. await reset();
  67. await browser.reloadSession();
  68. // 若冷启动出现 WelcomeScreen,走公开的“暂不设置”路径进入匿名工作区。
  69. await browser.waitUntil(
  70. async () =>
  71. (await byId(TID_LOGIN_TRIGGER).isExisting()) ||
  72. (await byId(TID_LOGIN_USE_API_KEY_BUTTON).isExisting()),
  73. { timeout: 30_000 },
  74. );
  75. if (await byId(TID_LOGIN_USE_API_KEY_BUTTON).isExisting()) {
  76. await byId(TID_LOGIN_USE_API_KEY_BUTTON).click();
  77. await byId(TID_LOGIN_API_KEY_SKIP_BUTTON).click();
  78. }
  79. await waitForDefaultWorkspaceReady();
  80. await expectAnonymous();
  81. await reset({
  82. popup: this.currentTest!.title.includes("popup"),
  83. banner: this.currentTest!.title.includes("banner"),
  84. });
  85. await browser.execute(() => window.dispatchEvent(new Event("online")));
  86. });
  87. after(async () => {
  88. await browser.electron.restoreAllMocks();
  89. await clearAppData();
  90. });
  91. it("MTE-08 anonymous popup: queries and reports cancellation without Authorization", async () => {
  92. await byId("cloud-content-dialog").waitForDisplayed({ timeout: 15_000 });
  93. await byId("cloud-content-dialog").$('[data-slot="dialog-close"]').click();
  94. await byId("cloud-content-dialog").waitForExist({ reverse: true });
  95. await browser.waitUntil(async () => (await state()).events.length === 1);
  96. const ledger = await state();
  97. expect(ledger.events).toEqual([{ campaign_id: "independent", action_type: "cancel" }]);
  98. const requests = ledger.requestDetails.filter((request) =>
  99. request.path.startsWith("/api/v1/marketing/touch"),
  100. );
  101. expect(requests.some((request) => request.method === "GET")).toBe(true);
  102. expect(requests.filter((request) => request.method === "POST")).toHaveLength(1);
  103. expect(requests.every((request) => !request.headers.authenticated)).toBe(true);
  104. });
  105. it("MTE-05 anonymous banner: closes normally and opens login instead of submitting a claim", async () => {
  106. const banner = byId("marketing-banner");
  107. await banner.waitForDisplayed({ timeout: 15_000 });
  108. // 空文案 fixture 使用 App 的本地化回退名称,不能只匹配英文。
  109. await banner.$('button[aria-label="Close"], button[aria-label="关闭"]').click();
  110. await banner.waitForExist({ reverse: true });
  111. await browser.waitUntil(async () => (await state()).events.length === 1);
  112. const closed = await state();
  113. expect(closed.events).toEqual([{ campaign_id: "banner-1", action_type: "cancel" }]);
  114. expect(
  115. closed.requestDetails
  116. .filter((request) => request.path.endsWith("/touch/action"))
  117. .every((request) => !request.headers.authenticated),
  118. ).toBe(true);
  119. await fetch(`${origin()}/__e2e/marketing/redeliver-banner`, { method: "POST" });
  120. await browser.execute(() => window.dispatchEvent(new Event("online")));
  121. await banner.waitForDisplayed({ timeout: 15_000 });
  122. await banner.$('button[aria-label="Open"], button[aria-label="打开"]').click();
  123. await byId(TID_LOGIN_USE_API_KEY_BUTTON).waitForDisplayed({ timeout: 15_000 });
  124. const after = await state();
  125. expect(after.events).toEqual(closed.events);
  126. expect(
  127. after.requestDetails.filter((request) => request.path.endsWith("/billing/claim")),
  128. ).toHaveLength(0);
  129. await expectAnonymous();
  130. });
  131. });