conversation-thread.spec.ts (18263B)
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/conversation-thread.ts'; 9 declare global { 10 interface Window { 11 hcp103: typeof Fixture; 12 hcp103Fixture: Awaited<ReturnType<typeof Fixture.renderFixture>>; 13 hcp103Pending?: Promise<boolean>; 14 } 15 } 16 let server: Awaited<ReturnType<typeof createStaticHarness>>, 17 sockets: WebSocketServer, 18 endpoint: string, 19 bundle: string; 20 let events: unknown[][] = [], 21 requests: unknown[][] = []; 22 test.beforeAll(async () => { 23 server = await createStaticHarness(); 24 sockets = new WebSocketServer({ 25 host: '127.0.0.1', 26 port: 0, 27 maxPayload: 65536 28 }); 29 await once(sockets, 'listening'); 30 const address = sockets.address(); 31 if (!address || typeof address === 'string') throw Error('no loopback'); 32 endpoint = 'ws://127.0.0.1:' + address.port; 33 sockets.on('connection', (socket) => 34 socket.on('message', (bytes) => { 35 const raw = Buffer.isBuffer(bytes) 36 ? bytes 37 : Array.isArray(bytes) 38 ? Buffer.concat(bytes) 39 : Buffer.from(bytes), 40 frame = JSON.parse(raw.toString()) as unknown[]; 41 if (frame[0] === 'EVENT') { 42 events.push(frame); 43 const event = frame[1] as { id: string }; 44 socket.send(JSON.stringify(['OK', event.id, true, ''])); 45 } 46 if (frame[0] === 'REQ') { 47 requests.push(frame); 48 socket.send(JSON.stringify(['EOSE', frame[1]])); 49 } 50 }) 51 ); 52 const result = await build({ 53 configFile: false, 54 logLevel: 'silent', 55 plugins: [svelte({ configFile: false })], 56 resolve: { 57 conditions: ['browser'], 58 alias: { 59 '$app/navigation': fileURLToPath( 60 new URL('./harness/conversation-thread.ts', import.meta.url) 61 ) 62 } 63 }, 64 build: { 65 write: false, 66 minify: false, 67 lib: { 68 entry: fileURLToPath( 69 new URL('./harness/conversation-thread.ts', import.meta.url) 70 ), 71 name: 'hcp103', 72 formats: ['iife'] 73 } 74 } 75 }); 76 const output = Array.isArray(result) ? result[0] : result; 77 if (!('output' in output)) throw Error('no bundle'); 78 const chunks = output.output.filter((row) => row.type === 'chunk'); 79 expect(chunks).toHaveLength(1); 80 bundle = chunks[0].code; 81 }); 82 test.afterAll(async () => { 83 for (const socket of sockets.clients) socket.terminate(); 84 await new Promise<void>((resolve) => sockets.close(() => resolve())); 85 await server.close(); 86 }); 87 async function load(page: import('@playwright/test').Page) { 88 await page.addInitScript( 89 ({ endpoint }) => { 90 const Native = window.WebSocket; 91 window.WebSocket = class extends Native { 92 constructor(url: string | URL, protocols?: string | string[]) { 93 if ( 94 ![ 95 'wss://peer.example.org', 96 'wss://archive.example.org', 97 'wss://discovery.example.org' 98 ].includes(new URL(String(url)).origin) 99 ) 100 throw Error('unapproved destination'); 101 super(endpoint, protocols); 102 } 103 }; 104 }, 105 { endpoint } 106 ); 107 await page.goto(server.url + '/search'); 108 await page.addScriptTag({ content: bundle }); 109 } 110 test.beforeEach(async ({ page }) => { 111 events = []; 112 requests = []; 113 await load(page); 114 }); 115 test.afterEach(async ({ page }) => { 116 if (!page.isClosed()) 117 await page.evaluate(async () => { 118 window.hcp103Fixture?.settle(); 119 await window.hcp103Pending; 120 await window.hcp103Fixture?.close(); 121 }); 122 }); 123 async function ready( 124 page: import('@playwright/test').Page, 125 kind: Fixture.Scenario = 'inbound', 126 selectedId?: string 127 ) { 128 await page.evaluate( 129 async ({ kind, selectedId }) => { 130 window.hcp103Fixture = await window.hcp103.renderFixture( 131 kind, 132 selectedId 133 ); 134 }, 135 { kind, selectedId } 136 ); 137 await page.getByRole('button', { name: 'Check current preference' }).click(); 138 } 139 async function unlock(page: import('@playwright/test').Page) { 140 await page 141 .getByRole('button', { name: 'Unlock messages', exact: true }) 142 .click(); 143 await expect 144 .poll(() => page.evaluate(() => window.hcp103Fixture.thread().status)) 145 .toBe('ready'); 146 await expect(page.locator('[data-message-id]')).not.toHaveCount(0); 147 } 148 const missing = '12345678-1234-4234-8234-123456789abd'; 149 function records(rows: unknown[]) { 150 return rows.map( 151 (row) => 152 JSON.parse((row as { wire: string }).wire) as { 153 outer: string; 154 read: { rumorHash: string } | null; 155 } 156 ); 157 } 158 test('production private guest load keeps generic title noindex safe inbox link and no private effects', async ({ 159 page 160 }) => { 161 await page.goto(server.url + '/messages/' + missing); 162 await expect( 163 page.getByRole('heading', { name: 'Connect or unlock' }) 164 ).toBeVisible(); 165 await expect( 166 page.getByRole('link', { name: 'Back to messages' }) 167 ).toHaveAttribute('href', '/messages'); 168 await expect(page).toHaveTitle('HarvestCircle'); 169 await expect(page.locator('meta[name="robots"]')).toHaveAttribute( 170 'content', 171 'noindex' 172 ); 173 await expect(page.locator('[data-message-id]')).toHaveCount(0); 174 expect(events).toEqual([]); 175 expect(requests).toEqual([]); 176 }); 177 test('actual owner UUID and ciphertext stay unpreviewed and unread before explicit unlock', async ({ 178 page 179 }) => { 180 await ready(page); 181 const native = await page.evaluate(() => window.hcp103Fixture.native()); 182 expect(native.pairs).toHaveLength(1); 183 expect(records(native.received).every((row) => row.read === null)).toBe(true); 184 await expect(page.locator('[data-message-id]')).toHaveCount(0); 185 await expect( 186 page.getByText('Actual encrypted thread fixture', { exact: true }) 187 ).toHaveCount(0); 188 expect(await page.evaluate(() => window.hcp103Fixture.delta())).toEqual({ 189 decrypts: 0, 190 signs: 0, 191 encrypts: 0 192 }); 193 expect(events).toEqual([]); 194 expect(requests).toEqual([]); 195 }); 196 test('original explicit decrypt renders authenticated sender inner time and plain body in one chronological column', async ({ 197 page 198 }) => { 199 await ready(page); 200 await unlock(page); 201 await expect(page.locator('[data-message-id]')).toHaveCount(1); 202 await expect( 203 page.getByText('Actual encrypted thread fixture', { exact: true }) 204 ).toBeVisible(); 205 await expect(page.locator('[data-message-id]')).toContainText('Peer'); 206 await expect(page.locator('[data-message-id] time')).toHaveAttribute( 207 'datetime', 208 '2023-11-14T22:13:20.000Z' 209 ); 210 expect(await page.evaluate(() => window.hcp103Fixture.delta())).toEqual({ 211 decrypts: 2, 212 signs: 0, 213 encrypts: 0 214 }); 215 expect(events).toEqual([]); 216 expect(requests).toEqual([]); 217 }); 218 test('two actual encrypted product contexts sort per message with their own citation and no current price claim', async ({ 219 page 220 }) => { 221 await ready(page, 'contexts'); 222 await unlock(page); 223 await expect(page.locator('[data-message-id]')).toHaveCount(3); 224 const texts = await page.locator('[data-message-id]').allTextContents(); 225 expect(texts[0]).toContain('Actual encrypted thread fixture'); 226 expect(texts[1]).toContain('About apples'); 227 expect(texts[2]).toContain('About carrots'); 228 const refs = await page.evaluate(() => ({ 229 apples: window.hcp103Fixture.apples, 230 carrots: window.hcp103Fixture.carrots 231 })); 232 await expect( 233 page.locator('[data-message-id]').nth(1).getByRole('link') 234 ).toHaveAttribute('href', '/products/' + refs.apples); 235 await expect( 236 page.locator('[data-message-id]').nth(2).getByRole('link') 237 ).toHaveAttribute('href', '/products/' + refs.carrots); 238 await expect(page.locator('[data-message-id]').nth(1)).toContainText( 239 'unverified' 240 ); 241 await expect(page.locator('[data-message-id]').nth(2)).toContainText( 242 'not a current' 243 ); 244 expect(requests).toEqual([]); 245 expect(events).toEqual([]); 246 }); 247 test('original self archive labels You without inventing a recipient ACK or remote read', async ({ 248 page 249 }) => { 250 await ready(page, 'self_archive'); 251 await unlock(page); 252 await expect(page.locator('[data-message-id]')).toContainText('You'); 253 await expect(page.locator('[data-message-id]')).toContainText( 254 'Recipient delivery unknown' 255 ); 256 await expect(page.locator('[data-message-id]')).not.toContainText( 257 'Accepted by recipient inbox relay' 258 ); 259 await expect(page.locator('[data-message-id]')).not.toContainText('Seen'); 260 expect(events).toEqual([]); 261 expect(requests).toEqual([]); 262 }); 263 test('actual original loopback SDK ACK and native receipt supply relay acceptance without thread publication', async ({ 264 page 265 }) => { 266 await ready(page, 'receipt'); 267 expect(events).toHaveLength(2); 268 const native = await page.evaluate(() => window.hcp103Fixture.native()); 269 expect( 270 ( 271 JSON.parse((native.sends[0] as { wire: string }).wire) as { 272 receipts: { role: string; status: string }[]; 273 } 274 ).receipts.some( 275 (row: { role: string; status: string }) => 276 row.role === 'peer' && row.status === 'accepted' 277 ) 278 ).toBe(true); 279 await unlock(page); 280 await expect(page.locator('[data-message-id]')).toContainText( 281 'Accepted by recipient inbox relay' 282 ); 283 await expect(page.locator('[data-message-id]')).not.toContainText('Seen'); 284 expect(events).toHaveLength(2); 285 expect(requests).toEqual([]); 286 }); 287 test('different genuine outer wraps of the same rumor render once and retain both ciphertext copies', async ({ 288 page 289 }) => { 290 await ready(page, 'duplicate'); 291 await unlock(page); 292 await expect(page.locator('[data-message-id]')).toHaveCount(1); 293 expect( 294 (await page.evaluate(() => window.hcp103Fixture.native())).received 295 ).toHaveLength(2); 296 expect(await page.evaluate(() => window.hcp103Fixture.delta())).toEqual({ 297 decrypts: 4, 298 signs: 0, 299 encrypts: 0 300 }); 301 }); 302 test('only actual displayed original room records local read without changing ciphertext or notifying peer', async ({ 303 page 304 }) => { 305 await ready(page); 306 const before = await page.evaluate(() => window.hcp103Fixture.native()); 307 await unlock(page); 308 await expect 309 .poll( 310 async () => 311 records( 312 (await page.evaluate(() => window.hcp103Fixture.native())).received 313 ).filter((row) => row.read !== null).length 314 ) 315 .toBe(1); 316 const after = await page.evaluate(() => window.hcp103Fixture.native()); 317 expect(records(after.received).map((row) => row.outer)).toEqual( 318 records(before.received).map((row) => row.outer) 319 ); 320 expect(events).toEqual([]); 321 expect(requests).toEqual([]); 322 }); 323 test('copied original controller cannot import bodies navigation or native display effects', async ({ 324 page 325 }) => { 326 await ready(page); 327 await unlock(page); 328 const result = await page.evaluate(async () => { 329 const f = window.hcp103Fixture, 330 before = await f.native(); 331 return { 332 copied: await f.copied(), 333 before, 334 after: await f.native(), 335 delta: f.delta() 336 }; 337 }); 338 expect(result.copied.snapshot).toMatchObject({ 339 status: 'unavailable', 340 messages: [] 341 }); 342 expect(result.copied.message).toBeUndefined(); 343 expect(result.copied.resolve).toBe(false); 344 expect(result.copied.read).toBe(false); 345 expect(result.after).toEqual(result.before); 346 expect(events).toEqual([]); 347 expect(requests).toEqual([]); 348 }); 349 test('unknown original UUID refuses before SDK decrypt and never substitutes an existing pair', async ({ 350 page 351 }) => { 352 await ready(page, 'inbound', missing); 353 const before = await page.evaluate(() => window.hcp103Fixture.native()); 354 expect(await page.evaluate(() => window.hcp103Fixture.unlock())).toBe(false); 355 expect(await page.evaluate(() => window.hcp103Fixture.next())).toBe(false); 356 await expect(page.locator('[data-message-id]')).toHaveCount(0); 357 expect(await page.evaluate(() => window.hcp103Fixture.native())).toEqual( 358 before 359 ); 360 expect(await page.evaluate(() => window.hcp103Fixture.delta())).toEqual({ 361 decrypts: 0, 362 signs: 0, 363 encrypts: 0 364 }); 365 }); 366 test('same-owner UUID in a separate real storage profile is unavailable without peer inference', async ({ 367 page, 368 browser 369 }) => { 370 await ready(page); 371 const id = await page.evaluate(() => window.hcp103Fixture.conversationId), 372 context = await browser.newContext(); 373 try { 374 const other = await context.newPage(); 375 await load(other); 376 await ready(other, 'inbound', id); 377 const before = await other.evaluate(() => window.hcp103Fixture.native()); 378 expect(await other.evaluate(() => window.hcp103Fixture.unlock())).toBe( 379 false 380 ); 381 await expect(other.locator('[data-message-id]')).toHaveCount(0); 382 expect(await other.evaluate(() => window.hcp103Fixture.native())).toEqual( 383 before 384 ); 385 expect(await other.evaluate(() => window.hcp103Fixture.delta())).toEqual({ 386 decrypts: 0, 387 signs: 0, 388 encrypts: 0 389 }); 390 await other.evaluate(() => window.hcp103Fixture.close()); 391 } finally { 392 await context.close(); 393 } 394 }); 395 test('Stop and owner logout clear original thread bodies and deny stale display effects', async ({ 396 page 397 }) => { 398 await ready(page); 399 await unlock(page); 400 await page.evaluate(() => window.hcp103Fixture.stop()); 401 await expect(page.locator('[data-message-id]')).toHaveCount(0); 402 const result = await page.evaluate(async () => { 403 const f = window.hcp103Fixture, 404 before = await f.native(); 405 f.logout(); 406 return { 407 resolved: await f.resolve(f.conversationId), 408 before, 409 after: await f.native() 410 }; 411 }); 412 expect(result.resolved).toBe(false); 413 expect(result.after).toEqual(result.before); 414 }); 415 test('logout during held actual extension decrypt prevents late thread disclosure or native read', async ({ 416 page 417 }) => { 418 await ready(page); 419 await page.evaluate(() => { 420 const f = window.hcp103Fixture; 421 f.mode('hold_decrypt'); 422 window.hcp103Pending = f.unlock(); 423 }); 424 await expect 425 .poll(() => page.evaluate(() => window.hcp103Fixture.pending())) 426 .toBe(true); 427 const before = await page.evaluate(() => window.hcp103Fixture.native()); 428 await page.evaluate(() => { 429 window.hcp103Fixture.logout(); 430 window.hcp103Fixture.settle(); 431 }); 432 expect(await page.evaluate(() => window.hcp103Pending)).toBe(false); 433 await expect(page.locator('[data-message-id]')).toHaveCount(0); 434 expect(await page.evaluate(() => window.hcp103Fixture.native())).toEqual( 435 before 436 ); 437 }); 438 test('initial backfill is not replayed through a live log and later explicit batch preserves focus and reading position', async ({ 439 page 440 }) => { 441 await ready(page, 'backfill'); 442 await unlock(page); 443 await expect(page.locator('[data-message-id]')).toHaveCount(20); 444 await expect(page.locator('[data-thread-announcement]')).toHaveText(''); 445 await expect( 446 page.locator('[data-message-id] [aria-live="polite"]') 447 ).toHaveCount(0); 448 await page.getByRole('button', { name: 'Reading position marker' }).focus(); 449 await page.evaluate(() => window.scrollTo(0, 320)); 450 await expect 451 .poll(() => page.evaluate(() => window.scrollY)) 452 .toBeGreaterThan(0); 453 const before = await page.evaluate(() => ({ 454 active: document.activeElement?.id, 455 scroll: window.scrollY 456 })); 457 expect(await page.evaluate(() => window.hcp103Fixture.next())).toBe(true); 458 await expect(page.locator('[data-message-id]')).toHaveCount(21); 459 await expect(page.locator('[data-thread-announcement]')).toHaveAttribute( 460 'aria-live', 461 'polite' 462 ); 463 await expect(page.locator('[data-thread-announcement]')).toHaveText( 464 '1 message added.' 465 ); 466 expect( 467 await page.evaluate(() => ({ 468 active: document.activeElement?.id, 469 scroll: window.scrollY 470 })) 471 ).toEqual(before); 472 }); 473 test('hostile plaintext and missing parent stay inert without HTML external links or private-ID searches', async ({ 474 page 475 }) => { 476 await ready(page, 'hostile'); 477 await unlock(page); 478 await expect(page.locator('[data-message-id]')).toContainText('<img src=x>'); 479 await expect(page.locator('[data-message-id]')).toContainText( 480 'Earlier message unavailable' 481 ); 482 await expect(page.locator('[data-message-id] img')).toHaveCount(0); 483 await expect(page.locator('[data-message-id] a')).toHaveCount(0); 484 expect(requests).toEqual([]); 485 expect(events).toEqual([]); 486 expect(page.url()).not.toContain('f'.repeat(64)); 487 }); 488 test('changing to an unknown route selection revokes stale original row lookup without writes or replacement', async ({ 489 page 490 }) => { 491 await ready(page); 492 await unlock(page); 493 const result = await page.evaluate(async (missing) => { 494 const f = window.hcp103Fixture, 495 before = await f.native(), 496 result = await f.resolve(missing); 497 return { result, old: f.thread(), before, after: await f.native() }; 498 }, missing); 499 expect(result.result).toBe(false); 500 expect(result.old).toMatchObject({ status: 'unavailable', messages: [] }); 501 await expect(page.locator('[data-message-id]')).toHaveCount(0); 502 expect(result.after).toEqual(result.before); 503 }); 504 505 test('actual native owner lock contention leaves local read unclaimed and explicit retry confirms it after display', async ({ 506 page 507 }) => { 508 await ready(page); 509 await page.evaluate(() => window.hcp103Fixture.holdReadLock()); 510 await unlock(page); 511 await expect( 512 page.getByText( 513 'Local read status could not be confirmed. Encrypted copies are preserved.', 514 { exact: true } 515 ) 516 ).toBeVisible(); 517 expect( 518 records( 519 (await page.evaluate(() => window.hcp103Fixture.native())).received 520 ).every((row) => row.read === null) 521 ).toBe(true); 522 await page.evaluate(() => window.hcp103Fixture.releaseRead()); 523 await page.getByRole('button', { name: 'Retry local read status' }).click(); 524 await expect 525 .poll( 526 async () => 527 records( 528 (await page.evaluate(() => window.hcp103Fixture.native())).received 529 ).filter((row) => row.read !== null).length 530 ) 531 .toBe(1); 532 await expect( 533 page.getByRole('button', { name: 'Retry local read status' }) 534 ).toHaveCount(0); 535 expect(events).toEqual([]); 536 expect(requests).toEqual([]); 537 });