render.spec.ts 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { describe, expect, it } from 'vitest'
  2. import { PtySessionId } from '@deepseek-ai/dsh-pty'
  3. import { boundTerminalText, renderList, renderRead, renderSend, renderSendRead, renderSpawn } from '@deepseek-ai/dsh-tool-pty/src/render.ts'
  4. describe('tool-pty rendering', () => {
  5. it('renders spawn with and without names or MOTD', () => {
  6. expect(renderSpawn({ sessionId: PtySessionId('pty-1'), type: 'shell', status: { kind: 'running' }, motd: '' }, 1024))
  7. .toBe('started terminal session pty-1 [type: shell]\n(no startup output)')
  8. expect(renderSpawn({ sessionId: PtySessionId('pty-2'), name: 'main', type: 'shell', pid: 2, status: { kind: 'running' }, motd: 'ready' }, 1024))
  9. .toContain('pty-2 (main)')
  10. })
  11. it('renders running, exited, empty, and truncated sends', () => {
  12. expect(renderSend({ viewport: '', waitReason: 'timeout', sessionStatus: { kind: 'running' }, truncated: true }, 1024))
  13. .toBe('(no new output)\n[wait: timeout]\n[session: running]\n[output truncated]')
  14. expect(renderSend({ viewport: 'bye', waitReason: 'session_exit', sessionStatus: { kind: 'exited', exitCode: null, signal: 'SIGTERM' }, truncated: false }, 1024))
  15. .toContain('exited code=null signal=SIGTERM')
  16. expect(renderSend({ viewport: 'bye', waitReason: 'session_exit', sessionStatus: { kind: 'exited', exitCode: 2, signal: null }, truncated: false }, 1024))
  17. .toContain('exited code=2 signal=null')
  18. expect(renderSend({ viewport: 'bye', waitReason: 'session_exit', sessionStatus: { kind: 'exited', exitCode: null, signal: null }, truncated: false }, 1024))
  19. .toContain('exited code=null signal=null')
  20. expect(renderSendRead({ delta: '', truncated: true })).toBe('[output truncated]')
  21. expect(renderSendRead({ delta: 'x', truncated: true })).toBe('x\n[output truncated]')
  22. expect(renderSendRead({ delta: 'x\n', truncated: true })).toBe('x\n[output truncated]')
  23. expect(renderSendRead({ delta: 'x', truncated: false })).toBe('x')
  24. })
  25. it('renders history and every list status shape', () => {
  26. expect(renderRead({ text: '', totalLines: 0, lineBegin: 0, lineEnd: 0, truncated: true }, 1024))
  27. .toBe('(no retained output)\n[lines: 0-0 of 0]\n[output truncated]')
  28. expect(renderList([], 1024)).toBe('(no terminal sessions)')
  29. expect(renderList([
  30. { sessionId: PtySessionId('pty-1'), type: 'shell', status: { kind: 'running' } },
  31. { sessionId: PtySessionId('pty-2'), name: 'done', type: 'shell', pid: 9, status: { kind: 'exited', exitCode: 2, signal: null } },
  32. { sessionId: PtySessionId('pty-3'), type: 'shell', status: { kind: 'exited', exitCode: null, signal: 'SIGTERM' } },
  33. { sessionId: PtySessionId('pty-4'), type: 'shell', status: { kind: 'exited', exitCode: null, signal: null } },
  34. ], 1024)).toBe('pty-1 [shell] running\npty-2 (done) [shell] exited code=2 signal=null pid=9\npty-3 [shell] exited code=null signal=SIGTERM\npty-4 [shell] exited code=null signal=null')
  35. })
  36. it('bounds complete UTF-8 results while retaining terminal metadata when it fits', () => {
  37. const send = renderSend({
  38. viewport: `prefix-${'界'.repeat(40)}`,
  39. waitReason: 'stdin_read',
  40. sessionStatus: { kind: 'running' },
  41. truncated: false,
  42. }, 64)
  43. expect(Buffer.byteLength(send)).toBeLessThanOrEqual(64)
  44. expect(send).toContain('[wait: stdin_read]')
  45. expect(send).toContain('[output truncated]')
  46. const read = renderRead({
  47. text: 'x'.repeat(200), totalLines: 20, lineBegin: 0, lineEnd: 10, truncated: false,
  48. }, 48)
  49. expect(Buffer.byteLength(read)).toBeLessThanOrEqual(48)
  50. expect(read).toContain('[lines: 0-10 of 20]')
  51. expect(Buffer.byteLength(renderSpawn({
  52. sessionId: PtySessionId('pty-1'), type: 'shell', status: { kind: 'running' }, motd: 'x'.repeat(200),
  53. }, 32))).toBeLessThanOrEqual(32)
  54. const boundedSpawn = renderSpawn({
  55. sessionId: PtySessionId('pty-1'), type: 'shell', status: { kind: 'running' }, motd: 'x'.repeat(200),
  56. }, 96)
  57. expect(boundedSpawn).toContain('started terminal session pty-1')
  58. expect(boundedSpawn).toContain('[output truncated]')
  59. expect(Buffer.byteLength(renderSend({
  60. viewport: 'x'.repeat(200), waitReason: 'stdin_read', sessionStatus: { kind: 'running' }, truncated: false,
  61. }, 8))).toBeLessThanOrEqual(8)
  62. expect(boundTerminalText('x'.repeat(200), 8)).toHaveLength(8)
  63. expect(boundTerminalText('x'.repeat(200), 32).endsWith('[output truncated]')).toBe(true)
  64. })
  65. })