inbox-list.spec.ts (15979B)
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-list.ts'; 9 declare global { 10 interface Window { 11 hcp102: typeof Fixture; 12 hcp102Fixture: Awaited<ReturnType<typeof Fixture.renderFixture>>; 13 hcp102Pending?: Promise<boolean>; 14 } 15 } 16 let server: Awaited<ReturnType<typeof createStaticHarness>>, 17 sockets: WebSocketServer, 18 endpoint: string, 19 bundle: string; 20 let requests: unknown[][] = [], 21 events: unknown[][] = [], 22 closes: unknown[][] = [], 23 olderEvent: unknown; 24 test.beforeAll(async () => { 25 server = await createStaticHarness(); 26 sockets = new WebSocketServer({ 27 host: '127.0.0.1', 28 port: 0, 29 maxPayload: 65536 30 }); 31 await once(sockets, 'listening'); 32 const address = sockets.address(); 33 if (!address || typeof address === 'string') throw Error('no loopback'); 34 endpoint = 'ws://127.0.0.1:' + address.port; 35 sockets.on('connection', (socket) => 36 socket.on('message', (bytes) => { 37 const raw = Buffer.isBuffer(bytes) 38 ? bytes 39 : Array.isArray(bytes) 40 ? Buffer.concat(bytes) 41 : Buffer.from(bytes); 42 const frame = JSON.parse(raw.toString()) as unknown[]; 43 if (frame[0] === 'EVENT') events.push(frame); 44 if (frame[0] === 'CLOSE') closes.push(frame); 45 if (frame[0] === 'REQ') { 46 requests.push(frame); 47 const filter = frame[2] as Record<string, unknown>; 48 if (filter.limit !== 0) { 49 if (filter.until !== undefined && olderEvent) 50 socket.send(JSON.stringify(['EVENT', frame[1], olderEvent])); 51 socket.send(JSON.stringify(['EOSE', frame[1]])); 52 } 53 } 54 }) 55 ); 56 const result = await build({ 57 configFile: false, 58 logLevel: 'silent', 59 plugins: [svelte({ configFile: false })], 60 resolve: { 61 conditions: ['browser'], 62 alias: { 63 '$app/navigation': fileURLToPath( 64 new URL('./harness/inbox-list.ts', import.meta.url) 65 ) 66 } 67 }, 68 build: { 69 write: false, 70 minify: false, 71 lib: { 72 entry: fileURLToPath( 73 new URL('./harness/inbox-list.ts', import.meta.url) 74 ), 75 name: 'hcp102', 76 formats: ['iife'] 77 } 78 } 79 }); 80 const output = Array.isArray(result) ? result[0] : result; 81 if (!('output' in output)) throw Error('no bundle'); 82 const chunks = output.output.filter((row) => row.type === 'chunk'); 83 expect(chunks).toHaveLength(1); 84 bundle = chunks[0].code; 85 }); 86 test.afterAll(async () => { 87 for (const socket of sockets.clients) socket.terminate(); 88 await new Promise<void>((resolve) => sockets.close(() => resolve())); 89 await server.close(); 90 }); 91 test.beforeEach(async ({ page }) => { 92 requests = []; 93 events = []; 94 closes = []; 95 olderEvent = undefined; 96 await page.addInitScript( 97 ({ endpoint }) => { 98 const Native = window.WebSocket; 99 window.WebSocket = class extends Native { 100 constructor(url: string | URL, protocols?: string | string[]) { 101 if ( 102 ![ 103 'wss://archive.example.org', 104 'wss://discovery.example.org' 105 ].includes(new URL(String(url)).origin) 106 ) 107 throw Error('unapproved destination'); 108 super(endpoint, protocols); 109 } 110 }; 111 }, 112 { endpoint } 113 ); 114 await page.goto(server.url + '/search'); 115 await page.addScriptTag({ content: bundle }); 116 }); 117 test.afterEach(async ({ page }) => { 118 if (!page.isClosed()) 119 await page.evaluate(async () => { 120 window.hcp102Fixture?.settle(); 121 await window.hcp102Pending; 122 await window.hcp102Fixture?.close(); 123 }); 124 }); 125 async function ready( 126 page: import('@playwright/test').Page, 127 kind: 'inbound' | 'self_archive' | 'hostile_text' = 'inbound', 128 qualified = false, 129 variant: 'single' | 'duplicate' | 'mixed' = 'single' 130 ) { 131 await page.evaluate( 132 async ({ kind, qualified, variant }) => { 133 window.hcp102Fixture = await window.hcp102.renderFixture( 134 kind, 135 qualified, 136 variant 137 ); 138 }, 139 { kind, qualified, variant } 140 ); 141 await page.getByRole('button', { name: 'Check current preference' }).click(); 142 } 143 async function unlock(page: import('@playwright/test').Page) { 144 await page 145 .getByRole('button', { name: 'Unlock messages', exact: true }) 146 .click(); 147 await expect 148 .poll(() => page.evaluate(() => window.hcp102Fixture.list().status)) 149 .toBe('ready'); 150 } 151 test('production guest never renders request previews or background notification fiction', async ({ 152 page 153 }) => { 154 await page.goto(server.url + '/messages'); 155 await expect( 156 page.getByRole('heading', { name: 'Connect or unlock' }) 157 ).toBeVisible(); 158 await expect( 159 page.getByRole('heading', { name: 'Message requests' }) 160 ).toHaveCount(0); 161 await expect(page.getByText('Actual encrypted room fixture')).toHaveCount(0); 162 expect(requests).toEqual([]); 163 expect(events).toEqual([]); 164 }); 165 test('actual stored incoming ciphertext remains unpreviewed while original page is locked', async ({ 166 page 167 }) => { 168 await ready(page); 169 await expect( 170 page.getByRole('button', { name: 'Unlock messages', exact: true }) 171 ).toBeVisible(); 172 await expect(page.getByText('Actual encrypted room fixture')).toHaveCount(0); 173 await expect( 174 page.getByRole('heading', { name: 'Message requests' }) 175 ).toHaveCount(0); 176 expect(await page.evaluate(() => window.hcp102Fixture.delta())).toMatchObject( 177 { decrypts: 0, signs: 0, encrypts: 0 } 178 ); 179 expect(requests).toEqual([]); 180 }); 181 test('genuine authenticated incoming peer is one locally New request after explicit SDK decrypt', async ({ 182 page 183 }) => { 184 await ready(page); 185 await unlock(page); 186 await expect( 187 page.getByRole('heading', { name: 'Message requests', exact: true }) 188 ).toBeVisible(); 189 await expect( 190 page.getByRole('heading', { name: 'Conversations', exact: true }) 191 ).toHaveCount(0); 192 await expect( 193 page.getByText('Actual encrypted room fixture', { exact: true }) 194 ).toBeVisible(); 195 await expect(page.getByText('New', { exact: true })).toHaveCount(1); 196 const view = await page.evaluate(() => window.hcp102Fixture.list()); 197 expect(view.requests).toHaveLength(1); 198 expect(view.requests[0].peer).toBe( 199 await page.evaluate(() => window.hcp102Fixture.peer) 200 ); 201 expect(await page.evaluate(() => window.hcp102Fixture.delta())).toMatchObject( 202 { decrypts: 2, signs: 0, encrypts: 0 } 203 ); 204 expect(requests).toEqual([]); 205 }); 206 test('genuine self archive is a local conversation without outgoing New or empty request clutter', async ({ 207 page 208 }) => { 209 await ready(page, 'self_archive'); 210 await unlock(page); 211 await expect( 212 page.getByRole('heading', { name: 'Conversations', exact: true }) 213 ).toBeVisible(); 214 await expect( 215 page.getByRole('heading', { name: 'Message requests', exact: true }) 216 ).toHaveCount(0); 217 await expect(page.getByText('New', { exact: true })).toHaveCount(0); 218 await expect( 219 page.getByText('You: Actual encrypted room fixture', { exact: true }) 220 ).toBeVisible(); 221 expect( 222 (await page.evaluate(() => window.hcp102Fixture.list())).conversations 223 ).toHaveLength(1); 224 expect(events).toEqual([]); 225 }); 226 test('one authenticated pair with incoming and self archive stays one local conversation', async ({ 227 page 228 }) => { 229 await ready(page, 'inbound', false, 'mixed'); 230 await unlock(page); 231 const view = await page.evaluate(() => window.hcp102Fixture.list()); 232 expect(view.requests).toEqual([]); 233 expect(view.conversations).toHaveLength(1); 234 expect(view.conversations[0]).toMatchObject({ count: 2, unread: 1 }); 235 await expect(page.getByText('New', { exact: true })).toHaveCount(1); 236 expect(events).toEqual([]); 237 expect(requests).toEqual([]); 238 }); 239 test('two actual different outer wraps of one rumor produce one request and one local New', async ({ 240 page 241 }) => { 242 await ready(page, 'inbound', false, 'duplicate'); 243 await unlock(page); 244 const view = await page.evaluate(() => window.hcp102Fixture.list()); 245 expect(view.requests).toHaveLength(1); 246 expect(view.requests[0]).toMatchObject({ count: 1, unread: 1 }); 247 expect( 248 (await page.evaluate(() => window.hcp102Fixture.native())).received 249 ).toHaveLength(2); 250 expect(await page.evaluate(() => window.hcp102Fixture.delta())).toMatchObject( 251 { decrypts: 4 } 252 ); 253 await expect(page.getByText('New', { exact: true })).toHaveCount(1); 254 }); 255 test('actual original local read flag suppresses New without changing request acceptance or notifying a peer', async ({ 256 page 257 }) => { 258 await ready(page); 259 await unlock(page); 260 await expect(page.getByText('New', { exact: true })).toHaveCount(1); 261 const saved = await page.evaluate(() => window.hcp102Fixture.markRead()); 262 expect(saved?.status).toBe('saved'); 263 await page.evaluate(() => window.hcp102Fixture.next()); 264 await expect(page.getByText('New', { exact: true })).toHaveCount(0); 265 expect( 266 (await page.evaluate(() => window.hcp102Fixture.list())).requests 267 ).toHaveLength(1); 268 expect(events).toEqual([]); 269 expect(requests).toEqual([]); 270 }); 271 test('explicit original row mapping returns opaque local UUID and does not mark read or persist plaintext', async ({ 272 page 273 }) => { 274 await ready(page); 275 await unlock(page); 276 const before = await page.evaluate(() => window.hcp102Fixture.native()); 277 expect(before.pairs).toEqual([]); 278 await page.getByRole('button', { name: /Open conversation/ }).click(); 279 await expect 280 .poll(() => page.evaluate(() => window.hcp102Fixture.navigations().length)) 281 .toBe(1); 282 const href = await page.evaluate(() => window.hcp102Fixture.navigations()[0]); 283 expect(href).toMatch( 284 /^\/messages\/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ 285 ); 286 const after = await page.evaluate(() => window.hcp102Fixture.native()); 287 expect(after.pairs).toHaveLength(1); 288 expect(after.received).toEqual(before.received); 289 expect(JSON.stringify(after)).not.toContain('Actual encrypted room fixture'); 290 await expect(page.getByText('New', { exact: true })).toHaveCount(1); 291 expect(events).toEqual([]); 292 expect(requests).toEqual([]); 293 }); 294 test('copied page and scalar peer arguments cannot import rows or native mapping/history effects', async ({ 295 page 296 }) => { 297 await ready(page); 298 await unlock(page); 299 const before = await page.evaluate(() => window.hcp102Fixture.native()), 300 delta = await page.evaluate(() => window.hcp102Fixture.delta()), 301 copied = await page.evaluate(() => window.hcp102Fixture.copied()); 302 expect(copied).toMatchObject({ 303 list: { status: 'unavailable', requests: [], conversations: [] }, 304 older: false 305 }); 306 expect(copied.open).toBeUndefined(); 307 expect(await page.evaluate(() => window.hcp102Fixture.native())).toEqual( 308 before 309 ); 310 expect(await page.evaluate(() => window.hcp102Fixture.delta())).toEqual( 311 delta 312 ); 313 expect(requests).toEqual([]); 314 expect(events).toEqual([]); 315 }); 316 test('Stop removes decrypted rows and denies stale original row action before any native change', async ({ 317 page 318 }) => { 319 await ready(page); 320 await unlock(page); 321 const before = await page.evaluate(() => window.hcp102Fixture.native()); 322 await page 323 .getByRole('button', { name: 'Lock messages', exact: true }) 324 .click(); 325 await expect(page.getByText('Actual encrypted room fixture')).toHaveCount(0); 326 await expect(page.getByText('New', { exact: true })).toHaveCount(0); 327 expect( 328 await page.evaluate(() => window.hcp102Fixture.open()) 329 ).toBeUndefined(); 330 expect(await page.evaluate(() => window.hcp102Fixture.native())).toEqual( 331 before 332 ); 333 expect(requests).toEqual([]); 334 expect(events).toEqual([]); 335 }); 336 test('logout during actual held decrypt cannot render old-generation previews after SDK settlement', async ({ 337 page 338 }) => { 339 await ready(page); 340 await page.evaluate(() => { 341 window.hcp102Fixture.mode('hold_decrypt'); 342 window.hcp102Pending = window.hcp102Fixture.unlock(); 343 }); 344 await expect 345 .poll(() => page.evaluate(() => window.hcp102Fixture.pending())) 346 .toBe(true); 347 await page.evaluate(() => window.hcp102Fixture.logout()); 348 await expect(page.getByText('Actual encrypted room fixture')).toHaveCount(0); 349 await page.evaluate(async () => { 350 window.hcp102Fixture.settle(); 351 await window.hcp102Pending; 352 }); 353 await expect(page.getByText('Actual encrypted room fixture')).toHaveCount(0); 354 expect( 355 (await page.evaluate(() => window.hcp102Fixture.list())).requests 356 ).toEqual([]); 357 expect(events).toEqual([]); 358 expect(requests).toEqual([]); 359 }); 360 test('partial unlocked local list has visible limitations and never remote Seen or a live badge', async ({ 361 page 362 }) => { 363 await ready(page, 'hostile_text'); 364 await unlock(page); 365 await expect( 366 page.getByText( 367 'Inbox access has not been qualified and exercised. Unlocked local copies do not prove a successful relay check.', 368 { exact: true } 369 ) 370 ).toBeVisible(); 371 await expect(page.getByText(/https:\/\/untrusted.example.org/)).toBeVisible(); 372 await expect(page.locator('a[href*="untrusted.example.org"]')).toHaveCount(0); 373 await expect( 374 page.getByText(/^(Live|Seen|Read by|Order accepted)$/) 375 ).toHaveCount(0); 376 await expect( 377 page.getByRole('heading', { name: 'Conversations', exact: true }) 378 ).toHaveCount(0); 379 expect(events).toEqual([]); 380 expect(requests).toEqual([]); 381 }); 382 test('unqualified original Load older stays unavailable without private REQ or SDK work', async ({ 383 page 384 }) => { 385 await ready(page); 386 await unlock(page); 387 const before = await page.evaluate(() => window.hcp102Fixture.delta()); 388 expect(await page.evaluate(() => window.hcp102Fixture.older())).toBe(false); 389 expect(await page.evaluate(() => window.hcp102Fixture.delta())).toEqual( 390 before 391 ); 392 expect(events).toEqual([]); 393 expect(requests).toEqual([]); 394 }); 395 test('qualified explicit Load older uses original outer cursor and retains a real relay-only cipher without automatic decrypt', async ({ 396 page 397 }) => { 398 await ready(page, 'inbound', true); 399 await unlock(page); 400 await page 401 .getByRole('button', { name: 'Check for messages', exact: true }) 402 .click(); 403 await expect.poll(() => requests.length).toBe(3); 404 await expect 405 .poll(() => 406 page.evaluate(() => window.hcp102Fixture.snapshot().lastCheckedAt) 407 ) 408 .not.toBeNull(); 409 const before = await page.evaluate(() => window.hcp102Fixture.native()), 410 delta = await page.evaluate(() => window.hcp102Fixture.delta()); 411 olderEvent = JSON.parse( 412 await page.evaluate(() => window.hcp102Fixture.olderWire) 413 ); 414 await page 415 .getByRole('button', { name: 'Load older messages', exact: true }) 416 .click(); 417 await expect.poll(() => requests.length).toBe(4); 418 const filter = requests[3][2] as Record<string, unknown>; 419 expect(filter.limit).toBe(200); 420 expect(filter.until).toBe(1699999800); 421 await expect 422 .poll(() => 423 page.evaluate( 424 async () => (await window.hcp102Fixture.native()).received.length 425 ) 426 ) 427 .toBe(before.received.length + 1); 428 expect(await page.evaluate(() => window.hcp102Fixture.delta())).toEqual( 429 delta 430 ); 431 await expect( 432 page.getByRole('button', { name: 'Read new messages (1)', exact: true }) 433 ).toBeVisible(); 434 expect( 435 (await page.evaluate(() => window.hcp102Fixture.list())).requests 436 ).toHaveLength(1); 437 await page 438 .getByRole('button', { name: 'Read new messages (1)', exact: true }) 439 .click(); 440 await expect 441 .poll(() => page.evaluate(() => window.hcp102Fixture.delta().decrypts)) 442 .toBe(delta.decrypts + 2); 443 expect( 444 (await page.evaluate(() => window.hcp102Fixture.list())).requests[0].count 445 ).toBe(1); 446 expect(events).toEqual([]); 447 expect( 448 requests.filter( 449 (frame) => (frame[2] as Record<string, unknown>).limit === 0 450 ) 451 ).toHaveLength(1); 452 });