off-peak-limit-pre-gate.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { TID_OFFPEAK_CREATE_BUTTON, TID_AUTOMATIONS_OPEN } from "@zcode/shared";
  2. import { mkdirSync } from "node:fs";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import {
  6. clearAppData,
  7. clickTestIdByDom,
  8. waitForDefaultWorkspaceReady,
  9. } from "./helpers/desktop-app.js";
  10. import { loginWithApiKey } from "./helpers/conversation-session.js";
  11. /**
  12. * 闲时任务「服务端额度快照前置置灰」端到端(D39)。
  13. *
  14. * 由进程内 mock 网关驱动(wdio.conf startOffPeakE2EMock:本 spec 命中时额外设
  15. * ZCODE_OFFPEAK_MOCK_AVAILABILITY_BLOCK_MS=3600000)。client/configs 不再下发 limit,
  16. * UI 通过 GET /ticket/availability 得到 can_take_number=false + next_take_at。
  17. *
  18. * 断言:入口加载后创建按钮提前置灰,Tooltip 同时说明额度耗尽和服务端恢复时间。
  19. * 全程带 pause 并在关键态截图(ZCODE_OFFPEAK_SHOT_DIR)供录屏 review。
  20. */
  21. // Bugfix:硬编码系统临时目录在 Windows 不可用,默认截图目录必须跟随运行平台。
  22. const SHOT_DIR =
  23. process.env.ZCODE_OFFPEAK_SHOT_DIR ?? join(tmpdir(), "zc-offpeak-verify-shots");
  24. let shotSeq = 0;
  25. async function shot(label: string): Promise<void> {
  26. shotSeq += 1;
  27. const name = `${String(shotSeq).padStart(2, "0")}-${label}.png`;
  28. try {
  29. await browser.saveScreenshot(join(SHOT_DIR, name));
  30. } catch {
  31. // 截图失败不阻塞断言(录屏为辅)。
  32. }
  33. }
  34. describe("闲时任务服务端额度快照置灰 E2E(D39)", () => {
  35. before(() => {
  36. try {
  37. mkdirSync(SHOT_DIR, { recursive: true });
  38. } catch {
  39. // ignore
  40. }
  41. });
  42. after(async () => {
  43. await browser.electron.restoreAllMocks();
  44. await clearAppData();
  45. });
  46. it("availability 不可创建时按钮置灰并显示 next_take_at", async function () {
  47. this.timeout(150000);
  48. // 会话就绪:登录(若停在欢迎页)+ 默认工作区就绪即可进 Automations(在 Settings 内),
  49. // 不走 startNewTask 的对话草稿流程(与 off-peak 无关)。off-peak mock 网关免真实模型请求。
  50. try {
  51. await waitForDefaultWorkspaceReady(30000);
  52. } catch {
  53. await loginWithApiKey();
  54. await waitForDefaultWorkspaceReady(30000);
  55. }
  56. // 1) 打开 Automations 主视图。
  57. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, {
  58. timeoutMsg: "侧栏没有出现 Automations 入口",
  59. });
  60. await browser.pause(2500); // 录屏:停在主视图,展示初始按钮态
  61. await shot("automations-open");
  62. // 2) availability=false:按钮应在实际 POST /ticket 前置灰。
  63. await browser.waitUntil(async () => (await offPeakCreateButtonState()).present, {
  64. timeout: 10000,
  65. timeoutMsg: "闲时任务创建按钮没有出现(灰度未命中?)",
  66. });
  67. // 回归用户截图中的大字号场景,确保相对时长文案仍完整落在 Tooltip 背景内。
  68. await browser.execute(() => {
  69. document.documentElement.style.setProperty("--ui-font-size", "20px");
  70. });
  71. const state = await offPeakCreateButtonState();
  72. if (!state.disabled) {
  73. throw new Error("服务端额度不可用时,创建按钮应提前置灰");
  74. }
  75. if (!state.tooltip || !/额度|limit/i.test(state.tooltip)) {
  76. throw new Error(`额度置灰必须显示 Tooltip:${state.tooltip ?? "<empty>"}`);
  77. }
  78. if (!/(小时|分钟|hr|min)/i.test(state.tooltip)) {
  79. throw new Error(`额度 Tooltip 必须包含小时/分钟剩余时长:${state.tooltip}`);
  80. }
  81. if (/\d{4}|年|月|日/.test(state.tooltip)) {
  82. throw new Error(`额度 Tooltip 不应展示年月日或绝对日期:${state.tooltip}`);
  83. }
  84. if (!state.contentContained) {
  85. throw new Error("额度 Tooltip 文案必须完整包含在背景内");
  86. }
  87. await shot("create-button-quota-disabled");
  88. await browser.pause(3000); // 录屏:停在额度置灰态,展示最终效果
  89. });
  90. });
  91. /** 创建按钮当前态:是否存在 / 是否 disabled / Radix Tooltip 文案。 */
  92. async function offPeakCreateButtonState(): Promise<{
  93. present: boolean;
  94. disabled: boolean;
  95. tooltip: string | null;
  96. contentContained: boolean;
  97. }> {
  98. const state = await browser.execute((btnTid) => {
  99. const button = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  100. (item) => item.dataset.testid === btnTid,
  101. );
  102. if (!(button instanceof HTMLButtonElement)) {
  103. return { present: false, disabled: false };
  104. }
  105. return {
  106. present: true,
  107. disabled: button.disabled,
  108. };
  109. }, TID_OFFPEAK_CREATE_BUTTON);
  110. if (!state.present) {
  111. return { ...state, tooltip: null, contentContained: false };
  112. }
  113. const button = await $(`[data-testid="${TID_OFFPEAK_CREATE_BUTTON}"]`);
  114. const trigger = await button.$("..");
  115. // Radix Tooltip 依赖真实 pointer enter/leave 生命周期;合成的单个 pointermove 不会更新其
  116. // 内部 hover 状态,所以用 WebDriver 悬停并等待 portal 进入 open 状态。
  117. await trigger.moveTo();
  118. await browser.waitUntil(
  119. () =>
  120. browser.execute(() =>
  121. Array.from(
  122. document.querySelectorAll<HTMLElement>("[data-slot='tooltip-content']"),
  123. ).some((item) => item.dataset.state !== "closed"),
  124. ),
  125. { timeout: 5_000, timeoutMsg: "额度 Tooltip 没有在真实悬停后打开" },
  126. );
  127. const tooltip = await browser.execute(() => {
  128. const content = Array.from(
  129. document.querySelectorAll<HTMLElement>("[data-slot='tooltip-content']"),
  130. ).find((item) => item.dataset.state !== "closed");
  131. // ControlHintTooltip 的正文令牌已从 text-ui-base 收敛到 text-ui-sm;tooltip 已经
  132. // 真实打开,旧 E2E 因查询旧 class 而把有效文案误判为空。
  133. const title = content?.querySelector<HTMLElement>(":scope > span.text-ui-sm");
  134. if (!content || !title) {
  135. return { text: null, contentContained: false };
  136. }
  137. const contentRect = content.getBoundingClientRect();
  138. const titleRect = title.getBoundingClientRect();
  139. return {
  140. text: title.textContent?.trim() ?? null,
  141. contentContained:
  142. titleRect.left >= contentRect.left - 0.5 &&
  143. titleRect.right <= contentRect.right + 0.5 &&
  144. titleRect.top >= contentRect.top - 0.5 &&
  145. titleRect.bottom <= contentRect.bottom + 0.5,
  146. };
  147. });
  148. return {
  149. ...state,
  150. tooltip: tooltip.text,
  151. contentContained: tooltip.contentContained,
  152. };
  153. }