taskInteractionBadge.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import type { ZCodeTaskPendingInteraction } from "@zcode/shared";
  5. import { ZCodeIntlProvider } from "../src/i18n/IntlProvider.js";
  6. import {
  7. TaskInteractionBadge,
  8. getTaskInteractionBadgePresentation,
  9. } from "../src/TaskInteractionBadge.js";
  10. const countdown: ZCodeTaskPendingInteraction = {
  11. interactionId: "ask-1",
  12. kind: "userInput",
  13. toolName: "AskUserQuestion",
  14. autoResolution: {
  15. state: "visibleCountdown",
  16. startedAt: 0,
  17. visibleAt: 60_000,
  18. deadlineAt: 300_000,
  19. },
  20. };
  21. describe("TaskInteractionBadge", () => {
  22. it("keeps the first minute static, then derives absolute four-minute progress", () => {
  23. expect(getTaskInteractionBadgePresentation(countdown, false, 0)).toEqual({
  24. kind: "userInput",
  25. canSnooze: true,
  26. });
  27. expect(getTaskInteractionBadgePresentation(countdown, false, 59_000)).toEqual({
  28. kind: "userInput",
  29. canSnooze: true,
  30. });
  31. expect(getTaskInteractionBadgePresentation(countdown, false, 60_000)).toEqual({
  32. kind: "userInput",
  33. countdownProgress: 1,
  34. canSnooze: true,
  35. });
  36. expect(getTaskInteractionBadgePresentation(countdown, false, 180_000)).toEqual({
  37. kind: "userInput",
  38. countdownProgress: 0.5,
  39. canSnooze: true,
  40. });
  41. expect(getTaskInteractionBadgePresentation(countdown, false, 300_000)).toEqual({
  42. kind: "userInput",
  43. countdownProgress: 0,
  44. canSnooze: true,
  45. });
  46. });
  47. it("returns to a static full pill after snooze and never animates permissions", () => {
  48. expect(
  49. getTaskInteractionBadgePresentation(
  50. {
  51. interactionId: "ask-1",
  52. kind: "userInput",
  53. toolName: "AskUserQuestion",
  54. autoResolution: { state: "snoozed", startedAt: 0, snoozedAt: 299_000 },
  55. },
  56. false,
  57. 300_000,
  58. ),
  59. ).toEqual({ kind: "userInput", canSnooze: false });
  60. expect(
  61. getTaskInteractionBadgePresentation(
  62. { interactionId: "permission-1", kind: "permission" },
  63. false,
  64. 300_000,
  65. ),
  66. ).toEqual({ kind: "permission", canSnooze: false });
  67. expect(
  68. getTaskInteractionBadgePresentation(
  69. { interactionId: "exit-plan-1", kind: "userInput", toolName: "ExitPlanMode" },
  70. false,
  71. 300_000,
  72. ),
  73. ).toEqual({ kind: "permission", canSnooze: false });
  74. });
  75. it("uses toolName for Ask identity and autoResolution as the old-summary fallback", () => {
  76. expect(
  77. getTaskInteractionBadgePresentation(
  78. { interactionId: "ask-new", kind: "userInput", toolName: "AskUserQuestion" },
  79. false,
  80. 0,
  81. ),
  82. ).toEqual({ kind: "userInput", canSnooze: true });
  83. expect(
  84. getTaskInteractionBadgePresentation(
  85. {
  86. interactionId: "ask-old",
  87. kind: "userInput",
  88. autoResolution: {
  89. state: "hiddenGrace",
  90. startedAt: 0,
  91. visibleAt: 60_000,
  92. deadlineAt: 300_000,
  93. },
  94. },
  95. false,
  96. 0,
  97. ),
  98. ).toEqual({ kind: "userInput", canSnooze: true });
  99. });
  100. it("keeps waiting/stop copy centered above a left-anchored fill in both locales", () => {
  101. const render = (locale: "zh-CN" | "en-US") =>
  102. renderToStaticMarkup(
  103. createElement(
  104. ZCodeIntlProvider,
  105. { initialLocale: locale },
  106. createElement(TaskInteractionBadge, {
  107. interaction: countdown,
  108. now: 180_000,
  109. onSnoozeCountdown: () => true,
  110. formatMessage: (id: string) => {
  111. if (id === "taskList.permissionTag" || id === "taskList.userInputTag") {
  112. return locale === "zh-CN" ? "等待确认" : "Awaiting approval";
  113. }
  114. if (id === "taskList.stopCountdown") {
  115. return locale === "zh-CN" ? "停止计时" : "Stop timer";
  116. }
  117. return id;
  118. },
  119. }),
  120. ),
  121. );
  122. const zh = render("zh-CN");
  123. expect(zh).toContain("等待确认");
  124. expect(zh).toContain("停止计时");
  125. expect(zh).toContain('aria-label="停止计时"');
  126. expect(zh).toContain('data-countdown-snoozable="true"');
  127. expect(zh).toContain('data-countdown-progress="0.500"');
  128. expect(zh).toContain("origin-left");
  129. expect(zh).toContain("absolute inset-0");
  130. expect(zh).toContain("relative z-1 grid whitespace-nowrap");
  131. expect(zh).toContain("bg-interaction-confirmation-surface");
  132. expect(zh).toContain("text-interaction-confirmation-foreground");
  133. expect(zh).toContain("text-ui-sm");
  134. expect(zh).toContain("bg-interaction-ask-fill");
  135. expect(zh).toContain("hover:bg-secondary");
  136. expect(render("en-US")).toContain("Awaiting approval");
  137. expect(render("en-US")).toContain("Stop timer");
  138. });
  139. it("keeps permissions static even when a snooze callback is present", () => {
  140. const html = renderToStaticMarkup(
  141. createElement(TaskInteractionBadge, {
  142. interaction: { interactionId: "permission-1", kind: "permission" },
  143. now: 180_000,
  144. onSnoozeCountdown: () => true,
  145. formatMessage: (id: string) => (id === "taskList.permissionTag" ? "等待确认" : "停止计时"),
  146. }),
  147. );
  148. expect(html).toContain("等待确认");
  149. expect(html).not.toContain("停止计时");
  150. expect(html).not.toContain("data-countdown-snoozable");
  151. expect(html).toContain("bg-interaction-confirmation-surface");
  152. expect(html).toContain("text-interaction-confirmation-foreground");
  153. });
  154. it("keeps a snoozed Ask badge green and static until the interaction resolves", () => {
  155. const html = renderToStaticMarkup(
  156. createElement(TaskInteractionBadge, {
  157. interaction: {
  158. interactionId: "ask-1",
  159. kind: "userInput",
  160. toolName: "AskUserQuestion",
  161. autoResolution: { state: "snoozed", startedAt: 0, snoozedAt: 299_000 },
  162. },
  163. now: 300_000,
  164. onSnoozeCountdown: () => true,
  165. formatMessage: (id: string) => (id === "taskList.userInputTag" ? "等待确认" : "停止计时"),
  166. }),
  167. );
  168. expect(html).toContain("等待确认");
  169. expect(html).toContain("bg-interaction-confirmation-surface");
  170. expect(html).toContain("text-ui-sm");
  171. expect(html).not.toContain("停止计时");
  172. expect(html).not.toContain("data-countdown-snoozable");
  173. });
  174. });