usage-stats-app-usage.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {
  2. TID_SETTINGS_SECTION_NAV,
  3. TID_SETTINGS_USAGE_TAB,
  4. TID_TASK_SETTINGS_BUTTON,
  5. testId,
  6. } from "@zcode/shared/test-ids";
  7. import {
  8. clearAppData,
  9. clickTestIdByDom,
  10. getE2EAppDataPaths,
  11. waitForDefaultWorkspaceReady,
  12. } from "#e2e/helpers/desktop-app";
  13. import { clearAppUsageE2EFixture } from "#e2e/helpers/usage-stats-app-usage-fixture";
  14. describe("Usage Stats App Usage E2E", () => {
  15. after(async () => {
  16. await clearAppData();
  17. });
  18. it("US-AU-02/03/04/05: 聚合、range、Token Activity 与模型图表", async function () {
  19. this.timeout(120_000);
  20. await openAppUsage();
  21. await assertBodyIncludes(["e2e-model-alpha", "e2e-model-beta"]);
  22. await assertBodyIncludesAny(["Token Activity", "Token 活动"]);
  23. await assertBodyIncludesAny(["Daily Token Trend", "每日 Token 趋势图"]);
  24. await assertBodyIncludesAny(["Model Usage", "模型用量"]);
  25. // tokenNumberFormat 按 locale compact:英文为 9.5K,中文在 1 万以下保留 9510。
  26. await assertBodyIncludesAny(["9.5K", "9,510", "9510"]);
  27. await clickButtonByText(["Last 7 days", "近 7 日"]);
  28. await assertPressedButton(["Last 7 days", "近 7 日"]);
  29. await assertBodyIncludes(["e2e-model-alpha", "e2e-model-beta"]);
  30. // 7d 使用精确时间戳边界,恰好第 7 天的 fixture 会落在窗口外。
  31. await assertBodyIncludesAny(["2.4K", "2,406", "2406"]);
  32. await clickButtonByText(["Weekly", "每周"]);
  33. await assertPressedButton(["Weekly", "每周"]);
  34. await clickButtonByText(["Cumulative", "累计"]);
  35. await assertPressedButton(["Cumulative", "累计"]);
  36. await clickButtonByText(["Last 30 days", "近 30 日"]);
  37. await assertPressedButton(["Last 30 days", "近 30 日"]);
  38. await assertBodyIncludes(["e2e-model-gamma"]);
  39. await assertBodyIncludesAny(["4.5K", "4,510", "4510"]);
  40. });
  41. it("US-AU-01: usage facts 清空后显示确定空态", async function () {
  42. this.timeout(60_000);
  43. clearAppUsageE2EFixture(getE2EAppDataPaths().homeDir);
  44. await clickButtonByText(["Refresh", "刷新"]);
  45. await browser.waitUntil(
  46. async () => {
  47. const body = await $("body").getText();
  48. return /No usage data yet|还没有可展示的数据/.test(body);
  49. },
  50. { timeout: 20_000, timeoutMsg: "清空 usage facts 后没有显示 App Usage 空态" },
  51. );
  52. await assertBodyExcludes(["e2e-model-alpha", "e2e-model-gamma"]);
  53. });
  54. });
  55. async function openAppUsage(): Promise<void> {
  56. await waitForDefaultWorkspaceReady(60_000);
  57. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  58. timeout: 20_000,
  59. timeoutMsg: "App Usage E2E 无法打开设置页",
  60. });
  61. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "usage"), {
  62. timeout: 20_000,
  63. timeoutMsg: "设置页 Usage 分区没有出现",
  64. });
  65. await clickTestIdByDom(testId(TID_SETTINGS_USAGE_TAB, "app"), {
  66. timeout: 20_000,
  67. timeoutMsg: "App Usage tab 没有出现",
  68. });
  69. await browser.waitUntil(
  70. async () => /Token activity|Token 活动/i.test(await $("body").getText()),
  71. { timeout: 30_000, timeoutMsg: "App Usage 数据没有完成加载" },
  72. );
  73. }
  74. async function clickButtonByText(labels: string[]): Promise<void> {
  75. for (const button of await $$("button")) {
  76. if (labels.includes((await button.getText()).trim())) {
  77. await button.click();
  78. return;
  79. }
  80. }
  81. throw new Error(`找不到按钮: ${labels.join("/")}`);
  82. }
  83. async function assertPressedButton(labels: string[]): Promise<void> {
  84. await browser.waitUntil(
  85. async () =>
  86. browser.execute((candidateLabels) => {
  87. const target = Array.from(document.querySelectorAll<HTMLButtonElement>("button")).find(
  88. (button) => candidateLabels.includes(button.textContent?.trim() ?? ""),
  89. );
  90. return target?.getAttribute("data-state") === "active";
  91. }, labels),
  92. { timeout: 10_000, timeoutMsg: `${labels.join("/")} 没有进入 active 状态` },
  93. );
  94. }
  95. async function assertBodyIncludes(values: string[]): Promise<void> {
  96. await browser.waitUntil(
  97. async () => {
  98. const body = await $("body").getText();
  99. const normalizedBody = body.toLocaleLowerCase();
  100. return values.every((value) => normalizedBody.includes(value.toLocaleLowerCase()));
  101. },
  102. { timeout: 20_000, timeoutMsg: `页面缺少文本: ${values.join(", ")}` },
  103. );
  104. }
  105. async function assertBodyIncludesAny(values: string[]): Promise<void> {
  106. await browser.waitUntil(
  107. async () => {
  108. const body = await $("body").getText();
  109. const normalizedBody = body.toLocaleLowerCase();
  110. return values.some((value) => normalizedBody.includes(value.toLocaleLowerCase()));
  111. },
  112. { timeout: 20_000, timeoutMsg: `页面未出现任一文本: ${values.join(", ")}` },
  113. );
  114. }
  115. async function assertBodyExcludes(values: string[]): Promise<void> {
  116. await browser.waitUntil(
  117. async () => {
  118. const body = await $("body").getText();
  119. return values.every((value) => !body.includes(value));
  120. },
  121. { timeout: 20_000, timeoutMsg: `页面仍包含文本: ${values.join(", ")}` },
  122. );
  123. }