late-results.spec.ts (19556B)
1 import { test, expect, type Page } 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 Late from './harness/late-results.ts'; 7 import type * as Legacy from '../../src/lib/nostr/extension.ts'; 8 import type { ApprovedSignResult } from '../../src/lib/nostr/extension.ts'; 9 import type { PublicEffectResult } from '../../src/lib/runtime/effect-ownership.ts'; 10 declare global { 11 interface Window { 12 hcp055: typeof Late; 13 hcp055Fixture: ReturnType<typeof Late.makeFixture>; 14 hcp055Other: ReturnType<typeof Late.makeFixture>; 15 hcp055Legacy: typeof Legacy; 16 hcp055Session: Late.IdentitySession; 17 hcp055Operation: Late.IdentityPublicOperation; 18 hcp055Database: Parameters<typeof Late.closeBrowserDatabase>[0]; 19 hcp055Repository: ReturnType<typeof Late.createPublicQuotaRepository> & {}; 20 hcp055Release(): void; 21 hcp055Started(): void; 22 hcp055Inner: ApprovedSignResult; 23 hcp055Counts: { keys: number; signs: number; work: number }; 24 hcp055Task: Promise<PublicEffectResult<unknown>>; 25 } 26 } 27 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 28 test.beforeAll(async () => { 29 server = await createStaticHarness(); 30 const legacyOnly = process.env.HARVESTCIRCLE_HCP055_LEGACY_RED === '1'; 31 const result = await build({ 32 configFile: false, 33 logLevel: 'silent', 34 build: { 35 write: false, 36 minify: false, 37 lib: { 38 entry: fileURLToPath( 39 new URL( 40 legacyOnly 41 ? '../../src/lib/nostr/extension.ts' 42 : './harness/late-results.ts', 43 import.meta.url 44 ) 45 ), 46 name: legacyOnly ? 'hcp055Legacy' : 'hcp055', 47 formats: ['iife'] 48 } 49 } 50 }); 51 const output = Array.isArray(result) ? result[0] : result; 52 if (!('output' in output)) throw new Error('missing bundle'); 53 const chunks = output.output.filter((item) => item.type === 'chunk'); 54 expect(chunks).toHaveLength(1); 55 bundle = chunks[0].code; 56 console.log( 57 JSON.stringify({ 58 fixture: 'HCP055_ACTUAL_SDK_IDB_ORIGINAL_LATE_REVIEW', 59 bundleSha256: createHash('sha256').update(bundle).digest('hex'), 60 moduleIds: Object.keys(chunks[0].modules), 61 qualification: 62 'controlled local Chromium and genuine SDK; no named extension/UI publication/real relay claim' 63 }) 64 ); 65 }); 66 test.afterAll(async () => server.close()); 67 test.afterEach(async ({ page }) => { 68 await page.evaluate(() => { 69 window.hcp055Fixture?.close(); 70 window.hcp055Other?.close(); 71 if (window.hcp055Database) 72 window.hcp055.closeBrowserDatabase(window.hcp055Database); 73 }); 74 }); 75 test('legacy adapter capture never revives after observed account loss and same-author reconnect', async ({ 76 page 77 }) => { 78 await page.goto(server.url + '/search'); 79 const result = await build({ 80 configFile: false, 81 logLevel: 'silent', 82 build: { 83 write: false, 84 minify: false, 85 lib: { 86 entry: fileURLToPath( 87 new URL('../../src/lib/nostr/extension.ts', import.meta.url) 88 ), 89 name: 'hcp055Legacy', 90 formats: ['iife'] 91 } 92 } 93 }); 94 const output = Array.isArray(result) ? result[0] : result; 95 if (!('output' in output)) throw new Error('missing legacy bundle'); 96 const chunks = output.output.filter((item) => item.type === 'chunk'); 97 expect(chunks).toHaveLength(1); 98 await page.addScriptTag({ content: chunks[0].code }); 99 const observed = await page.evaluate(async () => { 100 const s = window.hcp055Legacy; 101 const owner = 102 '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'; 103 const other = 104 'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5'; 105 const adapter = s.createExtensionAdapter(); 106 window.nostr = { getPublicKey: () => Promise.resolve(owner) }; 107 await s.connectExtensionAdapter(adapter); 108 const original = s.extensionOwnershipCapture(adapter)!; 109 window.nostr = { getPublicKey: () => Promise.resolve(other) }; 110 await s.recheckExtensionAdapter(adapter); 111 const lost = original.current(); 112 window.nostr = { getPublicKey: () => Promise.resolve(owner) }; 113 await s.connectExtensionAdapter(adapter); 114 return { lost, revived: original.current() }; 115 }); 116 expect(observed).toEqual({ lost: false, revived: false }); 117 }); 118 async function load(page: Page) { 119 await page.goto(server.url + '/search'); 120 await page.addScriptTag({ content: bundle }); 121 await page.evaluate(async () => { 122 const s = window.hcp055, 123 f = s.makeFixture(); 124 window.hcp055Fixture = f; 125 window.hcp055Other = s.makeFixture(); 126 window.hcp055Counts = { keys: 0, signs: 0, work: 0 }; 127 window.nostr = { 128 getPublicKey: () => { 129 window.hcp055Counts.keys++; 130 return Promise.resolve(f.owner); 131 }, 132 signEvent: (template: Parameters<typeof f.sign>[0]) => { 133 window.hcp055Counts.signs++; 134 return Promise.resolve(f.sign(template)); 135 } 136 }; 137 const session = s.createIdentitySession(); 138 window.hcp055Session = session; 139 await s.connectIdentity(session); 140 const operation = s.captureIdentityPublicOperation( 141 session, 142 f.record, 143 f.id, 144 'reviewed_captured_operation' 145 ); 146 if (!operation) throw new Error('invalid operation'); 147 window.hcp055Operation = operation; 148 const db = await s.openBrowserDatabase(); 149 if (db.state !== 'ready') throw new Error(db.reason); 150 window.hcp055Database = db.owner; 151 const repo = s.createPublicQuotaRepository(db.owner, f.owner); 152 if (!repo) throw new Error('invalid repository'); 153 window.hcp055Repository = repo; 154 const input = document.createElement('input'); 155 input.id = 'hcp055-input'; 156 input.value = f.template.content; 157 input.oninput = () => s.invalidateIdentityOperations(session); 158 document.body.append(input); 159 window.hcp055Counts = { keys: 0, signs: 0, work: 0 }; 160 }); 161 } 162 async function begin( 163 page: Page, 164 forgotten = false, 165 mode: 'exact' | 'content' | 'cached' = 'exact' 166 ) { 167 await page.evaluate( 168 ({ forgotten, mode }) => { 169 const s = window.hcp055, 170 f = window.hcp055Fixture; 171 const started = new Promise<void>((resolve) => { 172 window.hcp055Started = resolve; 173 }); 174 window.nostr = { 175 getPublicKey: () => { 176 window.hcp055Counts.keys++; 177 return Promise.resolve(f.owner); 178 }, 179 signEvent: (template: Parameters<typeof f.sign>[0]) => { 180 window.hcp055Counts.signs++; 181 window.hcp055Started(); 182 return new Promise((resolve) => { 183 window.hcp055Release = () => resolve(f.sign(template, mode)); 184 }); 185 } 186 }; 187 const ownership = s.identityPublicOperationOwnership( 188 window.hcp055Session, 189 window.hcp055Operation 190 ); 191 if (!ownership) throw new Error('missing ownership'); 192 window.hcp055Task = s.runCapturedPublicEffect( 193 window.hcp055Repository, 194 f.record, 195 f.id, 196 ownership, 197 'reviewed_captured_operation', 198 async (lease) => { 199 window.hcp055Counts.work++; 200 const task = s.signIdentityPublicOperation( 201 window.hcp055Session, 202 window.hcp055Operation, 203 lease 204 ); 205 if (forgotten) { 206 void task.then((result) => { 207 window.hcp055Inner = result; 208 }); 209 await started; 210 return 'forgotten'; 211 } 212 const result = await task; 213 window.hcp055Inner = result; 214 return result; 215 } 216 ); 217 }, 218 { forgotten, mode } 219 ); 220 await expect 221 .poll(() => page.evaluate(() => window.hcp055Counts.signs)) 222 .toBe(1); 223 } 224 async function settle(page: Page) { 225 await page.evaluate(() => window.hcp055Release()); 226 expect( 227 await page.evaluate(async () => (await window.hcp055Task).status) 228 ).toBe('unknown'); 229 await expect 230 .poll(() => page.evaluate(() => window.hcp055Inner?.status)) 231 .toBe('unknown'); 232 expect(await page.evaluate(() => 'artifact' in window.hcp055Inner)).toBe( 233 false 234 ); 235 expect(await page.evaluate(() => window.hcp055Counts.signs)).toBe(1); 236 } 237 async function reviewed(page: Page, consent = 'review_original_late_artifact') { 238 return page.evaluate((consent) => { 239 const s = window.hcp055, 240 f = window.hcp055Fixture; 241 const artifact = s.reviewIdentityLatePublicArtifact( 242 window.hcp055Session, 243 window.hcp055Operation, 244 f.record, 245 consent 246 ); 247 return artifact ? s.capturedArtifactSnapshot(artifact) : undefined; 248 }, consent); 249 } 250 test('logout during approval retains exact original late result; foreign author cannot review or replay it', async ({ 251 page 252 }) => { 253 await load(page); 254 await begin(page); 255 await page.evaluate(() => 256 window.hcp055.disconnectIdentity(window.hcp055Session) 257 ); 258 await settle(page); 259 expect(await reviewed(page)).toBeUndefined(); 260 const after = await page.evaluate(async () => { 261 const s = window.hcp055, 262 f = window.hcp055Other; 263 window.nostr = { 264 getPublicKey: () => { 265 window.hcp055Counts.keys++; 266 return Promise.resolve(f.owner); 267 }, 268 signEvent: () => { 269 window.hcp055Counts.signs++; 270 throw new Error('unexpected replay'); 271 } 272 }; 273 await s.connectIdentity(window.hcp055Session); 274 return { 275 view: s.identityPublicOperationSnapshot( 276 window.hcp055Session, 277 window.hcp055Operation 278 ), 279 identity: s.identitySessionSnapshot(window.hcp055Session), 280 counts: window.hcp055Counts 281 }; 282 }); 283 expect(after.view).toMatchObject({ 284 current: false, 285 lateAvailable: true, 286 outcome: 'unknown' 287 }); 288 expect(after.identity).toMatchObject({ 289 publicKey: await page.evaluate(() => window.hcp055Other.owner) 290 }); 291 expect(after.counts.signs).toBe(1); 292 expect(await reviewed(page)).toBeUndefined(); 293 }); 294 for (const change of ['route', 'input'] as const) 295 test(`${change} invalidation fences pending work and keeps original capture unchanged`, async ({ 296 page 297 }) => { 298 await load(page); 299 const original = await page.evaluate(() => { 300 const s = window.hcp055, 301 f = window.hcp055Fixture; 302 return s.publicRecordWire(f.record, f.owner, f.id); 303 }); 304 await begin(page); 305 if (change === 'input') 306 await page.locator('#hcp055-input').fill('new public input'); 307 else 308 await page.evaluate(() => { 309 history.pushState({}, '', '/settings'); 310 window.hcp055.invalidateIdentityOperations(window.hcp055Session); 311 }); 312 await settle(page); 313 const state = await page.evaluate(async () => { 314 const s = window.hcp055, 315 f = window.hcp055Fixture, 316 inventory = await s.inspectPublicStorage(window.hcp055Repository); 317 return { 318 view: s.identityPublicOperationSnapshot( 319 window.hcp055Session, 320 window.hcp055Operation 321 ), 322 wire: s.publicRecordWire(f.record, f.owner, f.id), 323 count: inventory.ok 324 ? s.publicInventorySnapshot(window.hcp055Repository, inventory.value) 325 ?.rows.length 326 : -1, 327 calls: window.hcp055Counts 328 }; 329 }); 330 expect(state.wire).toBe(original); 331 expect(state.count).toBe(1); 332 expect(state.view).toMatchObject({ 333 current: false, 334 lateAvailable: true, 335 outcome: 'unknown' 336 }); 337 expect(state.calls).toEqual({ keys: 1, signs: 1, work: 1 }); 338 expect(await reviewed(page, 'unreviewed')).toBeUndefined(); 339 expect(await reviewed(page)).toBeDefined(); 340 }); 341 for (const action of ['stop', 'wait_expiry'] as const) 342 test(`${action} keeps actual prompt reserved and late artifact requires review`, async ({ 343 page 344 }) => { 345 await load(page); 346 await begin(page); 347 await page.evaluate((action) => { 348 const s = window.hcp055; 349 if (action === 'stop') 350 s.stopIdentityPublicOperation( 351 window.hcp055Session, 352 window.hcp055Operation 353 ); 354 else 355 s.expireIdentityPublicOperationWait( 356 window.hcp055Session, 357 window.hcp055Operation 358 ); 359 }, action); 360 const busy = await page.evaluate( 361 async () => 362 ( 363 await window.hcp055.connectIdentity( 364 window.hcp055.createIdentitySession() 365 ) 366 ).admission 367 ); 368 expect(busy).toBe('busy'); 369 await settle(page); 370 expect(await page.evaluate(() => window.hcp055Counts)).toEqual({ 371 keys: 1, 372 signs: 1, 373 work: 1 374 }); 375 expect(await reviewed(page, 'unreviewed')).toBeUndefined(); 376 expect(await reviewed(page)).toBeDefined(); 377 }); 378 test('forgotten operation retains original late review and never becomes a fresh draft', async ({ 379 page 380 }) => { 381 await load(page); 382 await begin(page, true); 383 await settle(page); 384 const before = await page.evaluate(() => ({ ...window.hcp055Counts })); 385 expect(await reviewed(page)).toBeDefined(); 386 const after = await page.evaluate(async () => { 387 const s = window.hcp055, 388 read = await s.inspectPublicStorage(window.hcp055Repository); 389 return { 390 calls: window.hcp055Counts, 391 rows: read.ok 392 ? s.publicInventorySnapshot(window.hcp055Repository, read.value)?.rows 393 : [] 394 }; 395 }); 396 expect(after.calls).toEqual(before); 397 expect(after.rows).toHaveLength(1); 398 expect(after.rows?.[0].id).toBe( 399 await page.evaluate(() => window.hcp055Fixture.id) 400 ); 401 }); 402 for (const mode of ['content', 'cached'] as const) 403 test(`invalid late ${mode} response cannot become a review capability`, async ({ 404 page 405 }) => { 406 await load(page); 407 await begin(page, false, mode); 408 await page.evaluate(() => 409 window.hcp055.invalidateIdentityOperations(window.hcp055Session) 410 ); 411 await settle(page); 412 expect(await reviewed(page)).toBeUndefined(); 413 expect( 414 await page.evaluate( 415 () => 416 window.hcp055.identityPublicOperationSnapshot( 417 window.hcp055Session, 418 window.hcp055Operation 419 )?.lateAvailable 420 ) 421 ).toBe(false); 422 }); 423 test('account loss then same-author reconnect cannot revive an original operation generation', async ({ 424 page 425 }) => { 426 await load(page); 427 await page.evaluate(async () => { 428 const s = window.hcp055, 429 other = window.hcp055Other; 430 window.nostr = { getPublicKey: () => Promise.resolve(other.owner) }; 431 await s.recheckIdentityOwner(window.hcp055Session); 432 }); 433 const old = await page.evaluate(() => 434 window.hcp055.identityPublicOperationSnapshot( 435 window.hcp055Session, 436 window.hcp055Operation 437 ) 438 ); 439 expect(old?.current).toBe(false); 440 const reconnected = await page.evaluate(async () => { 441 const s = window.hcp055, 442 f = window.hcp055Fixture; 443 window.nostr = { 444 getPublicKey: () => Promise.resolve(f.owner), 445 signEvent: () => { 446 window.hcp055Counts.signs++; 447 throw new Error('unexpected replay'); 448 } 449 }; 450 await s.connectIdentity(window.hcp055Session); 451 return s.identityPublicOperationSnapshot( 452 window.hcp055Session, 453 window.hcp055Operation 454 ); 455 }); 456 expect(reconnected?.current).toBe(false); 457 expect(await page.evaluate(() => window.hcp055Counts.signs)).toBe(0); 458 }); 459 test('same-author explicit review after reconnect returns only the original verified artifact with zero effects', async ({ 460 page 461 }) => { 462 await load(page); 463 await begin(page); 464 await page.evaluate(() => 465 window.hcp055.disconnectIdentity(window.hcp055Session) 466 ); 467 await settle(page); 468 await page.evaluate(async () => { 469 const s = window.hcp055, 470 f = window.hcp055Fixture; 471 window.nostr = { 472 getPublicKey: () => { 473 window.hcp055Counts.keys++; 474 return Promise.resolve(f.owner); 475 }, 476 signEvent: () => { 477 window.hcp055Counts.signs++; 478 throw new Error('unexpected new signature'); 479 } 480 }; 481 await s.connectIdentity(window.hcp055Session); 482 }); 483 const before = await page.evaluate(() => ({ ...window.hcp055Counts })); 484 const artifact = await reviewed(page); 485 expect(artifact?.author).toBe( 486 await page.evaluate(() => window.hcp055Fixture.owner) 487 ); 488 expect(artifact?.hash).toBe( 489 await page.evaluate(() => { 490 const s = window.hcp055, 491 f = window.hcp055Fixture, 492 row = s.publicRecordSnapshot(f.record, f.owner, f.id); 493 return row && row.family !== 'public_draft' ? row.capture.hash : null; 494 }) 495 ); 496 expect(await page.evaluate(() => window.hcp055Counts)).toEqual(before); 497 expect( 498 await page.evaluate( 499 () => 500 window.hcp055.identityPublicOperationSnapshot( 501 window.hcp055Session, 502 window.hcp055Operation 503 )?.current 504 ) 505 ).toBe(false); 506 }); 507 test('identity continuation cannot publish a signed artifact after its view was invalidated', async ({ 508 page 509 }) => { 510 let exercised = 0; 511 for (let depth = 0; depth <= 32; depth++) { 512 await load(page); 513 const observed = await page.evaluate(async (depth) => { 514 const s = window.hcp055, 515 f = window.hcp055Fixture; 516 let keys = 0, 517 beforeInvalidation: string | undefined; 518 window.nostr = { 519 getPublicKey: () => { 520 keys++; 521 if (keys === 2) { 522 let remaining = depth; 523 const advance = () => { 524 if (remaining-- > 0) queueMicrotask(advance); 525 else { 526 beforeInvalidation = s.identityPublicOperationSnapshot( 527 window.hcp055Session, 528 window.hcp055Operation 529 )?.outcome; 530 s.invalidateIdentityOperations(window.hcp055Session); 531 } 532 }; 533 queueMicrotask(advance); 534 } 535 return Promise.resolve(f.owner); 536 }, 537 signEvent: (template: Parameters<typeof f.sign>[0]) => 538 Promise.resolve(f.sign(template)) 539 }; 540 const ownership = s.identityPublicOperationOwnership( 541 window.hcp055Session, 542 window.hcp055Operation 543 )!; 544 let inner: ApprovedSignResult | undefined; 545 await s.runCapturedPublicEffect( 546 window.hcp055Repository, 547 f.record, 548 f.id, 549 ownership, 550 'reviewed_captured_operation', 551 async (lease) => { 552 inner = await s.signIdentityPublicOperation( 553 window.hcp055Session, 554 window.hcp055Operation, 555 lease 556 ); 557 return inner; 558 } 559 ); 560 const result = { 561 depth, 562 beforeInvalidation, 563 inner: inner?.status, 564 hasArtifact: inner !== undefined && 'artifact' in inner, 565 reviewed: 566 s.reviewIdentityLatePublicArtifact( 567 window.hcp055Session, 568 window.hcp055Operation, 569 f.record, 570 'review_original_late_artifact' 571 ) !== undefined, 572 view: s.identityPublicOperationSnapshot( 573 window.hcp055Session, 574 window.hcp055Operation 575 ) 576 }; 577 f.close(); 578 window.hcp055Other.close(); 579 s.closeBrowserDatabase(window.hcp055Database); 580 return result; 581 }, depth); 582 if (observed.beforeInvalidation === 'pending') { 583 exercised++; 584 expect(observed, `actual microtask depth ${depth}`).not.toMatchObject({ 585 inner: 'signed' 586 }); 587 expect(observed.hasArtifact).toBe(false); 588 expect(observed.reviewed).toBe(true); 589 } 590 } 591 expect(exercised).toBeGreaterThan(0); 592 });