Decoder: streaming APT.Decoder (coherent PLL or envelope demod, sync A+B tracking loop with coasting and timing-jump re-lock), telemetry analysis, Q-gated impulse (FM click) repair. GUI with fast / realtime decode, Listen, live input, telemetry table, markers. Encoder: APT.Encoder with test impairments (noise, clicks, clock offset, fading, line-timing jump); encoder dialog that feeds the decoder. Tools: offline decode/encode, quality survey, stub-DOM GUI smoke test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
4.9 KiB
JavaScript
112 lines
4.9 KiB
JavaScript
/*
|
|
* node tools/gui-smoke.js - loads the GUI scripts against a stub DOM and runs
|
|
* encoder dialog -> fast decode with repair. No browser needed; catches
|
|
* wiring and runtime errors, not layout.
|
|
*/
|
|
'use strict';
|
|
const fs = require('fs'), path = require('path'), vm = require('vm');
|
|
const dir = path.join(__dirname, '..');
|
|
const html = fs.readFileSync(path.join(dir, 'index.html'), 'utf8');
|
|
|
|
const els = {};
|
|
function El(id, tag) {
|
|
const e = {
|
|
id, tagName: (tag || 'div').toUpperCase(), style: {}, dataset: {}, children: [],
|
|
textContent: '', innerHTML: '', value: '', checked: false, disabled: false, files: [],
|
|
width: 300, height: 150, clientWidth: 1000, clientHeight: 600, scrollTop: 0,
|
|
classList: { add() {}, remove() {}, toggle() {} },
|
|
addEventListener(t, f) { (this['_' + t] = this['_' + t] || []).push(f); },
|
|
appendChild(c) { this.children.push(c); return c; },
|
|
querySelector() { return El('q'); },
|
|
getBoundingClientRect() { return { left: 0, top: 0, width: 1000, height: 600 }; },
|
|
showModal() { this.open = true; }, close() { this.open = false; },
|
|
getContext() { return Ctx(this); },
|
|
toBlob(cb) { cb({}); }, click() {},
|
|
};
|
|
return e;
|
|
}
|
|
function Ctx(c) {
|
|
return new Proxy({
|
|
createImageData: (w, h) => ({ width: w, height: h, data: new Uint8ClampedArray(w * h * 4) }),
|
|
getImageData: (x, y, w, h) => ({ data: new Uint8ClampedArray(w * h * 4).fill(128) }),
|
|
measureText: () => ({ width: 10 }),
|
|
}, { get: (t, k) => (k in t ? t[k] : () => {}) });
|
|
}
|
|
/* ids, tags and default values from the markup */
|
|
for (const m of html.matchAll(/<(\w+)([^>]*?)\sid="([^"]+)"([^>]*)>/g)) {
|
|
const e = El(m[3], m[1]);
|
|
const attrs = m[2] + m[4];
|
|
const v = attrs.match(/value="([^"]*)"/);
|
|
if (v) e.value = v[1];
|
|
if (/\schecked/.test(attrs)) e.checked = true;
|
|
els[m[3]] = e;
|
|
}
|
|
/* select defaults: first or selected option */
|
|
for (const m of html.matchAll(/<select id="([^"]+)"[^>]*>([\s\S]*?)<\/select>/g)) {
|
|
const opts = [...m[2].matchAll(/<option(?: value="([^"]*)")?([^>]*)>([^<]*)/g)];
|
|
const sel = opts.find((o) => /selected/.test(o[2])) || opts[0];
|
|
if (sel) els[m[1]].value = sel[1] !== undefined ? sel[1] : sel[3];
|
|
}
|
|
const radios = [...html.matchAll(/<input type="radio" name="encSrc" value="(\w+)"( checked)?/g)].map((m) => ({ value: m[1], checked: !!m[2], addEventListener() {} }));
|
|
|
|
const timers = [];
|
|
const ctx = {
|
|
console, Math, Date, JSON, Promise, Float32Array, Float64Array, Uint8Array, Uint8ClampedArray, Int16Array, ArrayBuffer, DataView, Map, Proxy, Blob: class { constructor(p) { this.parts = p; } },
|
|
URL: { createObjectURL: () => 'blob:x', revokeObjectURL() {} },
|
|
performance: { now: () => Date.now() },
|
|
setTimeout: (f) => timers.push(f), setInterval: () => 0, requestAnimationFrame: (f) => { ctx._raf = f; },
|
|
navigator: { mediaDevices: { enumerateDevices: async () => [] } },
|
|
alert: (m) => { throw new Error('alert: ' + m); },
|
|
document: {
|
|
getElementById: (id) => els[id] || (els[id] = El(id)),
|
|
createElement: (t) => El('new', t),
|
|
querySelector: (q) => {
|
|
const m = q.match(/value=(\w+)/);
|
|
return m ? radios.find((r) => r.value === m[1]) : radios.find((r) => r.checked);
|
|
},
|
|
querySelectorAll: () => radios,
|
|
},
|
|
addEventListener() {},
|
|
};
|
|
ctx.window = ctx;
|
|
ctx.globalThis = ctx;
|
|
vm.createContext(ctx);
|
|
for (const f of ['wav.js', 'apt.js', 'encoder.js', 'app.js', 'encoder-ui.js'])
|
|
vm.runInContext(fs.readFileSync(path.join(dir, f), 'utf8'), ctx, { filename: f });
|
|
|
|
const drain = async (max) => {
|
|
for (let i = 0; i < max && timers.length; i++) {
|
|
timers.shift()();
|
|
if (i % 20 === 0) { ctx._raf && ctx._raf(); await null; }
|
|
}
|
|
ctx._raf && ctx._raf();
|
|
};
|
|
|
|
(async () => {
|
|
/* encoder: pattern, 120 lines, 11025 Hz, noise + clicks + jump */
|
|
els.encLines.value = '120';
|
|
els.encRate.value = '11025';
|
|
els.encNoiseOn.checked = true; els.encSnr.value = '15';
|
|
els.encClicksOn.checked = true; els.encClicks.value = '60';
|
|
els.encJumpOn.checked = true; els.encJump.value = '70';
|
|
els.btnEncoder.onclick();
|
|
console.log('preview:', els.encInfo.textContent, '| canvas', els.encCanvas.width, 'x', els.encCanvas.height);
|
|
els.encDecode.onclick();
|
|
for (let k = 0; k < 50; k++) await drain(5000);
|
|
console.log('source:', els.srcName.textContent, '| status:', els.stMode.textContent, '| lines', els.stLines.textContent,
|
|
'| sync', els.stSync.textContent, '| clock', els.stClock.textContent, '| repaired', els.stRepair.textContent);
|
|
/* toggles */
|
|
els.repair.value = 'strong'; els.repair.onchange({ target: els.repair });
|
|
els.showRepairs.checked = true; els.showRepairs.onchange({ target: els.showRepairs });
|
|
ctx._raf();
|
|
console.log('strong repaired:', els.stRepair.textContent);
|
|
els.btnSave.onclick();
|
|
els.repair.value = 'off'; els.repair.onchange({ target: els.repair }); ctx._raf();
|
|
console.log('repair off:', els.stRepair.textContent);
|
|
/* save path */
|
|
els.encSave.onclick();
|
|
for (let k = 0; k < 20; k++) await drain(5000);
|
|
console.log('frames table has rows:', /<tr/.test(els.frames.innerHTML));
|
|
console.log('OK');
|
|
})().catch((e) => { console.error('FAIL', e); process.exit(1); });
|