From 09cc05ad97d18ca19b21028f4458c9772f237886 Mon Sep 17 00:00:00 2001 From: henrikhhag Date: Thu, 23 Jul 2026 11:31:46 +0200 Subject: [PATCH] fix(funbox): skip wikipedia section fetch when leaving the test page (@henrikhhag) --- .../__tests__/test/words-generator.spec.ts | 45 +++++++++++++++++++ frontend/src/ts/pages/test.ts | 1 + frontend/src/ts/test/test-logic.ts | 17 ++++--- frontend/src/ts/test/words-generator.ts | 12 ++++- 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 frontend/__tests__/test/words-generator.spec.ts diff --git a/frontend/__tests__/test/words-generator.spec.ts b/frontend/__tests__/test/words-generator.spec.ts new file mode 100644 index 000000000000..b710ec47f561 --- /dev/null +++ b/frontend/__tests__/test/words-generator.spec.ts @@ -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(); + }); + }); +}); diff --git a/frontend/src/ts/pages/test.ts b/frontend/src/ts/pages/test.ts index 337b6e2cf178..51938b95c2b9 100644 --- a/frontend/src/ts/pages/test.ts +++ b/frontend/src/ts/pages/test.ts @@ -16,6 +16,7 @@ export const page = new Page({ afterHide: async (): Promise => { void TestLogic.restart({ noAnim: true, + skipFunboxSection: true, }); void Funbox.clear(); updateFooterAndVerticalAds(true); diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index 553f403a7dd6..23cbd9827055 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -171,6 +171,7 @@ type RestartOptions = { practiseMissed?: boolean; noAnim?: boolean; isQuickRestart?: boolean; + skipFunboxSection?: boolean; }; export async function restart(options = {} as RestartOptions): Promise { @@ -328,7 +329,9 @@ export async function restart(options = {} as RestartOptions): Promise { await Funbox.rememberSettings(); - const initResult = await init(); + const initResult = await init({ + skipFunboxSection: options.skipFunboxSection, + }); if (!initResult) { setIsTestRestarting(false); @@ -351,7 +354,9 @@ let lastInitError: Error | null = null; let showedLazyModeNotification: boolean = false; let testReinitCount = 0; -async function init(): Promise { +async function init(options?: { + skipFunboxSection?: boolean; +}): Promise { console.debug("Initializing test"); testReinitCount++; if (testReinitCount > 3) { @@ -380,7 +385,7 @@ async function init(): Promise { } if (!language || language.name !== Config.language) { - return await init(); + return await init(options); } if (getActivePage() === "test") { @@ -479,7 +484,9 @@ async function init(): Promise { 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; @@ -504,7 +511,7 @@ async function init(): Promise { }); } - return await init(); + return await init(options); } let hasNumbers = false; diff --git a/frontend/src/ts/test/words-generator.ts b/frontend/src/ts/test/words-generator.ts index f2f0a0448729..831ee0636aa2 100644 --- a/frontend/src/ts/test/words-generator.ts +++ b/frontend/src/ts/test/words-generator.ts @@ -330,7 +330,11 @@ function getFunboxWordsFrequency(): FunboxWordsFrequency | undefined { } async function getFunboxSection(): Promise { - const ret = []; + const ret: string[] = []; + + if (skipFunboxSection) { + return ret; + } const funbox = findSingleActiveFunboxWithFunction("pullSection"); @@ -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[]; @@ -614,7 +619,9 @@ let previousRandomQuote: QuoteWithTextSplit | null = null; export async function generateWords( language: LanguageObject, + options?: { skipFunboxSection?: boolean }, ): Promise { + skipFunboxSection = options?.skipFunboxSection ?? false; if (!isRepeated()) { previousGetNextWordReturns = []; } @@ -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);