nested-envelope-sizes.spec.ts (7296B)
1 import { test, expect, type Page } 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 Sizes from './harness/nested-envelope-sizes.ts'; 6 declare global { 7 interface Window { 8 hcp079: typeof Sizes; 9 } 10 } 11 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 12 test.beforeAll(async () => { 13 server = await createStaticHarness(); 14 const built = await build({ 15 configFile: false, 16 logLevel: 'silent', 17 build: { 18 write: false, 19 minify: false, 20 lib: { 21 entry: fileURLToPath( 22 new URL('./harness/nested-envelope-sizes.ts', import.meta.url) 23 ), 24 name: 'hcp079', 25 formats: ['iife'] 26 } 27 } 28 }); 29 const output = Array.isArray(built) ? built[0] : built; 30 if (!('output' in output)) throw Error('missing bundle'); 31 const chunks = output.output.filter((x) => x.type === 'chunk'); 32 expect(chunks).toHaveLength(1); 33 bundle = chunks[0].code; 34 }); 35 test.afterAll(async () => server.close()); 36 async function load(page: Page) { 37 await page.goto(server.url + '/search'); 38 await page.addScriptTag({ content: bundle }); 39 } 40 for (const shape of [ 41 'ASCII', 42 'BMP', 43 'astral', 44 'maximum_escaped_rumor' 45 ] as const) 46 test(`${shape}: actual genuine pair measures every encoded layer and keeps one original`, async ({ 47 page 48 }) => { 49 await load(page); 50 const result = await page.evaluate(async (shape) => { 51 const s = window.hcp079, 52 bytes = (value: string) => new TextEncoder().encode(value).length; 53 let fixture = await s.makeFixture(); 54 try { 55 const initial = s.reservedSendRumorWire(fixture.reserved); 56 if (!initial) throw Error('missing original'); 57 const before = fixture.counts(); 58 let text: string; 59 function inspect(text: string) { 60 const plan = fixture.textPlan(text); 61 if (!plan) return undefined; 62 try { 63 return s.rumorPlanSnapshot(plan); 64 } finally { 65 s.stopRumorPlan(plan); 66 } 67 } 68 if (shape === 'maximum_escaped_rumor') { 69 let low = 0, 70 high = 4096; 71 while (low < high) { 72 const middle = Math.ceil((low + high) / 2); 73 if (inspect('\u0001'.repeat(middle))) low = middle; 74 else high = middle - 1; 75 } 76 text = '\u0001'.repeat(low); 77 for (let rest = 0; rest < 6; rest++) { 78 if (inspect(text + 'x')) text += 'x'; 79 else break; 80 } 81 const top = inspect(text); 82 if (!top || bytes(top.wire) !== 8192) 83 throw Error('maximum rumor not reached'); 84 } else { 85 const first = JSON.parse(initial) as { content: string }; 86 const overhead = 87 bytes(first.content) - bytes('Private seal sentinel'), 88 remaining = 4096 - overhead; 89 const width = shape === 'ASCII' ? 1 : shape === 'BMP' ? 2 : 4, 90 character = shape === 'ASCII' ? 'x' : shape === 'BMP' ? 'é' : '😀'; 91 text = 92 character.repeat(Math.floor(remaining / width)) + 93 'x'.repeat(remaining % width); 94 } 95 const accepted = inspect(text), 96 rejected = inspect(text + 'x'); 97 if (!accepted) throw Error('missing maximum plan'); 98 const unchanged = 99 JSON.stringify(before) === JSON.stringify(fixture.counts()) && 100 s.reservedSendRumorWire(fixture.reserved) === initial; 101 const planning = { 102 accepted: true, 103 plusOneRejected: !rejected, 104 noSDK: unchanged 105 }; 106 fixture.close(); 107 fixture = await s.makeFixture(text); 108 const original = s.reservedSendRumorWire(fixture.reserved); 109 if (!original) throw Error('missing new original'); 110 const rumor = JSON.parse(original) as { 111 id: string; 112 created_at: number; 113 content: string; 114 tags: string[][]; 115 }; 116 const pair = s.captureEnvelopePreparation( 117 fixture.identity, 118 fixture.reserved, 119 'reviewed_envelope_pair' 120 ); 121 if (!pair) throw Error('missing pair'); 122 const started = fixture.counts(); 123 const self = await s.prepareEnvelopeRole( 124 pair, 125 'self', 126 'reviewed_pair_role' 127 ), 128 peer = await s.prepareEnvelopeRole( 129 pair, 130 'peer', 131 'reviewed_pair_role' 132 ), 133 complete = s.envelopePreparationSnapshot(pair); 134 if (!complete?.self || !complete.peer) throw Error('pair incomplete'); 135 const rows = []; 136 for (const role of ['self', 'peer'] as const) { 137 const outer = role === 'self' ? complete.self : complete.peer, 138 sealWire = fixture.decryptWrap(outer.wire, role), 139 seal = JSON.parse(sealWire) as { content: string }, 140 wrap = JSON.parse(outer.wire) as { 141 content: string; 142 tags: string[][]; 143 }; 144 rows.push({ 145 role, 146 body_utf8: bytes(rumor.content), 147 rumor_utf8: bytes(original), 148 seal_ciphertext_utf8: bytes(seal.content), 149 signed_seal_utf8: bytes(sealWire), 150 outer_ciphertext_utf8: bytes(wrap.content), 151 signed_outer_utf8: bytes(outer.wire), 152 equal: fixture.decrypt(sealWire, role).equal, 153 sameHash: outer.rumorHash === rumor.id, 154 target: 155 wrap.tags[0]?.[1] === 156 (role === 'self' ? fixture.owner : fixture.peer), 157 soleTarget: wrap.tags.length === 1 && wrap.tags[0]?.length === 2 158 }); 159 } 160 const counts = fixture.counts(); 161 return { 162 planning, 163 self: self.status, 164 peer: peer.status, 165 originalUnchanged: 166 s.reservedSendRumorWire(fixture.reserved) === original, 167 signs: counts.signs - started.signs, 168 encrypts: counts.encrypts - started.encrypts, 169 rows 170 }; 171 } finally { 172 fixture.close(); 173 } 174 }, shape); 175 expect(result.planning).toEqual({ 176 accepted: true, 177 plusOneRejected: true, 178 noSDK: true 179 }); 180 expect(result).toMatchObject({ 181 self: 'prepared', 182 peer: 'complete', 183 originalUnchanged: true, 184 signs: 2, 185 encrypts: 2 186 }); 187 expect(result.rows).toHaveLength(2); 188 for (const row of result.rows) { 189 expect(row).toMatchObject({ 190 equal: true, 191 sameHash: true, 192 target: true, 193 soleTarget: true 194 }); 195 expect(row.body_utf8).toBeLessThanOrEqual(4096); 196 expect(row.rumor_utf8).toBeLessThanOrEqual(8192); 197 expect(row.signed_seal_utf8).toBeLessThanOrEqual(16384); 198 expect(row.signed_outer_utf8).toBeLessThanOrEqual(32768); 199 if (shape === 'maximum_escaped_rumor') { 200 expect(row.rumor_utf8).toBe(8192); 201 expect(row.seal_ciphertext_utf8).toBe(11012); 202 } else expect(row.body_utf8).toBe(4096); 203 } 204 console.log( 205 JSON.stringify({ 206 fixture: 'HCP079_ACTUAL_GENUINE_FACTORY_NESTED_MEASUREMENTS', 207 shape, 208 rows: result.rows 209 }) 210 ); 211 });