tui-transcript-view.test.ts 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  1. import { pathToFileURL } from 'node:url';
  2. import { stripVTControlCharacters } from 'node:util';
  3. import { visibleWidth } from '../../src/tui/rendering/text.js';
  4. import { getCapabilities, setCapabilities } from '../../src/tui/engine/public.js';
  5. import { describe, expect, it } from 'vitest';
  6. import { TranscriptView, resolveTuiDecisionColor } from '../../src/tui/transcript/view.js';
  7. import { createTranscriptCell } from '../../src/tui/transcript/model.js';
  8. import { TranscriptStore } from '../../src/tui/transcript/store.js';
  9. import {
  10. applyTuiRenderTheme,
  11. createTuiChalk,
  12. getTuiThemeSnapshot,
  13. tuiColors,
  14. } from '../../src/tui/theme/runtime.js';
  15. import { MINIMAX_CODE_DARK_THEME, MINIMAX_CODE_LIGHT_THEME } from '../../src/tui/theme/palettes.js';
  16. const previewDisplayModes = {
  17. revision: 0,
  18. resolveMainDisplayMode: () => 'preview' as const,
  19. };
  20. describe('TranscriptView', () => {
  21. it('renders a Markdown question receipt (Plan Review) as formatted prose', () => {
  22. const view = new TranscriptView(() => [
  23. createTranscriptCell({
  24. id: 'question:plan-review-view',
  25. kind: 'question',
  26. status: 'blocked',
  27. title: 'Plan Review',
  28. content: '# Implementation plan\n\n1. Add Runtime projection\n2. Add TUI controls',
  29. contentFormat: 'markdown',
  30. ephemeral: true,
  31. createdAtMs: 1,
  32. }),
  33. ]);
  34. const rendered = view.render(80).join('\n');
  35. // Heading marker + title, and the plan body rendered (heading hash stripped by Markdown).
  36. expect(rendered).toContain('Plan Review');
  37. expect(rendered).toContain('Implementation plan');
  38. expect(rendered).toContain('Add Runtime projection');
  39. expect(rendered).not.toContain('# Implementation plan');
  40. });
  41. it('renders a resolved Markdown question receipt with the decision detail', () => {
  42. const view = new TranscriptView(() => [
  43. createTranscriptCell({
  44. id: 'question:plan-review-resolved-view',
  45. kind: 'question',
  46. status: 'resolved',
  47. title: 'Plan Review',
  48. content: '# Plan\n\nShip it.',
  49. contentFormat: 'markdown',
  50. detail: 'Approved · implementation queued',
  51. ephemeral: true,
  52. createdAtMs: 1,
  53. }),
  54. ]);
  55. const rendered = view.render(80).join('\n');
  56. expect(rendered).toContain('Ship it.');
  57. expect(rendered).toContain('Approved · implementation queued');
  58. });
  59. it('marks user and Assistant blocks as semantic terminal zones', () => {
  60. const view = new TranscriptView(() => [
  61. createTranscriptCell({
  62. id: 'user-semantic-zone',
  63. kind: 'user',
  64. status: 'succeeded',
  65. content: 'Inspect the renderer',
  66. createdAtMs: 1,
  67. }),
  68. createTranscriptCell({
  69. id: 'assistant-semantic-zone',
  70. kind: 'assistant',
  71. status: 'succeeded',
  72. content: 'The renderer is stable.',
  73. createdAtMs: 2,
  74. }),
  75. ]);
  76. const lines = view.render(60);
  77. expect(lines.filter((line) => line.startsWith('\x1b]133;A\x07'))).toHaveLength(2);
  78. expect(lines.filter((line) => line.includes('\x1b]133;B\x07\x1b]133;C\x07'))).toHaveLength(2);
  79. });
  80. it('attaches a pending steer to the active flow as the next instruction', () => {
  81. const width = 60;
  82. const view = new TranscriptView(() => [
  83. createTranscriptCell({
  84. id: 'active-tool',
  85. kind: 'tool',
  86. status: 'running',
  87. title: 'bash',
  88. content: JSON.stringify({ command: 'pnpm test' }),
  89. turnId: 'turn-active',
  90. createdAtMs: 1,
  91. }),
  92. createTranscriptCell({
  93. id: 'pending-steer',
  94. kind: 'user',
  95. status: 'pending',
  96. content: 'Only inspect the login flow',
  97. userPresentation: 'pending-steer',
  98. turnId: 'turn-active',
  99. createdAtMs: 2,
  100. }),
  101. ]);
  102. const lines = view.render(width).map((line) => stripVTControlCharacters(line).trimEnd());
  103. const toolIndex = lines.findIndex((line) => line.includes('pnpm test'));
  104. const steerIndex = lines.findIndex((line) => line.includes('Only inspect the login flow'));
  105. expect(lines[steerIndex]).toBe(' ↳ Next · Only inspect the login flow');
  106. expect(steerIndex).toBe(toolIndex + 1);
  107. expect(lines.every((line) => visibleWidth(line) <= width)).toBe(true);
  108. });
  109. it.each([0, 1, 2, 3, 4, 7])(
  110. 'keeps the pending steer annotation within a %i-column viewport',
  111. (width) => {
  112. const view = new TranscriptView(() => [
  113. createTranscriptCell({
  114. id: 'pending-steer-narrow',
  115. kind: 'user',
  116. status: 'pending',
  117. content: 'Steer',
  118. userPresentation: 'pending-steer',
  119. createdAtMs: 1,
  120. }),
  121. ]);
  122. const lines = view.render(width).map(stripVTControlCharacters);
  123. expect(lines.every((line) => visibleWidth(line) <= width)).toBe(true);
  124. },
  125. );
  126. it('aligns wrapped pending steer content beneath the instruction text', () => {
  127. const view = new TranscriptView(() => [
  128. createTranscriptCell({
  129. id: 'pending-steer-wrapped',
  130. kind: 'user',
  131. status: 'pending',
  132. content: 'Compare this implementation with the other repositories before changing it',
  133. userPresentation: 'pending-steer',
  134. createdAtMs: 1,
  135. }),
  136. ]);
  137. const lines = view.render(32).map((line) => stripVTControlCharacters(line).trimEnd());
  138. expect(lines[0]).toMatch(/^ {2}↳ Next · /u);
  139. expect(lines.length).toBeGreaterThan(1);
  140. expect(lines.slice(1).every((line) => line.startsWith(' '.repeat(11)))).toBe(true);
  141. expect(lines.every((line) => visibleWidth(line) <= 32)).toBe(true);
  142. });
  143. it.each([
  144. ['succeeded', 'Not applied'],
  145. ['blocked', 'Not applied'],
  146. ['cancelled', 'Not applied'],
  147. ['failed', 'May not have applied'],
  148. ] as const)('settles an unconsumed %s steer annotation', (status, label) => {
  149. const view = new TranscriptView(() => [
  150. createTranscriptCell({
  151. id: `pending-steer-${status}`,
  152. kind: 'user',
  153. status,
  154. content: 'Steer',
  155. userPresentation: 'pending-steer',
  156. createdAtMs: 1,
  157. }),
  158. ]);
  159. const rendered = view.render(60).map(stripVTControlCharacters).join('\n');
  160. expect(rendered).toContain(label);
  161. expect(rendered).not.toContain('Next ·');
  162. });
  163. it.each([
  164. [25_400, '25s'],
  165. [60_000, '1min0s'],
  166. [7_770_000, '2h9min30s'],
  167. ])('renders completed Run duration of %s ms as %s', (durationMs, expected) => {
  168. const view = new TranscriptView(() => [
  169. createTranscriptCell({
  170. id: 'turn-duration-1',
  171. kind: 'turn-duration',
  172. status: 'succeeded',
  173. content: '',
  174. durationMs,
  175. createdAtMs: 1,
  176. }),
  177. ]);
  178. expect(view.render(60).join('\n')).toContain(`└ Completed in ${expected}`);
  179. });
  180. it('distinguishes an interrupted Run from a completed response', () => {
  181. const view = new TranscriptView(() => [
  182. createTranscriptCell({
  183. id: 'turn-duration-cancelled',
  184. kind: 'turn-duration',
  185. status: 'cancelled',
  186. content: '',
  187. durationMs: 1_250,
  188. createdAtMs: 1,
  189. }),
  190. ]);
  191. expect(view.render(60).join('\n')).toContain('└ Interrupted after 1s');
  192. });
  193. it.each([
  194. ['running', 'Compacting context', '•'],
  195. ['succeeded', 'Context compacted', '•'],
  196. ['failed', 'Context compaction failed', '×'],
  197. ] as const)(
  198. 'renders Runtime-owned compaction %s as a title-only state row',
  199. (status, title, marker) => {
  200. const view = new TranscriptView(() => [
  201. createTranscriptCell({
  202. id: 'compaction-1',
  203. kind: 'compaction',
  204. status,
  205. title,
  206. content: 'summary must stay hidden',
  207. createdAtMs: 1,
  208. }),
  209. ]);
  210. const rendered = view.render(60).join('\n');
  211. expect(rendered).toContain(marker);
  212. expect(rendered).toContain(title);
  213. expect(rendered).not.toContain('summary must stay hidden');
  214. },
  215. );
  216. it('renders a completed compaction as one compact token-delta row', () => {
  217. const view = new TranscriptView(() => [
  218. createTranscriptCell({
  219. id: 'compaction-1',
  220. kind: 'compaction',
  221. status: 'succeeded',
  222. content: '',
  223. tokensBefore: 14_200,
  224. tokensAfter: 5_100,
  225. createdAtMs: 1,
  226. }),
  227. ]);
  228. expect(view.render(60).join('\n')).toContain('• Context compacted · 14.2k → 5.1k tokens');
  229. });
  230. it('reuses an unchanged transcript frame and invalidates it when a cell changes', () => {
  231. let cells = [
  232. createTranscriptCell({
  233. id: 'assistant-1',
  234. kind: 'assistant',
  235. status: 'succeeded',
  236. content: 'Stable answer',
  237. createdAtMs: 1,
  238. }),
  239. ];
  240. const view = new TranscriptView(() => cells);
  241. const first = view.render(50);
  242. expect(view.render(50)).toBe(first);
  243. const original = cells[0];
  244. if (!original) throw new Error('missing transcript fixture');
  245. cells = [{ ...original, content: 'Updated answer', updatedAtMs: 2 }];
  246. const updated = view.render(50);
  247. expect(updated).not.toBe(first);
  248. expect(updated.join('\n')).toContain('Updated answer');
  249. });
  250. it('renders rich delivery markup as a compact terminal file receipt', () => {
  251. const view = new TranscriptView(() => [
  252. createTranscriptCell({
  253. id: 'assistant-assets',
  254. kind: 'assistant',
  255. status: 'succeeded',
  256. content: [
  257. 'Done.',
  258. '<deliver-assets>',
  259. '<media src="/workspace/chart.png" caption="Chart" />',
  260. '</deliver-assets>',
  261. ].join('\n'),
  262. createdAtMs: 1,
  263. }),
  264. ]);
  265. const rendered = stripVTControlCharacters(view.render(80).join('\n'));
  266. expect(rendered).toContain('Created chart.png ↗ · Chart');
  267. expect(rendered).not.toContain('/workspace/chart.png');
  268. expect(rendered).not.toContain('Chart · /workspace/chart.png');
  269. expect(rendered).not.toContain('<deliver-assets>');
  270. expect(rendered).not.toContain('<media');
  271. });
  272. it('keeps leaked caption text and absolute paths out of the compact receipt', () => {
  273. const path = '/Users/example/workspace/sample-project/output/mavis-architecture.html';
  274. const view = new TranscriptView(() => [
  275. createTranscriptCell({
  276. id: 'assistant-long-asset',
  277. kind: 'assistant',
  278. status: 'succeeded',
  279. content: [
  280. 'Done.',
  281. '<deliver-assets>',
  282. `<media src="${path}" caption="Mavis architecture overview · ${path}RunTurnInput leaked text" />`,
  283. '</deliver-assets>',
  284. ].join('\n'),
  285. createdAtMs: 1,
  286. }),
  287. ]);
  288. const lines = view.render(36).map((line) => stripVTControlCharacters(line).trimEnd());
  289. const receiptLine = lines.findIndex((line) => line.includes('mavis-architecture.html ↗'));
  290. const normalized = lines.join(' ').replace(/\s+/gu, ' ');
  291. expect(receiptLine).toBeGreaterThan(-1);
  292. expect(normalized).toContain('Mavis architecture overview');
  293. expect(normalized).not.toContain('leaked text');
  294. expect(normalized).not.toContain('/Users/example');
  295. });
  296. it('makes the generated file label a highlighted OSC 8 file link when supported', () => {
  297. const originalCapabilities = getCapabilities();
  298. const originalTheme = getTuiThemeSnapshot();
  299. const palette =
  300. originalTheme.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME;
  301. setCapabilities({ ...originalCapabilities, hyperlinks: true });
  302. applyTuiRenderTheme(palette, 3);
  303. try {
  304. const view = new TranscriptView(() => [
  305. createTranscriptCell({
  306. id: 'assistant-linked-asset',
  307. kind: 'assistant',
  308. status: 'succeeded',
  309. content:
  310. '<deliver-assets><media src="/workspace/hello-world.html" caption="hello-world.html" /></deliver-assets>',
  311. createdAtMs: 1,
  312. }),
  313. ]);
  314. const rendered = view.render(80).join('\n');
  315. expect(rendered).toContain(`\x1b]8;;${pathToFileURL('/workspace/hello-world.html').href}\x1b\\`);
  316. expect(rendered).toContain('\x1b[4m');
  317. expect(stripVTControlCharacters(rendered)).toContain('Created hello-world.html ↗');
  318. } finally {
  319. setCapabilities(originalCapabilities);
  320. applyTuiRenderTheme(palette, originalTheme.colorLevel);
  321. }
  322. });
  323. it('aligns assistant prose and preambles on one large dot per segment', () => {
  324. const view = new TranscriptView(() => [
  325. createTranscriptCell({
  326. id: 'assistant-preamble-anchor',
  327. kind: 'assistant-preamble',
  328. status: 'succeeded',
  329. content: 'I will inspect the transcript renderer before changing it.',
  330. createdAtMs: 1,
  331. }),
  332. createTranscriptCell({
  333. id: 'assistant-answer-anchor',
  334. kind: 'assistant',
  335. status: 'succeeded',
  336. content: 'The renderer now keeps wrapped continuation lines aligned with the answer.',
  337. createdAtMs: 2,
  338. }),
  339. ]);
  340. const lines = view
  341. .render(28)
  342. .map((line) => stripVTControlCharacters(line).trimEnd())
  343. .filter(Boolean);
  344. expect(lines.filter((line) => line.startsWith('● '))).toHaveLength(2);
  345. expect(lines.filter((line) => line.includes('●'))).toHaveLength(2);
  346. expect(lines.filter((line) => !line.includes('●')).every((line) => line.startsWith(' '))).toBe(
  347. true,
  348. );
  349. });
  350. it('renders assistant dots in the same tone as the body text', () => {
  351. const original = getTuiThemeSnapshot();
  352. const originalPalette =
  353. original.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME;
  354. applyTuiRenderTheme(MINIMAX_CODE_DARK_THEME, 3);
  355. try {
  356. const view = new TranscriptView(() => [
  357. createTranscriptCell({
  358. id: 'assistant-tone-anchor',
  359. kind: 'assistant',
  360. status: 'succeeded',
  361. content: 'Same tone',
  362. createdAtMs: 1,
  363. }),
  364. ]);
  365. const line = view.render(28)[0] ?? '';
  366. const color = createTuiChalk({ colorLevel: 3 });
  367. expect(line).toContain(color.hex(MINIMAX_CODE_DARK_THEME.colors.text)('●'));
  368. expect(line).not.toContain(color.hex(MINIMAX_CODE_DARK_THEME.colors.signal)('●'));
  369. } finally {
  370. applyTuiRenderTheme(originalPalette, original.colorLevel);
  371. }
  372. });
  373. it('keeps assistant dots and Markdown within narrow terminal widths', () => {
  374. const view = new TranscriptView(() => [
  375. createTranscriptCell({
  376. id: 'assistant-narrow-anchor',
  377. kind: 'assistant',
  378. status: 'succeeded',
  379. content: [
  380. 'A compact answer with a long line that wraps.',
  381. '',
  382. '```ts',
  383. 'const value = 1;',
  384. '```',
  385. ].join('\n'),
  386. createdAtMs: 1,
  387. }),
  388. ]);
  389. const lines = view.render(28);
  390. const plainLines = lines.map((line) => stripVTControlCharacters(line).trimEnd());
  391. expect(plainLines[0]).toMatch(/^● /u);
  392. expect(plainLines.slice(1).every((line) => !line.includes('●'))).toBe(true);
  393. expect(plainLines.join('\n')).toContain('const value = 1;');
  394. expect(lines.every((line) => visibleWidth(line) <= 28)).toBe(true);
  395. for (const width of [0, 1, 2, 3, 4, 8]) {
  396. expect(view.render(width).every((line) => visibleWidth(line) <= width)).toBe(true);
  397. }
  398. });
  399. it('renders assistant code blocks as compact highlighted content without chrome', () => {
  400. const view = new TranscriptView(() => [
  401. createTranscriptCell({
  402. id: 'assistant-code',
  403. kind: 'assistant',
  404. status: 'succeeded',
  405. content:
  406. "```ts title=policy\nconst startupExecutionPolicy = 'quarantined-with-a-long-suffix';\n```",
  407. createdAtMs: 1,
  408. }),
  409. ]);
  410. const lines = view.render(28);
  411. const plainLines = lines.map((line) => stripVTControlCharacters(line).trimEnd());
  412. expect(plainLines.join('\n')).not.toContain('```');
  413. expect(plainLines.join('\n')).not.toMatch(/[╭╮╰╯│]/u);
  414. expect(plainLines.join('\n')).toContain('startupExecutionPolicy');
  415. expect(plainLines.join('\n')).toContain('quarantined-with-a-');
  416. expect(lines.every((line) => visibleWidth(line) <= 28)).toBe(true);
  417. });
  418. it('invalidates a streaming frame when content changes at the same Runtime timestamp', () => {
  419. let cells = [
  420. createTranscriptCell({
  421. id: 'thinking-live',
  422. kind: 'thinking',
  423. status: 'running',
  424. content: 'First chunk',
  425. createdAtMs: 1,
  426. updatedAtMs: 10,
  427. expanded: true,
  428. }),
  429. ];
  430. const view = new TranscriptView(() => cells);
  431. const first = view.render(50);
  432. const current = cells[0];
  433. if (!current) throw new Error('missing transcript fixture');
  434. cells = [{ ...current, content: 'First chunk\nSecond chunk' }];
  435. const second = view.render(50);
  436. expect(second).not.toBe(first);
  437. expect(second.join('\n')).toContain('Second chunk');
  438. });
  439. it('renders Pi Markdown tables, nested blocks, and partial closing fences', () => {
  440. const view = new TranscriptView(() => [
  441. createTranscriptCell({
  442. id: 'assistant-markdown',
  443. kind: 'assistant',
  444. status: 'running',
  445. content: [
  446. '| Name | State |',
  447. '| --- | --- |',
  448. '| Markdown | **ready** |',
  449. '',
  450. '> Notes',
  451. '> - nested `code`',
  452. '',
  453. '```ts',
  454. 'const value = 1;',
  455. '``',
  456. ].join('\n'),
  457. createdAtMs: 1,
  458. }),
  459. ]);
  460. const rendered = view.render(60).join('\n');
  461. expect(rendered).toContain('│ Name');
  462. expect(rendered).toContain('│ Notes');
  463. expect(rendered).toContain('- nested code');
  464. expect(rendered).toContain('const value = 1;');
  465. expect(rendered.match(/^\s*``\s*$/gmu)).toBeNull();
  466. });
  467. it('renders local inspection reports as a complete bordered table', () => {
  468. const view = new TranscriptView(() => [
  469. createTranscriptCell({
  470. id: 'inspection-status',
  471. kind: 'inspection',
  472. status: 'succeeded',
  473. content: 'MCode status\nAccount: Connected with MiniMax',
  474. inspection: {
  475. title: 'MCode status',
  476. badge: { label: 'READY', tone: 'success' },
  477. sections: [
  478. {
  479. title: 'Account',
  480. rows: [
  481. { label: 'Account', value: 'Connected with MiniMax', tone: 'success' },
  482. { label: 'Provider', value: 'minimax' },
  483. ],
  484. },
  485. {
  486. title: 'Model',
  487. rows: [{ label: 'Active', value: 'minimax/MiniMax-M3' }],
  488. },
  489. ],
  490. footer: 'Read only',
  491. },
  492. createdAtMs: 1,
  493. }),
  494. ]);
  495. const rendered = stripVTControlCharacters(view.render(60).join('\n'));
  496. expect(rendered).toContain('╭─');
  497. expect(rendered).toMatch(/│ MCode status\s+● READY │/u);
  498. expect(rendered).toContain('READY');
  499. expect(rendered).toContain('├─ Account ');
  500. expect(rendered).toMatch(/│ Account\s+│ Connected with MiniMax\s+│/u);
  501. expect(rendered).toContain('├─ Model ');
  502. expect(rendered).toMatch(/│ Read only\s+│/u);
  503. expect(rendered).toContain('╰─');
  504. expect(rendered.split('\n').every((line) => visibleWidth(line) === 60)).toBe(true);
  505. });
  506. it('stacks inspection labels and values on narrow terminals', () => {
  507. const view = new TranscriptView(() => [
  508. createTranscriptCell({
  509. id: 'inspection-config',
  510. kind: 'inspection',
  511. status: 'succeeded',
  512. content: 'Effective configuration',
  513. inspection: {
  514. title: 'Effective configuration',
  515. badge: { label: 'READ ONLY', tone: 'accent' },
  516. sections: [
  517. {
  518. title: 'Location',
  519. rows: [
  520. {
  521. label: 'Source',
  522. value: '/home/dev/.minimax/config.yaml',
  523. },
  524. ],
  525. },
  526. ],
  527. },
  528. createdAtMs: 1,
  529. }),
  530. ]);
  531. const rendered = stripVTControlCharacters(view.render(28).join('\n'));
  532. expect(rendered).toMatch(/│ Source\s+│/u);
  533. expect(rendered).toContain('│ /home/dev/.minimax');
  534. expect(rendered).toContain('│ /config.yaml');
  535. expect(rendered).not.toMatch(/│ Source\s+│?\s*\/home/u);
  536. expect(rendered).toContain('╰─');
  537. expect(rendered.split('\n').every((line) => visibleWidth(line) === 28)).toBe(true);
  538. });
  539. it('routes usage inspection reports through the compact usage visualization', () => {
  540. const view = new TranscriptView(() => [
  541. createTranscriptCell({
  542. id: 'inspection-usage',
  543. kind: 'inspection',
  544. status: 'succeeded',
  545. content: 'Session usage',
  546. inspection: {
  547. title: 'Session usage',
  548. badge: { label: 'HIDDEN', tone: 'accent' },
  549. sections: [{ title: 'Internal details', rows: [{ label: 'Turns', value: '9' }] }],
  550. footer: 'Runtime source',
  551. visualization: {
  552. kind: 'usage',
  553. model: 'minimax-cn-coding-plan/MiniMax-M3',
  554. inputTokens: 145_000,
  555. outputTokens: 1_400,
  556. totalTokens: 146_400,
  557. context: null,
  558. },
  559. },
  560. createdAtMs: 1,
  561. }),
  562. ]);
  563. const rendered = stripVTControlCharacters(view.render(60).join('\n'));
  564. expect(rendered).toContain('Usage');
  565. expect(rendered).not.toContain('HIDDEN');
  566. expect(rendered).not.toContain('Internal details');
  567. expect(rendered).not.toContain('Runtime source');
  568. expect(rendered.split('\n').every((line) => visibleWidth(line) <= 60)).toBe(true);
  569. });
  570. it('reuses unaffected item renders when Ctrl+O changes detail presentation', () => {
  571. const view = new TranscriptView(() => [
  572. createTranscriptCell({
  573. id: 'assistant-before-details',
  574. kind: 'assistant',
  575. status: 'succeeded',
  576. content: 'Stable answer',
  577. createdAtMs: 1,
  578. }),
  579. createTranscriptCell({
  580. id: 'tool-details',
  581. kind: 'tool',
  582. status: 'succeeded',
  583. title: 'bash',
  584. content: '{"command":"pnpm test"}',
  585. detail: 'result line 1\nresult line 2',
  586. createdAtMs: 2,
  587. }),
  588. createTranscriptCell({
  589. id: 'summary-after-details',
  590. kind: 'final-summary',
  591. status: 'succeeded',
  592. content: 'Stable summary',
  593. createdAtMs: 3,
  594. }),
  595. ]);
  596. view.render(60);
  597. view.toggleDetailMode();
  598. const detailed = view.render(60).join('\n');
  599. const performance = view.getPerformanceSnapshot();
  600. expect(detailed).toContain('result line 1');
  601. expect(performance.unitCacheHits).toBe(2);
  602. expect(performance.unitCacheMisses).toBe(1);
  603. });
  604. it('rerenders only the active item while detailed content streams', () => {
  605. const store = new TranscriptStore([
  606. createTranscriptCell({
  607. id: 'settled-before-stream',
  608. kind: 'assistant',
  609. status: 'succeeded',
  610. content: 'Stable answer',
  611. createdAtMs: 1,
  612. }),
  613. createTranscriptCell({
  614. id: 'active-thinking-stream',
  615. kind: 'thinking',
  616. status: 'running',
  617. content: 'Inspecting',
  618. createdAtMs: 2,
  619. }),
  620. ]);
  621. const view = new TranscriptView(store);
  622. view.toggleDetailMode();
  623. view.render(60);
  624. store.upsert({
  625. id: 'active-thinking-stream',
  626. content: 'Inspecting the renderer',
  627. updatedAtMs: 3,
  628. });
  629. const rendered = view.render(60).join('\n');
  630. const performance = view.getPerformanceSnapshot();
  631. expect(rendered).toContain('Inspecting the renderer');
  632. expect(performance.unitCacheHits).toBe(1);
  633. expect(performance.unitCacheMisses).toBe(1);
  634. });
  635. it('uses the generic fallback for tools without a built-in definition', () => {
  636. const view = new TranscriptView(() => [
  637. createTranscriptCell({
  638. id: 'deploy-1',
  639. kind: 'tool',
  640. status: 'succeeded',
  641. title: 'deploy',
  642. content: 'preview',
  643. createdAtMs: 1,
  644. }),
  645. ]);
  646. expect(view.render(50).join('\n')).toContain('Deploy (preview)');
  647. });
  648. it('renders product Web Tool summaries and hides output in compact mode', () => {
  649. const view = new TranscriptView(() => [
  650. createTranscriptCell({
  651. id: 'web-search',
  652. kind: 'tool',
  653. status: 'succeeded',
  654. title: 'mcp__matrix__web_search',
  655. content: '{"query":"AI news today 2026-08-07"}',
  656. detail: 'private search result',
  657. createdAtMs: 1,
  658. }),
  659. createTranscriptCell({
  660. id: 'web-fetch',
  661. kind: 'tool',
  662. status: 'succeeded',
  663. title: 'web_fetch',
  664. content: '{"url":"https://example.com/article"}',
  665. detail: 'private fetched body',
  666. createdAtMs: 2,
  667. }),
  668. ]);
  669. const compact = stripVTControlCharacters(view.render(90).join('\n'));
  670. expect(compact).toContain('Used WebSearch (AI news today 2026-08-07)');
  671. expect(compact).toContain('Used WebFetch (https://example.com/article)');
  672. expect(compact).not.toContain('matrix.web_search');
  673. expect(compact).not.toContain('private search result');
  674. expect(compact).not.toContain('private fetched body');
  675. view.toggleDetailMode();
  676. const detailed = stripVTControlCharacters(view.render(90).join('\n'));
  677. expect(detailed).toContain('private search result');
  678. expect(detailed).toContain('private fetched body');
  679. });
  680. it('keeps non-Web MCP identities while hiding their output in compact mode', () => {
  681. const view = new TranscriptView(() => [
  682. createTranscriptCell({
  683. id: 'mcp-1',
  684. kind: 'tool',
  685. status: 'succeeded',
  686. title: 'mcp__jira__create_issue',
  687. content: '{"description":"Broken checkout"}',
  688. detail: 'issue-123',
  689. createdAtMs: 1,
  690. }),
  691. ]);
  692. const compact = stripVTControlCharacters(view.render(72).join('\n'));
  693. expect(compact).toContain('Called jira.create_issue (Broken checkout)');
  694. expect(compact).not.toContain('issue-123');
  695. view.toggleDetailMode();
  696. expect(stripVTControlCharacters(view.render(72).join('\n'))).toContain('issue-123');
  697. });
  698. it('keeps failed Web Tool errors hidden until detailed mode is requested', () => {
  699. const view = new TranscriptView(() => [
  700. createTranscriptCell({
  701. id: 'web-fetch-failed',
  702. kind: 'tool',
  703. status: 'failed',
  704. title: 'web_fetch',
  705. content: '{"url":"https://example.com/missing"}',
  706. detail: 'HTTP 404 response body',
  707. createdAtMs: 1,
  708. }),
  709. ]);
  710. const compact = stripVTControlCharacters(view.render(72).join('\n'));
  711. expect(compact).toContain('WebFetch failed (https://example.com/missing)');
  712. expect(compact).not.toContain('HTTP 404 response body');
  713. view.toggleDetailMode();
  714. expect(stripVTControlCharacters(view.render(72).join('\n'))).toContain(
  715. 'HTTP 404 response body',
  716. );
  717. });
  718. it('hides structured changes until Tool details are requested', () => {
  719. const view = new TranscriptView(() => [
  720. createTranscriptCell({
  721. id: 'edit-1',
  722. kind: 'tool',
  723. status: 'succeeded',
  724. title: 'edit',
  725. content: '{"path":"src/greeting.ts"}',
  726. structuredPreview: {
  727. schemaVersion: 1,
  728. state: 'applied',
  729. blocks: [
  730. {
  731. kind: 'diff',
  732. path: 'src/greeting.ts',
  733. diff: '- old greeting\n+ new greeting',
  734. addedLines: 1,
  735. removedLines: 1,
  736. truncated: false,
  737. },
  738. ],
  739. },
  740. createdAtMs: 1,
  741. turnId: 'turn-1',
  742. }),
  743. ]);
  744. const compact = stripVTControlCharacters(view.render(60).join('\n'));
  745. expect(compact).toContain('Edited (src/greeting.ts)');
  746. expect(compact).not.toContain('Applied');
  747. expect(compact).not.toContain('- old greeting');
  748. expect(compact).not.toContain('+ new greeting');
  749. view.toggleDetailMode();
  750. const detailed = stripVTControlCharacters(view.render(60).join('\n'));
  751. expect(detailed).toContain('Applied');
  752. expect(detailed).toContain('- old greeting');
  753. expect(detailed).toContain('+ new greeting');
  754. });
  755. it.each([{ width: 80 }, { width: 40 }, { width: 28 }])(
  756. 'keeps Tool rows within the terminal width at $width columns',
  757. ({ width }) => {
  758. const view = new TranscriptView(() => [
  759. createTranscriptCell({
  760. id: `responsive-tool-${width}`,
  761. kind: 'tool',
  762. status: 'succeeded',
  763. title: 'bash',
  764. content: JSON.stringify({ command: 'pnpm test' }),
  765. detail: 'All tests passed',
  766. createdAtMs: 1,
  767. }),
  768. ]);
  769. const lines = view.render(width);
  770. expect(lines.every((line) => visibleWidth(line) <= width)).toBe(true);
  771. },
  772. );
  773. it('avoids duplicate structured success output but keeps the failure reason', () => {
  774. const preview = {
  775. schemaVersion: 1 as const,
  776. state: 'not-applied' as const,
  777. blocks: [
  778. {
  779. kind: 'diff' as const,
  780. path: 'src/greeting.ts',
  781. diff: '- old greeting\n+ new greeting',
  782. addedLines: 1,
  783. removedLines: 1,
  784. truncated: false,
  785. },
  786. ],
  787. };
  788. const succeeded = createTranscriptCell({
  789. id: 'edit-succeeded',
  790. kind: 'tool',
  791. status: 'succeeded',
  792. title: 'edit',
  793. content: '{"path":"src/greeting.ts"}',
  794. detail: 'duplicate raw success payload',
  795. structuredPreview: { ...preview, state: 'applied' },
  796. createdAtMs: 1,
  797. });
  798. const failed = createTranscriptCell({
  799. ...succeeded,
  800. id: 'edit-failed',
  801. status: 'failed',
  802. detail: 'Permission denied',
  803. structuredPreview: preview,
  804. });
  805. const successView = new TranscriptView(() => [succeeded]);
  806. const failureView = new TranscriptView(() => [failed]);
  807. successView.toggleDetailMode();
  808. failureView.toggleDetailMode();
  809. const successOutput = stripVTControlCharacters(successView.render(64).join('\n'));
  810. const failureOutput = stripVTControlCharacters(failureView.render(64).join('\n'));
  811. expect(successOutput).toContain('Applied');
  812. expect(successOutput).not.toContain('duplicate raw success payload');
  813. expect(failureOutput).toContain('Not applied');
  814. expect(failureOutput).toContain('Permission denied');
  815. });
  816. it('keeps completed Diff previews hidden until inline details are requested', () => {
  817. const view = new TranscriptView(() => [
  818. createTranscriptCell({
  819. id: 'edit-large',
  820. kind: 'tool',
  821. status: 'succeeded',
  822. title: 'edit',
  823. content: '{"path":"src/large.ts"}',
  824. structuredPreview: {
  825. schemaVersion: 1,
  826. state: 'applied',
  827. blocks: [
  828. {
  829. kind: 'diff',
  830. path: 'src/large.ts',
  831. diff: Array.from({ length: 8 }, (_, index) => `+ line ${index + 1}`).join('\n'),
  832. addedLines: 8,
  833. removedLines: 0,
  834. truncated: false,
  835. },
  836. ],
  837. },
  838. createdAtMs: 1,
  839. }),
  840. ]);
  841. const compact = stripVTControlCharacters(view.render(60).join('\n'));
  842. expect(compact).toContain('Edited (src/large.ts)');
  843. expect(compact).not.toContain('Applied');
  844. expect(compact).not.toContain('lines hidden');
  845. expect(compact).not.toContain('+ line 8');
  846. expect(view.toggleDetailMode()).toBe('detailed');
  847. const detailed = stripVTControlCharacters(view.render(60).join('\n'));
  848. expect(detailed).toContain('+ line 8');
  849. expect(detailed).not.toContain('lines hidden');
  850. });
  851. it('renders intent and execution as one compact visual rail', () => {
  852. const view = new TranscriptView(() => [
  853. createTranscriptCell({
  854. id: 'user-1',
  855. kind: 'user',
  856. status: 'succeeded',
  857. content: 'Please **inspect** this.',
  858. createdAtMs: 1,
  859. turnId: 'turn-1',
  860. }),
  861. createTranscriptCell({
  862. id: 'thinking-1',
  863. kind: 'thinking',
  864. status: 'running',
  865. content: 'Reading the repository',
  866. createdAtMs: 2,
  867. turnId: 'turn-1',
  868. expanded: true,
  869. }),
  870. createTranscriptCell({
  871. id: 'tool-1',
  872. kind: 'tool',
  873. status: 'running',
  874. title: 'read',
  875. content: 'README.md',
  876. createdAtMs: 3,
  877. turnId: 'turn-1',
  878. }),
  879. createTranscriptCell({
  880. id: 'assistant-1',
  881. kind: 'assistant',
  882. status: 'running',
  883. content: 'I found `one` result.',
  884. createdAtMs: 4,
  885. turnId: 'turn-1',
  886. }),
  887. createTranscriptCell({
  888. id: 'error-1',
  889. kind: 'error',
  890. status: 'failed',
  891. content: 'provider unavailable',
  892. createdAtMs: 5,
  893. }),
  894. ]);
  895. const lines = view.render(50);
  896. const rendered = lines.join('\n');
  897. expect(rendered).toContain('› Please inspect this.');
  898. expect(rendered).not.toContain('YOU');
  899. expect(rendered).toContain('├ • Thinking…');
  900. expect(rendered).toContain('│ Reading the repository');
  901. expect(rendered).toContain('└ • Reading (README.md)');
  902. expect(rendered).toContain('I found one result.');
  903. expect(rendered).toContain('× Error provider unavailable');
  904. const thinkingIndex = lines.findIndex((line) => line.includes('Reading the repository'));
  905. expect(lines[thinkingIndex + 1]).toContain('Reading (README.md)');
  906. });
  907. it('renders user messages as a vertically padded full-width prompt band', () => {
  908. const original = getTuiThemeSnapshot();
  909. const originalPalette =
  910. original.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME;
  911. applyTuiRenderTheme(MINIMAX_CODE_LIGHT_THEME, 3);
  912. try {
  913. const view = new TranscriptView(() => [
  914. createTranscriptCell({
  915. id: 'user-band',
  916. kind: 'user',
  917. status: 'succeeded',
  918. content: 'Short message',
  919. createdAtMs: 1,
  920. }),
  921. ]);
  922. const lines = view.render(24);
  923. expect(lines).toHaveLength(3);
  924. expect(lines.map(stripVTControlCharacters)).toEqual([
  925. ' '.repeat(24),
  926. ' › Short message'.padEnd(24),
  927. ' '.repeat(24),
  928. ]);
  929. expect(lines.join('\n')).not.toContain('YOU');
  930. expect(lines.every((line) => line.includes('\u001B[48;2;245;245;245m'))).toBe(true);
  931. expect(lines.every((line) => visibleWidth(line) === 24)).toBe(true);
  932. } finally {
  933. applyTuiRenderTheme(originalPalette, original.colorLevel);
  934. }
  935. });
  936. it('renders structured attachment cards before user text and omits an empty text band', () => {
  937. const withText = new TranscriptView(() => [
  938. createTranscriptCell({
  939. id: 'user-with-attachments',
  940. kind: 'user',
  941. status: 'succeeded',
  942. content: 'Inspect these files',
  943. attachments: [
  944. {
  945. type: 'image',
  946. fileName: 'diagram.png',
  947. mimeType: 'image/png',
  948. sizeBytes: 2048,
  949. },
  950. {
  951. type: 'file',
  952. fileName: 'notes.md',
  953. mimeType: 'text/markdown',
  954. },
  955. {
  956. type: 'file',
  957. fileName: 'walkthrough.mov',
  958. mimeType: 'video/quicktime',
  959. sizeBytes: 4096,
  960. },
  961. ],
  962. createdAtMs: 1,
  963. }),
  964. ]);
  965. const rendered = withText.render(48).map(stripVTControlCharacters);
  966. const imageIndex = rendered.findIndex((line) => line.includes('Image diagram.png'));
  967. const fileIndex = rendered.findIndex((line) => line.includes('File notes.md'));
  968. const videoIndex = rendered.findIndex((line) => line.includes('Video walkthrough.mov'));
  969. const textIndex = rendered.findIndex((line) => line.includes('› Inspect these files'));
  970. expect(imageIndex).toBeGreaterThanOrEqual(0);
  971. expect(fileIndex).toBeGreaterThan(imageIndex);
  972. expect(videoIndex).toBeGreaterThan(fileIndex);
  973. expect(textIndex).toBeGreaterThan(videoIndex);
  974. expect(rendered.join('\n')).not.toContain('Attachments:');
  975. const attachmentOnly = new TranscriptView(() => [
  976. createTranscriptCell({
  977. id: 'attachment-only',
  978. kind: 'user',
  979. status: 'succeeded',
  980. content: '',
  981. attachments: [
  982. {
  983. type: 'image',
  984. fileName: 'only.png',
  985. mimeType: 'image/png',
  986. sizeBytes: 1024,
  987. },
  988. ],
  989. createdAtMs: 1,
  990. }),
  991. ]);
  992. const attachmentOnlyLines = attachmentOnly.render(48).map(stripVTControlCharacters);
  993. expect(attachmentOnlyLines.join('\n')).toContain('Image only.png');
  994. expect(attachmentOnlyLines.every((line) => !line.includes('›'))).toBe(true);
  995. });
  996. it('shows a bounded result preview below a completed tool', () => {
  997. const view = new TranscriptView(
  998. () => [
  999. createTranscriptCell({
  1000. id: 'tool-1',
  1001. kind: 'tool',
  1002. status: 'succeeded',
  1003. title: 'bash',
  1004. content: 'pnpm test',
  1005. detail: ['114 tests passed', '0 failed', 'completed in 1.4s', 'coverage 92%'].join('\n'),
  1006. createdAtMs: 1,
  1007. }),
  1008. ],
  1009. { displayModes: previewDisplayModes },
  1010. );
  1011. const lines = view.render(40);
  1012. const rendered = lines.join('\n');
  1013. expect(rendered).toContain('└ • Ran pnpm test · 4 output lines');
  1014. expect(rendered).toContain('$ pnpm test');
  1015. expect(rendered).toContain('114 tests passed');
  1016. expect(rendered).toContain('1 line hidden');
  1017. expect(rendered).toContain('coverage 92%');
  1018. expect(rendered).not.toContain('completed in 1.4s');
  1019. });
  1020. it('distinguishes a running background bash dispatch from a foreground command', () => {
  1021. const view = new TranscriptView(
  1022. () => [
  1023. createTranscriptCell({
  1024. id: 'tool-background-running',
  1025. kind: 'tool',
  1026. status: 'running',
  1027. title: 'bash',
  1028. content: JSON.stringify({ command: 'sleep 30', run_in_background: true }),
  1029. createdAtMs: 1,
  1030. }),
  1031. ],
  1032. { displayModes: previewDisplayModes },
  1033. );
  1034. const rendered = view.render(80).map(stripVTControlCharacters).join('\n');
  1035. expect(rendered).toContain('Starting background task');
  1036. expect(rendered).toContain('$ sleep 30');
  1037. expect(rendered).not.toContain('Running ');
  1038. });
  1039. it('shows the task id after a background bash dispatch starts', () => {
  1040. const view = new TranscriptView(
  1041. () => [
  1042. createTranscriptCell({
  1043. id: 'tool-background-started',
  1044. kind: 'tool',
  1045. status: 'succeeded',
  1046. title: 'bash',
  1047. content: JSON.stringify({ command: 'sleep 30', run_in_background: true }),
  1048. detail:
  1049. '<bash_background task_id="bg_abc123">\nStarted local background bash task bg_abc123.\n</bash_background>',
  1050. createdAtMs: 1,
  1051. }),
  1052. ],
  1053. { displayModes: previewDisplayModes },
  1054. );
  1055. const rendered = view.render(80).map(stripVTControlCharacters).join('\n');
  1056. expect(rendered).toContain('Started background task bg_abc123');
  1057. expect(rendered).toContain('$ sleep 30');
  1058. expect(rendered).not.toContain('3 output lines');
  1059. expect(rendered).not.toContain('<bash_background');
  1060. });
  1061. it('recognizes replayed background bash history from its result envelope', () => {
  1062. const view = new TranscriptView(() => [
  1063. createTranscriptCell({
  1064. id: 'tool-background-replayed',
  1065. kind: 'tool',
  1066. status: 'succeeded',
  1067. title: 'bash',
  1068. content: JSON.stringify({ command: 'sleep 30' }),
  1069. detail:
  1070. '<bash_background task_id="bg_replayed">\nStarted local background bash task bg_replayed.\n</bash_background>',
  1071. createdAtMs: 1,
  1072. }),
  1073. ]);
  1074. const rendered = view.render(80).map(stripVTControlCharacters).join('\n');
  1075. expect(rendered).toContain('Started background task bg_replayed');
  1076. expect(rendered).not.toContain('Ran 3 output lines');
  1077. });
  1078. it('keeps multiline shell commands out of the single-line execution summary', () => {
  1079. const command = [
  1080. "git commit -q -F - <<'EOF'",
  1081. 'fix(tui): keep execution rows aligned',
  1082. '',
  1083. 'Keep multiline commit messages on the detail rail.',
  1084. 'EOF',
  1085. ].join('\n');
  1086. const view = new TranscriptView(() => [
  1087. createTranscriptCell({
  1088. id: 'tool-multiline-shell',
  1089. kind: 'tool',
  1090. status: 'succeeded',
  1091. title: 'bash',
  1092. content: JSON.stringify({ command }),
  1093. createdAtMs: 1,
  1094. }),
  1095. ]);
  1096. const compact = view.render(80).map(stripVTControlCharacters);
  1097. expect(compact).toEqual(["└ • Ran git commit -q -F - <<'EOF'… · no output"]);
  1098. expect(compact.join('\n')).not.toContain('fix(tui): keep execution rows aligned');
  1099. view.toggleDetailMode();
  1100. const detailed = view.render(80).map(stripVTControlCharacters);
  1101. expect(detailed[0]).toBe("└ • Ran git commit -q -F - <<'EOF'… · no output");
  1102. expect(detailed[1]).toBe(" $ git commit -q -F - <<'EOF'");
  1103. expect(detailed[2]).toBe(' fix(tui): keep execution rows aligned');
  1104. });
  1105. it('shows the shell command alongside the output count on the collapsed row', () => {
  1106. const view = new TranscriptView(() => [
  1107. createTranscriptCell({
  1108. id: 'tool-shell-summary',
  1109. kind: 'tool',
  1110. status: 'succeeded',
  1111. title: 'bash',
  1112. content: JSON.stringify({ command: 'git merge origin/preview_train' }),
  1113. detail: 'Updating a1b2c3d..e4f5g6h\nFast-forward',
  1114. createdAtMs: 1,
  1115. }),
  1116. ]);
  1117. const rendered = view.render(80).map(stripVTControlCharacters);
  1118. expect(rendered[0]).toBe('└ • Ran git merge origin/preview_train · 2 output lines');
  1119. });
  1120. it('syntax-highlights executable commands on the collapsed shell row', () => {
  1121. const original = getTuiThemeSnapshot();
  1122. const originalPalette =
  1123. original.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME;
  1124. applyTuiRenderTheme(MINIMAX_CODE_DARK_THEME, 3);
  1125. try {
  1126. const command = 'ls packages/tui/src/ && echo "---" && ls packages/tui/src/tui/ 2>/dev/null';
  1127. const view = new TranscriptView(() => [
  1128. createTranscriptCell({
  1129. id: 'tool-shell-highlighted-summary',
  1130. kind: 'tool',
  1131. status: 'succeeded',
  1132. title: 'bash',
  1133. content: JSON.stringify({ command }),
  1134. detail: 'account\nacp',
  1135. createdAtMs: 1,
  1136. }),
  1137. ]);
  1138. const rendered = view.render(140)[0] ?? '';
  1139. const ansi = createTuiChalk({ colorLevel: 3 });
  1140. expect(stripVTControlCharacters(rendered)).toBe(`└ • Ran ${command} · 2 output lines`);
  1141. expect(rendered).toContain(ansi.bold.hex(tuiColors.accent)('ls'));
  1142. expect(rendered).toContain(ansi.bold.hex(tuiColors.accent)('echo'));
  1143. } finally {
  1144. applyTuiRenderTheme(originalPalette, original.colorLevel);
  1145. }
  1146. });
  1147. it('trims the shell command but keeps the output count when the row is narrow', () => {
  1148. const view = new TranscriptView(() => [
  1149. createTranscriptCell({
  1150. id: 'tool-shell-narrow',
  1151. kind: 'tool',
  1152. status: 'succeeded',
  1153. title: 'bash',
  1154. content: JSON.stringify({
  1155. command: 'pnpm --filter @mavis/ui exec vitest run packages/ui/test/unit/example.test.tsx',
  1156. }),
  1157. detail: 'ok',
  1158. createdAtMs: 1,
  1159. }),
  1160. ]);
  1161. const rendered = view.render(56).map(stripVTControlCharacters);
  1162. expect(rendered[0]).toContain('1 output line');
  1163. expect(rendered[0]).toContain('…');
  1164. expect(rendered[0]?.length).toBeLessThanOrEqual(56);
  1165. });
  1166. it('keeps failed search queries on a single execution-summary row', () => {
  1167. const view = new TranscriptView(() => [
  1168. createTranscriptCell({
  1169. id: 'tool-multiline-search',
  1170. kind: 'tool',
  1171. status: 'failed',
  1172. title: 'search',
  1173. content: JSON.stringify({ query: 'compact",\noutput_mode' }),
  1174. detail: 'No matches found',
  1175. createdAtMs: 1,
  1176. }),
  1177. ]);
  1178. const compact = view.render(80).map(stripVTControlCharacters);
  1179. expect(compact[0]).toBe('└ × Search failed (compact",)');
  1180. expect(compact[0]).not.toContain('\n');
  1181. });
  1182. it('labels an invalid grep regex separately from a generic search failure', () => {
  1183. const view = new TranscriptView(() => [
  1184. createTranscriptCell({
  1185. id: 'tool-invalid-regex',
  1186. kind: 'tool',
  1187. status: 'failed',
  1188. title: 'grep',
  1189. content: JSON.stringify({ pattern: '(?:fork: async|fork(input)' }),
  1190. detail: 'Invalid search pattern: unclosed group.',
  1191. toolErrorCode: 'invalid_regex',
  1192. createdAtMs: 1,
  1193. }),
  1194. ]);
  1195. const compact = view.render(100).map(stripVTControlCharacters);
  1196. expect(compact[0]).toBe('└ × Invalid search pattern ((?:fork: async|fork(input))');
  1197. });
  1198. it('shows live Thinking detail and fully collapses it on completion', () => {
  1199. const running = createTranscriptCell({
  1200. id: 'thinking-running',
  1201. kind: 'thinking',
  1202. status: 'running',
  1203. content: 'Inspecting the repository',
  1204. createdAtMs: 1_000,
  1205. updatedAtMs: 1_600,
  1206. });
  1207. const completed = createTranscriptCell({
  1208. ...running,
  1209. id: 'thinking-completed',
  1210. status: 'succeeded',
  1211. durationMs: 1_600,
  1212. updatedAtMs: 2_600,
  1213. });
  1214. const live = new TranscriptView(() => [running]).render(60).join('\n');
  1215. const settled = new TranscriptView(() => [completed]).render(60).join('\n');
  1216. expect(live).toContain('Thinking…');
  1217. expect(live).toContain('Inspecting the repository');
  1218. expect(settled).toContain('Thought for 1.6s');
  1219. expect(settled).not.toContain('Inspecting the repository');
  1220. });
  1221. it('keeps live Thinking to a tail preview until the user expands it', () => {
  1222. const running = createTranscriptCell({
  1223. id: 'thinking-running-preview',
  1224. kind: 'thinking',
  1225. status: 'running',
  1226. content: ['one', 'two', 'three', 'four', 'five'].join('\n'),
  1227. createdAtMs: 1,
  1228. });
  1229. const expanded = createTranscriptCell({
  1230. ...running,
  1231. id: 'thinking-running-expanded',
  1232. expanded: true,
  1233. });
  1234. const preview = new TranscriptView(() => [running]).render(60).join('\n');
  1235. expect(preview).not.toContain('one');
  1236. expect(preview).not.toContain('two');
  1237. expect(preview).toContain('2 earlier lines');
  1238. expect(preview).toContain('three');
  1239. expect(preview).toContain('five');
  1240. const fullView = new TranscriptView(() => [expanded]);
  1241. fullView.toggleDetailMode();
  1242. const full = fullView.render(60).join('\n');
  1243. expect(full).toContain('one');
  1244. expect(full).toContain('five');
  1245. });
  1246. it('uses tool-specific bounded previews for live output and errors', () => {
  1247. const detail = Array.from({ length: 14 }, (_, index) => `output-${index + 1}`).join('\n');
  1248. const running = createTranscriptCell({
  1249. id: 'tool-bash-running-preview',
  1250. kind: 'tool',
  1251. status: 'running',
  1252. title: 'bash',
  1253. content: 'pnpm test',
  1254. detail,
  1255. createdAtMs: 1,
  1256. });
  1257. const failed = createTranscriptCell({
  1258. ...running,
  1259. id: 'tool-bash-failed-preview',
  1260. status: 'failed',
  1261. });
  1262. const live = new TranscriptView(() => [running], {
  1263. displayModes: previewDisplayModes,
  1264. })
  1265. .render(80)
  1266. .join('\n');
  1267. expect(live).not.toContain('output-1\n');
  1268. expect(live).toContain('11 earlier lines');
  1269. expect(live).toContain('output-14');
  1270. const error = new TranscriptView(() => [failed], {
  1271. displayModes: previewDisplayModes,
  1272. })
  1273. .render(80)
  1274. .join('\n');
  1275. expect(error).toContain('output-14');
  1276. expect(error).not.toContain('output-1\n');
  1277. });
  1278. it('bounds previews by rendered rows when one physical line wraps', () => {
  1279. const view = new TranscriptView(
  1280. () => [
  1281. createTranscriptCell({
  1282. id: 'tool-read-wrapped-preview',
  1283. kind: 'tool',
  1284. status: 'running',
  1285. title: 'read',
  1286. content: 'README.md',
  1287. detail: Array.from({ length: 80 }, (_, index) => `word-${index + 1}`).join(' '),
  1288. createdAtMs: 1,
  1289. }),
  1290. ],
  1291. { displayModes: previewDisplayModes },
  1292. );
  1293. const detailLines = view.render(30).slice(1);
  1294. expect(detailLines.length).toBeLessThanOrEqual(4);
  1295. expect(detailLines.join('\n')).toContain('earlier lines');
  1296. expect(detailLines.join('\n')).toContain('word-80');
  1297. });
  1298. it('hides a running tool result until the user requests details', () => {
  1299. const running = createTranscriptCell({
  1300. id: 'tool-running',
  1301. kind: 'tool',
  1302. status: 'running',
  1303. title: 'bash',
  1304. content: 'pnpm test',
  1305. detail: 'streaming output',
  1306. createdAtMs: 1,
  1307. });
  1308. const view = new TranscriptView(() => [running]);
  1309. expect(view.render(60).join('\n')).not.toContain('streaming output');
  1310. view.toggleDetailMode();
  1311. expect(view.render(60).join('\n')).toContain('streaming output');
  1312. });
  1313. it('keeps completed Thinking collapsed while live Thinking remains visible', () => {
  1314. const completed = createTranscriptCell({
  1315. id: 'thinking-completed',
  1316. kind: 'thinking',
  1317. status: 'succeeded',
  1318. content: ['First decision', 'Second decision', 'Third decision', 'Final detail'].join('\n'),
  1319. createdAtMs: 1,
  1320. });
  1321. const running = createTranscriptCell({
  1322. id: 'thinking-running',
  1323. kind: 'thinking',
  1324. status: 'running',
  1325. content: 'Inspecting the code',
  1326. createdAtMs: 2,
  1327. });
  1328. const completedView = new TranscriptView(() => [completed]);
  1329. const compact = completedView.render(40).join('\n');
  1330. expect(compact).toContain('Thought');
  1331. expect(compact).not.toContain('First decision');
  1332. expect(compact).not.toContain('Final detail');
  1333. expect(new TranscriptView(() => [running]).render(40).join('\n')).toContain(
  1334. 'Inspecting the code',
  1335. );
  1336. completedView.toggleDetailMode();
  1337. expect(completedView.render(40).join('\n')).toContain('First decision');
  1338. expect(completedView.render(40).join('\n')).toContain('Final detail');
  1339. });
  1340. it('extracts the useful target from structured tool arguments', () => {
  1341. const view = new TranscriptView(() => [
  1342. createTranscriptCell({
  1343. id: 'tool-structured',
  1344. kind: 'tool',
  1345. status: 'running',
  1346. title: 'read',
  1347. content: JSON.stringify({ path: 'src/cart/price.ts', offset: 120 }),
  1348. createdAtMs: 1,
  1349. }),
  1350. ]);
  1351. const rendered = view.render(50).join('\n');
  1352. expect(rendered).toContain('Reading (src/cart/price.ts)');
  1353. expect(rendered).not.toContain('{"path"');
  1354. });
  1355. it('collapses grouped read targets in compact mode and reveals them in detailed mode', () => {
  1356. const view = new TranscriptView(() => [
  1357. createTranscriptCell({
  1358. id: 'thinking-1',
  1359. kind: 'thinking',
  1360. status: 'running',
  1361. content: 'Inspecting the implementation',
  1362. createdAtMs: 1,
  1363. turnId: 'turn-1',
  1364. }),
  1365. createTranscriptCell({
  1366. id: 'read-1',
  1367. kind: 'tool',
  1368. status: 'succeeded',
  1369. title: 'read',
  1370. content: JSON.stringify({ path: 'src/app.ts' }),
  1371. createdAtMs: 2,
  1372. turnId: 'turn-1',
  1373. }),
  1374. createTranscriptCell({
  1375. id: 'read-2',
  1376. kind: 'tool',
  1377. status: 'running',
  1378. title: 'read',
  1379. content: JSON.stringify({ path: 'src/runtime.ts' }),
  1380. createdAtMs: 3,
  1381. turnId: 'turn-1',
  1382. }),
  1383. createTranscriptCell({
  1384. id: 'read-3',
  1385. kind: 'tool',
  1386. status: 'failed',
  1387. title: 'read',
  1388. content: JSON.stringify({ path: 'src/missing.ts' }),
  1389. createdAtMs: 4,
  1390. turnId: 'turn-1',
  1391. }),
  1392. createTranscriptCell({
  1393. id: 'read-4',
  1394. kind: 'tool',
  1395. status: 'succeeded',
  1396. title: 'read',
  1397. content: JSON.stringify({ path: 'src/app.ts' }),
  1398. createdAtMs: 5,
  1399. turnId: 'turn-1',
  1400. }),
  1401. createTranscriptCell({
  1402. id: 'bash-1',
  1403. kind: 'tool',
  1404. status: 'running',
  1405. title: 'bash',
  1406. content: JSON.stringify({ command: 'pnpm test' }),
  1407. createdAtMs: 6,
  1408. turnId: 'turn-1',
  1409. }),
  1410. ]);
  1411. const compact = view.render(64).join('\n');
  1412. expect(compact).toContain('Reading 3 files');
  1413. expect(compact).toContain('1 failed');
  1414. expect(compact).not.toContain('src/app.ts');
  1415. expect(compact).not.toContain('src/runtime.ts');
  1416. expect(compact).not.toContain('src/missing.ts');
  1417. expect(compact).toContain('└ • Running');
  1418. expect(compact).toContain('pnpm test');
  1419. view.toggleDetailMode();
  1420. const detailed = view.render(64).join('\n');
  1421. expect(detailed).toContain('src/app.ts');
  1422. expect(detailed).toContain('src/runtime.ts');
  1423. expect(detailed).toContain('reading');
  1424. expect(detailed).toContain('src/missing.ts');
  1425. expect(detailed).toContain('failed');
  1426. expect(detailed).toContain('$ pnpm test');
  1427. expect(detailed.match(/src\/app\.ts/gu)).toHaveLength(2);
  1428. expect(compact).toContain('4 calls');
  1429. expect(detailed.match(/\bRead {2}src\//gu)).toBeNull();
  1430. });
  1431. it('keeps read groups turn-scoped and preserves expanded details', () => {
  1432. const view = new TranscriptView(() => [
  1433. createTranscriptCell({
  1434. id: 'read-1',
  1435. kind: 'tool',
  1436. status: 'succeeded',
  1437. title: 'read',
  1438. content: 'src/a.ts',
  1439. detail: 'const a = 1;',
  1440. expanded: true,
  1441. createdAtMs: 1,
  1442. turnId: 'turn-1',
  1443. }),
  1444. createTranscriptCell({
  1445. id: 'read-2',
  1446. kind: 'tool',
  1447. status: 'succeeded',
  1448. title: 'read',
  1449. content: 'src/b.ts',
  1450. createdAtMs: 2,
  1451. turnId: 'turn-1',
  1452. }),
  1453. createTranscriptCell({
  1454. id: 'read-3',
  1455. kind: 'tool',
  1456. status: 'succeeded',
  1457. title: 'read',
  1458. content: 'src/c.ts',
  1459. createdAtMs: 3,
  1460. turnId: 'turn-2',
  1461. }),
  1462. ]);
  1463. const compact = view.render(50).join('\n');
  1464. expect(compact).toContain('Read 2 files');
  1465. expect(compact).not.toContain('const a = 1;');
  1466. expect(compact).toContain('Read (src/c.ts)');
  1467. view.toggleDetailMode();
  1468. expect(view.render(50).join('\n')).toContain('const a = 1;');
  1469. });
  1470. it('labels a single image read as Read Image', () => {
  1471. const view = new TranscriptView(() => [
  1472. createTranscriptCell({
  1473. id: 'read-image',
  1474. kind: 'tool',
  1475. status: 'succeeded',
  1476. title: 'read',
  1477. content: JSON.stringify({ path: 'figures/reference_overview.png' }),
  1478. detail: 'Read image file [image/png]',
  1479. createdAtMs: 1,
  1480. }),
  1481. ]);
  1482. const rendered = stripVTControlCharacters(view.render(80).join('\n'));
  1483. expect(rendered).toContain('Read Image (figures/reference_overview.png)');
  1484. });
  1485. it('collapses grouped search and path targets while preserving the concrete action', () => {
  1486. const view = new TranscriptView(() => [
  1487. createTranscriptCell({
  1488. id: 'search-1',
  1489. kind: 'tool',
  1490. status: 'succeeded',
  1491. title: 'grep',
  1492. content: JSON.stringify({ query: 'watchStream' }),
  1493. createdAtMs: 1,
  1494. turnId: 'turn-1',
  1495. }),
  1496. createTranscriptCell({
  1497. id: 'list-1',
  1498. kind: 'tool',
  1499. status: 'succeeded',
  1500. title: 'glob',
  1501. content: JSON.stringify({ pattern: '**/*.ts' }),
  1502. createdAtMs: 2,
  1503. turnId: 'turn-1',
  1504. }),
  1505. ]);
  1506. const compact = view.render(64).join('\n');
  1507. expect(compact).toContain('Explored 2 operations');
  1508. expect(compact).not.toContain('watchStream');
  1509. expect(compact).not.toContain('**/*.ts');
  1510. expect(compact).not.toContain('searching');
  1511. expect(compact).not.toContain('listing');
  1512. view.toggleDetailMode();
  1513. const detailed = view.render(64).join('\n');
  1514. expect(detailed).toContain('watchStream');
  1515. expect(detailed).toContain('**/*.ts');
  1516. });
  1517. it('keeps unanswered prompts in the picker and shows compact answer progress and receipts', () => {
  1518. const view = new TranscriptView(() => [
  1519. createTranscriptCell({
  1520. id: 'permission-1',
  1521. kind: 'permission',
  1522. status: 'blocked',
  1523. title: 'bash',
  1524. content: 'Run the unit tests',
  1525. createdAtMs: 1,
  1526. }),
  1527. createTranscriptCell({
  1528. id: 'permission-2',
  1529. kind: 'permission',
  1530. status: 'resolved',
  1531. title: 'write_file',
  1532. content: 'Update the changelog',
  1533. detail: 'Allowed for this conversation · write_file',
  1534. createdAtMs: 2,
  1535. }),
  1536. createTranscriptCell({
  1537. id: 'question-1',
  1538. kind: 'question',
  1539. status: 'blocked',
  1540. title: 'Input required',
  1541. content: '',
  1542. createdAtMs: 3,
  1543. }),
  1544. createTranscriptCell({
  1545. id: 'question-2',
  1546. kind: 'question',
  1547. status: 'blocked',
  1548. title: 'Release setup',
  1549. content: 'Release channel Stable',
  1550. detail: '1 of 2 answered',
  1551. createdAtMs: 4,
  1552. }),
  1553. createTranscriptCell({
  1554. id: 'question-3',
  1555. kind: 'question',
  1556. status: 'pending',
  1557. title: 'Answers sent',
  1558. content: 'Release channel Stable\nCollaboration Mixed mode',
  1559. detail: 'Sending your answer…',
  1560. createdAtMs: 5,
  1561. }),
  1562. createTranscriptCell({
  1563. id: 'question-4',
  1564. kind: 'question',
  1565. status: 'resolved',
  1566. title: 'Answers sent',
  1567. content: 'Release channel Stable\nCollaboration Mixed mode',
  1568. detail: 'MCode is continuing…',
  1569. createdAtMs: 6,
  1570. }),
  1571. ]);
  1572. const rendered = view.render(60).join('\n');
  1573. expect(rendered).not.toContain('Approval needed');
  1574. expect(rendered).not.toContain('Run the unit tests');
  1575. expect(rendered).toContain('✓ Allowed for this conversation · write_file');
  1576. expect(rendered).not.toContain('Update the changelog');
  1577. expect(rendered).not.toContain('decision panel');
  1578. expect(rendered).toContain('◆ Answers so far');
  1579. expect(rendered).toContain('1 of 2 answered');
  1580. expect(rendered).toContain('● Sending answers…');
  1581. expect(rendered).toContain('✓ Answers sent');
  1582. expect(rendered).toContain('Release channel Stable');
  1583. expect(rendered).toContain('Collaboration Mixed mode');
  1584. expect(rendered).toContain('MCode is continuing');
  1585. expect(rendered).not.toContain('/allow');
  1586. expect(rendered).not.toContain('/q1');
  1587. });
  1588. it('uses a denied lifecycle marker for resolved permission receipts', () => {
  1589. const view = new TranscriptView(() => [
  1590. createTranscriptCell({
  1591. id: 'permission-denied',
  1592. kind: 'permission',
  1593. status: 'resolved',
  1594. title: 'bash',
  1595. content: 'Run a command',
  1596. detail: 'Denied · bash',
  1597. createdAtMs: 1,
  1598. }),
  1599. ]);
  1600. expect(view.render(48).join('\n')).toContain('× Denied · bash');
  1601. });
  1602. it('uses readable action labels and never exposes generic JSON as a tool summary', () => {
  1603. const view = new TranscriptView(() => [
  1604. createTranscriptCell({
  1605. id: 'memory-1',
  1606. kind: 'tool',
  1607. status: 'succeeded',
  1608. title: 'Memory',
  1609. content: JSON.stringify({
  1610. target: 'user',
  1611. operation: 'append',
  1612. content: 'large private implementation payload',
  1613. }),
  1614. createdAtMs: 1,
  1615. }),
  1616. createTranscriptCell({
  1617. id: 'write-1',
  1618. kind: 'tool',
  1619. status: 'succeeded',
  1620. title: 'write_file',
  1621. content: JSON.stringify({ file_path: 'src/app.ts', contents: '...' }),
  1622. createdAtMs: 2,
  1623. }),
  1624. createTranscriptCell({
  1625. id: 'task-1',
  1626. kind: 'tool',
  1627. status: 'running',
  1628. title: 'task',
  1629. content: JSON.stringify({
  1630. agent_name: 'verifier',
  1631. description: 'Review recent CLI changes',
  1632. }),
  1633. createdAtMs: 3,
  1634. }),
  1635. ]);
  1636. const rendered = view.render(80).join('\n');
  1637. expect(rendered).toContain('Updated memory (user · append)');
  1638. expect(rendered).toContain('Wrote (src/app.ts)');
  1639. expect(rendered).not.toContain('large private implementation payload');
  1640. expect(rendered).not.toContain('{"target"');
  1641. expect(rendered).not.toContain('Write_file');
  1642. expect(rendered).toContain('Delegating (verifier · Review recent CLI changes)');
  1643. });
  1644. it('uses blue for Ask User, gold for permission confirmation, and green after resolution', () => {
  1645. expect(resolveTuiDecisionColor('question', false)).toBe(tuiColors.signal);
  1646. expect(resolveTuiDecisionColor('permission', false)).toBe(tuiColors.warning);
  1647. expect(resolveTuiDecisionColor('question', true)).toBe(tuiColors.success);
  1648. });
  1649. it('keeps every rail row inside a narrow terminal', () => {
  1650. const view = new TranscriptView(() => [
  1651. createTranscriptCell({
  1652. id: 'user-narrow',
  1653. kind: 'user',
  1654. status: 'succeeded',
  1655. content: 'Inspect this deliberately long request before changing anything.',
  1656. createdAtMs: 1,
  1657. }),
  1658. createTranscriptCell({
  1659. id: 'tool-narrow',
  1660. kind: 'tool',
  1661. status: 'running',
  1662. title: 'read',
  1663. content: 'src/a-deliberately-long-file-name.ts',
  1664. detail: 'Waiting for a deliberately long result from the runtime.',
  1665. createdAtMs: 2,
  1666. }),
  1667. ]);
  1668. const lines = view.render(28);
  1669. expect(lines.every((line) => visibleWidth(line) <= 28)).toBe(true);
  1670. expect(lines.some((line) => line.includes('Read') && line.includes('…'))).toBe(true);
  1671. });
  1672. it('keeps a stable turn-window anchor as new cells append', () => {
  1673. let cells = Array.from({ length: 80 }, (_, index) =>
  1674. createTranscriptCell({
  1675. id: `cell-${index + 1}`,
  1676. turnId: `turn-${Math.floor(index / 2) + 1}`,
  1677. kind: 'final-summary',
  1678. status: 'succeeded',
  1679. content: `message ${index + 1}`,
  1680. createdAtMs: index + 1,
  1681. }),
  1682. );
  1683. const view = new TranscriptView(() => cells, { maxInitialTurns: 10 });
  1684. const first = view.render(80).join('\n');
  1685. cells = [
  1686. ...cells,
  1687. createTranscriptCell({
  1688. id: 'cell-81',
  1689. turnId: 'turn-41',
  1690. kind: 'final-summary',
  1691. status: 'succeeded',
  1692. content: 'message 81',
  1693. createdAtMs: 81,
  1694. }),
  1695. ];
  1696. const appended = view.render(80).join('\n');
  1697. expect(first).toContain('30 earlier turns omitted');
  1698. expect(first).toContain('message 61');
  1699. expect(first).not.toContain('message 60');
  1700. expect(appended).toContain('message 61');
  1701. expect(appended).toContain('message 81');
  1702. });
  1703. it('folds the oldest cells inside one oversized turn while preserving its request and tail', () => {
  1704. const cells = [
  1705. createTranscriptCell({
  1706. id: 'user-1',
  1707. turnId: 'turn-1',
  1708. kind: 'user',
  1709. status: 'succeeded',
  1710. content: 'Inspect the repository',
  1711. createdAtMs: 1,
  1712. }),
  1713. ...Array.from({ length: 200 }, (_, index) =>
  1714. createTranscriptCell({
  1715. id: `tool-${index + 1}`,
  1716. turnId: 'turn-1',
  1717. kind: 'tool',
  1718. status: 'succeeded',
  1719. title: 'read',
  1720. content: `src/file-${index + 1}.ts`,
  1721. createdAtMs: index + 2,
  1722. }),
  1723. ),
  1724. createTranscriptCell({
  1725. id: 'assistant-final',
  1726. turnId: 'turn-1',
  1727. kind: 'assistant',
  1728. status: 'succeeded',
  1729. content: 'Final conclusion',
  1730. createdAtMs: 202,
  1731. }),
  1732. ];
  1733. const view = new TranscriptView(() => cells, { maxCellsPerTurn: 40 });
  1734. const rendered = view.render(80).join('\n');
  1735. expect(rendered).toContain('Inspect the repository');
  1736. expect(rendered).toContain('earlier steps folded');
  1737. expect(rendered).toContain('Final conclusion');
  1738. expect(view.getPerformanceSnapshot()).toMatchObject({
  1739. sourceUnits: 202,
  1740. projectedCells: 41,
  1741. foldedCells: 162,
  1742. });
  1743. });
  1744. it('renders Todo progress as a compact read-only checklist', () => {
  1745. const view = new TranscriptView(() => [
  1746. createTranscriptCell({
  1747. id: 'todo-turn-1',
  1748. kind: 'todo',
  1749. status: 'running',
  1750. content: '',
  1751. todoItems: [
  1752. { content: 'Inspect competitors', status: 'completed' },
  1753. { content: 'Add Todo display', status: 'in_progress' },
  1754. { content: 'Run focused tests', status: 'pending' },
  1755. { content: 'Discard obsolete draft', status: 'cancelled' },
  1756. ],
  1757. createdAtMs: 1,
  1758. }),
  1759. ]);
  1760. const rendered = stripVTControlCharacters(view.render(48).join('\n'));
  1761. expect(rendered).toContain('Todo list 1/4 · 1 cancelled');
  1762. expect(rendered).toContain('✓ Inspect competitors');
  1763. expect(rendered).toContain('● Add Todo display');
  1764. expect(rendered).toContain('○ Run focused tests');
  1765. expect(rendered).toContain('× Discard obsolete draft');
  1766. expect(rendered).not.toContain('todowrite');
  1767. });
  1768. it('wraps long Todo items within a narrow terminal', () => {
  1769. const view = new TranscriptView(() => [
  1770. createTranscriptCell({
  1771. id: 'todo-turn-1',
  1772. kind: 'todo',
  1773. status: 'running',
  1774. content: '',
  1775. todoItems: [
  1776. {
  1777. content: 'Implement a deliberately long task description that must wrap',
  1778. status: 'in_progress',
  1779. },
  1780. ],
  1781. createdAtMs: 1,
  1782. }),
  1783. ]);
  1784. const lines = view.render(24);
  1785. expect(stripVTControlCharacters(lines.join('\n'))).toContain('Todo list 0/1');
  1786. expect(lines.length).toBeGreaterThan(2);
  1787. expect(lines.every((line) => visibleWidth(line) <= 24)).toBe(true);
  1788. });
  1789. });