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

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