tiny-scene-three / tests /check_export.mjs
paralaif's picture
Tiny Scene: text to animated three.js scenes with a 4.85M Transformer
dd53693 verified
Raw History Blame Contribute Delete
1.82 kB
// Checks the .glb export in a real browser: node tests/check_export.mjs
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 server = http.createServer((req, res) => {
const f = path.join(WEB, decodeURIComponent(req.url.split('?')[0]).replace(/\/$/, '/index.html'));
if (!fs.existsSync(f)) { res.writeHead(404); return res.end(); }
res.writeHead(200, { 'content-type': { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json' }[path.extname(f)] ?? 'application/octet-stream' });
fs.createReadStream(f).pipe(res);
}).listen(0);
const root = `${process.env.HOME}/.cache/ms-playwright`;
const exe = process.env.CHROME ?? fs.readdirSync(root).filter((d) => d.startsWith('chromium_headless_shell')).flatMap((d) => fs.readdirSync(`${root}/${d}`).map((s) => `${root}/${d}/${s}/chrome-headless-shell`)).find((p) => fs.existsSync(p));
const browser = await chromium.launch({ executablePath: exe, args: ['--use-gl=angle', '--use-angle=swiftshader', '--enable-unsafe-swiftshader'] });
const page = await browser.newPage();
await page.goto(`http://localhost:${server.address().port}/?q=${encodeURIComponent('a knight riding a dragon next to a tower')}`);
await page.waitForFunction(() => window.tinyScene?.ready, null, { timeout: 60000 });
const r = await page.evaluate(async () => {
const blob = await window.tinyScene.stage.exportGLB(), head = new Uint8Array(await blob.slice(0, 4).arrayBuffer());
return { bytes: blob.size, magic: String.fromCharCode(...head) };
});
console.log('glb export:', r, r.magic === 'glTF' && r.bytes > 10000 ? 'ok' : 'FAILED');
await browser.close();
server.close();
process.exit(r.magic === 'glTF' ? 0 : 1);