Skip to content
Open
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
150 changes: 150 additions & 0 deletions frontend/__tests__/input/handlers/insert-text.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
getInputElementValue,
setInputElementValue,
} from "../../../src/ts/input/input-element";
import { onInsertText } from "../../../src/ts/input/handlers/insert-text";
import {
getMatchingLigatureOverride,
shouldIgnoreLigatureCompletion,
} from "../../../src/ts/input/helpers/ligatures";

const mocks = vi.hoisted(() => ({
currentWord: "",
input: {
current: "",
syncWithInputElement: vi.fn(),
},
}));

vi.mock("../../../src/ts/test/test-ui", () => ({}));
vi.mock("../../../src/ts/test/test-state", () => ({
activeWordIndex: 0,
isActive: true,
}));
vi.mock("../../../src/ts/test/test-logic", () => ({
startTest: vi.fn(),
}));
vi.mock("../../../src/ts/test/test-input", () => ({
input: mocks.input,
corrected: { update: vi.fn() },
incrementAccuracy: vi.fn(),
incrementKeypressCount: vi.fn(),
incrementKeypressErrors: vi.fn(),
pushKeypressWord: vi.fn(),
pushMissedWord: vi.fn(),
setBurstStart: vi.fn(),
setCurrentNotAfk: vi.fn(),
}));
vi.mock("../../../src/ts/test/test-words", () => ({
words: {
getCurrentText: vi.fn(() => mocks.currentWord),
},
}));
vi.mock("../../../src/ts/input/helpers/fail-or-finish", () => ({
checkIfFailedDueToDifficulty: vi.fn(),
checkIfFailedDueToMinBurst: vi.fn(),
checkIfFinished: vi.fn(),
}));
vi.mock("../../../src/ts/test/funbox/list", () => ({
findSingleActiveFunboxWithFunction: vi.fn(),
isFunboxActiveWithProperty: vi.fn(() => false),
}));
vi.mock("../../../src/ts/test/replay", () => ({
addReplayEvent: vi.fn(),
}));
vi.mock("../../../src/ts/config/store", () => ({
Config: {
blindMode: false,
keymapMode: "off",
language: "english",
mode: "words",
oppositeShiftMode: "off",
stopOnError: "off",
},
}));
vi.mock("../../../src/ts/events/keymap", () => ({
flash: vi.fn(),
}));
vi.mock("../../../src/ts/test/weak-spot", () => ({
updateScore: vi.fn(),
}));
vi.mock("../../../src/ts/legacy-states/composition", () => ({
getData: vi.fn(() => ""),
}));
vi.mock("../../../src/ts/input/state", () => ({
getIncorrectShiftsInARow: vi.fn(() => 0),
incrementIncorrectShiftsInARow: vi.fn(),
isCorrectShiftUsed: vi.fn(() => true),
resetIncorrectShiftsInARow: vi.fn(),
}));
vi.mock("../../../src/ts/states/notifications", () => ({
showNoticeNotification: vi.fn(),
}));
vi.mock("../../../src/ts/input/helpers/word-navigation", () => ({
goToNextWord: vi.fn(async () => ({
increasedWordIndex: false,
lastBurst: null,
})),
}));
vi.mock("../../../src/ts/input/handlers/before-insert-text", () => ({
onBeforeInsertText: vi.fn(),
}));

describe("insert-text ligature input overrides", () => {
Comment thread
Dawn-Fighter marked this conversation as resolved.
beforeEach(() => {
mocks.currentWord = "";
mocks.input.current = "";
mocks.input.syncWithInputElement.mockImplementation(() => {
mocks.input.current = getInputElementValue().inputValue;
});
setInputElementValue("");
});

afterEach(() => {
vi.clearAllMocks();
setInputElementValue("");
});

it.each([
["o", "œ", "œ"],
["O", "Œ", "Œ"],
["a", "æ", "æ"],
["A", "Æ", "Æ"],
])(
"normalizes '%s' to '%s' when target is '%s'",
(data, target, expected) => {
expect(getMatchingLigatureOverride(data, target)).toBe(expected);
},
);

it.each([
["e", "œ", "œuvre"],
["E", "Œ", "ŒUVRE"],
["e", "æ", "æther"],
["E", "Æ", "ÆTHER"],
["e", "bœ", "bœuf"],
])("ignores completion '%s' after '%s'", (data, input, word) => {
expect(shouldIgnoreLigatureCompletion(data, input, word)).toBe(true);
});

it("does not normalize unrelated input", () => {
expect(getMatchingLigatureOverride("e", "œ")).toBeNull();
expect(shouldIgnoreLigatureCompletion("u", "œ", "œuvre")).toBe(false);
});

it("removes the completion character and keeps input state synced", async () => {
mocks.currentWord = "œuvre";
mocks.input.current = "œ";
setInputElementValue("œe");

await onInsertText({
now: performance.now(),
data: "e",
});

expect(getInputElementValue().inputValue).toBe("œ");
expect(mocks.input.current).toBe("œ");
expect(mocks.input.syncWithInputElement).toHaveBeenCalledOnce();
});
});
22 changes: 22 additions & 0 deletions frontend/src/ts/input/handlers/insert-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import {
isCharCorrect,
shouldInsertSpaceCharacter,
} from "../helpers/validation";
import {
getMatchingLigatureOverride,
shouldIgnoreLigatureCompletion,
} from "../helpers/ligatures";

const charOverrides = new Map<string, string>([
["…", "..."],
Expand Down Expand Up @@ -82,6 +86,18 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
return;
}

if (
shouldIgnoreLigatureCompletion(
options.data,
TestInput.input.current,
TestWords.words.getCurrentText(),
)
) {
setInputElementValue(inputValue.slice(0, -options.data.length));
TestInput.input.syncWithInputElement();
return;
}
Comment thread
Dawn-Fighter marked this conversation as resolved.

const charOverride = charOverrides.get(options.data);
if (
charOverride !== undefined &&
Expand Down Expand Up @@ -301,6 +317,12 @@ function normalizeDataAndUpdateInputIfNeeded(
) {
replaceInputElementLastValueChar(targetChar);
normalizedData = targetChar;
} else {
const ligatureOverride = getMatchingLigatureOverride(data, targetChar);
if (ligatureOverride !== null) {
replaceInputElementLastValueChar(ligatureOverride);
normalizedData = ligatureOverride;
}
}
return normalizedData;
}
Expand Down
34 changes: 34 additions & 0 deletions frontend/src/ts/input/helpers/ligatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const ligatureInputOverrides = new Map<string, string>([
["œ", "oe"],
["Œ", "OE"],
["æ", "ae"],
["Æ", "AE"],
]);

export function getMatchingLigatureOverride(
data: string,
targetChar: string | undefined,
): string | null {
if (targetChar === undefined) return null;

const override = ligatureInputOverrides.get(targetChar);
if (override?.[0] !== data) return null;

return targetChar;
}

export function shouldIgnoreLigatureCompletion(
data: string,
currentInput: string,
currentWord: string,
): boolean {
const previousTargetChar = currentWord[currentInput.length - 1];
if (previousTargetChar === undefined) return false;

const override = ligatureInputOverrides.get(previousTargetChar);
if (override === undefined) return false;

return (
currentInput.endsWith(previousTargetChar) && data === override.slice(1)
);
}
Loading