From 41c0fd728489f8c12bb18fce56125d9d5009b6f1 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sun, 21 Dec 2025 10:34:38 +0100 Subject: [PATCH] rework resolve schema --- frontend/src/ts/test/test-logic.ts | 60 ++++++++++++++++++------------ frontend/src/ts/tribe/types.ts | 60 +++++++++++++++++++++++------- 2 files changed, 83 insertions(+), 37 deletions(-) diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index ce1c32583..24df55aa3 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -1018,6 +1018,7 @@ export async function finish(difficultyFailed = false): Promise { const mode2Number = parseInt(completedEvent.mode2); let tooShort = false; + let invalidReason = ""; //fail checks const dateDur = (TestStats.end3 - TestStats.start3) / 1000; if ( @@ -1029,6 +1030,7 @@ export async function finish(difficultyFailed = false): Promise { Notifications.add("Test invalid - inconsistent test duration", 0); console.error("Test duration inconsistent", ce.testDuration, dateDur); TestStats.setInvalid(); + invalidReason = "inconsistent test duration"; dontSave = true; } else if (difficultyFailed) { Notifications.add(`Test failed - ${failReason}`, 0, { @@ -1039,11 +1041,13 @@ export async function finish(difficultyFailed = false): Promise { Notifications.add("Test invalid - AFK detected", 0); TestStats.setInvalid(); dontSave = true; + invalidReason = "afk detected"; TribeState.setAutoReady(false); } else if (TestState.isRepeated) { Notifications.add("Test invalid - repeated", 0); TestStats.setInvalid(); dontSave = true; + invalidReason = "repeated test"; } else if ( completedEvent.testDuration < 1 || (Config.mode === "time" && mode2Number < 15 && mode2Number > 0) || @@ -1065,6 +1069,7 @@ export async function finish(difficultyFailed = false): Promise { ) { Notifications.add("Test invalid - too short", 0); TestStats.setInvalid(); + invalidReason = "too short"; tooShort = true; dontSave = true; } else if ( @@ -1078,6 +1083,7 @@ export async function finish(difficultyFailed = false): Promise { ) { Notifications.add("Test invalid - wpm", 0); TestStats.setInvalid(); + invalidReason = "wpm"; dontSave = true; } else if ( completedEvent.rawWpm < 0 || @@ -1090,6 +1096,7 @@ export async function finish(difficultyFailed = false): Promise { ) { Notifications.add("Test invalid - raw", 0); TestStats.setInvalid(); + invalidReason = "raw wpm"; dontSave = true; } else if ( (!DB.getSnapshot()?.lbOptOut && @@ -1099,6 +1106,7 @@ export async function finish(difficultyFailed = false): Promise { ) { Notifications.add("Test invalid - accuracy", 0); TestStats.setInvalid(); + invalidReason = "accuracy"; dontSave = true; } @@ -1177,14 +1185,16 @@ export async function finish(difficultyFailed = false): Promise { if (dontSave) { void AnalyticsController.log("testCompletedInvalid"); resolveTestSavePromise({ - afk: afkDetected, - bailedOut: completedEvent.bailedOut, - repeated: TestState.isRepeated, - tooShort: tooShort, - failed: difficultyFailed, - failedReason: failReason, login: true, - valid: false, + bailedOut: completedEvent.bailedOut, + ...(TestStats.invalid + ? { valid: false, invalidReason } + : difficultyFailed + ? { + failed: true, + failedReason: failReason, + } + : {}), }); } else { TestStats.resetIncomplete(); @@ -1208,17 +1218,17 @@ export async function finish(difficultyFailed = false): Promise { if (promise.response && promise.response.status === 200) { void AnalyticsController.log("testCompleted"); resolveTestSavePromise({ - afk: afkDetected, - bailedOut: completedEvent.bailedOut, - repeated: TestState.isRepeated, - tooShort: tooShort, - failed: difficultyFailed, - failedReason: failReason, - isPb: promise.response.body.data?.isPb ?? false, - saved: promise.saved, - saveFailedMessage: promise.message, login: true, - valid: true, + bailedOut: completedEvent.bailedOut, + ...(promise.saved + ? { + saved: true, + isPb: promise.response.body.data?.isPb ?? false, + } + : { + saved: false, + saveFailedMessage: promise.message, + }), }); } }); @@ -1231,14 +1241,16 @@ export async function finish(difficultyFailed = false): Promise { notSignedInLastResult = completedEvent; } resolveTestSavePromise({ - afk: afkDetected, - bailedOut: completedEvent.bailedOut, - repeated: TestState.isRepeated, - tooShort: tooShort, - failed: difficultyFailed, - failedReason: failReason, login: false, - valid: !dontSave, + bailedOut: completedEvent.bailedOut, + ...(TestStats.invalid + ? { valid: false, invalidReason } + : difficultyFailed + ? { + failed: true, + failedReason: failReason, + } + : {}), }); // it wont save but result.update needs it diff --git a/frontend/src/ts/tribe/types.ts b/frontend/src/ts/tribe/types.ts index 9d8745f81..62bead38c 100644 --- a/frontend/src/ts/tribe/types.ts +++ b/frontend/src/ts/tribe/types.ts @@ -34,19 +34,53 @@ export type Result = { resolve: ResultResolve; }; -export type ResultResolve = { - login?: boolean; - saved?: boolean; - failed?: boolean; - afk?: boolean; - repeated?: boolean; - failedReason?: string; - valid?: boolean; - tooShort?: boolean; - saveFailedMessage?: string; - isPb?: boolean; - bailedOut?: boolean; -}; +type LoggedInDontSave = { + login: true; + bailedOut: boolean; +} & ( + | { + valid: false; + invalidReason: string; + } + | { + failed: true; + failedReason: string; + } + // oxlint-disable-next-line no-empty-object-type + | {} +); + +type LoggedInSave = { + login: true; + bailedOut: boolean; +} & ( + | { + saved: true; + isPb: boolean; + } + | { + saved: false; + saveFailedMessage: string; + } +); + +type LoggedOut = { + login: false; + bailedOut: boolean; +} & ( + | { + valid: false; + invalidReason: string; + } + | { + failed: true; + failedReason: string; + } + // oxlint-disable-next-line no-empty-object-type + | {} +); + +export type ResultResolve = LoggedInDontSave | LoggedInSave | LoggedOut; export type RoomJoin = { room: Room;