tui-goal-banner.test.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { GlobalThreadGoal } from "@mavis/shared/global-events";
  3. import { createTuiChalk } from "../../src/tui/theme/runtime.js";
  4. import {
  5. TuiGoalBanner,
  6. formatGoalElapsed,
  7. formatGoalCompletionReceipt,
  8. formatGoalSummary,
  9. } from "../../src/tui/features/goal/banner.js";
  10. import { visibleWidth } from "../../src/tui/rendering/text.js";
  11. const activeGoal: GlobalThreadGoal = {
  12. goalId: "goal-1",
  13. sessionId: "session-1",
  14. objective: "Ship TUI Goal support with client-aligned interactions",
  15. status: "active",
  16. createdAt: 10,
  17. updatedAt: 20,
  18. tokensUsed: 1200,
  19. turnsUsed: 3,
  20. timeUsedSeconds: 42,
  21. tokenBudget: null,
  22. statusReason: null,
  23. hasKickoffAttachments: true,
  24. };
  25. describe("TuiGoalBanner", () => {
  26. it("stops elapsed-time renders while an interaction owns the viewport", () => {
  27. vi.useFakeTimers();
  28. try {
  29. const requestRender = vi.fn();
  30. const banner = new TuiGoalBanner({ requestRender });
  31. banner.setGoal(activeGoal);
  32. requestRender.mockClear();
  33. vi.advanceTimersByTime(1_000);
  34. expect(requestRender).toHaveBeenCalledOnce();
  35. banner.setAnimationPaused(true);
  36. requestRender.mockClear();
  37. vi.advanceTimersByTime(2_000);
  38. expect(requestRender).not.toHaveBeenCalled();
  39. banner.setAnimationPaused(false);
  40. vi.advanceTimersByTime(1_000);
  41. expect(requestRender).toHaveBeenCalledOnce();
  42. banner.dispose();
  43. } finally {
  44. vi.useRealTimers();
  45. }
  46. });
  47. it("shows status, monotonic elapsed time, attachments, objective, and lifecycle actions", () => {
  48. let now = 100_000;
  49. const requestRender = vi.fn();
  50. const banner = new TuiGoalBanner({
  51. now: () => now,
  52. animate: false,
  53. requestRender,
  54. chalk: createTuiChalk({ noColor: true }),
  55. });
  56. expect(banner.render(100)).toEqual([]);
  57. banner.setGoal(activeGoal);
  58. now += 3_000;
  59. const lines = banner.render(100);
  60. expect(lines[0]).toContain("Goal · Active · 45s active · Attachment");
  61. expect(lines[1]).toContain("1.2K tokens");
  62. expect(lines[1]).toContain("3 turns");
  63. expect(lines.join("\n")).toContain(activeGoal.objective);
  64. expect(lines.join("\n")).toContain("/goal pause");
  65. expect(lines.join("\n")).toContain("/goal edit");
  66. expect(lines.join("\n")).toContain("/goal clear");
  67. expect(requestRender).toHaveBeenCalled();
  68. });
  69. it("keeps the hour rollover and Goal receipts in the same format", () => {
  70. let now = 100_000;
  71. const banner = new TuiGoalBanner({ now: () => now, animate: false });
  72. banner.setGoal({ ...activeGoal, timeUsedSeconds: 3_599 });
  73. expect(banner.render(100).join("\n")).toContain("59min59s active");
  74. now += 1_000;
  75. expect(banner.render(100).join("\n")).toContain("1h0min0s active");
  76. const goal = { ...activeGoal, timeUsedSeconds: 7_770 };
  77. banner.setGoal(goal);
  78. expect(banner.render(100).join("\n")).toContain("2h9min30s active");
  79. expect(banner.render(24).every((line) => visibleWidth(line) <= 24)).toBe(
  80. true,
  81. );
  82. expect(formatGoalSummary(goal)).toContain("Time used: 2h9min30s");
  83. expect(
  84. formatGoalCompletionReceipt({ ...goal, status: "complete" }),
  85. ).toContain("2h9min30s");
  86. });
  87. it("keeps paused time still and fits narrow terminals", () => {
  88. let now = 200_000;
  89. const banner = new TuiGoalBanner({
  90. now: () => now,
  91. animate: false,
  92. chalk: createTuiChalk({ noColor: true }),
  93. });
  94. banner.setGoal({ ...activeGoal, status: "paused", timeUsedSeconds: 62 });
  95. now += 30_000;
  96. const lines = banner.render(24);
  97. expect(lines.join("\n")).toContain("1min2s");
  98. expect(lines.join("\n")).toContain("/goal resume");
  99. expect(lines.every((line) => visibleWidth(line) <= 24)).toBe(true);
  100. });
  101. /**
  102. * A parked Goal used to read as plain "Active" here, so a verification that
  103. * legitimately runs for minutes was indistinguishable from a stuck Goal.
  104. * Every wait the runtime can publish must be visible, not just verification.
  105. */
  106. it.each([
  107. ["verification", "Verifying the result"],
  108. ["questionnaire", "Waiting for your answer"],
  109. ["permission", "Waiting for permission"],
  110. ["plan", "Waiting for Plan to finish"],
  111. ["required_background", "Waiting for background tasks"],
  112. ["automation_owner_conflict", "Waiting for automation"],
  113. ["dependency_unavailable", "Waiting for a dependency"],
  114. ["unknown", "Waiting for requirements"],
  115. ] as const)("reports an active Goal parked on %s", (reason, label) => {
  116. const banner = new TuiGoalBanner({
  117. animate: false,
  118. chalk: createTuiChalk({ noColor: true }),
  119. });
  120. banner.setGoal({ ...activeGoal, executionWait: { reason, sinceMs: 30 } });
  121. const output = banner.render(100).join("\n");
  122. expect(output).toContain(`Goal · ${label}`);
  123. expect(output).not.toContain("Goal · Active");
  124. // A wait replaces the status label; it is not a lifecycle state, so the
  125. // Goal keeps its `active` actions.
  126. expect(output).toContain("/goal pause");
  127. });
  128. it("returns to the plain active label once the wait is cleared", () => {
  129. const banner = new TuiGoalBanner({
  130. animate: false,
  131. chalk: createTuiChalk({ noColor: true }),
  132. });
  133. banner.setGoal({
  134. ...activeGoal,
  135. executionWait: { reason: "verification", sinceMs: 30 },
  136. });
  137. expect(banner.render(100).join("\n")).toContain("Verifying the result");
  138. // The runtime omits the field entirely once the wait retires.
  139. banner.setGoal({ ...activeGoal });
  140. const output = banner.render(100).join("\n");
  141. expect(output).toContain("Goal · Active");
  142. expect(output).not.toContain("Verifying the result");
  143. });
  144. /**
  145. * Waits are an execution detail *inside* `active`. A projection that arrived
  146. * out of order must never make a paused Goal claim it is still verifying.
  147. */
  148. it("ignores a wait carried by a non-active Goal", () => {
  149. const banner = new TuiGoalBanner({
  150. animate: false,
  151. chalk: createTuiChalk({ noColor: true }),
  152. });
  153. banner.setGoal({
  154. ...activeGoal,
  155. status: "paused",
  156. executionWait: { reason: "verification", sinceMs: 30 },
  157. });
  158. const output = banner.render(100).join("\n");
  159. expect(output).toContain("Goal · Paused");
  160. expect(output).not.toContain("Verifying the result");
  161. });
  162. it("does not keep a completed Goal in the persistent composer banner", () => {
  163. const banner = new TuiGoalBanner({
  164. animate: false,
  165. chalk: createTuiChalk({ noColor: true }),
  166. });
  167. banner.setGoal({ ...activeGoal, status: "complete" });
  168. expect(banner.render(100)).toEqual([]);
  169. });
  170. it("guides a budget-limited Goal to replacement without offering resume", () => {
  171. const banner = new TuiGoalBanner({
  172. animate: false,
  173. chalk: createTuiChalk({ noColor: true }),
  174. });
  175. banner.setGoal({ ...activeGoal, status: "budget_limited" });
  176. const output = banner.render(100).join("\n");
  177. expect(output).toContain(
  178. "/goal clear, then /goal <objective> starts a new Goal",
  179. );
  180. expect(output).not.toContain("/goal resume");
  181. });
  182. it("shows a typed usage limitation and its recovery action", () => {
  183. const banner = new TuiGoalBanner({
  184. animate: false,
  185. chalk: createTuiChalk({ noColor: true }),
  186. });
  187. banner.setGoal({
  188. ...activeGoal,
  189. status: "usage_limited",
  190. statusReason: "usage_limited(provider_quota)",
  191. });
  192. const output = banner.render(180).join("\n");
  193. expect(output).toContain("Goal · Usage limited");
  194. expect(output).not.toContain("usage_limited(provider_quota)");
  195. expect(output).toContain("Resume after provider access recovers");
  196. expect(output).toContain("/goal resume");
  197. expect(output).not.toContain("Complete");
  198. });
  199. it.each([
  200. ["met", "Met"],
  201. ["impossible", "Impossible"],
  202. ["inconclusive", "Inconclusive"],
  203. ] as const)("shows the latest %s verifier verdict", (verdict, label) => {
  204. const banner = new TuiGoalBanner({
  205. animate: false,
  206. chalk: createTuiChalk({ noColor: true }),
  207. });
  208. banner.setGoal({
  209. ...activeGoal,
  210. lastVerification: {
  211. backend: "evaluator",
  212. verdict,
  213. reason: `${verdict} reason`,
  214. missing: [],
  215. notMetStreak: 0,
  216. at: 1_700_000_000_000,
  217. },
  218. });
  219. const output = banner.render(180).join("\n");
  220. expect(output).toContain(`Latest verifier: ${label}`);
  221. expect(output).not.toContain("not-met streak");
  222. expect(output).not.toContain("missing:");
  223. });
  224. it("shows not_met streak and a bounded missing summary on its own fitted line", () => {
  225. const banner = new TuiGoalBanner({
  226. animate: false,
  227. chalk: createTuiChalk({ noColor: true }),
  228. });
  229. banner.setGoal({
  230. ...activeGoal,
  231. lastVerification: {
  232. backend: "evaluator",
  233. verdict: "not_met",
  234. reason: "Release evidence is incomplete",
  235. missing: ["smoke\n test", "release approval", "artifact checksum"],
  236. notMetStreak: 2,
  237. at: 1_700_000_000_000,
  238. },
  239. });
  240. const wide = banner.render(180);
  241. expect(wide[2]).toContain("Latest verifier: Not met");
  242. expect(wide[2]).toContain("not-met streak 2");
  243. expect(wide[2]).toContain("missing: smoke test; release approval +1");
  244. const narrow = banner.render(24);
  245. expect(narrow.every((line) => visibleWidth(line) <= 24)).toBe(true);
  246. });
  247. it("retains seconds in compact Goal durations beyond one hour", () => {
  248. expect(formatGoalElapsed(8)).toBe("8s");
  249. expect(formatGoalElapsed(62)).toBe("1min2s");
  250. expect(formatGoalElapsed(7_320)).toBe("2h2min0s");
  251. expect(formatGoalElapsed(7_770)).toBe("2h9min30s");
  252. });
  253. });