File size: 3,229 Bytes
dd53693 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // Builds every object, action, place, time and weather the model can name and checks nothing breaks.
// node tests/check_scene.mjs
import fs from 'node:fs';
import * as THREE from 'three';
import { applyEdit, emptyScene, parse } from '../web/js/lang/scenelang.js';
import { BUILDERS, buildScene } from '../web/js/scene/layout.js';
const meta = JSON.parse(fs.readFileSync(new URL('../web/model/model.json', import.meta.url)));
const vals = (k) => meta.vocab.filter((t) => t.startsWith(k + '=')).map((t) => t.split('=')[1]);
const types = vals('type').filter((t) => t !== 'it' && t !== 'all'), acts = vals('act');
let fail = 0;
const bad = (msg) => { fail++; console.log('FAIL', msg); };
const missing = types.filter((t) => !BUILDERS[t]);
if (missing.length) bad('no builder for: ' + missing.join(', '));
function finite(root) {
let ok = true;
root.updateMatrixWorld(true);
root.traverse((o) => { for (const v of o.matrixWorld.elements) if (!Number.isFinite(v)) ok = false; });
return ok;
}
function run(sc, label) {
try {
const b = buildScene(sc);
for (const t of [0, 0.37, 1.9, 7.3, 31.4]) { b.update(t); if (!finite(b.root)) return bad(label + ' non-finite at t=' + t); }
if (!Number.isFinite(b.stageR) || b.box.isEmpty()) bad(label + ' bad bounds');
b.dispose();
} catch (e) { bad(label + ' threw ' + e.stack.split('\n').slice(0, 3).join(' | ')); }
}
const scene = (ents, extra = {}) => ({ ...emptyScene(), ...extra, ents: ents.map((e, i) => ({ id: 'e' + (i + 1), ...e })) });
let n = 0;
for (const type of types) for (const act of [undefined, ...acts]) { run(scene([{ type, act }]), `${type}/${act}`); n++; }
for (const loc of vals('loc')) for (const time of vals('time')) for (const weather of vals('weather')) {
run(scene([{ type: 'dog', act: 'walk' }, { type: 'boat' }], { loc, time, weather }), `${loc}/${time}/${weather}`); n++;
}
for (const rel of vals('rel')) for (const a of ['alien', 'fox', 'bird', 'ball', 'car']) for (const b of ['horse', 'skateboard', 'tree', 'ufo', 'chair']) {
run(scene([{ type: a, rel, ref: 'e2', act: rel === 'over' ? 'jump' : 'dance', n: 2 }, { type: b }]), `${a} ${rel} ${b}`); n++;
}
for (const size of vals('size')) run(scene([{ type: 'cow', size, n: 5, color: 'gold' }]), 'size ' + size), n++;
// scene language round trip
const p = parse('<new> loc=beach time=sunset <ent> type=fox color=red act=jump rel=over <ent> type=car color=blue'.split(' '));
if (p.kind !== 'new' || p.scene.ents.length !== 2 || p.scene.ents[0].ref !== 'e2') bad('parse new');
const e = parse('<edit> <set> type=fox color=green <add> type=tree n=2 <del> type=car <cam> shot=out <env> weather=rain'.split(' '));
const r = applyEdit(p.scene, e.ops);
if (r.scene.ents.length !== 2 || r.scene.ents[0].color !== 'green' || r.scene.ents[0].rel || r.scene.zoom !== 1 || r.scene.weather !== 'rain') bad('apply edit ' + JSON.stringify(r.scene));
if (applyEdit(p.scene, parse('<edit> <set> type=cow color=red'.split(' ')).ops).notes[0]?.code !== 'missing') bad('missing note');
run(r.scene, 'edited scene');
console.log(`${n} scenes built, ${types.length} types, ${acts.length} actions, three r${THREE.REVISION}: ${fail ? fail + ' FAILED' : 'all ok'}`);
process.exit(fail ? 1 : 0);
|