food-reader.test.ts (12083B)
1 import { describe, expect, it } from 'vitest'; 2 import { markerPartition } from '../../src/lib/contracts/food-availability-v1/partition.ts'; 3 import { 4 projectFoodParts, 5 readFoodEvent 6 } from '../../src/lib/contracts/food-availability-v1/read.ts'; 7 import { incomingAmount } from '../../src/lib/contracts/food-availability-v1/text.ts'; 8 import { projectImages } from '../../src/lib/contracts/food-availability-v1/media.ts'; 9 import { verifyEnvelope } from '../../src/lib/nostr/verified-envelope.ts'; 10 11 const base = { 12 kind: 30402, 13 created_at: 1700000060, 14 content: 'Food this week.', 15 tags: [ 16 ['d', 'carrots'], 17 ['title', 'Carrots'], 18 ['summary', 'Fresh carrots'], 19 ['published_at', '1700000000'], 20 ['location', 'Victoria'], 21 ['price', '003.500', 'cad'], 22 ['radroots:price_unit', 'lb'], 23 ['status', 'active'] 24 ] 25 }; 26 function changed(name: string, value: string[]) { 27 return { 28 ...base, 29 tags: base.tags.map((tag) => (tag[0] === name ? value : [...tag])) 30 }; 31 } 32 function code(value: ReturnType<typeof projectFoodParts>) { 33 return value.outcome === 'rejected' ? value.error.code : value.outcome; 34 } 35 describe('raw marker partition precedes shape and focused-domain checks', () => { 36 for (const name of ['radroots:price_unit', 'radroots:quantity']) 37 it(name, () => 38 expect(markerPartition([[name]])).toBe('focused_food_availability') 39 ); 40 for (const name of ['radroots:primary_bin', 'radroots:bin', 'radroots:price']) 41 it(name, () => { 42 expect(markerPartition([[name]])).toBe('operational_listing'); 43 expect( 44 code(projectFoodParts({ ...base, tags: [[name], ['delivery']] })) 45 ).toBe('excluded'); 46 expect( 47 code( 48 projectFoodParts({ ...base, tags: [[name], ['radroots:quantity']] }) 49 ) 50 ).toBe('food_profile_ambiguous'); 51 }); 52 it('exact names only and empty tags', () => 53 expect( 54 markerPartition([[], ['Radroots:quantity'], ['radroots:quantity ']]) 55 ).toBe('generic_nip99')); 56 it('unknown core fields never prevent generic exclusion', () => 57 expect( 58 code( 59 projectFoodParts({ 60 ...base, 61 tags: [ 62 ['status', 'unknown'], 63 ['price', 'bad'] 64 ] 65 }) 66 ) 67 ).toBe('excluded')); 68 }); 69 describe('tolerant values retain precision and unknown versus zero', () => { 70 for (const [input, expected] of [ 71 ['000.000', '0'], 72 ['003.500', '3.5'], 73 ['0000000000000000000000000001', '1'], 74 ['9999999999999999999999999999', '9999999999999999999999999999'] 75 ]) 76 it(input, () => expect(incomingAmount(input)).toBe(expected)); 77 for (const input of [ 78 '', 79 '.1', 80 '1.', 81 '+1', 82 '-1', 83 '1e3', 84 '1,000', 85 ' 1', 86 '١', 87 '0'.repeat(29), 88 '1.2.3' 89 ]) 90 it('rejects ' + input, () => expect(incomingAmount(input)).toBeUndefined()); 91 it('normalizes inbound only', () => { 92 const r = projectFoodParts(base); 93 expect(r.outcome).toBe('focused'); 94 if (r.outcome === 'focused') { 95 expect(r.projection.price).toEqual({ 96 amount: '3.5', 97 currency: 'CAD', 98 unit: 'lb' 99 }); 100 expect(r.projection.quantity).toBeNull(); 101 } 102 }); 103 for (const status of ['withdrawn', 'ACTIVE', 'unknown', '']) 104 it('unknown status ' + status, () => 105 expect( 106 code(projectFoodParts(changed('status', ['status', status]))) 107 ).toBe('food_status_invalid') 108 ); 109 it('zero price is valid while quantity zero is not missing', () => { 110 expect( 111 code(projectFoodParts(changed('price', ['price', '0', 'CAD']))) 112 ).toBe('focused'); 113 expect( 114 code( 115 projectFoodParts({ 116 ...base, 117 tags: [...base.tags, ['radroots:quantity', '000.0', 'lb']] 118 }) 119 ) 120 ).toBe('quantity_zero'); 121 }); 122 it('bad quantity cannot become zero or missing', () => 123 expect( 124 code( 125 projectFoodParts({ 126 ...base, 127 tags: [...base.tags, ['radroots:quantity', 'bad', 'lb']] 128 }) 129 ) 130 ).toBe('quantity_invalid')); 131 it('singleton shape checks precede invalid content', () => 132 expect( 133 code( 134 projectFoodParts({ 135 ...base, 136 content: '', 137 tags: [...base.tags, ['title', 'duplicate']] 138 }) 139 ) 140 ).toBe('food_tag_invalid')); 141 it('unsafe numeric time and unsupported publication time fail', () => { 142 expect( 143 code( 144 projectFoodParts({ 145 ...base, 146 created_at: Number('18446744073709551615') 147 }) 148 ) 149 ).toBe('envelope_invalid'); 150 expect( 151 code( 152 projectFoodParts( 153 changed('published_at', ['published_at', '18446744073709551615']) 154 ) 155 ) 156 ).toBe('food_published_at_invalid'); 157 }); 158 for (const value of [ 159 '\ufeffCarrots', 160 'Carrots\u200b', 161 'Carrots\n', 162 ' Carrots', 163 'Carrots\u0085' 164 ]) 165 it('rejects invalid text ' + JSON.stringify(value), () => 166 expect(code(projectFoodParts(changed('title', ['title', value])))).toBe( 167 'food_text_invalid' 168 ) 169 ); 170 it('identifier is byte bounded without imposing text trimming', () => 171 expect(code(projectFoodParts(changed('d', ['d', 'é'.repeat(257)])))).toBe( 172 'food_identifier_invalid' 173 )); 174 }); 175 describe('bounded incoming image diagnostics are structural only', () => { 176 const hash = 'a'.repeat(64); 177 it('first64 and ordered overflow diagnosis', () => { 178 const r = projectImages( 179 Array.from({ length: 65 }, (_, i) => [ 180 'image', 181 `https://media.example/${i.toString(16).padStart(64, '0')}.webp`, 182 '800x600' 183 ]) 184 ); 185 expect(r.images).toHaveLength(64); 186 expect(r.diagnostics).toEqual(['food_image_count_exceeded']); 187 }); 188 it('raw duplicates and digest duplicates differ', () => { 189 const r = projectImages([ 190 ['image', `https://a.example/${hash}.png`, '1x1'], 191 ['image', `https://b.example/${hash}.webp`, '1x1'], 192 ['image', 'bad', '01x1'], 193 ['image', 'bad'] 194 ]); 195 expect(r.images[1].diagnostics).toEqual(['food_image_duplicate_digest']); 196 expect(r.images[3].diagnostics).toEqual([ 197 'food_image_shape_invalid', 198 'food_image_url_invalid', 199 'food_image_dimensions_missing', 200 'food_image_duplicate_url' 201 ]); 202 }); 203 for (const url of [ 204 'https://user:pass@a.example/a', 205 'https://a.example', 206 'https://é.example/a', 207 'https://a..example/a', 208 'javascript:alert(1)', 209 'https://a.example/\u200ba' 210 ]) 211 it('rejects URL ' + url, () => 212 expect(projectImages([['image', url, '1x1']]).images[0].url).toBeNull() 213 ); 214 for (const dims of ['0x1', '01x1', '1X1', '4294967296x1', '1x1x1']) 215 it('rejects dimensions ' + dims, () => 216 expect( 217 projectImages([['image', 'https://a.example/a', dims]]).images[0] 218 .dimensions 219 ).toBeNull() 220 ); 221 it('accepts full u32 dimensions', () => 222 expect( 223 projectImages([['image', 'https://a.example/a', '4294967295x1']]) 224 .images[0].dimensions 225 ).toEqual({ width: 4294967295, height: 1 })); 226 }); 227 describe('raw input has no implied verification trust', () => { 228 for (const raw of [ 229 undefined, 230 {}, 231 'null', 232 '[]', 233 '{', 234 ' '.repeat(262145), 235 '"\\ud800"' 236 ]) 237 it('rejects malformed bounded envelope', () => 238 expect(verifyEnvelope(raw).ok).toBe(false)); 239 it('invalid signature is rejected before generic/operational classification', () => 240 expect( 241 readFoodEvent( 242 JSON.stringify({ 243 id: '0'.repeat(64), 244 pubkey: '0'.repeat(64), 245 sig: '0'.repeat(128), 246 created_at: 1, 247 kind: 30402, 248 content: '', 249 tags: [['radroots:bin']] 250 }) 251 ).outcome 252 ).toBe('rejected')); 253 }); 254 255 describe('exact shared reader text and numeric boundaries', () => { 256 it('content maximum uses UTF8 bytes and never prototype form limits', () => { 257 expect( 258 code(projectFoodParts({ ...base, content: 'a'.repeat(131072) })) 259 ).toBe('focused'); 260 expect( 261 code(projectFoodParts({ ...base, content: 'é'.repeat(65537) })) 262 ).toBe('food_content_too_large'); 263 }); 264 it('all contract whitespace is missing while internal content controls are preserved', () => { 265 expect( 266 code(projectFoodParts({ ...base, content: '\u001c\u001f\u0085' })) 267 ).toBe('food_content_missing'); 268 expect(code(projectFoodParts({ ...base, content: 'a\u0000b' }))).toBe( 269 'focused' 270 ); 271 }); 272 it('text byte limit and identifiers do not use display character counts', () => { 273 expect( 274 code(projectFoodParts(changed('title', ['title', 'é'.repeat(2048)]))) 275 ).toBe('focused'); 276 expect( 277 code(projectFoodParts(changed('title', ['title', 'é'.repeat(2049)]))) 278 ).toBe('envelope_invalid'); 279 expect(code(projectFoodParts(changed('d', ['d', 'é'.repeat(256)])))).toBe( 280 'focused' 281 ); 282 }); 283 for (const input of ['0', '01', '+1', '1e3', '1700000061']) 284 it('publication ' + input, () => 285 expect( 286 code(projectFoodParts(changed('published_at', ['published_at', input]))) 287 ).toBe( 288 input === '1700000061' 289 ? 'food_published_at_future' 290 : 'food_published_at_invalid' 291 ) 292 ); 293 for (const input of ['cad', 'cAd', 'AAA']) 294 it('actual inbound ASCII currency ' + input, () => 295 expect( 296 code(projectFoodParts(changed('price', ['price', '1', input]))) 297 ).toBe('focused') 298 ); 299 for (const input of ['CA', 'CAD', '12A', 'CA D']) 300 it('reject currency ' + input, () => 301 expect( 302 code(projectFoodParts(changed('price', ['price', '1', input]))) 303 ).toBe('price_currency_invalid') 304 ); 305 it('unit values are exact and quantity does not normalize case or mismatched units', () => { 306 expect( 307 code( 308 projectFoodParts( 309 changed('radroots:price_unit', ['radroots:price_unit', 'LB']) 310 ) 311 ) 312 ).toBe('price_unit_invalid'); 313 expect( 314 code( 315 projectFoodParts({ 316 ...base, 317 tags: [...base.tags, ['radroots:quantity', '1', 'kg']] 318 }) 319 ) 320 ).toBe('quantity_invalid'); 321 }); 322 it('duplicate quantity is invalid even when both match', () => 323 expect( 324 code( 325 projectFoodParts({ 326 ...base, 327 tags: [ 328 ...base.tags, 329 ['radroots:quantity', '1', 'lb'], 330 ['radroots:quantity', '1', 'lb'] 331 ] 332 }) 333 ) 334 ).toBe('quantity_invalid')); 335 it('ill formed Unicode cannot pass through UTF8 replacement', () => { 336 expect(code(projectFoodParts(changed('d', ['d', '\ud800'])))).toBe( 337 'envelope_invalid' 338 ); 339 expect(code(projectFoodParts(changed('title', ['title', '\ud800'])))).toBe( 340 'envelope_invalid' 341 ); 342 }); 343 }); 344 345 it('bounded constant errors cannot be altered through a prior rejection', () => { 346 const prior = verifyEnvelope('null'); 347 if (!prior.ok) 348 expect(() => { 349 (prior.error as { message: string }).message = 'unbounded replacement'; 350 }).toThrow(); 351 const next = verifyEnvelope('null'); 352 if (!next.ok) 353 expect(next.error).toEqual({ 354 code: 'envelope_invalid', 355 message: 'invalid or unsupported bounded NIP-01 envelope' 356 }); 357 }); 358 359 describe('direct pure parts remain bounded before marker/value scans', () => { 360 it('tag count limit is enforced before generic exclusion', () => 361 expect( 362 code( 363 projectFoodParts({ 364 ...base, 365 tags: Array.from({ length: 1025 }, () => ['t', 'food']) 366 }) 367 ) 368 ).toBe('envelope_invalid')); 369 it('raw content work cap is separate from shared content limit', () => 370 expect( 371 code(projectFoodParts({ ...base, content: 'x'.repeat(262145) })) 372 ).toBe('envelope_invalid')); 373 it('tag byte and total element limits precede profile checks', () => { 374 expect( 375 code(projectFoodParts({ ...base, tags: [['t', 'é'.repeat(2049)]] })) 376 ).toBe('envelope_invalid'); 377 expect( 378 code( 379 projectFoodParts({ 380 ...base, 381 tags: [['t', ...Array.from({ length: 4096 }, () => 'x')]] 382 }) 383 ) 384 ).toBe('envelope_invalid'); 385 }); 386 });