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
45 changes: 45 additions & 0 deletions frontend/__tests__/test/words-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import type { LanguageObject } from "@monkeytype/schemas/languages";

vi.mock("../../src/ts/test/wikipedia", () => ({
getSection: vi.fn(async () => ({
title: "Mock",
author: "Mock",
words: ["fetched", "wikipedia", "words"],
})),
}));

import { Config } from "../../src/ts/config/store";
import { generateWords } from "../../src/ts/test/words-generator";
import { getSection } from "../../src/ts/test/wikipedia";

const testLanguage: LanguageObject = {
name: "english",
words: ["the", "quick", "brown", "fox", "jumps", "over", "lazy", "dog"],
};

describe("words-generator", () => {
describe("generateWords", () => {
beforeEach(() => {
vi.clearAllMocks();
Config.mode = "words";
Config.words = 5;
Config.funbox = ["wikipedia"];
});

it("does not pull the funbox section when skipFunboxSection is true", async () => {
const result = await generateWords(testLanguage, {
skipFunboxSection: true,
});

expect(getSection).not.toHaveBeenCalled();
expect(result.words.length).toBeGreaterThan(0);
});

it("pulls the funbox section when skipFunboxSection is not set", async () => {
await generateWords(testLanguage);

expect(getSection).toHaveBeenCalled();
});
});
});
1 change: 1 addition & 0 deletions frontend/src/ts/pages/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const page = new Page({
afterHide: async (): Promise<void> => {
void TestLogic.restart({
noAnim: true,
skipFunboxSection: true,
});
void Funbox.clear();
updateFooterAndVerticalAds(true);
Expand Down
17 changes: 12 additions & 5 deletions frontend/src/ts/test/test-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type RestartOptions = {
practiseMissed?: boolean;
noAnim?: boolean;
isQuickRestart?: boolean;
skipFunboxSection?: boolean;
};

export async function restart(options = {} as RestartOptions): Promise<void> {
Expand Down Expand Up @@ -328,7 +329,9 @@ export async function restart(options = {} as RestartOptions): Promise<void> {

await Funbox.rememberSettings();

const initResult = await init();
const initResult = await init({
skipFunboxSection: options.skipFunboxSection,
});

if (!initResult) {
setIsTestRestarting(false);
Expand All @@ -351,7 +354,9 @@ let lastInitError: Error | null = null;
let showedLazyModeNotification: boolean = false;
let testReinitCount = 0;

async function init(): Promise<boolean> {
async function init(options?: {
skipFunboxSection?: boolean;
}): Promise<boolean> {
console.debug("Initializing test");
testReinitCount++;
if (testReinitCount > 3) {
Expand Down Expand Up @@ -380,7 +385,7 @@ async function init(): Promise<boolean> {
}

if (!language || language.name !== Config.language) {
return await init();
return await init(options);
}

if (getActivePage() === "test") {
Expand Down Expand Up @@ -479,7 +484,9 @@ async function init(): Promise<boolean> {
let generatedWords: string[] = [];
let generatedSectionIndexes: number[] = [];
try {
const gen = await WordsGenerator.generateWords(language);
const gen = await WordsGenerator.generateWords(language, {
skipFunboxSection: options?.skipFunboxSection,
});
generatedWords = gen.words;
generatedSectionIndexes = gen.sectionIndexes;
wordsHaveTab = gen.hasTab;
Expand All @@ -504,7 +511,7 @@ async function init(): Promise<boolean> {
});
}

return await init();
return await init(options);
}

let hasNumbers = false;
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/ts/test/words-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,11 @@ function getFunboxWordsFrequency(): FunboxWordsFrequency | undefined {
}

async function getFunboxSection(): Promise<string[]> {
const ret = [];
const ret: string[] = [];

if (skipFunboxSection) {
return ret;
}

const funbox = findSingleActiveFunboxWithFunction("pullSection");

Expand Down Expand Up @@ -600,6 +604,7 @@ async function getQuoteWordList(
let currentWordset: Wordset | null = null;
let currentLanguage: LanguageObject | null = null;
let isCurrentlyUsingFunboxSection = false;
let skipFunboxSection = false;

type GenerateWordsReturn = {
words: string[];
Expand All @@ -614,7 +619,9 @@ let previousRandomQuote: QuoteWithTextSplit | null = null;

export async function generateWords(
language: LanguageObject,
options?: { skipFunboxSection?: boolean },
): Promise<GenerateWordsReturn> {
skipFunboxSection = options?.skipFunboxSection ?? false;
if (!isRepeated()) {
previousGetNextWordReturns = [];
}
Expand All @@ -634,7 +641,8 @@ export async function generateWords(
allJoiningScript: language.joiningScript ?? false,
};

isCurrentlyUsingFunboxSection = isFunboxActiveWithFunction("pullSection");
isCurrentlyUsingFunboxSection =
!skipFunboxSection && isFunboxActiveWithFunction("pullSection");

const wordOrder = getWordOrder();
console.debug("Word order", wordOrder);
Expand Down
Loading