This commit is contained in:
larssand
2026-08-24 21:28:38 +02:00
parent 355a1ad3b0
commit 3aeb77a042
13 changed files with 642 additions and 8 deletions

View File

@@ -7,8 +7,10 @@ const {
buildPublicTrainingProfile,
findPublicTrainingShare,
hasPublicTrainingShare,
getPublicBestDurationResult,
normalizePublicTrainingSince,
normalizePublicTrainingWindow,
splitPublicTrainingStints,
} = require("../server/public_training_profile.js");
const DRIVER_ONE_TOKEN = "driver-one-token-12345678901234567890";
@@ -109,3 +111,17 @@ test("public timer query normalization bounds age and window values", () => {
assert.equal(normalizePublicTrainingWindow("-100"), 0);
assert.equal(normalizePublicTrainingWindow(String(25 * 60 * 60 * 1000)), 24 * 60 * 60 * 1000);
});
test("public profile duration result includes the lap started before five minutes", () => {
const laps = Array.from({ length: 16 }, (_, index) => ({
sessionId: "practice_1",
timestamp: (index + 1) * 18_000,
lapMs: 18_000,
}));
laps.push({ sessionId: "practice_1", timestamp: 314_123, lapMs: 26_123 });
const result = getPublicBestDurationResult(splitPublicTrainingStints(laps), 300_000);
assert.equal(result.lapCount, 17);
assert.equal(result.totalMs, 314_123);
assert.equal(result.averageLapMs, 18_478);
});

View File

@@ -2,12 +2,16 @@ import test from "node:test";
import assert from "node:assert/strict";
import {
buildDriverActivePracticeStats,
buildDriverTrainingStats,
buildTrainingRecommendation,
getBestConsecutiveTrainingLaps,
getBestTrainingDurationResult,
getDriverTrainingLaps,
getNextTrainingMilestone,
getTrainingGoalProgress,
normalizeTrainingState,
splitTrainingStints,
syncTrainingLedger,
} from "../src/training_logic.js";
@@ -105,6 +109,97 @@ test("driver statistics remain isolated and aggregate days and sessions", () =>
assert.equal(stats.dailySummaries[1].laps, 2);
});
test("active practice stats include only the selected driver's current valid laps", () => {
const startedAt = 10_000;
const state = {
activeSessionId: "practice_1",
sessions: [{ id: "practice_1", name: "Wednesday Open Practice", type: "open_practice", status: "running", startedAt }],
resultsBySession: {
practice_1: {
passings: [
{ driverId: "driver_1", timestamp: 9_000, lapMs: 15_000, validLap: true },
{ driverId: "driver_1", timestamp: 20_000, lapMs: 16_000, validLap: true },
{ driverId: "driver_1", timestamp: 37_000, lapMs: 17_000, validLap: true },
{ driverId: "driver_1", timestamp: 45_000, lapMs: 8_000, validLap: false },
{ driverId: "driver_2", timestamp: 54_000, lapMs: 16_500, validLap: true },
],
},
},
};
assert.deepEqual(buildDriverActivePracticeStats(state, "driver_1", 70_000), {
sessionId: "practice_1",
sessionName: "Wednesday Open Practice",
startedAt,
elapsedMs: 60_000,
laps: 2,
bestLapMs: 16_000,
averageLapMs: 16_500,
lastLapMs: 17_000,
});
});
test("active practice stats ignore races and stopped practice sessions", () => {
const base = { activeSessionId: "session_1", resultsBySession: {} };
assert.equal(buildDriverActivePracticeStats({ ...base, sessions: [{ id: "session_1", type: "final", status: "running" }] }, "driver_1"), null);
assert.equal(buildDriverActivePracticeStats({ ...base, sessions: [{ id: "session_1", type: "free_practice", status: "finished" }] }, "driver_1"), null);
});
test("training stints split after a break over two minutes or a session change", () => {
const laps = [
{ sessionId: "practice_1", timestamp: 18_000, lapMs: 18_000 },
{ sessionId: "practice_1", timestamp: 36_000, lapMs: 18_000 },
{ sessionId: "practice_1", timestamp: 200_001, lapMs: 164_001 },
{ sessionId: "practice_2", timestamp: 218_001, lapMs: 18_000 },
];
const stints = splitTrainingStints(laps);
assert.equal(stints.length, 3);
assert.equal(stints[0].laps.length, 2);
assert.equal(stints[1].laps.length, 1);
assert.equal(stints[1].laps[0].excludedFromPace, true);
assert.equal(stints[2].sessionId, "practice_2");
});
test("best consecutive laps never cross a stint boundary", () => {
const stints = splitTrainingStints([
{ sessionId: "practice_1", timestamp: 18_000, lapMs: 18_000 },
{ sessionId: "practice_1", timestamp: 35_000, lapMs: 17_000 },
{ sessionId: "practice_1", timestamp: 51_000, lapMs: 16_000 },
{ sessionId: "practice_1", timestamp: 66_000, lapMs: 15_000 },
{ sessionId: "practice_1", timestamp: 201_001, lapMs: 15_000 },
]);
const best = getBestConsecutiveTrainingLaps(stints, 3);
assert.equal(best.totalMs, 48_000);
assert.equal(best.averageLapMs, 16_000);
assert.equal(best.startedAt, 18_000);
});
test("best five-minute result counts a final lap started before the cutoff and reports its pace", () => {
const laps = Array.from({ length: 16 }, (_, index) => ({
sessionId: "practice_1",
timestamp: (index + 1) * 18_000,
lapMs: 18_000,
}));
laps.push({ sessionId: "practice_1", timestamp: 314_123, lapMs: 26_123 });
const result = getBestTrainingDurationResult(splitTrainingStints(laps), 300_000);
assert.equal(result.lapCount, 17);
assert.equal(result.totalMs, 314_123);
assert.equal(result.averageLapMs, 18_478);
assert.equal(result.startedAt, 0);
assert.equal(result.endedAt, 314_123);
});
test("a stint shorter than the target duration has no five-minute result", () => {
const stints = splitTrainingStints([
{ sessionId: "practice_1", timestamp: 18_000, lapMs: 18_000 },
{ sessionId: "practice_1", timestamp: 36_000, lapMs: 18_000 },
]);
assert.equal(getBestTrainingDurationResult(stints, 300_000), null);
});
test("recommendations select baseline, consistency, and progression", () => {
assert.equal(buildTrainingRecommendation({ totalLaps: 9 }).level, "baseline");
assert.deepEqual(