decryption-queue.spec.ts (12796B)
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/decryption-queue.ts'; 6 declare global { 7 interface Window { 8 hcp098: typeof Fixture; 9 hcp098Fixture: Awaited<ReturnType<typeof Fixture.makeFixture>>; 10 hcp098Pending: Promise<unknown>; 11 } 12 } 13 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 14 test.beforeAll(async () => { 15 server = await createStaticHarness(); 16 const result = await build({ 17 configFile: false, 18 logLevel: 'silent', 19 build: { 20 write: false, 21 minify: false, 22 lib: { 23 entry: fileURLToPath( 24 new URL('./harness/decryption-queue.ts', import.meta.url) 25 ), 26 name: 'HCP098', 27 formats: ['iife'] 28 } 29 } 30 }); 31 const outputs = Array.isArray(result) ? result : [result]; 32 const chunk = outputs 33 .flatMap((output) => ('output' in output ? output.output : [])) 34 .find((x) => x.type === 'chunk'); 35 if (!chunk || chunk.type !== 'chunk') throw Error('no actual JS'); 36 bundle = chunk.code + '\nwindow.hcp098=HCP098;'; 37 }); 38 test.beforeEach(async ({ page }, testInfo) => { 39 await page.goto(server.url + '/messages'); 40 await page.addScriptTag({ content: bundle }); 41 await page.evaluate(async (unknown) => { 42 window.hcp098Fixture = await window.hcp098.makeFixture(21, unknown); 43 }, testInfo.title.startsWith('unknown authenticated blocked')); 44 }); 45 test.afterEach(async ({ page }) => { 46 await page.evaluate(() => window.hcp098Fixture?.close()); 47 }); 48 test.afterAll(async () => { 49 await server?.close(); 50 }); 51 test('21st envelope waits new explicit action after20 actual serialized nested decryptions', async ({ 52 page 53 }) => { 54 expect(await page.evaluate(() => window.hcp098Fixture.capture())).toBe(true); 55 await page.evaluate(() => window.hcp098Fixture.refresh()); 56 expect( 57 (await page.evaluate(() => window.hcp098Fixture.snapshot()))?.queued 58 ).toBe(21); 59 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(true); 60 expect(await page.evaluate(() => window.hcp098Fixture.delta())).toMatchObject( 61 { decrypts: 40, signs: 0, encrypts: 0 } 62 ); 63 expect( 64 await page.evaluate(() => window.hcp098Fixture.snapshot()) 65 ).toMatchObject({ state: 'needs_action', queued: 1, attempted: 20 }); 66 expect(await page.evaluate(() => window.hcp098Fixture.cache())).toMatchObject( 67 { count: 1 } 68 ); 69 expect( 70 await page.evaluate(() => window.hcp098Fixture.batch('not_reviewed')) 71 ).toBe(false); 72 expect( 73 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 74 ).toBe(40); 75 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(true); 76 expect( 77 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 78 ).toBe(42); 79 expect( 80 await page.evaluate(() => window.hcp098Fixture.snapshot()) 81 ).toMatchObject({ state: 'idle', queued: 0, attempted: 1 }); 82 expect( 83 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 84 ).toBe(true); 85 expect(await page.evaluate(() => window.hcp098Fixture.publicContains())).toBe( 86 false 87 ); 88 }); 89 test('connect capture refresh and unreviewed batch never automatically decrypt', async ({ 90 page 91 }) => { 92 expect(await page.evaluate(() => window.hcp098Fixture.delta())).toMatchObject( 93 { decrypts: 0, signs: 0, encrypts: 0 } 94 ); 95 expect(await page.evaluate(() => window.hcp098Fixture.capture('wrong'))).toBe( 96 false 97 ); 98 expect(await page.evaluate(() => window.hcp098Fixture.capture())).toBe(true); 99 await page.evaluate(() => window.hcp098Fixture.refresh()); 100 expect(await page.evaluate(() => window.hcp098Fixture.batch('wrong'))).toBe( 101 false 102 ); 103 expect(await page.evaluate(() => window.hcp098Fixture.delta())).toMatchObject( 104 { decrypts: 0, signs: 0, encrypts: 0 } 105 ); 106 expect( 107 await page.evaluate(() => window.hcp098Fixture.snapshot()) 108 ).toMatchObject({ queued: 21, attempted: 0 }); 109 }); 110 test('actual provider refusal pauses scheduling and preserves every original ciphertext', async ({ 111 page 112 }) => { 113 await page.evaluate(() => { 114 window.hcp098Fixture.capture(); 115 window.hcp098Fixture.mode('declined_decrypt'); 116 }); 117 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(false); 118 expect( 119 await page.evaluate(() => window.hcp098Fixture.snapshot()) 120 ).toMatchObject({ 121 state: 'paused', 122 reason: 'refused', 123 queued: 21, 124 attempted: 1 125 }); 126 expect( 127 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 128 ).toBe(1); 129 expect( 130 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 131 ).toBe(true); 132 await page.evaluate(() => window.hcp098Fixture.mode('normal')); 133 expect(await page.evaluate(() => window.hcp098Fixture.batch('wrong'))).toBe( 134 false 135 ); 136 expect( 137 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 138 ).toBe(1); 139 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(true); 140 expect( 141 await page.evaluate(() => window.hcp098Fixture.snapshot()) 142 ).toMatchObject({ queued: 1, attempted: 20 }); 143 }); 144 test('unknown authenticated blocked inner peer still consumes20 envelope costs before hiding bodies', async ({ 145 page 146 }) => { 147 await page.evaluate(() => window.hcp098Fixture.capture()); 148 expect( 149 await page.evaluate(() => 150 window.hcp098Fixture.batch('reviewed_decrypt_batch', [ 151 window.hcp098Fixture.stranger 152 ]) 153 ) 154 ).toBe(true); 155 expect( 156 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 157 ).toBe(40); 158 expect( 159 await page.evaluate(() => window.hcp098Fixture.snapshot()) 160 ).toMatchObject({ queued: 1, attempted: 20, blocked: 20 }); 161 expect(await page.evaluate(() => window.hcp098Fixture.cache())).toMatchObject( 162 { count: 0 } 163 ); 164 expect( 165 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 166 ).toBe(true); 167 }); 168 test('blocking disposable outer author does not replace actual authenticated inner peer', async ({ 169 page 170 }) => { 171 await page.evaluate(() => window.hcp098Fixture.capture()); 172 expect( 173 await page.evaluate(() => 174 window.hcp098Fixture.batch('reviewed_decrypt_batch', [ 175 window.hcp098Fixture.disposable 176 ]) 177 ) 178 ).toBe(true); 179 expect( 180 await page.evaluate(() => window.hcp098Fixture.snapshot()) 181 ).toMatchObject({ queued: 1, attempted: 20, blocked: 0 }); 182 expect(await page.evaluate(() => window.hcp098Fixture.cache())).toMatchObject( 183 { count: 1 } 184 ); 185 }); 186 test('expired UI wait preserves actual occupied SDK slot until original provider settles', async ({ 187 page 188 }) => { 189 await page.evaluate(() => { 190 window.hcp098Fixture.capture(); 191 window.hcp098Fixture.mode('hold_decrypt'); 192 window.hcp098Pending = window.hcp098Fixture.batch(); 193 }); 194 await expect 195 .poll(() => page.evaluate(() => window.hcp098Fixture.scheduler())) 196 .toMatchObject({ state: 'active', pending: 'decrypt' }); 197 await page.evaluate(() => window.hcp098Fixture.expire()); 198 expect( 199 await page.evaluate(() => window.hcp098Fixture.snapshot()) 200 ).toMatchObject({ state: 'paused', reason: 'wait_expired', queued: 21 }); 201 expect( 202 await page.evaluate(() => window.hcp098Fixture.scheduler()) 203 ).toMatchObject({ 204 state: 'active', 205 pending: 'decrypt', 206 phase: 'wait_expired' 207 }); 208 expect( 209 await page.evaluate(() => window.hcp098Fixture.competing()) 210 ).toMatchObject({ status: 'busy' }); 211 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(false); 212 await page.evaluate(() => window.hcp098Fixture.settle()); 213 await page.evaluate(() => window.hcp098Pending); 214 expect( 215 await page.evaluate(() => window.hcp098Fixture.scheduler()) 216 ).toMatchObject({ state: 'idle' }); 217 expect( 218 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 219 ).toBe(1); 220 expect(await page.evaluate(() => window.hcp098Fixture.cache())).toMatchObject( 221 { count: 0 } 222 ); 223 expect( 224 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 225 ).toBe(true); 226 }); 227 test('logout during actual pending decrypt cannot cache a late result or schedule a second envelope', async ({ 228 page 229 }) => { 230 await page.evaluate(() => { 231 window.hcp098Fixture.capture(); 232 window.hcp098Fixture.mode('hold_decrypt'); 233 window.hcp098Pending = window.hcp098Fixture.batch(); 234 }); 235 await expect 236 .poll(() => page.evaluate(() => window.hcp098Fixture.scheduler())) 237 .toMatchObject({ state: 'active', pending: 'decrypt' }); 238 await page.evaluate(() => window.hcp098Fixture.disconnect()); 239 expect( 240 await page.evaluate(() => window.hcp098Fixture.scheduler()) 241 ).toMatchObject({ state: 'active', pending: 'decrypt' }); 242 await page.evaluate(() => window.hcp098Fixture.settle()); 243 await page.evaluate(() => window.hcp098Pending); 244 expect( 245 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 246 ).toBe(1); 247 expect(await page.evaluate(() => window.hcp098Fixture.cache())).toMatchObject( 248 { closed: true, count: 0 } 249 ); 250 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(false); 251 expect( 252 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 253 ).toBe(true); 254 }); 255 test('forged copied closed-unlock and invalid block metadata cannot gain SDK admission', async ({ 256 page 257 }) => { 258 await page.evaluate(() => window.hcp098Fixture.capture()); 259 expect(await page.evaluate(() => window.hcp098Fixture.forged())).toBe(false); 260 expect(await page.evaluate(() => window.hcp098Fixture.copied())).toBe(false); 261 expect( 262 await page.evaluate(() => 263 window.hcp098Fixture.batch('reviewed_decrypt_batch', ['wrong']) 264 ) 265 ).toBe(false); 266 await page.evaluate(() => window.hcp098Fixture.closeUnlock()); 267 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(false); 268 expect( 269 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 270 ).toBe(0); 271 expect( 272 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 273 ).toBe(true); 274 }); 275 276 test('malformed decrypted layer is isolated but consumes the explicit envelope cap', async ({ 277 page 278 }) => { 279 await page.evaluate(() => { 280 window.hcp098Fixture.capture(); 281 window.hcp098Fixture.mode('wrong_plaintext'); 282 }); 283 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(true); 284 expect( 285 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 286 ).toBe(20); 287 expect( 288 await page.evaluate(() => window.hcp098Fixture.snapshot()) 289 ).toMatchObject({ 290 state: 'needs_action', 291 queued: 1, 292 attempted: 20, 293 invalid: 20 294 }); 295 expect(await page.evaluate(() => window.hcp098Fixture.cache())).toMatchObject( 296 { count: 0 } 297 ); 298 expect( 299 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 300 ).toBe(true); 301 }); 302 test('explicit Stop during actual provider wait keeps SDK occupied and prevents the next envelope', async ({ 303 page 304 }) => { 305 await page.evaluate(() => { 306 window.hcp098Fixture.capture(); 307 window.hcp098Fixture.mode('hold_decrypt'); 308 window.hcp098Pending = window.hcp098Fixture.batch(); 309 }); 310 await expect 311 .poll(() => page.evaluate(() => window.hcp098Fixture.scheduler())) 312 .toMatchObject({ state: 'active', pending: 'decrypt' }); 313 await page.evaluate(() => window.hcp098Fixture.stop()); 314 expect( 315 await page.evaluate(() => window.hcp098Fixture.snapshot()) 316 ).toMatchObject({ state: 'stopped', busy: true }); 317 expect( 318 await page.evaluate(() => window.hcp098Fixture.competing()) 319 ).toMatchObject({ status: 'busy' }); 320 expect(await page.evaluate(() => window.hcp098Fixture.batch())).toBe(false); 321 await page.evaluate(() => window.hcp098Fixture.settle()); 322 await page.evaluate(() => window.hcp098Pending); 323 expect( 324 await page.evaluate(() => window.hcp098Fixture.scheduler()) 325 ).toMatchObject({ state: 'idle' }); 326 expect( 327 (await page.evaluate(() => window.hcp098Fixture.delta())).decrypts 328 ).toBe(1); 329 expect(await page.evaluate(() => window.hcp098Fixture.cache())).toMatchObject( 330 { count: 0 } 331 ); 332 expect( 333 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 334 ).toBe(true); 335 }); 336 test('same-key genuine foreign identity generation cannot import original unlock authority', async ({ 337 page 338 }) => { 339 await page.evaluate(() => window.hcp098Fixture.capture()); 340 await page.evaluate(() => window.hcp098Fixture.refresh()); 341 expect( 342 await page.evaluate(() => window.hcp098Fixture.foreignCapture()) 343 ).toEqual({ captured: false, keys: 0, decrypts: 0, signs: 0 }); 344 expect( 345 await page.evaluate(() => window.hcp098Fixture.snapshot()) 346 ).toMatchObject({ queued: 21, attempted: 0 }); 347 expect( 348 await page.evaluate(() => window.hcp098Fixture.encryptedUnchanged()) 349 ).toBe(true); 350 });