|
| 1 | +import { describe, it, expect, vi, beforeEach, afterAll } from "vitest"; |
| 2 | +import { |
| 3 | + checkIfFailedDueToMinBurst, |
| 4 | + checkIfFailedDueToDifficulty, |
| 5 | + checkIfFinished, |
| 6 | +} from "../../../src/ts/input/helpers/fail-or-finish"; |
| 7 | +import { __testing } from "../../../src/ts/config"; |
| 8 | +import * as Misc from "../../../src/ts/utils/misc"; |
| 9 | +import * as TestLogic from "../../../src/ts/test/test-logic"; |
| 10 | +import * as Strings from "../../../src/ts/utils/strings"; |
| 11 | + |
| 12 | +const { replaceConfig } = __testing; |
| 13 | + |
| 14 | +vi.mock("../../../src/ts/utils/misc", async (importOriginal) => { |
| 15 | + const actual = await importOriginal< |
| 16 | + typeof import("../../../src/ts/utils/misc") |
| 17 | + >(); |
| 18 | + return { |
| 19 | + ...actual, |
| 20 | + whorf: vi.fn(), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +vi.mock("../../../src/ts/test/test-logic", () => ({ |
| 25 | + areAllTestWordsGenerated: vi.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock("../../../src/ts/utils/strings", () => ({ |
| 29 | + isSpace: vi.fn(), |
| 30 | +})); |
| 31 | + |
| 32 | +describe("checkIfFailedDueToMinBurst", () => { |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + replaceConfig({ |
| 36 | + minBurst: "off", |
| 37 | + mode: "time", |
| 38 | + minBurstCustomSpeed: 100, |
| 39 | + }); |
| 40 | + (Misc.whorf as any).mockReturnValue(0); |
| 41 | + (TestLogic.areAllTestWordsGenerated as any).mockReturnValue(true); |
| 42 | + }); |
| 43 | + |
| 44 | + afterAll(() => { |
| 45 | + replaceConfig({}); |
| 46 | + }); |
| 47 | + |
| 48 | + it.each([ |
| 49 | + { |
| 50 | + desc: "returns false if minBurst is off", |
| 51 | + config: { minBurst: "off" }, |
| 52 | + lastBurst: 50, |
| 53 | + expected: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + desc: "returns false if lastBurst is null", |
| 57 | + config: { minBurst: "fixed" }, |
| 58 | + lastBurst: null, |
| 59 | + expected: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + desc: "returns true if fixed burst is too slow", |
| 63 | + config: { minBurst: "fixed", minBurstCustomSpeed: 100 }, |
| 64 | + lastBurst: 99, |
| 65 | + expected: true, |
| 66 | + }, |
| 67 | + { |
| 68 | + desc: "returns false if fixed burst is fast enough", |
| 69 | + config: { minBurst: "fixed", minBurstCustomSpeed: 100 }, |
| 70 | + lastBurst: 100, |
| 71 | + expected: false, |
| 72 | + }, |
| 73 | + { |
| 74 | + desc: "returns true if flex burst is too slow", |
| 75 | + config: { minBurst: "flex", minBurstCustomSpeed: 100 }, |
| 76 | + lastBurst: 49, |
| 77 | + whorfRet: 50, |
| 78 | + expected: true, |
| 79 | + }, |
| 80 | + { |
| 81 | + desc: "returns false if flex burst is fast enough", |
| 82 | + config: { minBurst: "flex", minBurstCustomSpeed: 100 }, |
| 83 | + lastBurst: 50, |
| 84 | + whorfRet: 50, |
| 85 | + expected: false, |
| 86 | + }, |
| 87 | + ])("$desc", ({ config, lastBurst, whorfRet, expected }) => { |
| 88 | + replaceConfig(config as any); |
| 89 | + if (whorfRet !== undefined) { |
| 90 | + (Misc.whorf as any).mockReturnValue(whorfRet); |
| 91 | + } |
| 92 | + |
| 93 | + const result = checkIfFailedDueToMinBurst({ |
| 94 | + testInputWithData: "test", |
| 95 | + currentWord: "test", |
| 96 | + lastBurst, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(result).toBe(expected); |
| 100 | + }); |
| 101 | + |
| 102 | + it("uses correct length for whorf calculation in zen mode", () => { |
| 103 | + replaceConfig({ minBurst: "flex", mode: "zen", minBurstCustomSpeed: 100 }); |
| 104 | + checkIfFailedDueToMinBurst({ |
| 105 | + testInputWithData: "zeninput", |
| 106 | + currentWord: "ignored", |
| 107 | + lastBurst: 50, |
| 108 | + }); |
| 109 | + expect(Misc.whorf).toHaveBeenCalledWith(100, 8); |
| 110 | + }); |
| 111 | + |
| 112 | + it("uses correct length for whorf calculation in normal mode", () => { |
| 113 | + replaceConfig({ minBurst: "flex", mode: "time", minBurstCustomSpeed: 100 }); |
| 114 | + checkIfFailedDueToMinBurst({ |
| 115 | + testInputWithData: "input", |
| 116 | + currentWord: "target", |
| 117 | + lastBurst: 50, |
| 118 | + }); |
| 119 | + expect(Misc.whorf).toHaveBeenCalledWith(100, 6); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("checkIfFailedDueToDifficulty", () => { |
| 124 | + beforeEach(() => { |
| 125 | + replaceConfig({ |
| 126 | + mode: "time", |
| 127 | + difficulty: "normal", |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + afterAll(() => { |
| 132 | + replaceConfig({}); |
| 133 | + }); |
| 134 | + |
| 135 | + it.each([ |
| 136 | + { |
| 137 | + desc: "zen mode, master - never fails", |
| 138 | + config: { mode: "zen", difficulty: "master" }, |
| 139 | + correct: false, |
| 140 | + spaceOrNewline: true, |
| 141 | + input: "hello", |
| 142 | + expected: false, |
| 143 | + }, |
| 144 | + { |
| 145 | + desc: "zen mode - never fails", |
| 146 | + config: { mode: "zen", difficulty: "normal" }, |
| 147 | + correct: false, |
| 148 | + spaceOrNewline: true, |
| 149 | + input: "hello", |
| 150 | + expected: false, |
| 151 | + }, |
| 152 | + // |
| 153 | + { |
| 154 | + desc: "normal typing incorrect- never fails", |
| 155 | + config: { difficulty: "normal" }, |
| 156 | + correct: false, |
| 157 | + spaceOrNewline: false, |
| 158 | + input: "hello", |
| 159 | + expected: false, |
| 160 | + }, |
| 161 | + { |
| 162 | + desc: "normal typing space incorrect - never fails", |
| 163 | + config: { difficulty: "normal" }, |
| 164 | + correct: false, |
| 165 | + spaceOrNewline: true, |
| 166 | + input: "hello", |
| 167 | + expected: false, |
| 168 | + }, |
| 169 | + { |
| 170 | + desc: "normal typing correct - never fails", |
| 171 | + config: { difficulty: "normal" }, |
| 172 | + correct: true, |
| 173 | + spaceOrNewline: false, |
| 174 | + input: "hello", |
| 175 | + expected: false, |
| 176 | + }, |
| 177 | + { |
| 178 | + desc: "normal typing space correct - never fails", |
| 179 | + config: { difficulty: "normal" }, |
| 180 | + correct: true, |
| 181 | + spaceOrNewline: true, |
| 182 | + input: "hello", |
| 183 | + expected: false, |
| 184 | + }, |
| 185 | + // |
| 186 | + { |
| 187 | + desc: "expert - fail if incorrect space", |
| 188 | + config: { difficulty: "expert" }, |
| 189 | + correct: false, |
| 190 | + spaceOrNewline: true, |
| 191 | + input: "he", |
| 192 | + expected: true, |
| 193 | + }, |
| 194 | + { |
| 195 | + desc: "expert - dont fail if space is the first character", |
| 196 | + config: { difficulty: "expert" }, |
| 197 | + correct: false, |
| 198 | + spaceOrNewline: true, |
| 199 | + input: " ", |
| 200 | + expected: false, |
| 201 | + }, |
| 202 | + { |
| 203 | + desc: "expert: - dont fail if just typing", |
| 204 | + config: { difficulty: "expert" }, |
| 205 | + correct: false, |
| 206 | + spaceOrNewline: false, |
| 207 | + input: "h", |
| 208 | + expected: false, |
| 209 | + }, |
| 210 | + { |
| 211 | + desc: "expert: - dont fail if just typing", |
| 212 | + config: { difficulty: "expert" }, |
| 213 | + correct: true, |
| 214 | + spaceOrNewline: false, |
| 215 | + input: "h", |
| 216 | + expected: false, |
| 217 | + }, |
| 218 | + // |
| 219 | + { |
| 220 | + desc: "master - fail if incorrect char", |
| 221 | + config: { difficulty: "master" }, |
| 222 | + correct: false, |
| 223 | + spaceOrNewline: false, |
| 224 | + input: "h", |
| 225 | + expected: true, |
| 226 | + }, |
| 227 | + { |
| 228 | + desc: "master - fail if incorrect first space", |
| 229 | + config: { difficulty: "master" }, |
| 230 | + correct: true, |
| 231 | + spaceOrNewline: true, |
| 232 | + input: " ", |
| 233 | + expected: false, |
| 234 | + }, |
| 235 | + { |
| 236 | + desc: "master - dont fail if correct char", |
| 237 | + config: { difficulty: "master" }, |
| 238 | + correct: true, |
| 239 | + spaceOrNewline: false, |
| 240 | + input: "a", |
| 241 | + expected: false, |
| 242 | + }, |
| 243 | + { |
| 244 | + desc: "master - dont fail if correct space", |
| 245 | + config: { difficulty: "master" }, |
| 246 | + correct: true, |
| 247 | + spaceOrNewline: true, |
| 248 | + input: " ", |
| 249 | + expected: false, |
| 250 | + }, |
| 251 | + ])("$desc", ({ config, correct, spaceOrNewline, input, expected }) => { |
| 252 | + replaceConfig(config as any); |
| 253 | + const result = checkIfFailedDueToDifficulty({ |
| 254 | + testInputWithData: input, |
| 255 | + correct, |
| 256 | + spaceOrNewline, |
| 257 | + }); |
| 258 | + expect(result).toBe(expected); |
| 259 | + }); |
| 260 | +}); |
| 261 | + |
| 262 | +describe("checkIfFinished", () => { |
| 263 | + beforeEach(() => { |
| 264 | + vi.clearAllMocks(); |
| 265 | + replaceConfig({ |
| 266 | + quickEnd: false, |
| 267 | + stopOnError: "off", |
| 268 | + }); |
| 269 | + (Strings.isSpace as any).mockReturnValue(false); |
| 270 | + (TestLogic.areAllTestWordsGenerated as any).mockReturnValue(true); |
| 271 | + }); |
| 272 | + |
| 273 | + afterAll(() => { |
| 274 | + replaceConfig({}); |
| 275 | + }); |
| 276 | + |
| 277 | + it.each([ |
| 278 | + { |
| 279 | + desc: "false if not all words typed", |
| 280 | + allWordsTyped: false, |
| 281 | + testInputWithData: "word", |
| 282 | + currentWord: "word", |
| 283 | + expected: false, |
| 284 | + }, |
| 285 | + { |
| 286 | + desc: "false if not all words generated, but on the last word", |
| 287 | + allWordsGenerated: false, |
| 288 | + allWordsTyped: true, |
| 289 | + testInputWithData: "word", |
| 290 | + currentWord: "word", |
| 291 | + expected: false, |
| 292 | + }, |
| 293 | + { |
| 294 | + desc: "true if last word is correct", |
| 295 | + allWordsTyped: true, |
| 296 | + testInputWithData: "word", |
| 297 | + currentWord: "word", |
| 298 | + expected: true, |
| 299 | + }, |
| 300 | + { |
| 301 | + desc: "true if quickEnd enabled and lengths match", |
| 302 | + allWordsTyped: true, |
| 303 | + testInputWithData: "asdf", |
| 304 | + currentWord: "word", |
| 305 | + config: { quickEnd: true }, |
| 306 | + expected: true, |
| 307 | + }, |
| 308 | + { |
| 309 | + desc: "false if quickEnd disabled and lengths match", |
| 310 | + allWordsTyped: true, |
| 311 | + testInputWithData: "asdf", |
| 312 | + currentWord: "word", |
| 313 | + config: { quickEnd: false }, |
| 314 | + expected: false, |
| 315 | + }, |
| 316 | + { |
| 317 | + desc: "true if space on the last word", |
| 318 | + allWordsTyped: true, |
| 319 | + testInputWithData: "wo ", |
| 320 | + currentWord: "word", |
| 321 | + shouldGoToNextWord: true, |
| 322 | + expected: true, |
| 323 | + }, |
| 324 | + { |
| 325 | + desc: "false if still typing, quickend disabled", |
| 326 | + allWordsTyped: true, |
| 327 | + testInputWithData: "wordwordword", |
| 328 | + currentWord: "word", |
| 329 | + expected: false, |
| 330 | + }, |
| 331 | + ] as { |
| 332 | + desc: string; |
| 333 | + allWordsTyped: boolean; |
| 334 | + allWordsGenerated?: boolean; |
| 335 | + shouldGoToNextWord: boolean; |
| 336 | + testInputWithData: string; |
| 337 | + currentWord: string; |
| 338 | + config?: Record<string, any>; |
| 339 | + isSpace?: boolean; |
| 340 | + expected: boolean; |
| 341 | + }[])( |
| 342 | + "$desc", |
| 343 | + ({ |
| 344 | + allWordsTyped, |
| 345 | + allWordsGenerated, |
| 346 | + shouldGoToNextWord, |
| 347 | + testInputWithData, |
| 348 | + currentWord, |
| 349 | + config, |
| 350 | + expected, |
| 351 | + }) => { |
| 352 | + if (config) replaceConfig(config as any); |
| 353 | + |
| 354 | + const result = checkIfFinished({ |
| 355 | + shouldGoToNextWord, |
| 356 | + testInputWithData, |
| 357 | + currentWord, |
| 358 | + allWordsTyped, |
| 359 | + allWordsGenerated: allWordsGenerated ?? true, |
| 360 | + }); |
| 361 | + |
| 362 | + expect(result).toBe(expected); |
| 363 | + } |
| 364 | + ); |
| 365 | +}); |
0 commit comments