tiny-scene-three / tests /shot.mjs
paralaif's picture
Tiny Scene: text to animated three.js scenes with a 4.85M Transformer
dd53693 verified
Raw History Blame Contribute Delete
2.72 kB
// Opens the app in headless Chromium, runs prompts and saves screenshots.
// node tests/shot.mjs <out-dir> "name=prompt one;prompt two@t" ...
import fs from 'node:fs';
import http from 'node:http';
import path from 'node:path';
import { chromium } from 'playwright-core';
const WEB = new URL('../web/', import.meta.url).pathname;
const MIME = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.bin': 'application/octet-stream' };
const server = http.createServer((req, res) => {
const f = path.join(WEB, decodeURIComponent(req.url.split('?')[0]).replace(/\/$/, '/index.html'));
if (!f.startsWith(WEB) || !fs.existsSync(f)) { res.writeHead(404); return res.end(); }
res.writeHead(200, { 'content-type': MIME[path.extname(f)] ?? 'application/octet-stream' });
fs.createReadStream(f).pipe(res);
}).listen(0);
const port = server.address().port;
const [out, ...jobs] = process.argv.slice(2);
fs.mkdirSync(out, { recursive: true });
const exe = process.env.CHROME ?? fs.readdirSync(`${process.env.HOME}/.cache/ms-playwright`).filter((d) => d.startsWith('chromium_headless_shell'))
.map((d) => `${process.env.HOME}/.cache/ms-playwright/${d}`).flatMap((d) => fs.readdirSync(d).flatMap((s) => [`${d}/${s}/chrome-headless-shell`, `${d}/${s}/headless_shell`])).find((p) => fs.existsSync(p));
const browser = await chromium.launch({ executablePath: exe, args: ['--use-gl=angle', '--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist'] });
const size = process.env.SIZE?.split('x').map(Number) ?? [1280, 760];
const page = await browser.newPage({ viewport: { width: size[0], height: size[1] } });
const errors = [];
page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text()); });
page.on('pageerror', (e) => errors.push(String(e)));
for (const job of jobs) {
const [name, rest] = job.split(/=(.*)/s), [q, t = '2.5'] = rest.split('@');
await page.goto(`http://localhost:${port}/?q=${encodeURIComponent(q)}&t=${t}`);
await page.waitForFunction(() => window.tinyScene?.ready, null, { timeout: 60000 }).catch(async () => {
console.log('NOT READY', await page.evaluate(() => document.getElementById('status')?.textContent), errors);
process.exit(1);
});
await page.waitForTimeout(700);
const info = await page.evaluate(() => ({ scene: window.tinyScene.scene, log: [...document.querySelectorAll('.msg.bot')].map((m) => m.innerText.replace(/\n+/g, ' | ')).slice(1) }));
await page.screenshot({ path: `${out}/${name}.png` });
console.log(name, '->', info.log.join(' || '));
}
if (errors.length) console.log('PAGE ERRORS:\n' + [...new Set(errors)].join('\n'));
await browser.close();
server.close();