tui-thinking-preview.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { stripVTControlCharacters } from 'node:util';
  2. import { afterEach, describe, expect, it, vi } from 'vitest';
  3. import * as textLayout from '../../src/tui/engine/utils.js';
  4. import { TuiTranscriptPanel } from '../../src/tui/features/transcript/panel.js';
  5. import { formatTuiTranscriptMarkdown } from '../../src/tui/transcript/export.js';
  6. import { createTranscriptCell } from '../../src/tui/transcript/model.js';
  7. import { TranscriptStore } from '../../src/tui/transcript/store.js';
  8. import { TranscriptView } from '../../src/tui/transcript/view.js';
  9. import { MINIMAX_CODE_DARK_THEME, MINIMAX_CODE_LIGHT_THEME } from '../../src/tui/theme/palettes.js';
  10. import { applyTuiRenderTheme, getTuiThemeSnapshot } from '../../src/tui/theme/runtime.js';
  11. function thinkingStore(content: string, status: 'running' | 'failed' | 'succeeded' = 'running') {
  12. return new TranscriptStore([
  13. createTranscriptCell({ id: 'thinking', kind: 'thinking', status, content, createdAtMs: 1 }),
  14. ]);
  15. }
  16. describe('Thinking preview', () => {
  17. it.each(['assistant', 'thinking'] as const)(
  18. 'sanitizes %s in the expanded transcript inspector',
  19. (kind) => {
  20. const control = '\x1b]52;c;U1lOVEhFVElD\x07';
  21. const store = new TranscriptStore([
  22. createTranscriptCell({
  23. id: 'untrusted-inspector',
  24. kind,
  25. status: 'succeeded',
  26. content: `${control}\x1b[8mVisible text\x1b[0m`,
  27. createdAtMs: 1,
  28. }),
  29. ]);
  30. const panel = new TuiTranscriptPanel({
  31. source: store,
  32. onCancel: () => undefined,
  33. requestRender: () => undefined,
  34. });
  35. panel.handleInput('\u000f');
  36. const rendered = panel.render(100).join('\n');
  37. expect(rendered).not.toContain(control);
  38. expect(rendered).not.toContain('\x1b[8m');
  39. expect(stripVTControlCharacters(rendered)).toContain('Visible text');
  40. },
  41. );
  42. afterEach(() => vi.restoreAllMocks());
  43. it('bounds colored layout work while new deltas stream into the same full cell', () => {
  44. const originalTheme = getTuiThemeSnapshot();
  45. const wrap = vi.spyOn(textLayout, 'wrapTextWithAnsi');
  46. const store = thinkingStore('Original reasoning\n');
  47. const view = new TranscriptView(store);
  48. let fullContent = store.get('thinking')!.content;
  49. applyTuiRenderTheme(MINIMAX_CODE_DARK_THEME, 2);
  50. try {
  51. for (const index of [1, 2, 3]) {
  52. const delta = `${'A previous reasoning line.\n'.repeat(4_000)}Latest step ${index}`;
  53. fullContent += delta;
  54. store.queueTextDelta('thinking', delta);
  55. store.flushTextDeltas(index + 1);
  56. wrap.mockClear();
  57. const rendered = stripVTControlCharacters(view.render(1_000).join('\n'));
  58. expect(rendered).toContain(`Latest step ${index}`);
  59. expect(rendered).toContain('… earlier content');
  60. expect(rendered).not.toContain('Original reasoning');
  61. // Assert bounded input to the real layout engine, not a timing threshold.
  62. const layoutChars = wrap.mock.calls.reduce(
  63. (total, [text]) => total + stripVTControlCharacters(text).length,
  64. 0,
  65. );
  66. expect(layoutChars).toBeGreaterThan(0);
  67. expect(layoutChars).toBeLessThan(2_500);
  68. expect(store.length).toBe(1);
  69. expect(store.get('thinking')?.content).toBe(fullContent);
  70. }
  71. } finally {
  72. applyTuiRenderTheme(
  73. originalTheme.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME,
  74. originalTheme.colorLevel,
  75. );
  76. }
  77. });
  78. it.each(['running', 'failed', 'succeeded'] as const)(
  79. 'keeps the correct edge and omission hint for a long %s preview that fits in three rows',
  80. (status) => {
  81. const store = thinkingStore(`HEAD ${'x'.repeat(8_000)} TAIL`, status);
  82. const view = new TranscriptView(store, {
  83. displayModes: { revision: 0, resolveMainDisplayMode: () => 'preview' },
  84. });
  85. const lines = view.render(1_000).map(stripVTControlCharacters);
  86. const head = status === 'succeeded';
  87. expect(lines.join('\n')).toContain(head ? 'HEAD' : 'TAIL');
  88. expect(lines.join('\n')).not.toContain(head ? 'TAIL' : 'HEAD');
  89. expect(lines.join('\n')).toContain(head ? '… more content' : '… earlier content');
  90. expect(lines.join('\n')).not.toMatch(/\d+ (?:earlier|more) lines/u);
  91. expect(lines.length).toBeLessThanOrEqual(5);
  92. },
  93. );
  94. it.each(['running', 'succeeded'] as const)('keeps Unicode intact at the %s cut', (status) => {
  95. const content =
  96. status === 'running'
  97. ? `old content🙂${'中'.repeat(1_999)}`
  98. : `${'中'.repeat(1_999)}🙂new content`;
  99. const store = thinkingStore(content, status);
  100. const view = new TranscriptView(store, {
  101. displayModes: { revision: 0, resolveMainDisplayMode: () => 'preview' },
  102. });
  103. const wrap = vi.spyOn(textLayout, 'wrapTextWithAnsi');
  104. view.render(1_000);
  105. for (const [text] of wrap.mock.calls) {
  106. expect(text).not.toMatch(/[\uD800-\uDFFF]/u);
  107. }
  108. expect(store.get('thinking')?.content).toBe(content);
  109. });
  110. it('preserves full detail toggling and Transcript search/copy after previewing long thinking', async () => {
  111. const content = `Original reasoning\n${'intermediate step\n'.repeat(300)}Latest reasoning`;
  112. const store = thinkingStore(content);
  113. const view = new TranscriptView(store);
  114. const exportedBefore = formatTuiTranscriptMarkdown(store);
  115. expect(view.render(80).join('\n')).not.toContain('Original reasoning');
  116. view.toggleDetailMode();
  117. expect(view.render(80).join('\n')).toContain('Original reasoning');
  118. expect(view.render(80).join('\n')).toContain('Latest reasoning');
  119. view.toggleDetailMode();
  120. expect(view.render(80).join('\n')).not.toContain('Original reasoning');
  121. const writeClipboardText = vi.fn(async (_text: string) => undefined);
  122. const panel = new TuiTranscriptPanel({
  123. source: store,
  124. onCancel: vi.fn(),
  125. requestRender: vi.fn(),
  126. writeClipboardText,
  127. });
  128. panel.handleInput('/');
  129. panel.handleInput('Original reasoning');
  130. panel.handleInput('\r');
  131. expect(panel.render(80).join('\n')).toContain('1 match');
  132. const wrap = vi.spyOn(textLayout, 'wrapTextWithAnsi');
  133. panel.handleInput('\u000f'); // Ctrl+O expands all blocks in the inspector.
  134. panel.render(80);
  135. expect(
  136. wrap.mock.calls.some(([text]) => stripVTControlCharacters(text).includes(content)),
  137. ).toBe(true);
  138. panel.handleInput('c');
  139. await vi.waitFor(() => expect(writeClipboardText).toHaveBeenCalledOnce());
  140. expect(writeClipboardText.mock.calls[0]?.[0]).toContain(content);
  141. expect(store.get('thinking')?.content).toBe(content);
  142. // Export keeps its existing policy (thinking is not included).
  143. expect(formatTuiTranscriptMarkdown(store)).toBe(exportedBefore);
  144. });
  145. });