fix(training): keep practice laps in training stats after Reset data

Resetting a running Open Practice deleted its results, the Timing view
recreated an empty result at once, and the next save rebuilt that
session's training laps from nothing - wiping every earlier lap from
the drivers' stats. Reset now syncs first and records resultsResetAt;
laps from before it are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
larssand
2026-10-03 23:13:58 +02:00
parent 8b63dd2372
commit 3cc4fa0845
10 changed files with 141 additions and 7 deletions

View File

@@ -151,6 +151,34 @@ test("ledger sync retains archived laps and rebuilds current session laps", () =
assert.equal(synced.markers.length, 1);
});
test("resetting a running Open Practice keeps its earlier laps in the training stats", () => {
const lap = (lapMs, timestamp) => ({ driverId: "driver_1", competitorKey: "driver_1", lapMs, timestamp, validLap: true });
const state = {
sessions: [{ id: "op", name: "Open Practice", type: "open_practice", eventId: "event_1" }],
events: [{ id: "event_1", name: "Club" }],
resultsBySession: { op: { passings: [lap(20000, 1000), lap(19500, 2000), lap(19000, 3000)] } },
};
let training = syncTrainingLedger({}, state, { uid: createUid() });
assert.equal(training.ledger.length, 3);
// "Reset data" on Timing: the session keeps running, its results are cleared.
state.sessions[0].resultsResetAt = 5000;
delete state.resultsBySession.op;
training = syncTrainingLedger(training, state, { uid: createUid() });
assert.equal(training.ledger.length, 3, "nothing is lost right after the reset");
// The next day's first lap brings the session back into the results. This used to wipe
// every lap from before the reset out of the training stats.
state.resultsBySession.op = { passings: [lap(18500, 90000)] };
training = syncTrainingLedger(training, state, { uid: createUid() });
assert.deepEqual(training.ledger.map((entry) => entry.lapMs), [20000, 19500, 19000, 18500]);
// A lap removed from the current results (judge invalidated it) still leaves the stats.
state.resultsBySession.op.passings[0].validLap = false;
training = syncTrainingLedger(training, state, { uid: createUid() });
assert.deepEqual(training.ledger.map((entry) => entry.lapMs), [20000, 19500, 19000]);
});
test("driver statistics remain isolated and aggregate days and sessions", () => {
const firstDay = new Date(2026, 0, 10, 12, 0, 0).getTime();
const secondDay = new Date(2026, 0, 11, 12, 0, 0).getTime();