food-reader.test.ts (5188B)
1 import assert from 'node:assert/strict'; 2 import { readFile } from 'node:fs/promises'; 3 import test from 'node:test'; 4 import { readFoodEvent } from '../../src/lib/contracts/food-availability-v1/read.ts'; 5 import { 6 verifyEnvelope, 7 verifiedEnvelopeSnapshot 8 } from '../../src/lib/nostr/verified-envelope.ts'; 9 10 interface Vector { 11 id: string; 12 kind: string; 13 expected: Record<string, unknown>; 14 signed_wires: Record<string, string>; 15 } 16 const corpus = JSON.parse( 17 await readFile( 18 new URL( 19 '../../../contracts/interop/food_availability/corpus.v1.json', 20 import.meta.url 21 ), 22 'utf8' 23 ) 24 ) as { vectors: Vector[] }; 25 for (const row of corpus.vectors) { 26 if (!row.signed_wires.event) continue; 27 void test(`pinned Rust reader outcome: ${row.id}`, () => { 28 const actual = readFoodEvent(row.signed_wires.event); 29 const expected = row.expected; 30 if ('error' in expected) { 31 assert.equal(actual.outcome, 'rejected'); 32 if (actual.outcome === 'rejected') 33 assert.deepEqual(actual.error, expected.error); 34 } else if ('projection' in expected) { 35 assert.equal(actual.outcome, 'admitted'); 36 if (actual.outcome === 'admitted') { 37 assert.equal(actual.event_id, expected.event_id); 38 assert.deepEqual(actual.projection, expected.projection); 39 } 40 } else if ('partition' in expected) { 41 assert.equal(actual.outcome, 'excluded'); 42 if (actual.outcome === 'excluded') { 43 assert.equal(actual.event_id, expected.event_id); 44 assert.equal(actual.partition, expected.partition); 45 } 46 } else { 47 assert.equal(actual.outcome, 'admitted'); 48 if (actual.outcome === 'admitted') { 49 assert.equal(actual.event_id, expected.event_id); 50 assert.equal(actual.projection.images.length, expected.image_count); 51 assert.deepEqual( 52 actual.projection.images[0].raw_tag, 53 expected.first_raw_tag 54 ); 55 assert.deepEqual( 56 actual.projection.images.at(-1)?.raw_tag, 57 expected.last_raw_tag 58 ); 59 assert.deepEqual(actual.projection.diagnostics, expected.diagnostics); 60 } 61 } 62 }); 63 } 64 void test('all public signed wires cross actual SDK verification; detached snapshots cannot change retained proof', () => { 65 let checked = 0; 66 for (const row of corpus.vectors) 67 for (const raw of Object.values(row.signed_wires)) { 68 const result = verifyEnvelope(raw); 69 if (row.id.endsWith('_031')) { 70 assert.equal(result.ok, false); 71 } else { 72 assert.equal(result.ok, true, row.id); 73 if (result.ok) { 74 const a = verifiedEnvelopeSnapshot(result.value)!; 75 a.tags.length = 0; 76 a.content = 'mutated'; 77 a.created_at = 0; 78 assert.deepEqual( 79 verifiedEnvelopeSnapshot(result.value), 80 JSON.parse(raw), 81 row.id 82 ); 83 assert.equal(verifiedEnvelopeSnapshot({} as never), undefined); 84 } 85 } 86 checked++; 87 } 88 assert.equal(checked, 36); 89 }); 90 91 const sample = corpus.vectors.find((row) => row.id.endsWith('_014'))! 92 .signed_wires.event; 93 void test('root numeric spelling cannot be laundered by JSON numeric rounding', () => { 94 for (const spelling of [ 95 '1700000060.0', 96 '1.700000060e9', 97 '1700000060.00000000001' 98 ]) { 99 const altered = sample.replace( 100 /"created_at":\s*[0-9]+/u, 101 `"created_at":${spelling}` 102 ); 103 assert.notEqual(altered, sample); 104 assert.equal(verifyEnvelope(altered).ok, false, spelling); 105 } 106 const altered = sample.replace(/"kind":\s*30402/u, '"kind":30402.0'); 107 assert.notEqual(altered, sample); 108 assert.equal(verifyEnvelope(altered).ok, false); 109 }); 110 void test('root numeric source is separate from nested unknown extension fields', () => { 111 const altered = sample.replace( 112 /\}$/u, 113 ',"extension":{"kind":1.0,"created_at":2e0}}' 114 ); 115 assert.equal(verifyEnvelope(altered).ok, true); 116 }); 117 void test('extra-field counts and serialized key/value byte charge match pinned wire limits', () => { 118 const original = JSON.parse(sample) as Record<string, unknown>; 119 const tooMany = { 120 ...original, 121 ...Object.fromEntries(Array.from({ length: 65 }, (_, i) => [`x${i}`, 0])) 122 }; 123 assert.equal(verifyEnvelope(JSON.stringify(tooMany)).ok, false); 124 const value = 'x'.repeat(65536 - 6); // JSON key \"x\": plus JSON value quotes = six bytes. 125 assert.equal( 126 verifyEnvelope(JSON.stringify({ ...original, x: value })).ok, 127 true 128 ); 129 assert.equal( 130 verifyEnvelope(JSON.stringify({ ...original, x: value + 'x' })).ok, 131 false 132 ); 133 const maximum = { 134 ...original, 135 ...Object.fromEntries(Array.from({ length: 64 }, (_, i) => [`x${i}`, 0])) 136 }; 137 assert.equal(verifyEnvelope(JSON.stringify(maximum)).ok, true); 138 }); 139 140 void test('parsed nonfinite extras and ill formed Unicode are rejected before verified proof', () => { 141 for (const suffix of [ 142 '"extension":1e309', 143 '"extension":{"value":-1e309}', 144 '"extension":"\\ud800"', 145 '"\\ud800":0' 146 ]) { 147 const altered = sample.replace(/\}$/u, `,` + suffix + '}'); 148 assert.notEqual(altered, sample); 149 assert.equal(verifyEnvelope(altered).ok, false, suffix); 150 } 151 });