contextQuotaResetOpportunityReminder.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // @vitest-environment jsdom
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { createElement } from "react";
  4. import { describe, expect, it } from "vitest";
  5. import {
  6. ContextQuotaResetOpportunityReminderContent,
  7. createContextQuotaResetOpportunityDismissalStore,
  8. resolveContextQuotaResetOpportunityReminder,
  9. resolveContextQuotaResetOpportunityTriggerTone,
  10. resolveContextTriggerTooltipKind,
  11. shouldDismissContextQuotaResetOpportunityReminder,
  12. type ContextQuotaResetOpportunityDismissal,
  13. } from "@/chat-input-toolbar/contextQuotaResetOpportunityReminder.js";
  14. const opportunity = {
  15. count: 2,
  16. expiresAt: 1_000_000,
  17. sourceKey: "provider-a",
  18. visible: true,
  19. } as const;
  20. const unread: ContextQuotaResetOpportunityDismissal = {
  21. initial: false,
  22. opportunityKey: null,
  23. urgent: false,
  24. };
  25. describe("context quota reset opportunity reminder state", () => {
  26. it("only dismisses the reminder when pointer down happens outside its trigger and content", () => {
  27. const trigger = document.createElement("span");
  28. const triggerChild = document.createElement("button");
  29. trigger.append(triggerChild);
  30. const reminder = document.createElement("span");
  31. reminder.dataset.contextResetReminder = "initial";
  32. const reminderChild = document.createElement("button");
  33. reminder.append(reminderChild);
  34. const outside = document.createElement("div");
  35. const outsideText = document.createTextNode("outside");
  36. outside.append(outsideText);
  37. expect(shouldDismissContextQuotaResetOpportunityReminder(triggerChild, trigger)).toBe(false);
  38. expect(shouldDismissContextQuotaResetOpportunityReminder(reminderChild, trigger)).toBe(false);
  39. expect(shouldDismissContextQuotaResetOpportunityReminder(outside, trigger)).toBe(true);
  40. expect(shouldDismissContextQuotaResetOpportunityReminder(outsideText, trigger)).toBe(true);
  41. });
  42. it("keeps a dismissed phase hidden when the conversation component remounts", () => {
  43. const store = createContextQuotaResetOpportunityDismissalStore();
  44. const reminder = resolveContextQuotaResetOpportunityReminder({
  45. dismissal: store.getSnapshot(),
  46. now: opportunity.expiresAt - 181_000,
  47. opportunity,
  48. });
  49. expect(reminder?.phase).toBe("initial");
  50. store.dismiss(reminder!);
  51. // 模拟切换会话后的组件重新挂载:窗口级 store 仍是同一个实例。
  52. expect(
  53. resolveContextQuotaResetOpportunityReminder({
  54. dismissal: store.getSnapshot(),
  55. now: opportunity.expiresAt - 181_000,
  56. opportunity,
  57. }),
  58. ).toBeNull();
  59. expect(
  60. resolveContextQuotaResetOpportunityReminder({
  61. dismissal: store.getSnapshot(),
  62. now: opportunity.expiresAt - 180_000,
  63. opportunity,
  64. })?.phase,
  65. ).toBe("urgent");
  66. });
  67. it("uses success before the urgent window and warning during the final three minutes", () => {
  68. expect(
  69. resolveContextQuotaResetOpportunityTriggerTone({
  70. now: opportunity.expiresAt - 181_000,
  71. opportunity,
  72. }),
  73. ).toBe("available");
  74. expect(
  75. resolveContextQuotaResetOpportunityTriggerTone({
  76. now: opportunity.expiresAt - 180_000,
  77. opportunity,
  78. }),
  79. ).toBe("urgent");
  80. });
  81. it("removes the trigger tone when the opportunity is unavailable or expired", () => {
  82. expect(
  83. resolveContextQuotaResetOpportunityTriggerTone({
  84. now: opportunity.expiresAt,
  85. opportunity,
  86. }),
  87. ).toBeNull();
  88. expect(
  89. resolveContextQuotaResetOpportunityTriggerTone({
  90. now: opportunity.expiresAt - 181_000,
  91. opportunity: { ...opportunity, visible: false },
  92. }),
  93. ).toBeNull();
  94. });
  95. it("shows a persistent initial reminder for a new opportunity", () => {
  96. expect(
  97. resolveContextQuotaResetOpportunityReminder({
  98. dismissal: unread,
  99. now: opportunity.expiresAt - 181_000,
  100. opportunity,
  101. }),
  102. ).toMatchObject({
  103. count: 2,
  104. phase: "initial",
  105. remainingSeconds: 181,
  106. });
  107. });
  108. it("switches to urgent exactly at three minutes even after initial dismissal", () => {
  109. const opportunityKey = `${opportunity.sourceKey}:${opportunity.expiresAt}`;
  110. expect(
  111. resolveContextQuotaResetOpportunityReminder({
  112. dismissal: { initial: true, opportunityKey, urgent: false },
  113. now: opportunity.expiresAt - 180_000,
  114. opportunity,
  115. }),
  116. ).toMatchObject({ phase: "urgent", remainingSeconds: 180 });
  117. });
  118. it("replaces an unread initial reminder with urgent content at the threshold", () => {
  119. expect(
  120. resolveContextQuotaResetOpportunityReminder({
  121. dismissal: unread,
  122. now: opportunity.expiresAt - 179_001,
  123. opportunity,
  124. })?.phase,
  125. ).toBe("urgent");
  126. });
  127. it("does not repeat an urgent reminder after it is dismissed", () => {
  128. const opportunityKey = `${opportunity.sourceKey}:${opportunity.expiresAt}`;
  129. expect(
  130. resolveContextQuotaResetOpportunityReminder({
  131. dismissal: { initial: true, opportunityKey, urgent: true },
  132. now: opportunity.expiresAt - 60_000,
  133. opportunity,
  134. }),
  135. ).toBeNull();
  136. });
  137. it("keeps dismissal when count changes but resets it for a new expiry", () => {
  138. const opportunityKey = `${opportunity.sourceKey}:${opportunity.expiresAt}`;
  139. const dismissal = { initial: true, opportunityKey, urgent: false } as const;
  140. expect(
  141. resolveContextQuotaResetOpportunityReminder({
  142. dismissal,
  143. now: opportunity.expiresAt - 181_000,
  144. opportunity: { ...opportunity, count: 3 },
  145. }),
  146. ).toBeNull();
  147. expect(
  148. resolveContextQuotaResetOpportunityReminder({
  149. dismissal,
  150. now: opportunity.expiresAt + 1_000,
  151. opportunity: { ...opportunity, expiresAt: opportunity.expiresAt + 300_000 },
  152. })?.phase,
  153. ).toBe("initial");
  154. });
  155. it("clears reminders for hidden, empty, or expired opportunities", () => {
  156. for (const candidate of [
  157. { ...opportunity, visible: false },
  158. { ...opportunity, count: 0 },
  159. { ...opportunity, expiresAt: 500_000 },
  160. ]) {
  161. expect(
  162. resolveContextQuotaResetOpportunityReminder({
  163. dismissal: unread,
  164. now: 500_000,
  165. opportunity: candidate,
  166. }),
  167. ).toBeNull();
  168. }
  169. });
  170. it("gives automatic reset status priority over opportunity phases", () => {
  171. expect(resolveContextTriggerTooltipKind("processing", "urgent")).toBe("reset-status");
  172. expect(resolveContextTriggerTooltipKind(null, "urgent")).toBe("urgent");
  173. expect(resolveContextTriggerTooltipKind(null, "initial")).toBe("initial");
  174. });
  175. });
  176. describe("ContextQuotaResetOpportunityReminderContent", () => {
  177. const intl = {
  178. formatMessage: ({ id }: { id: string }, values?: Record<string, unknown>) =>
  179. `${id}:${Object.values(values ?? {}).join(":")}`,
  180. } as never;
  181. it("renders the localized initial reminder and dismissal control", () => {
  182. const html = renderToStaticMarkup(
  183. createElement(ContextQuotaResetOpportunityReminderContent, {
  184. count: 2,
  185. intl,
  186. onDismiss: () => undefined,
  187. phase: "initial",
  188. remainingSeconds: 181,
  189. }),
  190. );
  191. expect(html).toContain("codingPlan.quotaReset.contextReminder.available:2");
  192. expect(html).toContain("data-context-reset-reminder-dismiss");
  193. });
  194. it("renders the localized urgent reminder and dismissal action", () => {
  195. const html = renderToStaticMarkup(
  196. createElement(ContextQuotaResetOpportunityReminderContent, {
  197. count: 1,
  198. intl,
  199. onDismiss: () => undefined,
  200. phase: "urgent",
  201. remainingSeconds: 179,
  202. }),
  203. );
  204. expect(html).toContain("codingPlan.quotaReset.contextReminder.expiresIn");
  205. expect(html).toContain("codingPlan.quotaReset.contextReminder.dismiss");
  206. expect(html).toContain("data-context-reset-reminder-copy");
  207. });
  208. });