listing-view.test.ts (6477B)
1 import { afterAll, describe, expect, it } from 'vitest'; 2 import { readFileSync } from 'node:fs'; 3 import { finalizeEvent } from 'applesauce-core/helpers'; 4 import { searchResolvedFood } from '../../src/lib/catalog/search-match.ts'; 5 import { 6 createPublicHeadCandidate, 7 publicHeadEnvelope 8 } from '../../src/lib/catalog/heads.ts'; 9 import { 10 createHeadResolver, 11 resolveHeads, 12 headResolutionSnapshot 13 } from '../../src/lib/catalog/resolve-head.ts'; 14 import { 15 verifyEnvelope, 16 verifiedEnvelopeSnapshot 17 } from '../../src/lib/nostr/verified-envelope.ts'; 18 import { 19 createPublicScheduler, 20 createPublicRun, 21 openPublicRequest, 22 closePublicScheduler 23 } from '../../src/lib/nostr/request-scope.ts'; 24 import { validateRelayPolicy } from '../../src/lib/config/relays.ts'; 25 import { 26 createPublicRetention, 27 retainPublicEnvelope, 28 publicRetentionKnown 29 } from '../../src/lib/catalog/retention.ts'; 30 const corpus = JSON.parse( 31 readFileSync( 32 new URL( 33 '../../../contracts/interop/food_availability/corpus.v1.json', 34 import.meta.url 35 ), 36 'utf8' 37 ) 38 ) as { vectors: { id: string; signed_wires: Record<string, string> }[] }; 39 const base = JSON.parse( 40 corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous 41 ) as { tags: string[][]; content: string; created_at: number }; 42 const cleanup: (() => void)[] = []; 43 afterAll(() => { 44 for (const stop of cleanup) stop(); 45 }); 46 function fixture() { 47 const key = crypto.getRandomValues(new Uint8Array(32)); 48 cleanup.push(() => key.fill(0)); 49 const proof = ( 50 time = base.created_at, 51 tags = base.tags, 52 content = base.content, 53 kind = 30402 54 ) => { 55 const p = verifyEnvelope( 56 JSON.stringify( 57 finalizeEvent({ kind, created_at: time, tags, content }, key) 58 ) 59 ); 60 if (!p.ok) throw Error('search fixture'); 61 return p.value; 62 }; 63 const head = (p: ReturnType<typeof proof>) => { 64 const h = createPublicHeadCandidate(p); 65 if (!h) throw Error('search head'); 66 return h; 67 }; 68 const policy = validateRelayPolicy( 69 JSON.stringify({ 70 schemaVersion: 1, 71 public: [], 72 inbox: [], 73 postingEnabled: false, 74 messagingEnabled: false, 75 operatorDenylist: [] 76 }) 77 )!; 78 const scheduler = createPublicScheduler({ 79 now: () => 0, 80 schedule: () => () => {} 81 }), 82 run = createPublicRun(scheduler, policy); 83 cleanup.push(() => closePublicScheduler(scheduler)); 84 const retention = createPublicRetention(); 85 let available = true, 86 now = base.created_at + 10, 87 assessments = 0; 88 const model = createHeadResolver( 89 run, 90 (kind, filters, next) => openPublicRequest(run, kind, () => () => {}, next), 91 { 92 nowSeconds: () => { 93 assessments++; 94 return now; 95 } 96 }, 97 { 98 retain: (heads) => { 99 for (const h of heads) 100 retainPublicEnvelope(retention, publicHeadEnvelope(h)); 101 return true; 102 }, 103 read: (h) => publicRetentionKnown(retention, h), 104 available: () => available 105 } 106 ); 107 return { 108 proof, 109 head, 110 model, 111 retention, 112 add: (p: ReturnType<typeof proof>) => resolveHeads(model, [head(p)]), 113 retain: (p: ReturnType<typeof proof>) => retainPublicEnvelope(retention, p), 114 unavailable: () => { 115 available = false; 116 }, 117 clock: (value: number) => { 118 now = value; 119 }, 120 clockSamples: () => assessments 121 }; 122 } 123 import { 124 orderFoodRows, 125 listingView 126 } from '../../src/lib/catalog/listing-view.ts'; 127 describe('exact advertised listing views', () => { 128 it('keeps signed text and decimals beyond safe integers, and unknown quantity', () => { 129 const f = fixture(); 130 const tags = base.tags 131 .filter((t) => t[0] !== 'radroots:quantity') 132 .map((t) => 133 t[0] === 'price' 134 ? ['price', '9007199254740993.123456789', 'CAD'] 135 : t[0] === 'title' 136 ? ['title', '<script>CARROTS</script>'] 137 : [...t] 138 ); 139 const p = f.proof(base.created_at, tags); 140 f.add(p); 141 const row = headResolutionSnapshot(f.model)[0]; 142 const value = listingView(row)!; 143 expect(value.title).toBe('<script>CARROTS</script>'); 144 expect(value.price).toEqual(row.state.food!.price); 145 expect(value.price.amount).toBe('9007199254740993.123456789'); 146 expect(value.quantity).toBeNull(); 147 expect(value.href).toMatch(/^\/products\/naddr1/); 148 expect(value.href).not.toContain('?'); 149 (value.price as { amount: string }).amount = '0'; 150 expect(listingView(row)!.price.amount).toBe('9007199254740993.123456789'); 151 expect(verifiedEnvelopeSnapshot(p)?.content).toBe(base.content); 152 }); 153 it('orders revision time then lower event ID without mutating the input', () => { 154 const f = fixture(); 155 for (const [d, t] of [ 156 ['a', 0], 157 ['b', 1], 158 ['c', 1] 159 ] as const) 160 f.add( 161 f.proof( 162 base.created_at + t, 163 base.tags.map((v) => (v[0] === 'd' ? ['d', d] : [...v])) 164 ) 165 ); 166 const rows = headResolutionSnapshot(f.model), 167 before = rows.map((v) => v.state.head.id); 168 const ordered = orderFoodRows(rows); 169 expect(ordered[0].state.head.created_at).toBe(base.created_at + 1); 170 expect(ordered[0].state.head.id < ordered[1].state.head.id).toBe(true); 171 expect(rows.map((v) => v.state.head.id)).toEqual(before); 172 }); 173 it('does not promote older keyword matches after sold, renamed or withdrawn latest evidence', () => { 174 for (const outcome of ['sold', 'renamed', 'withdrawn']) { 175 const f = fixture(), 176 old = f.proof(); 177 f.add(old); 178 const tags = base.tags.map((t) => 179 outcome === 'sold' && t[0] === 'status' 180 ? ['status', 'sold'] 181 : outcome === 'renamed' && t[0] === 'title' 182 ? ['title', 'Celery'] 183 : [...t] 184 ); 185 const newer = f.proof( 186 base.created_at + 1, 187 tags, 188 outcome === 'renamed' ? 'Celery' : base.content 189 ); 190 f.retain(newer); 191 if (outcome === 'withdrawn') 192 f.retain( 193 f.proof( 194 base.created_at, 195 [['e', verifiedEnvelopeSnapshot(newer)!.id]], 196 '', 197 5 198 ) 199 ); 200 const matched = searchResolvedFood( 201 f.model, 202 outcome === 'renamed' ? 'nantes' : '' 203 ); 204 expect(matched.ok && matched.rows).toEqual([]); 205 if (outcome !== 'renamed') 206 expect(listingView(headResolutionSnapshot(f.model)[0])).toBeUndefined(); 207 } 208 }); 209 });