Files
JIANT/web/test_backup.mjs
T
kurogedelicandChanceTheMaker 727f272015 Felucca 1.0 source
Big New Features. Engines: FM6 (6-operator FM, replaces DIGITAL; old sounds
convert), SLICE in the standard build with manual slices, DRUM, NOISE; PHYS
membranes and sympathetic strings. 13 engines in one display order.
Sequencer: piano roll, drum grid, songs (patterns A-D), motion recording,
per-step chance, divisions to 4 bars, ARP REPEAT and beat LED; chord keys.
Effects: FX hold layer with a harmonizer, spring reverb, modulation matrix.
Quick layers, undo, on-device naming, new UI (Inter Tight, Fukiai icons,
8 palettes). USB audio input; TRS MIDI in, clock, bend, sustain. Web: full
backup and restore, sample recording and trim, return to the official
firmware. Build: font, icon, keycap, palette and FM6 patch generators;
release folder with the licence texts. Licences: LICENSES/ (Inter Tight
OFL, Fukiai MIT, DaisySP MIT, Rings MIT, msfa Apache-2.0, JieLi SDK
Apache-2.0). Removed: the bitmap font and icon atlas, their generators.

Co-authored-by: ChanceTheMaker <471816+ChanceTheMaker@users.noreply.github.com>
2026-10-05 23:16:53 +09:00

80 lines
5.5 KiB
JavaScript

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
// fm1backup.js against a simulated device: 7-bit packing, CRC, manifest checks, a full capture,
// validation before the first write, restore order (live music last) and abort on a failed chunk.
import { BACKUP_IDS, BACKUP_CMD, bkU32, bkR32, bkPack, bkUnpack, bkCrc, bkManifest, readBackup, captureBackup, restoreBackup } from "./fm1backup.js";
let fails = 0;
const ok = (c, what) => { console.log(`${what.padEnd(72)} ${c ? "ok" : "FAIL"}`); if (!c) fails++; };
const throws = (f) => { try { f(); return false; } catch { return true; } };
const athrows = async (f) => { try { await f(); return false; } catch { return true; } };
const rnd = (n, seed) => Uint8Array.from({ length: n }, (_, i) => (i * 131 + seed * 17 + (i >> 3)) & 255);
ok(bkR32(bkU32(0xdeadbeef)) === 0xdeadbeef && bkCrc(new TextEncoder().encode("123456789")) === 0xcbf43926, "backup: numbers and CRC-32");
const b = rnd(1000, 1);
ok(bkUnpack(bkPack(b), b.length).every((v, i) => v === b[i]), "backup: 7-bit pack / unpack round trip");
ok(throws(() => bkUnpack([...bkPack(b.subarray(0, 14)), 0], 14)), "backup: trailing bytes refused");
/* a device: objects by id, sample slots, the order of writes */
function device(objs, opt = {}) {
const d = { objs: new Map(objs), log: [], staged: null };
d.request = async ([cmd, a]) => {
if (cmd === BACKUP_CMD.LIST) {
const out = [1, 0, BACKUP_IDS.length];
for (const id of BACKUP_IDS) { const v = d.objs.get(id) || new Uint8Array(0); out.push(id, ...bkU32(v.length), ...bkU32(v.length ? bkCrc(v) : 0)); }
return out;
}
if (cmd === BACKUP_CMD.GET) {
const id = a[0], off = bkR32(a, 1), n = a[6] | a[7] << 7, v = d.objs.get(id);
if (opt.changeOnGet === id && off === 0) v[0] ^= 1;
return [id, 0, ...bkU32(off), n & 127, n >> 7, ...bkPack(v.subarray(off, off + n))];
}
if (cmd === BACKUP_CMD.PUT) {
const [op, id] = a;
if (op === 0) { d.staged = { id, size: bkR32(a, 2), crc: bkR32(a, 7), bytes: [] }; return [op, id, 0]; }
if (op === 1) {
if (opt.failChunk === id) return [op, id, 2];
const off = bkR32(a, 2), p = a.slice(7), n = Math.min(256, d.staged.size - off);
d.staged.bytes.push(...bkUnpack(p, n)); return [op, id, 0];
}
if (op === 2) { const s = Uint8Array.from(d.staged.bytes); const rc = bkCrc(s) === d.staged.crc ? 0 : 2; if (!rc) { d.objs.set(id, s); d.log.push(id); } return [op, id, rc]; }
if (op === 3) { d.log.push(`abort ${id}`); return [op, id, 0]; }
}
if (cmd === 11 || cmd === 12 || cmd === 13 || cmd === 14) { if (cmd === 13 || cmd === 14) d.log.push(32 + a[0]); return [a[0], 0]; }
throw new Error(`unexpected ${cmd}`);
};
return d;
}
const objs = [[0, rnd(3388, 2)], [1, rnd(1200, 3)], [2, rnd(3388, 4)], [6, rnd(3080, 5)]];
const dev = device(objs);
const file = await captureBackup(dev.request, "TEST");
ok(file.objects.length === BACKUP_IDS.length && file.objects[0].size === 3388 && file.objects[3].size === 0, "backup: capture lists every object, empty ones as 0");
ok(readBackup(JSON.stringify(file)).objects[2].bytes.every((v, i) => v === objs[2][1][i]), "backup: capture -> file -> bytes round trip");
ok(await athrows(() => captureBackup(device(objs.map(([i, v]) => [i, v.slice()]), { changeOnGet: 2 }).request, "TEST")), "backup: a device that changes during capture fails the capture");
const bad = JSON.parse(JSON.stringify(file)); bad.objects[2].crc ^= 1;
ok(throws(() => readBackup(bad)), "backup: a damaged file is refused");
const noRun = JSON.parse(JSON.stringify(file)); noRun.objects[0] = { ...noRun.objects[0], size: 0, crc: 0, data: "" };
ok(throws(() => readBackup(noRun)), "backup: a file without the current music is refused");
ok(throws(() => bkManifest([1, 0, 3])), "backup: a short manifest is refused");
const target = device([]);
const damaged = JSON.parse(JSON.stringify(file)); damaged.objects[6].crc ^= 1;
ok(await athrows(() => restoreBackup(target.request, damaged)) && target.log.length === 0, "backup: restore validates every byte before the first write");
await restoreBackup(target.request, file);
const order = target.log.filter((x) => typeof x === "number");
ok(order.at(-1) === 0 && order.at(-2) === 1 && order.indexOf(2) < order.indexOf(1), "backup: restore order: projects, banks, samples, settings, live music last");
ok([0, 1, 2, 6].every((id) => target.objs.get(id).every((v, i) => v === objs.find((o) => o[0] === id)[1][i])), "backup: restored objects equal the source");
{ /* the FM6 patch bank (id 8) and the archives of firmware before it (11 objects, no id 8) */
ok(BACKUP_IDS.includes(8) && file.objects.some((o) => o.id === 8), "backup: the FM6 bank (id 8) is part of the archive");
const old = JSON.parse(JSON.stringify(file)); old.objects = old.objects.filter((o) => o.id !== 8);
ok(readBackup(old).objects.length === 11, "backup: an archive of the 11 objects before FM6 still reads");
const odd = JSON.parse(JSON.stringify(file)); odd.objects = odd.objects.filter((o) => o.id !== 33);
ok(throws(() => readBackup(odd)), "backup: an archive missing another object is refused");
}
const failing = device([], { failChunk: 2 });
ok(await athrows(() => restoreBackup(failing.request, file)) && failing.log.includes("abort 2") && !failing.log.includes(0), "backup: a refused chunk aborts that object and stops before the live music");
console.log(fails ? `BACKUP WEB TESTS FAILED (${fails})` : "backup web tests passed");
process.exit(fails ? 1 : 0);