settings-language-system-default.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { mkdir, readFile, writeFile } from "node:fs/promises";
  2. import { homedir } from "node:os";
  3. import { join } from "node:path";
  4. import {
  5. TID_SETTINGS_LOCALE_SELECT_ITEM,
  6. TID_SETTINGS_LOCALE_SELECT_TRIGGER,
  7. TID_SETTINGS_PAGE,
  8. TID_SETTINGS_SECTION_NAV,
  9. TID_TASK_SETTINGS_BUTTON,
  10. testId,
  11. } from "@zcode/shared/test-ids";
  12. import {
  13. DEFAULT_WORKSPACE,
  14. clearAppData,
  15. clickTestIdByDom,
  16. clickTestIdByWebDriver,
  17. quitElectronAppGracefully,
  18. waitForDefaultWorkspaceReady,
  19. waitForWorkspaceApp,
  20. } from "#e2e/helpers/desktop-app";
  21. import { skipOccupationOnboardingIfPresent } from "#e2e/helpers/occupation-onboarding";
  22. import { sel } from "#e2e/helpers/selectors";
  23. const SETTINGS_FILE = join(homedir(), ".zcode", "v2", "setting.json");
  24. describe("设置语言 System default E2E", () => {
  25. after(async () => {
  26. await browser.electron.restoreAllMocks();
  27. await clearAppData();
  28. });
  29. it("SL-01: 系统首选中文且 app locale 为英文时 System default 应解析为中文", async function () {
  30. this.timeout(90000);
  31. await prepareEnglishPreference();
  32. const getPreferredSystemLanguagesMock = await browser.electron.mock(
  33. "app",
  34. "getPreferredSystemLanguages",
  35. );
  36. const getLocaleMock = await browser.electron.mock("app", "getLocale");
  37. await getPreferredSystemLanguagesMock.mockReturnValue(["zh-Hans-CN", "en-CN"]);
  38. await getLocaleMock.mockReturnValue("en-US");
  39. await skipOccupationOnboardingIfPresent();
  40. await waitForDefaultWorkspaceReady(30000);
  41. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  42. await openGeneralSettings();
  43. await assertBodyTextIncludes(["Language"]);
  44. await selectLocalePreference("system");
  45. await assertBodyTextIncludes(["界面语言", "系统默认"]);
  46. await browser.waitUntil(
  47. async () => {
  48. const settings = await readSettings();
  49. return settings.localePreference === "system" && settings.locale === "zh-CN";
  50. },
  51. {
  52. timeout: 15000,
  53. timeoutMsg: "System default 没有按 Electron 首选系统语言写回 zh-CN",
  54. },
  55. );
  56. });
  57. });
  58. async function prepareEnglishPreference() {
  59. await quitElectronAppGracefully();
  60. await writeSettings({
  61. locale: "en-US",
  62. localePreference: "en-US",
  63. lastWorkspaceSession: [
  64. { kind: "local", workspacePath: DEFAULT_WORKSPACE, workspacePurpose: "project" },
  65. ],
  66. lastActiveTabIndex: 0,
  67. });
  68. await browser.reloadSession();
  69. }
  70. async function openGeneralSettings() {
  71. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  72. timeout: 15000,
  73. timeoutMsg: "没有找到设置入口按钮",
  74. });
  75. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  76. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"), {
  77. timeout: 15000,
  78. timeoutMsg: "设置页没有出现通用分区入口",
  79. });
  80. }
  81. async function selectLocalePreference(preference: "system" | "zh-CN" | "en-US") {
  82. await clickTestIdByWebDriver(TID_SETTINGS_LOCALE_SELECT_TRIGGER, {
  83. timeout: 15000,
  84. timeoutMsg: "界面语言下拉触发器没有出现",
  85. });
  86. await clickTestIdByDom(testId(TID_SETTINGS_LOCALE_SELECT_ITEM, preference), {
  87. timeout: 15000,
  88. timeoutMsg: `界面语言下拉项没有出现: ${preference}`,
  89. });
  90. }
  91. async function assertBodyTextIncludes(expectedTexts: string[]) {
  92. await browser.waitUntil(
  93. async () => {
  94. const bodyText = await browser.execute(() => document.body.innerText);
  95. return expectedTexts.every((text) => bodyText.includes(text));
  96. },
  97. {
  98. timeout: 15000,
  99. timeoutMsg: `页面没有显示预期文本: ${expectedTexts.join(" / ")}`,
  100. },
  101. );
  102. }
  103. async function writeSettings(patch: Record<string, unknown>) {
  104. await mkdir(join(homedir(), ".zcode", "v2"), { recursive: true });
  105. const current = await readSettings();
  106. await writeFile(SETTINGS_FILE, JSON.stringify({ ...current, ...patch }, null, 2), "utf-8");
  107. }
  108. async function readSettings(): Promise<Record<string, unknown>> {
  109. try {
  110. return JSON.parse(await readFile(SETTINGS_FILE, "utf-8")) as Record<string, unknown>;
  111. } catch {
  112. return {};
  113. }
  114. }