Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions backend/__tests__/utils/misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,43 +197,6 @@ describe("Misc Utils", () => {
);
});

it("sanitizeString", () => {
const testCases = [
{
input: "h̶̼͔̭͈̏́̀́͋͜ͅe̵̺̞̦̫̫͔̋́̅̅̃̀͝͝ļ̶̬̯͚͇̺͍̞̫̟͖͋̓͛̆̒̓͜ĺ̴̗̘͇̬̆͂͌̈͊͝͝ỡ̴̡̦̩̠̞̐̃͆̚͠͝",
expected: "hello",
},
{
input: "hello",
expected: "hello",
},
{
input: "hel lo",
expected: "hel lo",
},
{
input: " hel lo ",
expected: "hel lo",
},
{
input: "",
expected: "",
},
{
input: " \n\n\n",
expected: "",
},
{
input: undefined,
expected: undefined,
},
];

testCases.forEach(({ input, expected }) => {
expect(Misc.sanitizeString(input)).toEqual(expected);
});
});

it("getOrdinalNumberString", () => {
const testCases = [
{
Expand Down
4 changes: 2 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
"@vitest/coverage-v8": "4.1.5",
"concurrently": "8.2.2",
"openapi3-ts": "2.0.2",
"oxlint": "1.64.0",
"oxlint-tsgolint": "0.22.1",
"oxlint": "1.68.0",
"oxlint-tsgolint": "0.23.0",
"readline-sync": "1.4.10",
"supertest": "7.1.4",
"testcontainers": "11.11.0",
Expand Down
13 changes: 1 addition & 12 deletions backend/src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MILLISECONDS_IN_DAY } from "@monkeytype/util/date-and-time";
import { roundTo2 } from "@monkeytype/util/numbers";
export { sanitizeString } from "@monkeytype/util/strings";
import uaparser from "ua-parser-js";
import { MonkeyRequest } from "../api/types";
import { ObjectId } from "mongodb";
Expand Down Expand Up @@ -115,18 +116,6 @@ export function flattenObjectDeep(
return result;
}

export function sanitizeString(str: string | undefined): string | undefined {
if (str === undefined || str === "") {
return str;
}

return str
.replace(/[\u0300-\u036F]/g, "")
.trim()
.replace(/\n{3,}/g, "\n\n")
.replace(/\s{3,}/g, " ");
}

const suffixes = ["th", "st", "nd", "rd"];

export function getOrdinalNumberString(number: number): string {
Expand Down
105 changes: 104 additions & 1 deletion frontend/__tests__/test/events/stats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ vi.mock("../../../src/ts/test/test-state", () => ({
}));

vi.mock("../../../src/ts/config/store", () => ({
Config: { mode: "words", funbox: "" },
Config: { mode: "words", funbox: [] as string[] },
}));

vi.mock("../../../src/ts/test/test-words", () => {
Expand Down Expand Up @@ -140,6 +140,7 @@ describe("stats.ts", () => {
resetTestEvents();
__testing.resetPressedKeys();
(Config as { mode: string }).mode = "words";
(Config as { funbox: string[] }).funbox = [];
(TestState as { activeWordIndex: number }).activeWordIndex = 0;
TestWords.list.length = 0;
});
Expand Down Expand Up @@ -502,6 +503,40 @@ describe("stats.ts", () => {
});
});

describe("getTargetWord", () => {
it("returns simulatedInput in zen mode", () => {
(Config as { mode: string }).mode = "zen";
expect(statsTesting.getTargetWord(0, "anything", false)).toBe("anything");
});

it("returns word without trailing space when it ends with newline", () => {
TestWords.list.push("hello\n");
expect(statsTesting.getTargetWord(0, "hello", false)).toBe("hello\n");
});

it("appends trailing space for non-last word", () => {
TestWords.list.push("hello");
expect(statsTesting.getTargetWord(0, "hello", false)).toBe("hello ");
});

it("does not append trailing space for last word", () => {
TestWords.list.push("hello");
expect(statsTesting.getTargetWord(0, "hello", true)).toBe("hello");
});

it("does not append trailing space when nospace funbox is active", () => {
TestWords.list.push("hello");
(Config as { funbox: string[] }).funbox = ["nospace"];
expect(statsTesting.getTargetWord(0, "hello", false)).toBe("hello");
});

it("does not append trailing space when underscore_spaces funbox is active", () => {
TestWords.list.push("hello");
(Config as { funbox: string[] }).funbox = ["underscore_spaces"];
expect(statsTesting.getTargetWord(0, "hello", false)).toBe("hello");
});
});

describe("getChars", () => {
it("counts all correct for a perfectly typed word", () => {
TestWords.list.push("hello");
Expand Down Expand Up @@ -675,6 +710,74 @@ describe("stats.ts", () => {
// at 2s: 3 + 2 ("cd") = 5 correctWord chars → (5/5)*60/2 = 30
expect(wpm[1]).toBe(30);
});

it("counts non-last word as correct without trailing space when nospace funbox is active", () => {
(Config as { funbox: string[] }).funbox = ["nospace"];
TestWords.list.push("ab", "cd");
(TestState as { activeWordIndex: number }).activeWordIndex = 1;

logTestEvent("timer", 1000, timer("start", 0));
// type "ab" then "cd" with no space between (nospace mode)
logTestEvent(
"input",
1100,
input({ charIndex: 0, wordIndex: 0, data: "a" }),
);
logTestEvent(
"input",
1200,
input({ charIndex: 1, wordIndex: 0, data: "b" }),
);
logTestEvent(
"input",
1300,
input({ charIndex: 0, wordIndex: 1, data: "c" }),
);
logTestEvent(
"input",
1400,
input({ charIndex: 1, wordIndex: 1, data: "d" }),
);
logTestEvent("timer", 2000, timer("step", 1));
logTestEvent("timer", 2000, timer("end", 1));

const wpm = getWpmHistory();
// both words fully correct → 4 correctWord chars in 1s = (4/5)*60 = 48
expect(wpm).toEqual([48]);
});

it("counts multiline word as correct when target ends in newline", () => {
TestWords.list.push("hello\n", "world");
(TestState as { activeWordIndex: number }).activeWordIndex = 1;

logTestEvent("timer", 1000, timer("start", 0));
// type "hello\n"
const word1 = "hello\n";
for (let i = 0; i < word1.length; i++) {
logTestEvent(
"input",
1100 + i * 50,
input({ charIndex: i, wordIndex: 0, data: word1[i] as string }),
);
}
// type "world"
const word2 = "world";
for (let i = 0; i < word2.length; i++) {
logTestEvent(
"input",
1500 + i * 50,
input({ charIndex: i, wordIndex: 1, data: word2[i] as string }),
);
}
logTestEvent("timer", 2000, timer("step", 1));
logTestEvent("timer", 2000, timer("end", 1));

const wpm = getWpmHistory();
// word 0: "hello\n" target matches input "hello\n" → 6 correctWord
// word 1: "world" (last word) matches → 5 correctWord
// 11 chars in 1s = (11/5)*60 = 132
expect(wpm).toEqual([132]);
});
});

describe("forceReleaseAllKeys", () => {
Expand Down
6 changes: 3 additions & 3 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@
"eslint-plugin-compat": "7.0.0",
"eslint-plugin-solid": "0.14.5",
"firebase-tools": "13.15.1",
"fontawesome-subset": "4.4.0",
"fontawesome-subset": "4.6.0",
"happy-dom": "20.8.9",
"jsdom": "27.4.0",
"madge": "8.0.0",
"magic-string": "0.30.17",
"normalize.css": "8.0.1",
"oxlint": "1.64.0",
"oxlint-tsgolint": "0.22.1",
"oxlint": "1.68.0",
"oxlint-tsgolint": "0.23.0",
"postcss": "8.5.15",
"sass": "1.70.0",
"solid-devtools": "0.34.5",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ts/config/metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export const configMetadata: ConfigMetadataObject = {
changeRequiresRestart: false,
group: "behavior",
description:
"Disable result saving, in case you want to practice without affecting your account stats.",
'Set this setting to "off" in case you want to practice without saving new results to your account and affecting your statistics.',
},
blindMode: {
key: "blindMode",
Expand Down
19 changes: 14 additions & 5 deletions frontend/src/ts/test/events/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { calculateWpm } from "../../utils/numbers";
import { mean, roundTo2 } from "@monkeytype/util/numbers";
import { InputEvent, TestEvent } from "./types";
import { Config } from "../../config/store";
import { isFunboxActive } from "../funbox/list";

function getTimerBoundaries(events: TestEvent[]): number[] {
const boundaries: number[] = [];
Expand Down Expand Up @@ -240,7 +241,17 @@ function getTargetWord(
return word;
}

return word + (lastWord ? "" : " ");
let wordEnd = "";

if (!lastWord) {
wordEnd = " ";
}

if (isFunboxActive("nospace") || isFunboxActive("underscore_spaces")) {
wordEnd = "";
}

return word + wordEnd;
}
}

Expand Down Expand Up @@ -430,10 +441,7 @@ export function getWpmHistory(): number[] {
const lastWord = wordIndex === adjustedMax;

const trimmed = lastWord ? input.trimEnd() : input;
const targetWord =
Config.mode === "zen"
? trimmed
: TestWords.words.getText(wordIndex) + (lastWord ? "" : " ");
const targetWord = getTargetWord(wordIndex, trimmed, lastWord);
totalCorrect += countChars(
trimmed,
targetWord,
Expand Down Expand Up @@ -514,4 +522,5 @@ export function forceReleaseAllKeys(): void {

export const __testing = {
getTimerBoundaries,
getTargetWord,
};
3 changes: 2 additions & 1 deletion frontend/src/ts/test/test-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ export async function addWord(): Promise<void> {
const toPushCount = funboxToPush?.split(":")[1];
if (toPushCount !== undefined) bound = +toPushCount - 1;

if (TestWords.words.length - TestState.activeWordIndex > bound) {
if (TestWords.words.length - (TestState.activeWordIndex + 1) > bound) {
console.debug("Not adding word, enough words already");
return;
}
Expand Down Expand Up @@ -1163,6 +1163,7 @@ function compareCompletedEvents(
mode2: ce.mode2,
difficulty: ce.difficulty,
duration: ce.testDuration,
funboxes: getActiveFunboxNames().join(","),
// ce: ce as Record<string, unknown>,
// ce2: ce2 as Record<string, unknown>,
},
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@
"knip": "2.19.2",
"lint-staged": "13.2.3",
"only-allow": "1.2.1",
"oxfmt": "0.49.0",
"oxlint": "1.64.0",
"oxlint-tsgolint": "0.22.1",
"oxfmt": "0.53.0",
"oxlint": "1.68.0",
"oxlint-tsgolint": "0.23.0",
"prettier": "3.7.1",
"stylelint": "17.6.0",
"stylelint-config-standard": "40.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"@monkeytype/typescript-config": "workspace:*",
"@types/node": "24.9.1",
"madge": "8.0.0",
"oxlint": "1.64.0",
"oxlint-tsgolint": "0.22.1",
"oxlint": "1.68.0",
"oxlint-tsgolint": "0.23.0",
"tsup": "8.4.0",
"typescript": "6.0.2",
"vitest": "4.1.0"
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const ReportCompletedEventMismatchRequestSchema = z.object({
mode2: Mode2Schema.optional(),
difficulty: DifficultySchema.optional(),
duration: z.number().max(200).optional(),
funboxes: z.string().max(100).optional(),
// ce: z.record(z.unknown()),
// ce2: z.record(z.unknown()),
});
Expand Down
4 changes: 2 additions & 2 deletions packages/funbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"@monkeytype/typescript-config": "workspace:*",
"@types/node": "24.9.1",
"madge": "8.0.0",
"oxlint": "1.64.0",
"oxlint-tsgolint": "0.22.1",
"oxlint": "1.68.0",
"oxlint-tsgolint": "0.23.0",
"tsup": "8.4.0",
"typescript": "6.0.2",
"vitest": "4.1.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/release/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"devDependencies": {
"nodemon": "3.1.14",
"oxlint": "1.64.0",
"oxlint-tsgolint": "0.22.1"
"oxlint": "1.68.0",
"oxlint-tsgolint": "0.23.0"
}
}
7 changes: 5 additions & 2 deletions packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
"lint": "oxlint . --type-aware --type-check",
"lint-fast": "oxlint ."
},
"dependencies": {
"@monkeytype/util": "workspace:*"
},
"devDependencies": {
"@monkeytype/tsup-config": "workspace:*",
"@monkeytype/typescript-config": "workspace:*",
"@types/node": "24.9.1",
"madge": "8.0.0",
"oxlint": "1.64.0",
"oxlint-tsgolint": "0.22.1",
"oxlint": "1.68.0",
"oxlint-tsgolint": "0.23.0",
"tsup": "8.4.0",
"typescript": "6.0.2",
"vitest": "4.1.0"
Expand Down
13 changes: 1 addition & 12 deletions packages/schemas/src/validation/validation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { replaceHomoglyphs } from "./homoglyphs";
import { sanitizeString } from "@monkeytype/util/strings";
import { ZodEffects, ZodString } from "zod";

// Sorry for the bad words
Expand Down Expand Up @@ -391,18 +392,6 @@ const disallowedWords = [
"zabourah",
];

function sanitizeString(str: string | undefined): string | undefined {
if (str === undefined || str === "") {
return str;
}

return str
.replace(/[\u0300-\u036F]/g, "")
.trim()
.replace(/\n{3,}/g, "\n\n")
.replace(/\s{3,}/g, " ");
}

function containsDisallowedWords(
text: string,
mode: "word" | "substring",
Expand Down
Loading
Loading