feedback-upload-privacy.test.ts 16 KB

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