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
25 changes: 13 additions & 12 deletions frontend/__tests__/test/events/stats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ vi.mock("../../../src/ts/test/test-stats", () => ({
}));

vi.mock("../../../src/ts/test/test-state", () => ({
activeWordIndex: 0,
bailedOut: false,
resultCalculating: false,
koreanStatus: false,
}));

const mockState = vi.hoisted(() => ({ activeWordIndex: 0 }));

vi.mock("../../../src/ts/config/store", () => ({
Config: { mode: "words", funbox: [] as string[], words: 25, time: 0 },
getConfig: {},
Expand Down Expand Up @@ -51,6 +52,7 @@ vi.mock("../../../src/ts/test/custom-text", () => ({

vi.mock("../../../src/ts/states/test", () => ({
getCurrentQuote: () => null,
getActiveWordIndex: () => mockState.activeWordIndex,
}));

import {
Expand Down Expand Up @@ -90,7 +92,6 @@ import type {
} from "../../../src/ts/test/events/types";
import { Config } from "../../../src/ts/config/store";
import { Keycode } from "../../../src/ts/constants/keys";
import * as TestState from "../../../src/ts/test/test-state";
import { words as TestWords } from "../../../src/ts/test/test-words";
import { isFunboxActiveWithProperty } from "../../../src/ts/test/funbox/list";

Expand Down Expand Up @@ -197,7 +198,7 @@ describe("stats.ts", () => {
(Config as { funbox: string[] }).funbox = [];
(Config as { words: number }).words = 25;
(Config as { time: number }).time = 0;
(TestState as { activeWordIndex: number }).activeWordIndex = 0;
mockState.activeWordIndex = 0;
TestWords.reset();
inputPerWord.clear();
});
Expand Down Expand Up @@ -1152,7 +1153,7 @@ describe("stats.ts", () => {
describe("getChars", () => {
it("counts all correct for a perfectly typed word", () => {
pushWords("hello");
(TestState as { activeWordIndex: number }).activeWordIndex = 0;
mockState.activeWordIndex = 0;

logTestEvent("timer", 1000, timer("start", 0));
for (let i = 0; i < 5; i++) {
Expand All @@ -1173,7 +1174,7 @@ describe("stats.ts", () => {

it("counts incorrect chars", () => {
pushWords("ab");
(TestState as { activeWordIndex: number }).activeWordIndex = 0;
mockState.activeWordIndex = 0;

logTestEvent("timer", 1000, timer("start", 0));
logTestEvent(
Expand All @@ -1194,7 +1195,7 @@ describe("stats.ts", () => {

it("counts extra chars", () => {
pushWords("ab");
(TestState as { activeWordIndex: number }).activeWordIndex = 0;
mockState.activeWordIndex = 0;

logTestEvent("timer", 1000, timer("start", 0));
logTestEvent(
Expand All @@ -1219,7 +1220,7 @@ describe("stats.ts", () => {

it("counts missed chars for completed non-last words", () => {
pushWords("hello", "world");
(TestState as { activeWordIndex: number }).activeWordIndex = 1;
mockState.activeWordIndex = 1;

logTestEvent("timer", 1000, timer("start", 0));
// type "hel" then space (incomplete first word)
Expand Down Expand Up @@ -1265,7 +1266,7 @@ describe("stats.ts", () => {
// Japanese IME commits words with the ideographic space U+3000, while the
// target word separator is a regular space — normalize so it still counts
pushWords("しり", "かこ");
(TestState as { activeWordIndex: number }).activeWordIndex = 1;
mockState.activeWordIndex = 1;

logTestEvent("timer", 1000, timer("start", 0));
logTestEvent(
Expand Down Expand Up @@ -1305,7 +1306,7 @@ describe("stats.ts", () => {
describe("getWpmHistory", () => {
it("returns wpm at each timer boundary", () => {
pushWords("hello");
(TestState as { activeWordIndex: number }).activeWordIndex = 0;
mockState.activeWordIndex = 0;

logTestEvent("timer", 1000, timer("start", 0));
// type "hello" in first second — 5 correct word chars
Expand All @@ -1326,7 +1327,7 @@ describe("stats.ts", () => {

it("returns cumulative wpm across boundaries", () => {
pushWords("ab", "cd");
(TestState as { activeWordIndex: number }).activeWordIndex = 1;
mockState.activeWordIndex = 1;

logTestEvent("timer", 1000, timer("start", 0));
// type "ab " in first second — correct word
Expand Down Expand Up @@ -1371,7 +1372,7 @@ describe("stats.ts", () => {
it("counts non-last word as correct without trailing space when nospace funbox is active", () => {
(Config as { funbox: string[] }).funbox = ["nospace"];
pushWords("ab", "cd");
(TestState as { activeWordIndex: number }).activeWordIndex = 1;
mockState.activeWordIndex = 1;

logTestEvent("timer", 1000, timer("start", 0));
// type "ab" then "cd" with no space between (nospace mode)
Expand Down Expand Up @@ -1405,7 +1406,7 @@ describe("stats.ts", () => {

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

logTestEvent("timer", 1000, timer("start", 0));
// type "hello\n"
Expand Down
4 changes: 2 additions & 2 deletions frontend/__tests__/test/test-words.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach, vi } from "vitest";

vi.mock("../../src/ts/test/test-state", () => ({
activeWordIndex: 0,
vi.mock("../../src/ts/states/test", () => ({
getActiveWordIndex: () => 0,
}));

import { words } from "../../src/ts/test/test-words";
Expand Down
13 changes: 5 additions & 8 deletions frontend/src/ts/input/handlers/before-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getInputElementValue } from "../input-element";
import * as TestUI from "../../test/test-ui";
import { isAwaitingNextWord } from "../state";
import { getInputForWord } from "../../test/events/data";
import { isTestActive } from "../../states/test";
import { getActiveWordIndex, isTestActive } from "../../states/test";

export function onBeforeDelete(event: InputEvent): void {
if (!isTestActive()) {
Expand All @@ -30,15 +30,13 @@ export function onBeforeDelete(event: InputEvent): void {

if (inputIsEmpty) {
// we are on the first word, just prevent default, nothing to go back to
if (TestState.activeWordIndex === 0) {
if (getActiveWordIndex() === 0) {
event.preventDefault();
return;
}

// this is nested because we only wanna pull the element from the dom if needed
const previousWordElement = TestUI.getWordElement(
TestState.activeWordIndex - 1,
);
const previousWordElement = TestUI.getWordElement(getActiveWordIndex() - 1);
if (previousWordElement === null) {
event.preventDefault();
return;
Expand All @@ -51,10 +49,9 @@ export function onBeforeDelete(event: InputEvent): void {
}

const confidence = Config.confidenceMode;
const previousWord = TestWords.words.get(TestState.activeWordIndex - 1);
const previousWord = TestWords.words.get(getActiveWordIndex() - 1);
const previousWordCorrect =
getInputForWord(TestState.activeWordIndex - 1) ===
previousWord?.textWithCommit;
getInputForWord(getActiveWordIndex() - 1) === previousWord?.textWithCommit;

if (confidence === "on" && inputIsEmpty && !previousWordCorrect) {
event.preventDefault();
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/ts/input/handlers/before-insert-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isFunboxActiveWithProperty } from "../../test/funbox/list";
import { getInputElementValue } from "../input-element";
import { isAwaitingNextWord } from "../state";
import * as SlowTimer from "../../legacy-states/slow-timer";
import { wordsHaveNewline } from "../../states/test";
import { getActiveWordIndex, wordsHaveNewline } from "../../states/test";
import { shouldGoToNextWord } from "../helpers/validation";
import { getCommitCharacterType, normalizeData } from "../helpers/util";
import { getCurrentInput } from "../../test/events/data";
Expand Down Expand Up @@ -101,9 +101,7 @@ export function onBeforeInsertText(data: string): boolean {
// because this check is expensive (causes layout reflows)

// if there is pending word data, we need to account for that
const pendingWordData = TestUI.pendingWordData.get(
TestState.activeWordIndex,
);
const pendingWordData = TestUI.pendingWordData.get(getActiveWordIndex());
const { top: topAfterAppend, height: heightAfterAppend } =
TestUI.getActiveWordTopAndHeightWithDifferentData(
(pendingWordData ?? inputValue) + data,
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/ts/input/handlers/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { Config } from "../../config/store";
import { goToPreviousWord } from "../helpers/word-navigation";
import { DeleteInputType } from "../helpers/input-type";
import { getCurrentInput, logTestEvent } from "../../test/events/data";
import { activeWordIndex } from "../../test/test-state";
import { getActiveWordIndex } from "../../states/test";

export function onDelete(inputType: DeleteInputType, now: number): void {
const { realInputValue } = getInputElementValue();

const inputBeforeDelete = getCurrentInput();
const activeWordIndexBeforeDelete = activeWordIndex;
const activeWordIndexBeforeDelete = getActiveWordIndex();

const inputAfterDelete = getInputElementValue().inputValue;

Expand Down Expand Up @@ -44,7 +44,7 @@ export function onDelete(inputType: DeleteInputType, now: number): void {
const postNavInputValue = getInputElementValue().inputValue;
logTestEvent("input", now, {
inputType: "deleteContentBackward",
wordIndex: activeWordIndex,
wordIndex: getActiveWordIndex(),
charIndex: postNavInputValue.length,
inputValue: postNavInputValue,
});
Expand All @@ -63,7 +63,7 @@ export function onDelete(inputType: DeleteInputType, now: number): void {
const postNavInputValue = getInputElementValue().inputValue;
logTestEvent("input", now, {
inputType: inputType,
wordIndex: activeWordIndex,
wordIndex: getActiveWordIndex(),
charIndex: postNavInputValue.length,
inputValue: postNavInputValue,
...(inputBeforeDelete !== "" ? { clearedNextWord: true } : {}),
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/ts/input/handlers/insert-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
checkIfFinished,
} from "../helpers/fail-or-finish";
import { removeLanguageSize } from "../../utils/strings";
import * as TestState from "../../test/test-state";
import * as TestLogic from "../../test/test-logic";
import { Config } from "../../config/store";
import { flash } from "../../events/keymap";
Expand All @@ -31,7 +30,7 @@ import { shouldGoToNextWord, isCharCorrect } from "../helpers/validation";
import { getCurrentInput, logTestEvent } from "../../test/events/data";
import { getCommitCharacterType, normalizeData } from "../helpers/util";
import { areAllWordsGenerated } from "../../test/words-generator";
import { isTestActive } from "../../states/test";
import { getActiveWordIndex, isTestActive } from "../../states/test";

const charOverrides = new Map<string, string>([
["…", "..."],
Expand Down Expand Up @@ -140,7 +139,7 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
// helper consts
const lastInMultiOrSingle =
lastInMultiIndex === true || lastInMultiIndex === undefined;
const wordIndex = TestState.activeWordIndex;
const wordIndex = getActiveWordIndex();
const correctShiftUsed =
Config.oppositeShiftMode === "off" ? null : isCorrectShiftUsed();
const commitCharacterType = getCommitCharacterType({
Expand Down
24 changes: 14 additions & 10 deletions frontend/src/ts/input/helpers/word-navigation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Config } from "../../config/store";
import * as TestUI from "../../test/test-ui";
import * as PaceCaret from "../../test/pace-caret";
import * as TestState from "../../test/test-state";
import {
decreaseActiveWordIndex,
getActiveWordIndex,
increaseActiveWordIndex,
} from "../../states/test";
import * as TestLogic from "../../test/test-logic";
import * as TestWords from "../../test/test-words";
import {
Expand Down Expand Up @@ -42,7 +46,7 @@ export async function goToNextWord({
}

if (Config.minBurst !== "off" || Config.liveBurstStyle !== "off") {
const burst = getWordBurst(buildEventLog(), TestState.activeWordIndex, now);
const burst = getWordBurst(buildEventLog(), getActiveWordIndex(), now);
ret.lastBurst = burst;
}

Expand All @@ -51,10 +55,10 @@ export async function goToNextWord({
TestWords.words.getCurrent()?.textWithCommit ?? "",
);

const nextWord = TestWords.words.get(TestState.activeWordIndex + 1)?.text;
const nextWord = TestWords.words.get(getActiveWordIndex() + 1)?.text;
if (nextWord !== undefined) Funbox.toggleScript(nextWord);

const lastWord = TestState.activeWordIndex >= TestWords.words.length - 1;
const lastWord = getActiveWordIndex() >= TestWords.words.length - 1;
if (lastWord) {
setAwaitingNextWord(true);
showLoaderBar();
Expand All @@ -66,11 +70,11 @@ export async function goToNextWord({
}

if (
TestState.activeWordIndex < TestWords.words.length - 1 ||
getActiveWordIndex() < TestWords.words.length - 1 ||
Config.mode === "zen"
) {
ret.increasedWordIndex = true;
TestState.increaseActiveWordIndex();
increaseActiveWordIndex();
}

setInputElementValue("");
Expand All @@ -80,24 +84,24 @@ export async function goToNextWord({
}

export function goToPreviousWord(inputType: DeleteInputType): void {
if (TestState.activeWordIndex === 0) {
if (getActiveWordIndex() === 0) {
setInputElementValue("");
return;
}

TestUI.beforeTestWordChange("back", null);

TestState.decreaseActiveWordIndex();
decreaseActiveWordIndex();

const word = TestWords.words.get(TestState.activeWordIndex)?.text;
const word = TestWords.words.get(getActiveWordIndex())?.text;
if (word !== undefined) Funbox.toggleScript(word);

const nospaceEnabled = isFunboxActiveWithProperty("nospace");

if (inputType === "deleteWordBackward") {
setInputElementValue("");
} else if (inputType === "deleteContentBackward") {
const word = getInputForWord(TestState.activeWordIndex);
const word = getInputForWord(getActiveWordIndex());
if (nospaceEnabled) {
// nospace has no separator, so the prior word's commit was its last
// letter; a single backspace deletes that letter (same as non-nospace
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/ts/input/listeners/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import * as TestLogic from "../../test/test-logic";
import { setLastInsertCompositionTextData } from "../state";
import { onInsertText } from "../handlers/insert-text";
import { logTestEvent } from "../../test/events/data";
import { isTestActive, setCompositionText } from "../../states/test";
import {
getActiveWordIndex,
isTestActive,
setCompositionText,
} from "../../states/test";

const inputEl = getInputElement();

Expand All @@ -27,7 +31,7 @@ inputEl.addEventListener("compositionstart", (event) => {

logTestEvent("composition", now, {
event: "start",
wordIndex: TestState.activeWordIndex,
wordIndex: getActiveWordIndex(),
});
});

Expand All @@ -46,7 +50,7 @@ inputEl.addEventListener("compositionupdate", (event) => {
logTestEvent("composition", now, {
event: "update",
data: event.data,
wordIndex: TestState.activeWordIndex,
wordIndex: getActiveWordIndex(),
});
});

Expand All @@ -72,6 +76,6 @@ inputEl.addEventListener("compositionend", async (event) => {
logTestEvent("composition", now, {
event: "end",
data: event.data,
wordIndex: TestState.activeWordIndex,
wordIndex: getActiveWordIndex(),
});
});
4 changes: 2 additions & 2 deletions frontend/src/ts/input/listeners/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { onBeforeDelete } from "../handlers/before-delete";
import * as TestWords from "../../test/test-words";
import * as CompositionState from "../../legacy-states/composition";
import * as TestState from "../../test/test-state";
import { activeWordIndex } from "../../test/test-state";
import { getActiveWordIndex } from "../../states/test";
import { getCurrentInput } from "../../test/events/data";
import { areAllWordsGenerated } from "../../test/words-generator";

Expand Down Expand Up @@ -125,7 +125,7 @@ inputEl.addEventListener("input", async (event) => {
inputType === "insertCompositionText" ||
inputType === "insertFromComposition"
) {
const allWordsTyped = activeWordIndex >= TestWords.words.length - 1;
const allWordsTyped = getActiveWordIndex() >= TestWords.words.length - 1;
const inputPlusComposition =
getCurrentInput() + (CompositionState.getData() ?? "");
const inputPlusCompositionIsCorrect =
Expand Down
13 changes: 0 additions & 13 deletions frontend/src/ts/legacy-states/time.ts

This file was deleted.

Loading
Loading