inbox-setup-view.spec.ts (9654B)
1 import { test, expect } from '@playwright/test'; 2 import { build } from 'vite'; 3 import { svelte } from '@sveltejs/vite-plugin-svelte'; 4 import { fileURLToPath } from 'node:url'; 5 import { once } from 'node:events'; 6 import { WebSocketServer } from 'ws'; 7 import { createStaticHarness } from '../integration/harness/static.ts'; 8 import type * as Fixture from './harness/inbox-setup-view.ts'; 9 declare global { 10 interface Window { 11 hcp070: typeof Fixture; 12 hcp070Fixture: Awaited<ReturnType<typeof Fixture.renderFixture>>; 13 hcp070Release?: () => void; 14 hcp070Provider?: Awaited<ReturnType<typeof Fixture.makeSetupFixture>>; 15 } 16 } 17 let server: Awaited<ReturnType<typeof createStaticHarness>>; 18 let socketServer: WebSocketServer, bundle: string, endpoint: string; 19 let frames: string[] = []; 20 test.beforeAll(async () => { 21 server = await createStaticHarness(); 22 socketServer = new WebSocketServer({ 23 host: '127.0.0.1', 24 port: 0, 25 maxPayload: 65536 26 }); 27 await once(socketServer, 'listening'); 28 const address = socketServer.address(); 29 if (!address || typeof address === 'string') 30 throw new Error('missing loopback'); 31 endpoint = 'ws://127.0.0.1:' + address.port; 32 socketServer.on('connection', (socket) => 33 socket.on('message', (bytes) => { 34 const frame = JSON.parse( 35 Buffer.from(bytes as ArrayBuffer).toString() 36 ) as [string, { id: string }]; 37 if (frame[0] === 'EVENT') { 38 frames.push(JSON.stringify(frame)); 39 socket.send(JSON.stringify(['OK', frame[1].id, true, ''])); 40 } 41 }) 42 ); 43 const result = await build({ 44 configFile: false, 45 logLevel: 'silent', 46 plugins: [svelte({ configFile: false })], 47 resolve: { conditions: ['browser'] }, 48 build: { 49 write: false, 50 minify: false, 51 lib: { 52 entry: fileURLToPath( 53 new URL('./harness/inbox-setup-view.ts', import.meta.url) 54 ), 55 name: 'hcp070', 56 formats: ['iife'] 57 } 58 } 59 }); 60 const output = Array.isArray(result) ? result[0] : result; 61 if (!('output' in output)) throw new Error('missing fixture output'); 62 const chunks = output.output.filter((row) => row.type === 'chunk'); 63 expect(chunks).toHaveLength(1); 64 bundle = chunks[0].code; 65 }); 66 test.afterAll(async () => { 67 for (const socket of socketServer.clients) socket.terminate(); 68 await new Promise<void>((resolve) => socketServer.close(() => resolve())); 69 await server.close(); 70 }); 71 test.beforeEach(async ({ page }) => { 72 frames = []; 73 await page.addInitScript( 74 ({ endpoint }) => { 75 const Native = window.WebSocket; 76 window.WebSocket = class extends Native { 77 constructor(url: string | URL, protocols?: string | string[]) { 78 if (String(url) !== 'wss://discovery.example.org/') 79 throw new Error('unexpected destination'); 80 super(endpoint, protocols); 81 } 82 }; 83 }, 84 { endpoint } 85 ); 86 await page.goto(server.url + '/search'); 87 await page.addScriptTag({ content: bundle }); 88 }); 89 test.afterEach(async ({ page }) => { 90 if (!page.isClosed()) 91 await page.evaluate(async () => { 92 window.hcp070Release?.(); 93 await window.hcp070Fixture?.close(); 94 window.hcp070Provider?.close(); 95 }); 96 }); 97 test('production shared gate reacquires its genuine controller after disconnect and reconnect', async ({ 98 page 99 }) => { 100 await page.goto(server.url + '/messages'); 101 await page.addScriptTag({ content: bundle }); 102 await page.evaluate(async () => { 103 window.hcp070Provider = await window.hcp070.makeSetupFixture(); 104 }); 105 const main = page.locator('#main-content'); 106 const panel = main.getByRole('region', { name: 'Inbox setup' }); 107 await main.getByRole('button', { name: 'Connect extension' }).click(); 108 await main.getByRole('button', { name: 'Check messaging support' }).click(); 109 await expect(panel.getByText(/^Public key:/)).toBeVisible(); 110 await page.locator('header summary').click(); 111 await page 112 .locator('header') 113 .getByRole('button', { name: 'Disconnect', exact: true }) 114 .click(); 115 await expect(panel.getByText(/^Public key:/)).toHaveCount(0); 116 await main.getByRole('button', { name: 'Connect extension' }).click(); 117 await main.getByRole('button', { name: 'Check messaging support' }).click(); 118 await expect(panel.getByText(/^Public key:/)).toBeVisible(); 119 expect(frames).toEqual([]); 120 }); 121 test('incomplete actual lookup shows uncertainty without an empty-success claim', async ({ 122 page 123 }) => { 124 await page.evaluate(async () => { 125 window.hcp070Fixture = await window.hcp070.renderFixture('incomplete'); 126 }); 127 await page.getByRole('button', { name: 'Check current preference' }).click(); 128 await expect( 129 page.getByText( 130 'The lookup is incomplete. Your current inbox preference is unknown.' 131 ) 132 ).toBeVisible(); 133 await expect(page.getByRole('button', { name: 'Enable inbox' })).toHaveCount( 134 0 135 ); 136 expect(frames).toEqual([]); 137 }); 138 test('inline review and Not now retain parent memory text and never send', async ({ 139 page 140 }) => { 141 await page.evaluate(async () => { 142 window.hcp070Fixture = await window.hcp070.renderFixture(); 143 }); 144 await page.getByLabel('Memory-only enquiry').fill('Keep this unsent request'); 145 await page.getByRole('button', { name: 'Check current preference' }).click(); 146 await page.getByLabel('wss://inbox.example.org', { exact: true }).check(); 147 await page.getByRole('button', { name: 'Review inbox change' }).click(); 148 await expect( 149 page.getByText( 150 /This replaces your public inbox preference for other clients/ 151 ) 152 ).toBeVisible(); 153 await expect( 154 page 155 .getByRole('list', { name: 'Current observed inbox destinations' }) 156 .getByText('wss://old.example.org', { exact: true }) 157 ).toBeVisible(); 158 await expect( 159 page 160 .getByRole('list', { name: 'Proposed public inbox destinations' }) 161 .getByText('wss://old.example.org', { exact: true }) 162 ).toBeVisible(); 163 await expect( 164 page 165 .getByRole('list', { name: 'Proposed public inbox destinations' }) 166 .getByText('wss://inbox.example.org', { exact: true }) 167 ).toBeVisible(); 168 await page.getByRole('button', { name: 'Not now' }).click(); 169 await expect(page.getByLabel('Memory-only enquiry')).toHaveValue( 170 'Keep this unsent request' 171 ); 172 await expect(page.getByText('Explicit enquiry sends: 0')).toBeVisible(); 173 expect(frames).toEqual([]); 174 }); 175 test('compatible observed preference offers Unlock and never a redundant replacement', async ({ 176 page 177 }) => { 178 await page.evaluate(async () => { 179 window.hcp070Fixture = await window.hcp070.renderFixture('compatible'); 180 }); 181 await page.getByRole('button', { name: 'Check current preference' }).click(); 182 await expect( 183 page.getByRole('button', { name: 'Unlock inbox' }) 184 ).toBeVisible(); 185 await expect(page.getByRole('button', { name: 'Enable inbox' })).toHaveCount( 186 0 187 ); 188 await page.getByRole('button', { name: 'Unlock inbox' }).click(); 189 await expect( 190 page.getByText('Inbox access has not been qualified and exercised.') 191 ).toBeVisible(); 192 expect(frames).toEqual([]); 193 }); 194 test('Enable displays real approval wait, records named preference acceptance and never sends an enquiry', async ({ 195 page 196 }) => { 197 await page.evaluate(async () => { 198 const f = await window.hcp070.renderFixture('publish'); 199 window.hcp070Fixture = f; 200 const wait = new Promise<void>((resolve) => { 201 window.hcp070Release = resolve; 202 }); 203 f.beforeSign(() => wait); 204 }); 205 await page.getByRole('button', { name: 'Check current preference' }).click(); 206 await page.getByLabel('wss://inbox.example.org', { exact: true }).check(); 207 await page.getByRole('button', { name: 'Review inbox change' }).click(); 208 await page.getByRole('button', { name: 'Enable inbox' }).click(); 209 await expect( 210 page.getByText( 211 'Preparing your inbox setup. Extension approvals or named relay acceptance may be pending. Another approval may be needed.' 212 ) 213 ).toBeVisible(); 214 expect(frames).toEqual([]); 215 await page.evaluate(() => window.hcp070Release?.()); 216 await expect( 217 page.getByText('Relay acceptance and exact readback are separate facts.') 218 ).toBeVisible(); 219 await expect( 220 page.getByText('Accepted by wss://discovery.example.org') 221 ).toBeVisible(); 222 await expect(page.getByText('Explicit enquiry sends: 0')).toBeVisible(); 223 expect(frames).toHaveLength(1); 224 expect((JSON.parse(frames[0]) as [string, { kind: number }])[1].kind).toBe( 225 10050 226 ); 227 }); 228 test('Not now during real approval wait prevents a late signature from publishing', async ({ 229 page 230 }) => { 231 await page.evaluate(async () => { 232 const f = await window.hcp070.renderFixture('publish'); 233 window.hcp070Fixture = f; 234 const wait = new Promise<void>((resolve) => { 235 window.hcp070Release = resolve; 236 }); 237 f.beforeSign(() => wait); 238 }); 239 await page.getByRole('button', { name: 'Check current preference' }).click(); 240 await page.getByLabel('wss://inbox.example.org', { exact: true }).check(); 241 await page.getByRole('button', { name: 'Review inbox change' }).click(); 242 await page.getByRole('button', { name: 'Enable inbox' }).click(); 243 await expect( 244 page.getByText( 245 'Preparing your inbox setup. Extension approvals or named relay acceptance may be pending. Another approval may be needed.' 246 ) 247 ).toBeVisible(); 248 await page.getByRole('button', { name: 'Not now' }).click(); 249 await page.evaluate(() => window.hcp070Release?.()); 250 await expect( 251 page.getByText( 252 'Inbox setup is paused. Your unsent text stays in this page.' 253 ) 254 ).toBeVisible(); 255 await expect(page.getByText('Explicit enquiry sends: 0')).toBeVisible(); 256 expect(frames).toEqual([]); 257 });