feedback-upload-privacy.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import { mkdir } from "node:fs/promises";
  2. import { join, resolve } from "node:path";
  3. import { build } from "esbuild";
  4. import { TID_FEEDBACK_LOGS_OPT_IN } from "@zcode/shared/test-ids";
  5. import {
  6. pinE2ELocale,
  7. readSettings,
  8. seedSettings,
  9. waitForDefaultWorkspaceReady,
  10. } from "#e2e/helpers/desktop-app";
  11. import { startHelpConfigFixture } from "#e2e/helpers/help-client-config-fixture";
  12. async function dismissInitialOnboarding() {
  13. const onboarding = $('[data-testid="onboarding-page"]');
  14. await browser.waitUntil(
  15. async () =>
  16. (await onboarding.isExisting()) ||
  17. (await $('button[aria-label="Help"]').isExisting()) ||
  18. (await $('button[aria-label="帮助"]').isExisting()),
  19. { timeout: 15000 },
  20. );
  21. if (await onboarding.isDisplayed()) {
  22. // 当前 staging 的引导按本地完成记录触发;只关窗口后刷新会重开。
  23. // 通过现有跳过操作完成隔离测试账号的三步引导,不改变产品实现。
  24. for (let step = 0; step < 3; step += 1) {
  25. const before = await onboarding.getText();
  26. await onboarding.$("footer > button").click();
  27. await browser.waitUntil(
  28. async () => !(await onboarding.isDisplayed()) || (await onboarding.getText()) !== before,
  29. { timeout: 15000 },
  30. );
  31. if (!(await onboarding.isDisplayed())) break;
  32. }
  33. await onboarding.waitForDisplayed({ reverse: true, timeout: 15000 });
  34. }
  35. }
  36. describe("反馈上传授权(manual review)", () => {
  37. const screenshotDirectory = join(process.cwd(), ".e2e-artifacts", "feedback-upload-privacy");
  38. let settings: Awaited<ReturnType<typeof readSettings>>;
  39. let server: Awaited<ReturnType<typeof startHelpConfigFixture>>;
  40. let windowSize:
  41. | { width: number; height: number; minWidth: number; minHeight: number }
  42. | undefined;
  43. before(async () => {
  44. await mkdir(screenshotDirectory, { recursive: true });
  45. await dismissInitialOnboarding();
  46. await waitForDefaultWorkspaceReady(30000);
  47. settings = await readSettings();
  48. windowSize = await browser.electron.execute((electron) => {
  49. const window = electron.BrowserWindow.getAllWindows()[0]!;
  50. const bounds = window.getBounds();
  51. const [minWidth = 0, minHeight = 0] = window.getMinimumSize();
  52. window.setMinimumSize(360, 400);
  53. return { width: bounds.width, height: bounds.height, minWidth, minHeight };
  54. });
  55. server = await startHelpConfigFixture({ feedback_use_external_form: false });
  56. });
  57. after(async () => {
  58. await browser.keys("Escape");
  59. if (settings) await seedSettings(settings);
  60. if (windowSize)
  61. await browser.electron.execute((electron, size) => {
  62. const window = electron.BrowserWindow.getAllWindows()[0]!;
  63. window.setMinimumSize(size.minWidth, size.minHeight);
  64. window.setSize(size.width, size.height);
  65. }, windowSize);
  66. await server?.close();
  67. });
  68. for (const [locale, width, height, helpLabel, reportLabel, hint] of [
  69. ["en-US", 1280, 900, "Help", "Report", "On by default"],
  70. ["zh-CN", 430, 900, "帮助", "问题上报", "默认开启"],
  71. ] as const) {
  72. it(`FB-PRIV-01/08 / FB-DESC-01 / FB-REQ-A11Y-01: ${locale} ${width}px 反馈必填标识与日志选择`, async () => {
  73. await seedSettings({ locale, localePreference: locale, zcodeEndpointOrigin: server.origin });
  74. await browser.electron.execute(
  75. (electron, size) => {
  76. electron.BrowserWindow.getAllWindows()[0]!.setSize(size.width, size.height);
  77. },
  78. { width, height },
  79. );
  80. await browser.refresh();
  81. await dismissInitialOnboarding();
  82. await waitForDefaultWorkspaceReady(30000);
  83. // 帮助按钮的 aria-label 跟随 renderer 语言;settings 写入不会改已加载的 renderer。
  84. await pinE2ELocale(locale);
  85. const help = $(`button[aria-label="${helpLabel}"]`);
  86. await help.waitForDisplayed({ timeout: 10000 });
  87. await help.click();
  88. await $(`//*[@role="menuitem" and contains(.,"${reportLabel}")]`).click();
  89. const dialog = $('[role="dialog"]');
  90. await dialog.waitForDisplayed({ timeout: 10000 });
  91. const description = dialog.$("textarea");
  92. const submitButton = dialog.$(
  93. `button=${locale === "zh-CN" ? "提交反馈" : "Submit feedback"}`,
  94. );
  95. await expect(description).toHaveAttribute("required");
  96. await expect(description).toHaveAttribute("aria-required", "true");
  97. await expect(description).toHaveAttribute(
  98. "aria-label",
  99. locale === "zh-CN" ? "问题描述" : "Issue description",
  100. );
  101. const bugMarker = dialog.$("section h3 span[aria-hidden='true']");
  102. await expect(bugMarker).toHaveText("*");
  103. await expect(submitButton).toBeDisabled();
  104. const logs = $(`[data-testid="${TID_FEEDBACK_LOGS_OPT_IN}"]`);
  105. await expect(logs).toHaveAttribute("aria-checked", "true");
  106. await expect(dialog).toHaveText(expect.stringContaining(hint));
  107. expect(await dialog.$$("img").length).toBe(0);
  108. await logs.click();
  109. await expect(logs).toHaveAttribute("aria-checked", "false");
  110. await logs.click();
  111. await expect(logs).toHaveAttribute("aria-checked", "true");
  112. for (const theme of ["light", "dark"] as const) {
  113. await browser.execute((value) => {
  114. const actions = (
  115. window as typeof window & { __testActions?: { setTheme?: (theme: string) => void } }
  116. ).__testActions;
  117. if (!actions?.setTheme) throw new Error("缺少主题测试入口");
  118. actions.setTheme(value);
  119. }, theme);
  120. await expect(bugMarker).toBeDisplayed();
  121. await logs.scrollIntoView();
  122. await expect(logs).toBeDisplayed();
  123. await browser.saveScreenshot(
  124. join(screenshotDirectory, `zcode-feedback-${locale}-${theme}.png`),
  125. );
  126. }
  127. await browser.keys("Escape");
  128. await help.click();
  129. const requestLabel = locale === "zh-CN" ? "给产品提需求" : "Request a feature";
  130. await $(`//*[@role="menuitem" and contains(.,"${requestLabel}")]`).click();
  131. await dialog.waitForDisplayed({ timeout: 10000 });
  132. const requiredMarkers = await dialog.$$("section h3 span");
  133. expect(requiredMarkers.length).toBe(2);
  134. for (const marker of requiredMarkers) {
  135. await expect(marker).toHaveText("*");
  136. await expect(marker).toHaveAttribute("aria-hidden", "true");
  137. }
  138. const requestFields = await dialog.$$("textarea");
  139. expect(requestFields.length).toBe(2);
  140. const fieldLabels =
  141. locale === "zh-CN"
  142. ? ["需求描述", "期望的解决方案"]
  143. : ["Request description", "Expected solution"];
  144. for await (const [index, field] of requestFields.entries()) {
  145. await expect(field).toHaveAttribute("required");
  146. await expect(field).toHaveAttribute("aria-required", "true");
  147. await expect(field).toHaveAttribute("aria-label", fieldLabels[index]);
  148. }
  149. await expect(
  150. dialog.$(`button=${locale === "zh-CN" ? "提交需求" : "Submit request"}`),
  151. ).toBeDisabled();
  152. for (const theme of ["light", "dark"] as const) {
  153. await browser.execute((value) => {
  154. const actions = (
  155. window as typeof window & { __testActions?: { setTheme?: (theme: string) => void } }
  156. ).__testActions;
  157. if (!actions?.setTheme) throw new Error("缺少主题测试入口");
  158. actions.setTheme(value);
  159. }, theme);
  160. for (const marker of requiredMarkers) await expect(marker).toBeDisplayed();
  161. await browser.saveScreenshot(
  162. join(screenshotDirectory, `zcode-feature-request-${locale}-${theme}.png`),
  163. );
  164. }
  165. await browser.keys("Escape");
  166. });
  167. }
  168. });
  169. // 浏览器 fixture 单独由 esbuild 打包,不向 WDIO 的 Window 类型注入产品 store 声明。
  170. declare global {
  171. interface Window {
  172. feedbackSubmitFixture?: {
  173. failNextCreate(): void;
  174. restore(jobId: string): void;
  175. snapshot(): {
  176. calls: { create: number; prepare: number; upload: string[]; cleanup: number };
  177. createInputs: Array<Record<string, unknown>>;
  178. jobs: Array<{ id: string; status: string; includeLogs?: boolean }>;
  179. };
  180. };
  181. }
  182. }
  183. describe("FB-PRIV-02: 反馈表单到日志服务边界", () => {
  184. let server: Awaited<ReturnType<typeof startHelpConfigFixture>>;
  185. let mainHandle: string;
  186. let windowId: number;
  187. before(async () => {
  188. // 按当前 spec 定位仓库根目录,否则测试页面会从仓库外查找入口与 tsconfig。
  189. const root = resolve(import.meta.dirname, "../../../../../..");
  190. const bundle = await build({
  191. entryPoints: [resolve(root, "packages/desktop/test/e2e/helpers/feedback-submit-fixture.js")],
  192. bundle: true,
  193. write: false,
  194. platform: "browser",
  195. format: "iife",
  196. alias: { "@": resolve(root, "packages/ui/src") },
  197. define: { "import.meta.env": "{}", "process.env.NODE_ENV": '"test"' },
  198. tsconfig: resolve(root, "packages/ui/tsconfig.json"),
  199. });
  200. // fixture 页的脚本在 head 同步执行;等 DOM 就绪后再挂载真实表单。
  201. server = await startHelpConfigFixture(
  202. {},
  203. `
  204. window.addEventListener("error", (event) => {
  205. document.documentElement.dataset.fixtureError = event.message;
  206. });
  207. window.addEventListener("DOMContentLoaded", () => { ${bundle.outputFiles[0]!.text} });
  208. `,
  209. );
  210. mainHandle = await browser.getWindowHandle();
  211. const existing = await browser.getWindowHandles();
  212. windowId = await browser.electron.execute(async (electron, url) => {
  213. const win = new electron.BrowserWindow({
  214. width: 1000,
  215. height: 900,
  216. show: false,
  217. // 隐藏窗口也保持回调计时稳定,避免 Chromium 后台节流影响表单完成回调。
  218. webPreferences: { sandbox: true, nodeIntegration: false, backgroundThrottling: false },
  219. });
  220. await win.loadURL(url);
  221. return win.id;
  222. }, server.origin);
  223. await browser.waitUntil(
  224. async () => (await browser.getWindowHandles()).length > existing.length,
  225. );
  226. const handle = (await browser.getWindowHandles()).find((item) => !existing.includes(item))!;
  227. await browser.switchToWindow(handle);
  228. });
  229. after(async () => {
  230. if (windowId)
  231. await browser.electron.execute(
  232. (electron, id) => electron.BrowserWindow.fromId(id)?.destroy(),
  233. windowId,
  234. );
  235. if (mainHandle) await browser.switchToWindow(mainHandle);
  236. await server?.close();
  237. });
  238. const snapshot = () => browser.execute(() => window.feedbackSubmitFixture!.snapshot());
  239. const submit = async () => {
  240. const previousJobs = (await snapshot()).jobs.length;
  241. await $("button=Submit feedback").click();
  242. await browser.waitUntil(
  243. async () => {
  244. const jobs = (await snapshot()).jobs;
  245. return jobs.length > previousJobs && ["success", "error"].includes(jobs.at(-1)!.status);
  246. },
  247. { timeout: 10000 },
  248. );
  249. };
  250. for (const kind of ["ssh", "wsl", "docker"] as const) {
  251. for (const includeLogs of [true, false]) {
  252. const caseId = includeLogs ? "FB-REMOTE-LOG-01" : "FB-REMOTE-LOG-02";
  253. it(`${caseId}: ${kind} 连接反馈${includeLogs ? "默认上传日志" : "允许取消日志"}`, async () => {
  254. await browser.url(`${server.origin}/#remoteKind=${kind}`);
  255. await browser.refresh();
  256. const openConnectionFeedback = async () => {
  257. const feedback = $("button=Feedback");
  258. await feedback.waitForDisplayed({ timeout: 10000 });
  259. await feedback.click();
  260. const logs = $(`[data-testid="${TID_FEEDBACK_LOGS_OPT_IN}"]`);
  261. await expect(logs).toHaveAttribute("aria-checked", "true");
  262. return logs;
  263. };
  264. const logs = await openConnectionFeedback();
  265. await expect($("textarea")).toHaveValue(
  266. expect.stringContaining("E2E_REMOTE_CONNECTION_LOG"),
  267. );
  268. if (!includeLogs) {
  269. await logs.click();
  270. await expect(logs).toHaveAttribute("aria-checked", "false");
  271. }
  272. await submit();
  273. const result = await snapshot();
  274. expect(result.jobs.at(-1)).toMatchObject({ status: "success", includeLogs });
  275. expect(result.createInputs).toHaveLength(1);
  276. expect(result.createInputs[0]).toMatchObject({
  277. title: "An error occurred during remote connection. Please help investigate.",
  278. description: expect.stringContaining("E2E_REMOTE_CONNECTION_LOG"),
  279. type: "bug",
  280. });
  281. expect(result.createInputs[0]!.description).toContain("E2E_REMOTE_FEEDBACK_FAILED");
  282. expect(result.calls).toEqual({
  283. create: 1,
  284. prepare: includeLogs ? 1 : 0,
  285. upload: includeLogs ? ["log"] : [],
  286. cleanup: includeLogs ? 1 : 0,
  287. });
  288. if (!includeLogs) await openConnectionFeedback();
  289. });
  290. }
  291. }
  292. it("FB-DESC-02: 空描述不调用服务,有效描述可提交", async () => {
  293. await browser.url(server.origin);
  294. const description = $("textarea");
  295. await description.waitForDisplayed({ timeout: 10000 });
  296. const submitButton = $("button=Submit feedback");
  297. await expect(description).toHaveAttribute("required");
  298. await expect(submitButton).toBeDisabled();
  299. await description.setValue(" ");
  300. await expect(submitButton).toBeDisabled();
  301. expect((await snapshot()).calls).toEqual({ create: 0, prepare: 0, upload: [], cleanup: 0 });
  302. await description.setValue("E2E_FEEDBACK_REQUIRED_DESCRIPTION");
  303. await expect(submitButton).toBeEnabled();
  304. // WebDriver clearValue 不保证触发 React 的 input;通过实际退格事件验证清空。
  305. await description.setValue(" ");
  306. await browser.keys("Backspace");
  307. await expect(description).toHaveValue("");
  308. await expect(submitButton).toBeDisabled();
  309. expect((await snapshot()).calls).toEqual({ create: 0, prepare: 0, upload: [], cleanup: 0 });
  310. await description.setValue("E2E_FEEDBACK_REQUIRED_DESCRIPTION");
  311. await submit();
  312. const result = await snapshot();
  313. expect(result.createInputs).toHaveLength(1);
  314. expect(result.createInputs[0]).toMatchObject({
  315. title: "E2E_FEEDBACK_REQUIRED_DESCRIPTION",
  316. description: expect.stringContaining("E2E_FEEDBACK_REQUIRED_DESCRIPTION"),
  317. });
  318. });
  319. for (const [name, includeLogs, restore] of [
  320. ["默认提交", true, false],
  321. ["取消后提交", false, false],
  322. ["开启日志的后台任务恢复后重试", true, true],
  323. ["取消日志的后台任务恢复后重试", false, true],
  324. ] as const) {
  325. it(`${name}:验证实际归档/上传调用`, async () => {
  326. await browser.refresh();
  327. const logs = $(`[data-testid="${TID_FEEDBACK_LOGS_OPT_IN}"]`);
  328. await browser.waitUntil(
  329. async () => {
  330. const error = await browser.execute(() => document.documentElement.dataset.fixtureError);
  331. if (error) throw new Error(`Feedback fixture failed: ${error}`);
  332. return logs.isExisting();
  333. },
  334. { timeout: 10000 },
  335. );
  336. await expect(logs).toHaveAttribute("aria-checked", "true");
  337. await $("textarea").setValue("E2E_FEEDBACK_LOG_CHOICE");
  338. if (!includeLogs) await logs.click();
  339. if (restore) {
  340. await browser.execute(() => window.feedbackSubmitFixture!.failNextCreate());
  341. await submit();
  342. const failed = await snapshot();
  343. expect(failed.jobs[0]).toMatchObject({ status: "error", includeLogs });
  344. expect(failed.calls).toEqual({ create: 1, prepare: 0, upload: [], cleanup: 0 });
  345. await browser.execute(
  346. (id) => window.feedbackSubmitFixture!.restore(id),
  347. failed.jobs[0]!.id,
  348. );
  349. await expect($("textarea")).toHaveValue("E2E_FEEDBACK_LOG_CHOICE");
  350. await expect(logs).toHaveAttribute("aria-checked", String(includeLogs));
  351. }
  352. await submit();
  353. const result = await snapshot();
  354. expect(result.jobs.at(-1)).toMatchObject({ status: "success", includeLogs });
  355. expect(result.createInputs).toHaveLength(restore ? 2 : 1);
  356. for (const input of result.createInputs) {
  357. expect(input).toMatchObject({
  358. title: "E2E_FEEDBACK_LOG_CHOICE",
  359. description: expect.stringContaining("E2E_FEEDBACK_LOG_CHOICE"),
  360. type: "bug",
  361. });
  362. }
  363. if (restore) expect(result.createInputs[1]).toEqual(result.createInputs[0]);
  364. expect(result.calls).toEqual({
  365. create: restore ? 2 : 1,
  366. prepare: includeLogs ? 1 : 0,
  367. upload: includeLogs ? ["log"] : [],
  368. cleanup: includeLogs ? 1 : 0,
  369. });
  370. });
  371. }
  372. });