Files
efitenandClaude Opus 4.8 a58f17a5c2 fix: resume capture on heard packet after standstill auto-pause (v1.3.3)
After the 5-min standstill auto-pause, capture only resumed when a GPS
watchPosition callback reported movement. On a backgrounded / screen-off
PWA those callbacks stall, so a drive that resumed after a stop logged
nothing until the app was foregrounded — a whole drive silently lost
(reported for BE-BOC-myst, 2026-07-08 near Bree).

Wake-on-packet: a heard packet now also advances the idle gate via the new
pure captureDecision(), so movement resumes capture on the first packet
from a moved position even when the GPS callback cadence dropped. A packet
still at the parked anchor stays paused. Capture stays fully decoupled from
the network (IndexedDB queue), so this is a capture fix, not an upload one.

Bump SW cache v10→v11 for the frontend change.

Closes #9

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 16:22:07 +02:00

90 lines
4.3 KiB
JavaScript

// Pure idle-gate logic: from a stream of GPS fixes, decide moving vs stationary so
// capture/upload pauses when parked and resumes on movement. Run: node --test
import { test } from 'node:test';
import assert from 'node:assert';
import { updateMotion, captureDecision, distanceM, DEFAULT_RADIUS_M, DEFAULT_DWELL_MS } from '../src/motion.js';
const MIN = 60 * 1000;
test('distanceM is ~0 for the same point and ~111 m per 0.001° latitude', () => {
const a = { lat: 50.85, lon: 4.5 };
assert.ok(distanceM(a, a) < 1);
assert.ok(distanceM(a, { lat: 50.851, lon: 4.5 }) > 100);
});
test('first fix seeds an active (not paused) gate anchored at the fix', () => {
const s = updateMotion(null, { lat: 50.85, lon: 4.5 }, 0);
assert.strictEqual(s.paused, false);
assert.deepStrictEqual(s.anchor, { lat: 50.85, lon: 4.5 });
});
test('continuous movement (each fix > radius) stays active and re-anchors', () => {
let s = updateMotion(null, { lat: 50.850, lon: 4.5 }, 0);
s = updateMotion(s, { lat: 50.853, lon: 4.5 }, 5 * MIN); // ~330 m
assert.strictEqual(s.paused, false);
assert.strictEqual(s.anchor.lat, 50.853); // re-anchored
s = updateMotion(s, { lat: 50.856, lon: 4.5 }, 20 * MIN); // moved again
assert.strictEqual(s.paused, false); // no pause despite >dwell elapsed overall
});
test('staying within the radius past the dwell time pauses', () => {
let s = updateMotion(null, { lat: 50.85, lon: 4.5 }, 0);
s = updateMotion(s, { lat: 50.8501, lon: 4.5 }, 3 * MIN); // ~11 m jitter, within radius
assert.strictEqual(s.paused, false); // not long enough yet
s = updateMotion(s, { lat: 50.8501, lon: 4.5 }, 16 * MIN);
assert.strictEqual(s.paused, true);
});
test('moving again after a pause resumes and re-anchors', () => {
let s = updateMotion(null, { lat: 50.85, lon: 4.5 }, 0);
s = updateMotion(s, { lat: 50.85, lon: 4.5 }, 16 * MIN);
assert.strictEqual(s.paused, true);
s = updateMotion(s, { lat: 50.86, lon: 4.5 }, 17 * MIN); // ~1.1 km away
assert.strictEqual(s.paused, false);
assert.strictEqual(s.anchor.lat, 50.86);
});
test('within-radius jitter does not reset the dwell timer', () => {
let s = updateMotion(null, { lat: 50.85, lon: 4.5 }, 0);
s = updateMotion(s, { lat: 50.8500, lon: 4.5001 }, 5 * MIN);
s = updateMotion(s, { lat: 50.8501, lon: 4.4999 }, 10 * MIN);
assert.strictEqual(s.anchorTime, 0); // anchor time held from arrival
s = updateMotion(s, { lat: 50.8500, lon: 4.5000 }, 16 * MIN);
assert.strictEqual(s.paused, true); // paused at arrival+16min, jitter didn't reset it
});
// --- captureDecision: wake-on-packet gate --------------------------------------
// A heard packet advances the idle gate too, so movement resumes capture even when
// the GPS callback cadence stalled (backgrounded PWA). Returns { motion, capture }.
test('heard packet while paused, with a moved fix, resumes and captures', () => {
let s = updateMotion(null, { lat: 50.85, lon: 4.5 }, 0);
s = updateMotion(s, { lat: 50.85, lon: 4.5 }, 16 * MIN);
assert.strictEqual(s.paused, true); // parked → paused
const d = captureDecision(s, { lat: 50.86, lon: 4.5 }, 17 * MIN); // packet from ~1.1 km away
assert.strictEqual(d.capture, true);
assert.strictEqual(d.motion.paused, false);
assert.strictEqual(d.motion.anchor.lat, 50.86); // re-anchored to where we now are
});
test('heard packet while paused, with a stale parked fix, stays paused and does not capture', () => {
let s = updateMotion(null, { lat: 50.85, lon: 4.5 }, 0);
s = updateMotion(s, { lat: 50.85, lon: 4.5 }, 16 * MIN);
assert.strictEqual(s.paused, true);
const d = captureDecision(s, { lat: 50.8501, lon: 4.5 }, 17 * MIN); // ~11 m jitter, still parked
assert.strictEqual(d.capture, false);
assert.strictEqual(d.motion.paused, true);
});
test('heard packet while active captures', () => {
const s = updateMotion(null, { lat: 50.85, lon: 4.5 }, 0); // active
const d = captureDecision(s, { lat: 50.8502, lon: 4.5 }, 1 * MIN);
assert.strictEqual(d.capture, true);
assert.strictEqual(d.motion.paused, false);
});
test('default thresholds are 75 m / 5 min', () => {
assert.strictEqual(DEFAULT_RADIUS_M, 75);
assert.strictEqual(DEFAULT_DWELL_MS, 5 * 60 * 1000);
});