Fix unit tests

This commit is contained in:
James Babcock committed 2026-09-25 22:13:02 -07:00
1 parent 2f88b108a8
commit efafcb0f0c
2 files changed
+40 -48

No files matched your search

@@ -24,12 +24,7 @@ const small = font(18), large = font(32);
const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 };
function compass(width = 576, height = 260) {
const settings = new Map();
const debug = load('app/apps/compass/debug.ts', { '../../native/settings-store': {
getBooleanSetting: (key, fallback) => settings.get(key) ?? fallback,
setBooleanSetting: (key, value) => settings.set(key, value),
} });
let listener, window, renders = 0, pops = 0;
let listener, window;
class RecordingImage extends graphics.GrayImage {
texts = [];
drawText(font, x, y, text, value) {
@@ -56,64 +51,62 @@ function compass(width = 576, height = 260) {
'../../ui/shell/geometry': { screenCenterInViewportX: () => width / 2 - 32 },
'../../ui/shell/in-process-window': {
YieldAtRootLayer: class { constructor(layer) { return layer; } },
createInProcessWindow: (options) => { window = options; return { requestRender: () => ++renders }; },
createInProcessWindow: (options) => { window = options; return { requestRender() {} }; },
},
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
'../../native/location-permissions': { hasLocationPermission: () => false },
'./calibration': calibration,
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
'./debug': debug,
}, { setInterval: () => 1, clearInterval() {} });
const ctx = { stack: { getBaseSize: () => ({ width, height }), pop: () => ++pops } };
const open = () => app.createCompassAppWindow({ onClosed() {} }); open();
return { open, debug, settings, menu: () => window.menuItems(),
toggle: () => window.menuItems().find((item) => item.label.startsWith('Debug information:')).onSelect(ctx),
const ctx = { stack: { getBaseSize: () => ({ width, height }), pop() {} } };
app.createCompassAppWindow({ onClosed() {} });
return { menu: () => window.menuItems(),
paint: () => window.baseLayer.paint(ctx),
heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }),
renders: () => renders, pops: () => pops };
heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }) };
}
test('debug toggle defaults off, repaints, closes menu and survives reopening', () => {
const app = compass(); app.heading(diagnostics);
assert.equal(app.debug.isCompassDebugEnabled(), false);
assert.ok(!app.paint().texts.some((t) => t.text.startsWith('Mag:')));
const before = app.renders(); app.toggle();
assert.equal(app.debug.isCompassDebugEnabled(), true);
assert.ok(app.renders() > before && app.pops() === 1);
app.open(); assert.equal(app.debug.isCompassDebugEnabled(), true);
app.toggle(); assert.equal(app.debug.isCompassDebugEnabled(), false);
const calibrationText = (image) => image.texts.find((t) => t.text.startsWith('Calibration:'));
const overlaps = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
test('calibration readiness is always shown and the debug toggle is gone', () => {
const app = compass();
assert.ok(!app.menu().some((item) => item.label.startsWith('Debug information')));
assert.equal(calibrationText(app.paint()).text, 'Calibration: --');
app.heading(diagnostics);
assert.equal(calibrationText(app.paint()).text, 'Calibration: ●●●');
assert.ok(!app.paint().texts.some((t) => /^(Mag|Anom|Src):/.test(t.text)));
});
test('diagnostics sit to the right of the heading without overlap or clipping', () => {
for (const width of [240, 320, 576, 640]) {
const app = compass(width); app.toggle(); app.heading(diagnostics);
test('calibration dots track firmware magnetic accuracy and never go stale', () => {
const app = compass();
for (const [accuracy, dots] of [[0, '○○○'], [1, '●○○'], [2, '●●○'], [3, '●●●'], [4, '●●●'], [-1, '--']]) {
app.heading({ ...diagnostics, magneticAccuracy: accuracy });
assert.equal(calibrationText(app.paint()).text, `Calibration: ${dots}`, `accuracy ${accuracy}`);
}
app.heading(diagnostics);
// Legacy samples (no 0x80 flag) and missing diagnostics must replace the old value.
app.heading({ ...diagnostics, flags: 0 });
assert.equal(calibrationText(app.paint()).text, 'Calibration: --');
app.heading(diagnostics);
app.heading(undefined);
assert.equal(calibrationText(app.paint()).text, 'Calibration: --');
});
test('calibration readout fits the viewport without overlapping the heading or status', () => {
for (const [width, height] of [[240, 260], [320, 260], [576, 260], [640, 260], [576, 288], [576, 140]]) {
const app = compass(width, height); app.heading({ ...diagnostics, magneticAccuracy: 2 });
const image = app.paint();
const heading = image.texts.find((t) => t.text === '359° N');
for (const text of ['Mag: 3/3', 'Anom: 2', 'Src: RV']) {
const label = image.texts.find((t) => t.text === text);
assert.ok(label, text);
assert.ok(label.x > heading.x + heading.width);
assert.ok(label.x + label.width <= width && label.y + label.height <= image.height);
const label = calibrationText(image);
assert.ok(label.x >= 0 && label.y >= 0, `${width}x${height}`);
assert.ok(label.x + label.width <= width && label.y + label.height <= image.height, `${width}x${height}`);
for (const other of image.texts.filter((t) => t !== label)) {
assert.ok(!overlaps(label, other), `${width}x${height}: overlaps "${other.text}"`);
}
const status = image.texts.find((t) => t.text === 'Magnetic heading');
assert.ok(image.texts.filter((t) => t.text.startsWith('Src:')).every((t) => t.y + t.height < status.y));
if (process.env.COMPASS_PREVIEW && width === 576) {
if (process.env.COMPASS_PREVIEW && width === 576 && height === 260) {
fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([
Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels),
]));
}
}
});
test('legacy and missing samples replace diagnostics instead of leaving stale values', () => {
const app = compass(); app.toggle(); app.heading(diagnostics);
assert.ok(app.paint().texts.some((t) => t.text === 'Mag: 3/3'));
app.heading(undefined);
assert.ok(app.paint().texts.some((t) => t.text === 'unavailable'));
assert.ok(!app.paint().texts.some((t) => t.text === 'Mag: 3/3'));
assert.deepEqual(Array.from(app.debug.compassDebugLines({ ...diagnostics, flags: 0 })), ['Sample data', 'unavailable']);
for (const [source, name] of [[1, 'GRV'], [2, 'GMRV'], [3, 'RV']]) {
assert.equal(app.debug.compassDebugLines({ ...diagnostics, orientationSource: source })[2], `Src: ${name}`);
}
});
-1
View File
@@ -47,7 +47,6 @@ const compass = load('app/apps/compass/compass-app.ts', {
'./calibration': calibration,
'./calibration-layer': {},
'./compass-rose': rose,
'./debug': { compassDebugLines: () => [], isCompassDebugEnabled: () => false, setCompassDebugEnabled: () => {} },
'./declination': {},
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) },
}, `