extension-scheduler.spec.ts (8969B)
1 import { expect, test } from '@playwright/test'; 2 import { build } from 'vite'; 3 import { fileURLToPath } from 'node:url'; 4 import { createHash } from 'node:crypto'; 5 import { createStaticHarness } from '../integration/harness/static.ts'; 6 import type * as Scheduler from '../../src/lib/nostr/extension-scheduler.ts'; 7 declare global { 8 interface Window { 9 hcp052: typeof Scheduler; 10 hcp052Scheduler: Scheduler.ExtensionScheduler; 11 hcp052Action: Scheduler.ExtensionAction; 12 hcp052Task: Promise<Scheduler.ExtensionActionResult<unknown>>; 13 hcp052Release: (value: string) => void; 14 hcp052Calls: string[]; 15 } 16 } 17 const owner = 18 '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'; 19 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 20 test.beforeAll(async () => { 21 server = await createStaticHarness(); 22 const result = await build({ 23 configFile: false, 24 logLevel: 'silent', 25 build: { 26 write: false, 27 minify: false, 28 lib: { 29 entry: fileURLToPath( 30 new URL('../../src/lib/nostr/extension-scheduler.ts', import.meta.url) 31 ), 32 name: 'hcp052', 33 formats: ['iife'] 34 } 35 } 36 }); 37 const output = Array.isArray(result) ? result[0] : result; 38 if (!('output' in output)) throw new Error('missing bundle'); 39 const chunks = output.output.filter((v) => v.type === 'chunk'); 40 expect(chunks).toHaveLength(1); 41 bundle = chunks[0].code; 42 console.log( 43 JSON.stringify({ 44 fixture: 'HCP052_ACTUAL_SCHEDULER_CONTROLLED_CALLS', 45 moduleIds: Object.keys(chunks[0].modules), 46 bundleSha256: createHash('sha256').update(bundle).digest('hex'), 47 claim: 48 'actual Chromium and current scheduler; no real extension qualification' 49 }) 50 ); 51 }); 52 test.afterAll(async () => await Promise.resolve(server.close())); 53 test.beforeEach(async ({ page }) => { 54 await page.goto(server.url + '/search'); 55 await page.addScriptTag({ content: bundle }); 56 await page.evaluate(() => { 57 window.hcp052Scheduler = window.hcp052.browserExtensionScheduler()!; 58 window.hcp052Calls = []; 59 }); 60 }); 61 test('load and shared accessor request no extension permission', async ({ 62 page 63 }) => { 64 expect( 65 await page.evaluate(() => ({ 66 calls: window.hcp052Calls, 67 same: 68 window.hcp052.browserExtensionScheduler() === window.hcp052Scheduler, 69 snapshot: window.hcp052.extensionSchedulerSnapshot(window.hcp052Scheduler) 70 })) 71 ).toEqual({ calls: [], same: true, snapshot: { state: 'idle' } }); 72 }); 73 test('one explicit batch serializes all five call kinds and blocks competing actions', async ({ 74 page 75 }) => { 76 await page.evaluate((owner) => { 77 window.hcp052Task = window.hcp052.runExtensionAction( 78 window.hcp052Scheduler, 79 { owner, session: Symbol(), operation: Symbol() }, 80 () => true, 81 async (a) => { 82 const key = await window.hcp052.callExtension(a, 'key', () => { 83 window.hcp052Calls.push('key'); 84 return new Promise<string>((resolve) => { 85 window.hcp052Release = resolve; 86 }); 87 }); 88 for (const kind of ['sign', 'encrypt', 'decrypt', 'auth'] as const) 89 await window.hcp052.callExtension(a, kind, async () => { 90 window.hcp052Calls.push(kind); 91 return await Promise.resolve(kind); 92 }); 93 return key; 94 } 95 ); 96 }, owner); 97 expect( 98 await page.evaluate( 99 (owner) => 100 window.hcp052.runExtensionAction( 101 window.hcp052Scheduler, 102 { owner, session: Symbol(), operation: Symbol() }, 103 () => true, 104 async () => { 105 window.hcp052Calls.push('competing'); 106 107 await Promise.resolve(); 108 } 109 ), 110 owner 111 ) 112 ).toEqual({ status: 'busy' }); 113 await page.evaluate((owner) => window.hcp052Release(owner), owner); 114 expect(await page.evaluate(() => window.hcp052Task)).toMatchObject({ 115 status: 'completed', 116 value: { status: 'settled', current: true, value: owner } 117 }); 118 expect(await page.evaluate(() => window.hcp052Calls)).toEqual([ 119 'key', 120 'sign', 121 'encrypt', 122 'decrypt', 123 'auth' 124 ]); 125 }); 126 test('UI timeout retains slot, original capture and late signature without next AUTH', async ({ 127 page 128 }) => { 129 await page.evaluate((owner) => { 130 window.hcp052Task = window.hcp052.runExtensionAction( 131 window.hcp052Scheduler, 132 { owner, session: Symbol(), operation: Symbol() }, 133 () => true, 134 async (a) => { 135 window.hcp052Action = a; 136 const signed = await window.hcp052.callExtension( 137 a, 138 'sign', 139 () => 140 new Promise<string>((resolve) => { 141 window.hcp052Calls.push('sign'); 142 window.hcp052Release = resolve; 143 }) 144 ); 145 const auth = await window.hcp052.callExtension(a, 'auth', async () => { 146 window.hcp052Calls.push('auth'); 147 return await Promise.resolve('unused'); 148 }); 149 return { signed, auth }; 150 } 151 ); 152 window.hcp052.markExtensionWaitExpired(window.hcp052Action); 153 }, owner); 154 expect( 155 await page.evaluate(() => 156 window.hcp052.extensionSchedulerSnapshot(window.hcp052Scheduler) 157 ) 158 ).toEqual({ state: 'active', phase: 'wait_expired', pending: 'sign' }); 159 expect( 160 await page.evaluate( 161 (owner) => 162 window.hcp052.runExtensionAction( 163 window.hcp052Scheduler, 164 { owner, session: Symbol(), operation: Symbol() }, 165 () => true, 166 async () => { 167 window.hcp052Calls.push('unexpected'); 168 169 await Promise.resolve(); 170 } 171 ), 172 owner 173 ) 174 ).toEqual({ status: 'busy' }); 175 await page.evaluate(() => window.hcp052Release('controlled late signature')); 176 expect(await page.evaluate(() => window.hcp052Task)).toMatchObject({ 177 status: 'wait_expired', 178 capture: { owner }, 179 value: { 180 signed: { 181 status: 'settled', 182 current: false, 183 value: 'controlled late signature', 184 capture: { owner } 185 }, 186 auth: { status: 'wait_expired' } 187 } 188 }); 189 expect(await page.evaluate(() => window.hcp052Calls)).toEqual(['sign']); 190 }); 191 test('decrypt denial pauses batch with safe finite outcome and no automatic resume', async ({ 192 page 193 }) => { 194 const outcome = await page.evaluate( 195 (owner) => 196 window.hcp052.runExtensionAction( 197 window.hcp052Scheduler, 198 { owner, session: Symbol(), operation: Symbol() }, 199 () => true, 200 async (a) => { 201 const denied = await window.hcp052.callExtension( 202 a, 203 'decrypt', 204 async () => { 205 window.hcp052Calls.push('decrypt'); 206 await Promise.resolve(); 207 throw new Error('controlled private body'); 208 } 209 ); 210 const next = await window.hcp052.callExtension( 211 a, 212 'sign', 213 async () => { 214 window.hcp052Calls.push('sign'); 215 return await Promise.resolve('unused'); 216 } 217 ); 218 return { denied, next }; 219 } 220 ), 221 owner 222 ); 223 expect(outcome).toMatchObject({ 224 status: 'denied', 225 value: { denied: { status: 'denied' }, next: { status: 'denied' } } 226 }); 227 expect(JSON.stringify(outcome)).not.toContain('controlled private body'); 228 expect(await page.evaluate(() => window.hcp052Calls)).toEqual(['decrypt']); 229 }); 230 test('local stop and forgotten await retain shared browser scheduler until settlement', async ({ 231 page 232 }) => { 233 await page.evaluate((owner) => { 234 window.hcp052Task = window.hcp052.runExtensionAction( 235 window.hcp052Scheduler, 236 { owner, session: Symbol(), operation: Symbol() }, 237 () => true, 238 async (a) => { 239 window.hcp052Action = a; 240 void window.hcp052.callExtension( 241 a, 242 'encrypt', 243 () => 244 new Promise<string>((resolve) => { 245 window.hcp052Calls.push('encrypt'); 246 window.hcp052Release = resolve; 247 }) 248 ); 249 window.hcp052.stopExtensionAction(a); 250 251 await Promise.resolve(); 252 } 253 ); 254 }, owner); 255 expect( 256 await page.evaluate( 257 (owner) => 258 window.hcp052.runExtensionAction( 259 window.hcp052.browserExtensionScheduler()!, 260 { owner, session: Symbol(), operation: Symbol() }, 261 () => true, 262 async () => { 263 window.hcp052Calls.push('unexpected'); 264 265 await Promise.resolve(); 266 } 267 ), 268 owner 269 ) 270 ).toEqual({ status: 'busy' }); 271 await page.evaluate(() => window.hcp052Release('late cipher')); 272 expect(await page.evaluate(() => window.hcp052Task)).toMatchObject({ 273 status: 'stopped' 274 }); 275 expect( 276 await page.evaluate(() => 277 window.hcp052.extensionSchedulerSnapshot(window.hcp052Scheduler) 278 ) 279 ).toEqual({ state: 'idle' }); 280 expect(await page.evaluate(() => window.hcp052Calls)).toEqual(['encrypt']); 281 });