212 lines
9.5 KiB
JavaScript
212 lines
9.5 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createRequire } from "node:module";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const {
|
|
buildPublicTrainingProfile,
|
|
findPublicTrainingShare,
|
|
hasPublicTrainingShare,
|
|
getPublicBestDurationResult,
|
|
normalizePublicTrainingSince,
|
|
normalizePublicTrainingWindow,
|
|
splitPublicTrainingStints,
|
|
} = require("../server/public_training_profile.js");
|
|
|
|
const DRIVER_ONE_TOKEN = "driver-one-token-12345678901234567890";
|
|
const DRIVER_TWO_TOKEN = "driver-two-token-12345678901234567890";
|
|
const DISABLED_TOKEN = "disabled-token-123456789012345678901";
|
|
|
|
function buildState(now = Date.now()) {
|
|
return {
|
|
drivers: [
|
|
{ id: "driver_1", profileId: "profile_1", name: "Driver One", brand: "Associated 2WD", classId: "class_1", transponder: "secret-transponder-one" },
|
|
{ id: "driver_1_4wd", profileId: "profile_1", name: "Driver One", brand: "Associated 4WD", classId: "class_2", transponder: "secret-transponder-four" },
|
|
{ id: "driver_2", profileId: "profile_2", name: "Driver Two", brand: "Xray", classId: "class_1", transponder: "secret-transponder-two" },
|
|
],
|
|
classes: [
|
|
{ id: "class_1", name: "2WD" },
|
|
{ id: "class_2", name: "4WD" },
|
|
],
|
|
settings: {
|
|
clubName: "Test Club",
|
|
clubTagline: "Practice",
|
|
logoDataUrl: "javascript:alert(1)",
|
|
wsUrl: "ws://secret-decoder:9000",
|
|
},
|
|
training: {
|
|
shares: [
|
|
{ driverId: "driver_1", token: DRIVER_ONE_TOKEN, enabled: true },
|
|
{ driverId: "driver_2", token: DRIVER_TWO_TOKEN, enabled: true },
|
|
{ driverId: "driver_1", token: DISABLED_TOKEN, enabled: false },
|
|
],
|
|
goals: [
|
|
{ driverId: "driver_1", title: "Bronze", reward: "Badge", targetLaps: 5 },
|
|
{ driverId: "driver_2", title: "Other goal", reward: "Other reward", targetLaps: 10 },
|
|
],
|
|
ledger: [
|
|
{ driverId: "driver_1", sessionId: "free", timestamp: now - 25 * 60000, lapMs: 19000 },
|
|
{ driverId: "driver_1", sessionId: "free", timestamp: now - 120000, lapMs: 18000 },
|
|
{ driverId: "driver_1", sessionId: "free", timestamp: now - 12000, lapMs: 16500 },
|
|
{ driverId: "driver_1", sessionId: "free", timestamp: now - 4000, lapMs: 16000 },
|
|
{ driverId: "driver_1", sessionId: "free", timestamp: now - 2000, lapMs: 500 },
|
|
{ driverId: "driver_1_4wd", sessionId: "free", timestamp: now - 11000, lapMs: 14500 },
|
|
{ driverId: "driver_1_4wd", sessionId: "free", timestamp: now - 5000, lapMs: 14000 },
|
|
{ driverId: "driver_2", sessionId: "free", timestamp: now - 3000, lapMs: 12000 },
|
|
],
|
|
},
|
|
resultsBySession: {
|
|
free: {
|
|
splits: [
|
|
{ driverId: "driver_1", splitMs: 7200 },
|
|
{ driverId: "driver_1", splitMs: 7000 },
|
|
{ driverId: "driver_1_4wd", splitMs: 6400 },
|
|
{ driverId: "driver_2", splitMs: 6100 },
|
|
],
|
|
pitStops: [
|
|
{ driverId: "driver_1", pitStopMs: 12500, transponder: "secret-transponder-one" },
|
|
{ driverId: "driver_1_4wd", pitStopMs: 11000, transponder: "secret-transponder-four" },
|
|
{ driverId: "driver_2", pitStopMs: 9500, transponder: "secret-transponder-two" },
|
|
],
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
test("public share lookup rejects short, disabled, and unknown tokens", () => {
|
|
const state = buildState();
|
|
assert.equal(findPublicTrainingShare(state, "short"), null);
|
|
assert.equal(findPublicTrainingShare(state, DISABLED_TOKEN), null);
|
|
assert.equal(hasPublicTrainingShare(state, "unknown-token-123456789012345678"), false);
|
|
assert.equal(hasPublicTrainingShare(state, DRIVER_ONE_TOKEN), true);
|
|
});
|
|
|
|
test("public profile contains only the selected driver's sanitized aggregates", () => {
|
|
const now = Date.now();
|
|
const profile = buildPublicTrainingProfile(buildState(now), DRIVER_ONE_TOKEN, "2026-08-24T12:00:00.000Z", now - 30000, 30000);
|
|
|
|
assert.equal(profile.profile.name, "Driver One");
|
|
assert.equal(profile.profile.brand, "Associated 2WD");
|
|
assert.equal(profile.profile.className, "2WD");
|
|
assert.equal("clubRank" in profile.profile, false);
|
|
assert.equal("rankedDrivers" in profile.profile, false);
|
|
assert.equal(profile.branding.clubName, "Test Club");
|
|
assert.equal(profile.branding.logoDataUrl, "");
|
|
assert.equal(profile.stats.totalLaps, 4);
|
|
assert.equal(profile.stats.bestLapMs, 16000);
|
|
assert.deepEqual(profile.currentSession, { laps: 2, bestLapMs: 16000, averageLapMs: 16250 });
|
|
assert.equal(profile.goals.length, 1);
|
|
assert.equal(profile.goals[0].title, "Bronze");
|
|
assert.equal(profile.goals[0].percent, 80);
|
|
assert.equal(profile.classProfiles.length, 2);
|
|
assert.equal(profile.classProfiles[0].stats.totalLaps, 4);
|
|
assert.equal(profile.classProfiles[0].stats.achievements.find((item) => item.id === "first_session").unlocked, true);
|
|
assert.deepEqual(profile.classProfiles[0].channelStats, {
|
|
splitCount: 2,
|
|
bestSplitMs: 7000,
|
|
averageSplitMs: 7100,
|
|
pitStopCount: 1,
|
|
totalPitMs: 12500,
|
|
bestPitStopMs: 12500,
|
|
averagePitStopMs: 12500,
|
|
});
|
|
assert.equal(profile.classProfiles[1].className, "4WD");
|
|
assert.equal(profile.classProfiles[1].stats.totalLaps, 2);
|
|
assert.equal(profile.classProfiles[1].stats.bestLapMs, 14000);
|
|
assert.equal(profile.classProfiles[1].channelStats.splitCount, 1);
|
|
assert.equal(profile.classProfiles[1].channelStats.pitStopCount, 1);
|
|
|
|
const serialized = JSON.stringify(profile);
|
|
assert.equal(serialized.includes("Driver Two"), false);
|
|
assert.equal(serialized.includes("driver_1"), false);
|
|
assert.equal(serialized.includes("secret-transponder"), false);
|
|
assert.equal(serialized.includes("secret-decoder"), false);
|
|
});
|
|
|
|
test("public profile exposes only class-scoped development trends and achievements", () => {
|
|
const now = Date.now();
|
|
const state = buildState(now);
|
|
state.training.ledger = Array.from({ length: 60 }, (_, index) => ({
|
|
driverId: "driver_1",
|
|
sessionId: "practice_2wd",
|
|
timestamp: now - (60 - index) * 18_000,
|
|
lapMs: index < 30 ? 18_000 : 17_000,
|
|
}));
|
|
const profile = buildPublicTrainingProfile(state, DRIVER_ONE_TOKEN, "");
|
|
const twoWheelDrive = profile.classProfiles.find((item) => item.classId === "class_1");
|
|
const fourWheelDrive = profile.classProfiles.find((item) => item.classId === "class_2");
|
|
|
|
assert.equal(twoWheelDrive.stats.trendSampleReady, true);
|
|
assert.equal(twoWheelDrive.stats.averageTrendMs, 1_000);
|
|
assert.equal(twoWheelDrive.stats.achievements.find((item) => item.id === "controlled_5").unlocked, true);
|
|
assert.equal(fourWheelDrive.stats.totalLaps, 0);
|
|
assert.equal(fourWheelDrive.stats.trendSampleReady, false);
|
|
});
|
|
|
|
test("public timer window freezes aggregates after the personal session", () => {
|
|
const now = Date.now();
|
|
const profile = buildPublicTrainingProfile(buildState(now), DRIVER_ONE_TOKEN, "", now - 30 * 60000, 20 * 60000);
|
|
|
|
assert.deepEqual(profile.currentSession, { laps: 1, bestLapMs: 19000, averageLapMs: 19000 });
|
|
});
|
|
|
|
test("public profile returns null when token or driver is unavailable", () => {
|
|
const state = buildState();
|
|
assert.equal(buildPublicTrainingProfile(state, DISABLED_TOKEN, ""), null);
|
|
state.drivers = state.drivers.filter((driver) => driver.id !== "driver_1");
|
|
assert.equal(buildPublicTrainingProfile(state, DRIVER_ONE_TOKEN, ""), null);
|
|
});
|
|
|
|
test("public timer query normalization bounds age and window values", () => {
|
|
const before = Date.now();
|
|
const since = normalizePublicTrainingSince(undefined, "30000");
|
|
const after = Date.now();
|
|
assert.ok(since >= before - 30000 && since <= after - 30000);
|
|
assert.equal(normalizePublicTrainingSince(undefined, String(25 * 60 * 60 * 1000)), 0);
|
|
assert.equal(normalizePublicTrainingWindow("-100"), 0);
|
|
assert.equal(normalizePublicTrainingWindow(String(25 * 60 * 60 * 1000)), 24 * 60 * 60 * 1000);
|
|
});
|
|
|
|
test("public profile counts a max-boundary lap but excludes it from pace", () => {
|
|
const now = Date.now();
|
|
const state = buildState(now);
|
|
state.training.ledger = [
|
|
{ driverId: "driver_1", sourceId: "tp_1", sessionId: "practice", timestamp: now - 127_000, lapMs: 37_000 },
|
|
{ driverId: "driver_1", sourceId: "tp_1", sessionId: "practice", timestamp: now - 90_000, lapMs: 37_000 },
|
|
{ driverId: "driver_1", sourceId: "tp_1", sessionId: "practice", timestamp: now, lapMs: 90_000, excludedFromPace: true, stintBoundary: true },
|
|
];
|
|
|
|
const profile = buildPublicTrainingProfile(state, DRIVER_ONE_TOKEN, "");
|
|
assert.equal(profile.stats.totalLaps, 3);
|
|
assert.equal(profile.stats.bestLapMs, 37_000);
|
|
assert.equal(profile.stats.averageLapMs, 37_000);
|
|
assert.equal(profile.stats.stintCount, 2);
|
|
});
|
|
|
|
test("public five-minute result never combines simultaneous transponder sources", () => {
|
|
const laps = ["tp_a", "tp_b"].flatMap((sourceId) => Array.from({ length: 9 }, (_, index) => ({
|
|
sourceId,
|
|
sessionId: "practice_1",
|
|
timestamp: (index + 1) * 37_000,
|
|
lapMs: 37_000,
|
|
})));
|
|
const result = getPublicBestDurationResult(splitPublicTrainingStints(laps), 300_000);
|
|
|
|
assert.equal(result.lapCount, 9);
|
|
assert.equal(result.totalMs, 333_000);
|
|
});
|
|
|
|
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);
|
|
});
|