forked from Blendman974/faceclaw
Fix unit tests
This commit is contained in:
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 };
|
const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 };
|
||||||
|
|
||||||
function compass(width = 576, height = 260) {
|
function compass(width = 576, height = 260) {
|
||||||
const settings = new Map();
|
let listener, window;
|
||||||
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;
|
|
||||||
class RecordingImage extends graphics.GrayImage {
|
class RecordingImage extends graphics.GrayImage {
|
||||||
texts = [];
|
texts = [];
|
||||||
drawText(font, x, y, text, value) {
|
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/geometry': { screenCenterInViewportX: () => width / 2 - 32 },
|
||||||
'../../ui/shell/in-process-window': {
|
'../../ui/shell/in-process-window': {
|
||||||
YieldAtRootLayer: class { constructor(layer) { return layer; } },
|
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 } },
|
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
|
||||||
'../../native/location-permissions': { hasLocationPermission: () => false },
|
'../../native/location-permissions': { hasLocationPermission: () => false },
|
||||||
'./calibration': calibration,
|
'./calibration': calibration,
|
||||||
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
|
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
|
||||||
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
|
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
|
||||||
'./debug': debug,
|
|
||||||
}, { setInterval: () => 1, clearInterval() {} });
|
}, { setInterval: () => 1, clearInterval() {} });
|
||||||
const ctx = { stack: { getBaseSize: () => ({ width, height }), pop: () => ++pops } };
|
const ctx = { stack: { getBaseSize: () => ({ width, height }), pop() {} } };
|
||||||
const open = () => app.createCompassAppWindow({ onClosed() {} }); open();
|
app.createCompassAppWindow({ onClosed() {} });
|
||||||
return { open, debug, settings, menu: () => window.menuItems(),
|
return { menu: () => window.menuItems(),
|
||||||
toggle: () => window.menuItems().find((item) => item.label.startsWith('Debug information:')).onSelect(ctx),
|
|
||||||
paint: () => window.baseLayer.paint(ctx),
|
paint: () => window.baseLayer.paint(ctx),
|
||||||
heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }),
|
heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }) };
|
||||||
renders: () => renders, pops: () => pops };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test('debug toggle defaults off, repaints, closes menu and survives reopening', () => {
|
const calibrationText = (image) => image.texts.find((t) => t.text.startsWith('Calibration:'));
|
||||||
const app = compass(); app.heading(diagnostics);
|
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;
|
||||||
assert.equal(app.debug.isCompassDebugEnabled(), false);
|
|
||||||
assert.ok(!app.paint().texts.some((t) => t.text.startsWith('Mag:')));
|
test('calibration readiness is always shown and the debug toggle is gone', () => {
|
||||||
const before = app.renders(); app.toggle();
|
const app = compass();
|
||||||
assert.equal(app.debug.isCompassDebugEnabled(), true);
|
assert.ok(!app.menu().some((item) => item.label.startsWith('Debug information')));
|
||||||
assert.ok(app.renders() > before && app.pops() === 1);
|
assert.equal(calibrationText(app.paint()).text, 'Calibration: --');
|
||||||
app.open(); assert.equal(app.debug.isCompassDebugEnabled(), true);
|
app.heading(diagnostics);
|
||||||
app.toggle(); assert.equal(app.debug.isCompassDebugEnabled(), false);
|
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', () => {
|
test('calibration dots track firmware magnetic accuracy and never go stale', () => {
|
||||||
for (const width of [240, 320, 576, 640]) {
|
const app = compass();
|
||||||
const app = compass(width); app.toggle(); app.heading(diagnostics);
|
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 image = app.paint();
|
||||||
const heading = image.texts.find((t) => t.text === '359° N');
|
const label = calibrationText(image);
|
||||||
for (const text of ['Mag: 3/3', 'Anom: 2', 'Src: RV']) {
|
assert.ok(label.x >= 0 && label.y >= 0, `${width}x${height}`);
|
||||||
const label = image.texts.find((t) => t.text === text);
|
assert.ok(label.x + label.width <= width && label.y + label.height <= image.height, `${width}x${height}`);
|
||||||
assert.ok(label, text);
|
for (const other of image.texts.filter((t) => t !== label)) {
|
||||||
assert.ok(label.x > heading.x + heading.width);
|
assert.ok(!overlaps(label, other), `${width}x${height}: overlaps "${other.text}"`);
|
||||||
assert.ok(label.x + label.width <= width && label.y + label.height <= image.height);
|
|
||||||
}
|
}
|
||||||
const status = image.texts.find((t) => t.text === 'Magnetic heading');
|
if (process.env.COMPASS_PREVIEW && width === 576 && height === 260) {
|
||||||
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) {
|
|
||||||
fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([
|
fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([
|
||||||
Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels),
|
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}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
@@ -47,7 +47,6 @@ const compass = load('app/apps/compass/compass-app.ts', {
|
|||||||
'./calibration': calibration,
|
'./calibration': calibration,
|
||||||
'./calibration-layer': {},
|
'./calibration-layer': {},
|
||||||
'./compass-rose': rose,
|
'./compass-rose': rose,
|
||||||
'./debug': { compassDebugLines: () => [], isCompassDebugEnabled: () => false, setCompassDebugEnabled: () => {} },
|
|
||||||
'./declination': {},
|
'./declination': {},
|
||||||
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) },
|
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) },
|
||||||
}, `
|
}, `
|
||||||
|
|||||||
Reference in new issue
Block a user