@@ -121,6 +121,107 @@ describe("SQL result previews", () => {
121121 expect ( saved . writes ) . toHaveLength ( 0 )
122122 } )
123123
124+ test ( "quotes column separators in aliases without spilling" , async ( ) => {
125+ const saved = spill ( )
126+ const formatted = await formatSqlResult ( result ( [ [ 1 , 2 ] ] , [ "a | b" , "plain" ] ) , {
127+ limit : 100 ,
128+ write : saved . write ,
129+ } )
130+
131+ expect ( formatted . output ) . toBe ( '"a | b" | plain\n--------+------\n1 | 2\n\n(1 rows)' )
132+ expect ( formatted . metadata ) . toMatchObject ( {
133+ preview_rows : 1 ,
134+ preview_columns : 2 ,
135+ schema_truncated : false ,
136+ output_truncated : false ,
137+ } )
138+ expect ( saved . writes ) . toHaveLength ( 0 )
139+ } )
140+
141+ test ( "quotes newline and carriage return aliases without spilling" , async ( ) => {
142+ for ( const alias of [ "alpha\nbeta" , 'alpha\r"beta"\\tail' ] ) {
143+ const saved = spill ( )
144+ const formatted = await formatSqlResult ( result ( [ [ 1 ] ] , [ alias ] ) , { limit : 100 , write : saved . write } )
145+ const rendered = JSON . stringify ( alias )
146+
147+ expect ( formatted . output ) . toBe ( `${ rendered } \n${ "-" . repeat ( rendered . length ) } \n1\n\n(1 rows)` )
148+ expect ( formatted . metadata ) . toMatchObject ( {
149+ preview_rows : 1 ,
150+ preview_columns : 1 ,
151+ schema_truncated : false ,
152+ output_truncated : false ,
153+ } )
154+ expect ( saved . writes ) . toHaveLength ( 0 )
155+ }
156+ } )
157+
158+ test ( "clips aliases after accounting for JSON escaping" , async ( ) => {
159+ const alias = "\n" . repeat ( 64 )
160+ const saved = spill ( )
161+ const formatted = await formatSqlResult ( result ( [ [ 1 ] ] , [ alias ] ) , { limit : 100 , write : saved . write } )
162+
163+ expect ( formatted . output ) . toContain ( '"\\n\\n' )
164+ expect ( formatted . output ) . toContain ( "…[alias clipped]" )
165+ expect ( formatted . metadata ) . toMatchObject ( { schema_truncated : true , output_truncated : true } )
166+ expect ( Buffer . byteLength ( formatted . output , "utf8" ) ) . toBeLessThanOrEqual ( SQL_MAX_PREVIEW_BYTES )
167+ expect ( saved . rows ( ) ) . toEqual ( [ { columns : [ alias ] , values : [ 1 ] } ] )
168+ } )
169+
170+ test ( "renders distinct small Buffer cells as base64 without spilling" , async ( ) => {
171+ const saved = spill ( )
172+ const formatted = await formatSqlResult ( result ( [ [ Buffer . from ( [ 0x80 ] ) ] , [ Buffer . from ( [ 0x81 ] ) ] ] ) , {
173+ limit : 100 ,
174+ write : saved . write ,
175+ } )
176+
177+ expect ( body ( formatted . output ) ) . toBe ( "base64:gA==\nbase64:gQ==" )
178+ expect ( formatted . metadata ) . toMatchObject ( { preview_rows : 2 , output_truncated : false , truncated : false } )
179+ expect ( formatted . metadata . result_path ) . toBeUndefined ( )
180+ expect ( saved . writes ) . toHaveLength ( 0 )
181+ } )
182+
183+ test ( "renders Uint8Array views and nested binary cells as base64 without spilling" , async ( ) => {
184+ const cell = new Uint8Array ( [ 255 , 1 , 128 , 254 ] ) . subarray ( 1 , 3 )
185+ const saved = spill ( )
186+ const formatted = await formatSqlResult ( result ( [ [ cell ] , [ new Uint8Array ( ) ] , [ [ cell , Buffer . from ( [ 0x81 ] ) ] ] ] ) , {
187+ limit : 100 ,
188+ write : saved . write ,
189+ } )
190+
191+ expect ( body ( formatted . output ) ) . toBe ( "base64:AYA=\nbase64:\nbase64:AYA=,base64:gQ==" )
192+ expect ( formatted . metadata . output_truncated ) . toBe ( false )
193+ expect ( saved . writes ) . toHaveLength ( 0 )
194+ } )
195+
196+ test ( "clips base64 expansion below the binary threshold and spills complete bytes" , async ( ) => {
197+ const cell = Buffer . alloc ( SQL_MAX_PREVIEW_BYTES - 1 , 0x80 )
198+ const saved = spill ( )
199+ const formatted = await formatSqlResult ( result ( [ [ cell ] ] ) , { limit : 100 , write : saved . write } )
200+
201+ expect ( body ( formatted . output ) ) . toStartWith ( "base64:gICAgICA" )
202+ expect ( body ( formatted . output ) ) . toContain ( "…[cell clipped," )
203+ expect ( formatted . metadata . output_truncated ) . toBe ( true )
204+ expect ( Buffer . byteLength ( formatted . output , "utf8" ) ) . toBeLessThanOrEqual ( SQL_MAX_PREVIEW_BYTES )
205+ expect ( saved . rows ( ) ) . toEqual ( [ { columns : [ "value" ] , values : [ { $bytes : cell . toString ( "base64" ) } ] } ] )
206+ } )
207+
208+ test ( "renders oversized binary cells as a base64 prefix with the existing clip marker" , async ( ) => {
209+ const bytes = Buffer . alloc ( SQL_MAX_PREVIEW_BYTES * 2 , Buffer . from ( [ 0x80 , 0x81 , 0x82 ] ) )
210+ for ( const cell of [ bytes , new Uint8Array ( bytes ) ] ) {
211+ const saved = spill ( )
212+ const formatted = await formatSqlResult ( result ( [ [ cell ] ] ) , { limit : 100 , write : saved . write } )
213+ const preview = body ( formatted . output )
214+
215+ expect ( preview ) . toStartWith ( "base64:gIGCgIGC" )
216+ expect ( preview ) . toContain ( "…[cell clipped," )
217+ expect ( preview ) . not . toContain ( "\uFFFD" )
218+ expect ( bytes . toString ( "base64" ) ) . toStartWith ( preview . slice ( "base64:" . length ) . split ( "…[cell clipped," ) [ 0 ] ! )
219+ expect ( formatted . metadata . output_truncated ) . toBe ( true )
220+ expect ( Buffer . byteLength ( formatted . output , "utf8" ) ) . toBeLessThanOrEqual ( SQL_MAX_PREVIEW_BYTES )
221+ expect ( saved . rows ( ) ) . toEqual ( [ { columns : [ "value" ] , values : [ { $bytes : bytes . toString ( "base64" ) } ] } ] )
222+ }
223+ } )
224+
124225 test ( "quotes comma-bearing strings in array cells" , async ( ) => {
125226 const saved = spill ( )
126227 const formatted = await formatSqlResult ( result ( [ [ [ "x,y" , "z" ] ] ] ) , { limit : 100 , write : saved . write } )
@@ -253,7 +354,7 @@ describe("SQL result previews", () => {
253354 const saved = spill ( )
254355 const formatted = await formatSqlResult ( result ( cells . map ( ( cell ) => [ cell ] ) ) , { limit : 100 , write : saved . write } )
255356
256- expect ( body ( formatted . output ) ) . toBe ( cells . map ( String ) . join ( "\n" ) )
357+ expect ( body ( formatted . output ) ) . toBe ( [ String ( cells [ 0 ] ) , String ( cells [ 1 ] ) , "base64:AYA=" , "42" , "true" ] . join ( "\n" ) )
257358 expect ( saved . writes ) . toHaveLength ( 0 )
258359 } )
259360
0 commit comments