const assert=require('node:assert/strict'); const fs=require('node:fs'); const {chromium}=require(process.env.PLAYWRIGHT_MODULE || 'playwright-core'); const expected=require('./expected.json'); const base=process.env.TEST_URL || 'http://127.0.0.1:8796'; const chrome=process.env.CHROME; const results={}; (async()=>{ const browser=await chromium.launch({headless:true,...(chrome?{executablePath:chrome}:{}),args:['--no-sandbox']}); const page=await browser.newPage({viewport:{width:1600,height:1100},colorScheme:'light'}), errors=[],external=[]; page.on('pageerror',error=>errors.push(error.message)); page.on('request',request=>{if(new URL(request.url()).origin!==new URL(base).origin && !request.url().startsWith('data:'))external.push(request.url())}); const start=Date.now();await page.goto(base+'/?theme=light',{waitUntil:'networkidle'});await page.waitForFunction(()=>window.pybotSummary); results.initial_load_ms=Date.now()-start;assert.equal(await page.evaluate(()=>pybotSummary.messages),expected.messages); assert.ok(await page.locator('#welcome-dialog').isVisible()); await page.screenshot({path:'test-results/welcome-light.png',fullPage:true}); await page.getByRole('button',{name:'Close welcome',exact:true}).click();assert.equal(await page.locator('#welcome-dialog').isVisible(),false); // A previous dismissal must never hide the welcome panel on a new page load. await page.evaluate(()=>localStorage.setItem('pybot-welcome-dismissed','true')); await page.reload({waitUntil:'networkidle'});await page.waitForFunction(()=>window.pybotSummary); assert.ok(await page.locator('#welcome-dialog').isVisible()); await page.evaluate(()=>localStorage.removeItem('pybot-welcome-dismissed')); await page.getByRole('button',{name:'Close welcome',exact:true}).click(); await page.reload({waitUntil:'networkidle'});await page.waitForFunction(()=>window.pybotSummary); assert.ok(await page.locator('#welcome-dialog').isVisible()); await page.getByRole('button',{name:'Close welcome',exact:true}).click(); results.welcome_on_reload=['legacy dismissal flag','fresh dismissal']; assert.equal(await page.locator('#board').evaluate(el=>Math.round(el.getBoundingClientRect().width)),1600); await page.locator('#show-welcome').click();await page.keyboard.press('Escape');assert.equal(await page.locator('#welcome-dialog').isVisible(),false); await page.locator('#show-welcome').click();await page.mouse.click(2,2);assert.equal(await page.locator('#welcome-dialog').isVisible(),false); results.welcome_overlay_dismissal=['close button','Escape','outside click']; const frame=page.frames().find(f=>f.url().includes('board.html')); const transport=await frame.evaluate(async()=>{ const files=['data/events.bin.gz','assets/fonts/LetterGothic.ttf'];const checks=[]; for(const file of files){const response=await fetch(file);checks.push({file,status:response.status,redirected:response.redirected,origin:new URL(response.url).origin,cache:response.headers.get('Cache-Control'),encoding:response.headers.get('Content-Encoding')})} return checks; }); for(const check of transport){assert.equal(check.status,200);assert.equal(check.redirected,false);assert.equal(check.origin,new URL(base).origin);assert.equal(check.cache,'no-cache');assert.equal(check.encoding,null)} results.same_origin_data_and_fonts=true; assert.equal(await frame.locator('#messages .msg').count(),60);assert.equal(await frame.locator('#login').isVisible(),false); const firstHash=await frame.evaluate(async()=>{const m=await archiveApi('GET','/messages/1');const hash=await crypto.subtle.digest('SHA-256',new TextEncoder().encode(m.body));return [...new Uint8Array(hash)].map(x=>x.toString(16).padStart(2,'0')).join('')}); assert.equal(firstHash,expected.first_message_sha256); // Shared links must reach arbitrarily old messages without paging from the end. await frame.evaluate(async()=>jumpTo(await archiveApi('GET','/messages/1')));assert.equal(await frame.locator('.msg[data-mid="1"]').count(),1); await frame.locator('#messages .name[data-profile="'+expected.profile.id+'"]').first().click();await frame.locator('.profile .stat').waitFor(); assert.equal((await frame.locator('.profile .stat').textContent()).replace(/,/g,''),String(expected.profile.count)); await frame.locator('#panel-close').click(); // The largest thread must expose every reply, with bounded rendering. await frame.evaluate(async root=>{await jumpTo(await archiveApi('GET','/messages/'+root));await openThread(root)},expected.largest_thread.root); assert.ok((await frame.locator('.thread-count').textContent()).includes(expected.largest_thread.replies.toLocaleString('en'))); assert.ok(await frame.locator('#panel-scroll .msg').count()<=61); await frame.getByRole('button',{name:'First',exact:true}).click();await frame.waitForFunction(()=>state.panel.start===0); results.largest_thread_replies=expected.largest_thread.replies;await frame.locator('#panel-close').click(); // Search runs over the complete archive, including old replies, in a worker. const searchStart=Date.now();const found=await frame.evaluate(async()=>archiveApi('GET','/search?q='+encodeURIComponent('"Lane start" from:@noble-walrus in:#general')+'&sort=oldest&limit=5')); assert.equal(found.messages[0].id,1);results.first_search_ms=Date.now()-searchStart; await page.keyboard.press('Control+k');await frame.locator('#search-q').fill('"Lane start" from:@noble-walrus in:#general');await frame.locator('#search-form button').click();await frame.locator('#search-results .sres').first().waitFor(); assert.ok((await frame.locator('#search-results').textContent()).includes('Lane start')); // Doc deltas must reconstruct exactly, both at the snapshot and during replay. async function docHash(){return frame.evaluate(async()=>{const doc=await archiveApi('GET','/channels/1/doc'),hash=await crypto.subtle.digest('SHA-256',new TextEncoder().encode(doc.body));return {version:doc.version,sha256:[...new Uint8Array(hash)].map(x=>x.toString(16).padStart(2,'0')).join('')}})} assert.deepEqual(await docHash(),expected.doc.latest); await frame.evaluate(async()=>{await refreshChannels();await openChannel(1)}); const seekStart=Date.now();await page.evaluate(time=>window.pybotSeek(time),expected.cutoff); assert.equal(await frame.evaluate(()=>ArchiveUtils.upperBound(pybotArchive.times,pybotArchive.cutoff)),expected.messages_at_cutoff); assert.deepEqual(await docHash(),expected.doc.historical); assert.ok(await frame.evaluate(()=>state.roots.every(m=>m.created_at<=pybotArchive.cutoff))); results.seek_ms=Date.now()-seekStart;results.seek_render_ms=await page.evaluate(()=>pybotLastRender.milliseconds); // Forward and backward seeks keep message rendering bounded. const renders=[];for(const fraction of [.25,.75,.4,1]){await page.evaluate(async fraction=>{await pybotSeek(pybotSummary.first_at+fraction*(pybotSummary.last_at-pybotSummary.first_at))},fraction);renders.push(await page.evaluate(()=>pybotLastRender.milliseconds));assert.ok(await frame.locator('#messages .msg').count()<=240)} results.seek_render_samples_ms=renders; await page.locator('#play').click();await page.waitForTimeout(1400);await page.locator('#play').click();await page.waitForTimeout(300); assert.ok(await frame.evaluate(()=>pybotArchive.cutoffstate.roots.every(m=>m.created_at<=pybotArchive.cutoff))); await page.locator('#full').click();await page.waitForFunction(()=>!rendering && window.pybotLastRender && document.querySelector('#full').disabled); await frame.locator('#search-input').fill(''); await page.screenshot({path:process.env.SCREENSHOT_DIR?process.env.SCREENSHOT_DIR+'/desktop-light.png':'test-results/desktop-light.png',fullPage:true}); await page.locator('#show-welcome').click();await page.locator('#theme').click();await page.screenshot({path:'test-results/welcome-dark.png',fullPage:true}); await page.locator('#close-welcome').click();await page.screenshot({path:process.env.SCREENSHOT_DIR?process.env.SCREENSHOT_DIR+'/desktop-dark.png':'test-results/desktop-dark.png',fullPage:true}); const mobile=await browser.newPage({viewport:{width:430,height:932},colorScheme:'light'});mobile.on('pageerror',error=>errors.push(error.message)); await mobile.goto(base+'/?theme=light',{waitUntil:'networkidle'});await mobile.waitForFunction(()=>window.pybotSummary);assert.equal(await mobile.evaluate(()=>document.documentElement.scrollWidth),430); await mobile.screenshot({path:'test-results/welcome-mobile.png',fullPage:true});await mobile.locator('#close-welcome').click(); await mobile.locator('#board').scrollIntoViewIfNeeded(); const mobileFrame=mobile.frames().find(f=>f.url().includes('board.html'));await mobileFrame.locator('#menu').click();assert.ok(await mobileFrame.locator('#app').evaluate(el=>el.classList.contains('nav'))); await mobileFrame.locator('#channels button').filter({hasText:'answers'}).click();assert.equal(await mobileFrame.locator('#app').evaluate(el=>el.classList.contains('nav')),false); await mobile.screenshot({path:process.env.SCREENSHOT_DIR?process.env.SCREENSHOT_DIR+'/mobile.png':'test-results/mobile.png',fullPage:true}); assert.deepEqual(errors,[]);assert.deepEqual(external,[]);results.errors=errors;results.external_requests=external;results.status='passed'; await browser.close();fs.writeFileSync('test-results/browser.json',JSON.stringify(results,null,2)+'\n');console.log(JSON.stringify(results)); })().catch(error=>{console.error(error.stack);process.exit(1)});