private-validation-cache.spec.ts (7163B)
1 import { expect, test } from '@playwright/test'; 2 import { build } from 'vite'; 3 import { fileURLToPath } from 'node:url'; 4 import { createHash } from 'node:crypto'; 5 import { createStaticHarness } from '../integration/harness/static.ts'; 6 import type * as Fixture from './harness/private-validation-cache.ts'; 7 declare global { 8 interface Window { 9 cr005: typeof Fixture; 10 cr005Fixture: Awaited<ReturnType<typeof Fixture.createFixture>>; 11 } 12 } 13 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 14 const baseline = process.env.HCP_CR005_PHASE === 'baseline'; 15 test.beforeAll(async () => { 16 server = await createStaticHarness(); 17 const entry = fileURLToPath( 18 new URL('./harness/private-validation-cache.ts', import.meta.url) 19 ), 20 result = await build({ 21 configFile: false, 22 logLevel: 'silent', 23 plugins: [ 24 { 25 name: 'cr005-count-real-validator', 26 enforce: 'pre', 27 transform(code, id) { 28 if (!id.endsWith('/src/lib/nostr/verified-envelope.ts')) return; 29 const marker = ' if (\n typeof raw'; 30 expect(code.split(marker)).toHaveLength(2); 31 return { 32 code: code.replace( 33 marker, 34 ' globalThis.__cr005VerifyCalls = (globalThis.__cr005VerifyCalls ?? 0) + 1;\n' + 35 marker 36 ), 37 map: null 38 }; 39 } 40 } 41 ], 42 build: { 43 write: false, 44 minify: false, 45 lib: { entry, name: 'cr005', formats: ['iife'] } 46 } 47 }); 48 const output = Array.isArray(result) ? result[0] : result; 49 if (!('output' in output)) throw Error('fixture bundle absent'); 50 const chunks = output.output.filter((row) => row.type === 'chunk'); 51 expect(chunks).toHaveLength(1); 52 bundle = chunks[0].code; 53 console.log( 54 JSON.stringify({ 55 fixture: 'CR005_TEST_ONLY_ACTUAL_IDB_REAL_VALIDATOR_COUNT', 56 phase: baseline ? 'baseline' : 'candidate', 57 bundleSha256: createHash('sha256').update(bundle).digest('hex'), 58 moduleIds: Object.keys(chunks[0].modules), 59 qualification: 60 'Counter before unchanged real validator, no fabricated proofs/operator/fullQ' 61 }) 62 ); 63 }); 64 test.afterAll(async () => server?.close()); 65 test.beforeEach(async ({ page }) => { 66 await page.goto(server.url + '/about'); 67 await page.addScriptTag({ content: bundle }); 68 await page.evaluate(async () => { 69 window.cr005Fixture = await window.cr005.createFixture(); 70 }); 71 }); 72 test.afterEach(async ({ page }) => { 73 if (!page.isClosed()) await page.evaluate(() => window.cr005Fixture?.close()); 74 }); 75 test('CR005 actual capacity sample retains complete cursor checks while reusing unchanged validation', async ({ 76 page 77 }) => { 78 const result = await page.evaluate(async () => { 79 const f = window.cr005Fixture; 80 return { 81 first: await f.sample(), 82 second: await f.sample(), 83 inventory: await f.inventory(), 84 metadata: await f.metadata(), 85 outboxFirst: await f.sample('private_sends'), 86 outboxSecond: await f.sample('private_sends'), 87 bytes: f.bytes() 88 }; 89 }); 90 console.log( 91 JSON.stringify({ 92 fixture: 'CR005_ACTUAL_CAPACITY_COUNTS', 93 phase: baseline ? 'baseline' : 'candidate', 94 ...result 95 }) 96 ); 97 for (const row of [ 98 result.first, 99 result.second, 100 result.inventory, 101 result.metadata 102 ]) 103 expect(row).toMatchObject({ ok: true, cursorVisits: 2000 }); 104 expect(result.inventory.count).toBe(2000); 105 expect(result.metadata.count).toBe(2000); 106 expect(result.outboxFirst).toMatchObject({ ok: true, cursorVisits: 100 }); 107 expect(result.outboxSecond).toMatchObject({ ok: true, cursorVisits: 100 }); 108 expect(result.bytes.received).toBeLessThanOrEqual(50331648); 109 expect(result.bytes.sends).toBeLessThanOrEqual(8388608); 110 if (baseline) { 111 expect(result.first.verificationCalls).toBe(2001); 112 expect(result.second.verificationCalls).toBe(2001); 113 expect(result.inventory.verificationCalls).toBe(4000); 114 expect(result.metadata.verificationCalls).toBe(6000); 115 } else { 116 expect(result.first.verificationCalls).toBe(2000); 117 expect(result.second.verificationCalls).toBe(0); 118 expect(result.inventory.verificationCalls).toBe(0); 119 expect(result.metadata.verificationCalls).toBe(0); 120 } 121 }); 122 for (const change of ['valid', 'corrupt', 'summary', 'id'] as const) 123 test( 124 'CR005 warm complete namespace rejects or revalidates changed ' + 125 change + 126 ' tail through a second connection', 127 async ({ page }) => { 128 const result = await page.evaluate(async (change) => { 129 const f = window.cr005Fixture; 130 await f.sample(); 131 await f.changeTail(change, true); 132 return await f.sample(); 133 }, change); 134 if (change === 'valid') 135 expect(result).toMatchObject({ 136 ok: true, 137 cursorVisits: 2000, 138 verificationCalls: 1 139 }); 140 else 141 expect(result).toMatchObject({ 142 ok: false, 143 reason: 'corrupt_record', 144 cursorVisits: 2000 145 }); 146 } 147 ); 148 test('CR005 wrong namespace cannot borrow warm validation or claim empty owner history', async ({ 149 page 150 }) => { 151 const result = await page.evaluate(async () => { 152 const f = window.cr005Fixture; 153 await f.sample(); 154 return { read: await f.foreignRead(), metadata: await f.metadata(true) }; 155 }); 156 expect(result.read).toMatchObject({ ok: false }); 157 expect(result.metadata).toMatchObject({ ok: false, reason: 'invalid_scope' }); 158 }); 159 test('CR005 warm outbox still rejects actual101rows without eviction', async ({ 160 page 161 }) => { 162 const result = await page.evaluate(async () => { 163 const f = window.cr005Fixture; 164 await f.sample('private_sends'); 165 return await f.overfillSends(); 166 }); 167 expect(result).toMatchObject({ ok: false, reason: 'capacity' }); 168 }); 169 test('CR005 encoded byte accounting rejects changed oversized received namespace', async ({ 170 page 171 }) => { 172 const result = await page.evaluate(async () => { 173 const f = window.cr005Fixture; 174 await f.sample(); 175 return await f.oversizeReceived(); 176 }); 177 expect(result.bytes).toBeGreaterThan(50331648); 178 expect(result).toMatchObject({ ok: false, reason: 'capacity' }); 179 }); 180 181 test('CR005 warm received namespace rejects actual2001rows without eviction', async ({ 182 page 183 }) => { 184 const result = await page.evaluate(async () => { 185 const f = window.cr005Fixture; 186 await f.sample(); 187 return await f.overfillReceived(); 188 }); 189 expect(result).toMatchObject({ ok: false, reason: 'capacity' }); 190 }); 191 test('CR005 changed current read/source metadata is revalidated and reflected in original history', async ({ 192 page 193 }) => { 194 const result = await page.evaluate(async () => { 195 const f = window.cr005Fixture; 196 await f.sample(); 197 await f.changeTail('metadata', true); 198 return { read: await f.sample(), metadata: await f.metadata() }; 199 }); 200 expect(result.read).toMatchObject({ 201 ok: true, 202 cursorVisits: 2000, 203 verificationCalls: 1 204 }); 205 expect(result.metadata).toMatchObject({ 206 ok: true, 207 cursorVisits: 2000, 208 verificationCalls: 0, 209 count: 2001 210 }); 211 });