Files
JIANT/tests/install_test.py
Claude da1b1dd2c1 Copyright headers: name Juan Jiant on JIANT's own files, add him on the modified Felucca ones
Comment-only change (the built image is byte-identical: felucca.bin 583936 B, felucca.fwsc 684736 B, same SHA-256).

- 37 files written new for JIANT FM after the fork (no Felucca text in them: checked against ac6bfcbd by literal
  code lines, runs of consecutive lines, same-named functions and moves in their creating commit) carried Leo
  Kuroshita's copyright line copied from a template: it is now Juan Jiant's (SPDX GPL-3.0-only kept). Among them
  sing.c, eng_animal.c, eng_input.c, eng_string.c, pfx.c, seq_live.c, ui_engvis.c, ui_lens.c, ui_lz*.c, ui_being.c,
  ui_organic.c, ui_tech.c, ui_dx.c, ui_live.c, tools/lyrics/*.txt, gen_drum_maps.py, gen_px_font.py and 14 tests.
- 140 files that hold Felucca's code (Felucca 1.1.5.1 files changed since, imports of Felucca 1.5.x, the SLOOP-based
  song_main/song_play, and new files that carry copied Felucca helpers: drumx_voice.c dv_exp2/dv_kb, gen_lyrics.py's
  Huffman, test harness helpers) keep Leo's notice and add "Portions Copyright (C) 2026 Juan Ignacio Antonucci
  (Juan Jiant)". Files unchanged since the fork are left as they were.
- LICENSING.md and README.md name both copyright holders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-11-11 18:04:53 +00:00

324 lines
14 KiB
Python
Executable File

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
# Portions Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant)
"""tools/fm1_install.py against a simulated FM-1 (no hardware, no mido).
The fake device is the one in web/test_web.mjs: identity on the handshake,
then "device asks, host answers" reads of the logical image. Run from the repo root:
python3 tests/install_test.py
Also checks logical_image/product_of against web/fm1pkg.js (needs node and
build/felucca.fwsc, skipped otherwise)."""
import io
import queue
import shutil
import subprocess
import sys
import tempfile
import threading
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tools"))
import fm1_install as I # noqa: E402
I.DELAY.update(open=0, start=0.01, reply=0, loader=0.01, reboot=0.01, retry=0.02, poll=0.05, hs=0.1,
idle_check=0.4, idle_write=0.4, wait_loader=2, wait_reboot=2)
failed = 0
def ok(cond, what):
global failed
print(f"{what:<64} {'ok' if cond else 'FAIL'}")
failed += not cond
# ---------------------------------------------------------- simulated FM-1 ---
class FakeLink:
def __init__(self, dev, gen):
self.dev, self.gen, self.q, self.closed = dev, gen, queue.Queue(), False
@property
def lost(self):
return self.gen != self.dev.gen or not self.dev.connected
def send(self, pkt):
if self.lost:
return False
self.dev.sent.append(bytes(pkt))
self.dev.rx(bytes(pkt))
return True
def read(self, timeout):
try:
return self.q.get(timeout=max(timeout, 0))
except queue.Empty:
return None
def drain(self):
while not self.q.empty():
self.q.get_nowait()
def close(self):
self.closed = True
class FakeFM1:
"""a MIDI backend with one FM-1 on it"""
def __init__(self, image, identity="FM-1_015", name="FM-1", unplug_after=None, after_write="FM-1_900",
stall_after=None, bad_addr=None, loader_name="Felucca Update", after_name="Felucca"):
self.image, self.unplug_after, self.after_write = image, unplug_after, after_write
self.loader_name, self.after_name = loader_name, after_name
self.stall_after, self.bad_addr = stall_after, bad_addr
self.served = self.bad = self.upgrades = 0
self.sent, self.links, self.gen, self.lock = [], [], 0, threading.Lock()
self.boot(identity, name)
def boot(self, identity, name):
with self.lock:
self.gen += 1
self.identity, self.name, self.connected = identity, name, True
self.waiting, self.queue = None, []
def input_names(self):
return [self.name] if self.connected else []
output_names = input_names
def open(self, in_name, out_name):
if in_name != self.name or not self.connected:
raise IOError("no such port")
link = FakeLink(self, self.gen)
self.links.append(link)
return link
def tx(self, pkt):
for link in self.links:
if link.gen == self.gen and not link.closed:
link.q.put(bytes(pkt))
def rx(self, d):
if d == I.HS_QUERY:
t = self.identity.encode()
self.tx(b"\xF0" + I.pack7(bytes([0, 0x59, 0x11, 0, 0, 0]) + t + bytes(28 - len(t))) + b"\xF7")
elif d == I.UPGRADE:
self.upgrades += 1
first = self.bad_addr if self.bad_addr is not None else 0
self.queue = ([(first + k * 512, 512) for k in range(6)] + [(I.FINISH_WRITE, 8)]
if self.identity.startswith("ota-")
else [(0, 64), (0x40, 160), (0x1000, 512), (I.FINISH_CHECK, 8)])
self.next()
elif self.waiting:
u = I.unpack7(d[1:-1])
addr, n = self.waiting
fin = addr >= I.FINISH_CHECK
want = b"success\0" if fin else self.image[addr:addr + n]
if u[14:14 + len(want)] != want or len(u) != 15 + len(want):
self.bad += 1
self.waiting = None
self.served += 1
if self.unplug_after and self.served >= self.unplug_after:
self.connected = False
return
if self.stall_after and self.served >= self.stall_after:
return
if addr == I.FINISH_CHECK:
threading.Timer(0.05, self.boot, ("ota-FM-1_900", self.loader_name)).start()
elif addr == I.FINISH_WRITE:
threading.Timer(0.05, self.boot, (self.after_write, self.after_name)).start()
else:
self.next()
def next(self):
if not self.queue:
return
self.waiting = addr, n = self.queue.pop(0)
u = bytearray([0, 0x59, 0x30, 0, 0, 0, 0]) + addr.to_bytes(4, "little") + bytes([n & 0xFF, n >> 8, 0])
u.append(~sum(u[6:14]) & 0xFF)
self.tx(b"\xF0" + I.pack7(u) + b"\xF7")
# ----------------------------------------------------------------- helpers ---
def package(product="FM-1_900", marker=True, size=0x2000):
raw = bytearray((i * 7) & 0xFF for i in range(size + I.BLOCKS))
for i in range(I.BLOCKS):
raw[i * I.BLK + I.KEEP] = (ord(product[i]) + i + 1) & 0xFF if i < len(product) else 0x7D
raw[0x1800:0x1800 + 16] = I.LOADER_MARK if marker else bytes(16)
return bytes(raw)
TMP = Path(tempfile.mkdtemp(prefix="felucca-install-"))
def pkgfile(name, raw):
p = TMP / name
p.write_bytes(raw)
return str(p)
def cli(args, dev, answer=True):
out, err = io.StringIO(), io.StringIO()
old, sys.stderr = sys.stderr, err
try:
rc = I.main(args, backend=dev, out=out, ask=lambda _p: answer)
finally:
sys.stderr = old
return rc, out.getvalue(), err.getvalue()
# ------------------------------------------------------------------- tests ---
def wire():
data = bytes(range(256)) * 3
ok(I.unpack7(I.pack7(data))[:len(data)] == data, "pack7 / unpack7 round trip")
pkt = I.response(0x12345, b"\x01\x02\x03", fl=5)
u = I.unpack7(pkt[1:-1])
ok(u[:3] == b"\x00\x59\x30" and int.from_bytes(u[7:11], "little") == 0x12345 and u[14:17] == b"\x01\x02\x03"
and ~sum(u[6:17]) & 0xFF == u[17], "response: header, address, data, checksum")
raw = package("FM-1_906")
ok(I.product_of(raw) == "FM-1_906" and len(I.logical_image(raw)) == len(raw) - 20, "product_of / logical_image (synthetic)")
def installs():
raw = package()
image = I.logical_image(raw)
p = pkgfile("ok.fwsc", raw)
dev = FakeFM1(image)
rc, out, err = cli([p, "--yes"], dev)
ok(rc == 0 and dev.bad == 0 and dev.upgrades == 2 and dev.served == 11 and "done: the FM-1 runs FM-1_900" in out
and "100%" in out, f"install: running -> loader -> Felucca ({dev.served} reads)")
dev = FakeFM1(image, identity="ota-FM-1_900", name="Felucca Update")
rc, out, err = cli([p, "--yes"], dev)
ok(rc == 0 and dev.bad == 0 and dev.upgrades == 1 and "update mode" in out, "install: device already in update mode -> finishes the write")
dev = FakeFM1(image)
rc, out, err = cli([p], dev, answer=False)
ok(rc == 1 and dev.upgrades == 0 and "cancelled" in out, "install: answer no -> nothing sent but the handshake")
dev = FakeFM1(image, identity="FM-1_905", name="Felucca")
rc, out, err = cli(["--info"], dev)
ok(rc == 0 and "FM-1_905" in out and "running" in out and dev.upgrades == 0, "--info: identity of the connected FM-1")
dev = FakeFM1(image, identity="ota-FM-1_900", name="Felucca Update")
rc, out, err = cli(["--info"], dev)
ok(rc == 0 and "update loader" in out, "--info: device in update mode")
# (0.9.12) the USB names over the versions: "Felucca" / "Felucca Update" (Felucca), "JIANT FM" / "JIANT Update"
# (JIANT 0.7.0..0.9.11), "JIANT" / "JIANT Update" (0.9.12); what the device is comes from its identity, not the name
for name in ("FM-1", "JIANT", "JIANT FM", "Felucca", "2- JIANT", "MIDIIN2 (JIANT)"):
dev = FakeFM1(image, identity="FM-1_900", name=name, loader_name="JIANT Update", after_name="JIANT")
rc, out, err = cli([p, "--yes"], dev)
ok(rc == 0 and dev.bad == 0 and dev.upgrades == 2 and "done: the FM-1 runs FM-1_900" in out,
f"install: a port named {name!r} -> 'JIANT Update' -> 'JIANT'")
dev = FakeFM1(image, identity="ota-FM-1_900", name="JIANT Update", after_name="JIANT")
rc, out, err = cli(["--info"], dev)
ok(rc == 0 and "update loader" in out, "--info: 'JIANT Update' (ota-FM-1_900) is the update loader")
dev = FakeFM1(image, identity="FM-1_015", name="JIANT")
rc, out, err = cli(["--info"], dev)
ok(rc == 0 and "FM-1_015" in out and "update loader" not in out, "--info: a port named 'JIANT' answering FM-1_015 is that firmware")
dev = FakeFM1(image, identity="\x1b[2J\x1b]0;x\x07FM-1_905", name="Felucca")
rc, out, err = cli(["--info"], dev)
ok(rc == 0 and "FM-1_905" in out and not any(c in out for c in "\x1b\x07"),
"--info: control bytes in the device's identity are not printed")
def errors():
raw = package()
image = I.logical_image(raw)
p = pkgfile("ok.fwsc", raw)
rc, out, err = cli([p, "--yes"], FakeFM1(image, name="IAC Driver Bus 1"))
ok(rc == 3 and "not found" in err and "IAC Driver" in err, "no FM-1 port -> exit 3, lists the MIDI inputs")
dev = FakeFM1(image)
rc, out, err = cli([p, "--yes", "--port", "Felucca"], dev)
ok(rc == 3 and dev.upgrades == 0, "--port that matches nothing -> exit 3")
dev = FakeFM1(image, name="My Interface")
rc, out, err = cli(["--info", "--port", "my int"], dev)
ok(rc == 0 and "FM-1_015" in out, "--port picks a port the default match skips")
t0 = time.monotonic()
rc, out, err = cli([p, "--yes"], FakeFM1(image, unplug_after=2))
ok(rc == 4 and "disconnected" in err and "nothing was written" in err and time.monotonic() - t0 < 3,
"unplugged in step 1 -> exit 4 'lost', nothing written")
t0 = time.monotonic()
rc, out, err = cli([p, "--yes"], FakeFM1(image, identity="ota-FM-1_900", name="Felucca Update", unplug_after=3))
ok(rc == 4 and "run the install again" in err and time.monotonic() - t0 < 3, "unplugged during the write -> exit 4 at once")
rc, out, err = cli([p, "--yes"], FakeFM1(image, identity="ota-FM-1_900", name="Felucca Update", stall_after=3))
ok(rc == 4 and "stopped answering" in err, "loader stops answering -> exit 4 after the idle time")
rc, out, err = cli([p, "--yes"], FakeFM1(image, identity="ota-FM-1_900", name="Felucca Update", bad_addr=len(image)))
ok(rc == 4 and "outside the package" in err, "read past the package -> exit 4")
rc, out, err = cli([p, "--yes"], FakeFM1(image, after_write="FM-1_015"))
ok(rc == 6 and "reports FM-1_015" in err, "another identity after the restart -> exit 6")
dev = FakeFM1(image, identity="XY-9_001", name="usb-midi")
rc, out, err = cli([p, "--yes"], dev)
ok(rc == 6 and dev.upgrades == 0, "another model -> exit 6, nothing sent")
class Stuck(FakeFM1): # the loader never shows up
def boot(self, identity, name):
super().boot(identity, name)
if identity.startswith("ota-"):
self.connected = False
rc, out, err = cli([p, "--yes"], Stuck(image))
ok(rc == 5 and "loader did not appear" in err, "no loader after step 1 -> exit 5")
plain = pkgfile("plain.fwsc", package(marker=False))
dev = FakeFM1(image)
rc, out, err = cli([plain, "--yes"], dev)
ok(rc == 2 and "no Felucca update loader" in err and dev.sent == [], "package without the loader marker -> exit 2, no MIDI")
dev = FakeFM1(I.logical_image(package(marker=False)))
rc, out, err = cli([plain, "--yes", "--force"], dev)
ok(rc == 0 and dev.bad == 0, "... installs with --force")
rc, out, err = cli([pkgfile("short.fwsc", b"\0" * 100), "--yes"], FakeFM1(image))
ok(rc == 2 and "too short" in err, "not a package -> exit 2")
rc, out, err = cli([str(TMP / "missing.fwsc"), "--yes"], FakeFM1(image))
ok(rc == 2, "missing file -> exit 2")
def against_js():
clean = ROOT / "build/felucca.fwsc"
if not shutil.which("node") or not clean.exists():
print("logical image vs fm1pkg.js: skipped (needs node and build/felucca.fwsc)")
return
js = ("import { logicalImage, productOf } from %r; import { readFileSync } from 'node:fs';"
"const p = readFileSync(process.argv[1]); process.stderr.write(productOf(p));"
"process.stdout.write(logicalImage(p));") % str(ROOT / "web/fm1pkg.js")
r = subprocess.run(["node", "--input-type=module", "-e", js, str(clean)], capture_output=True, check=True)
raw = clean.read_bytes()
ok(I.logical_image(raw) == r.stdout and I.product_of(raw) == r.stderr.decode(),
f"logical_image / product_of == fm1pkg.js ({clean.name}, {I.product_of(raw)})")
ok(I.LOADER_MARK in raw, f"{clean.name} carries the Felucca loader marker")
def official():
"""#32: back to the official V15 from Felucca, without --force (only the unmodified file)"""
v15 = ROOT / "FM-1_v15.fwsc"
if not v15.exists():
print("official V15 restore: skipped (needs FM-1_v15.fwsc in the repo root)")
return
raw = v15.read_bytes()
dev = FakeFM1(I.logical_image(raw), identity="FM-1_900", name="Felucca", after_write="FM-1_015")
rc, out, err = cli([str(v15), "--yes"], dev)
ok(rc == 0 and dev.bad == 0 and "FM-1_015" in out and dev.identity == "FM-1_015",
"official V15 (FM-1.fwsc) from Felucca: installed without --force, back as FM-1_015")
bad = bytearray(raw)
bad[-1] ^= 1
dev = FakeFM1(I.logical_image(bytes(bad)), identity="FM-1_900", name="Felucca")
rc, out, err = cli([pkgfile("v15mod.fwsc", bytes(bad)), "--yes"], dev)
ok(rc == 2 and "official V15" in err and dev.sent == [], "a modified V15 is still refused (no MIDI)")
wire()
installs()
errors()
against_js()
official()
shutil.rmtree(TMP, ignore_errors=True)
print(f"INSTALL TESTS FAILED ({failed})" if failed else "install tests passed")
sys.exit(1 if failed else 0)