tui-thinking-preview.test.ts 5.8 KB

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