received-envelopes.spec.ts (7517B)
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/received-envelopes.ts'; 6 declare global { 7 interface Window { 8 hcp091: typeof Fixture; 9 hcp091Fixture: Awaited<ReturnType<typeof Fixture.makeFixture>>; 10 hcp091Run?: ReturnType< 11 Awaited<ReturnType<typeof Fixture.makeFixture>>['run'] 12 >; 13 } 14 } 15 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 16 test.beforeAll(async () => { 17 server = await createStaticHarness(); 18 const result = await build({ 19 configFile: false, 20 logLevel: 'silent', 21 build: { 22 write: false, 23 minify: false, 24 lib: { 25 entry: fileURLToPath( 26 new URL('./harness/received-envelopes.ts', import.meta.url) 27 ), 28 name: 'hcp091', 29 formats: ['iife'] 30 } 31 } 32 }); 33 const output = Array.isArray(result) ? result[0] : result; 34 if (!('output' in output)) throw Error('bundle missing'); 35 const chunks = output.output.filter((x) => x.type === 'chunk'); 36 expect(chunks).toHaveLength(1); 37 bundle = chunks[0].code; 38 }); 39 test.afterAll(async () => { 40 await server.close(); 41 }); 42 test.beforeEach(async ({ page }) => { 43 await page.goto(server.url + '/search'); 44 await page.addScriptTag({ content: bundle }); 45 await page.evaluate(async () => { 46 window.hcp091Fixture = await window.hcp091.makeFixture(); 47 }); 48 }); 49 test.afterEach(async ({ page }) => { 50 if (!page.isClosed()) 51 await page.evaluate(() => window.hcp091Fixture?.close()); 52 }); 53 test('stored actual ciphertext is explicitly unlocked with two stock SDK decryptions and no plaintext persistence', async ({ 54 page 55 }) => { 56 const before = await page.evaluate(() => window.hcp091Fixture.countsValue()); 57 const result = await page.evaluate(async () => { 58 const f = window.hcp091Fixture; 59 f.capture(); 60 const result = await f.run(); 61 return { 62 result, 63 counts: f.countsValue(), 64 stored: await f.stored(), 65 forged: f.forged() 66 }; 67 }); 68 expect(result.result.status).toBe('authenticated'); 69 expect(result.result.snapshot).toBeDefined(); 70 expect(result.counts.decrypts).toBe(before.decrypts + 2); 71 expect(result.counts.keys).toBeGreaterThan(before.keys + 1); 72 expect(result.counts.encrypts).toBe(before.encrypts); 73 expect(result.counts.signs).toBe(before.signs); 74 expect(result.stored).toMatchObject({ 75 family: 'received_envelope', 76 read: null 77 }); 78 expect(result.stored).not.toHaveProperty('body'); 79 expect(result.forged).toBeUndefined(); 80 }); 81 test('absent explicit unlock cannot capture or decrypt', async ({ page }) => { 82 const r = await page.evaluate(async () => { 83 const f = window.hcp091Fixture, 84 before = f.countsValue(); 85 return { 86 capture: f.capture('unreviewed'), 87 result: await f.run(), 88 before, 89 after: f.countsValue() 90 }; 91 }); 92 expect(r.capture).toBe(false); 93 expect(r.result.status).toBe('invalid'); 94 expect(r.after).toEqual(r.before); 95 }); 96 test('one invalid decrypted seal is isolated and a later explicit valid candidate remains usable', async ({ 97 page 98 }) => { 99 const r = await page.evaluate(async () => { 100 const f = window.hcp091Fixture; 101 f.mode('wrong_plaintext'); 102 f.capture(); 103 const bad = await f.run(); 104 f.mode('normal'); 105 f.capture(); 106 const good = await f.run(); 107 return { bad, good }; 108 }); 109 expect(r.bad.status).toBe('mismatch'); 110 expect(r.bad.snapshot).toBeUndefined(); 111 expect(r.good.status).toBe('authenticated'); 112 }); 113 test('changed fresh extension owner fences the first decrypt', async ({ 114 page 115 }) => { 116 const r = await page.evaluate(async () => { 117 const f = window.hcp091Fixture, 118 before = f.countsValue(); 119 f.mode('changed_key'); 120 f.capture(); 121 const result = await f.run(); 122 return { result, before, after: f.countsValue() }; 123 }); 124 expect(r.result.status).toBe('stopped'); 125 expect(r.after.decrypts).toBe(r.before.decrypts); 126 expect(r.result.snapshot).toBeUndefined(); 127 }); 128 test('actual decryption refusal has no nested proof or automatic follow-up', async ({ 129 page 130 }) => { 131 const r = await page.evaluate(async () => { 132 const f = window.hcp091Fixture, 133 before = f.countsValue(); 134 f.mode('declined_decrypt'); 135 f.capture(); 136 const result = await f.run(); 137 return { result, before, after: f.countsValue() }; 138 }); 139 expect(r.result.status).toBe('refused'); 140 expect(r.after.decrypts).toBe(r.before.decrypts + 1); 141 expect(r.result.snapshot).toBeUndefined(); 142 }); 143 test('Stop during actual provider wait cannot mint a proof or schedule inner decryption', async ({ 144 page 145 }) => { 146 const before = await page.evaluate(() => { 147 const f = window.hcp091Fixture; 148 f.mode('hold_decrypt'); 149 f.capture(); 150 window.hcp091Run = f.run(); 151 return f.countsValue(); 152 }); 153 await expect 154 .poll(() => page.evaluate(() => window.hcp091Fixture.pending())) 155 .toBe(true); 156 const r = await page.evaluate(async () => { 157 const f = window.hcp091Fixture; 158 f.stop(); 159 f.settle(); 160 return { 161 result: await window.hcp091Run, 162 counts: f.countsValue(), 163 snapshot: f.snapshot() 164 }; 165 }); 166 expect(r.result?.status).toBe('stopped'); 167 expect(r.counts.decrypts).toBe(before.decrypts + 1); 168 expect(r.snapshot).toBeUndefined(); 169 }); 170 test('logout during actual provider wait destroys session-owned nested evidence', async ({ 171 page 172 }) => { 173 await page.evaluate(() => { 174 const f = window.hcp091Fixture; 175 f.mode('hold_decrypt'); 176 f.capture(); 177 window.hcp091Run = f.run(); 178 }); 179 await expect 180 .poll(() => page.evaluate(() => window.hcp091Fixture.pending())) 181 .toBe(true); 182 const result = await page.evaluate(async () => { 183 const f = window.hcp091Fixture; 184 f.disconnect(); 185 f.settle(); 186 return await window.hcp091Run; 187 }); 188 expect(result?.status).toBe('stopped'); 189 expect(result?.snapshot).toBeUndefined(); 190 }); 191 test('changed actual stored full-wire metadata during decrypt invalidates original custody', async ({ 192 page 193 }) => { 194 await page.evaluate(() => { 195 const f = window.hcp091Fixture; 196 f.mode('hold_decrypt'); 197 f.capture(); 198 window.hcp091Run = f.run(); 199 }); 200 await expect 201 .poll(() => page.evaluate(() => window.hcp091Fixture.pending())) 202 .toBe(true); 203 expect( 204 await page.evaluate(async () => { 205 const f = window.hcp091Fixture; 206 const result = await f.changeObservedSource(); 207 f.mode('normal'); 208 f.settle(); 209 return result.ok; 210 }) 211 ).toBe(true); 212 const result = await page.evaluate(async () => await window.hcp091Run); 213 expect(result?.status).toBe('conflict'); 214 expect(result?.snapshot).toBeUndefined(); 215 }); 216 217 test('actual stored-wire change during held final SDK key check cannot mint current nested custody', async ({ 218 page 219 }) => { 220 await page.evaluate(() => { 221 const f = window.hcp091Fixture; 222 f.holdFinalKey(); 223 f.capture(); 224 window.hcp091Run = f.run(); 225 }); 226 await expect 227 .poll(() => page.evaluate(() => window.hcp091Fixture.pendingFinalKey())) 228 .toBe(true); 229 expect( 230 await page.evaluate(async () => { 231 const f = window.hcp091Fixture; 232 const changed = await f.changeObservedSource(); 233 f.settleFinalKey(); 234 return changed.ok; 235 }) 236 ).toBe(true); 237 const result = await page.evaluate(async () => await window.hcp091Run); 238 expect(result?.status).toBe('conflict'); 239 expect(result?.snapshot).toBeUndefined(); 240 });