inbox-setup.spec.ts (8471B)
1 import { test, expect } 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 Setup from './harness/inbox-setup.ts'; 7 import type { BrowserDatabase } from '../../src/lib/persistence/database.ts'; 8 import type { PublicQuotaRepository } from '../../src/lib/persistence/quota.ts'; 9 import type { InboxSetupReview } from '../../src/lib/messaging/inbox-setup.ts'; 10 declare global { 11 interface Window { 12 hcp065: typeof Setup; 13 hcp065Fixture: Awaited<ReturnType<typeof Setup.makeSetupFixture>>; 14 hcp065Db: BrowserDatabase; 15 hcp065Repo: PublicQuotaRepository; 16 hcp065Review: InboxSetupReview; 17 } 18 } 19 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 20 test.beforeAll(async () => { 21 server = await createStaticHarness(); 22 const entry = fileURLToPath( 23 new URL('./harness/inbox-setup.ts', import.meta.url) 24 ); 25 const result = await build({ 26 configFile: false, 27 logLevel: 'silent', 28 build: { 29 write: false, 30 minify: false, 31 lib: { entry, name: 'hcp065', formats: ['iife'] } 32 } 33 }); 34 const output = Array.isArray(result) ? result[0] : result; 35 if (!('output' in output)) throw new Error('missing fixture output'); 36 const chunks = output.output.filter((x) => x.type === 'chunk'); 37 expect(chunks).toHaveLength(1); 38 bundle = chunks[0].code; 39 console.log( 40 JSON.stringify({ 41 fixture: 'HCP065_ACTUAL_SDK_OWNERS_AND_REAL_IDB_WEB_LOCKS', 42 bundleSha256: createHash('sha256').update(bundle).digest('hex'), 43 moduleIds: Object.keys(chunks[0].modules), 44 qualification: 45 'controlled local Chromium/source only; no real extension/relay/client/retention qualification' 46 }) 47 ); 48 }); 49 test.afterAll(async () => server.close()); 50 test.beforeEach(async ({ page }) => { 51 await page.goto(server.url + '/search'); 52 await page.addScriptTag({ content: bundle }); 53 await page.evaluate(async () => { 54 const s = window.hcp065; 55 window.hcp065Fixture = await s.makeSetupFixture(); 56 const opened = await s.openBrowserDatabase(); 57 if (opened.state !== 'ready') throw new Error(opened.reason); 58 window.hcp065Db = opened.owner; 59 const repo = s.createPublicQuotaRepository( 60 opened.owner, 61 window.hcp065Fixture.owner 62 ); 63 if (!repo) throw new Error('missing owner repository'); 64 window.hcp065Repo = repo; 65 }); 66 }); 67 test.afterEach(async ({ page }) => { 68 if (!page.isClosed()) 69 await page.evaluate(() => { 70 window.hcp065Fixture?.close(); 71 if (window.hcp065Db) window.hcp065.closeBrowserDatabase(window.hcp065Db); 72 }); 73 }); 74 test('Connect and review alone write nothing; explicit durable capture equals the exact preview', async ({ 75 page 76 }) => { 77 const r = await page.evaluate(async () => { 78 const s = window.hcp065, 79 f = window.hcp065Fixture, 80 review = await s.reviewInboxSetup( 81 f.identity, 82 f.own.resolver, 83 s.fixturePolicy(), 84 s.setupInput(), 85 () => 101 86 ); 87 if (review.status !== 'review') throw new Error(review.reason); 88 const preview = s.inboxSetupPreview(review.review)!, 89 before = await s.inspectPublicStorage(window.hcp065Repo); 90 const count = before.ok 91 ? s.publicInventorySnapshot(window.hcp065Repo, before.value)?.rows.length 92 : -1; 93 const id = crypto.randomUUID(), 94 saved = await s.saveInboxPreferenceOperation( 95 window.hcp065Repo, 96 review.review, 97 f.own.resolver, 98 id, 99 'reviewed_global_inbox_replacement' 100 ); 101 return { 102 count, 103 preview, 104 status: saved.status, 105 row: 106 saved.status === 'saved' 107 ? s.publicRecordSnapshot(saved.record, f.owner, id) 108 : null, 109 counts: f.counts() 110 }; 111 }); 112 expect(r.count).toBe(0); 113 expect(r.status).toBe('saved'); 114 expect(r.row?.family).toBe('preference_operation'); 115 if (r.row?.family === 'preference_operation') { 116 expect(r.row.capture.wire).toBe(r.preview.wire); 117 expect(r.row.source.wire).toBe(r.preview.originalWire); 118 expect(r.row.capture.targets).toEqual(r.preview.destinations); 119 expect(r.row.artifact).toBeNull(); 120 expect(r.row.receipts).toEqual([]); 121 } 122 expect(r.counts.signs).toBe(0); 123 }); 124 test('same command replay retains original; conflicting preview never overwrites it', async ({ 125 page 126 }) => { 127 const r = await page.evaluate(async () => { 128 const s = window.hcp065, 129 f = window.hcp065Fixture, 130 id = crypto.randomUUID(); 131 const first = await s.reviewInboxSetup( 132 f.identity, 133 f.own.resolver, 134 s.fixturePolicy(), 135 s.setupInput(), 136 () => 101 137 ); 138 if (first.status !== 'review') throw new Error(first.reason); 139 const a = await s.saveInboxPreferenceOperation( 140 window.hcp065Repo, 141 first.review, 142 f.own.resolver, 143 id, 144 'reviewed_global_inbox_replacement' 145 ), 146 b = await s.saveInboxPreferenceOperation( 147 window.hcp065Repo, 148 first.review, 149 f.own.resolver, 150 id, 151 'reviewed_global_inbox_replacement' 152 ); 153 const different = await s.reviewInboxSetup( 154 f.identity, 155 f.own.resolver, 156 s.fixturePolicy(), 157 s.setupInput({ createdAt: 102 }), 158 () => 102 159 ); 160 if (different.status !== 'review') throw new Error(different.reason); 161 const c = await s.saveInboxPreferenceOperation( 162 window.hcp065Repo, 163 different.review, 164 f.own.resolver, 165 id, 166 'reviewed_global_inbox_replacement' 167 ); 168 return { 169 a: a.status, 170 b: b.status, 171 c: c.status, 172 original: 173 a.status === 'saved' ? s.publicRecordWire(a.record, f.owner, id) : null, 174 retained: 175 b.status === 'existing' 176 ? s.publicRecordWire(b.record, f.owner, id) 177 : null, 178 counts: f.counts() 179 }; 180 }); 181 expect(r.a).toBe('saved'); 182 expect(r.b).toBe('existing'); 183 expect(r.c).toBe('conflict'); 184 expect(r.retained).toBe(r.original); 185 expect(r.counts.signs).toBe(0); 186 }); 187 test('changed known head prevents durable admission before any extension or publication effect', async ({ 188 page 189 }) => { 190 const r = await page.evaluate(async () => { 191 const s = window.hcp065, 192 f = window.hcp065Fixture, 193 r = await s.reviewInboxSetup( 194 f.identity, 195 f.own.resolver, 196 s.fixturePolicy(), 197 s.setupInput(), 198 () => 101 199 ); 200 if (r.status !== 'review') throw new Error(r.reason); 201 const fresh = f.resolve(f.newerEvent); 202 const saved = await s.saveInboxPreferenceOperation( 203 window.hcp065Repo, 204 r.review, 205 fresh.resolver, 206 crypto.randomUUID(), 207 'reviewed_global_inbox_replacement' 208 ); 209 const inv = await s.inspectPublicStorage(window.hcp065Repo); 210 return { 211 status: saved.status, 212 count: inv.ok 213 ? s.publicInventorySnapshot(window.hcp065Repo, inv.value)?.rows.length 214 : -1, 215 counts: f.counts() 216 }; 217 }); 218 expect(r.status).toBe('conflict'); 219 expect(r.count).toBe(0); 220 expect(r.counts.signs).toBe(0); 221 }); 222 test('missing Web Locks or consent keeps browse usable and creates no preference', async ({ 223 page 224 }) => { 225 const r = await page.evaluate(async () => { 226 const s = window.hcp065, 227 f = window.hcp065Fixture, 228 r = await s.reviewInboxSetup( 229 f.identity, 230 f.own.resolver, 231 s.fixturePolicy(), 232 s.setupInput(), 233 () => 101 234 ); 235 if (r.status !== 'review') throw new Error(r.reason); 236 const denied = await s.saveInboxPreferenceOperation( 237 window.hcp065Repo, 238 r.review, 239 f.own.resolver, 240 crypto.randomUUID(), 241 'connect' 242 ); 243 Object.defineProperty(navigator, 'locks', { 244 configurable: true, 245 value: undefined 246 }); 247 const unsupported = await s.saveInboxPreferenceOperation( 248 window.hcp065Repo, 249 r.review, 250 f.own.resolver, 251 crypto.randomUUID(), 252 'reviewed_global_inbox_replacement' 253 ); 254 const inv = await s.inspectPublicStorage(window.hcp065Repo); 255 return { 256 denied: denied.status, 257 unsupported: unsupported.status, 258 count: inv.ok 259 ? s.publicInventorySnapshot(window.hcp065Repo, inv.value)?.rows.length 260 : -1 261 }; 262 }); 263 expect(r).toEqual({ 264 denied: 'invalid', 265 unsupported: 'unavailable', 266 count: 0 267 }); 268 expect(page.url()).toContain('/search'); 269 });