Files
VibemakerSandbox/app/test/motion-reference.test.js
T

147 lines
4.5 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { JointMotionReference } from "../src/platform/policy/JointMotionReference.js";
import { ObservationPipeline } from "../src/platform/policy/ObservationPipeline.js";
import { validatePackage } from "../src/platform/loader/SchemaValidator.js";
const names = ["b", "a"];
const header = {
schema: 1,
dtype: "float32-le",
layout: "positions_then_velocities",
frames: 3,
step_dt: 0.02,
joint_names: names,
data: "motion.f32",
};
const samples = [1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 15, 16];
function bytes(values = samples) {
const out = new ArrayBuffer(values.length * 4),
view = new DataView(out);
values.forEach((v, i) => view.setFloat32(i * 4, v, true));
return out;
}
function reference() {
return new JointMotionReference(header, bytes(), names, 0.02);
}
function read(ref) {
const out = new Float32Array(4);
ref.write(out, 0);
return [...out];
}
test("joint reference preserves command order, is simulation-clocked, clamps without wrapping and resets", () => {
const ref = reference();
ref.sample(20);
assert.deepEqual(read(ref), [1, 2, 11, 12]);
ref.sample(20.02);
assert.deepEqual(read(ref), [3, 4, 13, 14]);
ref.sample(20.02);
assert.equal(ref.frame, 1);
ref.sample(99);
assert.deepEqual(read(ref), [5, 6, 15, 16]);
ref.reset();
ref.sample(200);
assert.deepEqual(read(ref), [1, 2, 11, 12]);
assert.throws(() => ref.sample(NaN), /time/);
});
test("reference layout, clock, joint order, finiteness and exact binary length fail closed", () => {
for (const spec of [
{ ...header, dtype: "float64" },
{ ...header, frames: 0 },
{ ...header, step_dt: 0.01 },
{ ...header, joint_names: ["a", "b"] },
{ ...header, layout: "unknown" },
])
assert.throws(
() => new JointMotionReference(spec, bytes(), names, 0.02),
/Motion reference/,
);
assert.throws(
() => new JointMotionReference(header, bytes().slice(0, 8), names, 0.02),
/sample count/,
);
assert.throws(
() =>
new JointMotionReference(
header,
bytes(samples.map((v, i) => (i ? v : NaN))),
names,
0.02,
),
/non-finite/,
);
});
test("motion observations are reference inputs only; diagnostic reads do not advance or write physics/targets", () => {
const ref = reference(),
targets = new Map([["b", 100]]);
const ctx = {
reference: ref,
targets,
config: { joints: { groups: { policy: names } } },
};
const obs = new ObservationPipeline(
{ obs: { terms: [{ type: "motion_command", group: "policy" }] } },
ctx,
);
ref.sample(5);
ref.sample(5.02);
assert.deepEqual([...obs.write().obs], [3, 4, 13, 14]);
assert.deepEqual([...obs.write({ advance: false }).obs], [3, 4, 13, 14]);
assert.equal(ref.frame, 1);
assert.equal(targets.get("b"), 100);
assert.throws(
() =>
new ObservationPipeline(
{ obs: { terms: [{ type: "motion_command", group: "policy" }] } },
{ ...ctx, reference: null },
),
/reference/,
);
});
test("shipped RP1 motion contracts use actual 126D student terms and per-skill pose/clock", () => {
const config = JSON.parse(
readFileSync(new URL("../public/robots/rp1/robot.json", import.meta.url)),
);
validatePackage(config);
for (const skill of config.skills.filter((s) => s.kind === "episodic")) {
assert.equal(
config.input.bindings.find((b) => b.action === `skill:${skill.id}`).label,
skill.id === "dance" ? "Dance" : "Wave",
);
assert.deepEqual(
skill.observations.obs.terms.map((t) => t.type),
[
"motion_command",
"gyro",
"projected_gravity",
"joint_pos_rel",
"joint_vel",
"last_action",
],
);
assert.equal(skill.actions[0].scale, 0.25);
assert.equal(Object.keys(skill.defaults).length, 24);
const base = new URL("../public/robots/rp1/", import.meta.url);
const spec = JSON.parse(readFileSync(new URL(skill.reference, base)));
const buffer = readFileSync(new URL(spec.data, base));
const ref = new JointMotionReference(
spec,
buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength,
),
config.joints.groups.policy,
0.02,
);
assert.ok(skill.duration <= ref.frames * ref.dt);
const invalid = structuredClone(config);
delete invalid.skills.find((s) => s.id === skill.id).reference;
assert.throws(() => validatePackage(invalid), /reference/);
}
});