Skip to content

3D engine that AI agents drive

23.2 KB
Raw Blame
import './support/load-agent-scene-modules.mjs';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import test from 'node:test';
import { createAgentScene } from '../src/agent-scene.js';
import { curve, curveSet } from '../src/modeling.js';
import { sampleNurbs, sampleSpline, shapeToSpline, trimSamples } from '../src/modeling-splines.js';

function hashOf(mesh) {
  const hash = createHash('sha256');
  for (const array of [mesh.positions, mesh.normals, mesh.uvs, mesh.indices]) hash.update(Buffer.from(array.buffer, array.byteOffset, array.byteLength));
  return hash.digest('hex');
}

function bounds({ positions }) {
  const minimum = [Infinity, Infinity, Infinity];
  const maximum = [-Infinity, -Infinity, -Infinity];
  for (let offset = 0; offset < positions.length; offset += 3) {
    for (let axis = 0; axis < 3; axis++) {
      minimum[axis] = Math.min(minimum[axis], positions[offset + axis]);
      maximum[axis] = Math.max(maximum[axis], positions[offset + axis]);
    }
  }
  return { minimum, maximum };
}

function openEdges({ positions, indices }) {
  const keyOf = vertex => Array.from(positions.subarray(vertex * 3, vertex * 3 + 3), value => value.toFixed(5)).join(',');
  const uses = new Map();
  for (let offset = 0; offset < indices.length; offset += 3) {
    for (let side = 0; side < 3; side++) {
      const from = keyOf(indices[offset + side]);
      const to = keyOf(indices[offset + (side + 1) % 3]);
      if (from === to) continue;
      const edge = from < to ? `${from}|${to}` : `${to}|${from}`;
      uses.set(edge, (uses.get(edge) ?? 0) + 1);
    }
  }
  return [...uses.values()].filter(count => count !== 2).length;
}

const wire = { radius: 0.01, radialSegments: 4 };

test('an arc primitive lies on its circle and ends where the sweep ends', () => {
  const spline = shapeToSpline({ type: 'arc', radius: 2, startAngle: 0, sweep: Math.PI, segments: 4 });
  const { points } = sampleSpline({ ...spline, points: spline.points }, 16);
  for (const point of points) assert.ok(Math.abs(Math.hypot(point[0], point[1]) - 2) < 1e-5, `off the circle: ${point}`);
  assert.deepEqual(points[0].map(Math.abs).map(value => Math.round(value * 1e6)), [2e6, 0, 0]);
  assert.ok(Math.abs(points.at(-1)[0] + 2) < 1e-9 && Math.abs(points.at(-1)[1]) < 1e-9);
  assert.equal(spline.closed, false);
  assert.equal(shapeToSpline({ type: 'arc', sweep: Math.PI * 2 }).closed, true, 'a full turn closes');
});

test('an arc span wider than a half turn is refused with the segment count to pass', () => {
  assert.throws(() => shapeToSpline({ type: 'arc', sweep: Math.PI * 2, segments: 1 }), /Pass shape\.segments of 2 or more/);
  assert.throws(() => shapeToSpline({ type: 'arc', sweep: 6, segments: 1 }), /too wide for a Bezier span/);
  const halfTurn = shapeToSpline({ type: 'arc', sweep: Math.PI, segments: 1 });
  assert.ok(halfTurn.points.every(point => point.handleOut.every(value => Math.abs(value) < 10)));
});

test('a spiral rises and widens, and a spiral staircase rail is a watertight swept tube', () => {
  const rail = curveSet([{ shape: { type: 'spiral', rotations: 1.5, startRadius: 1, endRadius: 1, height: 3, pointsPerRotation: 24 } }], { radius: 0.03, radialSegments: 6 });
  const { minimum, maximum } = bounds(rail);
  assert.ok(Math.abs(minimum[1]) < 0.06 || Math.abs(minimum[2]) < 0.06);
  assert.ok(Math.abs(maximum[2] - 3) < 0.04, `rail top at ${maximum[2]}`);
  assert.ok(maximum[0] <= 1.04 && minimum[0] >= -1.04);
  assert.equal(openEdges(rail), 0);
  const cone = shapeToSpline({ type: 'spiral', rotations: 2, startRadius: 0.5, endRadius: 2, height: 0, pointsPerRotation: 8 });
  assert.equal(cone.points.length, 17);
  assert.ok(Math.abs(Math.hypot(...cone.points.at(-1).slice(0, 2)) - 2) < 1e-9);
  const reversed = shapeToSpline({ type: 'spiral', rotations: 0.25, reverse: true, height: 0, pointsPerRotation: 4 });
  assert.ok(reversed.points[1][1] < 0, 'reverse turns clockwise');
});

test('a star alternates outer and inner radius and is a closed poly spline', () => {
  const star = shapeToSpline({ type: 'star', points: 5, innerRadius: 0.4, outerRadius: 1, innerTwist: 0 });
  assert.equal(star.type, 'poly');
  assert.equal(star.closed, true);
  assert.equal(star.points.length, 10);
  star.points.forEach((point, index) => assert.ok(Math.abs(Math.hypot(point[0], point[1]) - (index % 2 ? 0.4 : 1)) < 1e-9));
  assert.equal(openEdges(curveSet([{ shape: { type: 'star' } }], wire)), 0);
});

test('trim by factor keeps a prefix of the curve, and by length matches', () => {
  const full = [[0, 0, 0], [4, 0, 0]];
  const half = curve(full, { ...wire, trim: { end: 0.5 } });
  assert.ok(Math.abs(bounds(half).maximum[0] - 2) < 1e-6);
  assert.equal(openEdges(half), 0, 'a trimmed curve is capped');
  const byLength = curve(full, { ...wire, trim: { start: 1, end: 3, length: true } });
  const { minimum, maximum } = bounds(byLength);
  assert.ok(Math.abs(minimum[0] - 1) < 1e-6 && Math.abs(maximum[0] - 3) < 1e-6);
  const clamped = curve(full, { ...wire, trim: { start: 1, end: 99, length: true } });
  assert.ok(Math.abs(bounds(clamped).maximum[0] - 4) < 1e-6, 'a length past the end stops at the end');
  const whole = curve(full, { ...wire, trim: { start: 0, end: 1 } });
  assert.equal(hashOf(whole), hashOf(curve(full, wire)), 'the full range changes nothing on an open curve');
});

test('trimming a closed curve opens it', () => {
  const ring = { shape: { type: 'arc', sweep: Math.PI * 2, segments: 8 } };
  const closed = curveSet([ring], wire);
  const quarter = curveSet([{ ...ring, trim: { end: 0.25 } }], wire);
  assert.equal(openEdges(closed), 0);
  assert.equal(openEdges(quarter), 0);
  const { minimum, maximum } = bounds(quarter);
  assert.ok(minimum[0] > -0.02 && minimum[1] > -0.02, 'a quarter turn stays in the first quadrant');
  assert.ok(Math.abs(maximum[0] - 1) < 0.02 && Math.abs(maximum[1] - 1) < 0.02);
});

test('a growing vine: each growth step extends the previous trim with the same start', () => {
  const vine = [[0, 0, 0], [1, 1, 0], [2, 0, 1], [3, 1, 1], [4, 0, 0]];
  const reach = growth => bounds(curve(vine, { ...wire, resolution: 8, trim: { end: growth } })).maximum[0];
  const fractions = [0.1, 0.25, 0.5, 0.75, 1];
  const reaches = fractions.map(reach);
  for (let index = 1; index < reaches.length; index++) assert.ok(reaches[index] > reaches[index - 1], `step ${index} grows`);
  const sampled = sampleSpline({ type: 'bezier', closed: false, points: vine.map(position => ({ position })) }, 8);
  const prefix = trimSamples(sampled, false, { end: 0.5 });
  assert.deepEqual(prefix.points[0], sampled.points[0]);
  const lastKept = prefix.points.at(-2);
  assert.ok(sampled.points.some(point => point.every((value, axis) => value === lastKept[axis])), 'interior samples are untouched');
});

test('a spline set merges its splines into one mesh with indices offset', () => {
  const first = curve([[0, 0, 0], [1, 0, 0]], wire);
  const second = curveSet([{ shape: { type: 'star' } }], wire);
  const both = curveSet([{ points: [[0, 0, 0], [1, 0, 0]] }, { shape: { type: 'star' } }], wire);
  assert.equal(both.positions.length, first.positions.length + second.positions.length);
  assert.equal(both.indices.length, first.indices.length + second.indices.length);
  assert.deepEqual(Array.from(both.positions.subarray(0, first.positions.length)), Array.from(first.positions));
  assert.equal(Math.max(...both.indices), both.positions.length / 3 - 1);
  assert.equal(openEdges(both), 0);
});

test('a poly spline keeps its corners and a NURBS spline passes through its ends', () => {
  const corner = [[0, 0, 0], [1, 0, 0], [1, 1, 0]];
  const poly = curve(corner, { ...wire, kind: 'poly' });
  assert.ok(Math.abs(bounds(poly).maximum[0] - 1.01) < 1e-6, 'the corner is not rounded off');
  const nurbs = sampleNurbs(corner, [1, 1, 1], false, 8);
  assert.deepEqual(nurbs.points[0], corner[0]);
  assert.ok(nurbs.points.at(-1).every((value, axis) => Math.abs(value - corner[2][axis]) < 1e-12));
  const pulled = sampleNurbs(corner, [1, 8, 1], false, 8);
  const middle = point => Math.hypot(point[0] - 1, point[1] - 0);
  assert.ok(Math.min(...pulled.points.map(middle)) < Math.min(...nurbs.points.map(middle)), 'a higher weight pulls the curve to its point');
  const ring = sampleNurbs([[1, 0, 0], [0, 1, 0], [-1, 0, 0], [0, -1, 0]], [1, 1, 1, 1], true, 6);
  assert.equal(ring.points.length, 24);
  assert.equal(openEdges(curve([[1, 0, 0], [0, 1, 0], [-1, 0, 0], [0, -1, 0]], { ...wire, kind: 'nurbs', closed: true })), 0);
});

test('curve output is deterministic and matches the recorded geometry', () => {
  const build = () => curveSet([
    { shape: { type: 'spiral', rotations: 1, height: 1 }, trim: { start: 0.1, end: 0.9 } },
    { points: [[0, 0, 0], [1, 1, 0], [2, 0, 1], [3, 1, 1]], kind: 'nurbs', weights: [1, 2, 1, 1] },
    { shape: { type: 'star', points: 6 } },
  ], { radius: 0.02, radialSegments: 5, taper: [1, 0.5] });
  assert.equal(hashOf(build()), hashOf(build()));
  assert.equal(hashOf(build()), '066f5ace79bc38d147707b116d1488465cc729d4effa8a17ce10ae9a1f31289b');
});

test('curve rejects bad splines with the field and what to pass', () => {
  const line = [[0, 0, 0], [1, 0, 0]];
  const cases = [
    [() => curve(line, { trim: { start: 0.6, end: 0.4 } }), /trim\.end must be greater than points\.trim\.start|trim\.end must be greater/],
    [() => curve(line, { trim: { end: 2 } }), /trim\.start and .*trim\.end must be from 0 to 1/],
    [() => curve(line, { trim: { bogus: 1 } }), /Unknown .*trim field: bogus/],
    [() => curve(line, { kind: 'spiral' }), /kind must be one of bezier, poly, nurbs/],
    [() => curve(line, { weights: [1, 1] }), /weights apply to kind nurbs only/],
    [() => curve(line, { kind: 'nurbs', weights: [1] }), /needs one weight per point \(2\)/],
    [() => curve(line, { kind: 'nurbs', weights: [1, 0] }), /greater than 0/],
    [() => curveSet([], {}), /splines needs 1 to 64 entries/],
    [() => curveSet(Array.from({ length: 65 }, () => ({ points: line })), {}), /splines needs 1 to 64 entries/],
    [() => curveSet([{ points: line, shape: { type: 'star' } }], {}), /exactly one of points or shape/],
    [() => curveSet([{ shape: { type: 'star' }, closed: true }], {}), /closed and .*kind come from shape/],
    [() => curveSet([{ shape: { type: 'wave' } }], {}), /shape\.type must be one of arc, spiral, star/],
    [() => curveSet([{ shape: { type: 'star', points: 2 } }], {}), /shape\.points must be an integer from 3 to 256/],
    [() => curveSet([{ shape: { type: 'arc', sweep: 0 } }], {}), /shape\.sweep must not be 0/],
    [() => curveSet([{ shape: { type: 'spiral', rotations: 64, pointsPerRotation: 128 } }], {}), /at most 1024 points/],
    [() => curveSet([{ shape: { type: 'star', spikes: 3 } }], {}), /Unknown shape field: spikes/],
    [() => curveSet([{ points: line }, { points: [line[0]] }], {}), /splines\[1\]\.points needs 2 to 1024 entries/],
    [() => curveSet(Array.from({ length: 5 }, () => ({ points: Array.from({ length: 1000 }, (_, index) => [index, 0, 0]) })), {}), /at most 4096/],
    [() => curveSet([{ shape: { type: 'star', points: 256 } }], { maxVertices: 100 }), /exceeding maxVertices 100/],
  ];
  for (const [attempt, pattern] of cases) assert.throws(attempt, pattern);
});

test('a recipe curve geometry takes a shape, splines, kind and trim, and rejects mixing them', () => {
  const recipe = geometry => ({
    format: 'crucible.scene',
    schemaVersion: 1,
    materials: [{ id: 'metal', color: [0.6, 0.6, 0.65], metallic: 1, roughness: 0.3 }],
    geometries: [{ id: 'g', type: 'curve', ...geometry }],
    nodes: [{ id: 'n', geometry: 'g', material: 'metal' }],
  });
  const star = createAgentScene(recipe({ shape: { type: 'star' }, radius: 0.02 }));
  assert.ok(star.parts[0].geometry.indices.length > 0);
  const several = createAgentScene(recipe({ splines: [{ points: [[0, 0, 0], [1, 0, 0]], trim: { end: 0.5 } }, { shape: { type: 'arc' } }], radius: 0.02 }));
  assert.ok(several.parts[0].geometry.indices.length > 0);
  assert.throws(() => createAgentScene(recipe({ points: [[0, 0, 0], [1, 0, 0]], splines: [{ shape: { type: 'star' } }] })), /cannot be used with splines/);
  assert.throws(() => createAgentScene(recipe({ shape: { type: 'star', points: 1 } })), /shape\.points/);
  assert.throws(() => createAgentScene(recipe({ shape: { type: 'star' }, trim: { end: 3 } })), /trim/);
});

function vineRecipe(grow = { name: 'vine', steps: 8 }, keyframes = [{ time: 0, value: 0 }, { time: 1, value: 1 }]) {
  return {
    format: 'crucible.scene',
    schemaVersion: 1,
    materials: [{ id: 'leaf', color: [0.2, 0.6, 0.2] }],
    geometries: [{ id: 'vine', type: 'curve', points: [[0, 0, 0], [2, 0, 0], [4, 1, 0], [6, 1, 0]], kind: 'poly', radius: 0.02, radialSegments: 4, grow }],
    nodes: [{ id: 'vine', geometry: 'vine', material: 'leaf' }],
    motion: { duration: 1, tracks: [{ kind: 'morph', id: 'vine', property: 'weight', keyframes }] },
  };
}

function still(grow) {
  const { motion, ...recipe } = vineRecipe(grow);
  return recipe;
}

// The renderer adds weight times offset to every vertex; this does the same on the CPU.
function blendedPositions(part) {
  const blended = Float32Array.from(part.geometry.positions);
  part.geometry.morphTargets.forEach((target, index) => {
    for (let component = 0; component < blended.length; component++) blended[component] += part.morph.weights[index] * target.positions[component];
  });
  return blended;
}

test('a growing vine reaches further along its curve as the grow track plays, and seeking equals playing', () => {
  const scene = createAgentScene(vineRecipe());
  const [part] = scene.parts;
  const reach = () => bounds({ positions: blendedPositions(part) }).maximum[0];
  const played = [0, 0.25, 0.5, 0.75, 1].map(time => {
    scene.sampleMotion(time);
    return reach();
  });
  played.reduce((previous, current) => {
    assert.ok(current > previous, `the vine grew from ${previous} to ${current}`);
    return current;
  });
  assert.ok(played[0] < 0.3, `a bare root at the start (${played[0]})`);
  const whole = curve([[0, 0, 0], [2, 0, 0], [4, 1, 0], [6, 1, 0]], { kind: 'poly', radius: 0.02, radialSegments: 4 });
  assert.ok(Math.abs(bounds(whole).maximum[0] - played.at(-1)) < 0.02, `the whole curve at the end (${played.at(-1)})`);
  scene.sampleMotion(1);
  scene.sampleMotion(0.5);
  const sought = reach();
  const fresh = createAgentScene(vineRecipe());
  fresh.sampleMotion(0.5);
  assert.equal(sought, bounds({ positions: blendedPositions(fresh.parts[0]) }).maximum[0], 'seeking equals a fresh sample');
  assert.equal(sought, played[2]);
});

test('a growing vine without a track shows the curve grown to grow.value, and the targets are deterministic', () => {
  const grown = createAgentScene(still({ name: 'vine', steps: 4 }));
  const [part] = grown.parts;
  assert.deepEqual(part.geometry.morphTargetNames, ['vine#1', 'vine#2', 'vine#3', 'vine#4']);
  assert.deepEqual(Array.from(part.morph.weights), [0, 0, 0, 1]);
  const half = createAgentScene(still({ name: 'vine', steps: 4, value: 0.5 }));
  assert.deepEqual(Array.from(half.parts[0].morph.weights), [0, 1, 0, 0]);
  const again = createAgentScene(still({ name: 'vine', steps: 4 }));
  assert.deepEqual(part.geometry.morphTargets[2].positions, again.parts[0].geometry.morphTargets[2].positions);
});

test('grow rejects bad fields with what to pass', () => {
  for (const [grow, pattern] of [
    [{ steps: 4 }, /grow\.name/],
    [{ name: 'vine', steps: 1 }, /grow\.steps/],
    [{ name: 'vine', steps: 99 }, /grow\.steps/],
    [{ name: 'vine', value: 2 }, /grow\.value/],
    [{ name: 'vine', bogus: 1 }, /Unknown grow field: bogus/],
  ]) assert.throws(() => createAgentScene(still(grow)), pattern);
});

test('a closed curve grows to full length with no trim, and its first and last states share a vertex count', () => {
  const closedShapes = {
    polygon: { points: [[0, 0, 0], [2, 0, 0], [2, 2, 0], [0, 2, 0]], closed: true },
    star: { shape: { type: 'star' } },
  };
  for (const [name, spline] of Object.entries(closedShapes)) {
    const { geometries, ...recipe } = still();
    geometries[0] = { id: 'vine', type: 'curve', ...spline, radius: 0.02, radialSegments: 4, grow: { name: 'vine', steps: 4 } };
    const geometry = createAgentScene({ ...recipe, geometries }).parts[0].geometry;
    const vertexCount = geometry.positions.length;
    assert.equal(geometry.morphTargets.length, 4, name);
    assert.equal(geometry.morphTargets[0].positions.length, vertexCount, `${name} first state`);
    assert.equal(geometry.morphTargets[3].positions.length, vertexCount, `${name} last state`);
  }
});

// Cox-de Boor recursion straight from the definition of the basis functions, to check the implementation against.
function referenceBasis(index, degree, parameter, knots, lastDomainKnot) {
  if (degree === 0) {
    const inSpan = parameter >= knots[index] && parameter < knots[index + 1];
    const atEnd = parameter === lastDomainKnot && knots[index] < knots[index + 1] && knots[index + 1] === lastDomainKnot;
    return inSpan || atEnd ? 1 : 0;
  }
  const leftSpan = knots[index + degree] - knots[index];
  const rightSpan = knots[index + degree + 1] - knots[index + 1];
  const left = leftSpan === 0 ? 0 : (parameter - knots[index]) / leftSpan * referenceBasis(index, degree - 1, parameter, knots, lastDomainKnot);
  const right = rightSpan === 0 ? 0 : (knots[index + degree + 1] - parameter) / rightSpan * referenceBasis(index + 1, degree - 1, parameter, knots, lastDomainKnot);
  return left + right;
}

function referenceNurbs(controls, weights, order, knots, parameter) {
  const sum = [0, 0, 0];
  let weightSum = 0;
  controls.forEach((control, index) => {
    const weighted = referenceBasis(index, order - 1, parameter, knots, knots[controls.length]) * weights[index];
    weightSum += weighted;
    for (let axis = 0; axis < 3; axis++) sum[axis] += weighted * control[axis];
  });
  return sum.map(value => value / weightSum);
}

function assertMatchesReference(controls, weights, options, resolution) {
  const { order, knots } = options;
  const samples = sampleNurbs(controls, weights, false, resolution, options);
  const start = knots[order - 1];
  const end = knots[controls.length];
  const spans = controls.length - 1;
  samples.points.forEach((point, index) => {
    const expected = referenceNurbs(controls, weights, order, knots, start + (end - start) * index / (spans * resolution));
    point.forEach((value, axis) => assert.ok(Math.abs(value - expected[axis]) < 1e-9, `sample ${index} axis ${axis}: ${value} against ${expected[axis]}`));
  });
}

test('NURBS order and knots match a reference evaluation', () => {
  const controls = [[0, 0, 0], [1, 2, 0], [2, -1, 1], [3, 2, 0], [4, 0, 2]];
  const weights = [1, 2, 0.5, 1.5, 1];
  assertMatchesReference(controls, weights, { order: 3, knots: [0, 0, 0, 1, 2, 3, 3, 3] }, 7);
  assertMatchesReference(controls, weights, { order: 5, knots: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] }, 7);
  assertMatchesReference(controls, weights, { order: 4, knots: [0, 1, 2, 3, 4, 5, 6, 7, 8] }, 7);
  assertMatchesReference(controls, weights, { order: 3, knots: [0, 0, 0, 0.5, 0.5, 2, 2, 2] }, 7);
});

test('a quadratic rational NURBS draws an exact quarter circle', () => {
  const controls = [[1, 0, 0], [1, 1, 0], [0, 1, 0]];
  const samples = sampleNurbs(controls, [1, Math.SQRT1_2, 1], false, 16, { order: 3 });
  for (const [x, y] of samples.points) assert.ok(Math.abs(Math.hypot(x, y) - 1) < 1e-12);
});

test('knotMode uniform leaves the end points, clamped keeps them, order 2 is the control polygon', () => {
  const controls = [[0, 0, 0], [1, 1, 0], [2, 0, 0], [3, 1, 0], [4, 0, 0]];
  const weights = Array(5).fill(1);
  const clamped = sampleNurbs(controls, weights, false, 4, { order: 4, knotMode: 'clamped' });
  const uniform = sampleNurbs(controls, weights, false, 4, { order: 4, knotMode: 'uniform' });
  assert.deepEqual(clamped.points[0], controls[0]);
  assert.notDeepEqual(uniform.points[0], controls[0]);
  assert.deepEqual(sampleNurbs(controls, weights, false, 4), clamped);
  const linear = sampleNurbs(controls, weights, false, 4, { order: 2 });
  assert.deepEqual(linear.points[4], controls[1]);
});

test('curve takes order, knots and knotMode and rejects bad ones with what to pass', () => {
  const points = [[0, 0, 0], [1, 1, 0], [2, 0, 0], [3, 1, 0]];
  const wire = { radius: 0.05, resolution: 4 };
  const base = curve(points, { ...wire, kind: 'nurbs' });
  assert.equal(hashOf(curve(points, { ...wire, kind: 'nurbs', order: 4 })), hashOf(base));
  assert.equal(hashOf(curve(points, { ...wire, kind: 'nurbs', knots: [0, 0, 0, 0, 1, 1, 1, 1] })), hashOf(base));
  assert.notEqual(hashOf(curve(points, { ...wire, kind: 'nurbs', order: 3 })), hashOf(base));
  assert.equal(hashOf(curve(points, { ...wire, kind: 'nurbs', order: 3 })), hashOf(curve(points, { ...wire, kind: 'nurbs', order: 3 })));
  const nurbs = { ...wire, kind: 'nurbs' };
  const cases = [
    [{ ...nurbs, order: 1 }, /order must be an integer from 2 to 4/],
    [{ ...nurbs, order: 5 }, /order must be an integer from 2 to 4/],
    [{ ...nurbs, order: 2.5 }, /order must be an integer/],
    [{ ...nurbs, knots: [0, 0, 0, 1, 1, 1] }, /needs 8 values \(points 4 plus order 4\)/],
    [{ ...nurbs, knots: [0, 0, 0, 0, 1, 0.5, 1, 1] }, /must not decrease; knots\[5\]/],
    [{ ...nurbs, knots: [0, 0, 0, 0, 0, 0, 0, 0] }, /must rise between knots\[3\] and knots\[4\]/],
    [{ ...nurbs, knots: [0, 0, 0, 0, 1, 1, 1, NaN] }, /knots must be a finite float/],
    [{ ...nurbs, knots: [0, 0, 0, 0, 1, 1, 1, 1], knotMode: 'uniform' }, /exclude each other/],
    [{ ...nurbs, knotMode: 'periodic' }, /knotMode must be one of clamped, uniform/],
    [{ ...nurbs, closed: true, knotMode: 'uniform' }, /apply to open splines/],
    [{ ...wire, order: 3 }, /order is not allowed here/],
    [{ ...wire, knotMode: 'uniform' }, /knotMode is not allowed here/],
  ];
  for (const [options, pattern] of cases) assert.throws(() => curve(points, options), pattern);
});

test('NURBS knots repeated beyond the order or leaving a zero-width last span are rejected, valid ones sample finite', () => {
  const points = [[0, 0, 0], [1, 1, 0], [2, 0, 0], [3, 1, 0], [4, 0, 0]];
  const wire = { radius: 0.05, resolution: 4, kind: 'nurbs', order: 4 };
  assert.throws(() => curve(points, { ...wire, knots: [0, 0, 0, 0, 1, 1, 1, 1, 1] }), /repeats 1 more than order 4 times/);
  assert.throws(() => curve(points, { ...wire, knots: [0, 0, 0, 0, 1, 1, 1, 1, 2] }), /knots\[5\] must be above knots\[4\]/);
  const weights = Array(points.length).fill(1);
  for (const knots of [[0, 0, 0, 0, 1, 2, 2, 2, 2], [0, 0, 0, 1, 1, 2, 2, 2, 2]]) {
    const sampled = sampleNurbs(points, weights, false, 6, { order: 4, knots });
    for (const point of sampled.points) for (const value of point) assert.ok(Number.isFinite(value));
  }
});

test('a recipe curve geometry takes order and knots', () => {
  const recipe = geometry => ({
    format: 'crucible.scene',
    schemaVersion: 1,
    materials: [{ id: 'metal', color: [0.6, 0.6, 0.65] }],
    geometries: [{ id: 'g', type: 'curve', kind: 'nurbs', points: [[0, 0, 0], [1, 1, 0], [2, 0, 0], [3, 1, 0]], radius: 0.05, ...geometry }],
    nodes: [{ id: 'n', geometry: 'g', material: 'metal' }],
  });
  const cubic = createAgentScene(recipe({}));
  const quadratic = createAgentScene(recipe({ order: 3, knots: [0, 0, 0, 1, 2, 2, 2] }));
  assert.ok(quadratic.parts[0].geometry.indices.length > 0);
  assert.notEqual(hashOf(quadratic.parts[0].geometry), hashOf(cubic.parts[0].geometry));
  assert.throws(() => createAgentScene(recipe({ order: 9 })), /order must be an integer from 2 to 4/);
});