private-preparation.ts (7212B)
1 import { makeFixture as makePairedFixture } from './paired-delivery.ts'; 2 import { reservedSendRumorWire } from '../../../src/lib/messaging/send-identity.ts'; 3 import { disconnectIdentity } from '../../../src/lib/runtime/identity-session.ts'; 4 import { captureEnquiryContext } from '../../../src/lib/messaging/enquiry-context.ts'; 5 import { verifyEnvelope } from '../../../src/lib/nostr/verified-envelope.ts'; 6 import type { EventTemplate, finalizeEvent } from 'applesauce-core/helpers'; 7 import { 8 createPrivateComposer, 9 updatePrivateComposerText, 10 privateComposerText, 11 privateComposerSnapshot, 12 bindPrivateComposerPreparation, 13 closePrivateComposer 14 } from '../../../src/lib/messaging/composer-state.ts'; 15 import { 16 captureDirtyNavigation, 17 requestPrivateNavigation, 18 confirmDiscardNavigation, 19 keepPrivateEditing, 20 dirtyNavigationSnapshot, 21 privateNavigationBeforeUnload, 22 type DirtyNavigation 23 } from '../../../src/lib/runtime/dirty-navigation.ts'; 24 import { 25 prepareSelfRecovery, 26 preparePairedDelivery, 27 selfRecoveryPreparationSnapshot, 28 preparedSelfRecovery, 29 captureSelfRecoveryPreparation, 30 stopSelfRecoveryPreparation 31 } from '../../../src/lib/messaging/prepare-send.ts'; 32 export { 33 updatePrivateComposerText, 34 privateComposerText, 35 privateComposerSnapshot, 36 bindPrivateComposerPreparation, 37 closePrivateComposer, 38 requestPrivateNavigation, 39 confirmDiscardNavigation, 40 keepPrivateEditing, 41 dirtyNavigationSnapshot, 42 prepareSelfRecovery, 43 preparePairedDelivery, 44 selfRecoveryPreparationSnapshot, 45 preparedSelfRecovery, 46 captureSelfRecoveryPreparation, 47 stopSelfRecoveryPreparation, 48 disconnectIdentity 49 }; 50 export function attachPrivateBeforeUnload(token: DirtyNavigation) { 51 const handler = (event: BeforeUnloadEvent) => { 52 if (privateNavigationBeforeUnload(token, event)) event.returnValue = ''; 53 }; 54 window.addEventListener('beforeunload', handler); 55 return () => window.removeEventListener('beforeunload', handler); 56 } 57 export async function makeFixture() { 58 const f = await makePairedFixture(), 59 wire = reservedSendRumorWire(f.reserved), 60 composerInput = createPrivateComposer( 61 f.identity, 62 'reviewed_private_composer' 63 ); 64 if (!wire || !composerInput) throw Error('missing private fixture'); 65 const composer = composerInput; 66 const original = (JSON.parse(wire) as { content: string }).content; 67 if (!updatePrivateComposerText(composer, original, 'reviewed_private_text')) 68 throw Error('private text refused'); 69 if ( 70 !bindPrivateComposerPreparation( 71 composer, 72 f.reserved, 73 f.preparation, 74 undefined, 75 'reviewed_composer_preparation' 76 ) 77 ) 78 throw Error('private intent binding refused'); 79 const navigationInput = captureDirtyNavigation(composer); 80 if (!navigationInput) throw Error('dirty contract unavailable'); 81 const navigation = navigationInput; 82 async function enquiryContext(title = 'Carrots') { 83 const provider = window.nostr as Readonly<{ 84 signEvent( 85 template: EventTemplate 86 ): Promise<ReturnType<typeof finalizeEvent>>; 87 }>; 88 f.mode('wrong_author'); 89 try { 90 const signed = await provider.signEvent({ 91 kind: 30402, 92 created_at: 1700000060, 93 content: 'Public carrots', 94 tags: [ 95 ['d', 'carrots'], 96 ['title', title], 97 ['summary', 'Fresh carrots'], 98 ['published_at', '1700000000'], 99 ['location', 'Victoria'], 100 ['price', '3.5', 'CAD'], 101 ['radroots:price_unit', 'lb'], 102 ['status', 'active'] 103 ] 104 }); 105 const proof = verifyEnvelope( 106 JSON.stringify({ 107 id: signed.id, 108 pubkey: signed.pubkey, 109 sig: signed.sig, 110 kind: signed.kind, 111 created_at: signed.created_at, 112 tags: signed.tags, 113 content: signed.content 114 }) 115 ), 116 context = proof.ok && captureEnquiryContext(proof.value); 117 if (!context) throw Error('missing genuine enquiry context'); 118 return context; 119 } finally { 120 f.mode('normal'); 121 } 122 } 123 // The fixture exercises the shared contract with real browser controls. It 124 // does not add a production page or qualify a manual accessibility review. 125 const panel = document.createElement('section'), 126 textarea = document.createElement('textarea'), 127 warning = document.createElement('p'), 128 keep = document.createElement('button'), 129 discard = document.createElement('button'); 130 panel.setAttribute('aria-label', 'Private source fixture'); 131 textarea.setAttribute('aria-label', 'Private text'); 132 textarea.value = original; 133 warning.setAttribute('role', 'status'); 134 keep.textContent = 'Keep editing'; 135 discard.textContent = 'Discard draft'; 136 function render() { 137 warning.textContent = dirtyNavigationSnapshot(navigation)?.copy ?? ''; 138 textarea.value = privateComposerText(composer) ?? ''; 139 } 140 keep.addEventListener('click', () => { 141 keepPrivateEditing(navigation); 142 render(); 143 textarea.focus(); 144 }); 145 discard.addEventListener('click', () => { 146 confirmDiscardNavigation(navigation, 'reviewed_discard_private_text'); 147 render(); 148 }); 149 panel.addEventListener('keydown', (event) => { 150 if (event.key === 'Escape') { 151 keepPrivateEditing(navigation); 152 render(); 153 textarea.focus(); 154 } 155 }); 156 textarea.addEventListener('input', () => { 157 updatePrivateComposerText( 158 composer, 159 textarea.value, 160 'reviewed_private_text' 161 ); 162 render(); 163 }); 164 panel.append(textarea, warning, keep, discard); 165 document.body.append(panel); 166 let restoreReadback = () => {}; 167 let readbackFailures = 0; 168 function loseReadback() { 169 restoreReadback(); 170 const put = Reflect.get<IDBObjectStore, 'put'>( 171 IDBObjectStore.prototype, 172 'put' 173 ), 174 openCursor = Reflect.get<IDBIndex, 'openCursor'>( 175 IDBIndex.prototype, 176 'openCursor' 177 ); 178 let committed = false; 179 readbackFailures = 0; 180 IDBObjectStore.prototype.put = function ( 181 value: unknown, 182 key?: IDBValidKey 183 ) { 184 const request = 185 key === undefined ? put.call(this, value) : put.call(this, value, key); 186 if (this.name === 'private_sends') committed = true; 187 return request; 188 }; 189 IDBIndex.prototype.openCursor = function ( 190 query?: IDBValidKey | IDBKeyRange | null, 191 direction?: IDBCursorDirection 192 ) { 193 if (this.objectStore.name === 'private_sends' && committed) { 194 readbackFailures++; 195 throw Error('controlled readback unavailable'); 196 } 197 return openCursor.call(this, query, direction); 198 }; 199 restoreReadback = () => { 200 IDBObjectStore.prototype.put = put; 201 IDBIndex.prototype.openCursor = openCursor; 202 }; 203 return restoreReadback; 204 } 205 return { 206 ...f, 207 composer, 208 navigation, 209 original, 210 enquiryContext, 211 loseReadback, 212 readbackFailures: () => readbackFailures, 213 requestClose() { 214 const result = requestPrivateNavigation(navigation); 215 render(); 216 keep.focus(); 217 return result; 218 }, 219 close() { 220 restoreReadback(); 221 panel.remove(); 222 closePrivateComposer(composer); 223 f.close(); 224 } 225 }; 226 }