feedback-upload-privacy.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { tmpdir } from "node:os";
  2. import { join, resolve } from "node:path";
  3. import { build } from "esbuild";
  4. import { TID_FEEDBACK_LOGS_OPT_IN } from "@zcode/shared";
  5. import {
  6. readSettings,
  7. seedSettings,
  8. waitForDefaultWorkspaceReady,
  9. } from "../../../helpers/desktop-app.js";
  10. import { startHelpConfigFixture } from "../../../helpers/help-client-config-fixture.js";
  11. async function dismissInitialOnboarding() {
  12. const onboarding = $('[data-testid="onboarding-page"]');
  13. await browser.waitUntil(
  14. async () =>
  15. (await onboarding.isExisting()) ||
  16. (await $('button[aria-label="Help"]').isExisting()) ||
  17. (await $('button[aria-label="帮助"]').isExisting()),
  18. { timeout: 15000 },
  19. );
  20. if (await onboarding.isDisplayed()) {
  21. // 当前 staging 的引导按本地完成记录触发;只关窗口后刷新会重开。
  22. // 通过现有跳过操作完成隔离测试账号的三步引导,不改变产品实现。
  23. for (let step = 0; step < 3; step += 1) {
  24. const before = await onboarding.getText();
  25. await onboarding.$("footer > button").click();
  26. await browser.waitUntil(
  27. async () => !(await onboarding.isDisplayed()) || (await onboarding.getText()) !== before,
  28. { timeout: 15000 },
  29. );
  30. if (!(await onboarding.isDisplayed())) break;
  31. }
  32. await onboarding.waitForDisplayed({ reverse: true, timeout: 15000 });
  33. }
  34. }
  35. describe("反馈上传授权(manual review)", () => {
  36. let settings: Awaited<ReturnType<typeof readSettings>>;
  37. let server: Awaited<ReturnType<typeof startHelpConfigFixture>>;
  38. let windowSize:
  39. | { width: number; height: number; minWidth: number; minHeight: number }
  40. | undefined;
  41. before(async () => {
  42. await dismissInitialOnboarding();
  43. await waitForDefaultWorkspaceReady(30000);
  44. settings = await readSettings();
  45. windowSize = await browser.electron.execute((electron) => {
  46. const window = electron.BrowserWindow.getAllWindows()[0]!;
  47. const bounds = window.getBounds();
  48. const [minWidth = 0, minHeight = 0] = window.getMinimumSize();
  49. window.setMinimumSize(360, 400);
  50. return { width: bounds.width, height: bounds.height, minWidth, minHeight };
  51. });
  52. server = await startHelpConfigFixture({ feedback_use_external_form: false });
  53. });
  54. after(async () => {
  55. await browser.keys("Escape");
  56. if (settings) await seedSettings(settings);
  57. if (windowSize)
  58. await browser.electron.execute((electron, size) => {
  59. const window = electron.BrowserWindow.getAllWindows()[0]!;
  60. window.setMinimumSize(size.minWidth, size.minHeight);
  61. window.setSize(size.width, size.height);
  62. }, windowSize);
  63. await server?.close();
  64. });
  65. for (const [locale, width, height, helpLabel, reportLabel, hint] of [
  66. ["en-US", 1280, 900, "Help", "Report", "On by default"],
  67. ["zh-CN", 430, 900, "帮助", "问题上报", "默认开启"],
  68. ] as const) {
  69. it(`FB-PRIV-01/08: ${locale} ${width}px 新反馈默认开启日志且允许取消`, async () => {
  70. await seedSettings({ locale, localePreference: locale, zcodeEndpointOrigin: server.origin });
  71. await browser.electron.execute(
  72. (electron, size) => {
  73. electron.BrowserWindow.getAllWindows()[0]!.setSize(size.width, size.height);
  74. },
  75. { width, height },
  76. );
  77. await browser.refresh();
  78. await dismissInitialOnboarding();
  79. await waitForDefaultWorkspaceReady(30000);
  80. const help = $(`button[aria-label="${helpLabel}"]`);
  81. await help.waitForDisplayed({ timeout: 10000 });
  82. await help.click();
  83. await $(`//*[@role="menuitem" and contains(.,"${reportLabel}")]`).click();
  84. const dialog = $('[role="dialog"]');
  85. await dialog.waitForDisplayed({ timeout: 10000 });
  86. const logs = $(`[data-testid="${TID_FEEDBACK_LOGS_OPT_IN}"]`);
  87. await expect(logs).toHaveAttribute("aria-checked", "true");
  88. await expect(dialog).toHaveText(expect.stringContaining(hint));
  89. expect(await dialog.$$("img").length).toBe(0);
  90. await logs.click();
  91. await expect(logs).toHaveAttribute("aria-checked", "false");
  92. await logs.click();
  93. await expect(logs).toHaveAttribute("aria-checked", "true");
  94. for (const theme of ["light", "dark"] as const) {
  95. await browser.execute((value) => {
  96. const actions = (
  97. window as typeof window & { __testActions?: { setTheme?: (theme: string) => void } }
  98. ).__testActions;
  99. if (!actions?.setTheme) throw new Error("缺少主题测试入口");
  100. actions.setTheme(value);
  101. }, theme);
  102. await logs.scrollIntoView();
  103. await expect(logs).toBeDisplayed();
  104. // E2E runtime path guard 禁止源码出现裸系统临时目录字面量,须走 tmpdir() 拼接。
  105. await browser.saveScreenshot(join(tmpdir(), `zcode-feedback-${locale}-${theme}.png`));
  106. }
  107. await browser.keys("Escape");
  108. });
  109. }
  110. });
  111. // 浏览器 fixture 单独由 esbuild 打包,不向 WDIO 的 Window 类型注入产品 store 声明。
  112. declare global {
  113. interface Window {
  114. feedbackSubmitFixture?: {
  115. failNextCreate(): void;
  116. restore(jobId: string): void;
  117. snapshot(): {
  118. calls: { create: number; prepare: number; upload: string[]; cleanup: number };
  119. createInputs: Array<Record<string, unknown>>;
  120. jobs: Array<{ id: string; status: string; includeLogs?: boolean }>;
  121. };
  122. };
  123. }
  124. }
  125. describe("FB-PRIV-02: 反馈表单到日志服务边界", () => {
  126. let server: Awaited<ReturnType<typeof startHelpConfigFixture>>;
  127. let mainHandle: string;
  128. let windowId: number;
  129. before(async () => {
  130. const root = resolve(import.meta.dirname, "../../../../../../..");
  131. const bundle = await build({
  132. entryPoints: [resolve(root, "packages/desktop/test/e2e/helpers/feedback-submit-fixture.js")],
  133. bundle: true,
  134. write: false,
  135. platform: "browser",
  136. format: "iife",
  137. alias: { "@": resolve(root, "packages/ui/src") },
  138. define: { "import.meta.env": "{}", "process.env.NODE_ENV": '"test"' },
  139. tsconfig: resolve(root, "packages/ui/tsconfig.json"),
  140. });
  141. // fixture 页的脚本在 head 同步执行;等 DOM 就绪后再挂载真实表单。
  142. server = await startHelpConfigFixture(
  143. {},
  144. `
  145. window.addEventListener("error", (event) => {
  146. document.documentElement.dataset.fixtureError = event.message;
  147. });
  148. window.addEventListener("DOMContentLoaded", () => { ${bundle.outputFiles[0]!.text} });
  149. `,
  150. );
  151. mainHandle = await browser.getWindowHandle();
  152. const existing = await browser.getWindowHandles();
  153. windowId = await browser.electron.execute(async (electron, url) => {
  154. const win = new electron.BrowserWindow({
  155. width: 1000,
  156. height: 900,
  157. show: false,
  158. // 隐藏窗口也保持回调计时稳定,避免 Chromium 后台节流影响表单完成回调。
  159. webPreferences: { sandbox: true, nodeIntegration: false, backgroundThrottling: false },
  160. });
  161. await win.loadURL(url);
  162. return win.id;
  163. }, server.origin);
  164. await browser.waitUntil(
  165. async () => (await browser.getWindowHandles()).length > existing.length,
  166. );
  167. const handle = (await browser.getWindowHandles()).find((item) => !existing.includes(item))!;
  168. await browser.switchToWindow(handle);
  169. });
  170. after(async () => {
  171. if (windowId)
  172. await browser.electron.execute(
  173. (electron, id) => electron.BrowserWindow.fromId(id)?.destroy(),
  174. windowId,
  175. );
  176. if (mainHandle) await browser.switchToWindow(mainHandle);
  177. await server?.close();
  178. });
  179. const snapshot = () => browser.execute(() => window.feedbackSubmitFixture!.snapshot());
  180. const submit = async () => {
  181. const previousJobs = (await snapshot()).jobs.length;
  182. await $("button=Submit feedback").click();
  183. await browser.waitUntil(
  184. async () => {
  185. const jobs = (await snapshot()).jobs;
  186. return jobs.length > previousJobs && ["success", "error"].includes(jobs.at(-1)!.status);
  187. },
  188. { timeout: 10000 },
  189. );
  190. };
  191. for (const [name, includeLogs, restore] of [
  192. ["默认提交", true, false],
  193. ["取消后提交", false, false],
  194. ["开启日志的后台任务恢复后重试", true, true],
  195. ["取消日志的后台任务恢复后重试", false, true],
  196. ] as const) {
  197. it(`${name}:验证实际归档/上传调用`, async () => {
  198. await browser.refresh();
  199. const logs = $(`[data-testid="${TID_FEEDBACK_LOGS_OPT_IN}"]`);
  200. await browser.waitUntil(
  201. async () => {
  202. const error = await browser.execute(() => document.documentElement.dataset.fixtureError);
  203. if (error) throw new Error(`Feedback fixture failed: ${error}`);
  204. return logs.isExisting();
  205. },
  206. { timeout: 10000 },
  207. );
  208. await expect(logs).toHaveAttribute("aria-checked", "true");
  209. await $("textarea").setValue("E2E_FEEDBACK_LOG_CHOICE");
  210. if (!includeLogs) await logs.click();
  211. if (restore) {
  212. await browser.execute(() => window.feedbackSubmitFixture!.failNextCreate());
  213. await submit();
  214. const failed = await snapshot();
  215. expect(failed.jobs[0]).toMatchObject({ status: "error", includeLogs });
  216. expect(failed.calls).toEqual({ create: 1, prepare: 0, upload: [], cleanup: 0 });
  217. await browser.execute(
  218. (id) => window.feedbackSubmitFixture!.restore(id),
  219. failed.jobs[0]!.id,
  220. );
  221. await expect($("textarea")).toHaveValue("E2E_FEEDBACK_LOG_CHOICE");
  222. await expect(logs).toHaveAttribute("aria-checked", String(includeLogs));
  223. }
  224. await submit();
  225. const result = await snapshot();
  226. expect(result.jobs.at(-1)).toMatchObject({ status: "success", includeLogs });
  227. expect(result.createInputs).toHaveLength(restore ? 2 : 1);
  228. for (const input of result.createInputs) {
  229. expect(input).toMatchObject({
  230. title: "E2E_FEEDBACK_LOG_CHOICE",
  231. description: expect.stringContaining("E2E_FEEDBACK_LOG_CHOICE"),
  232. type: "bug",
  233. });
  234. }
  235. if (restore) expect(result.createInputs[1]).toEqual(result.createInputs[0]);
  236. expect(result.calls).toEqual({
  237. create: restore ? 2 : 1,
  238. prepare: includeLogs ? 1 : 0,
  239. upload: includeLogs ? ["log"] : [],
  240. cleanup: includeLogs ? 1 : 0,
  241. });
  242. });
  243. }
  244. });