codingPlanQuotaResetUi.test.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. import { describe, expect, it } from "vitest";
  2. import type { CodingPlanResetStatusSnapshot, UsageQuotaLimit } from "@zcode/shared";
  3. import {
  4. CODING_PLAN_QUOTA_RESET_AUTOMATIC_PROCESSING_MS,
  5. CODING_PLAN_QUOTA_RESET_DONE_DISPLAY_MS,
  6. advanceCodingPlanQuotaResetCelebration,
  7. applyCodingPlanQuotaResetStatus,
  8. completeCodingPlanQuotaResetEntitlementRefresh,
  9. failCodingPlanQuotaResetManualUse,
  10. hasUsableCodingPlanQuotaResetOpportunity,
  11. startCodingPlanQuotaResetManualUse,
  12. resolveCodingPlanQuotaResetActionCompletedAt,
  13. resolveCodingPlanQuotaResetAutomaticPhase,
  14. resolveCodingPlanQuotaResetLimit,
  15. resolveCodingPlanQuotaResetRemainingPercentage,
  16. resolveCodingPlanQuotaResetStatusVisible,
  17. mergeCodingPlanQuotaResetOpportunityBadges,
  18. } from "@/lib/codingPlanQuotaResetUi.js";
  19. const FIVE_HOURS_MS = 5 * 60 * 60 * 1_000;
  20. const WEEK_MS = 7 * 24 * 60 * 60 * 1_000;
  21. function buildLimit(overrides: Partial<UsageQuotaLimit> = {}): UsageQuotaLimit {
  22. return {
  23. type: "TOKENS_LIMIT",
  24. percentage: 42,
  25. nextResetTime: Date.UTC(2026, 7, 7, 15, 0, 0),
  26. usageDetails: [],
  27. ...overrides,
  28. };
  29. }
  30. describe("coding plan quota reset ui state", () => {
  31. it("触发器撒花只认自动完成的新 used_at,手动完成不播放", () => {
  32. // 手动核销(非自动完成窗口):无论是否经历 processing,触发器都不撒花。
  33. const manualDone = advanceCodingPlanQuotaResetCelebration(null, {
  34. sourceKey: "builtin:personal",
  35. completedAt: 1_000,
  36. automaticCompletion: false,
  37. });
  38. expect(manualDone.shouldCelebrate).toBe(false);
  39. // 自动完成:同一 source 的新 used_at 播放一次,重复渲染/轮询不重播。
  40. const automaticDone = advanceCodingPlanQuotaResetCelebration(manualDone.state, {
  41. sourceKey: "builtin:personal",
  42. completedAt: 2_000,
  43. automaticCompletion: true,
  44. });
  45. expect(automaticDone.shouldCelebrate).toBe(true);
  46. expect(
  47. advanceCodingPlanQuotaResetCelebration(automaticDone.state, {
  48. sourceKey: "builtin:personal",
  49. completedAt: 2_000,
  50. automaticCompletion: true,
  51. }).shouldCelebrate,
  52. ).toBe(false);
  53. // 同一 source 之后又发生新的自动重置(新 used_at),仍应再播放一次。
  54. const nextAutomatic = advanceCodingPlanQuotaResetCelebration(automaticDone.state, {
  55. sourceKey: "builtin:personal",
  56. completedAt: 3_000,
  57. automaticCompletion: true,
  58. });
  59. expect(nextAutomatic.shouldCelebrate).toBe(true);
  60. // source 切换清空轨迹:新 source 的同一 used_at 独立判断,不继承旧 source 的记录。
  61. const switched = advanceCodingPlanQuotaResetCelebration(nextAutomatic.state, {
  62. sourceKey: "team-plan:builtin:team-a",
  63. completedAt: 3_000,
  64. automaticCompletion: true,
  65. });
  66. expect(switched.shouldCelebrate).toBe(true);
  67. expect(switched.state).toEqual({
  68. sourceKey: "team-plan:builtin:team-a",
  69. celebratedCompletedAt: 3_000,
  70. });
  71. // 没有 completedAt 时(available/processing)永不撒花。
  72. expect(
  73. advanceCodingPlanQuotaResetCelebration(switched.state, {
  74. sourceKey: "team-plan:builtin:team-a",
  75. completedAt: null,
  76. automaticCompletion: true,
  77. }).shouldCelebrate,
  78. ).toBe(false);
  79. });
  80. it("只根据服务端有效机会创建 AVAILABLE,取最早过期时间", () => {
  81. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  82. const status: CodingPlanResetStatusSnapshot = {
  83. availableFiveHourResets: [
  84. { expireAt: now + 60_000 },
  85. { expireAt: now + 30_000 },
  86. { expireAt: now - 1 },
  87. ],
  88. availableWeekResets: [],
  89. latestFiveHourResetHistory: null,
  90. latestWeekResetHistory: null,
  91. hasUnreadHistory: false,
  92. };
  93. expect(applyCodingPlanQuotaResetStatus(null, status, now)).toEqual({
  94. status: "available",
  95. opportunityCount: 2,
  96. opportunityExpiresAt: now + 30_000,
  97. startedAt: null,
  98. completedAt: null,
  99. observedAt: null,
  100. quotaOverridePending: false,
  101. nextResetAt: null,
  102. idempotencyKey: null,
  103. error: null,
  104. });
  105. });
  106. it("旧历史未标记 unread 时不在首次挂载伪造完成态", () => {
  107. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  108. expect(
  109. applyCodingPlanQuotaResetStatus(
  110. null,
  111. {
  112. availableFiveHourResets: [],
  113. availableWeekResets: [],
  114. latestFiveHourResetHistory: { usedAt: now - FIVE_HOURS_MS },
  115. latestWeekResetHistory: null,
  116. hasUnreadHistory: false,
  117. },
  118. now,
  119. ),
  120. ).toBeNull();
  121. });
  122. it("自动 unread 历史或手动 processing 对账后使用服务端 usedAt 完成", () => {
  123. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  124. const usedAt = now - 1_000;
  125. const status: CodingPlanResetStatusSnapshot = {
  126. availableFiveHourResets: [],
  127. availableWeekResets: [],
  128. latestFiveHourResetHistory: { usedAt },
  129. latestWeekResetHistory: null,
  130. hasUnreadHistory: true,
  131. };
  132. const automatic = applyCodingPlanQuotaResetStatus(null, status, now);
  133. expect(automatic?.status).toBe("completed");
  134. expect(automatic?.completedAt).toBe(usedAt);
  135. expect(automatic?.observedAt).toBe(now);
  136. expect(automatic?.quotaOverridePending).toBe(true);
  137. const available = applyCodingPlanQuotaResetStatus(
  138. null,
  139. {
  140. ...status,
  141. latestFiveHourResetHistory: null,
  142. hasUnreadHistory: false,
  143. availableFiveHourResets: [{ expireAt: now + 30_000 }],
  144. },
  145. now,
  146. );
  147. const processing = startCodingPlanQuotaResetManualUse(available, "retry-same-key", now);
  148. // 手动对账的 manualStartedAt 由 hook 共享轨迹给出:used_at 已不同于点击前基线。
  149. const manual = applyCodingPlanQuotaResetStatus(
  150. processing,
  151. { ...status, hasUnreadHistory: false },
  152. now + 1_000,
  153. now,
  154. );
  155. expect(manual?.status).toBe("completed");
  156. expect(manual?.completedAt).toBe(usedAt);
  157. expect(manual?.idempotencyKey).toBeNull();
  158. });
  159. it("跨入口观察手动完成历史时使用共享 startedAt,重复轮询不退化为自动完成", () => {
  160. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  161. const usedAt = now + 1_000;
  162. const manualStartedAt = now - 500;
  163. const snapshot = {
  164. availableFiveHourResets: [],
  165. availableWeekResets: [],
  166. latestFiveHourResetHistory: { usedAt },
  167. latestWeekResetHistory: null,
  168. // 其他入口读取前,发起入口可能已经把 unread 历史标记为已读。
  169. hasUnreadHistory: false,
  170. };
  171. const completed = applyCodingPlanQuotaResetStatus(null, snapshot, now, manualStartedAt);
  172. expect(completed?.startedAt).toBe(manualStartedAt);
  173. expect(resolveCodingPlanQuotaResetStatusVisible(completed, now)).toBe(false);
  174. const repeated = applyCodingPlanQuotaResetStatus(completed, snapshot, now + 30_000);
  175. expect(repeated?.startedAt).toBe(manualStartedAt);
  176. expect(resolveCodingPlanQuotaResetStatusVisible(repeated, now + 30_000)).toBe(false);
  177. });
  178. it("自动重置短提示从首次观察时间开始,重复轮询同一 usedAt 不续期", () => {
  179. const usedAt = Date.UTC(2026, 7, 7, 10, 0, 0);
  180. const firstObservedAt = usedAt + 30_000;
  181. const snapshot: CodingPlanResetStatusSnapshot = {
  182. availableFiveHourResets: [],
  183. availableWeekResets: [],
  184. latestFiveHourResetHistory: { usedAt },
  185. latestWeekResetHistory: null,
  186. hasUnreadHistory: true,
  187. };
  188. const first = applyCodingPlanQuotaResetStatus(null, snapshot, firstObservedAt);
  189. expect(first?.completedAt).toBe(usedAt);
  190. expect(first?.observedAt).toBe(firstObservedAt);
  191. expect(resolveCodingPlanQuotaResetStatusVisible(first, firstObservedAt)).toBe(true);
  192. const repeated = applyCodingPlanQuotaResetStatus(
  193. first,
  194. { ...snapshot, hasUnreadHistory: false },
  195. firstObservedAt + 2_000,
  196. );
  197. expect(repeated?.observedAt).toBe(firstObservedAt);
  198. expect(
  199. resolveCodingPlanQuotaResetStatusVisible(
  200. repeated,
  201. firstObservedAt + CODING_PLAN_QUOTA_RESET_DONE_DISPLAY_MS,
  202. ),
  203. ).toBe(false);
  204. });
  205. it("手动失败恢复 AVAILABLE 并保留原幂等键供重试", () => {
  206. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  207. const available = applyCodingPlanQuotaResetStatus(
  208. null,
  209. {
  210. availableFiveHourResets: [{ expireAt: now + 30_000 }],
  211. availableWeekResets: [],
  212. latestFiveHourResetHistory: null,
  213. latestWeekResetHistory: null,
  214. hasUnreadHistory: false,
  215. },
  216. now,
  217. );
  218. const processing = startCodingPlanQuotaResetManualUse(available, "same-key", now);
  219. const failed = failCodingPlanQuotaResetManualUse(processing, "code=2007");
  220. expect(failed).toMatchObject({
  221. status: "available",
  222. idempotencyKey: "same-key",
  223. error: "code=2007",
  224. });
  225. expect(
  226. startCodingPlanQuotaResetManualUse(failed, "must-not-replace", now + 1_000)?.idempotencyKey,
  227. ).toBe("same-key");
  228. });
  229. it("已完成时对 5 小时额度覆盖为 100% 剩余并改写下次重置时间", () => {
  230. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  231. const completed = applyCodingPlanQuotaResetStatus(
  232. null,
  233. {
  234. availableFiveHourResets: [],
  235. availableWeekResets: [],
  236. latestFiveHourResetHistory: { usedAt: now },
  237. latestWeekResetHistory: null,
  238. hasUnreadHistory: true,
  239. },
  240. now,
  241. );
  242. const overridden = resolveCodingPlanQuotaResetLimit(buildLimit(), completed);
  243. expect(overridden?.percentage).toBe(0);
  244. expect(overridden?.nextResetTime).toBe(now + FIVE_HOURS_MS);
  245. const refreshed = completeCodingPlanQuotaResetEntitlementRefresh(completed, now);
  246. expect(refreshed?.quotaOverridePending).toBe(false);
  247. expect(resolveCodingPlanQuotaResetLimit(buildLimit(), refreshed)).toEqual(buildLimit());
  248. // Bugfix:重置后额度池无活跃窗口时,刷新回来的真实额度缺失 nextResetTime;
  249. // 完成态期间继续用 completedAt + 周期 兜底,重置时间不能闪现即消失。
  250. expect(
  251. resolveCodingPlanQuotaResetLimit(buildLimit({ nextResetTime: undefined }), refreshed),
  252. ).toMatchObject({ percentage: 42, nextResetTime: now + FIVE_HOURS_MS });
  253. const repeated = applyCodingPlanQuotaResetStatus(
  254. refreshed,
  255. {
  256. availableFiveHourResets: [],
  257. availableWeekResets: [],
  258. latestFiveHourResetHistory: { usedAt: now },
  259. latestWeekResetHistory: null,
  260. hasUnreadHistory: false,
  261. },
  262. now + 30_000,
  263. );
  264. expect(repeated?.quotaOverridePending).toBe(false);
  265. // 未完成(available / processing)时保留服务端真实额度。
  266. expect(
  267. resolveCodingPlanQuotaResetLimit(
  268. buildLimit(),
  269. applyCodingPlanQuotaResetStatus(
  270. null,
  271. {
  272. availableFiveHourResets: [{ expireAt: now + 60_000 }],
  273. availableWeekResets: [],
  274. latestFiveHourResetHistory: null,
  275. latestWeekResetHistory: null,
  276. hasUnreadHistory: false,
  277. },
  278. now,
  279. ),
  280. ),
  281. ).toEqual(buildLimit());
  282. expect(
  283. resolveCodingPlanQuotaResetLimit(
  284. buildLimit(),
  285. startCodingPlanQuotaResetManualUse(
  286. applyCodingPlanQuotaResetStatus(
  287. null,
  288. {
  289. availableFiveHourResets: [{ expireAt: now + 60_000 }],
  290. availableWeekResets: [],
  291. latestFiveHourResetHistory: null,
  292. latestWeekResetHistory: null,
  293. hasUnreadHistory: false,
  294. },
  295. now,
  296. ),
  297. "key",
  298. now,
  299. ),
  300. ),
  301. ).toEqual(buildLimit());
  302. });
  303. it("未完成时保留服务端返回的真实剩余百分比", () => {
  304. expect(
  305. resolveCodingPlanQuotaResetRemainingPercentage(
  306. 37,
  307. applyCodingPlanQuotaResetStatus(
  308. null,
  309. {
  310. availableFiveHourResets: [{ expireAt: Date.UTC(2026, 7, 7) + 60_000 }],
  311. availableWeekResets: [],
  312. latestFiveHourResetHistory: null,
  313. latestWeekResetHistory: null,
  314. hasUnreadHistory: false,
  315. },
  316. Date.UTC(2026, 7, 7),
  317. ),
  318. ),
  319. ).toBe(37);
  320. });
  321. it("手动重置只播放按钮烟花,不显示 processing 或 completed Tooltip", () => {
  322. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  323. const available = applyCodingPlanQuotaResetStatus(
  324. null,
  325. {
  326. availableFiveHourResets: [{ expireAt: now + 60_000 }],
  327. availableWeekResets: [],
  328. latestFiveHourResetHistory: null,
  329. latestWeekResetHistory: null,
  330. hasUnreadHistory: false,
  331. },
  332. now,
  333. );
  334. expect(resolveCodingPlanQuotaResetStatusVisible(available, now)).toBe(false);
  335. const processing = startCodingPlanQuotaResetManualUse(available, "key", now);
  336. expect(resolveCodingPlanQuotaResetStatusVisible(processing, now + 999_999)).toBe(false);
  337. const completed = applyCodingPlanQuotaResetStatus(
  338. processing,
  339. {
  340. availableFiveHourResets: [],
  341. availableWeekResets: [],
  342. latestFiveHourResetHistory: { usedAt: now },
  343. latestWeekResetHistory: null,
  344. hasUnreadHistory: false,
  345. },
  346. now,
  347. );
  348. expect(resolveCodingPlanQuotaResetStatusVisible(completed, now)).toBe(false);
  349. });
  350. it("自动或运营重置的 completed Tooltip 在展示窗口结束后收起", () => {
  351. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  352. const completed = applyCodingPlanQuotaResetStatus(
  353. null,
  354. {
  355. availableFiveHourResets: [],
  356. availableWeekResets: [],
  357. latestFiveHourResetHistory: { usedAt: now - 30_000 },
  358. latestWeekResetHistory: null,
  359. hasUnreadHistory: true,
  360. },
  361. now,
  362. );
  363. expect(resolveCodingPlanQuotaResetStatusVisible(completed, now)).toBe(true);
  364. expect(
  365. resolveCodingPlanQuotaResetStatusVisible(
  366. completed,
  367. now + CODING_PLAN_QUOTA_RESET_DONE_DISPLAY_MS - 1,
  368. ),
  369. ).toBe(true);
  370. expect(
  371. resolveCodingPlanQuotaResetStatusVisible(
  372. completed,
  373. now + CODING_PLAN_QUOTA_RESET_DONE_DISPLAY_MS,
  374. ),
  375. ).toBe(false);
  376. expect(resolveCodingPlanQuotaResetStatusVisible(null, now)).toBe(false);
  377. });
  378. it("Composer 自动重置合成阶段:先正在重置再常驻已重置,hover 收起且手动/未完成不进入", () => {
  379. const usedAt = Date.UTC(2026, 7, 7, 10, 0, 0);
  380. const observedAt = usedAt + 30_000;
  381. const snapshot: CodingPlanResetStatusSnapshot = {
  382. availableFiveHourResets: [],
  383. availableWeekResets: [],
  384. latestFiveHourResetHistory: { usedAt },
  385. latestWeekResetHistory: null,
  386. hasUnreadHistory: true,
  387. };
  388. const automatic = applyCodingPlanQuotaResetStatus(null, snapshot, observedAt);
  389. expect(automatic?.startedAt).toBeNull();
  390. expect(automatic?.observedAt).toBe(observedAt);
  391. // 首次观察起约 1 秒展示“正在重置”。
  392. expect(resolveCodingPlanQuotaResetAutomaticPhase(automatic, observedAt, false)).toBe(
  393. "processing",
  394. );
  395. expect(
  396. resolveCodingPlanQuotaResetAutomaticPhase(
  397. automatic,
  398. observedAt + CODING_PLAN_QUOTA_RESET_AUTOMATIC_PROCESSING_MS - 1,
  399. false,
  400. ),
  401. ).toBe("processing");
  402. // 到期后切换为“已重置”,且无上限、一直保留。
  403. expect(
  404. resolveCodingPlanQuotaResetAutomaticPhase(
  405. automatic,
  406. observedAt + CODING_PLAN_QUOTA_RESET_AUTOMATIC_PROCESSING_MS,
  407. false,
  408. ),
  409. ).toBe("completed");
  410. expect(
  411. resolveCodingPlanQuotaResetAutomaticPhase(automatic, observedAt + 3_600_000, false),
  412. ).toBe("completed");
  413. // hover 收起后返回 null。
  414. expect(resolveCodingPlanQuotaResetAutomaticPhase(automatic, observedAt, true)).toBeNull();
  415. // 手动完成(startedAt 不为空)不进入触发器合成阶段。
  416. const manual = applyCodingPlanQuotaResetStatus(null, snapshot, observedAt, usedAt - 500);
  417. expect(manual?.startedAt).toBe(usedAt - 500);
  418. expect(resolveCodingPlanQuotaResetAutomaticPhase(manual, observedAt, false)).toBeNull();
  419. // available / null 不进入。
  420. const available = applyCodingPlanQuotaResetStatus(
  421. null,
  422. {
  423. availableFiveHourResets: [{ expireAt: observedAt + 60_000 }],
  424. availableWeekResets: [],
  425. latestFiveHourResetHistory: null,
  426. latestWeekResetHistory: null,
  427. hasUnreadHistory: false,
  428. },
  429. observedAt,
  430. );
  431. expect(resolveCodingPlanQuotaResetAutomaticPhase(available, observedAt, false)).toBeNull();
  432. expect(resolveCodingPlanQuotaResetAutomaticPhase(null, observedAt, false)).toBeNull();
  433. });
  434. it("按 resetType 读取周额度机会创建 AVAILABLE", () => {
  435. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  436. const status: CodingPlanResetStatusSnapshot = {
  437. // 五小时无机会,周额度有机会:resetType=WEEK 只应读取周字段。
  438. availableFiveHourResets: [],
  439. availableWeekResets: [{ expireAt: now + 90_000 }, { expireAt: now + 45_000 }],
  440. latestFiveHourResetHistory: null,
  441. latestWeekResetHistory: null,
  442. hasUnreadHistory: false,
  443. };
  444. expect(applyCodingPlanQuotaResetStatus(null, status, now, null, "WEEK")).toMatchObject({
  445. status: "available",
  446. opportunityCount: 2,
  447. opportunityExpiresAt: now + 45_000,
  448. });
  449. // 同一 status 下 FIVE_HOUR 无机会,不应被周机会带出 available。
  450. expect(applyCodingPlanQuotaResetStatus(null, status, now)).toBeNull();
  451. });
  452. it("周额度未读历史完成时按 7 天乐观改写下次重置时间", () => {
  453. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  454. const usedAt = now - 1_000;
  455. const completed = applyCodingPlanQuotaResetStatus(
  456. null,
  457. {
  458. availableFiveHourResets: [],
  459. availableWeekResets: [],
  460. latestFiveHourResetHistory: null,
  461. latestWeekResetHistory: { usedAt },
  462. hasUnreadHistory: true,
  463. },
  464. now,
  465. null,
  466. "WEEK",
  467. );
  468. expect(completed?.status).toBe("completed");
  469. expect(completed?.completedAt).toBe(usedAt);
  470. expect(completed?.nextResetAt).toBe(usedAt + WEEK_MS);
  471. const overridden = resolveCodingPlanQuotaResetLimit(buildLimit(), completed);
  472. expect(overridden?.percentage).toBe(0);
  473. expect(overridden?.nextResetTime).toBe(usedAt + WEEK_MS);
  474. });
  475. it("同一 used_at 周期内新发放的机会打破粘滞完成态,回到 AVAILABLE", () => {
  476. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  477. const usedAt = now - 1_000;
  478. const completedSnapshot: CodingPlanResetStatusSnapshot = {
  479. availableFiveHourResets: [],
  480. availableWeekResets: [],
  481. latestFiveHourResetHistory: { usedAt },
  482. latestWeekResetHistory: null,
  483. hasUnreadHistory: true,
  484. };
  485. const completed = applyCodingPlanQuotaResetStatus(null, completedSnapshot, now);
  486. expect(completed?.status).toBe("completed");
  487. // 后端在同一周期内(used_at 未变)再次发放机会:完成态必须让位,否则新机会被
  488. // UI 永久吞掉,用户要重新登录清空窗口内存态才能看到重置入口。
  489. const regrantSnapshot: CodingPlanResetStatusSnapshot = {
  490. ...completedSnapshot,
  491. availableFiveHourResets: [{ expireAt: now + 120_000 }],
  492. hasUnreadHistory: false,
  493. };
  494. const revived = applyCodingPlanQuotaResetStatus(completed, regrantSnapshot, now + 60_000);
  495. expect(revived?.status).toBe("available");
  496. expect(revived?.opportunityCount).toBe(1);
  497. // 共享手动轨迹归因同理:有新机会时不再把 used_at 归为旧的手动完成。
  498. expect(
  499. applyCodingPlanQuotaResetStatus(null, regrantSnapshot, now + 60_000, usedAt - 500)?.status,
  500. ).toBe("available");
  501. // 没有新机会时粘滞保持:重复轮询不得把完成态清空。
  502. const sticky = applyCodingPlanQuotaResetStatus(
  503. completed,
  504. { ...completedSnapshot, hasUnreadHistory: false },
  505. now + 60_000,
  506. );
  507. expect(sticky?.status).toBe("completed");
  508. // 刚发现的未读完成优先于新机会:完成提示与额度校正先播,下一轮再让位。
  509. const unreadWithOpportunity = applyCodingPlanQuotaResetStatus(
  510. null,
  511. {
  512. ...regrantSnapshot,
  513. hasUnreadHistory: true,
  514. },
  515. now,
  516. );
  517. expect(unreadWithOpportunity?.status).toBe("completed");
  518. // processing 对账不受影响:机会余额 >0 时手动 /use 确认循环仍能看到 completed。
  519. const available = applyCodingPlanQuotaResetStatus(
  520. null,
  521. {
  522. ...completedSnapshot,
  523. latestFiveHourResetHistory: null,
  524. hasUnreadHistory: false,
  525. availableFiveHourResets: [{ expireAt: now + 60_000 }],
  526. },
  527. now,
  528. );
  529. const processing = startCodingPlanQuotaResetManualUse(available, "key", now);
  530. const confirmed = applyCodingPlanQuotaResetStatus(
  531. processing,
  532. regrantSnapshot,
  533. now + 1_000,
  534. now,
  535. );
  536. expect(confirmed?.status).toBe("completed");
  537. });
  538. it("同类型多张机会:完成态携带余下机会,可直接再次核销,耗尽后不可核销", () => {
  539. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  540. const firstUsedAt = now + 1_000;
  541. const available = applyCodingPlanQuotaResetStatus(
  542. null,
  543. {
  544. availableFiveHourResets: [{ expireAt: now + 60_000 }, { expireAt: now + 120_000 }],
  545. availableWeekResets: [],
  546. latestFiveHourResetHistory: null,
  547. latestWeekResetHistory: null,
  548. hasUnreadHistory: false,
  549. },
  550. now,
  551. );
  552. expect(hasUsableCodingPlanQuotaResetOpportunity(available, now)).toBe(true);
  553. const processing = startCodingPlanQuotaResetManualUse(available, "first-key", now);
  554. // processing 期间不可再次核销,避免同一张机会重复发 /use。
  555. expect(hasUsableCodingPlanQuotaResetOpportunity(processing, now)).toBe(false);
  556. // 核销最早到期的一张:同一 status 快照同时给出 used_at 与余下 1 张机会。
  557. const afterFirstUse: CodingPlanResetStatusSnapshot = {
  558. availableFiveHourResets: [{ expireAt: now + 120_000 }],
  559. availableWeekResets: [],
  560. latestFiveHourResetHistory: { usedAt: firstUsedAt },
  561. latestWeekResetHistory: null,
  562. hasUnreadHistory: true,
  563. };
  564. const completed = applyCodingPlanQuotaResetStatus(processing, afterFirstUse, now + 2_000, now);
  565. expect(completed).toMatchObject({
  566. status: "completed",
  567. completedAt: firstUsedAt,
  568. // 完成态不再把余下机会清零,否则弹框会把空到期渲染成「0 分 0 秒」。
  569. opportunityCount: 1,
  570. opportunityExpiresAt: now + 120_000,
  571. idempotencyKey: null,
  572. });
  573. expect(hasUsableCodingPlanQuotaResetOpportunity(completed, now + 2_000)).toBe(true);
  574. // 完成态下直接再次核销:进入 processing 并生成新的幂等键,保留机会快照供失败恢复。
  575. const secondProcessing = startCodingPlanQuotaResetManualUse(
  576. completed,
  577. "second-key",
  578. now + 3_000,
  579. );
  580. expect(secondProcessing).toMatchObject({
  581. status: "processing",
  582. startedAt: now + 3_000,
  583. completedAt: null,
  584. opportunityCount: 1,
  585. opportunityExpiresAt: now + 120_000,
  586. idempotencyKey: "second-key",
  587. });
  588. expect(failCodingPlanQuotaResetManualUse(secondProcessing, "code=2007")).toMatchObject({
  589. status: "available",
  590. opportunityCount: 1,
  591. opportunityExpiresAt: now + 120_000,
  592. idempotencyKey: "second-key",
  593. });
  594. // 最后一张核销后机会耗尽:完成态张数为 0、到期为空,不可再核销。
  595. const exhausted = applyCodingPlanQuotaResetStatus(
  596. secondProcessing,
  597. {
  598. ...afterFirstUse,
  599. availableFiveHourResets: [],
  600. latestFiveHourResetHistory: { usedAt: now + 4_000 },
  601. },
  602. now + 5_000,
  603. now + 3_000,
  604. );
  605. expect(exhausted).toMatchObject({
  606. status: "completed",
  607. completedAt: now + 4_000,
  608. opportunityCount: 0,
  609. opportunityExpiresAt: null,
  610. });
  611. expect(hasUsableCodingPlanQuotaResetOpportunity(exhausted, now + 5_000)).toBe(false);
  612. expect(startCodingPlanQuotaResetManualUse(exhausted, "noop-key", now + 5_000)).toBe(exhausted);
  613. // 余下机会已过期:同样不可核销,start 保持原状态。
  614. expect(hasUsableCodingPlanQuotaResetOpportunity(completed, now + 120_000)).toBe(false);
  615. expect(startCodingPlanQuotaResetManualUse(completed, "expired-key", now + 120_000)).toBe(
  616. completed,
  617. );
  618. expect(hasUsableCodingPlanQuotaResetOpportunity(null, now)).toBe(false);
  619. // 额度标题旁操作:仍有可核销机会时不展示「已重置」锚点,保持「重置」入口;耗尽后才展示。
  620. expect(
  621. resolveCodingPlanQuotaResetActionCompletedAt({ entry: completed, opportunityVisible: true }),
  622. ).toBeNull();
  623. expect(
  624. resolveCodingPlanQuotaResetActionCompletedAt({ entry: exhausted, opportunityVisible: false }),
  625. ).toBe(now + 4_000);
  626. expect(
  627. resolveCodingPlanQuotaResetActionCompletedAt({ entry: null, opportunityVisible: false }),
  628. ).toBeNull();
  629. });
  630. it("processing 只认不同于点击前基线的新 used_at:滞后快照(含 unread 未清)保持处理中", () => {
  631. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  632. const firstUsedAt = now - 2_000;
  633. // 第一张刚核销:完成态携带余下 1 张,上一张的 history/read 可能尚未生效。
  634. const afterFirstUse: CodingPlanResetStatusSnapshot = {
  635. availableFiveHourResets: [{ expireAt: now + 60_000 }],
  636. availableWeekResets: [],
  637. latestFiveHourResetHistory: { usedAt: firstUsedAt },
  638. latestWeekResetHistory: null,
  639. hasUnreadHistory: true,
  640. };
  641. const completed = applyCodingPlanQuotaResetStatus(
  642. null,
  643. afterFirstUse,
  644. now - 1_000,
  645. now - 3_000,
  646. );
  647. expect(completed).toMatchObject({ status: "completed", opportunityCount: 1 });
  648. const processing = startCodingPlanQuotaResetManualUse(completed, "second-key", now);
  649. // /use 后 status 仍是消费前快照:共享轨迹比较基线后不给 manualStartedAt。
  650. // 无论 unread 是否已清,都不能把上一张的 used_at 当成本次完成。
  651. for (const hasUnreadHistory of [true, false]) {
  652. expect(
  653. applyCodingPlanQuotaResetStatus(
  654. processing,
  655. { ...afterFirstUse, hasUnreadHistory },
  656. now + 100,
  657. ),
  658. ).toBe(processing);
  659. }
  660. // 读到新的 used_at 后轨迹绑定、给出 manualStartedAt,才进入完成并按新快照展示余下张数。
  661. const confirmed = applyCodingPlanQuotaResetStatus(
  662. processing,
  663. {
  664. ...afterFirstUse,
  665. availableFiveHourResets: [],
  666. latestFiveHourResetHistory: { usedAt: now + 500 },
  667. },
  668. now + 800,
  669. now,
  670. );
  671. expect(confirmed).toMatchObject({
  672. status: "completed",
  673. completedAt: now + 500,
  674. startedAt: now,
  675. opportunityCount: 0,
  676. });
  677. });
  678. it("五小时与周机会徽标合并:次数累加,倒计时取最早到期,单侧可见时只计该侧", () => {
  679. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  680. // 双侧可见:次数累加,倒计时取更早的周机会到期时间。
  681. expect(
  682. mergeCodingPlanQuotaResetOpportunityBadges([
  683. { count: 2, expiresAt: now + 60_000, visible: true },
  684. { count: 1, expiresAt: now + 30_000, visible: true },
  685. ]),
  686. ).toEqual({ count: 3, expiresAt: now + 30_000, visible: true });
  687. // 一侧隐藏(额度满/无机会):只显示可见一侧,不把隐藏侧的次数累进去。
  688. expect(
  689. mergeCodingPlanQuotaResetOpportunityBadges([
  690. { count: 2, expiresAt: now + 60_000, visible: true },
  691. { count: 1, expiresAt: now + 30_000, visible: false },
  692. ]),
  693. ).toEqual({ count: 2, expiresAt: now + 60_000, visible: true });
  694. // 全部隐藏或次数为 0:徽标不可见。
  695. expect(
  696. mergeCodingPlanQuotaResetOpportunityBadges([
  697. { count: 0, expiresAt: now + 60_000, visible: true },
  698. { count: 1, expiresAt: null, visible: false },
  699. ]),
  700. ).toEqual({ count: 0, expiresAt: null, visible: false });
  701. });
  702. it("共享 has_unread_history 只归属 used_at 最新的一类,避免错误完成另一类", () => {
  703. const now = Date.UTC(2026, 7, 7, 10, 0, 0);
  704. const staleFiveHourUsedAt = now - FIVE_HOURS_MS;
  705. const freshWeekUsedAt = now - 1_000;
  706. // 单一 unread 游标 + 两类历史:只有 used_at 最新的周额度应进入完成态。
  707. const snapshot: CodingPlanResetStatusSnapshot = {
  708. availableFiveHourResets: [],
  709. availableWeekResets: [],
  710. latestFiveHourResetHistory: { usedAt: staleFiveHourUsedAt },
  711. latestWeekResetHistory: { usedAt: freshWeekUsedAt },
  712. hasUnreadHistory: true,
  713. };
  714. // 过期的五小时历史不拥有 unread 标记,首次挂载不得伪造完成。
  715. expect(applyCodingPlanQuotaResetStatus(null, snapshot, now, null, "FIVE_HOUR")).toBeNull();
  716. // 更新的周历史拥有 unread 标记,正常完成。
  717. const week = applyCodingPlanQuotaResetStatus(null, snapshot, now, null, "WEEK");
  718. expect(week?.status).toBe("completed");
  719. expect(week?.completedAt).toBe(freshWeekUsedAt);
  720. // used_at 相等时按当前类型归属:两类都能各自完成,不会互相抢占。
  721. const tie: CodingPlanResetStatusSnapshot = {
  722. ...snapshot,
  723. latestFiveHourResetHistory: { usedAt: freshWeekUsedAt },
  724. };
  725. expect(applyCodingPlanQuotaResetStatus(null, tie, now, null, "FIVE_HOUR")?.status).toBe(
  726. "completed",
  727. );
  728. expect(applyCodingPlanQuotaResetStatus(null, tie, now, null, "WEEK")?.status).toBe("completed");
  729. });
  730. });