multi-tab.spec.ts (6873B)
1 import { expect, test, 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 Reservations from './harness/private-send-reservations.ts'; 7 import type { RumorPlan } from '../../src/lib/messaging/rumor-plan.ts'; 8 declare global { 9 interface Window { 10 hcp135: typeof Reservations; 11 hcp135Fixture: Awaited<ReturnType<typeof Reservations.makeFixture>>; 12 hcp135Plan: RumorPlan; 13 } 14 } 15 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 16 test.beforeAll(async () => { 17 server = await createStaticHarness(); 18 const result = await build({ 19 configFile: false, 20 logLevel: 'silent', 21 build: { 22 write: false, 23 minify: false, 24 lib: { 25 entry: fileURLToPath( 26 new URL('./harness/private-send-reservations.ts', import.meta.url) 27 ), 28 name: 'hcp135', 29 formats: ['iife'] 30 } 31 } 32 }); 33 const output = Array.isArray(result) ? result[0] : result; 34 if (!('output' in output)) throw Error('missing bundle'); 35 const chunks = output.output.filter((item) => item.type === 'chunk'); 36 expect(chunks).toHaveLength(1); 37 bundle = chunks[0].code; 38 console.log( 39 JSON.stringify({ 40 fixture: 'HCP135_NATIVE_MULTITAB_ORIGINAL_COMMAND', 41 bundleSha256: createHash('sha256').update(bundle).digest('hex'), 42 moduleIds: Object.keys(chunks[0].modules), 43 qualification: 44 'Real Chromium IDB/WebLocks and actual page loss; controlled provider/clock, not installed extension, real clock or independent-client Q.' 45 }) 46 ); 47 }); 48 test.afterAll(async () => server.close()); 49 async function load(page: Page, shared?: Reservations.Shared) { 50 await page.goto(server.url + '/search'); 51 await page.addScriptTag({ content: bundle }); 52 return page.evaluate(async (shared) => { 53 window.hcp135Fixture = await window.hcp135.makeFixture(shared); 54 window.hcp135Plan = window.hcp135Fixture.plan(); 55 return window.hcp135Fixture.shared(); 56 }, shared); 57 } 58 async function invoke(page: Page) { 59 return page.evaluate(async () => { 60 const f = window.hcp135Fixture, 61 s = window.hcp135, 62 before = f.counts(); 63 const result = await f.reserve(s.command, window.hcp135Plan); 64 return { 65 status: result.status, 66 before, 67 after: f.counts(), 68 metadata: 69 result.status === 'reserved' || result.status === 'existing' 70 ? s.reservedSendSnapshot(result.identity) 71 : undefined, 72 wire: 73 result.status === 'reserved' || result.status === 'existing' 74 ? s.reservedSendRumorWire(result.identity) 75 : undefined, 76 rows: await f.rows() 77 }; 78 }); 79 } 80 test('same command races retain one original rumor after actual tab loss; distinct intent stays distinct', async ({ 81 page, 82 context 83 }) => { 84 const shared = await load(page), 85 second = await context.newPage(); 86 await load(second, shared); 87 const raced = await Promise.all([invoke(page), invoke(second)]); 88 expect(raced.filter((r) => r.status === 'reserved')).toHaveLength(1); 89 expect( 90 raced.filter((r) => r.status === 'busy' || r.status === 'existing') 91 ).toHaveLength(1); 92 for (const r of raced) expect(r.after).toEqual(r.before); 93 const original = raced.find((r) => r.status === 'reserved')!; 94 expect(original.rows).toHaveLength(1); 95 expect(JSON.parse((original.rows[0] as { wire: string }).wire)).toEqual( 96 original.metadata 97 ); 98 expect(original.wire).toContain('Private reservation sentinel'); 99 expect(JSON.stringify(original.rows)).not.toContain( 100 'Private reservation sentinel' 101 ); 102 const joined = await invoke(second); 103 expect(joined.status).toBe('existing'); 104 expect(joined.metadata).toEqual(original.metadata); 105 expect(joined.wire).toBe(original.wire); 106 expect(joined.rows).toEqual(original.rows); 107 expect(joined.after).toEqual(joined.before); 108 await page.close(); 109 const survived = await invoke(second); 110 expect(survived.status).toBe('existing'); 111 expect(survived.metadata).toEqual(original.metadata); 112 expect(survived.wire).toBe(original.wire); 113 expect(survived.rows).toEqual(original.rows); 114 expect(survived.after).toEqual(survived.before); 115 const distinct = await second.evaluate(async () => { 116 const f = window.hcp135Fixture, 117 s = window.hcp135, 118 before = f.counts(); 119 const id = '12345678-1234-4234-8234-123456789abd'; 120 const collision = await f.reserve(id, f.plan()), 121 beforeProgress = await f.rows(); 122 const observed = f.shared().milliseconds; 123 f.setTime(observed + 1000); 124 const next = await f.reserve(id, f.plan()); 125 if (next.status !== 'reserved') 126 throw Error('expected distinct source reservation'); 127 return { 128 collision: collision.status, 129 beforeProgress, 130 before, 131 after: f.counts(), 132 metadata: s.reservedSendSnapshot(next.identity), 133 wire: s.reservedSendRumorWire(next.identity), 134 rows: await f.rows(), 135 observedSeconds: Math.floor((observed + 1000) / 1000) 136 }; 137 }); 138 expect(distinct.collision).toBe('clock_conflict'); 139 expect(distinct.beforeProgress).toEqual(original.rows); 140 expect(distinct.metadata?.createdAt).toBe(distinct.observedSeconds); 141 expect(distinct.metadata?.createdAt).toBe(original.metadata!.createdAt + 1); 142 expect(distinct.metadata?.id).not.toBe(original.metadata!.id); 143 expect(distinct.metadata?.rumorHash).not.toBe(original.metadata!.rumorHash); 144 expect(distinct.after).toEqual(distinct.before); 145 expect(distinct.rows).toHaveLength(2); 146 expect(distinct.rows).toContainEqual(original.rows[0]); 147 expect( 148 distinct.rows.map( 149 (row) => JSON.parse((row as { wire: string }).wire) as unknown 150 ) 151 ).toContainEqual(distinct.metadata); 152 const wire = JSON.parse(distinct.wire!) as { 153 created_at: number; 154 tags: string[][]; 155 }; 156 expect(wire.created_at).toBe(distinct.observedSeconds); 157 expect(wire.tags).toEqual( 158 (JSON.parse(original.wire!) as { tags: string[][] }).tags 159 ); 160 expect(JSON.stringify(distinct.rows)).not.toContain( 161 'Private reservation sentinel' 162 ); 163 }); 164 test('missing WebLocks refuses new reservations in both tabs while guest search remains usable', async ({ 165 page, 166 context 167 }) => { 168 const shared = await load(page), 169 second = await context.newPage(); 170 await load(second, shared); 171 for (const tab of [page, second]) { 172 await tab.evaluate(() => 173 Object.defineProperty(navigator, 'locks', { 174 configurable: true, 175 value: undefined 176 }) 177 ); 178 const result = await invoke(tab); 179 expect(result.status).toBe('unavailable'); 180 expect(result.rows).toEqual([]); 181 expect(result.after).toEqual(result.before); 182 await expect( 183 tab.getByRole('heading', { name: 'Search food', exact: true }) 184 ).toBeVisible(); 185 } 186 });