@@ -37,6 +37,16 @@ describe("classifyAttachmentBytes", () => {
3737 buffer : completeUtf8 . subarray ( 0 , 4096 ) ,
3838 expectedClass : "binary" ,
3939 } ,
40+ {
41+ name : "an invalid continuation after the sniff boundary" ,
42+ buffer : Buffer . concat ( [ completeUtf8 . subarray ( 0 , 4095 ) , Buffer . from ( [ 0xe2 , 0x28 ] ) ] ) ,
43+ expectedClass : "binary" ,
44+ } ,
45+ {
46+ name : "an incomplete sequence at the actual 4,097-byte EOF" ,
47+ buffer : Buffer . concat ( [ completeUtf8 . subarray ( 0 , 4095 ) , Buffer . from ( [ 0xe2 , 0x82 ] ) ] ) ,
48+ expectedClass : "binary" ,
49+ } ,
4050 {
4151 name : "an invalid byte before the sniff boundary" ,
4252 buffer : Buffer . concat ( [
@@ -54,6 +64,62 @@ describe("classifyAttachmentBytes", () => {
5464 } ) ;
5565 } ) ;
5666
67+ it . each ( [
68+ { name : "two-byte sequence" , prefixLength : 4095 , bytes : [ 0xc2 , 0xa3 ] , expectedClass : "text" } ,
69+ {
70+ name : "three-byte sequence" ,
71+ prefixLength : 4095 ,
72+ bytes : [ 0xe2 , 0x82 , 0xac ] ,
73+ expectedClass : "text" ,
74+ } ,
75+ {
76+ name : "four-byte sequence after its first byte" ,
77+ prefixLength : 4095 ,
78+ bytes : [ 0xf0 , 0x9f , 0xa6 , 0x80 ] ,
79+ expectedClass : "text" ,
80+ } ,
81+ {
82+ name : "four-byte sequence after its second byte" ,
83+ prefixLength : 4094 ,
84+ bytes : [ 0xf0 , 0x9f , 0xa6 , 0x80 ] ,
85+ expectedClass : "text" ,
86+ } ,
87+ {
88+ name : "four-byte sequence after its third byte" ,
89+ prefixLength : 4093 ,
90+ bytes : [ 0xf0 , 0x9f , 0xa6 , 0x80 ] ,
91+ expectedClass : "text" ,
92+ } ,
93+ {
94+ name : "overlong sequence crossing the boundary" ,
95+ prefixLength : 4095 ,
96+ bytes : [ 0xe0 , 0x80 , 0x80 ] ,
97+ expectedClass : "binary" ,
98+ } ,
99+ {
100+ name : "new sequence outside a complete sample" ,
101+ prefixLength : 4096 ,
102+ bytes : [ 0xf0 , 0x9f , 0xa6 , 0x80 ] ,
103+ expectedClass : "text" ,
104+ } ,
105+ {
106+ name : "invalid byte outside a complete sample" ,
107+ prefixLength : 4096 ,
108+ bytes : [ 0xff ] ,
109+ expectedClass : "text" ,
110+ } ,
111+ ] ) ( "bounds UTF-8 completion for a $name" , async ( { prefixLength, bytes, expectedClass } ) => {
112+ const buffer = Buffer . concat ( [
113+ completeUtf8 . subarray ( 0 , 4092 ) ,
114+ Buffer . alloc ( prefixLength - 4092 , 0x61 ) ,
115+ Buffer . from ( bytes ) ,
116+ ] ) ;
117+ await expect ( classifyAttachmentBytes ( { buffer, name : "notes" } ) ) . resolves . toEqual ( {
118+ mime : expectedClass === "text" ? "text/plain" : undefined ,
119+ class : expectedClass ,
120+ } ) ;
121+ } ) ;
122+
57123 it ( "infers delimited text from otherwise untyped bytes" , async ( ) => {
58124 await expect (
59125 classifyAttachmentBytes ( { buffer : Buffer . from ( "name,value\nopenclaw,1" ) , name : "data.bin" } ) ,
0 commit comments