import test from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createZip } from "../src/platform/export/zip.js"; import { MICRODUCK_JOINT_NAMES, buildDefaultHardwareProfile, buildMicroduckManifest, buildUnoqBundleFiles, } from "../src/robots/microduck/hardware-export.js"; test("createZip produces a CRC-valid archive readable by Python zipfile", () => { const bytes = createZip([ { path: "microduck_unoq/hello.txt", data: "hello microduck\n" }, { path: "microduck_unoq/bin/run.sh", data: "#!/bin/sh\necho ok\n", executable: true, }, { path: "microduck_unoq/raw.bin", data: new Uint8Array([0, 1, 2, 255, 128, 42]), }, ]); const dir = mkdtempSync(join(tmpdir(), "microduck-zip-test-")); const zipPath = join(dir, "bundle.zip"); try { writeFileSync(zipPath, bytes); const pyOut = execFileSync( "python3", [ "-c", [ "import sys, zipfile, json", "zf = zipfile.ZipFile(sys.argv[1], 'r')", "assert zf.testzip() is None", "names = sorted(zf.namelist())", "text = zf.read('microduck_unoq/hello.txt').decode()", "raw = list(zf.read('microduck_unoq/raw.bin'))", "mode = (zf.getinfo('microduck_unoq/bin/run.sh').external_attr >> 16) & 0o777", "print(json.dumps({'names': names, 'text': text, 'raw': raw, 'mode': mode}))", ].join("; "), zipPath, ], { encoding: "utf8" }, ); const parsed = JSON.parse(pyOut); assert.deepEqual(parsed.names, [ "microduck_unoq/bin/run.sh", "microduck_unoq/hello.txt", "microduck_unoq/raw.bin", ]); assert.equal(parsed.text, "hello microduck\n"); assert.deepEqual(parsed.raw, [0, 1, 2, 255, 128, 42]); assert.equal(parsed.mode, 0o755); } finally { rmSync(dir, { recursive: true, force: true }); } }); test("buildMicroduckManifest supports legs, optional rollers, and custom script/policy slots", () => { const legsManifest = buildMicroduckManifest({ selectedSkills: [ "walk", "stand", "sitstand", "kickL", "kickR", "groundpick", "roll", ], includeRollers: false, }); assert.equal(legsManifest.obs_size, 61); assert.equal(legsManifest.action_size, 14); assert.equal(legsManifest.command_size, 13); assert.equal(legsManifest.joint_names.length, 14); assert.equal(legsManifest.variants.rollers.enabled, false); assert.equal(Object.keys(legsManifest.skills).length, 7); const fullManifest = buildMicroduckManifest({ selectedSkills: [ "walk", "stand", "sitstand", "kickL", "kickR", "groundpick", "roll", "drive", "crouch", ], includeRollers: true, customMove: { type: "onnx", slot: "trick", name: "backflip", obsLen: 61, actionLen: 14, durationS: 2.5, bytes: new Uint8Array([1, 2, 3]), }, }); assert.equal(fullManifest.variants.rollers.enabled, true); assert.equal(fullManifest.skills.drive.path, "policies/drive.onnx"); assert.equal(fullManifest.skills.roll.path, "policies/custom_roll.onnx"); assert.equal(fullManifest.skills.roll.duration_s, 2.5); assert.deepEqual(fullManifest.custom_move, { name: "backflip", slot: "trick", type: "onnx", }); // Dimension mismatch must be rejected immediately assert.throws( () => buildMicroduckManifest({ selectedSkills: ["walk", "stand"], customMove: { type: "onnx", slot: "walk", name: "bad", obsLen: 63, actionLen: 18, bytes: new Uint8Array([1]), }, }), /61.*14|dimension|incompatible/i, ); }); test("buildDefaultHardwareProfile matches 14-joint Feetech + QMI8658 schema", () => { const profile = buildDefaultHardwareProfile(); assert.equal(profile.servo.ticks_per_rev, 4096); assert.equal(profile.servo.neutral_tick, 2048); assert.equal(profile.imu.i2c_addr, "0x6A"); assert.deepEqual(Object.keys(profile.joints), MICRODUCK_JOINT_NAMES); const ids = Object.values(profile.joints).map((j) => j.id); assert.equal(new Set(ids).size, 14); }); test("buildUnoqBundleFiles verifies referenced policies exist and packages cleanly", () => { const manifest = buildMicroduckManifest({ selectedSkills: ["walk", "stand"], customMove: { type: "script", slot: "script", name: "wave", script: { durationS: 1.0, loop: true, keyframes: [{ t: 0, cmd: [0.1, 0, 0] }], }, }, }); const profile = buildDefaultHardwareProfile(); const dummyOnnx = new Uint8Array([0x08, 0x09, 0x12, 0x04]); const entries = buildUnoqBundleFiles({ manifest, policyBlobs: { walk: dummyOnnx, stand: dummyOnnx, }, profile, customScript: { name: "wave", durationS: 1.0, loop: true, keyframes: [{ t: 0, cmd: [0.1, 0, 0] }], }, }); const paths = entries.map((e) => e.path); assert.ok(paths.includes("microduck_unoq/config/robot_manifest.json")); assert.ok(paths.includes("microduck_unoq/config/hardware_profile.json")); assert.ok(paths.includes("microduck_unoq/config/custom_move_script.json")); assert.ok(paths.includes("microduck_unoq/policies/walk.onnx")); assert.ok(paths.includes("microduck_unoq/policies/stand.onnx")); assert.throws( () => buildUnoqBundleFiles({ manifest, policyBlobs: { walk: dummyOnnx }, // stand missing profile, }), /stand/i, ); });