references.test.ts (7097B)
1 import { describe, expect, it, vi } from 'vitest'; 2 import * as pointers from 'applesauce-core/helpers/pointers'; 3 import { bech32 } from '@scure/base'; 4 import { 5 decodeProductReference, 6 encodeProductReference 7 } from '../../src/lib/nostr/references'; 8 import { productHref, productEditHref } from '../../src/lib/routes'; 9 import { match } from '../../src/params/naddr'; 10 import oracle from './reference-oracle.json'; 11 12 // Instrument the actual export; its implementation and decoded results remain 13 // real. Native ESM namespaces themselves are immutable and cannot be spied on. 14 vi.mock('applesauce-core/helpers/pointers', { spy: true }); 15 16 const pubkey = oracle.key_cases[0].pubkey; 17 const encode = (identifier: string, kind = 30402) => 18 pointers.naddrEncode({ 19 kind, 20 pubkey, 21 identifier, 22 relays: ['wss://example.invalid'] 23 }); 24 const raw = (input: string) => { 25 const decoded = bech32.decode(input, 2048); 26 return Uint8Array.from(bech32.fromWords(decoded.words)); 27 }; 28 const wrap = (data: Uint8Array) => 29 bech32.encode('naddr', bech32.toWords(data), 2048); 30 31 describe('bounded exact product references', () => { 32 it('ignores extra relay hints when encoding the same raw coordinate', () => { 33 const coordinate = { kind: 30402, pubkey, identifier: 'Carrots:菜' }; 34 const subtype = { 35 ...coordinate, 36 relays: ['wss://example.invalid/' + 'x'.repeat(2500)] 37 }; 38 expect(encodeProductReference(subtype)).toBe( 39 pointers.naddrEncode(coordinate) 40 ); 41 }); 42 it('never reads extra relay getters during coordinate encoding', () => { 43 const coordinate = { kind: 30402, pubkey, identifier: 'Carrots:菜' }; 44 const read = vi.fn(() => { 45 throw new Error('Extra relay field read'); 46 }); 47 const subtype = { 48 ...coordinate, 49 get relays() { 50 return read(); 51 } 52 }; 53 expect(encodeProductReference(subtype)).toBe( 54 pointers.naddrEncode(coordinate) 55 ); 56 expect(read).not.toHaveBeenCalled(); 57 }); 58 for (const row of oracle.coordinate_cases) 59 it(`Rust raw identifier ${JSON.stringify(row.identifier)}`, () => { 60 const input = encode(row.identifier); 61 const result = decodeProductReference(input); 62 expect(result).toEqual({ 63 kind: 30402, 64 pubkey, 65 identifier: row.identifier, 66 naddr: pointers.naddrEncode({ 67 kind: 30402, 68 pubkey, 69 identifier: row.identifier 70 }) 71 }); 72 expect( 73 encodeProductReference({ 74 kind: 30402, 75 pubkey, 76 identifier: row.identifier 77 }) 78 ).toBe(result?.naddr); 79 expect(decodeProductReference(result?.naddr)?.identifier).toBe( 80 row.identifier 81 ); 82 expect(productHref(input)).toBe(`/products/${result?.naddr}`); 83 expect(productEditHref(input)).toBe(`/products/${result?.naddr}/edit`); 84 expect(match(input)).toBe(true); 85 expect(decodeProductReference(input.toUpperCase())).toEqual(result); 86 }); 87 it('ignores unknown framed TLVs and uses the selected decoder first identifier', () => { 88 const bytes = raw(encode('Carrots:菜')); 89 const changed = wrap(Uint8Array.from([99, 3, 1, 2, 3, ...bytes, 0, 1, 65])); 90 expect(decodeProductReference(changed)?.identifier).toBe('Carrots:菜'); 91 expect(decodeProductReference(changed)?.naddr).toBe( 92 encodeProductReference({ kind: 30402, pubkey, identifier: 'Carrots:菜' }) 93 ); 94 }); 95 it('rejects malformed UTF8 instead of replacement, but accepts actual U+FFFD', () => { 96 const bytes = raw(encode('a')); 97 for (const invalid of [ 98 [0xff], 99 [0xc0, 0xaf], 100 [0xed, 0xa0, 0x80], 101 [0xf4, 0x90, 0x80, 0x80], 102 [0xe2, 0x82] 103 ]) { 104 const input = wrap( 105 Uint8Array.from([0, invalid.length, ...invalid, ...bytes.slice(3)]) 106 ); 107 expect(decodeProductReference(input)).toBeUndefined(); 108 } 109 expect(decodeProductReference(encode('\ufffd'))?.identifier).toBe('\ufffd'); 110 }); 111 it('rejects truncated TLV framing including malformed unknown TLVs', () => { 112 const bytes = raw(encode('a')); 113 for (const suffix of [[99], [99, 2, 1]]) 114 expect( 115 decodeProductReference(wrap(Uint8Array.from([...bytes, ...suffix]))) 116 ).toBeUndefined(); 117 }); 118 it('admits exactly 2048 bytes and rejects overflow before decoding', () => { 119 const bytes = [...raw(encode('Carrots:菜'))]; 120 // 1272 payload bytes encode to 2036 words plus the prefix/checksum:2048. 121 while (bytes.length < 1272) { 122 const length = Math.min(255, 1272 - bytes.length - 2); 123 bytes.push(99, length, ...Array.from({ length }, () => 0)); 124 } 125 const input = wrap(Uint8Array.from(bytes)); 126 expect(new TextEncoder().encode(input).length).toBe(2048); 127 expect(decodeProductReference(input)?.identifier).toBe('Carrots:菜'); 128 const spy = vi.spyOn(pointers, 'decodePointer'); 129 spy.mockClear(); 130 try { 131 expect(decodeProductReference(input + 'q')).toBeUndefined(); 132 expect(spy).not.toHaveBeenCalled(); 133 } finally { 134 spy.mockRestore(); 135 } 136 }); 137 it('bounds before Applesauce decoding', () => { 138 const spy = vi.spyOn(pointers, 'decodePointer'); 139 try { 140 for (const input of [ 141 'naddr1' + 'q'.repeat(2043), 142 '菜'.repeat(683), 143 'naddr1' + '\ud800', 144 null, 145 {}, 146 '' 147 ]) 148 expect(decodeProductReference(input)).toBeUndefined(); 149 expect(spy).not.toHaveBeenCalled(); 150 } finally { 151 spy.mockRestore(); 152 } 153 }); 154 it('rejects unsupported/mixed-case/bad keys and never fetches', () => { 155 const value = encode('a'); 156 const bad = [ 157 value.slice(0, 7).toUpperCase() + value.slice(7), 158 value + 'q', 159 encode('a', 1), 160 pointers.naddrEncode({ 161 kind: 30402, 162 pubkey: '0'.repeat(64), 163 identifier: 'a' 164 }), 165 pointers.naddrEncode({ 166 kind: 30402, 167 pubkey: 'f'.repeat(64), 168 identifier: 'a' 169 }), 170 'https://example.invalid/', 171 `nostr:${value}` 172 ]; 173 const fetchSpy = vi.spyOn(globalThis, 'fetch'); 174 try { 175 for (const input of bad) { 176 expect(decodeProductReference(input)).toBeUndefined(); 177 expect(productHref(input)).toBeUndefined(); 178 expect(productEditHref(input)).toBeUndefined(); 179 expect(match(input)).toBe(false); 180 } 181 expect(fetchSpy).not.toHaveBeenCalled(); 182 } finally { 183 fetchSpy.mockRestore(); 184 } 185 }); 186 it('does not truncate unencodable identifier bytes or repair invalid input', () => { 187 for (const identifier of ['x'.repeat(256), '菜'.repeat(86), '\ud800']) 188 expect( 189 encodeProductReference({ kind: 30402, pubkey, identifier }) 190 ).toBeUndefined(); 191 expect( 192 encodeProductReference({ kind: 1, pubkey, identifier: 'a' }) 193 ).toBeUndefined(); 194 expect( 195 encodeProductReference({ 196 kind: 30402, 197 pubkey: pubkey.toUpperCase(), 198 identifier: 'a' 199 }) 200 ).toBeUndefined(); 201 expect( 202 encodeProductReference({ 203 kind: 30402, 204 pubkey, 205 identifier: 'x'.repeat(255) 206 }) 207 ).toBeDefined(); 208 }); 209 });