conversation-directory.spec.ts (10372B)
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/conversation-directory.ts'; 6 declare global { 7 interface Window { 8 hcp100: typeof Fixture; 9 hcp100Fixture: Awaited<ReturnType<typeof Fixture.makeFixture>>; 10 } 11 } 12 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 13 test.beforeAll(async () => { 14 server = await createStaticHarness(); 15 const result = await build({ 16 configFile: false, 17 logLevel: 'silent', 18 build: { 19 write: false, 20 minify: false, 21 lib: { 22 entry: fileURLToPath( 23 new URL('./harness/conversation-directory.ts', import.meta.url) 24 ), 25 name: 'hcp100', 26 formats: ['iife'] 27 } 28 } 29 }); 30 const output = Array.isArray(result) ? result[0] : result; 31 if (!('output' in output)) throw Error('bundle missing'); 32 const chunks = output.output.filter((x) => x.type === 'chunk'); 33 expect(chunks).toHaveLength(1); 34 bundle = chunks[0].code; 35 }); 36 test.afterAll(async () => { 37 await server.close(); 38 }); 39 test.beforeEach(async ({ page }) => { 40 await page.goto(server.url + '/search'); 41 await page.addScriptTag({ content: bundle }); 42 await page.evaluate(async () => { 43 window.hcp100Fixture = await window.hcp100.makeFixture(); 44 await window.hcp100Fixture.ready(); 45 }); 46 }); 47 test.afterEach(async ({ page }) => { 48 if (!page.isClosed()) 49 await page.evaluate(() => window.hcp100Fixture?.close()); 50 }); 51 const missing = '12345678-1234-4234-8234-123456789abc'; 52 test('directory capture and unknown GET are inert and never mark an unread rumor read', async ({ 53 page 54 }) => { 55 const r = await page.evaluate(async (id) => { 56 const f = window.hcp100Fixture; 57 const before = await f.rawMetadata(), 58 counts = f.counts(); 59 const captured = f.capture(); 60 const result = await f.resolve(id); 61 return { 62 captured, 63 result, 64 before, 65 after: await f.rawMetadata(), 66 counts, 67 afterCounts: f.counts(), 68 unread: await f.unread() 69 }; 70 }, missing); 71 expect(r.captured).toBe(true); 72 expect(r.result).toEqual({ status: 'unavailable' }); 73 expect(r.after).toEqual(r.before); 74 expect(r.afterCounts).toEqual(r.counts); 75 expect(r.unread).toMatchObject({ count: 1 }); 76 }); 77 test('only explicitly reviewed admitted exchange creates a random owner mapping without read or SDK effects', async ({ 78 page 79 }) => { 80 const r = await page.evaluate(async () => { 81 const f = window.hcp100Fixture; 82 f.capture(); 83 const counts = f.counts(); 84 const save = await f.remember(); 85 const raw = await f.rawMetadata(); 86 return { 87 save, 88 raw, 89 unread: await f.unread(), 90 counts, 91 afterCounts: f.counts() 92 }; 93 }); 94 expect(r.save).toMatchObject({ status: 'saved' }); 95 expect(r.raw.pairs).toHaveLength(1); 96 expect(r.unread).toMatchObject({ count: 1 }); 97 expect(r.afterCounts).toEqual(r.counts); 98 const row = r.raw.pairs[0] as { owner: string; id: string; wire: string }; 99 expect(row.id).toMatch( 100 /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ 101 ); 102 expect(Object.keys(JSON.parse(row.wire) as object).sort()).toEqual( 103 ['schema', 'family', 'owner', 'id', 'peer'].sort() 104 ); 105 }); 106 test('known route lookup reads original owner metadata without new writes prompts or hidden publication', async ({ 107 page 108 }) => { 109 const r = await page.evaluate(async () => { 110 const f = window.hcp100Fixture; 111 f.capture(); 112 const saved = await f.remember(); 113 const raw = await f.rawMetadata(); 114 const id = (raw.pairs[0] as { id: string }).id; 115 const counts = f.counts(); 116 return { 117 saved, 118 id, 119 result: await f.resolve(id), 120 before: raw, 121 after: await f.rawMetadata(), 122 counts, 123 afterCounts: f.counts(), 124 owner: f.owner, 125 peer: f.peer 126 }; 127 }); 128 expect(r.result).toEqual({ 129 status: 'resolved', 130 owner: r.owner, 131 peer: r.peer, 132 conversationId: r.id, 133 href: '/messages/' + r.id 134 }); 135 expect(r.after).toEqual(r.before); 136 expect(r.afterCounts).toEqual(r.counts); 137 }); 138 test('two distinct actual encrypted product contexts share one participant-pair room', async ({ 139 page 140 }) => { 141 const r = await page.evaluate(async () => { 142 const f = window.hcp100Fixture; 143 f.capture(); 144 const first = await f.product('About carrots: two kilograms'), 145 second = await f.product('About apples: pickup tomorrow'); 146 return { 147 first, 148 second, 149 raw: await f.rawMetadata(), 150 messages: f.messages() 151 }; 152 }); 153 expect(r.first).toMatchObject({ status: 'saved' }); 154 expect(r.second).toMatchObject({ status: 'existing' }); 155 expect(r.raw.pairs).toHaveLength(1); 156 expect(r.messages).toMatchObject({ status: 'ready' }); 157 const messages = ( 158 r.messages as { messages: readonly { content: string }[] } 159 ).messages.map((x) => x.content); 160 expect(messages).toContain('About carrots: two kilograms'); 161 expect(messages).toContain('About apples: pickup tomorrow'); 162 }); 163 test('a genuine other account cannot infer an existing UUID peer or owner metadata', async ({ 164 page 165 }) => { 166 const r = await page.evaluate(async () => { 167 const f = window.hcp100Fixture; 168 f.capture(); 169 await f.remember(); 170 const raw = await f.rawMetadata(); 171 return await f.otherOwner((raw.pairs[0] as { id: string }).id); 172 }); 173 expect(r).toEqual({ status: 'unavailable' }); 174 }); 175 test('copied directory and copied room cannot authorize reading or mapping creation', async ({ 176 page 177 }) => { 178 const r = await page.evaluate(async (id) => { 179 const f = window.hcp100Fixture; 180 f.capture(); 181 const raw = await f.rawMetadata(); 182 return { 183 copied: await f.copied(id), 184 forged: await f.forgedRoom(), 185 before: raw, 186 after: await f.rawMetadata() 187 }; 188 }, missing); 189 expect(r.copied).toEqual({ status: 'unavailable' }); 190 expect(r.forged).toEqual({ status: 'invalid' }); 191 expect(r.after).toEqual(r.before); 192 }); 193 test('missing explicit mapping review never creates a substitute or marks read', async ({ 194 page 195 }) => { 196 const r = await page.evaluate(async () => { 197 const f = window.hcp100Fixture; 198 f.capture(); 199 const raw = await f.rawMetadata(); 200 return { 201 result: await f.remember('unreviewed'), 202 before: raw, 203 after: await f.rawMetadata(), 204 unread: await f.unread() 205 }; 206 }); 207 expect(r.result).toEqual({ status: 'invalid' }); 208 expect(r.after).toEqual(r.before); 209 expect(r.unread).toMatchObject({ count: 1 }); 210 }); 211 test('original owner logout invalidates lookup and admitted creation without returning metadata', async ({ 212 page 213 }) => { 214 const r = await page.evaluate(async () => { 215 const f = window.hcp100Fixture; 216 f.capture(); 217 await f.remember(); 218 const raw = await f.rawMetadata(); 219 const id = (raw.pairs[0] as { id: string }).id; 220 f.disconnect(); 221 return { 222 resolve: await f.resolve(id), 223 remember: await f.remember(), 224 before: raw, 225 after: await f.rawMetadata() 226 }; 227 }); 228 expect(r.resolve).toEqual({ status: 'unavailable' }); 229 expect(r.remember).toEqual({ status: 'unavailable' }); 230 expect(r.after).toEqual(r.before); 231 }); 232 test('corrupt native mapping fails safely without peer metadata or automatic reset', async ({ 233 page 234 }) => { 235 const r = await page.evaluate(async (id) => { 236 const f = window.hcp100Fixture; 237 f.capture(); 238 await f.corruptPair(); 239 const raw = await f.rawMetadata(); 240 return { 241 resolve: await f.resolve(id), 242 before: raw, 243 after: await f.rawMetadata() 244 }; 245 }, missing); 246 expect(r.resolve).toEqual({ status: 'unavailable' }); 247 expect(r.after).toEqual(r.before); 248 }); 249 test('same handle in another actual browser storage context is unavailable without substitution', async ({ 250 page, 251 browser 252 }) => { 253 const id = await page.evaluate(async () => { 254 const f = window.hcp100Fixture; 255 f.capture(); 256 await f.remember(); 257 return ((await f.rawMetadata()).pairs[0] as { id: string }).id; 258 }); 259 const context = await browser.newContext(); 260 try { 261 const other = await context.newPage(); 262 await other.goto(server.url + '/search'); 263 await other.addScriptTag({ content: bundle }); 264 const r = await other.evaluate(async (id) => { 265 const f = await window.hcp100.makeFixture(); 266 try { 267 await f.ready(); 268 f.capture(); 269 return { result: await f.resolve(id), raw: await f.rawMetadata() }; 270 } finally { 271 f.close(); 272 } 273 }, id); 274 expect(r.result).toEqual({ status: 'unavailable' }); 275 expect(r.raw.pairs).toEqual([]); 276 } finally { 277 await context.close(); 278 } 279 }); 280 test('direct private reload has generic gates title noindex and a safe inbox link without recipient inference', async ({ 281 page 282 }) => { 283 await page.goto(server.url + '/messages/' + missing); 284 await expect( 285 page.getByRole('heading', { name: 'Connect or unlock' }) 286 ).toBeVisible(); 287 await expect( 288 page.getByRole('link', { name: 'Back to messages' }) 289 ).toHaveAttribute('href', '/messages'); 290 await expect(page).toHaveTitle('HarvestCircle'); 291 await expect(page.locator('meta[name="robots"]')).toHaveAttribute( 292 'content', 293 'noindex' 294 ); 295 await expect(page.locator('body')).not.toContainText( 296 'Actual encrypted room fixture' 297 ); 298 expect(page.url()).toBe(server.url + '/messages/' + missing); 299 }); 300 301 test('mutated detached observation cannot import original metadata into a genuine same-owner foreign generation', async ({ 302 page 303 }) => { 304 const r = await page.evaluate(() => 305 window.hcp100Fixture.mutatedGenerationObservation() 306 ); 307 console.log( 308 JSON.stringify({ 309 fixture: 'HCP100_GENUINE_OBSERVATION_REBIND', 310 sameOwner: r.sameOwner, 311 differentGeneration: r.differentGeneration, 312 foreignAccepted: r.foreignAccepted, 313 originalAccepted: r.originalAccepted, 314 rememberedStatus: r.remembered.status, 315 actualPairs: r.raw.pairs.length, 316 qualification: 'SOURCE_FIXTURE_NOT_OPERATOR_AUTHORITY' 317 }) 318 ); 319 expect(r.sameOwner).toBe(true); 320 expect(r.differentGeneration).toBe(true); 321 expect(r.foreignAccepted).toBe(false); 322 expect(r.originalAccepted).toBe(true); 323 expect(r.remembered).toEqual({ status: 'unavailable' }); 324 expect(r.raw.pairs).toEqual([]); 325 });