Playbook and Three.js code kit for detailed dioramas: classic pixel art, stroked toon or pixel + stroke; two intake gates for look and model sourcing; free orbit, palette snap and ink; animated toy-scale cast; per-entity frame QA.
JavaScript agent-skillagent-skill-repositoryagent-skillsai-agentscreative-codingdioramagame-artpixel-artpixelationprocedural-artprocedural-generationskillthree-jsthreejswebgl
import test from 'node:test';
import assert from 'node:assert/strict';
import { pathToFileURL } from 'node:url';
import { createActionClearance } from '../templates/action-clearance.mjs';
const THREE = await import(pathToFileURL(process.env.THREE_MODULE_PATH || '/Users/apple/Desktop/monstertruck/node_modules/three/build/three.module.js').href);
const { sphere: S, capsule: C, obb: B, pairCheck: check, auditPose: pose, timesForInterval: grid, auditMotion: motion } = createActionClearance(THREE);
const part = (id, s, owner = id) => ({ id, owner, getShape: typeof s === 'function' ? s : () => s });
const box = B([0, 0, 0], [1, 1, 1]);
const contact = { a: 'seat', b: 'hips', start: 0, end: 1, maxPenetration: .2, reason: 'support' };
const support = () => [part('seat', box), part('hips', S([0, 1.05, 0], .1))];
test('standing actor clear of building', () => assert.ok(pose({ parts: [part('actor', C([0, 0, 0], [0, 2, 0], .2)), part('building', B([4, 1, 0], [1, 2, 1]))] }).clear));
test('walking actor sampled clear', () => { let t; assert.ok(motion({ source: { evaluate: x => t = x }, times: [0, .5, 1], parts: [part('actor', () => S([t, 0, 0], .2)), part('wall', S([5, 0, 0], .2))] }).clear); });
test('moved arm caught', () => { let x = 3; const parts = [part('arm', () => C([x, 0, 0], [x, 1, 0], .1)), part('wall', box)]; assert.ok(pose({ parts }).clear); x = 0; assert.equal(pose({ parts }).clear, false); });
test('animal tail caught', () => assert.equal(pose({ parts: [part('tail', C([-2, 0, 0], [2, 0, 0], .1), 'animal'), part('rock', box)] }).clear, false));
test('bounded seat support allowed', () => assert.ok(pose({ parts: support(), contacts: [contact], time: .5 }).clear));
test('contact does not mask knee', () => { const r = pose({ parts: [...support(), part('knee', S([0, 0, 0], .1), 'actor')], contacts: [contact] }); assert.ok(r.collisions.some(c => c.a === 'knee' || c.b === 'knee')); });
test('contact does not mask chair back', () => assert.equal(pose({ parts: [...support(), part('back', B([0, 1, 0], [.5, 1, .5]))], contacts: [contact] }).clear, false));
test('contact outside window forbidden', () => assert.equal(pose({ parts: support(), contacts: [contact], time: 2 }).clear, false));
test('deep intended contact forbidden', () => assert.equal(pose({ parts: [part('seat', box), part('hips', S([0, 0, 0], .1))], contacts: [contact] }).clear, false));
test('inside capsule retains deepest negative sdf', () => assert.ok(Math.abs(check(C([-.5, 0, 0], [.5, 0, 0], .2), box).separation + 1.2) < 1e-12));
test('segment outside box exact distance', () => assert.ok(Math.abs(check(C([-2, 2, 2], [2, 2, 2], .2), box).separation - (Math.sqrt(2) - .2)) < 1e-12));
test('rotated OBB', () => { const q = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 0, 1), Math.PI / 2); assert.ok(check(S([0, 1.5, 0], .1), B([0, 0, 0], [2, .2, .2], q)).separation < 0); });
test('crossed capsules', () => assert.equal(check(C([-1, 0, 0], [1, 0, 0], .2), C([0, -1, 0], [0, 1, 0], .3)).separation, -.5));
test('degenerate capsule is sphere', () => assert.equal(check(C([0, 0, 0], [0, 0, 0], 1), S([3, 0, 0], 1)).separation, 1));
test('world units nonuniform extents', () => assert.ok(check(S([99, 0, 0], .1), B([0, 0, 0], [100, .01, 2])).separation < 0));
test('touching allowed at zero margin', () => assert.ok(pose({ parts: [part('a', S([0, 0, 0], 1)), part('b', S([2, 0, 0], 1))] }).clear));
test('positive margin flags proximity accurately', () => { const r = pose({ parts: [part('a', S([0, 0, 0], 1)), part('b', S([2.1, 0, 0], 1))], clearance: .2 }); assert.equal(r.collisions[0].kind, 'clearance-violation'); });
test('same owner arm through body checked', () => assert.equal(pose({ parts: [part('arm', S([0, 0, 0], 1), 'actor'), part('body', S([0, 0, 0], 1), 'actor')] }).clear, false));
test('SAT diagonal distance explicitly axis bound', () => { const r = check(box, B([3, 3, 0], [1, 1, 1])); assert.equal(r.separation, 1); assert.match(r.method, /axis-bound/); });
test('broadphase removes separated bounds', () => assert.equal(pose({ parts: [part('a', box), part('b', B([30, 30, 0], [1, 1, 1]))] }).testedPairs, 0));
test('endpoints pass midpoint fails', () => { let t; const options = { source: { evaluate: x => t = x }, parts: [part('actor', () => S([4 * Math.abs(t - .5), 0, 0], .2)), part('obstacle', S([0, 0, 0], .2))] }; assert.ok(motion({ ...options, times: [0, 1] }).clear); const r = motion({ ...options, times: grid(0, 1, { maxStep: .5 }) }); assert.equal(r.clear, false); assert.equal(r.worstCases[0].time, .5); assert.equal(r.sampledOnly, true); });
test('grid endpoints exact events distinct deterministic', () => assert.deepEqual(grid(0, 1, { maxStep: .5, eventTimes: [.123, .5, .123] }), [0, .123, .5, 1]));
test('motion evaluates exactly requested increasing times', () => { const seen = []; motion({ source: { evaluate: t => seen.push(t) }, times: [0, .123, 1], parts: [part('a', box)] }); assert.deepEqual(seen, [0, .123, 1]); });
test('missing empty singleton duplicate and reversed motion coverage rejected before evaluation', () => { for (const times of [undefined, [], [0], [0, 0], [1, 0], [0, 1, .5], [0, NaN], [0, Infinity], new Array(2)]) { let calls = 0; assert.throws(() => motion({ source: { evaluate() { calls++; } }, times, parts: [part('a', box)] })); assert.equal(calls, 0); } });
test('async and thenable evaluation cannot silently audit stale snapshots', () => { for (const evaluate of [async () => {}, () => ({ then() {} })]) { let snapshots = 0; assert.throws(() => motion({ source: { evaluate }, times: [0, 1], parts: [part('a', () => { snapshots++; return box; })] }), /synchronous/); assert.equal(snapshots, 0); } });
test('empty duplicate missing unsupported NaN rejected', () => { for (const parts of [[], [part('a', box), part('a', box)], [{ id: 'a' }], [part('a', { type: 'mesh' })], [part('a', { type: 'sphere', center: [NaN, 0, 0], radius: 1 })], [part('', box)]]) assert.throws(() => pose({ parts })); });
test('bad dimensions quaternion rejected safely normalized', () => { assert.throws(() => S([0, 0, 0], 0)); assert.throws(() => B([0, 0, 0], [1, -1, 1])); assert.throws(() => B([0, 0, 0], [1, 1, 1], [0, 0, 0, 0])); assert.equal(B([0, 0, 0], [1, 1, 1], [0, 0, 0, 1e300]).quaternion.w, 1); });
test('missing contact proxy and malformed contacts rejected', () => { for (const rule of [{ ...contact, a: 'actor' }, { ...contact, maxPenetration: Infinity }, { ...contact, maxPenetration: -1 }, { ...contact, reason: '' }]) assert.throws(() => pose({ parts: support(), contacts: [rule] })); });
test('hidden material unused', () => assert.equal(pose({ parts: [{ ...part('a', box), material: { visible: false }, visible: false }, part('b', S([0, 0, 0], .1))] }).clear, false));
test('callback follows parent world positions every evaluation', () => { const parent = new THREE.Object3D(), child = new THREE.Object3D(); parent.add(child); child.position.x = 2; const parts = [part('child', () => { parent.updateMatrixWorld(true); return S(child.getWorldPosition(new THREE.Vector3()), .1); }), part('wall', box)]; assert.ok(pose({ parts }).clear); parent.position.x = -2; assert.equal(pose({ parts }).clear, false); });
test('independent required inventory verifies exact coverage regardless order', () => { const parts = [part('actor', box), part('wall', S([5, 0, 0], 1))]; assert.equal(pose({ parts, requiredIds: ['wall', 'actor'] }).coverageVerified, true); const r = motion({ source: { evaluate() {} }, times: [0, 1], parts, requiredIds: ['actor', 'wall'] }); assert.equal(r.coverageVerified, true); assert.ok(r.samples.every(s => s.coverageVerified)); });
test('omitted inventory explicitly reports unknown coverage in pose and motion', () => { const parts = [part('wall', box)]; assert.equal(pose({ parts }).coverageVerified, false); assert.equal(motion({ source: { evaluate() {} }, times: [0, 1], parts }).coverageVerified, false); });
test('missing whole actor extra and duplicate expected ids reject before snapshots', () => { for (const requiredIds of [['wall', 'wholeactor'], ['other'], [], ['wall', 'wall'], [''], null, 'wall', new Array(1)]) { let calls = 0; assert.throws(() => pose({ parts: [part('wall', () => { calls++; return box; })], requiredIds })); assert.equal(calls, 0); } });
test('unexpected extra proxy rejects before snapshots', () => { let calls = 0; assert.throws(() => pose({ parts: [part('actor', () => { calls++; return box; }), part('extra', box)], requiredIds: ['actor'] })); assert.equal(calls, 0); });
test('canonical ids no coordinate inferred owner', () => { const r = pose({ parts: [part('z', box), part('a', box)] }); assert.deepEqual([r.collisions[0].a, r.collisions[0].b], ['a', 'z']); });
test('invalid grid and motion fail closed', () => { assert.throws(() => grid(0, 1, { maxStep: 0 })); assert.throws(() => grid(0, 1, { maxStep: .1, eventTimes: [NaN] })); assert.throws(() => motion({ source: { evaluate() {} }, times: [], parts: [part('a', box)] })); });