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