From b5124ae3ce8297a49fab42f4318fb1c3c9c77970 Mon Sep 17 00:00:00 2001 From: Bertie690 <136088738+Bertie690@users.noreply.github.com> Date: Sat, 6 Sep 2025 18:43:26 -0400 Subject: [PATCH 1/6] [Misc/Docs] Fix Typedoc workflow to not break the "go to main" link (#6502) * Fixed missing `img src=` --- typedoc-plugins/typedoc-plugin-rename-svg.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/typedoc-plugins/typedoc-plugin-rename-svg.js b/typedoc-plugins/typedoc-plugin-rename-svg.js index 5fda4ee3c6d..307206d6006 100644 --- a/typedoc-plugins/typedoc-plugin-rename-svg.js +++ b/typedoc-plugins/typedoc-plugin-rename-svg.js @@ -19,15 +19,10 @@ export function load(app) { app.renderer.on(Renderer.EVENT_END_PAGE, page => { if (page.pageKind === PageKind.Index && page.contents) { page.contents = page.contents - // Replace links to the beta documentation site with the current ref name + // Replace the SVG to the beta documentation site with the current ref name .replace( - /href="(.*pagefaultgames.github.io\/pokerogue\/).*?"/, // formatting - `href="$1/${process.env.REF_NAME}"`, - ) - // Replace the link to Beta's coverage SVG with the SVG file for the branch in question. - .replace( - /img src=".*?coverage.svg/, // formatting - `img src="coverage.svg"`, + /^ Date: Sun, 7 Sep 2025 08:16:43 +0900 Subject: [PATCH 2/6] [UI] Add cyclic navigation in settings menu for LEFT/RIGHT buttons (#6404) * feat: add cyclic navigation to settings menu * review > refactor: use Phaser.Math.Wrap instead the ternary operator --- .../abstract-control-settings-ui-handler.ts | 18 +++++++++++++---- .../settings/abstract-settings-ui-handler.ts | 20 +++++++++++-------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/ui/settings/abstract-control-settings-ui-handler.ts b/src/ui/settings/abstract-control-settings-ui-handler.ts index c08f1570b75..17812785d1e 100644 --- a/src/ui/settings/abstract-control-settings-ui-handler.ts +++ b/src/ui/settings/abstract-control-settings-ui-handler.ts @@ -544,8 +544,13 @@ export abstract class AbstractControlSettingsUiHandler extends UiHandler { } if (this.settingBlacklisted.includes(setting) || setting.includes("BUTTON_")) { success = false; - } else if (this.optionCursors[cursor]) { - success = this.setOptionCursor(cursor, this.optionCursors[cursor] - 1, true); + } else { + // Cycle to the rightmost position when at the leftmost, otherwise move left + success = this.setOptionCursor( + cursor, + Phaser.Math.Wrap(this.optionCursors[cursor] - 1, 0, this.optionValueLabels[cursor].length), + true, + ); } break; case Button.RIGHT: // Move selection right within the current option set. @@ -554,8 +559,13 @@ export abstract class AbstractControlSettingsUiHandler extends UiHandler { } if (this.settingBlacklisted.includes(setting) || setting.includes("BUTTON_")) { success = false; - } else if (this.optionCursors[cursor] < this.optionValueLabels[cursor].length - 1) { - success = this.setOptionCursor(cursor, this.optionCursors[cursor] + 1, true); + } else { + // Cycle to the leftmost position when at the rightmost, otherwise move right + success = this.setOptionCursor( + cursor, + Phaser.Math.Wrap(this.optionCursors[cursor] + 1, 0, this.optionValueLabels[cursor].length), + true, + ); } break; case Button.CYCLE_FORM: diff --git a/src/ui/settings/abstract-settings-ui-handler.ts b/src/ui/settings/abstract-settings-ui-handler.ts index ae1bb40dbeb..85e93bd8e2e 100644 --- a/src/ui/settings/abstract-settings-ui-handler.ts +++ b/src/ui/settings/abstract-settings-ui-handler.ts @@ -318,16 +318,20 @@ export class AbstractSettingsUiHandler extends MessageUiHandler { } break; case Button.LEFT: - if (this.optionCursors[cursor]) { - // Moves the option cursor left, if possible. - success = this.setOptionCursor(cursor, this.optionCursors[cursor] - 1, true); - } + // Cycle to the rightmost position when at the leftmost, otherwise move left + success = this.setOptionCursor( + cursor, + Phaser.Math.Wrap(this.optionCursors[cursor] - 1, 0, this.optionValueLabels[cursor].length), + true, + ); break; case Button.RIGHT: - // Moves the option cursor right, if possible. - if (this.optionCursors[cursor] < this.optionValueLabels[cursor].length - 1) { - success = this.setOptionCursor(cursor, this.optionCursors[cursor] + 1, true); - } + // Cycle to the leftmost position when at the rightmost, otherwise move right + success = this.setOptionCursor( + cursor, + Phaser.Math.Wrap(this.optionCursors[cursor] + 1, 0, this.optionValueLabels[cursor].length), + true, + ); break; case Button.CYCLE_FORM: case Button.CYCLE_SHINY: From 9815c5eebffca0e0c6016782d581c130cae47102 Mon Sep 17 00:00:00 2001 From: Bertie690 <136088738+Bertie690@users.noreply.github.com> Date: Sun, 7 Sep 2025 10:18:47 -0400 Subject: [PATCH 3/6] [Test] Revamped `MockConsoleLog` with color support (#6218) * Added mock console and fixed up many many things * Cleaned up handling of colors and such * Added minor comment * Fix Focus Punch test * Fix typo * Remove redundant comment * Update vitest.setup.ts Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> * Added color map inside new folder * Made constants not object bc i was told to * Update src/constants/colors.ts * Removed all moves init check * Removed import * Fixed up some stuff + added aquamarine color to settings helper * Added logging for test end * Removed intentionally failing test * Fixed console log to use inheritance to not override vitest's wrapping * Added a custom Vitest reporter to not log the test name every 2 lines * Moved coloration to a hook to prevent misplacing things * Fixed import issue by copypasting vitest soure * Removed intentionally failing test look i need to check that `test:silent` works on github ok * Added REUSE annotations to copied parts of source * Fixed import issue --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> --- package.json | 1 + pnpm-lock.yaml | 3 + public/locales | 2 +- src/constants/colors.ts | 23 ++ src/phase-manager.ts | 2 +- src/phases/move-phase.ts | 6 +- test/abilities/cud-chew.test.ts | 2 +- test/moves/focus-punch.test.ts | 6 +- test/test-utils/game-wrapper.ts | 19 +- test/test-utils/helpers/overrides-helper.ts | 5 +- test/test-utils/helpers/settings-helper.ts | 4 +- test/test-utils/mocks/mock-console-log.ts | 80 ------- .../mocks/mock-console/color-map.json | 150 +++++++++++++ .../mocks/mock-console/infer-color.ts | 61 +++++ .../mocks/mock-console/mock-console.ts | 211 ++++++++++++++++++ .../mocks/mocks-container/mock-text.ts | 22 +- .../reporters/custom-default-reporter.ts | 62 +++++ test/test-utils/setup/test-end-log.ts | 113 ++++++++++ test/test-utils/test-file-initialization.ts | 25 ++- test/test-utils/text-interceptor.ts | 60 ++--- test/vitest.setup.ts | 36 ++- typedoc.config.js | 8 +- vitest.config.ts | 13 +- 23 files changed, 747 insertions(+), 167 deletions(-) create mode 100644 src/constants/colors.ts delete mode 100644 test/test-utils/mocks/mock-console-log.ts create mode 100644 test/test-utils/mocks/mock-console/color-map.json create mode 100644 test/test-utils/mocks/mock-console/infer-color.ts create mode 100644 test/test-utils/mocks/mock-console/mock-console.ts create mode 100644 test/test-utils/reporters/custom-default-reporter.ts create mode 100644 test/test-utils/setup/test-end-log.ts diff --git a/package.json b/package.json index 0620cf6a88c..f6097b8ccb9 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "@types/node": "^22.16.5", "@vitest/coverage-istanbul": "^3.2.4", "@vitest/expect": "^3.2.4", + "@vitest/utils": "^3.2.4", "chalk": "^5.4.1", "dependency-cruiser": "^16.10.4", "inquirer": "^12.8.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 089689818ac..be3e683f71c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -63,6 +63,9 @@ importers: '@vitest/expect': specifier: ^3.2.4 version: 3.2.4 + '@vitest/utils': + specifier: ^3.2.4 + version: 3.2.4 chalk: specifier: ^5.4.1 version: 5.4.1 diff --git a/public/locales b/public/locales index 2686cd3edc0..102cbdcd924 160000 --- a/public/locales +++ b/public/locales @@ -1 +1 @@ -Subproject commit 2686cd3edc0bd2c7a7f12cc54c00c109e51a48d7 +Subproject commit 102cbdcd924e2a7cdc7eab64d1ce79f6ec7604ff diff --git a/src/constants/colors.ts b/src/constants/colors.ts new file mode 100644 index 00000000000..e4d740addff --- /dev/null +++ b/src/constants/colors.ts @@ -0,0 +1,23 @@ +/** + * @module + * A big file storing colors used in logging. + * Minified by Terser during production builds, so has no overhead. + */ + +// Colors used in prod +export const PHASE_START_COLOR = "green" as const; +export const MOVE_COLOR = "RebeccaPurple" as const; + +// Colors used for testing code +export const NEW_TURN_COLOR = "#ffad00ff" as const; +export const UI_MSG_COLOR = "#009dffff" as const; +export const OVERRIDES_COLOR = "#b0b01eff" as const; +export const SETTINGS_COLOR = "#008844ff" as const; + +// Colors used for Vitest-related test utils +export const TEST_NAME_COLOR = "#008886ff" as const; +export const VITEST_PINK_COLOR = "#c162de" as const; + +// Mock console log stuff +export const TRACE_COLOR = "#b700ff" as const; +export const DEBUG_COLOR = "#874600ff" as const; diff --git a/src/phase-manager.ts b/src/phase-manager.ts index 281ac8bd671..68b7d74293b 100644 --- a/src/phase-manager.ts +++ b/src/phase-manager.ts @@ -392,7 +392,7 @@ export class PhaseManager { * Helper method to start and log the current phase. */ private startCurrentPhase(): void { - console.log(`%cStart Phase ${this.currentPhase.phaseName}`, "color:green;"); + console.log(`%cStart Phase ${this.currentPhase.phaseName}`, "color:${PHASE_START_COLOR};"); this.currentPhase.start(); } diff --git a/src/phases/move-phase.ts b/src/phases/move-phase.ts index 9a8e509e302..6587597a0d9 100644 --- a/src/phases/move-phase.ts +++ b/src/phases/move-phase.ts @@ -1,4 +1,5 @@ import { applyAbAttrs } from "#abilities/apply-ab-attrs"; +import { MOVE_COLOR } from "#app/constants/colors"; import { globalScene } from "#app/global-scene"; import { getPokemonNameWithAffix } from "#app/messages"; import Overrides from "#app/overrides"; @@ -118,7 +119,10 @@ export class MovePhase extends BattlePhase { public start(): void { super.start(); - console.log(MoveId[this.move.moveId], enumValueToKey(MoveUseMode, this.useMode)); + console.log( + `%cMove: ${MoveId[this.move.moveId]}\nUse Mode: ${enumValueToKey(MoveUseMode, this.useMode)}`, + `color:${MOVE_COLOR}`, + ); // Check if move is unusable (e.g. running out of PP due to a mid-turn Spite // or the user no longer being on field), ending the phase early if not. diff --git a/test/abilities/cud-chew.test.ts b/test/abilities/cud-chew.test.ts index f68141096eb..ae3b4ad8765 100644 --- a/test/abilities/cud-chew.test.ts +++ b/test/abilities/cud-chew.test.ts @@ -99,7 +99,7 @@ describe("Abilities - Cud Chew", () => { expect(abDisplaySpy.mock.calls[1][2]).toBe(false); // should display messgae - expect(game.textInterceptor.getLatestMessage()).toBe( + expect(game.textInterceptor.logs).toContain( i18next.t("battle:hpIsFull", { pokemonName: getPokemonNameWithAffix(farigiraf), }), diff --git a/test/moves/focus-punch.test.ts b/test/moves/focus-punch.test.ts index 9a76dbec0db..d7b40569aaa 100644 --- a/test/moves/focus-punch.test.ts +++ b/test/moves/focus-punch.test.ts @@ -9,7 +9,7 @@ import { TurnStartPhase } from "#phases/turn-start-phase"; import { GameManager } from "#test/test-utils/game-manager"; import i18next from "i18next"; import Phaser from "phaser"; -import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; describe("Moves - Focus Punch", () => { let phaserGame: Phaser.Game; @@ -125,8 +125,8 @@ describe("Moves - Focus Punch", () => { game.move.select(MoveId.FOCUS_PUNCH); await game.phaseInterceptor.to("MoveEndPhase", true); await game.phaseInterceptor.to("MessagePhase", false); - const consoleSpy = vi.spyOn(console, "log"); await game.phaseInterceptor.to("MoveEndPhase", true); - expect(consoleSpy).nthCalledWith(1, i18next.t("moveTriggers:lostFocus", { pokemonName: "Charizard" })); + expect(game.textInterceptor.logs).toContain(i18next.t("moveTriggers:lostFocus", { pokemonName: "Charizard" })); + expect(game.textInterceptor.logs).not.toContain(i18next.t("battle:attackFailed")); }); }); diff --git a/test/test-utils/game-wrapper.ts b/test/test-utils/game-wrapper.ts index 1a906bf8492..d18ea9301ea 100644 --- a/test/test-utils/game-wrapper.ts +++ b/test/test-utils/game-wrapper.ts @@ -6,17 +6,13 @@ import * as bypassLoginModule from "#app/global-vars/bypass-login"; import { MoveAnim } from "#data/battle-anims"; import { Pokemon } from "#field/pokemon"; import { version } from "#package.json"; -import { blobToString } from "#test/test-utils/game-manager-utils"; import { MockClock } from "#test/test-utils/mocks/mock-clock"; -import { MockFetch } from "#test/test-utils/mocks/mock-fetch"; import { MockGameObjectCreator } from "#test/test-utils/mocks/mock-game-object-creator"; import { MockLoader } from "#test/test-utils/mocks/mock-loader"; import { MockTextureManager } from "#test/test-utils/mocks/mock-texture-manager"; import { MockTimedEventManager } from "#test/test-utils/mocks/mock-timed-event-manager"; import { MockContainer } from "#test/test-utils/mocks/mocks-container/mock-container"; import { PokedexMonContainer } from "#ui/pokedex-mon-container"; -import { sessionIdKey } from "#utils/common"; -import { setCookie } from "#utils/cookies"; import fs from "node:fs"; import Phaser from "phaser"; import { vi } from "vitest"; @@ -28,20 +24,6 @@ const GamepadPlugin = Phaser.Input.Gamepad.GamepadPlugin; const EventEmitter = Phaser.Events.EventEmitter; const UpdateList = Phaser.GameObjects.UpdateList; -window.URL.createObjectURL = (blob: Blob) => { - blobToString(blob).then((data: string) => { - localStorage.setItem("toExport", data); - }); - return null; -}; -navigator.getGamepads = () => []; -global.fetch = vi.fn(MockFetch); -setCookie(sessionIdKey, "fake_token"); - -window.matchMedia = () => ({ - matches: false, -}); - export class GameWrapper { public game: Phaser.Game; public scene: BattleScene; @@ -99,6 +81,7 @@ export class GameWrapper { removeAll: () => null, }; + // TODO: Can't we just turn on `noAudio` in audio config? this.scene.sound = { play: () => null, pause: () => null, diff --git a/test/test-utils/helpers/overrides-helper.ts b/test/test-utils/helpers/overrides-helper.ts index 93b89688935..07ea1ea3d09 100644 --- a/test/test-utils/helpers/overrides-helper.ts +++ b/test/test-utils/helpers/overrides-helper.ts @@ -1,7 +1,9 @@ /** biome-ignore-start lint/correctness/noUnusedImports: tsdoc imports */ import type { NewArenaEvent } from "#events/battle-scene"; + /** biome-ignore-end lint/correctness/noUnusedImports: tsdoc imports */ +import { OVERRIDES_COLOR } from "#app/constants/colors"; import type { BattleStyle, RandomTrainerOverride } from "#app/overrides"; import Overrides from "#app/overrides"; import { AbilityId } from "#enums/ability-id"; @@ -19,6 +21,7 @@ import type { ModifierOverride } from "#modifiers/modifier-type"; import type { Variant } from "#sprites/variant"; import { GameManagerHelper } from "#test/test-utils/helpers/game-manager-helper"; import { coerceArray, shiftCharCodes } from "#utils/common"; +import chalk from "chalk"; import { vi } from "vitest"; /** @@ -665,6 +668,6 @@ export class OverridesHelper extends GameManagerHelper { } private log(...params: any[]) { - console.log("Overrides:", ...params); + console.log(chalk.hex(OVERRIDES_COLOR)(...params)); } } diff --git a/test/test-utils/helpers/settings-helper.ts b/test/test-utils/helpers/settings-helper.ts index a26aa2de33c..46ac74b83dc 100644 --- a/test/test-utils/helpers/settings-helper.ts +++ b/test/test-utils/helpers/settings-helper.ts @@ -1,7 +1,9 @@ +import { SETTINGS_COLOR } from "#app/constants/colors"; import { BattleStyle } from "#enums/battle-style"; import { ExpGainsSpeed } from "#enums/exp-gains-speed"; import { PlayerGender } from "#enums/player-gender"; import { GameManagerHelper } from "#test/test-utils/helpers/game-manager-helper"; +import chalk from "chalk"; /** * Helper to handle settings for tests @@ -49,6 +51,6 @@ export class SettingsHelper extends GameManagerHelper { } private log(...params: any[]) { - console.log("Settings:", ...params); + console.log(chalk.hex(SETTINGS_COLOR)(...params)); } } diff --git a/test/test-utils/mocks/mock-console-log.ts b/test/test-utils/mocks/mock-console-log.ts deleted file mode 100644 index f54d41fea3e..00000000000 --- a/test/test-utils/mocks/mock-console-log.ts +++ /dev/null @@ -1,80 +0,0 @@ -const originalLog = console.log; -const originalError = console.error; -const originalDebug = console.debug; -const originalWarn = console.warn; - -const blacklist = ["Phaser", "variant icon does not exist", 'Texture "%s" not found']; -const whitelist = ["Phase"]; - -export class MockConsoleLog { - constructor( - private logDisabled = false, - private phaseText = false, - ) {} - private logs: any[] = []; - private notified: any[] = []; - - public log(...args) { - const argsStr = this.getStr(args); - this.logs.push(argsStr); - if (this.logDisabled && !this.phaseText) { - return; - } - if ((this.phaseText && !whitelist.some(b => argsStr.includes(b))) || blacklist.some(b => argsStr.includes(b))) { - return; - } - originalLog(args); - } - public error(...args) { - const argsStr = this.getStr(args); - this.logs.push(argsStr); - originalError(args); // Appelle le console.error originel - } - public debug(...args) { - const argsStr = this.getStr(args); - this.logs.push(argsStr); - if (this.logDisabled && !this.phaseText) { - return; - } - if (!whitelist.some(b => argsStr.includes(b)) || blacklist.some(b => argsStr.includes(b))) { - return; - } - originalDebug(args); - } - warn(...args) { - const argsStr = this.getStr(args); - this.logs.push(args); - if (this.logDisabled && !this.phaseText) { - return; - } - if (!whitelist.some(b => argsStr.includes(b)) || blacklist.some(b => argsStr.includes(b))) { - return; - } - originalWarn(args); - } - notify(msg) { - originalLog(msg); - this.notified.push(msg); - } - getLogs() { - return this.logs; - } - clearLogs() { - this.logs = []; - } - getStr(...args) { - return args - .map(arg => { - if (typeof arg === "object" && arg !== null) { - // Handle objects including arrays - return JSON.stringify(arg, (_key, value) => (typeof value === "bigint" ? value.toString() : value)); - } - if (typeof arg === "bigint") { - // Handle BigInt values - return arg.toString(); - } - return arg.toString(); - }) - .join(";"); - } -} diff --git a/test/test-utils/mocks/mock-console/color-map.json b/test/test-utils/mocks/mock-console/color-map.json new file mode 100644 index 00000000000..ded83e889b0 --- /dev/null +++ b/test/test-utils/mocks/mock-console/color-map.json @@ -0,0 +1,150 @@ +{ + "AliceBlue": "f0f8ff", + "AntiqueWhite": "faebd7", + "Aqua": "00ffff", + "Aquamarine": "7fffd4", + "Azure": "f0ffff", + "Beige": "f5f5dc", + "Bisque": "ffe4c4", + "Black": "000000", + "BlanchedAlmond": "ffebcd", + "Blue": "0000ff", + "BlueViolet": "8a2be2", + "Brown": "a52a2a", + "BurlyWood": "deb887", + "CadetBlue": "5f9ea0", + "Chartreuse": "7fff00", + "Chocolate": "d2691e", + "Coral": "ff7f50", + "CornflowerBlue": "6495ed", + "Cornsilk": "fff8dc", + "Crimson": "dc143c", + "Cyan": "00ffff", + "DarkBlue": "00008b", + "DarkCyan": "008b8b", + "DarkGoldenRod": "b8860b", + "DarkGray": "a9a9a9", + "DarkGrey": "a9a9a9", + "DarkGreen": "006400", + "DarkKhaki": "bdb76b", + "DarkMagenta": "8b008b", + "DarkOliveGreen": "556b2f", + "DarkOrange": "ff8c00", + "DarkOrchid": "9932cc", + "DarkRed": "8b0000", + "DarkSalmon": "e9967a", + "DarkSeaGreen": "8fbc8f", + "DarkSlateBlue": "483d8b", + "DarkSlateGray": "2f4f4f", + "DarkSlateGrey": "2f4f4f", + "DarkTurquoise": "00ced1", + "DarkViolet": "9400d3", + "DeepPink": "ff1493", + "DeepSkyBlue": "00bfff", + "DimGray": "696969", + "DimGrey": "696969", + "DodgerBlue": "1e90ff", + "FireBrick": "b22222", + "FloralWhite": "fffaf0", + "ForestGreen": "228b22", + "Fuchsia": "ff00ff", + "Gainsboro": "dcdcdc", + "GhostWhite": "f8f8ff", + "Gold": "ffd700", + "GoldenRod": "daa520", + "Gray": "808080", + "Grey": "808080", + "Green": "008000", + "GreenYellow": "adff2f", + "HoneyDew": "f0fff0", + "HotPink": "ff69b4", + "IndianRed": "cd5c5c", + "Indigo": "4b0082", + "Ivory": "fffff0", + "Khaki": "f0e68c", + "Lavender": "e6e6fa", + "LavenderBlush": "fff0f5", + "LawnGreen": "7cfc00", + "LemonChiffon": "fffacd", + "LightBlue": "add8e6", + "LightCoral": "f08080", + "LightCyan": "e0ffff", + "LightGoldenRodYellow": "fafad2", + "LightGray": "d3d3d3", + "LightGrey": "d3d3d3", + "LightGreen": "90ee90", + "LightPink": "ffb6c1", + "LightSalmon": "ffa07a", + "LightSeaGreen": "20b2aa", + "LightSkyBlue": "87cefa", + "LightSlateGray": "778899", + "LightSlateGrey": "778899", + "LightSteelBlue": "b0c4de", + "LightYellow": "ffffe0", + "Lime": "00ff00", + "LimeGreen": "32cd32", + "Linen": "faf0e6", + "Magenta": "ff00ff", + "Maroon": "800000", + "MediumAquaMarine": "66cdaa", + "MediumBlue": "0000cd", + "MediumOrchid": "ba55d3", + "MediumPurple": "9370db", + "MediumSeaGreen": "3cb371", + "MediumSlateBlue": "7b68ee", + "MediumSpringGreen": "00fa9a", + "MediumTurquoise": "48d1cc", + "MediumVioletRed": "c71585", + "MidnightBlue": "191970", + "MintCream": "f5fffa", + "MistyRose": "ffe4e1", + "Moccasin": "ffe4b5", + "NavajoWhite": "ffdead", + "Navy": "000080", + "OldLace": "fdf5e6", + "Olive": "808000", + "OliveDrab": "6b8e23", + "Orange": "ffa500", + "OrangeRed": "ff4500", + "Orchid": "da70d6", + "PaleGoldenRod": "eee8aa", + "PaleGreen": "98fb98", + "PaleTurquoise": "afeeee", + "PaleVioletRed": "db7093", + "PapayaWhip": "ffefd5", + "PeachPuff": "ffdab9", + "Peru": "cd853f", + "Pink": "ffc0cb", + "Plum": "dda0dd", + "PowderBlue": "b0e0e6", + "Purple": "800080", + "RebeccaPurple": "663399", + "Red": "ff0000", + "RosyBrown": "bc8f8f", + "RoyalBlue": "4169e1", + "SaddleBrown": "8b4513", + "Salmon": "fa8072", + "SandyBrown": "f4a460", + "SeaGreen": "2e8b57", + "SeaShell": "fff5ee", + "Sienna": "a0522d", + "Silver": "c0c0c0", + "SkyBlue": "87ceeb", + "SlateBlue": "6a5acd", + "SlateGray": "708090", + "SlateGrey": "708090", + "Snow": "fffafa", + "SpringGreen": "00ff7f", + "SteelBlue": "4682b4", + "Tan": "d2b48c", + "Teal": "008080", + "Thistle": "d8bfd8", + "Tomato": "ff6347", + "Turquoise": "40e0d0", + "Violet": "ee82ee", + "Wheat": "f5deb3", + "White": "ffffff", + "WhiteSmoke": "f5f5f5", + "Yellow": "ffff00", + "YellowGreen": "9acd32" +} diff --git a/test/test-utils/mocks/mock-console/infer-color.ts b/test/test-utils/mocks/mock-console/infer-color.ts new file mode 100644 index 00000000000..e01adbc4ad4 --- /dev/null +++ b/test/test-utils/mocks/mock-console/infer-color.ts @@ -0,0 +1,61 @@ +import { hslToHex } from "#utils/common"; +import chalk, { type ChalkInstance, type ForegroundColorName, foregroundColorNames } from "chalk"; +import colorMap from "./color-map.json"; + +export function inferColorFormat(data: [string, ...unknown[]]): ChalkInstance { + // Remove all CSS format strings and find the first one containing something vaguely resembling a color + data[0] = data[0].replaceAll("%c", ""); + const args = data.slice(1).filter(t => typeof t === "string"); + const color = findColorPrefix(args); + + // If the color is within Chalk's native roster, use it directly. + if ((foregroundColorNames as string[]).includes(color)) { + return chalk[color as ForegroundColorName]; + } + + // Otherwise, coerce it to hex before feeding it in. + return getColor(color); +} + +/** + * Find the first string with a "color:" CSS directive in an argument list. + * @param args - The arguments containing the color directive + * @returns The found color, or `"green"` if none were found + */ +function findColorPrefix(args: string[]): string { + for (const arg of args) { + const match = /color:\s*(.+?)(?:;|$)/g.exec(arg); + if (match === null) { + continue; + } + + return match[1]; + } + return "green"; +} + +/** + * Coerce an arbitrary CSS color string to a Chalk instance. + * @param color - The color to coerce + * @returns The Chalk color equivalent. + */ +function getColor(color: string): ChalkInstance { + if (/^#([a-z0-9]{3,4}|[a-z0-9]{6}|[a-z0-9]{8})$/i.test(color)) { + // already in hex + return chalk.hex(color); + } + + const rgbMatch = /^rgba?\((\d{1,3})%?,\s*(\d{1,3})%?,?\s*(\d{1,3})%?,\s*/i.exec(color); + if (rgbMatch) { + const [red, green, blue] = rgbMatch; + return chalk.rgb(+red, +green, +blue); + } + + const hslMatch = /^hslv?\((\d{1,3}),\s*(\d{1,3})%,\s*(\d{1,3})%\)$/i.exec(color); + if (hslMatch) { + const [hue, saturation, light] = hslMatch; + return chalk.hex(hslToHex(+hue, +saturation / 100, +light / 100)); + } + + return chalk.hex(colorMap[color] ?? "#00ff95ff"); +} diff --git a/test/test-utils/mocks/mock-console/mock-console.ts b/test/test-utils/mocks/mock-console/mock-console.ts new file mode 100644 index 00000000000..52ed0af6aa7 --- /dev/null +++ b/test/test-utils/mocks/mock-console/mock-console.ts @@ -0,0 +1,211 @@ +import { DEBUG_COLOR, NEW_TURN_COLOR, TRACE_COLOR, UI_MSG_COLOR } from "#app/constants/colors"; +import { inferColorFormat } from "#test/test-utils/mocks/mock-console/infer-color"; +import { coerceArray } from "#utils/common"; +import { type InspectOptions, inspect } from "node:util"; +import chalk, { type ChalkInstance } from "chalk"; + +// Tell chalk we support truecolor +chalk.level = 3; + +// TODO: Review this +const blacklist = [ + "variant icon does not exist", // Repetitive warnings about icons not found + 'Texture "%s" not found', // Repetitive warnings about textures not found + "type: 'Pokemon',", // Large Pokemon objects + "gameVersion: ", // Large session-data and system-data objects + "Phaser v", // Phaser version text + "Seed:", // Stuff about wave seed (we should really stop logging this shit) + "Wave Seed:", // Stuff about wave seed (we should really stop logging this shit) +] as const; +const whitelist = ["Start Phase"] as const; + +const inspectOptions: InspectOptions = { sorted: true, breakLength: 120, numericSeparator: true }; + +/** + * The {@linkcode MockConsole} is a wrapper around the global {@linkcode console} object. + * It automatically colors text and such. + */ +export class MockConsole implements Omit { + /** + * A list of warnings that are queued to be displayed after all tests in the same file are finished. + */ + private static readonly queuedWarnings: unknown[][] = []; + /** + * The original `Console` object, preserved to avoid overwriting + * Vitest's native `console.log` wrapping. + */ + private console = console; + + //#region Static Properties + + /** + * Queue a warning to be printed after all tests in the same file are finished. + */ + // TODO: Add some warnings + public static queuePostTestWarning(...data: unknown[]): void { + MockConsole.queuedWarnings.push(data); + } + + /** + * Print and reset all post-test warnings. + */ + public static printPostTestWarnings(): void { + for (const data of MockConsole.queuedWarnings) { + console.warn(...data); + } + MockConsole.queuedWarnings.splice(0); + } + + //#endregion Private Properties + + //#region Utilities + + /** + * Check whether a given set of data is in the blacklist to be barred from logging. + * @param data - The data being logged + * @returns Whether `data` is blacklisted from console logging + */ + private checkBlacklist(data: unknown[]): boolean { + const dataStr = this.getStr(data); + return !whitelist.some(b => dataStr.includes(b)) && blacklist.some(b => dataStr.includes(b)); + } + + /** + * Returns a human-readable string representation of `data`. + */ + private getStr(data: unknown): string { + return inspect(data, inspectOptions); + } + + /** + * Stringify the given data in a manner fit for logging. + * @param color - A Chalk instance or other transformation function used to transform the output, + * or `undefined` to not transform it at all. + * @param data - The data that the format should be applied to. + * @returns A stringified copy of `data` with {@linkcode color} applied to each individual argument. + * @todo Do we need to apply color to each entry or just run it through `util.format`? + */ + private format(color: ((s: unknown) => unknown) | undefined, data: unknown | unknown[]): unknown[] { + data = coerceArray(data); + color ??= a => a; + return (data as unknown[]).map(a => color(typeof a === "function" || typeof a === "object" ? this.getStr(a) : a)); + } + + //#endregion Utilities + + //#region Custom wrappers + public info(...data: unknown[]) { + return this.log(...data); + } + + public trace(...data: unknown[]) { + if (this.checkBlacklist(data)) { + return; + } + + // TODO: Figure out how to add color to the full trace text + this.console.trace(...this.format(chalk.hex(TRACE_COLOR), data)); + } + + public debug(...data: unknown[]) { + if (this.checkBlacklist(data)) { + return; + } + + this.console.debug(...this.format(chalk.hex(DEBUG_COLOR), data)); + } + + public log(...data: unknown[]): void { + if (this.checkBlacklist(data)) { + return; + } + + let formatter: ChalkInstance | undefined; + + if (data.some(d => typeof d === "string" && d.includes("color:"))) { + // Infer the color format from the arguments, then remove everything but the message. + formatter = inferColorFormat(data as [string, ...unknown[]]); + data.splice(1); + } else if (data[0] === "[UI]") { + // Cyan for UI debug messages + formatter = chalk.hex(UI_MSG_COLOR); + } else if (typeof data[0] === "string" && data[0].startsWith("=====")) { + // Orange logging for "New Turn"/etc messages + formatter = chalk.hex(NEW_TURN_COLOR); + } + + this.console.log(...this.format(formatter, data)); + } + + public warn(...data: unknown[]) { + if (this.checkBlacklist(data)) { + return; + } + + this.console.warn(...this.format(chalk.yellow, data)); + } + + public error(...data: unknown[]) { + if (this.checkBlacklist(data)) { + return; + } + + this.console.error(...this.format(chalk.redBright, data)); + } + + //#endregion Custom Wrappers + + //#region Copy-pasted Console code + // TODO: Progressively add proper coloration and support for all these methods + public dir(...args: Parameters<(typeof console)["dir"]>): ReturnType<(typeof console)["dir"]> { + return this.console.dir(...args); + } + public dirxml(...args: Parameters<(typeof console)["dirxml"]>): ReturnType<(typeof console)["dirxml"]> { + return this.console.dirxml(...args); + } + public table(...args: Parameters<(typeof console)["table"]>): ReturnType<(typeof console)["table"]> { + return this.console.table(...args); + } + public group(...args: Parameters<(typeof console)["group"]>): ReturnType<(typeof console)["group"]> { + return this.console.group(...args); + } + public groupCollapsed( + ...args: Parameters<(typeof console)["groupCollapsed"]> + ): ReturnType<(typeof console)["groupCollapsed"]> { + return this.console.groupCollapsed(...args); + } + public groupEnd(...args: Parameters<(typeof console)["groupEnd"]>): ReturnType<(typeof console)["groupEnd"]> { + return this.console.groupEnd(...args); + } + public clear(...args: Parameters<(typeof console)["clear"]>): ReturnType<(typeof console)["clear"]> { + return this.console.clear(...args); + } + public count(...args: Parameters<(typeof console)["count"]>): ReturnType<(typeof console)["count"]> { + return this.console.count(...args); + } + public countReset(...args: Parameters<(typeof console)["countReset"]>): ReturnType<(typeof console)["countReset"]> { + return this.console.countReset(...args); + } + public assert(...args: Parameters<(typeof console)["assert"]>): ReturnType<(typeof console)["assert"]> { + return this.console.assert(...args); + } + public profile(...args: Parameters<(typeof console)["profile"]>): ReturnType<(typeof console)["profile"]> { + return this.console.profile(...args); + } + public profileEnd(...args: Parameters<(typeof console)["profileEnd"]>): ReturnType<(typeof console)["profileEnd"]> { + return this.console.profileEnd(...args); + } + public time(...args: Parameters<(typeof console)["time"]>): ReturnType<(typeof console)["time"]> { + return this.console.time(...args); + } + public timeLog(...args: Parameters<(typeof console)["timeLog"]>): ReturnType<(typeof console)["timeLog"]> { + return this.console.timeLog(...args); + } + public timeEnd(...args: Parameters<(typeof console)["timeEnd"]>): ReturnType<(typeof console)["timeEnd"]> { + return this.console.timeEnd(...args); + } + public timeStamp(...args: Parameters<(typeof console)["timeStamp"]>): ReturnType<(typeof console)["timeStamp"]> { + return this.console.timeStamp(...args); + } + //#endregion Copy-pasted Console code +} diff --git a/test/test-utils/mocks/mocks-container/mock-text.ts b/test/test-utils/mocks/mocks-container/mock-text.ts index ad2fce80972..a64aa45ef80 100644 --- a/test/test-utils/mocks/mocks-container/mock-text.ts +++ b/test/test-utils/mocks/mocks-container/mock-text.ts @@ -1,4 +1,5 @@ import type { MockGameObject } from "#test/test-utils/mocks/mock-game-object"; +import type { TextInterceptor } from "#test/test-utils/text-interceptor"; import { UI } from "#ui/ui"; export class MockText implements MockGameObject { @@ -82,13 +83,14 @@ export class MockText implements MockGameObject { showText( text: string, - delay?: number | null, + _delay?: number | null, callback?: Function | null, - callbackDelay?: number | null, - prompt?: boolean | null, - promptDelay?: number | null, + _callbackDelay?: number | null, + _prompt?: boolean | null, + _promptDelay?: number | null, ) { - this.scene.messageWrapper.showText(text, delay, callback, callbackDelay, prompt, promptDelay); + // TODO: this is a very bad way to pass calls around + (this.scene.messageWrapper as TextInterceptor).showText(text); if (callback) { callback(); } @@ -96,13 +98,13 @@ export class MockText implements MockGameObject { showDialogue( keyOrText: string, - name: string | undefined, - delay: number | null = 0, + name: string, + _delay: number | null, callback: Function, - callbackDelay?: number, - promptDelay?: number, + _callbackDelay?: number, + _promptDelay?: number, ) { - this.scene.messageWrapper.showDialogue(keyOrText, name, delay, callback, callbackDelay, promptDelay); + (this.scene.messageWrapper as TextInterceptor).showDialogue(keyOrText, name); if (callback) { callback(); } diff --git a/test/test-utils/reporters/custom-default-reporter.ts b/test/test-utils/reporters/custom-default-reporter.ts new file mode 100644 index 00000000000..15c4881b83c --- /dev/null +++ b/test/test-utils/reporters/custom-default-reporter.ts @@ -0,0 +1,62 @@ +import { relative } from "node:path"; +import { parseStacktrace } from "@vitest/utils/source-map"; +import chalk from "chalk"; +import type { UserConsoleLog } from "vitest"; +import type { TestState } from "vitest/node"; +import { DefaultReporter } from "vitest/reporters"; + +/** + * Custom Vitest reporter to strip the current file names from the output. + */ +export default class CustomDefaultReporter extends DefaultReporter { + public override onUserConsoleLog(log: UserConsoleLog, taskState?: TestState): void { + // This code is more or less copied verbatim from `vitest/reporters` source, with minor tweaks to use + // dependencies we actually _have_ (i.e. chalk) rather than ones we don't (i.e. tinyrainbow). + + // SPDX-SnippetBegin + // SPDX-SnippetCopyrightText: 2021 VoidZero Inc. and Vitest contributors + // SPDX-License-Identifier: MIT + + if (!super.shouldLog(log, taskState)) { + return; + } + + const output = log.type === "stdout" ? this.ctx.logger.outputStream : this.ctx.logger.errorStream; + + const write = (msg: string) => output.write(msg); + + const task = log.taskId ? this.ctx.state.idMap.get(log.taskId) : undefined; + + write(log.content); // this is about the only changed line (that and us skipping a newline) + + if (!log.origin) { + return; + } + + // Code for stack trace, ripped directly out of Vitest source code. + // I wish they had a helper function to do this so we didn't have to import `@vitest/utils`, but oh well... + // browser logs don't have an extra end of line at the end like Node.js does + if (log.browser) { + write("\n"); + } + + const project = task ? this.ctx.getProjectByName(task.file.projectName ?? "") : this.ctx.getRootProject(); + + const stack = log.browser ? (project.browser?.parseStacktrace(log.origin) ?? []) : parseStacktrace(log.origin); + + const highlight = task && stack.find(i => i.file === task.file.filepath); + + for (const frame of stack) { + const color = frame === highlight ? chalk.cyan : chalk.gray; + const path = relative(project.config.root, frame.file); + + const positions = [frame.method, `${path}:${chalk.dim(`${frame.line}:${frame.column}`)}`] + .filter(Boolean) + .join(" "); + + write(color(` ${chalk.dim(">")} ${positions}\n`)); + } + + // SPDX-SnippetEnd + } +} diff --git a/test/test-utils/setup/test-end-log.ts b/test/test-utils/setup/test-end-log.ts new file mode 100644 index 00000000000..9814ba8a45c --- /dev/null +++ b/test/test-utils/setup/test-end-log.ts @@ -0,0 +1,113 @@ +// biome-ignore lint/correctness/noUnusedImports: TSDoc +import type CustomDefaultReporter from "#test/test-utils/reporters/custom-default-reporter"; +import { basename, join, relative } from "path"; +import chalk from "chalk"; +import type { RunnerTask, RunnerTaskResult, RunnerTestCase } from "vitest"; + +/** + * @module + * Code to add markers to the beginning and end of tests. + * Intended for use with {@linkcode CustomDefaultReporter}, and placed inside test hooks + * (rather than as part of the reporter) to ensure Vitest waits for the log messages to be printed. + */ + +/** A long string of "="s to partition off each test from one another. */ +const TEST_END_BARRIER = chalk.bold.hex("#ff7c7cff")("=================="); + +// Colors used for Vitest-related test utils +const TEST_NAME_COLOR = "#008886ff" as const; +const VITEST_PINK_COLOR = "#c162de" as const; + +const testRoot = join(import.meta.dirname, "..", "..", ".."); + +/** + * Log the testfile name and path upon a case starting. \ + * Used to compensate for us overridding the global Console object and removing Vitest's + * test name annotations. + * @param test - The {@linkcode RunnerTask} passed from the context + */ +export function logTestStart(test: RunnerTask): void { + console.log(TEST_END_BARRIER); + console.log( + `${chalk.dim("> ")}${chalk.hex(VITEST_PINK_COLOR)("Starting test: ")}${chalk.hex(TEST_NAME_COLOR)(getTestName(test))}`, + ); +} + +/** + * Log the testfile name, path and result upon a case ending. \ + * Used to compensate for us overridding the global Console object and removing Vitest's + * test name annotations. + * @param task - The {@linkcode RunnerTestCase} passed from the hook + */ +export function logTestEnd(task: RunnerTestCase): void { + const durationStr = getDurationPrefix(task.result); + const resultStr = getResultStr(task.result); + console.log(`${chalk.dim("> ")}${chalk.black.bgHex(VITEST_PINK_COLOR)(" Test finished! ")} + Name: ${chalk.hex(TEST_NAME_COLOR)(getTestName(task))} + Result: ${resultStr}${durationStr} + File: ${chalk.hex("#d29b0eff")( + getPathFromTest(task.file.filepath) + (task.location ? `:${task.location.line}:${task.location.column}` : ""), + )}`); +} + +/** + * Get the path of the current test file relative to the `test` directory. + * @param abs - The absolute path to the file + * @returns The relative path with `test/` appended to it. + */ +function getPathFromTest(abs: string): string { + return join(basename(testRoot), relative(testRoot, abs)); +} + +function getResultStr(result: RunnerTaskResult | undefined): string { + if (result?.state !== "pass" && result?.state !== "fail") { + return "Unknown"; + } + + const resultStr = + result.state === "pass" + ? chalk.green.bold("✔ Passed") + : (result?.duration ?? 0) > 2 + ? chalk.cyan.bold("◴ Timed out") + : chalk.red.bold("✗ Failed"); + + return resultStr; +} + +/** + * Get the text to be displayed for a test's duration. + * @param result - The {@linkcode RunnerTaskResult} of the finished test + * @returns An appropriately colored suffix for the start time. + * Will return an empty string if `result.startTime` is `undefined` + */ +function getDurationPrefix(result?: RunnerTaskResult): string { + const startTime = result?.startTime; + if (!startTime) { + return ""; + } + const duration = Math.round(Date.now() - startTime); + + // TODO: Figure out a way to access the current vitest config from a hook + const color = duration > 10_000 ? chalk.yellow : chalk.green; + return ` ${chalk.dim("in")} ${color(duration)}${chalk.dim("ms")}`; +} + +// Function copied from vitest source to avoid having to import `@vitest/runner/utils` for 1 function + +// SPDX-SnippetBegin +// SPDX-SnippetCopyrightText: 2021 VoidZero Inc. and Vitest contributors +// SPDX-License-Identifier: MIT +function getTestName(task: RunnerTask, separator = " > "): string { + const names: string[] = [task.name]; + let current: RunnerTask | undefined = task; + + while ((current = current?.suite)) { + if (current?.name) { + names.unshift(current.name); + } + } + + return names.join(separator); +} + +// SPDX-SnippetEnd diff --git a/test/test-utils/test-file-initialization.ts b/test/test-utils/test-file-initialization.ts index 631d3f9146b..c172e2d1da8 100644 --- a/test/test-utils/test-file-initialization.ts +++ b/test/test-utils/test-file-initialization.ts @@ -3,7 +3,7 @@ import { initializeGame } from "#app/init/init"; import { initI18n } from "#plugins/i18n"; import { blobToString } from "#test/test-utils/game-manager-utils"; import { manageListeners } from "#test/test-utils/listeners-manager"; -import { MockConsoleLog } from "#test/test-utils/mocks/mock-console-log"; +import { MockConsole } from "#test/test-utils/mocks/mock-console/mock-console"; import { mockContext } from "#test/test-utils/mocks/mock-context-canvas"; import { mockLocalStorage } from "#test/test-utils/mocks/mock-local-storage"; import { MockImage } from "#test/test-utils/mocks/mocks-container/mock-image"; @@ -38,14 +38,22 @@ function initTestFile(): void { /** * Setup various stubs for testing. * @todo Move this into a dedicated stub file instead of running it once per test instance + * @todo review these to see which are actually necessary * @todo Investigate why this resets on new test suite start */ function setupStubs(): void { - Object.defineProperty(window, "localStorage", { - value: mockLocalStorage(), - }); - Object.defineProperty(window, "console", { - value: new MockConsoleLog(false), + Object.defineProperties(global, { + localStorage: { + value: mockLocalStorage(), + }, + console: { + value: new MockConsole(), + }, + matchMedia: { + value: () => ({ + matches: false, + }), + }, }); Object.defineProperty(document, "fonts", { writable: true, @@ -69,11 +77,6 @@ function setupStubs(): void { navigator.getGamepads = () => []; setCookie(SESSION_ID_COOKIE_NAME, "fake_token"); - window.matchMedia = () => - ({ - matches: false, - }) as any; - /** * Sets this object's position relative to another object with a given offset * @param guideObject - The {@linkcode Phaser.GameObjects.GameObject} to base the position off of diff --git a/test/test-utils/text-interceptor.ts b/test/test-utils/text-interceptor.ts index 36a5db4c78d..dfbaf2ff11c 100644 --- a/test/test-utils/text-interceptor.ts +++ b/test/test-utils/text-interceptor.ts @@ -1,39 +1,49 @@ +import type { BattleScene } from "#app/battle-scene"; +import chalk from "chalk"; + /** - * Class will intercept any text or dialogue message calls and log them for test purposes + * The {@linkcode TextInterceptor} is a wrapper class that intercepts and logs any messages + * that would be displayed on-screen. */ export class TextInterceptor { - private scene; - public logs: string[] = []; - constructor(scene) { - this.scene = scene; + /** A log containing messages having been displayed on screen, sorted in FIFO order. */ + public readonly logs: string[] = []; + + constructor(scene: BattleScene) { + // @ts-expect-error: Find another more sanitary way of doing this scene.messageWrapper = this; } - showText( - text: string, - _delay?: number, - _callback?: Function, - _callbackDelay?: number, - _prompt?: boolean, - _promptDelay?: number, - ): void { - console.log(text); + /** Clear the current content of the TextInterceptor. */ + public clearLogs(): void { + this.logs.splice(0); + } + + showText(text: string): void { + // NB: We do not format the raw _logs_ themselves as tests will be actively checking it. + console.log(this.formatText(text)); this.logs.push(text); } - showDialogue( - text: string, - name: string, - _delay?: number, - _callback?: Function, - _callbackDelay?: number, - _promptDelay?: number, - ): void { - console.log(name, text); + showDialogue(text: string, name: string): void { + console.log(`${name}: \n${this.formatText(text)}`); this.logs.push(name, text); } - getLatestMessage(): string { - return this.logs.pop() ?? ""; + /** + * Format text to be displayed to the test console, as follows: + * 1. Replaces new lines and new text boxes (marked by `$`) with indented new lines. + * 2. Removes all `@c{}`, `@d{}`, `@s{}`, and `@f{}` flags from the text. + * 3. Makes text blue + * @param text - The unformatted text + * @returns The formatted text + */ + private formatText(text: string): string { + return chalk.blue( + text + .replace(/\n/g, " ") + .replace(/\$/g, "\n ") + .replace(/@\w{.*?}/g, ""), + ); } } diff --git a/test/vitest.setup.ts b/test/vitest.setup.ts index be35e18e2e9..23adab01a05 100644 --- a/test/vitest.setup.ts +++ b/test/vitest.setup.ts @@ -1,16 +1,21 @@ import "vitest-canvas-mock"; +import { MockConsole } from "#test/test-utils/mocks/mock-console/mock-console"; +import { logTestEnd, logTestStart } from "#test/test-utils/setup/test-end-log"; import { initTests } from "#test/test-utils/test-file-initialization"; -import { afterAll, beforeAll, vi } from "vitest"; +import chalk from "chalk"; +import { afterAll, afterEach, beforeAll, beforeEach, vi } from "vitest"; -/** Set the timezone to UTC for tests. */ +//#region Mocking -/** Mock the override import to always return default values, ignoring any custom overrides. */ -vi.mock("#app/overrides", async importOriginal => { - const { defaultOverrides } = await importOriginal(); +// Mock the override import to always return default values, ignoring any custom overrides. +vi.mock(import("#app/overrides"), async importOriginal => { + const { defaultOverrides } = await importOriginal(); return { default: defaultOverrides, - defaultOverrides, + // Export `defaultOverrides` as a *copy*. + // This ensures we can easily reset `overrides` back to its default values after modifying it. + defaultOverrides: { ...defaultOverrides }, } satisfies typeof import("#app/overrides"); }); @@ -20,7 +25,7 @@ vi.mock("#app/overrides", async importOriginal => { * This is necessary because how our code is structured. * Do NOT try to put any of this code into external functions, it won't work as it's elevated during runtime. */ -vi.mock("i18next", async importOriginal => { +vi.mock(import("i18next"), async importOriginal => { console.log("Mocking i18next"); const { setupServer } = await import("msw/node"); const { http, HttpResponse } = await import("msw"); @@ -30,8 +35,11 @@ vi.mock("i18next", async importOriginal => { const filename = req.params[0]; try { - const json = await import(`../public/locales/en/${req.params[0]}`); - console.log("Loaded locale", filename); + const localeFiles = import.meta.glob("../public/locales/en/**/*.json", { eager: true }); + const json = localeFiles[`../public/locales/en/${filename}`] || {}; + if (import.meta.env.VITE_I18N_DEBUG === "1") { + console.log("Loaded locale", filename); + } return HttpResponse.json(json); } catch (err) { console.log(`Failed to load locale ${filename}!`, err); @@ -54,7 +62,15 @@ beforeAll(() => { initTests(); }); +beforeEach(context => { + logTestStart(context.task); +}); +afterEach(context => { + logTestEnd(context.task); +}); + afterAll(() => { global.server.close(); - console.log("Closing i18n MSW server!"); + MockConsole.printPostTestWarnings(); + console.log(chalk.hex("#dfb8d8")("Closing i18n MSW server!")); }); diff --git a/typedoc.config.js b/typedoc.config.js index 1f944cd544e..ef932a5d077 100644 --- a/typedoc.config.js +++ b/typedoc.config.js @@ -8,7 +8,13 @@ const dryRun = !!process.env.DRY_RUN?.match(/true/gi); const config = { entryPoints: ["./src", "./test/test-utils"], entryPointStrategy: "expand", - exclude: ["**/*+.test.ts", "src/polyfills.ts", "src/vite.env.d.ts"], + exclude: [ + "src/polyfills.ts", + "src/vite.env.d.ts", + "**/*+.test.ts", + "test/test-utils/setup", + "test/test-utils/reporters", + ], excludeReferences: true, // prevent documenting re-exports requiredToBeDocumented: [ "Enum", diff --git a/vitest.config.ts b/vitest.config.ts index 65c5427e591..7fa2494bb4e 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,18 +1,25 @@ -import { defineProject } from "vitest/config"; +import { defineConfig } from "vitest/config"; import { BaseSequencer, type TestSpecification } from "vitest/node"; import { defaultConfig } from "./vite.config"; -export default defineProject(({ mode }) => ({ +export default defineConfig(({ mode }) => ({ ...defaultConfig, test: { + reporters: process.env.GITHUB_ACTIONS + ? ["github-actions", "./test/test-utils/reporters/custom-default-reporter.ts"] + : ["./test/test-utils/reporters/custom-default-reporter.ts"], env: { TZ: "UTC", }, - testTimeout: 20000, + testTimeout: 20_000, + slowTestThreshold: 10_000, + // TODO: Consider enabling + // expect: {requireAssertions: true}, setupFiles: ["./test/font-face.setup.ts", "./test/vitest.setup.ts", "./test/matchers.setup.ts"], sequence: { sequencer: MySequencer, }, + includeTaskLocation: true, environment: "jsdom" as const, environmentOptions: { jsdom: { From e222731623b78aac65963f9293c6c8b4e0810d80 Mon Sep 17 00:00:00 2001 From: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com> Date: Sun, 7 Sep 2025 09:43:01 -0500 Subject: [PATCH 4/6] Center text in README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d381b8f47f5..73477968bc0 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -PokéRogue +
PokéRogue [![Discord Static Badge](https://img.shields.io/badge/Community_Discord-blurple?style=flat&logo=discord&logoSize=auto&labelColor=white&color=5865F2)](https://discord.gg/pokerogue) [![Docs Coverage Static Badge](https://pagefaultgames.github.io/pokerogue/beta/coverage.svg)](https://pagefaultgames.github.io/pokerogue/beta) [![Testing Badge](https://github.com/pagefaultgames/pokerogue/actions/workflows/tests.yml/badge.svg)](https://github.com/pagefaultgames/pokerogue/actions/workflows/tests.yml) -[![License: GNU AGPL v3](https://img.shields.io/badge/License-AGPL_v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0) +[![License: GNU AGPL v3](https://img.shields.io/badge/License-AGPL_v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
PokéRogue is a browser based Pokémon fangame heavily inspired by the roguelite genre. Battle endlessly while gathering stacking items, exploring many different biomes, fighting trainers, bosses, and more! From 2cf23b7ea7b1662c260b74ee61a42a19dd955e8b Mon Sep 17 00:00:00 2001 From: NightKev <34855794+DayKev@users.noreply.github.com> Date: Sun, 7 Sep 2025 10:58:40 -0700 Subject: [PATCH 5/6] [Misc] Fix console log colors --- src/constants/colors.ts | 4 +++- src/phase-manager.ts | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/constants/colors.ts b/src/constants/colors.ts index e4d740addff..717c5fa5f0d 100644 --- a/src/constants/colors.ts +++ b/src/constants/colors.ts @@ -5,8 +5,10 @@ */ // Colors used in prod +/** Color used for "Start Phase " logs */ export const PHASE_START_COLOR = "green" as const; -export const MOVE_COLOR = "RebeccaPurple" as const; +/** Color used for logs in `MovePhase` */ +export const MOVE_COLOR = "orchid" as const; // Colors used for testing code export const NEW_TURN_COLOR = "#ffad00ff" as const; diff --git a/src/phase-manager.ts b/src/phase-manager.ts index 68b7d74293b..2185de559ae 100644 --- a/src/phase-manager.ts +++ b/src/phase-manager.ts @@ -1,3 +1,4 @@ +import { PHASE_START_COLOR } from "#app/constants/colors"; import { globalScene } from "#app/global-scene"; import type { Phase } from "#app/phase"; import { type PhasePriorityQueue, PostSummonPhasePriorityQueue } from "#data/phase-priority-queue"; @@ -392,7 +393,7 @@ export class PhaseManager { * Helper method to start and log the current phase. */ private startCurrentPhase(): void { - console.log(`%cStart Phase ${this.currentPhase.phaseName}`, "color:${PHASE_START_COLOR};"); + console.log(`%cStart Phase ${this.currentPhase.phaseName}`, `color:${PHASE_START_COLOR};`); this.currentPhase.start(); } From 8b95361d616a778c47c2f0da9c9e40317996be9e Mon Sep 17 00:00:00 2001 From: Bertie690 <136088738+Bertie690@users.noreply.github.com> Date: Sun, 7 Sep 2025 14:35:52 -0400 Subject: [PATCH 6/6] [Dev] Added egg move parse script & script type checking (#6116) * Added egg move parse utility script * Update interactive.js Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> * Update interactive.js Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> * Update interactive.js Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> * Applied kev's reviews * Removed `basePath` from tsconfig the docs literally recommend against using it so yeah * Fixed up configs so that script folder has its own file * Reverted changes to egg move contents * renamed boilerplate so biome doesn't lint it * Fix `jsconfig.json` so that it doesn't typecheck all of `node_modules` See https://github.com/microsoft/TypeScript/issues/50862#issuecomment-1565175938 for more info * Update tsconfig.json Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> * Updated workflows and fixed issues * Removed eslint from linting workflow * Fixed type error in d.ts file to shut up linters * Reverted test-filters.yml * Update biome.jsonc * Update decrypt-save.js comment * Update interactive.js * Apply Biome * Fixed type errors for scripts * Fixed biome from removing tsdoc linkcodes * Update test/@types/vitest.d.ts --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> --- .github/workflows/linting.yml | 81 +++++++-- biome.jsonc | 15 +- package.json | 2 + scripts/create-test/create-test.js | 2 +- scripts/decrypt-save.js | 18 +- scripts/jsconfig.json | 17 ++ .../egg-move-template.boilerplate.ts | 10 ++ scripts/parse-egg-moves/help-message.js | 17 ++ scripts/parse-egg-moves/interactive.js | 108 +++++++++++ scripts/parse-egg-moves/main.js | 168 ++++++++++++++++++ scripts/parse-egg-moves/parse.js | 79 ++++++++ scripts/scrape-trainer-names/fetch-names.js | 8 +- src/data/balance/egg-moves.ts | 62 +------ src/init/init.ts | 2 - test/@types/vitest.d.ts | 12 +- .../helpers/challenge-mode-helper.ts | 2 +- tsconfig.json | 74 ++++---- 17 files changed, 550 insertions(+), 127 deletions(-) create mode 100644 scripts/jsconfig.json create mode 100644 scripts/parse-egg-moves/egg-move-template.boilerplate.ts create mode 100644 scripts/parse-egg-moves/help-message.js create mode 100644 scripts/parse-egg-moves/interactive.js create mode 100644 scripts/parse-egg-moves/main.js create mode 100644 scripts/parse-egg-moves/parse.js diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 08327ee3653..edecae64f95 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -18,7 +18,7 @@ on: jobs: run-linters: - name: Run linters + name: Run all linters timeout-minutes: 10 runs-on: ubuntu-latest @@ -26,27 +26,86 @@ jobs: - name: Check out Git repository uses: actions/checkout@v4 with: - submodules: 'recursive' + submodules: "recursive" - name: Install pnpm uses: pnpm/action-setup@v4 with: version: 10 - - name: Set up Node.js + - name: Set up Node uses: actions/setup-node@v4 with: - node-version-file: '.nvmrc' - cache: 'pnpm' + node-version-file: ".nvmrc" + cache: "pnpm" - - name: Install Node.js dependencies + - name: Install Node modules run: pnpm i - - name: Lint with Biome + # Lint files with Biome-Lint - https://biomejs.dev/linter/ + - name: Run Biome-Lint run: pnpm biome-ci + id: biome_lint + continue-on-error: true - - name: Check dependencies with depcruise + # Validate dependencies with dependency-cruiser - https://github.com/sverweij/dependency-cruiser + - name: Run Dependency-Cruise run: pnpm depcruise - - - name: Lint with ls-lint - run: pnpm ls-lint \ No newline at end of file + id: depcruise + continue-on-error: true + + # Validate types with tsc - https://www.typescriptlang.org/docs/handbook/compiler-options.html#using-the-cli + - name: Run Typecheck + run: pnpm typecheck + id: typecheck + continue-on-error: true + + # The exact same thing + - name: Run Typecheck (scripts) + run: pnpm typecheck:scripts + id: typecheck-scripts + continue-on-error: true + + - name: Evaluate for Errors + env: + BIOME_LINT_OUTCOME: ${{ steps.biome_lint.outcome }} + DEPCRUISE_OUTCOME: ${{ steps.depcruise.outcome }} + TYPECHECK_OUTCOME: ${{ steps.typecheck.outcome }} + TYPECHECK_SCRIPTS_OUTCOME: ${{ steps.typecheck-scripts.outcome }} + run: | + # Check for Errors + + # Make text red. + red () { + printf "\e[31m%s\e[0m" "$1" + } + + # Make text green. + green () { + printf "\e[32m%s\e[0m" "$1" + } + + print_result() { + local name=$1 + local outcome=$2 + if [ "$outcome" == "success" ]; then + printf "$(green "✅ $name: $outcome")\n" + else + printf "$(red "❌ $name: $outcome")\n" + fi + } + + print_result "Biome" "$BIOME_LINT_OUTCOME" + print_result "Depcruise" "$DEPCRUISE_OUTCOME" + print_result "Typecheck" "$TYPECHECK_OUTCOME" + print_result "Typecheck scripts" "$TYPECHECK_SCRIPTS_OUTCOME" + + if [[ "$BIOME_LINT_OUTCOME" != "success" || \ + "$DEPCRUISE_OUTCOME" != "success" || \ + "$TYPECHECK_OUTCOME" != "success" || \ + "$TYPECHECK_SCRIPTS_OUTCOME" != "success" ]]; then + printf "$(red "❌ One or more checks failed!")\n" >&2 + exit 1 + fi + + printf "$(green "✅ All checks passed!")\n" diff --git a/biome.jsonc b/biome.jsonc index a63ce0ee07d..e6f9ff5711a 100644 --- a/biome.jsonc +++ b/biome.jsonc @@ -175,10 +175,17 @@ } }, - // Overrides to prevent unused import removal inside `overrides.ts` and enums files (for TSDoc linkcodes), - // as well as in all TS files in `scripts/` (which are assumed to be boilerplate templates). + // Overrides to prevent unused import removal inside `overrides.ts`, enums & `.d.ts` files (for TSDoc linkcodes), + // as well as inside script boilerplate files. { - "includes": ["**/src/overrides.ts", "**/src/enums/**/*", "**/scripts/**/*.ts", "**/*.d.ts"], + // TODO: Rename existing boilerplates in the folder and remove this last alias + "includes": [ + "**/src/overrides.ts", + "**/src/enums/**/*", + "**/*.d.ts", + "scripts/**/*.boilerplate.ts", + "**/boilerplates/*.ts" + ], "linter": { "rules": { "correctness": { @@ -188,7 +195,7 @@ } }, { - "includes": ["**/src/overrides.ts", "**/scripts/**/*.ts"], + "includes": ["**/src/overrides.ts"], "linter": { "rules": { "style": { diff --git a/package.json b/package.json index f6097b8ccb9..d33c5e390d6 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,10 @@ "test:watch": "vitest watch --coverage --no-isolate", "test:silent": "vitest run --silent='passed-only' --no-isolate", "test:create": "node scripts/create-test/create-test.js", + "eggMoves:parse": "node scripts/parse-egg-moves/main.js", "scrape-trainers": "node scripts/scrape-trainer-names/main.js", "typecheck": "tsc --noEmit", + "typecheck:scripts": "tsc -p scripts/jsconfig.json", "biome": "biome check --write --changed --no-errors-on-unmatched --diagnostic-level=error", "biome-ci": "biome ci --diagnostic-level=error --reporter=github --no-errors-on-unmatched", "typedoc": "typedoc", diff --git a/scripts/create-test/create-test.js b/scripts/create-test/create-test.js index 765993959d1..5e395783da7 100644 --- a/scripts/create-test/create-test.js +++ b/scripts/create-test/create-test.js @@ -156,7 +156,7 @@ async function runInteractive() { console.log(chalk.green.bold(`✔ File created at: test/${localDir}/${fileName}.test.ts\n`)); console.groupEnd(); } catch (err) { - console.error(chalk.red("✗ Error: ", err.message)); + console.error(chalk.red("✗ Error: ", err)); } } diff --git a/scripts/decrypt-save.js b/scripts/decrypt-save.js index e50f152f159..26b0a311378 100644 --- a/scripts/decrypt-save.js +++ b/scripts/decrypt-save.js @@ -1,7 +1,6 @@ // Usage: node decrypt-save.js [save-file] -// biome-ignore lint/performance/noNamespaceImport: This is how you import fs from node -import * as fs from "node:fs"; +import fs from "node:fs"; import crypto_js from "crypto-js"; const { AES, enc } = crypto_js; @@ -60,6 +59,11 @@ function decryptSave(path) { try { fileData = fs.readFileSync(path, "utf8"); } catch (e) { + if (!(e instanceof Error)) { + console.error(`Unrecognized error: ${e}`); + process.exit(1); + } + // @ts-expect-error - e is usually a SystemError (all of which have codes) switch (e.code) { case "ENOENT": console.error(`File not found: ${path}`); @@ -104,6 +108,13 @@ function writeToFile(filePath, data) { try { fs.writeFileSync(filePath, data); } catch (e) { + if (!(e instanceof Error)) { + console.error("Unknown error detected: ", e); + process.exitCode = 1; + return; + } + + // @ts-expect-error - e is usually a SystemError (all of which have codes) switch (e.code) { case "EACCES": console.error(`Could not open ${filePath}: Permission denied`); @@ -114,7 +125,8 @@ function writeToFile(filePath, data) { default: console.error(`Error writing file: ${e.message}`); } - process.exit(1); + process.exitCode = 1; + return; } } diff --git a/scripts/jsconfig.json b/scripts/jsconfig.json new file mode 100644 index 00000000000..aed71f4f576 --- /dev/null +++ b/scripts/jsconfig.json @@ -0,0 +1,17 @@ +{ + "include": ["**/*.js"], + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "rootDir": ".", + "target": "esnext", + "module": "nodenext", + "moduleResolution": "nodenext", + "erasableSyntaxOnly": true, + "strict": true, + "noEmit": true, + // Forcibly disable `node_modules` recursion to prevent TSC from typechecking random JS files. + // This is disabled by default in `tsconfig.json`, but needs to be explicitly disabled from the default of `2` + "maxNodeModuleJsDepth": 0 + } +} diff --git a/scripts/parse-egg-moves/egg-move-template.boilerplate.ts b/scripts/parse-egg-moves/egg-move-template.boilerplate.ts new file mode 100644 index 00000000000..bfac05f4bde --- /dev/null +++ b/scripts/parse-egg-moves/egg-move-template.boilerplate.ts @@ -0,0 +1,10 @@ +//! DO NOT EDIT THIS FILE - CREATED BY THE `eggMoves:parse` script automatically +import { MoveId } from "#enums/move-id"; +import { SpeciesId } from "#enums/species-id"; + +/** + * An object mapping all base form {@linkcode SpeciesId}s to an array of {@linkcode MoveId}s corresponding + * to their current egg moves. + * Generated by the `eggMoves:parse` script using a CSV sourced from the current Balance Team spreadsheet. + */ +export const speciesEggMoves = "{{table}}"; diff --git a/scripts/parse-egg-moves/help-message.js b/scripts/parse-egg-moves/help-message.js new file mode 100644 index 00000000000..397a28e5011 --- /dev/null +++ b/scripts/parse-egg-moves/help-message.js @@ -0,0 +1,17 @@ +import chalk from "chalk"; + +/** Show help/usage text for the `eggMoves:parse` CLI. */ +export function showHelpText() { + console.log(` +Usage: ${chalk.cyan("pnpm eggMoves:parse [options]")} +If given no options, assumes ${chalk.blue("\`--interactive\`")}. +If given only a file path, assumes ${chalk.blue("\`--file\`")}. + +${chalk.hex("#ffa500")("Options:")} + ${chalk.blue("-h, --help")} Show this help message. + ${chalk.blue("-f, --file[=PATH]")} Specify a path to a CSV file to read, or provide one from stdin. + ${chalk.blue("-t, --text[=TEXT]")} + ${chalk.blue("-c, --console[=TEXT]")} Specify CSV text to read, or provide it from stdin. + ${chalk.blue("-i, --interactive")} Run in interactive mode (default) +`); +} diff --git a/scripts/parse-egg-moves/interactive.js b/scripts/parse-egg-moves/interactive.js new file mode 100644 index 00000000000..68ee41e7900 --- /dev/null +++ b/scripts/parse-egg-moves/interactive.js @@ -0,0 +1,108 @@ +import fs from "fs"; +import chalk from "chalk"; +import inquirer from "inquirer"; +import { showHelpText } from "./help-message.js"; + +/** + * @import { Option } from "./main.js" + */ + +/** + * Prompt the user to interactively select an option (console/file) to retrieve the egg move CSV. + * @returns {Promise