1+ /**
2+ * @license
3+ * Copyright 2025 Autohand AI LLC
4+ * SPDX-License-Identifier: Apache-2.0
5+ */
6+
7+ import { describe , it , expect } from 'bun:test' ;
8+ import React from 'react' ;
9+ import { render } from 'ink-testing-library' ;
10+ import { UserMessage } from '../../src/ui/ink/UserMessage.js' ;
11+ import { ThemeProvider } from '../../src/ui/theme/ThemeContext.js' ;
12+ import { I18nProvider } from '../../src/ui/i18n/index.js' ;
13+
14+ function renderWithProviders ( element : React . ReactElement ) {
15+ return render (
16+ < I18nProvider >
17+ < ThemeProvider >
18+ { element }
19+ </ ThemeProvider >
20+ </ I18nProvider >
21+ ) ;
22+ }
23+
24+ describe ( 'UserMessage' , ( ) => {
25+ describe ( 'normal messages' , ( ) => {
26+ it ( 'renders short messages with full background' , ( ) => {
27+ const { lastFrame } = renderWithProviders ( < UserMessage > Hello world</ UserMessage > ) ;
28+ const output = lastFrame ( ) ;
29+ expect ( output ) . toContain ( 'Hello world' ) ;
30+ } ) ;
31+
32+ it ( 'renders queued messages with prefix' , ( ) => {
33+ const { lastFrame } = renderWithProviders ( < UserMessage isQueued > Test message</ UserMessage > ) ;
34+ const output = lastFrame ( ) ;
35+ expect ( output ) . toContain ( '(queued)' ) ;
36+ expect ( output ) . toContain ( 'Test message' ) ;
37+ } ) ;
38+ } ) ;
39+
40+ describe ( 'large text handling' , ( ) => {
41+ it ( 'collapses text with more than 15 lines' , ( ) => {
42+ const largeText = Array ( 20 ) . fill ( 'Line of text' ) . join ( '\n' ) ;
43+ const { lastFrame } = renderWithProviders ( < UserMessage > { largeText } </ UserMessage > ) ;
44+ const output = lastFrame ( ) ;
45+
46+ // Should show compact box, not all lines
47+ expect ( output ) . toContain ( 'Text' ) ;
48+ expect ( output ) . toContain ( '20 lines' ) ;
49+ expect ( output ) . toContain ( 'collapsed for readability' ) ;
50+ } ) ;
51+
52+ it ( 'collapses text with more than 1500 characters' , ( ) => {
53+ const largeText = 'x' . repeat ( 2000 ) ;
54+ const { lastFrame } = renderWithProviders ( < UserMessage > { largeText } </ UserMessage > ) ;
55+ const output = lastFrame ( ) ;
56+
57+ // Should show compact box
58+ expect ( output ) . toContain ( 'Text' ) ;
59+ expect ( output ) . toContain ( 'collapsed for readability' ) ;
60+ } ) ;
61+
62+ it ( 'detects code blocks' , ( ) => {
63+ const codeBlock = '```javascript\n' + Array ( 20 ) . fill ( 'const x = 1;' ) . join ( '\n' ) + '\n```' ;
64+ const { lastFrame } = renderWithProviders ( < UserMessage > { codeBlock } </ UserMessage > ) ;
65+ const output = lastFrame ( ) ;
66+
67+ expect ( output ) . toContain ( 'Code block' ) ;
68+ } ) ;
69+
70+ it ( 'detects JSON content' , ( ) => {
71+ const json = JSON . stringify ( { data : Array ( 50 ) . fill ( { key : 'value' } ) } , null , 2 ) ;
72+ const { lastFrame } = renderWithProviders ( < UserMessage > { json } </ UserMessage > ) ;
73+ const output = lastFrame ( ) ;
74+
75+ expect ( output ) . toContain ( 'JSON' ) ;
76+ } ) ;
77+
78+ it ( 'detects stack traces' , ( ) => {
79+ const stackTrace = `Error: Something went wrong
80+ at Function.execute (file.js:10:15)
81+ at Object.<anonymous> (file.js:20:5)
82+ at Module._compile (module.js:653:30)
83+ ${ Array ( 15 ) . fill ( ' at someFunction (another.js:5:10)' ) . join ( '\n' ) } ` ;
84+
85+ const { lastFrame } = renderWithProviders ( < UserMessage > { stackTrace } </ UserMessage > ) ;
86+ const output = lastFrame ( ) ;
87+
88+ expect ( output ) . toContain ( 'Stack trace' ) ;
89+ } ) ;
90+
91+ it ( 'detects log output' , ( ) => {
92+ const logs = Array ( 20 ) . fill ( '[2024-01-15 10:30:45] [INFO] Processing request' ) . join ( '\n' ) ;
93+ const { lastFrame } = renderWithProviders ( < UserMessage > { logs } </ UserMessage > ) ;
94+ const output = lastFrame ( ) ;
95+
96+ expect ( output ) . toContain ( 'Log output' ) ;
97+ } ) ;
98+
99+ it ( 'detects diff/patch content' , ( ) => {
100+ const diff = `diff --git a/file.ts b/file.ts
101+ --- a/file.ts
102+ +++ b/file.ts
103+ @@ -1,5 +1,5 @@
104+ ${ Array ( 20 ) . fill ( '+ new line' ) . join ( '\n' ) } `;
105+
106+ const { lastFrame } = renderWithProviders ( < UserMessage > { diff } </ UserMessage > ) ;
107+ const output = lastFrame ( ) ;
108+
109+ expect ( output ) . toContain ( 'Diff' ) ;
110+ } ) ;
111+
112+ it ( 'shows byte size for large content' , ( ) => {
113+ const largeText = 'x' . repeat ( 5000 ) ;
114+ const { lastFrame } = renderWithProviders ( < UserMessage > { largeText } </ UserMessage > ) ;
115+ const output = lastFrame ( ) ;
116+
117+ expect ( output ) . toContain ( 'KB' ) ;
118+ } ) ;
119+
120+ it ( 'shows queued indicator in collapsed view' , ( ) => {
121+ const largeText = Array ( 20 ) . fill ( 'Line of text' ) . join ( '\n' ) ;
122+ const { lastFrame } = renderWithProviders ( < UserMessage isQueued > { largeText } </ UserMessage > ) ;
123+ const output = lastFrame ( ) ;
124+
125+ expect ( output ) . toContain ( '(queued)' ) ;
126+ } ) ;
127+ } ) ;
128+
129+ describe ( 'truncation for medium messages' , ( ) => {
130+ it ( 'truncates messages between 5 and 15 lines with ellipsis' , ( ) => {
131+ const mediumText = Array ( 10 ) . fill ( 'Line of text here' ) . join ( '\n' ) ;
132+ const { lastFrame } = renderWithProviders ( < UserMessage > { mediumText } </ UserMessage > ) ;
133+ const output = lastFrame ( ) ;
134+
135+ // Should show truncated with ...
136+ expect ( output ) . toContain ( '...' ) ;
137+ } ) ;
138+ } ) ;
139+ } ) ;
0 commit comments