enquiry-context.test.ts (7935B)
1 import { afterAll, expect, it } from 'vitest'; 2 import { finalizeEvent } from 'applesauce-core/helpers'; 3 import { decodePointer } from 'applesauce-core/helpers/pointers'; 4 import { 5 verifyEnvelope, 6 verifiedEnvelopeSnapshot, 7 type VerifiedEnvelope 8 } from '../../src/lib/nostr/verified-envelope.ts'; 9 import { enquiryReferences } from '../../src/lib/nostr/enquiry-references.ts'; 10 import { 11 captureEnquiryContext, 12 enquiryContextSnapshot, 13 buildEnquiryMessage, 14 assessEnquiryCitation, 15 type EnquiryContext 16 } from '../../src/lib/messaging/enquiry-context.ts'; 17 const keys: Uint8Array[] = []; 18 afterAll(() => { 19 for (const key of keys) key.fill(0); 20 }); 21 function seller() { 22 const key = crypto.getRandomValues(new Uint8Array(32)); 23 keys.push(key); 24 return ( 25 time = 1700000060, 26 title = 'Carrots', 27 identifier = 'carrots', 28 kind = 30402, 29 status = 'active' 30 ) => { 31 const event = finalizeEvent( 32 { 33 kind, 34 created_at: time, 35 content: 'Fresh food', 36 tags: [ 37 ['d', identifier], 38 ['title', title], 39 ['summary', 'Fresh carrots'], 40 ['published_at', '1700000000'], 41 ['location', 'Victoria'], 42 ['price', '3.5', 'CAD'], 43 ['radroots:price_unit', 'lb'], 44 ['status', status] 45 ] 46 }, 47 key 48 ); 49 const value = verifyEnvelope(JSON.stringify(event)); 50 if (!value.ok) throw Error('signed public test fixture invalid'); 51 return value.value; 52 }; 53 } 54 function context(proof: VerifiedEnvelope) { 55 const value = captureEnquiryContext(proof); 56 if (!value) throw Error('expected verified Food context'); 57 return value; 58 } 59 it('captures genuine signed seller, readable canonical product and exact version without relay hints', () => { 60 const proof = seller()(), 61 event = verifiedEnvelopeSnapshot(proof)!, 62 value = context(proof), 63 snapshot = enquiryContextSnapshot(value)!; 64 expect(snapshot.peer).toBe(event.pubkey); 65 expect(snapshot.eventId).toBe(event.id); 66 expect(snapshot.title).toBe('Carrots'); 67 const address = decodePointer(snapshot.naddr), 68 version = decodePointer(snapshot.nevent); 69 expect(address).toMatchObject({ 70 type: 'naddr', 71 data: { 72 kind: 30402, 73 pubkey: event.pubkey, 74 identifier: 'carrots', 75 relays: [] 76 } 77 }); 78 expect(version).toMatchObject({ 79 type: 'nevent', 80 data: { id: event.id, author: event.pubkey, kind: 30402, relays: [] } 81 }); 82 const message = buildEnquiryMessage(value, 'Could I collect some tomorrow?')!; 83 expect(message.tags).toEqual([['p', event.pubkey]]); 84 expect(message.content).toContain('"Carrots"'); 85 expect(message.content).toContain('nostr:' + snapshot.naddr); 86 expect(message.content).toContain('nostr:' + snapshot.nevent); 87 expect(message.content).toContain( 88 'Advertised price (seller assertion): 3.5 CAD/lb' 89 ); 90 expect(message.content).toContain( 91 'This is a message, not a confirmed order.' 92 ); 93 expect(message.content).toContain('Could I collect some tomorrow?'); 94 expect(message.kind).toBe(14); 95 expect(message).not.toHaveProperty('sig'); 96 expect(message).not.toHaveProperty('id'); 97 }); 98 it('body contact, display names and detached projections cannot redirect the verified publisher', () => { 99 const proof = seller()(), 100 other = verifiedEnvelopeSnapshot(seller()())!, 101 value = context(proof), 102 original = enquiryContextSnapshot(value)!; 103 const altered = enquiryContextSnapshot(value)! as { 104 peer: string; 105 title: string; 106 eventId: string; 107 }; 108 altered.peer = other.pubkey; 109 altered.title = 'Other farm'; 110 altered.eventId = other.id; 111 const message = buildEnquiryMessage( 112 value, 113 'Reply to nostr:' + other.pubkey + '\nSeller name: Other farm' 114 )!; 115 expect(message.tags).toEqual([['p', original.peer]]); 116 expect(enquiryContextSnapshot(value)).toEqual(original); 117 expect(message.content).toContain(original.nevent); 118 expect(message.tags.some((tag) => tag[0] === 'e')).toBe(false); 119 }); 120 it('forged proof/context casts and unsupported private kinds grant no citation or enquiry', () => { 121 expect(captureEnquiryContext({} as VerifiedEnvelope)).toBeUndefined(); 122 expect(enquiryReferences({} as VerifiedEnvelope)).toBeUndefined(); 123 expect(enquiryContextSnapshot({} as EnquiryContext)).toBeUndefined(); 124 expect(buildEnquiryMessage({} as EnquiryContext, 'hello')).toBeUndefined(); 125 for (const kind of [14, 13, 1059, 0, 1]) { 126 const proof = seller()(1700000060, 'Carrots', 'carrots', kind); 127 expect(enquiryReferences(proof)).toBeUndefined(); 128 expect(captureEnquiryContext(proof)).toBeUndefined(); 129 } 130 }); 131 it('missing or mismatching cited history remains unresolved even with matching body or name', () => { 132 const signed = seller(), 133 proof = signed(), 134 value = context(proof); 135 expect(assessEnquiryCitation(value, undefined)).toEqual({ 136 outcome: 'unresolved', 137 reason: 'missing_history' 138 }); 139 expect(assessEnquiryCitation(value, signed(1700000061))).toEqual({ 140 outcome: 'unresolved', 141 reason: 'different_version' 142 }); 143 expect(assessEnquiryCitation(value, seller()())).toEqual({ 144 outcome: 'unresolved', 145 reason: 'different_publisher' 146 }); 147 expect(assessEnquiryCitation(value, {} as VerifiedEnvelope)).toEqual({ 148 outcome: 'unresolved', 149 reason: 'missing_history' 150 }); 151 expect(assessEnquiryCitation(value, proof)).toEqual({ 152 outcome: 'matched_signed_advertisement', 153 peer: verifiedEnvelopeSnapshot(proof)!.pubkey, 154 eventId: verifiedEnvelopeSnapshot(proof)!.id 155 }); 156 }); 157 it('quoted facts remain advertisement assertions without allocation or locked price state', () => { 158 const value = context(seller()()), 159 snapshot = enquiryContextSnapshot(value)!; 160 expect(snapshot.claims).toBe('signed_advertisement_assertions'); 161 expect(snapshot).not.toHaveProperty('reserved'); 162 expect(snapshot).not.toHaveProperty('acceptedOrder'); 163 expect( 164 buildEnquiryMessage(value, 'I would like ten; this text is my assertion') 165 ?.tags 166 ).toHaveLength(1); 167 }); 168 it('raw identifier bytes survive canonical references and exact viewed-version capture', () => { 169 for (const identifier of ['caf\u00e9', 'cafe\u0301']) { 170 const proof = seller()(1700000060, 'Carrots', identifier), 171 refs = enquiryReferences(proof)!, 172 value = context(proof); 173 expect(refs.identifier).toBe(identifier); 174 expect(enquiryContextSnapshot(value)?.identifier).toBe(identifier); 175 expect(buildEnquiryMessage(value, '')?.content).toContain( 176 'nostr:' + refs.naddr 177 ); 178 } 179 }); 180 it('generic BOM reference preservation does not admit an unsupported Food identifier', () => { 181 const proof = seller()(1700000060, 'Carrots', '\ufeffcarrots'); 182 expect(enquiryReferences(proof)?.identifier).toBe('\ufeffcarrots'); 183 expect(captureEnquiryContext(proof)).toBeUndefined(); 184 }); 185 it('composed body includes context within the four KiB limit and never truncates', () => { 186 const value = context(seller()()), 187 seed = buildEnquiryMessage(value, '')!.content, 188 remaining = 4096 - new TextEncoder().encode(seed).length - 2; 189 expect( 190 new TextEncoder().encode( 191 buildEnquiryMessage(value, 'a'.repeat(remaining))!.content 192 ).length 193 ).toBe(4096); 194 expect(buildEnquiryMessage(value, 'a'.repeat(remaining + 1))).toBeUndefined(); 195 expect( 196 buildEnquiryMessage(value, 'é'.repeat(Math.floor(remaining / 2))) 197 ).toBeDefined(); 198 expect(buildEnquiryMessage(value, '\ud800')).toBeUndefined(); 199 expect(buildEnquiryMessage(value, { redirect: 'other' })).toBeUndefined(); 200 }); 201 it('known sold listing remains citable but cannot create a new product enquiry', () => { 202 const proof = seller()(1700000060, 'Carrots', 'carrots', 30402, 'sold'), 203 value = context(proof); 204 expect(enquiryContextSnapshot(value)?.status).toBe('sold'); 205 expect(assessEnquiryCitation(value, proof)?.outcome).toBe( 206 'matched_signed_advertisement' 207 ); 208 expect(buildEnquiryMessage(value, 'Could I buy some?')).toBeUndefined(); 209 });