inbox-retention.spec.ts (12173B)
1 let finiteSilent = false; 2 import { test, expect } from '@playwright/test'; 3 import { build } from 'vite'; 4 import { fileURLToPath } from 'node:url'; 5 import { once } from 'node:events'; 6 import { WebSocketServer, type WebSocket } from 'ws'; 7 import { createStaticHarness } from '../integration/harness/static.ts'; 8 import type * as Fixture from './harness/inbox-retention.ts'; 9 declare global { 10 interface Window { 11 hcp099: typeof Fixture; 12 hcp099Fixture: Awaited<ReturnType<typeof Fixture.makeFixture>>; 13 } 14 } 15 let server: Awaited<ReturnType<typeof createStaticHarness>>, 16 sockets: WebSocketServer, 17 endpoint: string, 18 bundle: string; 19 let requests: unknown[][] = [], 20 closes: unknown[][] = [], 21 live = new Map<WebSocket, string>(), 22 finite: Record<string, unknown>[] = []; 23 test.beforeAll(async () => { 24 server = await createStaticHarness(); 25 sockets = new WebSocketServer({ 26 host: '127.0.0.1', 27 port: 0, 28 maxPayload: 262144 29 }); 30 await once(sockets, 'listening'); 31 const a = sockets.address(); 32 if (!a || typeof a === 'string') throw Error('loopback absent'); 33 endpoint = 'ws://127.0.0.1:' + a.port; 34 sockets.on('connection', (socket) => 35 socket.on('message', (bytes) => { 36 const raw = Buffer.isBuffer(bytes) 37 ? bytes 38 : Array.isArray(bytes) 39 ? Buffer.concat(bytes) 40 : Buffer.from(bytes); 41 const frame = JSON.parse(raw.toString()) as unknown[]; 42 if (frame[0] === 'CLOSE') { 43 closes.push(frame); 44 return; 45 } 46 if (frame[0] !== 'REQ') return; 47 requests.push(frame); 48 const filter = frame[2] as { limit: number }; 49 if (filter.limit === 0) live.set(socket, String(frame[1])); 50 else if (finiteSilent) return; 51 else 52 for (const event of finite) 53 socket.send(JSON.stringify(['EVENT', frame[1], event])); 54 socket.send(JSON.stringify(['EOSE', frame[1]])); 55 }) 56 ); 57 const result = await build({ 58 configFile: false, 59 logLevel: 'silent', 60 build: { 61 write: false, 62 minify: false, 63 lib: { 64 entry: fileURLToPath( 65 new URL('./harness/inbox-retention.ts', import.meta.url) 66 ), 67 name: 'HCP099', 68 formats: ['iife'] 69 } 70 } 71 }); 72 const output = (Array.isArray(result) ? result : [result]) 73 .flatMap((r) => ('output' in r ? r.output : [])) 74 .find((x) => x.type === 'chunk'); 75 if (!output || output.type !== 'chunk') throw Error('bundle absent'); 76 bundle = output.code + '\nwindow.hcp099=HCP099;'; 77 }); 78 test.beforeEach(async ({ page }) => { 79 requests = []; 80 finiteSilent = false; 81 closes = []; 82 live = new Map(); 83 finite = []; 84 await page.addInitScript( 85 ({ endpoint }) => { 86 const Native = window.WebSocket; 87 window.WebSocket = class extends Native { 88 constructor(url: string | URL, protocols?: string | string[]) { 89 if (new URL(String(url)).origin !== 'wss://archive.example.org') 90 throw Error('egress refused'); 91 super(endpoint, protocols); 92 } 93 }; 94 }, 95 { endpoint } 96 ); 97 await page.goto(server.url + '/messages'); 98 await page.addScriptTag({ content: bundle }); 99 await page.evaluate(async () => { 100 window.hcp099Fixture = await window.hcp099.makeFixture(); 101 }); 102 }); 103 test.afterEach(async ({ page }) => { 104 if (!page.isClosed()) 105 await page.evaluate(() => window.hcp099Fixture?.close()); 106 }); 107 test.afterAll(async () => { 108 for (const s of sockets.clients) s.terminate(); 109 await new Promise<void>((r) => sockets.close(() => r())); 110 await server?.close(); 111 }); 112 function deliver(event: unknown, count: number) { 113 for (const [socket, id] of live) 114 for (let i = 0; i < count; i++) 115 socket.send(JSON.stringify(['EVENT', id, event])); 116 } 117 test('explicit Stop during actual queued native cursor aborts selected-copy deletion', async ({ 118 page 119 }) => { 120 await page.evaluate(() => window.hcp099Fixture.select()); 121 await page.evaluate(() => window.hcp099Fixture.fault('stop_cursor')); 122 expect(await page.evaluate(() => window.hcp099Fixture.purge())).toMatchObject( 123 { status: 'stopped' } 124 ); 125 await page.evaluate(() => window.hcp099Fixture.restore()); 126 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 127 3 128 ); 129 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 130 true 131 ); 132 }); 133 test('only two reviewed local received copies delete; third foreign owner and unresolved outbox remain', async ({ 134 page 135 }) => { 136 expect(await page.evaluate(() => window.hcp099Fixture.select())).toBe(true); 137 expect( 138 await page.evaluate(() => window.hcp099Fixture.snapshot()) 139 ).toMatchObject({ 140 state: 'review', 141 selected: 2, 142 localCopyLoss: true, 143 relayRecovery: 'not_guaranteed' 144 }); 145 expect(await page.evaluate(() => window.hcp099Fixture.purge())).toMatchObject( 146 { status: 'removed', removed: 2 } 147 ); 148 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 149 1 150 ); 151 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 152 true 153 ); 154 expect(await page.evaluate(() => window.hcp099Fixture.delta())).toEqual({ 155 decrypts: 0, 156 encrypts: 0, 157 signs: 0 158 }); 159 expect(requests).toEqual([]); 160 }); 161 test('capture is inert and wrong review or copied token cannot delete', async ({ 162 page 163 }) => { 164 expect( 165 await page.evaluate(() => window.hcp099Fixture.select(undefined, 'wrong')) 166 ).toBe(false); 167 expect(await page.evaluate(() => window.hcp099Fixture.select())).toBe(true); 168 expect( 169 await page.evaluate(() => window.hcp099Fixture.purge('wrong')) 170 ).toEqual({ status: 'invalid' }); 171 expect(await page.evaluate(() => window.hcp099Fixture.copied())).toEqual({ 172 status: 'invalid' 173 }); 174 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 175 3 176 ); 177 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 178 true 179 ); 180 }); 181 test('native whole-wire revision conflict aborts all selected copies', async ({ 182 page 183 }) => { 184 await page.evaluate(() => window.hcp099Fixture.select()); 185 await page.evaluate(() => window.hcp099Fixture.revise()); 186 expect(await page.evaluate(() => window.hcp099Fixture.purge())).toMatchObject( 187 { status: 'conflict' } 188 ); 189 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 190 3 191 ); 192 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 193 true 194 ); 195 }); 196 test('actual native delete abort rolls back every selected row without success', async ({ 197 page 198 }) => { 199 await page.evaluate(() => window.hcp099Fixture.select()); 200 await page.evaluate(() => window.hcp099Fixture.fault('abort')); 201 expect(await page.evaluate(() => window.hcp099Fixture.purge())).toMatchObject( 202 { status: 'aborted' } 203 ); 204 await page.evaluate(() => window.hcp099Fixture.restore()); 205 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 206 3 207 ); 208 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 209 true 210 ); 211 }); 212 test('actual post-delete readback failure is unknown completion and not definite no deletion', async ({ 213 page 214 }) => { 215 await page.evaluate(() => window.hcp099Fixture.select()); 216 await page.evaluate(() => window.hcp099Fixture.fault('readback')); 217 expect(await page.evaluate(() => window.hcp099Fixture.purge())).toMatchObject( 218 { status: 'unknown_completion' } 219 ); 220 await page.evaluate(() => window.hcp099Fixture.restore()); 221 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 222 1 223 ); 224 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 225 true 226 ); 227 }); 228 test('logout after selected review refuses local deletion in original namespace', async ({ 229 page 230 }) => { 231 await page.evaluate(() => window.hcp099Fixture.select()); 232 await page.evaluate(() => window.hcp099Fixture.logout()); 233 expect(await page.evaluate(() => window.hcp099Fixture.purge())).toMatchObject( 234 { status: 'stopped' } 235 ); 236 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 237 3 238 ); 239 }); 240 test('owner loss during actual queued native cursor prevents deletion before finish', async ({ 241 page 242 }) => { 243 await page.evaluate(() => window.hcp099Fixture.select()); 244 await page.evaluate(() => window.hcp099Fixture.fault('logout_cursor')); 245 expect(await page.evaluate(() => window.hcp099Fixture.purge())).toMatchObject( 246 { status: 'stopped' } 247 ); 248 await page.evaluate(() => window.hcp099Fixture.restore()); 249 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 250 3 251 ); 252 }); 253 test('501 repeated finite ciphertext deliveries consume budget before dedup and pause', async ({ 254 page 255 }) => { 256 const event = JSON.parse( 257 await page.evaluate(() => window.hcp099Fixture.outer) 258 ) as Record<string, unknown>; 259 finite = Array.from({ length: 501 }, () => event); 260 expect(await page.evaluate(() => window.hcp099Fixture.start())).toBe(true); 261 await expect 262 .poll(() => page.evaluate(() => window.hcp099Fixture.sync()?.state)) 263 .toBe('needs_action'); 264 expect(await page.evaluate(() => window.hcp099Fixture.sync())).toMatchObject({ 265 reason: 'budget', 266 historyComplete: false, 267 recoveryChoices: [ 268 'wait_then_review_refresh', 269 'review_history_scope', 270 'review_local_received_copies' 271 ] 272 }); 273 await expect.poll(() => closes.length).toBeGreaterThanOrEqual(2); 274 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 275 true 276 ); 277 }); 278 test('121 malformed live deliveries count before rejection and pause instead of spinning', async ({ 279 page 280 }) => { 281 await page.evaluate(() => window.hcp099Fixture.start()); 282 await expect.poll(() => requests.length).toBe(3); 283 await expect 284 .poll(() => page.evaluate(() => window.hcp099Fixture.sync()?.backfill)) 285 .toBe('complete'); 286 deliver(await page.evaluate(() => window.hcp099Fixture.storm()), 121); 287 await expect 288 .poll(() => page.evaluate(() => window.hcp099Fixture.sync()?.state)) 289 .toBe('needs_action'); 290 expect(await page.evaluate(() => window.hcp099Fixture.sync())).toMatchObject({ 291 reason: 'budget', 292 historyComplete: false 293 }); 294 await expect.poll(() => closes.length).toBeGreaterThanOrEqual(2); 295 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 296 true 297 ); 298 expect(requests).toHaveLength(3); 299 }); 300 test('oversized malformed live payloads consume two MiB before count cap and preserve outbox', async ({ 301 page 302 }) => { 303 await page.evaluate(() => window.hcp099Fixture.start()); 304 await expect.poll(() => requests.length).toBe(3); 305 await expect 306 .poll(() => page.evaluate(() => window.hcp099Fixture.sync()?.backfill)) 307 .toBe('complete'); 308 deliver(await page.evaluate(() => window.hcp099Fixture.storm(34000)), 63); 309 await expect 310 .poll(() => page.evaluate(() => window.hcp099Fixture.sync()?.state)) 311 .toBe('needs_action'); 312 expect(await page.evaluate(() => window.hcp099Fixture.sync())).toMatchObject({ 313 reason: 'budget', 314 retained: 0, 315 historyComplete: false 316 }); 317 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 318 true 319 ); 320 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 321 3 322 ); 323 expect(requests).toHaveLength(3); 324 }); 325 326 test('actual fifteen-second finite elapsed pause exposes reviewed recovery and no automatic second window', async ({ 327 page 328 }) => { 329 finiteSilent = true; 330 expect(await page.evaluate(() => window.hcp099Fixture.start())).toBe(true); 331 await expect.poll(() => requests.length).toBe(2); 332 await expect 333 .poll(() => page.evaluate(() => window.hcp099Fixture.sync()?.state)) 334 .toBe('needs_action'); 335 expect(await page.evaluate(() => window.hcp099Fixture.sync())).toMatchObject({ 336 reason: 'elapsed', 337 historyComplete: false, 338 recoveryChoices: [ 339 'wait_then_review_refresh', 340 'review_history_scope', 341 'review_local_received_copies' 342 ] 343 }); 344 await expect.poll(() => closes.length).toBeGreaterThanOrEqual(2); 345 expect(requests).toHaveLength(2); 346 expect(await page.evaluate(() => window.hcp099Fixture.preserved())).toBe( 347 true 348 ); 349 expect(await page.evaluate(() => window.hcp099Fixture.rows())).toHaveLength( 350 3 351 ); 352 });