app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

commit 1e9be275a3d2d4e257829a508a476a6fddecaf60
parent 4422dac7bdd418342d73e9e2b65e4b57b6a45b7c
Author: triesap <tyson@radroots.org>
Date:   Tue,  6 Oct 2026 17:27:52 +0000

availability: Assemble verified listings and bounded publisher views

- Order admitted current revisions before twenty-row pagination
- Preserve exact advertised values and stable product references
- Bound and coalesce asserted publisher metadata within public runs
- Verify lifecycle, page budgets, fallbacks and preserved native inputs

Diffstat:
Aweb/src/lib/catalog/listing-view.ts | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/src/lib/catalog/publishers.ts | 190+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/src/lib/catalog/search-run.ts | 57++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mweb/src/lib/catalog/search-state.ts | 2++
Mweb/src/lib/config/budgets.ts | 3++-
Aweb/src/lib/nostr/profile-query.ts | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/integration/search-publishers.test.ts | 232+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/unit/listing-view.test.ts | 209+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/unit/profile-query.test.ts | 39+++++++++++++++++++++++++++++++++++++++
Aweb/tests/unit/publishers.test.ts | 391+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/unit/search-run.test.ts | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 1314 insertions(+), 2 deletions(-)

diff --git a/web/src/lib/catalog/listing-view.ts b/web/src/lib/catalog/listing-view.ts @@ -0,0 +1,74 @@ +import type { HeadResolution } from './resolve-head.ts'; +import type { FoodProjection } from '../contracts/food-availability-v1/read.ts'; +import { encodeProductReference } from '../nostr/references.ts'; +import { internalHref } from '../navigation-url.ts'; +import type { PublisherView } from './publishers.ts'; +export type ListingView = Readonly<{ + eventId: string; + createdAt: number; + identifier: string; + href: string; + title: string; + summary: string; + content: string; + location: string; + price: FoodProjection['price']; + quantity: FoodProjection['quantity']; + publisher: PublisherView; + lastKnown: boolean; + coverage: HeadResolution['coverage']; +}>; +// Only the genuine resolver supplies these rows. Ordering follows winner, +// deletion, future and focused admission and local matching, before pagination. +export function orderFoodRows( + rows: readonly HeadResolution[] +): readonly HeadResolution[] { + return rows.toSorted( + (a, b) => + b.state.head.created_at - a.state.head.created_at || + (a.state.head.id < b.state.head.id + ? -1 + : a.state.head.id > b.state.head.id + ? 1 + : 0) + ); +} +export function listingView( + row: HeadResolution, + publisher?: PublisherView +): ListingView | undefined { + const food = row.state.food, + head = row.state.head; + if ( + !food || + food.status !== 'active' || + row.deletion.outcome === 'suppressed' + ) + return undefined; + const naddr = encodeProductReference({ + kind: 30402, + pubkey: head.pubkey, + identifier: food.identifier + }); + const href = + naddr === undefined ? undefined : internalHref(`/products/${naddr}`); + if (href === undefined) return undefined; + return { + eventId: head.id, + createdAt: head.created_at, + identifier: food.identifier, + href, + title: food.title, + summary: food.summary, + content: food.content, + location: food.location, + price: { ...food.price }, + quantity: food.quantity === null ? null : { ...food.quantity }, + publisher: + publisher?.pubkey === head.pubkey + ? { ...publisher } + : { pubkey: head.pubkey, label: head.pubkey, assertedName: false }, + lastKnown: row.lastKnown, + coverage: row.coverage + }; +} diff --git a/web/src/lib/catalog/publishers.ts b/web/src/lib/catalog/publishers.ts @@ -0,0 +1,190 @@ +import { + PUBLIC_QUERY_BUDGETS, + PUBLIC_SEARCH_BUDGETS +} from '../config/budgets.ts'; +import { canonicalPublicKey } from '../contracts/public-key.ts'; +import { + publisherProfileQuery, + assertedProfileName +} from '../nostr/profile-query.ts'; +import type { PublicRun, PublicRequest } from '../nostr/request-scope.ts'; +import { + subscribePublicView, + publicViewProjectionAvailable, + publicViewRunCurrent, + publicViewKnownEvidence, + type PublicView +} from '../runtime/public-runtime.ts'; +import { + createPublicHeadCandidate, + publicHeadEnvelope, + publicHeadSnapshot, + selectPublicHead, + type PublicHead +} from './heads.ts'; +import { evaluatePublicHeadDeletion } from './deletions.ts'; +import { assessFutureTimestamp, type WallClock } from './clock-policy.ts'; +declare const publisherBrand: unique symbol; +export type PublisherViews = Readonly<{ readonly [publisherBrand]: true }>; +export type PublisherView = Readonly<{ + pubkey: string; + label: string; + assertedName: boolean; +}>; +interface Owner { + readonly view: PublicView; + readonly clock: WallClock; + readonly heads: () => readonly PublicHead[]; + readonly remember: (head: PublicHead) => void; + readonly reserve: ( + run: PublicRun, + authors: readonly string[], + page: number + ) => readonly string[]; +} +const owners = new WeakMap<PublisherViews, Owner>(); +function ownerOf(token: PublisherViews): Owner { + const owner = owners.get(token); + if (!owner) throw new Error('publisher_owner_invalid'); + return owner; +} +export function createPublisherViews( + view: PublicView, + clock: WallClock +): PublisherViews { + publicViewProjectionAvailable(view); + if (typeof clock.nowSeconds !== 'function') + throw new Error('publisher_clock_invalid'); + const heads = new Map<string, PublicHead>(), + attempted = new WeakMap< + PublicRun, + Readonly<{ + seen: Map<string, true>; + pages: Map<number, Map<string, true>>; + }> + >(); + const token = Object.freeze({}) as PublisherViews; + owners.set(token, { + view, + clock, + heads: () => Array.from(heads.values()), + remember(head) { + const key = publicHeadSnapshot(head).pubkey; + if ( + !heads.has(key) && + heads.size >= PUBLIC_QUERY_BUDGETS.coordinatesPerRun + ) + return; + heads.set(key, selectPublicHead(heads.get(key), head).head); + }, + reserve(run, authors, page) { + let state = attempted.get(run); + if (!state) { + state = { seen: new Map(), pages: new Map() }; + attempted.set(run, state); + } + const seen = state.seen; + let pageAuthors = state.pages.get(page); + if (!pageAuthors) { + pageAuthors = new Map(); + state.pages.set(page, pageAuthors); + } + const fresh = new Map<string, true>(); + for (const key of authors) + if (!seen.has(key)) { + if (pageAuthors.size >= PUBLIC_SEARCH_BUDGETS.publisherAuthorsPerPage) + continue; + if (seen.size >= PUBLIC_QUERY_BUDGETS.coordinatesPerRun) + throw new Error('publisher_run_limit'); + pageAuthors.set(key, true); + seen.set(key, true); + fresh.set(key, true); + } + return Array.from(fresh.keys()); + } + }); + return token; +} +// Caller selects one current local page, never every discovered publisher. +// Page reservations are cumulative across reordered rows. Admission precedes +// effects and is not refunded on failure. New explicit +// generations can refresh metadata; duplicate page reads cannot retry blindly. +export function readPublisherPage( + token: PublisherViews, + run: PublicRun, + authors: readonly string[], + page = 1 +): readonly PublicRequest[] { + if ( + !Number.isSafeInteger(page) || + page < 1 || + page > + Math.ceil( + PUBLIC_QUERY_BUDGETS.coordinatesPerRun / PUBLIC_SEARCH_BUDGETS.pageRows + ) + ) + throw new Error('publisher_page_invalid'); + const filters = publisherProfileQuery(authors), + owner = ownerOf(token); + if ( + !publicViewProjectionAvailable(owner.view) || + !publicViewRunCurrent(owner.view, run) + ) + throw new Error('publisher_run_inactive'); + const fresh = owner.reserve(run, filters[0]?.authors ?? [], page); + if (!fresh.length) return []; + const selected = new Map(fresh.map((key) => [key, true])); + const request = subscribePublicView( + owner.view, + run, + 'profile', + publisherProfileQuery(fresh), + (proof) => { + const head = createPublicHeadCandidate(proof); + if (!head) return; + const value = publicHeadSnapshot(head); + if (value.kind === 0 && selected.has(value.pubkey)) owner.remember(head); + } + ); + return [request]; +} +export function publisherSnapshot( + token: PublisherViews, + pubkey: string +): PublisherView { + if (canonicalPublicKey(pubkey) === undefined) + throw new Error('publisher_key_invalid'); + const owner = ownerOf(token), + fallback = { pubkey, label: pubkey, assertedName: false }; + if (!publicViewProjectionAvailable(owner.view)) return fallback; + const retained = owner + .heads() + .find((head) => publicHeadSnapshot(head).pubkey === pubkey); + if (!retained) return fallback; + const known = publicViewKnownEvidence(owner.view, retained), + head = known.head ?? retained; + owner.remember(head); + let now: number; + try { + now = owner.clock.nowSeconds(); + } catch { + return fallback; + } + const settled = publicViewKnownEvidence(owner.view, head); + if ( + settled.head && + publicHeadSnapshot(settled.head).id !== publicHeadSnapshot(head).id + ) + return fallback; + if ( + !publicViewProjectionAvailable(owner.view) || + assessFutureTimestamp(publicHeadSnapshot(head).created_at, now) !== + 'within_policy' || + evaluatePublicHeadDeletion(head, settled.requests).outcome === 'suppressed' + ) + return fallback; + const name = assertedProfileName(publicHeadEnvelope(head)); + return name === undefined + ? fallback + : { pubkey, label: name, assertedName: true }; +} diff --git a/web/src/lib/catalog/search-run.ts b/web/src/lib/catalog/search-run.ts @@ -1,4 +1,15 @@ import { + orderFoodRows, + listingView, + type ListingView +} from './listing-view.ts'; +import { + createPublisherViews, + readPublisherPage, + publisherSnapshot, + type PublisherViews +} from './publishers.ts'; +import { PUBLIC_QUERY_BUDGETS, PUBLIC_SEARCH_BUDGETS, PUBLIC_SEARCH_RUN_BUDGETS @@ -64,6 +75,7 @@ export type FoodSearchCoordinator = Readonly<{ export type FoodSearchRun = Readonly<{ readonly [generationBrand]: true }>; interface Coordinator { readonly view: PublicView; + readonly publishers: PublisherViews; readonly clock: WallClock; readonly current: () => FoodSearchRun | undefined; readonly reserve: (token: FoodSearchRun) => void; @@ -250,6 +262,7 @@ export function createFoodSearchCoordinator( closed = false; coordinators.set(token, { view, + publishers: createPublisherViews(view, clock), clock, current: () => generation, reserve: (value) => { @@ -537,9 +550,18 @@ export function foodSearchSnapshot( ); const continuation = session.continuation().snapshot(), page = localSearchPage( - available && matched?.ok ? matched.rows : [], + available && matched?.ok ? orderFoodRows(matched.rows) : [], session.pages() ); + const listings = new Map<string, ListingView>(); + for (const row of page.rows) { + const value = listingView( + row, + publisherSnapshot(owner.publishers, row.state.head.pubkey) + ); + if (value) listings.set(value.eventId, value); + } + if (!current(session)) return undefined; return { generation: token, query: session.query, @@ -547,6 +569,7 @@ export function foodSearchSnapshot( continuation.gap && refresh === 'bounded-eose' ? 'partial' : refresh, available, rows: page.rows, + listings: Array.from(listings.values()), hasMore: page.hasMore, continuation, scopes: sources, @@ -554,6 +577,38 @@ export function foodSearchSnapshot( definitiveAbsence: false }; } +// Explicit auxiliary work for the current local page. Show more itself stays +// local; snapshots never schedule profile transport or prompt a signer. +export function resolveFoodSearchPublishers( + token: FoodSearchRun +): readonly PublicRequest[] { + const session = sessionOf(token); + requireCurrent(session); + const snapshot = foodSearchSnapshot( + viewCoordinators.get(session.coordinator.view)!.token + ); + requireCurrent(session); + if (!snapshot?.available) throw new Error('food_search_unavailable'); + const run = foodSearchRequestOwner(token); + const page = snapshot.rows.slice( + (session.pages() - 1) * PUBLIC_SEARCH_BUDGETS.pageRows, + session.pages() * PUBLIC_SEARCH_BUDGETS.pageRows + ); + try { + const requests = readPublisherPage( + session.coordinator.publishers, + run, + page.map((row) => row.state.head.pubkey), + session.pages() + ); + for (const request of requests) session.track(request); + requireCurrent(session); + return requests; + } catch (error) { + if (current(session)) session.fail(); + throw error; + } +} export function cancelFoodSearch(token: FoodSearchRun): void { const session = sessionOf(token); requireCurrent(session); diff --git a/web/src/lib/catalog/search-state.ts b/web/src/lib/catalog/search-state.ts @@ -1,3 +1,4 @@ +import type { ListingView } from './listing-view.ts'; import type { ChronologicalProgress } from './continuation.ts'; import type { publicRunSnapshot, @@ -21,6 +22,7 @@ export type SearchSnapshot<Generation> = Readonly<{ refresh: SearchRefresh; available: boolean; rows: readonly HeadResolution[]; + listings: readonly ListingView[]; hasMore: boolean; continuation: ChronologicalProgress; scopes: readonly SearchScope[]; diff --git a/web/src/lib/config/budgets.ts b/web/src/lib/config/budgets.ts @@ -39,7 +39,8 @@ export const PUBLIC_RETENTION_BUDGETS = Object.freeze({ export const PUBLIC_SEARCH_BUDGETS = Object.freeze({ queryBytes: 512, terms: 12, - pageRows: 20 + pageRows: 20, + publisherAuthorsPerPage: 20 }); // One NIP-50 round may select each of the three qualified sources separately; diff --git a/web/src/lib/nostr/profile-query.ts b/web/src/lib/nostr/profile-query.ts @@ -0,0 +1,59 @@ +import { getProfileContent } from 'applesauce-core/helpers'; +import { canonicalPublicKey } from '../contracts/public-key.ts'; +import { + PUBLIC_QUERY_BUDGETS, + PUBLIC_SEARCH_BUDGETS +} from '../config/budgets.ts'; +import { + verifiedEnvelopeSnapshot, + type VerifiedEnvelope +} from './verified-envelope.ts'; +import type { PublicFilter } from './exports.ts'; +export function publisherProfileQuery( + authors: readonly string[] +): readonly PublicFilter[] { + if (authors.length > PUBLIC_SEARCH_BUDGETS.publisherAuthorsPerPage) + throw new Error('publisher_page_limit'); + const unique = new Map<string, true>(); + for (const author of authors) { + if (canonicalPublicKey(author) === undefined) + throw new Error('publisher_key_invalid'); + unique.set(author, true); + } + return unique.size + ? [ + { + kinds: [0], + authors: Array.from(unique.keys()), + limit: PUBLIC_QUERY_BUDGETS.requestedPerRelay + } + ] + : []; +} +// Parse a fresh genuine envelope through the qualified generic SDK. Cached SDK +// symbols, profile URLs and claimed identity fields never become authority. +export function assertedProfileName( + proof: VerifiedEnvelope +): string | undefined { + const event = verifiedEnvelopeSnapshot(proof); + if (!event || event.kind !== 0) return undefined; + try { + const profile = getProfileContent(event); + if (!profile || typeof profile !== 'object' || Array.isArray(profile)) + return undefined; + for (const value of [ + profile.display_name, + profile.displayName, + profile.name + ]) + if ( + typeof value === 'string' && + value.trim() !== '' && + value.isWellFormed() + ) + return value; + } catch { + /* Unsupported metadata leaves the selected head at key fallback. */ + } + return undefined; +} diff --git a/web/tests/integration/search-publishers.test.ts b/web/tests/integration/search-publishers.test.ts @@ -0,0 +1,232 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { once } from 'node:events'; +import { readFile } from 'node:fs/promises'; +import { finalizeEvent } from 'applesauce-core/helpers'; +import WebSocket, { WebSocketServer } from 'ws'; +import { validateRelayPolicy } from '../../src/lib/config/relays.ts'; +import { + createPublicRuntimeContext, + mountPublicRuntime, + createPublicView, + closePublicRuntime +} from '../../src/lib/runtime/public-runtime.ts'; +import { + createFoodSearchCoordinator, + beginFoodSearch, + chronologicalFoodSearch, + foodSearchSnapshot, + resolveFoodSearchPublishers, + showMoreFoodSearch, + cancelFoodSearch +} from '../../src/lib/catalog/search-run.ts'; +await test('actual SDK listings request only twenty displayed publishers, keep asserted text separate and share lifecycle budgets', async () => { + const corpus = JSON.parse( + await readFile( + new URL( + '../../../contracts/interop/food_availability/corpus.v1.json', + import.meta.url + ), + 'utf8' + ) + ) as { vectors: { id: string; signed_wires: Record<string, string> }[] }; + const base = JSON.parse( + corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous + ) as { tags: string[][]; content: string; created_at: number }; + const keys: Uint8Array[] = []; + const listings = Array.from({ length: 21 }, (_, n) => { + const key = crypto.getRandomValues(new Uint8Array(32)); + keys.push(key); + return finalizeEvent( + { + kind: 30402, + created_at: base.created_at - n, + tags: base.tags.map((t) => + t[0] === 'd' ? ['d', `listing_${n}`] : [...t] + ), + content: base.content + }, + key + ); + }); + const profiles = keys.map((key, n) => + finalizeEvent( + { + kind: 0, + created_at: base.created_at, + tags: [], + content: JSON.stringify({ + display_name: `<script>Farmer ${n}</script>`, + pubkey: 'forged', + nip05: 'identity@example.org', + picture: 'https://tracking.example.org' + }) + }, + key + ) + ); + for (const key of keys) key.fill(0); + const origin = 'wss://one.example.org', + server = new WebSocketServer({ + host: '127.0.0.1', + port: 0, + maxPayload: 65536 + }); + const sockets: WebSocket[] = [], + filters: Record<string, unknown>[] = []; + server.on('connection', (socket) => { + sockets.push(socket); + socket.on('message', (bytes) => { + const buffer = Array.isArray(bytes) + ? Buffer.concat(bytes) + : Buffer.isBuffer(bytes) + ? bytes + : Buffer.from(bytes); + const frame = JSON.parse(buffer.toString('utf8')) as unknown[]; + if (frame[0] !== 'REQ') return; + const id = String(frame[1]), + queries = frame.slice(2) as Record<string, unknown>[]; + filters.push(...queries); + const kind = (queries[0].kinds as number[])[0]; + const matches = (pubkey: string, identifier?: string) => + queries.some((q) => { + const authors = q.authors as string[] | undefined; + return ( + !authors || + (authors.includes(pubkey) && + (identifier === undefined || + (q['#d'] as string[]).includes(identifier))) + ); + }); + const values = + kind === 0 + ? profiles.filter((v) => matches(v.pubkey)) + : kind === 30402 + ? listings.filter((v) => + matches(v.pubkey, v.tags.find((t) => t[0] === 'd')![1]) + ) + : []; + for (const value of values) + socket.send(JSON.stringify(['EVENT', id, value])); + socket.send(JSON.stringify(['EOSE', id])); + }); + }); + await once(server, 'listening'); + const address = server.address(); + assert.ok(address && typeof address !== 'string'); + const port = address.port; + const oldWindow = Object.getOwnPropertyDescriptor(globalThis, 'window'), + oldSocket = globalThis.WebSocket; + class FixtureSocket extends WebSocket { + constructor(url: string | URL) { + assert.equal(String(url).replace(/\/$/, ''), origin); + super(`ws://127.0.0.1:${port}`); + } + } + Object.defineProperty(globalThis, 'window', { + configurable: true, + value: {} + }); + globalThis.WebSocket = + FixtureSocket as unknown as typeof globalThis.WebSocket; + const context = createPublicRuntimeContext(); + try { + const policy = validateRelayPolicy( + JSON.stringify({ + schemaVersion: 1, + public: [{ origin, read: true, write: false, nip50: false }], + inbox: [], + postingEnabled: false, + messagingEnabled: false, + operatorDenylist: [] + }) + )!; + const view = createPublicView(mountPublicRuntime(context, policy)!); + const coordinator = createFoodSearchCoordinator(view, { + nowSeconds: () => base.created_at + 10 + }), + token = beginFoodSearch(coordinator, ''); + chronologicalFoodSearch(token); + for ( + let n = 0; + n < 200 && foodSearchSnapshot(coordinator)?.refresh !== 'bounded-eose'; + n++ + ) + await new Promise((resolve) => setTimeout(resolve, 5)); + const before = foodSearchSnapshot(coordinator)!; + assert.equal(before.listings.length, 20); + assert.equal(before.hasMore, true); + assert.deepEqual( + before.listings.map((v) => v.eventId), + listings.slice(0, 20).map((v) => v.id) + ); + assert.ok( + before.listings.every((v) => v.publisher.label === v.publisher.pubkey) + ); + resolveFoodSearchPublishers(token); + resolveFoodSearchPublishers(token); + for ( + let n = 0; + n < 200 && + !foodSearchSnapshot(coordinator)?.listings.every( + (v) => v.publisher.assertedName + ); + n++ + ) + await new Promise((resolve) => setTimeout(resolve, 5)); + const first = foodSearchSnapshot(coordinator)!; + const profileQueries = filters.filter( + (q) => (q.kinds as number[])[0] === 0 + ); + assert.equal(profileQueries.length, 1); + assert.equal((profileQueries[0].authors as string[]).length, 20); + assert.equal(profileQueries[0].limit, 200); + assert.deepEqual( + first.listings.map((v) => v.publisher.pubkey), + listings.slice(0, 20).map((v) => v.pubkey) + ); + assert.ok( + first.listings.every( + (v) => + v.publisher.assertedName && v.publisher.label.startsWith('<script>') + ) + ); + assert.equal(first.run?.ingress.deliveries, 62); + assert.equal(first.run?.activeRequests, 0); + const count = filters.length; + showMoreFoodSearch(token); + assert.equal(filters.length, count); + assert.equal(foodSearchSnapshot(coordinator)!.listings.length, 21); + resolveFoodSearchPublishers(token); + for ( + let n = 0; + n < 200 && + !foodSearchSnapshot(coordinator)?.listings[20].publisher.assertedName; + n++ + ) + await new Promise((resolve) => setTimeout(resolve, 5)); + const all = foodSearchSnapshot(coordinator)!; + assert.equal(all.run?.ingress.deliveries, 63); + const queries = filters.filter((q) => (q.kinds as number[])[0] === 0); + assert.equal(queries.length, 2); + assert.deepEqual(queries[1].authors, [listings[20].pubkey]); + assert.equal(all.definitiveAbsence, false); + assert.ok( + all.listings.every( + (v) => v.price.amount === before.listings[0].price.amount + ) + ); + cancelFoodSearch(token); + assert.throws(() => resolveFoodSearchPublishers(token)); + } finally { + closePublicRuntime(context); + for (const key of keys) key.fill(0); + globalThis.WebSocket = oldSocket; + if (oldWindow) Object.defineProperty(globalThis, 'window', oldWindow); + else delete (globalThis as { window?: unknown }).window; + for (const socket of sockets) socket.terminate(); + await new Promise<void>((resolve, reject) => + server.close((e) => (e ? reject(e) : resolve())) + ); + } +}); diff --git a/web/tests/unit/listing-view.test.ts b/web/tests/unit/listing-view.test.ts @@ -0,0 +1,209 @@ +import { afterAll, describe, expect, it } from 'vitest'; +import { readFileSync } from 'node:fs'; +import { finalizeEvent } from 'applesauce-core/helpers'; +import { searchResolvedFood } from '../../src/lib/catalog/search-match.ts'; +import { + createPublicHeadCandidate, + publicHeadEnvelope +} from '../../src/lib/catalog/heads.ts'; +import { + createHeadResolver, + resolveHeads, + headResolutionSnapshot +} from '../../src/lib/catalog/resolve-head.ts'; +import { + verifyEnvelope, + verifiedEnvelopeSnapshot +} from '../../src/lib/nostr/verified-envelope.ts'; +import { + createPublicScheduler, + createPublicRun, + openPublicRequest, + closePublicScheduler +} from '../../src/lib/nostr/request-scope.ts'; +import { validateRelayPolicy } from '../../src/lib/config/relays.ts'; +import { + createPublicRetention, + retainPublicEnvelope, + publicRetentionKnown +} from '../../src/lib/catalog/retention.ts'; +const corpus = JSON.parse( + readFileSync( + new URL( + '../../../contracts/interop/food_availability/corpus.v1.json', + import.meta.url + ), + 'utf8' + ) +) as { vectors: { id: string; signed_wires: Record<string, string> }[] }; +const base = JSON.parse( + corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous +) as { tags: string[][]; content: string; created_at: number }; +const cleanup: (() => void)[] = []; +afterAll(() => { + for (const stop of cleanup) stop(); +}); +function fixture() { + const key = crypto.getRandomValues(new Uint8Array(32)); + cleanup.push(() => key.fill(0)); + const proof = ( + time = base.created_at, + tags = base.tags, + content = base.content, + kind = 30402 + ) => { + const p = verifyEnvelope( + JSON.stringify( + finalizeEvent({ kind, created_at: time, tags, content }, key) + ) + ); + if (!p.ok) throw Error('search fixture'); + return p.value; + }; + const head = (p: ReturnType<typeof proof>) => { + const h = createPublicHeadCandidate(p); + if (!h) throw Error('search head'); + return h; + }; + const policy = validateRelayPolicy( + JSON.stringify({ + schemaVersion: 1, + public: [], + inbox: [], + postingEnabled: false, + messagingEnabled: false, + operatorDenylist: [] + }) + )!; + const scheduler = createPublicScheduler({ + now: () => 0, + schedule: () => () => {} + }), + run = createPublicRun(scheduler, policy); + cleanup.push(() => closePublicScheduler(scheduler)); + const retention = createPublicRetention(); + let available = true, + now = base.created_at + 10, + assessments = 0; + const model = createHeadResolver( + run, + (kind, filters, next) => openPublicRequest(run, kind, () => () => {}, next), + { + nowSeconds: () => { + assessments++; + return now; + } + }, + { + retain: (heads) => { + for (const h of heads) + retainPublicEnvelope(retention, publicHeadEnvelope(h)); + return true; + }, + read: (h) => publicRetentionKnown(retention, h), + available: () => available + } + ); + return { + proof, + head, + model, + retention, + add: (p: ReturnType<typeof proof>) => resolveHeads(model, [head(p)]), + retain: (p: ReturnType<typeof proof>) => retainPublicEnvelope(retention, p), + unavailable: () => { + available = false; + }, + clock: (value: number) => { + now = value; + }, + clockSamples: () => assessments + }; +} +import { + orderFoodRows, + listingView +} from '../../src/lib/catalog/listing-view.ts'; +describe('exact advertised listing views', () => { + it('keeps signed text and decimals beyond safe integers, and unknown quantity', () => { + const f = fixture(); + const tags = base.tags + .filter((t) => t[0] !== 'radroots:quantity') + .map((t) => + t[0] === 'price' + ? ['price', '9007199254740993.123456789', 'CAD'] + : t[0] === 'title' + ? ['title', '<script>CARROTS</script>'] + : [...t] + ); + const p = f.proof(base.created_at, tags); + f.add(p); + const row = headResolutionSnapshot(f.model)[0]; + const value = listingView(row)!; + expect(value.title).toBe('<script>CARROTS</script>'); + expect(value.price).toEqual(row.state.food!.price); + expect(value.price.amount).toBe('9007199254740993.123456789'); + expect(value.quantity).toBeNull(); + expect(value.href).toMatch(/^\/products\/naddr1/); + expect(value.href).not.toContain('?'); + (value.price as { amount: string }).amount = '0'; + expect(listingView(row)!.price.amount).toBe('9007199254740993.123456789'); + expect(verifiedEnvelopeSnapshot(p)?.content).toBe(base.content); + }); + it('orders revision time then lower event ID without mutating the input', () => { + const f = fixture(); + for (const [d, t] of [ + ['a', 0], + ['b', 1], + ['c', 1] + ] as const) + f.add( + f.proof( + base.created_at + t, + base.tags.map((v) => (v[0] === 'd' ? ['d', d] : [...v])) + ) + ); + const rows = headResolutionSnapshot(f.model), + before = rows.map((v) => v.state.head.id); + const ordered = orderFoodRows(rows); + expect(ordered[0].state.head.created_at).toBe(base.created_at + 1); + expect(ordered[0].state.head.id < ordered[1].state.head.id).toBe(true); + expect(rows.map((v) => v.state.head.id)).toEqual(before); + }); + it('does not promote older keyword matches after sold, renamed or withdrawn latest evidence', () => { + for (const outcome of ['sold', 'renamed', 'withdrawn']) { + const f = fixture(), + old = f.proof(); + f.add(old); + const tags = base.tags.map((t) => + outcome === 'sold' && t[0] === 'status' + ? ['status', 'sold'] + : outcome === 'renamed' && t[0] === 'title' + ? ['title', 'Celery'] + : [...t] + ); + const newer = f.proof( + base.created_at + 1, + tags, + outcome === 'renamed' ? 'Celery' : base.content + ); + f.retain(newer); + if (outcome === 'withdrawn') + f.retain( + f.proof( + base.created_at, + [['e', verifiedEnvelopeSnapshot(newer)!.id]], + '', + 5 + ) + ); + const matched = searchResolvedFood( + f.model, + outcome === 'renamed' ? 'nantes' : '' + ); + expect(matched.ok && matched.rows).toEqual([]); + if (outcome !== 'renamed') + expect(listingView(headResolutionSnapshot(f.model)[0])).toBeUndefined(); + } + }); +}); diff --git a/web/tests/unit/profile-query.test.ts b/web/tests/unit/profile-query.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest'; +import { finalizeEvent } from 'applesauce-core/helpers'; +import { publisherProfileQuery } from '../../src/lib/nostr/profile-query.ts'; +function author() { + const key = crypto.getRandomValues(new Uint8Array(32)); + try { + return finalizeEvent( + { kind: 0, created_at: 1, tags: [], content: '{}' }, + key + ).pubkey; + } finally { + key.fill(0); + } +} +describe('bounded publisher profile query', () => { + it('coalesces twenty page authors into one bounded generic metadata filter', () => { + const authors = Array.from({ length: 20 }, () => author()); + expect(publisherProfileQuery(authors)).toEqual([ + { kinds: [0], authors, limit: 200 } + ]); + expect(publisherProfileQuery([authors[0], authors[0]])).toEqual([ + { kinds: [0], authors: [authors[0]], limit: 200 } + ]); + const query = publisherProfileQuery(authors); + query[0].authors![0] = authors[1]; + expect(publisherProfileQuery(authors)[0].authors![0]).toBe(authors[0]); + expect(publisherProfileQuery([])).toEqual([]); + }); + it('rejects oversized or malformed admission before request effects', () => { + const key = author(); + for (const values of [ + Array.from({ length: 21 }, () => key), + ['wrong'], + [key.toUpperCase()], + ['0'.repeat(64)] + ]) + expect(() => publisherProfileQuery(values)).toThrow(); + }); +}); diff --git a/web/tests/unit/publishers.test.ts b/web/tests/unit/publishers.test.ts @@ -0,0 +1,391 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { readFileSync } from 'node:fs'; +import { finalizeEvent } from 'applesauce-core/helpers'; +import type { PublicPoolMessage } from '../../src/lib/nostr/exports.ts'; +import type { RequestClock } from '../../src/lib/nostr/request-scope.ts'; +let search: typeof import('../../src/lib/catalog/search-run.ts'); +let runtime: typeof import('../../src/lib/runtime/public-runtime.ts'); +let context: ReturnType<typeof runtime.createPublicRuntimeContext>; +const keys: Uint8Array[] = []; +beforeEach(async () => { + vi.resetModules(); + vi.stubGlobal('window', {}); + search = await import('../../src/lib/catalog/search-run.ts'); + runtime = await import('../../src/lib/runtime/public-runtime.ts'); +}); +afterEach(() => { + if (context) runtime.closePublicRuntime(context); + for (const key of keys.splice(0)) key.fill(0); + vi.restoreAllMocks(); + vi.unstubAllGlobals(); +}); +const corpus = JSON.parse( + readFileSync( + new URL( + '../../../contracts/interop/food_availability/corpus.v1.json', + import.meta.url + ), + 'utf8' + ) +) as { vectors: { id: string; signed_wires: Record<string, string> }[] }; +const base = JSON.parse( + corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous +) as { tags: string[][]; content: string; created_at: number }; +async function fixture(nip50 = false, sourceCount = 1) { + const { RelayPool } = await import('applesauce-relay/pool'); + const { validateRelayPolicy } = + await import('../../src/lib/config/relays.ts'); + const { verifyEnvelope } = + await import('../../src/lib/nostr/verified-envelope.ts'); + const { createPublicHeadCandidate } = + await import('../../src/lib/catalog/heads.ts'); + const channels: { + emit: (value: PublicPoolMessage) => void; + stopped: () => boolean; + }[] = []; + const provider = vi.spyOn(RelayPool.prototype, 'req').mockImplementation( + () => + ({ + subscribe(subscriber: { next?: (value: PublicPoolMessage) => void }) { + let stopped = false; + channels.push({ + emit: (value: PublicPoolMessage) => { + if (!stopped) subscriber.next?.(value); + }, + stopped: () => stopped + }); + return { + get closed() { + return stopped; + }, + unsubscribe() { + stopped = true; + } + }; + } + }) as unknown as ReturnType<InstanceType<typeof RelayPool>['req']> + ); + const origin = 'wss://one.example.org'; + const policy = validateRelayPolicy( + JSON.stringify({ + schemaVersion: 1, + public: Array.from({ length: sourceCount }, (_, n) => ({ + origin: n === 0 ? origin : `wss://source${n}.example.org`, + read: true, + write: false, + nip50 + })), + inbox: [], + postingEnabled: false, + messagingEnabled: false, + operatorDenylist: [] + }) + )!; + const key = crypto.getRandomValues(new Uint8Array(32)); + keys.push(key); + const wire = ( + title = 'Carrots', + d?: string, + time = base.created_at, + content = base.content + ) => + finalizeEvent( + { + kind: 30402, + created_at: time, + content, + tags: base.tags.map((tag) => + tag[0] === 'title' + ? ['title', title] + : tag[0] === 'd' && d !== undefined + ? ['d', d] + : [...tag] + ) + }, + key + ); + const head = (event = wire()) => { + const p = verifyEnvelope(JSON.stringify(event)); + if (!p.ok) throw Error('fixture proof'); + const h = createPublicHeadCandidate(p.value); + if (!h) throw Error('fixture head'); + return h; + }; + let now = 0, + reads = 0; + let onNow = () => {}; + const timers = new Map<() => void, { at: number; callback: () => void }>(); + const clock: RequestClock = { + now: () => { + reads++; + onNow(); + return now; + }, + schedule: (callback, delay) => { + const cancel = () => { + timers.delete(cancel); + }; + timers.set(cancel, { at: now + delay, callback }); + return cancel; + } + }; + context = runtime.createPublicRuntimeContext(); + const owner = runtime.mountPublicRuntime(context, policy, clock)!; + const view = runtime.createPublicView(owner); + const wallClock = { + nowSeconds: () => base.created_at + 10 + }; + const coordinator = search.createFoodSearchCoordinator(view, wallClock); + const begin = (query = '', heads: readonly ReturnType<typeof head>[] = []) => + search.beginFoodSearch(coordinator, query, heads); + const snapshot = () => search.foodSearchSnapshot(coordinator)!; + const emit = (i: number, event: ReturnType<typeof wire>) => + channels[i].emit({ type: 'EVENT', id: 'fixture', from: origin, event }); + return { + eose: (i: number) => { + const selected = provider.mock.calls[i][0]; + if (!Array.isArray(selected)) throw new Error('fixture source array'); + for (const source of selected) + channels[i].emit({ type: 'EOSE', id: 'fixture', from: String(source) }); + }, + coordinator, + view, + wallClock, + begin, + snapshot, + head, + wire, + emit, + channels, + provider, + origin, + timers, + reads: () => reads, + reenter: (callback: () => void) => { + onNow = callback; + }, + advance: (value: number) => { + now = value; + for (const timer of [...timers.values()]) + if (timer.at <= now) timer.callback(); + } + }; +} +let publishers: typeof import('../../src/lib/catalog/publishers.ts'); +beforeEach(async () => { + publishers = await import('../../src/lib/catalog/publishers.ts'); +}); +describe('genuine bounded public publisher views', () => { + it('coalesces displayed authors and preserves key fallback without an account', async () => { + const f = await fixture(), + token = f.begin(), + key = f.wire().pubkey; + const owner = publishers.createPublisherViews(f.view, f.wallClock), + run = search.foodSearchRequestOwner(token); + const reads = publishers.readPublisherPage(owner, run, [key, key]); + expect(reads).toHaveLength(1); + expect(f.provider).toHaveBeenCalledTimes(1); + expect(publishers.readPublisherPage(owner, run, [key])).toEqual([]); + expect(publishers.publisherSnapshot(owner, key)).toEqual({ + pubkey: key, + label: key, + assertedName: false + }); + const profile = finalizeEvent( + { + kind: 0, + created_at: base.created_at, + tags: [], + content: JSON.stringify({ + display_name: '<b>Farmer</b>', + pubkey: 'attacker', + nip05: 'claims@example.org', + picture: 'https://tracking.example.org' + }) + }, + keys[0] + ); + f.emit(0, profile); + expect(publishers.publisherSnapshot(owner, key)).toEqual({ + pubkey: key, + label: '<b>Farmer</b>', + assertedName: true + }); + expect(f.provider).toHaveBeenCalledTimes(1); + expect(f.snapshot().run?.ingress.deliveries).toBe(1); + }); + it('selects generic latest before unsupported/future parsing and never resurrects an old name', async () => { + const f = await fixture(), + token = f.begin(), + key = f.wire().pubkey; + const owner = publishers.createPublisherViews(f.view, f.wallClock); + publishers.readPublisherPage(owner, search.foodSearchRequestOwner(token), [ + key + ]); + const send = (time: number, content: string) => + f.emit( + 0, + finalizeEvent({ kind: 0, created_at: time, tags: [], content }, keys[0]) + ); + send(base.created_at, '{"name":"Old"}'); + expect(publishers.publisherSnapshot(owner, key).label).toBe('Old'); + send(base.created_at + 1, '{"name":123}'); + expect(publishers.publisherSnapshot(owner, key).label).toBe(key); + send(base.created_at, '{"name":"Old"}'); + expect(publishers.publisherSnapshot(owner, key).label).toBe(key); + send(base.created_at + 311, '{"name":"Future"}'); + expect(publishers.publisherSnapshot(owner, key).label).toBe(key); + }); + it('ignores corrupt signatures and nonrequested authors, and rejects a foreign run before effects', async () => { + const f = await fixture(), + token = f.begin(), + key = f.wire().pubkey; + const owner = publishers.createPublisherViews(f.view, f.wallClock), + run = search.foodSearchRequestOwner(token); + publishers.readPublisherPage(owner, run, [key]); + const profile = finalizeEvent( + { + kind: 0, + created_at: base.created_at, + tags: [], + content: '{"name":"Spoof"}' + }, + keys[0] + ); + f.emit(0, { ...profile, id: '0'.repeat(64) }); + const other = crypto.getRandomValues(new Uint8Array(32)); + keys.push(other); + f.emit( + 0, + finalizeEvent( + { + kind: 0, + created_at: base.created_at, + tags: [], + content: '{"name":"Other"}' + }, + other + ) + ); + expect(publishers.publisherSnapshot(owner, key).label).toBe(key); + expect(() => + publishers.readPublisherPage(owner, {} as typeof run, [key]) + ).toThrow(); + expect(f.provider).toHaveBeenCalledTimes(1); + search.cancelFoodSearch(token); + f.emit(0, profile); + expect(publishers.publisherSnapshot(owner, key).label).toBe(key); + expect(f.channels[0].stopped()).toBe(true); + }); + it('reserves a failed provider read without retry or losing known food', async () => { + const f = await fixture(), + token = f.begin('', [f.head()]), + key = f.wire().pubkey; + const owner = publishers.createPublisherViews(f.view, f.wallClock), + run = search.foodSearchRequestOwner(token); + f.provider.mockImplementationOnce(() => { + throw Error('isolated provider failure'); + }); + expect(() => publishers.readPublisherPage(owner, run, [key])).toThrow(); + expect(publishers.readPublisherPage(owner, run, [key])).toEqual([]); + expect(publishers.publisherSnapshot(owner, key)).toEqual({ + pubkey: key, + label: key, + assertedName: false + }); + expect(f.snapshot().rows).toHaveLength(1); + expect(f.provider).toHaveBeenCalledTimes(3); + expect(search.foodSearchRequestOwner(token)).toBe(run); + }); + it('does not publish a stale name when an injected clock delivers a newer unsupported head', async () => { + const f = await fixture(), + token = f.begin(), + key = f.wire().pubkey; + const owner = publishers.createPublisherViews(f.view, f.wallClock); + publishers.readPublisherPage(owner, search.foodSearchRequestOwner(token), [ + key + ]); + f.emit( + 0, + finalizeEvent( + { + kind: 0, + created_at: base.created_at, + tags: [], + content: '{"name":"Old"}' + }, + keys[0] + ) + ); + const newer = finalizeEvent( + { + kind: 0, + created_at: base.created_at + 1, + tags: [], + content: '{"name":123}' + }, + keys[0] + ); + let armed = true; + f.wallClock.nowSeconds = () => { + if (armed) { + armed = false; + f.emit(0, newer); + } + return base.created_at + 10; + }; + expect(publishers.publisherSnapshot(owner, key).label).toBe(key); + }); + it('enforces twenty publisher admissions per page across row reordering', async () => { + const f = await fixture(), + token = f.begin(), + owner = publishers.createPublisherViews(f.view, f.wallClock), + run = search.foodSearchRequestOwner(token); + const authors = Array.from({ length: 21 }, () => { + const key = crypto.getRandomValues(new Uint8Array(32)); + keys.push(key); + return finalizeEvent( + { kind: 0, created_at: base.created_at, tags: [], content: '{}' }, + key + ).pubkey; + }); + publishers.readPublisherPage(owner, run, authors.slice(0, 20)); + expect(publishers.readPublisherPage(owner, run, [authors[20]])).toEqual([]); + expect(f.provider).toHaveBeenCalledTimes(1); + expect(publishers.publisherSnapshot(owner, authors[20]).label).toBe( + authors[20] + ); + publishers.readPublisherPage(owner, run, [authors[20]], 2); + expect(f.provider).toHaveBeenCalledTimes(2); + for (const page of [0, 101, 1.5, NaN]) + expect(() => + publishers.readPublisherPage(owner, run, [authors[20]], page) + ).toThrow('publisher_page_invalid'); + expect(f.provider).toHaveBeenCalledTimes(2); + }); + it('retains fallback after a failed read and shares all global work and deadlines', async () => { + const f = await fixture(), + token = f.begin(), + key = f.wire().pubkey; + const owner = publishers.createPublisherViews(f.view, f.wallClock), + run = search.foodSearchRequestOwner(token); + publishers.readPublisherPage(owner, run, [key]); + const profile = finalizeEvent( + { + kind: 0, + created_at: base.created_at, + tags: [], + content: '{"name":"Known"}' + }, + keys[0] + ); + for (let n = 0; n < 2000; n++) f.emit(0, profile); + expect(f.snapshot().run?.ingress).toMatchObject({ + deliveries: 2000, + stopped: true + }); + expect(f.channels[0].stopped()).toBe(true); + expect(() => publishers.readPublisherPage(owner, run, [key])).toThrow(); + expect(publishers.publisherSnapshot(owner, key).label).toBe('Known'); + }); +}); diff --git a/web/tests/unit/search-run.test.ts b/web/tests/unit/search-run.test.ts @@ -174,6 +174,66 @@ async function fixture(nip50 = false, sourceCount = 1) { }; } describe('finite food search coordination (isolated SDK provider, not qualification)', () => { + it('enforces twenty publisher admissions per page across row reordering', async () => { + const f = await fixture(); + const values = Array.from({ length: 21 }, (_, n) => { + const key = crypto.getRandomValues(new Uint8Array(32)); + keys.push(key); + return finalizeEvent( + { + kind: 30402, + created_at: base.created_at + (n === 20 ? 1 : 0), + content: base.content, + tags: base.tags.map((t) => + t[0] === 'd' ? ['d', `page_${n}`] : [...t] + ) + }, + key + ); + }); + const token = f.begin( + '', + values.slice(0, 20).map((v) => f.head(v)) + ); + f.eose(0); + f.eose(1); + search.resolveFoodSearchPublishers(token); + f.eose(2); + search.chronologicalFoodSearch(token); + f.emit(3, values[20]); + f.snapshot(); + f.eose(4); + f.eose(5); + f.eose(3); + expect(f.snapshot().rows[0].state.head.id).toBe(values[20].id); + expect(search.resolveFoodSearchPublishers(token)).toEqual([]); + const profiles = f.provider.mock.calls.flatMap(([, input]) => + Array.isArray(input) && input[0].kinds?.[0] === 0 ? [input[0]] : [] + ); + expect(profiles).toHaveLength(1); + expect(profiles[0].authors).toHaveLength(20); + expect(f.snapshot().listings[0].publisher).toEqual({ + pubkey: values[20].pubkey, + label: values[20].pubkey, + assertedName: false + }); + }); + it('orders final verified revisions before selecting the first twenty rows', async () => { + const f = await fixture(); + const values = Array.from({ length: 26 }, (_, n) => + f.wire('Carrots', `ordered_${n}`, base.created_at - 26 + n) + ); + f.begin( + 'carrots', + values.map((value) => f.head(value)) + ); + expect(f.snapshot().rows.map((row) => row.state.head.id)).toEqual( + values + .toReversed() + .slice(0, 20) + .map((value) => value.id) + ); + }); it('reserves at most two chronological windows even after clean EOSE', async () => { const f = await fixture(), token = f.begin('');