Files
corescope-rx/test/background.test.mjs
efiten e3d7d96b54 web: never stamp a reception with a fix from before the page went to the background
A hidden page gets no GPS updates while Web Bluetooth can still deliver packets, so a
reception was stamped with the last position from before the page was hidden. Fixes now
carry their receive time; a hidden page only uses a fix up to 30 s old (receptions, RF
samples, pings). After a minute or more in the background while connected, Home says how
long and how many receptions were saved or not saved.
2026-11-11 21:27:19 +01:00

37 lines
1.7 KiB
JavaScript

// A hidden page must not stamp receptions with a position from before it went to the
// background, and afterwards it says how long it was away and what was lost.
// Run: node --test
import { test } from 'node:test';
import assert from 'node:assert';
import { usableFix, startSpell, endSpell, spellText, FIX_STALE_MS, BG_NOTICE_MIN_MS } from '../src/background.js';
const fix = (at) => ({ lat: 51, lon: 4, acc_m: 5, at });
test('a visible page uses any fix, a hidden one only a fresh fix', () => {
const now = 10_000_000;
const old = fix(now - FIX_STALE_MS - 1);
assert.equal(usableFix(old, now, false), old);
assert.equal(usableFix(old, now, true), null);
const fresh = fix(now - FIX_STALE_MS);
assert.equal(usableFix(fresh, now, true), fresh);
assert.equal(usableFix(null, now, false), null);
const noTime = { lat: 51, lon: 4, acc_m: 5 };
assert.equal(usableFix(noTime, now, true), noTime);
});
test('a background spell is reported only when it lasted a minute or more', () => {
const s = startSpell(0);
s.saved = 2; s.skipped = 5;
assert.equal(endSpell(s, BG_NOTICE_MIN_MS - 1), null);
assert.deepStrictEqual(endSpell(s, 12 * 60000), { ms: 12 * 60000, saved: 2, skipped: 5 });
assert.equal(endSpell(null, 99999999), null);
});
test('the notice says how long and what was not saved', () => {
assert.equal(spellText({ ms: 12 * 60000, saved: 1, skipped: 34 }),
'The app was in the background for 12 min: 1 reception saved, 34 receptions not saved because the browser paused GPS.'
+ ' Keep CoreDrive RX on screen while mapping. Tap to dismiss.');
assert.equal(spellText({ ms: 90000, saved: 0, skipped: 0 }),
'The app was in the background for 2 min: 0 receptions saved. Keep CoreDrive RX on screen while mapping. Tap to dismiss.');
});