refactor: add typed decoder plugin runtime

This commit is contained in:
sjg
2026-08-01 13:02:54 +02:00
parent 6a83e2e90a
commit 508cf2ba87
23 changed files with 438 additions and 324 deletions
@@ -23,10 +23,13 @@ test("AIS entry forwards positioned vessels with normalized metadata", async ()
Map,
console,
});
const runtime = await readFile(new URL("../../assets/web/generated/plugin-runtime.js", import.meta.url), "utf8");
const source = await readFile(new URL("../../assets/web/generated/ais.js", import.meta.url), "utf8");
new vm.Script(runtime).runInContext(context);
new vm.Script(source).runInContext(context);
window.onServerAis({ mmsi: 261000001, lat: 54.5, lon: 18.5, channel: "A", ts_ms: Date.now() });
window.trxPluginRuntime.dispatch("ais", { mmsi: 261000001, lat: 54.5, lon: 18.5, channel: "A", ts_ms: Date.now() });
assert.equal(window.onServerAis, undefined);
assert.equal(forwarded.length, 1);
assert.equal(forwarded[0].mmsi, 261000001);
assert.equal(forwarded[0].rig_id, null);
@@ -25,11 +25,14 @@ test("APRS entry normalizes positioned packets without remote symbol assets", as
Reflect,
console,
});
const runtime = await readFile(new URL("../../assets/web/generated/plugin-runtime.js", import.meta.url), "utf8");
const source = await readFile(new URL("../../assets/web/generated/aprs.js", import.meta.url), "utf8");
assert.equal(source.includes("raw.githubusercontent.com"), false);
new vm.Script(runtime).runInContext(context);
new vm.Script(source).runInContext(context);
window.onServerAprs({ src_call: "SP0ABC", dest_call: "APRS", crc_ok: true, lat: 54.5, lon: 18.5, symbol_table: "/", symbol_code: ">" });
window.trxPluginRuntime.dispatch("aprs", { src_call: "SP0ABC", dest_call: "APRS", crc_ok: true, lat: 54.5, lon: 18.5, symbol_table: "/", symbol_code: ">" });
assert.equal(window.onServerAprs, undefined);
assert.equal(forwarded.length, 1);
assert.equal(forwarded[0][0], "SP0ABC");
assert.equal(forwarded[0][1], 54.5);
@@ -21,11 +21,13 @@ test("HF APRS entry uses shared typed normalization and local symbols", async ()
Reflect,
console,
});
const runtime = await readFile(new URL("../../assets/web/generated/plugin-runtime.js", import.meta.url), "utf8");
const source = await readFile(new URL("../../assets/web/generated/hf-aprs.js", import.meta.url), "utf8");
assert.equal(source.includes("raw.githubusercontent.com"), false);
new vm.Script(runtime).runInContext(context);
new vm.Script(source).runInContext(context);
window.onServerHfAprs({ src_call: "SP0ABC", dest_call: "APRS", crc_ok: true, symbol_table: "/", symbol_code: ">" });
assert.equal(typeof window.restoreHfAprsHistory, "function");
assert.equal(typeof window.pruneHfAprsHistoryView, "function");
window.trxPluginRuntime.dispatch("hf_aprs", { src_call: "SP0ABC", dest_call: "APRS", crc_ok: true, symbol_table: "/", symbol_code: ">" });
assert.equal(window.onServerHfAprs, undefined);
assert.equal(window.trxPluginRuntime.hasDecoder("hf_aprs"), true);
});
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
//
// SPDX-License-Identifier: GPL-2.0-or-later
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import vm from "node:vm";
test("plugin runtime queues lazy decoder traffic and flushes in order on registration", async () => {
const window = {};
const context = vm.createContext({ window, Map, Error });
const source = await readFile(new URL("../../assets/web/generated/plugin-runtime.js", import.meta.url), "utf8");
new vm.Script(source).runInContext(context);
assert.equal(window.trxPluginRuntime.dispatch("late", { sequence: 1 }), false);
assert.equal(window.trxPluginRuntime.restore("late", [{ sequence: 2 }]), false);
const received = [];
window.trxPluginRuntime.registerDecoder({
id: "late",
onMessage: (message) => { received.push(["message", message.sequence]); },
restore: (messages) => { received.push(["restore", messages[0].sequence]); },
});
assert.deepEqual(received, [["message", 1], ["restore", 2]]);
});
@@ -22,10 +22,13 @@ test("VDES entry normalizes and forwards positioned server messages", async () =
Array,
console,
});
const runtime = await readFile(new URL("../../assets/web/generated/plugin-runtime.js", import.meta.url), "utf8");
const source = await readFile(new URL("../../assets/web/generated/vdes.js", import.meta.url), "utf8");
new vm.Script(runtime).runInContext(context);
new vm.Script(source).runInContext(context);
window.onServerVdes({ callsign: "SP0ABC", lat: 54.5, lon: 18.5, bit_len: 120, ts_ms: Date.now() });
window.trxPluginRuntime.dispatch("vdes", { callsign: "SP0ABC", lat: 54.5, lon: 18.5, bit_len: 120, ts_ms: Date.now() });
assert.equal(window.onServerVdes, undefined);
assert.equal(forwarded.length, 1);
assert.equal(forwarded[0].callsign, "SP0ABC");
assert.equal(forwarded[0].rig_id, null);