capability-gate.spec.ts (17499B)
1 import { test, expect, type BrowserContext } from '@playwright/test'; 2 import { execFileSync } from 'node:child_process'; 3 import { cp, mkdtemp, readFile, writeFile, rm } from 'node:fs/promises'; 4 import { tmpdir } from 'node:os'; 5 import path from 'node:path'; 6 import { fileURLToPath } from 'node:url'; 7 import { createHash } from 'node:crypto'; 8 import { createStaticHarness } from '../integration/harness/static.ts'; 9 declare global { 10 interface Window { 11 hcp056Counts: { 12 keys: number; 13 signs: number; 14 encrypts: number; 15 decrypts: number; 16 }; 17 hcp056Release?: () => void; 18 } 19 } 20 const owner = 21 '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'; 22 async function provider( 23 context: BrowserContext, 24 mode: 'exact' | 'refused' | 'pending' | 'unsupported' = 'exact' 25 ) { 26 await context.addInitScript( 27 ({ owner, mode }) => { 28 window.hcp056Counts = { keys: 0, signs: 0, encrypts: 0, decrypts: 0 }; 29 window.nostr = { 30 getPublicKey: () => { 31 window.hcp056Counts.keys++; 32 if (mode === 'refused') 33 return Promise.reject(new Error('HC_TEST_RAW_PROVIDER_DIAGNOSTIC')); 34 if (mode === 'pending') 35 return new Promise<string>((resolve) => { 36 window.hcp056Release = () => resolve(owner); 37 }); 38 return Promise.resolve(owner); 39 }, 40 signEvent: () => { 41 window.hcp056Counts.signs++; 42 return Promise.reject(new Error('Unexpected signature')); 43 }, 44 ...(mode === 'unsupported' 45 ? {} 46 : { 47 nip44: { 48 encrypt: (_key: string, text: string) => { 49 window.hcp056Counts.encrypts++; 50 return Promise.resolve(text); 51 }, 52 decrypt: (_key: string, text: string) => { 53 window.hcp056Counts.decrypts++; 54 return Promise.resolve(text); 55 } 56 } 57 }) 58 }; 59 }, 60 { owner, mode } 61 ); 62 } 63 let server: Awaited<ReturnType<typeof createStaticHarness>>; 64 test.beforeAll(async () => { 65 server = await createStaticHarness(); 66 }); 67 test.afterAll(async () => { 68 await server.close(); 69 }); 70 test('explicit Connect is enabled after hydration and never repeats Send', async ({ 71 browser 72 }) => { 73 const context = await browser.newContext(); 74 await provider(context); 75 try { 76 const page = await context.newPage(); 77 await page.goto(server.url + '/messages'); 78 const main = page.getByRole('main'), 79 connect = main.getByRole('button', { 80 name: 'Connect extension', 81 exact: true 82 }); 83 await expect(connect).toBeEnabled(); 84 await connect.focus(); 85 await page.keyboard.press('Enter'); 86 await expect(page.getByText('Identity', { exact: true })).toBeVisible(); 87 await expect(main.locator('[role="status"][tabindex="-1"]')).toBeFocused(); 88 await expect(main.locator('input, textarea, form')).toHaveCount(0); 89 expect(await page.evaluate(() => window.hcp056Counts)).toEqual({ 90 keys: 1, 91 signs: 0, 92 encrypts: 0, 93 decrypts: 0 94 }); 95 expect(await main.textContent()).not.toContain( 96 'HC_TEST_RAW_PROVIDER_DIAGNOSTIC' 97 ); 98 await page 99 .getByRole('navigation', { name: 'Primary', exact: true }) 100 .locator('summary') 101 .filter({ hasText: /^Identity$/ }) 102 .click(); 103 await page.getByRole('button', { name: 'Disconnect', exact: true }).click(); 104 await expect(connect).toBeEnabled(); 105 expect(await page.evaluate(() => window.hcp056Counts.keys)).toBe(1); 106 } finally { 107 await context.close(); 108 } 109 }); 110 test('failed protected Connect cannot steal focus from a later navbar Connect', async ({ 111 browser 112 }) => { 113 const context = await browser.newContext(); 114 await context.addInitScript((owner) => { 115 let calls = 0; 116 window.nostr = { 117 getPublicKey: () => 118 ++calls === 1 119 ? Promise.reject(new Error('denied')) 120 : Promise.resolve(owner), 121 signEvent: () => Promise.reject(new Error('Unexpected signature')) 122 }; 123 }, owner); 124 try { 125 const page = await context.newPage(); 126 await page.goto(server.url + '/messages'); 127 const main = page.getByRole('main'); 128 await main 129 .getByRole('button', { name: 'Connect extension', exact: true }) 130 .click(); 131 await expect( 132 main.getByText('Connection was not approved.', { exact: true }) 133 ).toBeVisible(); 134 await page 135 .getByRole('navigation', { name: 'Primary' }) 136 .getByRole('button', { name: 'Connect extension', exact: true }) 137 .click(); 138 await expect(page.getByText('Identity', { exact: true })).toBeVisible(); 139 console.log( 140 JSON.stringify({ 141 fixture: 'HCP056_COMPETING_FOCUS', 142 activeId: await page.evaluate(() => document.activeElement?.id) 143 }) 144 ); 145 await expect(page.locator('#identity-navbar-status')).toBeFocused(); 146 await expect(main.locator('#identity-protected-status')).not.toBeFocused(); 147 } finally { 148 await context.close(); 149 } 150 }); 151 test('Messages is discoverable to guests without acquiring identity or private data', async ({ 152 browser 153 }) => { 154 const context = await browser.newContext(); 155 await provider(context); 156 try { 157 const page = await context.newPage(); 158 await page.goto(server.url + '/about'); 159 const messages = page 160 .getByRole('navigation', { name: 'Primary' }) 161 .getByRole('link', { name: 'Messages', exact: true }); 162 await expect(messages).toHaveAttribute('href', '/messages'); 163 await messages.click(); 164 await expect( 165 page 166 .getByRole('main') 167 .getByRole('heading', { name: 'Connect or unlock', exact: true }) 168 ).toBeVisible(); 169 expect(await page.evaluate(() => window.hcp056Counts.keys)).toBe(0); 170 await expect( 171 page.locator('main input, main textarea, main form') 172 ).toHaveCount(0); 173 } finally { 174 await context.close(); 175 } 176 }); 177 test('denied connection has finite help and no raw provider diagnostic', async ({ 178 browser 179 }) => { 180 const context = await browser.newContext(); 181 await provider(context, 'refused'); 182 try { 183 const page = await context.newPage(); 184 await page.goto(server.url + '/messages'); 185 const main = page.getByRole('main'); 186 await main 187 .getByRole('button', { name: 'Connect extension', exact: true }) 188 .click(); 189 await expect( 190 main.getByText('Connection was not approved.', { exact: true }) 191 ).toBeVisible(); 192 await expect( 193 main.getByRole('link', { name: 'Connection help' }) 194 ).toHaveAttribute('href', '/about#help'); 195 expect(await main.textContent()).not.toContain( 196 'HC_TEST_RAW_PROVIDER_DIAGNOSTIC' 197 ); 198 expect(await page.evaluate(() => window.hcp056Counts)).toEqual({ 199 keys: 1, 200 signs: 0, 201 encrypts: 0, 202 decrypts: 0 203 }); 204 } finally { 205 await context.close(); 206 } 207 }); 208 test('pending connection survives route change without restoring a protected form or scheduling again', async ({ 209 browser 210 }) => { 211 const context = await browser.newContext(); 212 await provider(context, 'pending'); 213 try { 214 const page = await context.newPage(); 215 await page.goto(server.url + '/messages'); 216 await page 217 .locator('main') 218 .getByRole('button', { name: 'Connect extension', exact: true }) 219 .click(); 220 await expect( 221 page 222 .locator('main') 223 .getByText('Waiting for extension approval.', { exact: true }) 224 ).toBeVisible(); 225 await page.getByRole('link', { name: 'Search', exact: true }).click(); 226 const retained = page.getByLabel('What are you looking for?'); 227 await retained.focus(); 228 await page.evaluate(() => window.hcp056Release?.()); 229 await expect(page.getByText('Identity', { exact: true })).toBeVisible(); 230 await expect(retained).toBeFocused(); 231 await expect(page.getByLabel('What are you looking for?')).toBeVisible(); 232 expect(await page.evaluate(() => window.hcp056Counts)).toEqual({ 233 keys: 1, 234 signs: 0, 235 encrypts: 0, 236 decrypts: 0 237 }); 238 } finally { 239 await context.close(); 240 } 241 }); 242 test('Disconnect while approval is pending keeps the real slot and fences late connection', async ({ 243 browser 244 }) => { 245 const context = await browser.newContext(); 246 await provider(context, 'pending'); 247 try { 248 const page = await context.newPage(); 249 await page.goto(server.url + '/messages'); 250 const main = page.getByRole('main'); 251 await main 252 .getByRole('button', { name: 'Connect extension', exact: true }) 253 .click(); 254 await main.getByRole('button', { name: 'Disconnect', exact: true }).click(); 255 await main 256 .getByRole('button', { name: 'Connect extension', exact: true }) 257 .click(); 258 await expect( 259 main.getByText( 260 'An extension request is still pending. Wait for it to finish before trying again.', 261 { exact: true } 262 ) 263 ).toBeVisible(); 264 expect(await page.evaluate(() => window.hcp056Counts.keys)).toBe(1); 265 await page.evaluate(() => window.hcp056Release?.()); 266 await expect( 267 page 268 .getByRole('navigation', { name: 'Primary', exact: true }) 269 .locator('summary') 270 .filter({ hasText: /^Identity$/ }) 271 ).toHaveCount(0); 272 await expect(main.locator('input, textarea, form')).toHaveCount(0); 273 expect(await page.evaluate(() => window.hcp056Counts)).toEqual({ 274 keys: 1, 275 signs: 0, 276 encrypts: 0, 277 decrypts: 0 278 }); 279 // The old genuine SDK job must actually settle before the same author 280 // can connect again. The later navbar action owns focus on this same URL. 281 const navbarConnect = page 282 .getByRole('navigation', { name: 'Primary', exact: true }) 283 .getByRole('button', { name: 'Connect extension', exact: true }); 284 await expect 285 .poll(async () => { 286 await navbarConnect.click(); 287 return page.evaluate(() => window.hcp056Counts.keys); 288 }) 289 .toBe(2); 290 await page.evaluate(() => window.hcp056Release?.()); 291 await expect(page.getByText('Identity', { exact: true })).toBeVisible(); 292 await expect(page.locator('#identity-navbar-status')).toBeFocused(); 293 await expect(main.locator('#identity-protected-status')).not.toBeFocused(); 294 } finally { 295 await context.close(); 296 } 297 }); 298 test.describe('actual shared inline capability component', () => { 299 let fixtureServer: Awaited<ReturnType<typeof createStaticHarness>>, 300 directory: string; 301 test.beforeAll(async () => { 302 test.setTimeout(86_400_000); 303 directory = await mkdtemp(path.join(tmpdir(), 'hcp056-gates-')); 304 const capsule = fileURLToPath(new URL('../../../', import.meta.url)), 305 checkout = path.join(directory, 'checkout'); 306 execFileSync('git', [ 307 'clone', 308 '--quiet', 309 '--no-hardlinks', 310 capsule, 311 checkout 312 ]); 313 for (const name of [ 314 'src/lib/components/CapabilityGate.svelte', 315 'src/lib/runtime/view-context.ts', 316 'src/lib/runtime/identity-session.ts', 317 'src/lib/runtime/public-runtime.ts', 318 'src/lib/persistence/local-public-hides.ts', 319 'src/lib/persistence/local-private-blocks.ts', 320 'src/lib/components/AppShell.svelte', 321 'src/lib/components/AccountGate.svelte', 322 'src/lib/components/InboxSetup.svelte', 323 'src/lib/messaging/inbox-setup-view.ts', 324 'src/lib/messaging/resolve-inbox.ts', 325 'src/routes/+layout.svelte', 326 'tests/e2e/harness/capability-gate.svelte' 327 ]) 328 await cp( 329 path.join(capsule, 'web', name), 330 path.join(checkout, 'web', name) 331 ); 332 const fixture = await readFile( 333 path.join(capsule, 'web/tests/e2e/harness/capability-gate.svelte'), 334 'utf8' 335 ); 336 await writeFile( 337 path.join(checkout, 'web/src/routes/+page.svelte'), 338 fixture.replaceAll('../../../src/lib/', '../lib/') 339 ); 340 const store = execFileSync('corepack', ['pnpm', 'store', 'path'], { 341 cwd: path.join(capsule, 'web') 342 }) 343 .toString() 344 .trim(); 345 execFileSync( 346 'corepack', 347 [ 348 'pnpm', 349 'install', 350 '--offline', 351 '--frozen-lockfile', 352 '--store-dir', 353 path.dirname(store) 354 ], 355 { cwd: path.join(checkout, 'web'), timeout: 86400000, stdio: 'pipe' } 356 ); 357 const audit = execFileSync(process.execPath, ['tools/check-source.mjs'], { 358 cwd: path.join(checkout, 'web') 359 }).toString(); 360 execFileSync(process.execPath, ['tools/build-info.mjs'], { 361 cwd: path.join(checkout, 'web') 362 }); 363 execFileSync(process.execPath, ['node_modules/vite/bin/vite.js', 'build'], { 364 cwd: path.join(checkout, 'web'), 365 timeout: 86400000, 366 stdio: 'pipe' 367 }); 368 console.log( 369 JSON.stringify({ 370 fixture: 'HCP056_ACTUAL_SHARED_GATE', 371 sourceSha256: createHash('sha256').update(fixture).digest('hex'), 372 audit, 373 qualification: 374 'controlled Chromium and actual shared view/component/SDK; no named extension/inbox/private publication qualification' 375 }) 376 ); 377 fixtureServer = await createStaticHarness({ 378 buildRoot: path.join(checkout, 'web/build') 379 }); 380 }); 381 test.afterAll(async () => { 382 await fixtureServer?.close(); 383 if (directory) await rm(directory, { recursive: true, force: true }); 384 }); 385 test('Connect never repeats an earlier inline Send, and self-copy requires a separate reviewed action', async ({ 386 browser 387 }) => { 388 const context = await browser.newContext(); 389 await provider(context); 390 try { 391 const page = await context.newPage(); 392 await page.goto(fixtureServer.url); 393 const main = page.getByRole('main'); 394 await main.getByRole('button', { name: 'Explicit test Send' }).click(); 395 await main 396 .getByRole('button', { name: 'Connect extension', exact: true }) 397 .click(); 398 await expect( 399 main.getByText('Earlier sends: 1', { exact: true }) 400 ).toBeVisible(); 401 expect(await page.evaluate(() => window.hcp056Counts)).toEqual({ 402 keys: 1, 403 signs: 0, 404 encrypts: 0, 405 decrypts: 0 406 }); 407 await expect( 408 main.locator('[role="status"][tabindex="-1"]') 409 ).toBeFocused(); 410 await main 411 .getByRole('button', { name: 'Check messaging support', exact: true }) 412 .click(); 413 await expect( 414 main.getByText('Messaging support checked.', { exact: true }) 415 ).toBeVisible(); 416 expect(await page.evaluate(() => window.hcp056Counts)).toEqual({ 417 keys: 3, 418 signs: 0, 419 encrypts: 1, 420 decrypts: 1 421 }); 422 await expect( 423 main.getByText('Earlier sends: 1', { exact: true }) 424 ).toBeVisible(); 425 } finally { 426 await context.close(); 427 } 428 }); 429 test('reviewed probe waits for actual completion before returning focus', async ({ 430 browser 431 }) => { 432 const context = await browser.newContext(); 433 await provider(context); 434 try { 435 const page = await context.newPage(); 436 await page.goto(fixtureServer.url); 437 const main = page.getByRole('main'); 438 await main 439 .getByRole('button', { name: 'Connect extension', exact: true }) 440 .click(); 441 await page.evaluate(() => { 442 window.nostr = { 443 getPublicKey: () => 444 Promise.resolve( 445 '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' 446 ), 447 signEvent: () => Promise.reject(new Error('Unexpected signature')), 448 nip44: { 449 encrypt: (_key: string, text: string) => 450 new Promise<string>((resolve) => { 451 window.hcp056Release = () => resolve(text); 452 }), 453 decrypt: (_key: string, text: string) => Promise.resolve(text) 454 } 455 }; 456 }); 457 await main 458 .getByRole('button', { name: 'Check messaging support', exact: true }) 459 .click(); 460 await expect( 461 main.getByText('Waiting for extension approval.', { exact: true }) 462 ).toBeVisible(); 463 const retained = main.getByRole('button', { name: 'Explicit test Send' }); 464 await retained.focus(); 465 await expect(retained).toBeFocused(); 466 await expect(main.locator('#identity-inline-status')).not.toBeFocused(); 467 await page.evaluate(() => window.hcp056Release?.()); 468 await expect( 469 main.getByText('Messaging support checked.', { exact: true }) 470 ).toBeVisible(); 471 await expect(main.locator('#identity-inline-status')).toBeFocused(); 472 } finally { 473 await context.close(); 474 } 475 }); 476 test('connected signing-only identity has an explicit unsupported message-encryption state', async ({ 477 browser 478 }) => { 479 const context = await browser.newContext(); 480 await provider(context, 'unsupported'); 481 try { 482 const page = await context.newPage(); 483 await page.goto(fixtureServer.url); 484 const main = page.getByRole('main'); 485 await main 486 .getByRole('button', { name: 'Connect extension', exact: true }) 487 .click(); 488 await main 489 .getByRole('button', { name: 'Check messaging support', exact: true }) 490 .click(); 491 await expect( 492 main.getByText( 493 'This extension does not support the required message encryption.', 494 { exact: true } 495 ) 496 ).toBeVisible(); 497 await expect(main.locator('textarea, form')).toHaveCount(0); 498 expect( 499 await page.evaluate( 500 () => window.hcp056Counts.encrypts + window.hcp056Counts.signs 501 ) 502 ).toBe(0); 503 } finally { 504 await context.close(); 505 } 506 }); 507 });