selling-index.ts (7562B)
1 // HC_TEST_ONLY_RELAY/QUALIFICATION_PORT: original installed provider, native 2 // IndexedDB and bounded SDK loopback. Controlled observations are not live Q. 3 import { mount, unmount } from 'svelte'; 4 import Selling from '../../../src/routes/selling/+page.svelte'; 5 import { createFixture as publicationFixture } from './listing-operation-view.ts'; 6 import { makeFixture } from './approved-signing.ts'; 7 import { fixturePolicy } from './inbox-setup.ts'; 8 import { 9 createIdentityViewContext, 10 mountIdentityView, 11 connectIdentityView, 12 disconnectIdentityView, 13 closeIdentityView, 14 IDENTITY_VIEW_CONTEXT 15 } from '../../../src/lib/runtime/view-context.ts'; 16 import { 17 createPublicRuntimeContext, 18 mountPublicRuntime, 19 closePublicRuntime, 20 PUBLIC_RUNTIME_CONTEXT 21 } from '../../../src/lib/runtime/public-runtime.ts'; 22 import { 23 openBrowserDatabase, 24 closeBrowserDatabase, 25 browserDatabaseTransaction 26 } from '../../../src/lib/persistence/database.ts'; 27 import { 28 createPublicDraftRepository, 29 createPublicDraft, 30 readPublicDraft, 31 savePublicDraft 32 } from '../../../src/lib/persistence/drafts.ts'; 33 import type { PublicDraftForm } from '../../../src/lib/contracts/local-records.ts'; 34 import type { NostrEvent } from 'applesauce-core/helpers'; 35 import { buildFoodTemplate } from '../../../src/lib/contracts/food-availability-v1/write.ts'; 36 37 export async function createFixture(mode: 'empty' | 'retained' = 'retained') { 38 const original = mode === 'retained' ? await publicationFixture() : undefined; 39 const signer = mode === 'empty' ? makeFixture() : undefined; 40 const prior = Object.getOwnPropertyDescriptor(window, 'nostr'); 41 if (signer) 42 Object.defineProperty(window, 'nostr', { 43 configurable: true, 44 value: { getPublicKey: () => Promise.resolve(signer.owner) } 45 }); 46 if (original && (await original.signed()) !== 'prepared') 47 throw Error('original signed source missing'); 48 const row = await original?.row(); 49 const event = row?.artifact 50 ? (JSON.parse(row.artifact.wire) as NostrEvent) 51 : undefined; 52 const owner = original?.owner ?? signer?.owner; 53 if (!owner) throw Error('controlled owner missing'); 54 const context = createIdentityViewContext(); 55 mountIdentityView(context); 56 await connectIdentityView(context); 57 const publicContext = createPublicRuntimeContext(); 58 mountPublicRuntime(publicContext, fixturePolicy()); 59 const opened = await openBrowserDatabase(); 60 if (opened.state !== 'ready') throw Error(opened.reason); 61 const database = opened.owner; 62 let panel: ReturnType<typeof mount> | undefined; 63 let target: HTMLElement | undefined; 64 let switched: ReturnType<typeof makeFixture> | undefined; 65 return { 66 owner, 67 event, 68 command: original?.command, 69 remoteListing() { 70 if (!signer) throw Error('remote-only installed owner missing'); 71 const now = Math.floor(Date.now() / 1000); 72 const built = buildFoodTemplate({ 73 identifier: 'remote-only-food', 74 title: 'Remote own eggs', 75 description: 'Full external own listing description', 76 location: 'External farm', 77 amount: '6', 78 currency: 'CAD', 79 unit: 'each', 80 created_at: now, 81 published_at: now, 82 status: 'active' 83 }); 84 if (!built.ok) throw Error('focused external fixture invalid'); 85 return signer.sign({ 86 kind: 30402, 87 created_at: now, 88 tags: built.wire_parts.tags.map((tag) => [...tag]), 89 content: built.wire_parts.content 90 }); 91 }, 92 async switchOwner() { 93 switched = makeFixture(); 94 const next = switched.owner; 95 Object.defineProperty(window, 'nostr', { 96 configurable: true, 97 value: { getPublicKey: () => Promise.resolve(next) } 98 }); 99 await connectIdentityView(context); 100 return next; 101 }, 102 async render() { 103 if (panel) await unmount(panel); 104 target?.remove(); 105 target = document.createElement('div'); 106 target.id = 'hcp121-selling'; 107 document.body.append(target); 108 panel = mount(Selling, { 109 target, 110 context: new Map<string, unknown>([ 111 [IDENTITY_VIEW_CONTEXT, context], 112 [PUBLIC_RUNTIME_CONTEXT, publicContext] 113 ]) 114 }); 115 }, 116 async external() { 117 await original?.externalSource(); 118 const next = await original?.row(); 119 return next?.artifact 120 ? (JSON.parse(next.artifact.wire) as NostrEvent) 121 : undefined; 122 }, 123 async observed( 124 status: 'accepted' | 'refused' | 'timed_out', 125 target: number, 126 readback: boolean 127 ) { 128 await original?.evidence(status, target, readback); 129 }, 130 async settled() { 131 if (!original) throw Error('original operation missing'); 132 const current = await original.row(); 133 for (let i = 0; i < current.capture.targets.length; i++) 134 await original.evidence('accepted', i, true); 135 }, 136 async draft(title = 'Unfinished spinach') { 137 const repository = createPublicDraftRepository(database, owner); 138 if (!repository) throw Error('original draft repository missing'); 139 const form: PublicDraftForm = { 140 title, 141 description: '', 142 location: '', 143 amount: '', 144 currency: '', 145 unit: '', 146 quantity: '', 147 contactType: '', 148 contactValue: '' 149 }; 150 const saved = await createPublicDraft(repository, form); 151 if (!saved.ok) throw Error(saved.reason); 152 return saved.value; 153 }, 154 async changeCapturedDraft() { 155 const row = await original?.row(); 156 if (!row || row.source.type !== 'draft') 157 throw Error('original draft capture missing'); 158 const repository = createPublicDraftRepository(database, owner); 159 if (!repository) throw Error('original draft repository missing'); 160 const read = await readPublicDraft(repository, row.source.id); 161 if (!read.ok) throw Error(read.reason); 162 const saved = await savePublicDraft( 163 repository, 164 read.value.id, 165 read.value.revision, 166 { 167 ...read.value.form, 168 title: 'Unfinished proposed carrots', 169 description: '' 170 } 171 ); 172 if (!saved.ok) throw Error(saved.reason); 173 return saved.value; 174 }, 175 async records() { 176 const transaction = browserDatabaseTransaction( 177 database, 178 ['public_drafts', 'public_operations'], 179 'readonly' 180 ); 181 const drafts = transaction.objectStore('public_drafts').getAll(); 182 const operations = transaction.objectStore('public_operations').getAll(); 183 return await new Promise<{ drafts: unknown[]; operations: unknown[] }>( 184 (resolve, reject) => { 185 transaction.addEventListener('complete', () => 186 resolve({ drafts: drafts.result, operations: operations.result }) 187 ); 188 transaction.addEventListener('abort', () => 189 reject(Error('native seller read failed')) 190 ); 191 } 192 ); 193 }, 194 counts: () => original?.counts() ?? { signs: 0, keys: 0 }, 195 reconnect() { 196 return connectIdentityView(context); 197 }, 198 disconnect() { 199 disconnectIdentityView(context); 200 }, 201 async close() { 202 if (panel) await unmount(panel); 203 target?.remove(); 204 closePublicRuntime(publicContext); 205 closeIdentityView(context); 206 closeBrowserDatabase(database); 207 original?.close(); 208 signer?.close(); 209 switched?.close(); 210 if (signer) { 211 if (prior) Object.defineProperty(window, 'nostr', prior); 212 else Reflect.deleteProperty(window, 'nostr'); 213 } 214 } 215 }; 216 }