room-admission.spec.ts (7967B)
1 import { test, expect } from '@playwright/test'; 2 import { build } from 'vite'; 3 import { fileURLToPath } from 'node:url'; 4 import { createStaticHarness } from '../integration/harness/static.ts'; 5 import type * as Fixture from './harness/room-admission.ts'; 6 declare global { 7 interface Window { 8 hcp092: typeof Fixture; 9 hcp092Fixture: Awaited<ReturnType<typeof Fixture.makeFixture>>; 10 } 11 } 12 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 13 test.beforeAll(async () => { 14 server = await createStaticHarness(); 15 const result = await build({ 16 configFile: false, 17 logLevel: 'silent', 18 build: { 19 write: false, 20 minify: false, 21 lib: { 22 entry: fileURLToPath( 23 new URL('./harness/room-admission.ts', import.meta.url) 24 ), 25 name: 'hcp092', 26 formats: ['iife'] 27 } 28 } 29 }); 30 const output = Array.isArray(result) ? result[0] : result; 31 if (!('output' in output)) throw Error('bundle missing'); 32 const chunks = output.output.filter((x) => x.type === 'chunk'); 33 expect(chunks).toHaveLength(1); 34 bundle = chunks[0].code; 35 }); 36 test.afterAll(async () => { 37 await server.close(); 38 }); 39 test.beforeEach(async ({ page }) => { 40 await page.goto(server.url + '/search'); 41 await page.addScriptTag({ content: bundle }); 42 }); 43 test.afterEach(async ({ page }) => { 44 if (!page.isClosed()) 45 await page.evaluate(() => window.hcp092Fixture?.close()); 46 }); 47 test('actual SDK-authenticated inbound selects sender and rejects forged or copied room custody', async ({ 48 page 49 }) => { 50 const r = await page.evaluate(async () => { 51 const f = (window.hcp092Fixture = await window.hcp092.makeFixture()); 52 const unlocked = await f.unlock(); 53 return { 54 unlocked, 55 room: f.admit('inbound'), 56 peer: f.peer, 57 owner: f.owner, 58 forged: f.forged(), 59 copied: f.copied(), 60 id: f.rumorId 61 }; 62 }); 63 expect(r.unlocked).toBe('authenticated'); 64 expect(r.room).toMatchObject({ 65 owner: r.owner, 66 peer: r.peer, 67 sender: r.peer, 68 recipient: r.owner, 69 role: 'inbound', 70 rumorId: r.id 71 }); 72 expect(r.forged).toBeUndefined(); 73 expect(r.copied).toBeUndefined(); 74 }); 75 76 for (const [scenario, role] of [ 77 ['inbound_extensions', 'inbound'], 78 ['self_archive_extensions', 'self_archive'] 79 ] as const) 80 test(`actual received ${scenario} retains original identity across hints/q/tag-order projection`, async ({ 81 page 82 }) => { 83 const sockets: string[] = []; 84 page.on('websocket', (socket) => sockets.push(socket.url())); 85 const result = await page.evaluate( 86 async ({ scenario, role }) => { 87 const f = (window.hcp092Fixture = 88 await window.hcp092.makeFixture(scenario)); 89 return { 90 unlocked: await f.unlock(), 91 room: f.admit(role), 92 id: f.rumorId, 93 owner: f.owner, 94 peer: f.peer, 95 opposite: f.admit(role === 'inbound' ? 'self_archive' : 'inbound') 96 }; 97 }, 98 { scenario, role } 99 ); 100 expect(result.unlocked).toBe('authenticated'); 101 expect(result.room).toMatchObject({ 102 owner: result.owner, 103 peer: result.peer, 104 role, 105 rumorId: result.id, 106 content: 'Actual encrypted room fixture', 107 subject: 'Original subject', 108 replyTo: 'a'.repeat(64) 109 }); 110 expect(result.opposite).toBeUndefined(); 111 expect(sockets.some((url) => url.includes('untrusted.example.org'))).toBe( 112 false 113 ); 114 }); 115 for (const scenario of [ 116 'duplicate_recipient', 117 'duplicate_subject', 118 'duplicate_parent', 119 'expiration', 120 'file' 121 ] as const) 122 test(`fresh genuine encryption cannot admit unsupported/ambiguous ${scenario}`, async ({ 123 page 124 }) => { 125 const result = await page.evaluate(async (scenario) => { 126 const f = (window.hcp092Fixture = 127 await window.hcp092.makeFixture(scenario)); 128 return { 129 unlocked: await f.unlock(), 130 inbound: f.admit('inbound'), 131 archive: f.admit('self_archive') 132 }; 133 }, scenario); 134 expect(result.unlocked).toBe('mismatch'); 135 expect(result.inbound).toBeUndefined(); 136 expect(result.archive).toBeUndefined(); 137 }); 138 test('actual authenticated self archive preserves original peer recipient and rumor id', async ({ 139 page 140 }) => { 141 const r = await page.evaluate(async () => { 142 const f = (window.hcp092Fixture = 143 await window.hcp092.makeFixture('self_archive')); 144 return { 145 unlocked: await f.unlock(), 146 room: f.admit('self_archive'), 147 owner: f.owner, 148 peer: f.peer, 149 id: f.rumorId 150 }; 151 }); 152 expect(r.unlocked).toBe('authenticated'); 153 expect(r.room).toMatchObject({ 154 owner: r.owner, 155 sender: r.owner, 156 recipient: r.peer, 157 peer: r.peer, 158 role: 'self_archive', 159 rumorId: r.id 160 }); 161 }); 162 test('actual signed decrypted third-party rumor cannot admit connected owner room', async ({ 163 page 164 }) => { 165 const r = await page.evaluate(async () => { 166 const f = (window.hcp092Fixture = 167 await window.hcp092.makeFixture('third_party')); 168 return { unlocked: await f.unlock(), room: f.admit('inbound') }; 169 }); 170 expect(r.unlocked).toBe('authenticated'); 171 expect(r.room).toBeUndefined(); 172 }); 173 test('explicit expected role mismatch cannot reinterpret a valid inbound as archive', async ({ 174 page 175 }) => { 176 const r = await page.evaluate(async () => { 177 const f = (window.hcp092Fixture = await window.hcp092.makeFixture()); 178 return { 179 unlocked: await f.unlock(), 180 wrong: f.admit('self_archive'), 181 right: f.admit('inbound') 182 }; 183 }); 184 expect(r.unlocked).toBe('authenticated'); 185 expect(r.wrong).toBeUndefined(); 186 expect(r.right?.role).toBe('inbound'); 187 }); 188 test('body URL and subject never retarget actual authenticated peer', async ({ 189 page 190 }) => { 191 const r = await page.evaluate(async () => { 192 const f = (window.hcp092Fixture = 193 await window.hcp092.makeFixture('hostile_text')); 194 return { 195 unlocked: await f.unlock(), 196 room: f.admit('inbound'), 197 peer: f.peer, 198 other: f.other 199 }; 200 }); 201 expect(r.unlocked).toBe('authenticated'); 202 expect(r.room?.peer).toBe(r.peer); 203 expect(r.room?.peer).not.toBe(r.other); 204 }); 205 test('missing private parent and untrusted hint cause zero public or private socket lookups', async ({ 206 page 207 }) => { 208 const requests: string[] = []; 209 const sockets: string[] = []; 210 page.on('request', (request) => requests.push(request.url())); 211 page.on('websocket', (socket) => sockets.push(socket.url())); 212 const r = await page.evaluate(async () => { 213 const f = (window.hcp092Fixture = 214 await window.hcp092.makeFixture('missing_parent')); 215 return { unlocked: await f.unlock(), room: f.admit('inbound') }; 216 }); 217 expect(r.unlocked).toBe('authenticated'); 218 expect(r.room?.replyTo).toBe('a'.repeat(64)); 219 expect(requests).toEqual([]); 220 expect(sockets).toEqual([]); 221 }); 222 test('logout removes current genuine room custody and prevents readmission', async ({ 223 page 224 }) => { 225 const r = await page.evaluate(async () => { 226 const f = (window.hcp092Fixture = await window.hcp092.makeFixture()); 227 await f.unlock(); 228 const before = f.admit('inbound'); 229 f.disconnect(); 230 return { before, after: f.snapshot(), again: f.admit('inbound') }; 231 }); 232 expect(r.before).toBeDefined(); 233 expect(r.after).toBeUndefined(); 234 expect(r.again).toBeUndefined(); 235 }); 236 test('Stop revokes original nested-dependent room proof without cancelling already settled SDK work', async ({ 237 page 238 }) => { 239 const r = await page.evaluate(async () => { 240 const f = (window.hcp092Fixture = await window.hcp092.makeFixture()); 241 await f.unlock(); 242 const before = f.admit('inbound'), 243 counts = f.counts(); 244 f.stop(); 245 return { 246 before, 247 after: f.snapshot(), 248 again: f.admit('inbound'), 249 counts, 250 afterCounts: f.counts() 251 }; 252 }); 253 expect(r.before).toBeDefined(); 254 expect(r.after).toBeUndefined(); 255 expect(r.again).toBeUndefined(); 256 expect(r.afterCounts).toEqual(r.counts); 257 });