Topic6APIG / tests /browser.cjs
miyuki17's picture
feat(course): release public no-GPU image generation student lab
3b5f917 verified
Raw History Blame Contribute Delete
2.82 kB
/** Real Chromium smoke test. Requires 'playwright' installed in Node path. */
const fs=require('node:fs');
const path=require('node:path');
const http=require('node:http');
const assert=require('node:assert/strict');
const {chromium}=require('playwright');
const root=path.resolve(__dirname,'..');
const mime={'.html':'text/html','.css':'text/css','.js':'text/javascript','.mjs':'text/javascript','.svg':'image/svg+xml'};
const server=http.createServer((req,res)=>{
const name=decodeURIComponent(new URL(req.url,'http://localhost').pathname);
const resolved=path.resolve(root,'.'+name);
if(!(resolved===root || resolved.startsWith(root+path.sep))){res.writeHead(403).end();return;}
const p=resolved===root?path.join(root,'index.html'):resolved;
fs.readFile(p,(e,data)=>e?res.writeHead(404).end('Not found'):res.writeHead(200,{'Content-Type':mime[path.extname(p)]||'text/plain'}).end(data));
});
async function main(){
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
const base='http://127.0.0.1:'+server.address().port;
const browser=await chromium.launch({headless:true});
try {
for(const width of [1280,390]){
const page=await browser.newPage({viewport:{width,height:844}});
const errors=[];
page.on('pageerror',e=>errors.push(e.message));
await page.goto(base,{waitUntil:'networkidle'});
assert.match(await page.title(),/Image Generation/);
assert.equal(await page.locator('.architecture-card').count(),3);
assert.equal(await page.locator('.architecture-card img').count(),3);
await page.locator('#model').selectOption('sdxl');
await page.locator('#mode').selectOption('prompt');
await page.locator('#seed').fill('42');
await page.locator('button[type=submit]').click();
const plan=JSON.parse(await page.locator('#plan-output').textContent());
assert.equal(plan.runs.length,3);
assert.deepEqual(plan.runs.map(x=>x.prompt),['A teapot','A red teapot','A red ceramic teapot']);
await page.locator('#model').selectOption('ddpm');
assert.equal(await page.locator('#mode').inputValue(),'seed');
assert.equal(await page.locator('#prompt-fields').isVisible(),false);
await page.locator('button[type=submit]').click();
const ddpm=JSON.parse(await page.locator('#plan-output').textContent());
assert.equal(ddpm.runs.length,2);
assert.equal(ddpm.runs.some(x=>Object.hasOwn(x,'prompt')),false);
const overflow=await page.evaluate(()=>document.documentElement.scrollWidth-innerWidth);
assert.ok(overflow<=1,'Horizontal overflow '+width+': '+overflow);
assert.deepEqual(errors,[]);
console.log('BROWSER_PASS '+width+'px prompt/seed/navigation/overflow');
await page.close();
}
}finally{await browser.close();}
}
main().then(()=>server.close()).catch(err=>{console.error(err);server.close();process.exitCode=1;});