tui-activity-line.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { stripAnsi, visibleWidth } from '../../src/tui/rendering/text.js';
  2. import { describe, expect, it, vi } from 'vitest';
  3. import {
  4. TuiActivityLine,
  5. resolveTuiActivityColor,
  6. type TuiActivityLineState,
  7. } from '../../src/tui/shell/activity-line.js';
  8. import { createTuiChalk, tuiColors } from '../../src/tui/theme/runtime.js';
  9. import { formatTuiShortcut } from '../../src/tui/shell/shortcut-labels.js';
  10. import { createDefaultTuiKeybindingRegistry } from '../../src/tui/shell/keybindings.js';
  11. function render(state: TuiActivityLineState, width = 80): string {
  12. const component = new TuiActivityLine(state, {
  13. animate: false,
  14. now: () => 10_000,
  15. });
  16. return component.render(width).join('\n');
  17. }
  18. describe('TuiActivityLine', () => {
  19. it('renders a sanitized model retry counter as transient activity', () => {
  20. expect(
  21. render({
  22. phase: 'retrying',
  23. runId: 'turn-1',
  24. message: 'Retrying model request · 2/5',
  25. }),
  26. ).toContain('Retrying model request · 2/5');
  27. });
  28. it('stays out of the message stream while idle', () => {
  29. expect(render({ phase: 'idle' })).toBe('');
  30. });
  31. it.each([
  32. [{ phase: 'loading', runId: 'turn-1', startedAtMs: 8_000 }, 'Loading'],
  33. [{ phase: 'loading', loadingTarget: 'session', startedAtMs: 8_000 }, 'Loading session'],
  34. [{ phase: 'running', runId: 'turn-1', startedAtMs: 8_000 }, 'Running'],
  35. [{ phase: 'reconnecting', runId: 'turn-1', startedAtMs: 8_000 }, 'Reconnecting'],
  36. [{ phase: 'compacting', runId: 'turn-1', startedAtMs: 8_000 }, 'Compacting context'],
  37. [{ phase: 'stopping', runId: 'turn-1', startedAtMs: 8_000 }, 'Stopping response'],
  38. ] satisfies Array<[TuiActivityLineState, string]>)(
  39. '%s renders one aggregate label',
  40. (state, label) => {
  41. const output = render(state);
  42. expect(output).toContain(label);
  43. expect(output).toContain('2s');
  44. expect(output).not.toMatch(/Esc|waiting|Read ·|src\//u);
  45. },
  46. );
  47. it.each([
  48. [60_000, '1min0s'],
  49. [3_599_999, '59min59s'],
  50. [3_600_000, '1h0min0s'],
  51. [7_731_000, '2h8min51s'],
  52. [7_770_000, '2h9min30s'],
  53. ])('renders %s ms using compact hours, minutes, and seconds', (elapsedMs, expected) => {
  54. const state: TuiActivityLineState = {
  55. phase: 'loading',
  56. runId: 'turn-1',
  57. startedAtMs: 10_000 - elapsedMs,
  58. controls: ['guide', 'details', 'interrupt'],
  59. };
  60. expect(render(state, 120)).toContain(`Loading ${expected}`);
  61. for (const width of [16, 24, 40, 80]) {
  62. expect(visibleWidth(render(state, width))).toBeLessThanOrEqual(width);
  63. }
  64. });
  65. it('makes a stopping response read as a danger state instead of running', () => {
  66. const output = render({ phase: 'stopping', runId: 'turn-1', startedAtMs: 8_000 });
  67. expect(output).toContain('! Stopping response');
  68. expect(resolveTuiActivityColor({ phase: 'stopping' })).toBe(tuiColors.error);
  69. expect(resolveTuiActivityColor({ phase: 'running' })).toBe(tuiColors.orbit);
  70. });
  71. it('renders a bounded Runtime error without a spinner or elapsed timer', () => {
  72. const component = new TuiActivityLine(
  73. { phase: 'error', message: 'authentication expired\u001B[31m' },
  74. { animate: true, now: () => 10_000 },
  75. );
  76. const rendered = stripAnsi(component.render(80).join('\n'));
  77. expect(rendered).toContain('Error · authentication expired');
  78. expect(rendered).not.toContain('10s');
  79. expect(rendered).not.toContain('⠋');
  80. component.dispose();
  81. });
  82. it('suppresses the visible row for a settled failure already shown in the transcript', () => {
  83. // The generic "Runtime error" label would only repeat the transcript error. The machine
  84. // readback keeps the failure on the shell state (agentErrorSettled), independent of this row.
  85. expect(render({ phase: 'error', errorSettled: true })).toBe('');
  86. expect(render({ phase: 'error', errorSettled: true, runId: 'turn_boom' })).toBe('');
  87. });
  88. it('still renders an error row when the failure carries its own message', () => {
  89. const rendered = stripAnsi(
  90. render({ phase: 'error', errorSettled: true, message: 'boom' }),
  91. );
  92. expect(rendered).toContain('Error · boom');
  93. });
  94. it.each([80, 40, 28])(
  95. 'fits the aggregate spinner, label, and elapsed time at %s columns',
  96. (width) => {
  97. const output = render({ phase: 'running', runId: 'turn-1', startedAtMs: 8_000 }, width);
  98. expect(output).toContain('◇');
  99. expect(output).toContain('Running');
  100. expect(output).toContain('2s');
  101. expect(visibleWidth(output)).toBeLessThanOrEqual(width);
  102. },
  103. );
  104. it('offers the run controls next to the run they act on', () => {
  105. const output = render({
  106. phase: 'running',
  107. runId: 'turn-1',
  108. startedAtMs: 8_000,
  109. controls: ['guide', 'interrupt'],
  110. });
  111. expect(output).toContain('Running 2s');
  112. expect(output).toContain(`${formatTuiShortcut('enter')} steer`);
  113. expect(output).toContain('Esc stop');
  114. });
  115. it('renders controls from the active host keybinding registry', () => {
  116. const keybindings = createDefaultTuiKeybindingRegistry().withOverrides({
  117. 'run.submit-guidance': 'ctrl+y',
  118. });
  119. const component = new TuiActivityLine(
  120. {
  121. phase: 'running',
  122. runId: 'turn-1',
  123. controls: ['guide'],
  124. },
  125. { animate: false, keybindings },
  126. );
  127. expect(component.render(80).join('\n')).toContain('Ctrl+Y steer');
  128. });
  129. it('shows one-decimal provider output throughput while a run is active', () => {
  130. const output = render({
  131. phase: 'running',
  132. runId: 'turn-1',
  133. startedAtMs: 8_000,
  134. outputTokensPerSecond: 63,
  135. });
  136. expect(output).toContain('⚡ 63.0 tok/s');
  137. });
  138. it.each([undefined, 0, Number.NaN, Number.POSITIVE_INFINITY])(
  139. 'hides unavailable output throughput (%s)',
  140. (outputTokensPerSecond) => {
  141. const output = render({
  142. phase: 'running',
  143. runId: 'turn-1',
  144. outputTokensPerSecond,
  145. });
  146. expect(output).not.toContain('tok/s');
  147. },
  148. );
  149. it('drops steer before interrupt when the line runs out of room', () => {
  150. const state: TuiActivityLineState = {
  151. phase: 'running',
  152. runId: 'turn-1',
  153. startedAtMs: 8_000,
  154. controls: ['guide', 'interrupt'],
  155. };
  156. const full = visibleWidth(render(state, 200));
  157. const withoutGuide = render(state, full - 1);
  158. expect(withoutGuide).not.toContain('steer');
  159. expect(withoutGuide).toContain('Esc stop');
  160. expect(visibleWidth(withoutGuide)).toBeLessThanOrEqual(full - 1);
  161. const bare = render(state, 20);
  162. expect(bare).toContain('Running');
  163. expect(bare).not.toContain('stop');
  164. expect(visibleWidth(bare)).toBeLessThanOrEqual(20);
  165. });
  166. it('reveals elapsed time after the initial 0s flash window', () => {
  167. let now = 1_000;
  168. const component = new TuiActivityLine(
  169. { phase: 'loading', runId: 'turn-1' },
  170. { animate: false, now: () => now },
  171. );
  172. now = 2_499;
  173. expect(component.render(80).join('\n')).toContain('Loading');
  174. expect(component.render(80).join('\n')).not.toContain('0s');
  175. now = 2_500;
  176. expect(component.render(80).join('\n')).toContain('Loading 1s');
  177. });
  178. it('keeps one turn clock across loading, running, reconnecting, and stopping', () => {
  179. let now = 1_000;
  180. const component = new TuiActivityLine(
  181. { phase: 'loading', runId: 'turn-1' },
  182. { animate: false, now: () => now },
  183. );
  184. now = 4_000;
  185. component.setState({ phase: 'running', runId: 'turn-1' });
  186. now = 7_000;
  187. component.setState({ phase: 'reconnecting', runId: 'turn-1' });
  188. now = 9_000;
  189. component.setState({ phase: 'stopping', runId: 'turn-1' });
  190. expect(component.render(80).join('\n')).toContain('8s');
  191. });
  192. it('preserves the turn clock while an interaction panel temporarily owns the status', () => {
  193. let now = 1_000;
  194. const component = new TuiActivityLine(
  195. { phase: 'running', runId: 'turn-1' },
  196. { animate: false, now: () => now },
  197. );
  198. now = 5_000;
  199. component.setState({ phase: 'idle', runId: 'turn-1' });
  200. expect(component.render(80)).toEqual([]);
  201. now = 9_000;
  202. component.setState({ phase: 'running', runId: 'turn-1' });
  203. expect(component.render(80).join('\n')).toContain('8s');
  204. });
  205. it('animates a Braille spinner without painting terminal backgrounds', () => {
  206. vi.useFakeTimers();
  207. try {
  208. const component = new TuiActivityLine(
  209. { phase: 'running', runId: 'turn-1', startedAtMs: 8_000 },
  210. {
  211. animate: true,
  212. now: () => 10_000,
  213. chalk: createTuiChalk({ isTTY: true, noColor: false }),
  214. },
  215. );
  216. const first = component.render(80).join('\n');
  217. vi.advanceTimersByTime(80);
  218. const second = component.render(80).join('\n');
  219. expect(stripAnsi(first)).toContain('⠋ Running 2s');
  220. expect(stripAnsi(second)).toContain('⠙ Running 2s');
  221. expect(first).not.toContain('\u001B[48;2;');
  222. component.dispose();
  223. } finally {
  224. vi.useRealTimers();
  225. }
  226. });
  227. it('releases the animation timer when idle', () => {
  228. vi.useFakeTimers();
  229. try {
  230. const requestRender = vi.fn();
  231. const component = new TuiActivityLine(
  232. { phase: 'running', runId: 'turn-1' },
  233. { requestRender },
  234. );
  235. vi.advanceTimersByTime(240);
  236. expect(requestRender).toHaveBeenCalledTimes(3);
  237. component.setState({ phase: 'idle' });
  238. vi.advanceTimersByTime(240);
  239. expect(requestRender).toHaveBeenCalledTimes(3);
  240. component.dispose();
  241. expect(vi.getTimerCount()).toBe(0);
  242. } finally {
  243. vi.useRealTimers();
  244. }
  245. });
  246. });