contextQuotaResetOpportunityReminder.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // @vitest-environment jsdom
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { createElement } from "react";
  4. import { readFileSync } from "node:fs";
  5. import { describe, expect, it } from "vitest";
  6. import {
  7. ContextQuotaResetOpportunityReminderContent,
  8. createContextQuotaResetOpportunityDismissalStore,
  9. resolveContextQuotaResetOpportunityReminder,
  10. resolveContextQuotaResetOpportunityTriggerTone,
  11. resolveContextTriggerTooltipKind,
  12. shouldDismissContextQuotaResetOpportunityReminder,
  13. type ContextQuotaResetOpportunityDismissal,
  14. } from "@/chat-input-toolbar/contextQuotaResetOpportunityReminder.js";
  15. const opportunity = {
  16. count: 2,
  17. expiresAt: 1_000_000,
  18. sourceKey: "provider-a",
  19. visible: true,
  20. } as const;
  21. const unread: ContextQuotaResetOpportunityDismissal = {
  22. initial: false,
  23. opportunityKey: null,
  24. urgent: false,
  25. };
  26. describe("context quota reset opportunity reminder state", () => {
  27. it("only dismisses the reminder when pointer down happens outside its trigger and content", () => {
  28. const trigger = document.createElement("span");
  29. const triggerChild = document.createElement("button");
  30. trigger.append(triggerChild);
  31. const reminder = document.createElement("span");
  32. reminder.dataset.contextResetReminder = "initial";
  33. const reminderChild = document.createElement("button");
  34. reminder.append(reminderChild);
  35. const outside = document.createElement("div");
  36. const outsideText = document.createTextNode("outside");
  37. outside.append(outsideText);
  38. expect(shouldDismissContextQuotaResetOpportunityReminder(triggerChild, trigger)).toBe(false);
  39. expect(shouldDismissContextQuotaResetOpportunityReminder(reminderChild, trigger)).toBe(false);
  40. expect(shouldDismissContextQuotaResetOpportunityReminder(outside, trigger)).toBe(true);
  41. expect(shouldDismissContextQuotaResetOpportunityReminder(outsideText, trigger)).toBe(true);
  42. });
  43. it("keeps a dismissed phase hidden when the conversation component remounts", () => {
  44. const store = createContextQuotaResetOpportunityDismissalStore();
  45. const reminder = resolveContextQuotaResetOpportunityReminder({
  46. dismissal: store.getSnapshot(),
  47. now: opportunity.expiresAt - 181_000,
  48. opportunity,
  49. });
  50. expect(reminder?.phase).toBe("initial");
  51. store.dismiss(reminder!);
  52. // 模拟切换会话后的组件重新挂载:窗口级 store 仍是同一个实例。
  53. expect(
  54. resolveContextQuotaResetOpportunityReminder({
  55. dismissal: store.getSnapshot(),
  56. now: opportunity.expiresAt - 181_000,
  57. opportunity,
  58. }),
  59. ).toBeNull();
  60. expect(
  61. resolveContextQuotaResetOpportunityReminder({
  62. dismissal: store.getSnapshot(),
  63. now: opportunity.expiresAt - 180_000,
  64. opportunity,
  65. })?.phase,
  66. ).toBe("urgent");
  67. });
  68. it("uses success before the urgent window and warning during the final three minutes", () => {
  69. expect(
  70. resolveContextQuotaResetOpportunityTriggerTone({
  71. now: opportunity.expiresAt - 181_000,
  72. opportunity,
  73. }),
  74. ).toBe("available");
  75. expect(
  76. resolveContextQuotaResetOpportunityTriggerTone({
  77. now: opportunity.expiresAt - 180_000,
  78. opportunity,
  79. }),
  80. ).toBe("urgent");
  81. });
  82. it("removes the trigger tone when the opportunity is unavailable or expired", () => {
  83. expect(
  84. resolveContextQuotaResetOpportunityTriggerTone({
  85. now: opportunity.expiresAt,
  86. opportunity,
  87. }),
  88. ).toBeNull();
  89. expect(
  90. resolveContextQuotaResetOpportunityTriggerTone({
  91. now: opportunity.expiresAt - 181_000,
  92. opportunity: { ...opportunity, visible: false },
  93. }),
  94. ).toBeNull();
  95. });
  96. it("shows a persistent initial reminder for a new opportunity", () => {
  97. expect(
  98. resolveContextQuotaResetOpportunityReminder({
  99. dismissal: unread,
  100. now: opportunity.expiresAt - 181_000,
  101. opportunity,
  102. }),
  103. ).toMatchObject({
  104. count: 2,
  105. phase: "initial",
  106. remainingSeconds: 181,
  107. });
  108. });
  109. it("switches to urgent exactly at three minutes even after initial dismissal", () => {
  110. const opportunityKey = `${opportunity.sourceKey}:${opportunity.expiresAt}`;
  111. expect(
  112. resolveContextQuotaResetOpportunityReminder({
  113. dismissal: { initial: true, opportunityKey, urgent: false },
  114. now: opportunity.expiresAt - 180_000,
  115. opportunity,
  116. }),
  117. ).toMatchObject({ phase: "urgent", remainingSeconds: 180 });
  118. });
  119. it("replaces an unread initial reminder with urgent content at the threshold", () => {
  120. expect(
  121. resolveContextQuotaResetOpportunityReminder({
  122. dismissal: unread,
  123. now: opportunity.expiresAt - 179_001,
  124. opportunity,
  125. })?.phase,
  126. ).toBe("urgent");
  127. });
  128. it("does not repeat an urgent reminder after it is dismissed", () => {
  129. const opportunityKey = `${opportunity.sourceKey}:${opportunity.expiresAt}`;
  130. expect(
  131. resolveContextQuotaResetOpportunityReminder({
  132. dismissal: { initial: true, opportunityKey, urgent: true },
  133. now: opportunity.expiresAt - 60_000,
  134. opportunity,
  135. }),
  136. ).toBeNull();
  137. });
  138. it("keeps dismissal when count changes but resets it for a new expiry", () => {
  139. const opportunityKey = `${opportunity.sourceKey}:${opportunity.expiresAt}`;
  140. const dismissal = { initial: true, opportunityKey, urgent: false } as const;
  141. expect(
  142. resolveContextQuotaResetOpportunityReminder({
  143. dismissal,
  144. now: opportunity.expiresAt - 181_000,
  145. opportunity: { ...opportunity, count: 3 },
  146. }),
  147. ).toBeNull();
  148. expect(
  149. resolveContextQuotaResetOpportunityReminder({
  150. dismissal,
  151. now: opportunity.expiresAt + 1_000,
  152. opportunity: { ...opportunity, expiresAt: opportunity.expiresAt + 300_000 },
  153. })?.phase,
  154. ).toBe("initial");
  155. });
  156. it("clears reminders for hidden, empty, or expired opportunities", () => {
  157. for (const candidate of [
  158. { ...opportunity, visible: false },
  159. { ...opportunity, count: 0 },
  160. { ...opportunity, expiresAt: 500_000 },
  161. ]) {
  162. expect(
  163. resolveContextQuotaResetOpportunityReminder({
  164. dismissal: unread,
  165. now: 500_000,
  166. opportunity: candidate,
  167. }),
  168. ).toBeNull();
  169. }
  170. });
  171. it("gives automatic reset status priority over opportunity phases", () => {
  172. expect(resolveContextTriggerTooltipKind("processing", "urgent")).toBe("reset-status");
  173. expect(resolveContextTriggerTooltipKind(null, "urgent")).toBe("urgent");
  174. expect(resolveContextTriggerTooltipKind(null, "initial")).toBe("initial");
  175. });
  176. });
  177. describe("ContextQuotaResetOpportunityReminderContent", () => {
  178. const intl = {
  179. formatMessage: ({ id }: { id: string }, values?: Record<string, unknown>) =>
  180. `${id}:${Object.values(values ?? {}).join(":")}`,
  181. } as never;
  182. it("renders the localized initial gift reminder", () => {
  183. const html = renderToStaticMarkup(
  184. createElement(ContextQuotaResetOpportunityReminderContent, {
  185. count: 2,
  186. intl,
  187. onDismiss: () => undefined,
  188. phase: "initial",
  189. remainingSeconds: 181,
  190. }),
  191. );
  192. expect(html).toContain("codingPlan.quotaReset.contextReminder.available:2");
  193. expect(html).toContain("lucide-gift");
  194. expect(html).toContain("text-success");
  195. expect(html).toContain("text-ui-sm");
  196. expect(html).toContain("data-context-reset-reminder-dismiss");
  197. expect(html).toContain('data-slot="button"');
  198. expect(html).toContain('data-size="icon-xs"');
  199. expect(html).toContain("rounded-full");
  200. expect(html).not.toContain("-my-1");
  201. expect(html).not.toContain("-mr-1");
  202. expect(html).toContain("lucide-x");
  203. });
  204. it("renders an animated urgent alarm with reduced-motion protection", () => {
  205. const html = renderToStaticMarkup(
  206. createElement(ContextQuotaResetOpportunityReminderContent, {
  207. count: 1,
  208. intl,
  209. onDismiss: () => undefined,
  210. phase: "urgent",
  211. remainingSeconds: 179,
  212. }),
  213. );
  214. expect(html).toContain("codingPlan.quotaReset.contextReminder.expiresIn");
  215. expect(html).toContain("lucide-alarm-clock");
  216. expect(html).toContain("animate-zcode-alarm-ring");
  217. expect(html).toContain("motion-reduce:animate-none");
  218. expect(html).toContain("text-warning");
  219. expect(html).toContain("bg-warning/10");
  220. expect(html).toContain("rounded-full");
  221. expect(html).toContain("text-ui-xs");
  222. expect(html).toContain("px-1.5");
  223. expect(html).toContain("h-3.5");
  224. expect(html).toContain("leading-none");
  225. expect(html).not.toContain("font-mono");
  226. expect(html).toContain("tabular-nums");
  227. expect(html).toContain("codingPlan.quotaReset.contextReminder.dismiss");
  228. expect(html).toContain("data-context-reset-reminder-copy");
  229. expect(html).toContain("gap-0");
  230. });
  231. });
  232. describe("context quota reset opportunity reminder integration", () => {
  233. it("uses the shared ControlHintTooltip wrapper instead of composing tooltip primitives", () => {
  234. const source = readFileSync("packages/ui/src/chat-input-toolbar/contextUsage.tsx", "utf8");
  235. expect(source).toContain('import { ControlHintTooltip } from "@/ControlHintTooltip.js"');
  236. expect(source).toContain("standalone");
  237. expect(source).not.toContain("<TooltipProvider>");
  238. expect(source).not.toContain("<TooltipContent");
  239. expect(source).not.toContain("<TooltipTrigger");
  240. expect(source).not.toContain("sideOffset={6}");
  241. expect(source).toMatch(
  242. /triggerTooltipKind === "reset-status"\s*\? undefined\s*:\s*"bg-background py-0\.5 pr-0\.5"/,
  243. );
  244. expect(source).toContain('opportunityTriggerTone === "available" && "text-success"');
  245. expect(source).not.toContain('opportunityTriggerTone === "available" && "bg-success/10');
  246. expect(source).toMatch(
  247. /opportunityTriggerTone === "urgent"\s*&&\s*"bg-warning\/10 text-warning"/,
  248. );
  249. expect(source).toContain("useSyncExternalStore(");
  250. expect(source).toContain("contextQuotaResetOpportunityDismissalStore.dismiss(");
  251. expect(source).toContain('document.addEventListener("pointerdown"');
  252. expect(source).toContain("shouldDismissContextQuotaResetOpportunityReminder(");
  253. expect(source).toContain("triggerRef={contextUsageTriggerRef}");
  254. expect(source).not.toContain("useState<ContextQuotaResetOpportunityDismissal>");
  255. });
  256. });