diff --git a/.github/test-filters.yml b/.github/test-filters.yml index 89f4322adea..fc52e85082c 100644 --- a/.github/test-filters.yml +++ b/.github/test-filters.yml @@ -1,7 +1,8 @@ all: - - "src/**" - - "test/**" - - "public/**" + # Negations syntax from https://github.com/dorny/paths-filter/issues/184#issuecomment-2786521554 + - "src/**/!(*.{md,py,sh,gitkeep,gitignore})" + - "test/**/!(*.{md,py,sh,gitkeep,gitignore})" + - "public/**/!(*.{md,py,sh,gitkeep,gitignore})" # Workflows that can impact tests - ".github/workflows/test*.yml" - ".github/test-filters.yml" @@ -11,9 +12,4 @@ all: - "vite*" # vite.config.ts, vite.vitest.config.ts, vitest.workspace.ts - "tsconfig*.json" # tsconfig.json tweaking can impact compilation - "global.d.ts" - - ".env*" - # Blanket negations for files that cannot impact tests - - "!**/*.py" # No .py files - - "!**/*.sh" # No .sh files - - "!**/*.md" # No .md files - - "!**/.git*" # .gitkeep and family + - ".env*" \ No newline at end of file diff --git a/.github/workflows/test-shard-template.yml b/.github/workflows/test-shard-template.yml index a1146cb3497..98836bd335a 100644 --- a/.github/workflows/test-shard-template.yml +++ b/.github/workflows/test-shard-template.yml @@ -19,19 +19,20 @@ on: jobs: test: - name: Shard ${{ inputs.shard }} of ${{ inputs.totalShards }} + # We can't use dynmically named jobs until https://github.com/orgs/community/discussions/13261 is implemented + name: Shard runs-on: ubuntu-latest if: ${{ !inputs.skip }} steps: - name: Check out Git repository uses: actions/checkout@v4.2.2 with: - submodules: 'recursive' + submodules: "recursive" - name: Set up Node.js uses: actions/setup-node@v4 with: - node-version-file: '.nvmrc' - cache: 'npm' + node-version-file: ".nvmrc" + cache: "npm" - name: Install Node.js dependencies run: npm ci - name: Run tests diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f04a1987eff..c3b9666caa9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -25,6 +25,7 @@ jobs: - name: checkout uses: actions/checkout@v4 - uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 + id: filter with: filters: .github/test-filters.yml @@ -33,10 +34,10 @@ jobs: needs: check-path-change-filter strategy: matrix: - shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + shard: [1, 2, 3, 4, 5] uses: ./.github/workflows/test-shard-template.yml with: project: main shard: ${{ matrix.shard }} - totalShards: 10 - skip: ${{ needs.check-path-change-filter.outputs.all == 'false'}} + totalShards: 5 + skip: ${{ needs.check-path-change-filter.outputs.all != 'true'}} diff --git a/src/data/abilities/ability.ts b/src/data/abilities/ability.ts index b677dd2bd11..f49863639f0 100644 --- a/src/data/abilities/ability.ts +++ b/src/data/abilities/ability.ts @@ -7425,7 +7425,12 @@ export function initAbilities() { .uncopiable() .attr(NoTransformAbilityAbAttr), new Ability(Abilities.GOOD_AS_GOLD, 9) - .attr(MoveImmunityAbAttr, (pokemon, attacker, move) => pokemon !== attacker && move.category === MoveCategory.STATUS && ![ MoveTarget.ENEMY_SIDE, MoveTarget.BOTH_SIDES, MoveTarget.USER_SIDE ].includes(move.moveTarget)) + .attr(MoveImmunityAbAttr, (pokemon, attacker, move) => + pokemon !== attacker + && move.category === MoveCategory.STATUS + && ![ MoveTarget.ENEMY_SIDE, MoveTarget.BOTH_SIDES, MoveTarget.USER_SIDE ].includes(move.moveTarget) + ) + .edgeCase() // Heal Bell should not cure the status of a Pokemon with Good As Gold .ignorable(), new Ability(Abilities.VESSEL_OF_RUIN, 9) .attr(FieldMultiplyStatAbAttr, Stat.SPATK, 0.75) diff --git a/src/enums/drop-down-column.ts b/src/enums/drop-down-column.ts new file mode 100644 index 00000000000..b413d1f0bf4 --- /dev/null +++ b/src/enums/drop-down-column.ts @@ -0,0 +1,9 @@ +export enum DropDownColumn { + GEN, + TYPES, + BIOME, + CAUGHT, + UNLOCKS, + MISC, + SORT +} diff --git a/src/ui/egg-counter-container.ts b/src/ui/egg-counter-container.ts index 7bec7c97480..7bb32189681 100644 --- a/src/ui/egg-counter-container.ts +++ b/src/ui/egg-counter-container.ts @@ -43,14 +43,13 @@ export default class EggCounterContainer extends Phaser.GameObjects.Container { this.add(this.eggCountWindow); - const eggSprite = globalScene.add.sprite(19, 18, "egg", "egg_0"); - eggSprite.setScale(0.32); + const eggSprite = globalScene.add.sprite(19, 18, "egg", "egg_0").setScale(0.32); - this.eggCountText = addTextObject(28, 13, `${this.eggCount}`, TextStyle.MESSAGE, { fontSize: "66px" }); - this.eggCountText.setName("text-egg-count"); + this.eggCountText = addTextObject(28, 13, `${this.eggCount}`, TextStyle.MESSAGE, { fontSize: "66px" }).setName( + "text-egg-count", + ); - this.add(eggSprite); - this.add(this.eggCountText); + this.add([eggSprite, this.eggCountText]); } /** diff --git a/src/ui/filter-bar.ts b/src/ui/filter-bar.ts index 2aade130ed1..622488c04cd 100644 --- a/src/ui/filter-bar.ts +++ b/src/ui/filter-bar.ts @@ -5,16 +5,7 @@ import { addTextObject, getTextColor, TextStyle } from "./text"; import type { UiTheme } from "#enums/ui-theme"; import { addWindow, WindowVariant } from "./ui-theme"; import { globalScene } from "#app/global-scene"; - -export enum DropDownColumn { - GEN, - TYPES, - BIOME, - CAUGHT, - UNLOCKS, - MISC, - SORT, -} +import type { DropDownColumn } from "#enums/drop-down-column"; export class FilterBar extends Phaser.GameObjects.Container { private window: Phaser.GameObjects.NineSlice; @@ -49,13 +40,9 @@ export class FilterBar extends Phaser.GameObjects.Container { this.cursorOffset = cursorOffset; this.window = addWindow(0, 0, width, height, false, false, undefined, undefined, WindowVariant.THIN); - this.add(this.window); - this.cursorObj = globalScene.add.image(1, 1, "cursor"); - this.cursorObj.setScale(0.5); - this.cursorObj.setVisible(false); - this.cursorObj.setOrigin(0, 0); - this.add(this.cursorObj); + this.cursorObj = globalScene.add.image(1, 1, "cursor").setScale(0.5).setVisible(false).setOrigin(0); + this.add([this.window, this.cursorObj]); } /** diff --git a/src/ui/pokedex-ui-handler.ts b/src/ui/pokedex-ui-handler.ts index 08fe5d7442f..179d5b4664b 100644 --- a/src/ui/pokedex-ui-handler.ts +++ b/src/ui/pokedex-ui-handler.ts @@ -23,7 +23,8 @@ import type { Species } from "#enums/species"; import { Button } from "#enums/buttons"; import { DropDown, DropDownLabel, DropDownOption, DropDownState, DropDownType, SortCriteria } from "#app/ui/dropdown"; import { PokedexMonContainer } from "#app/ui/pokedex-mon-container"; -import { DropDownColumn, FilterBar } from "#app/ui/filter-bar"; +import { FilterBar } from "#app/ui/filter-bar"; +import { DropDownColumn } from "#enums/drop-down-column"; import { ScrollBar } from "#app/ui/scroll-bar"; import { Abilities } from "#enums/abilities"; import { diff --git a/src/ui/starter-select-ui-handler.ts b/src/ui/starter-select-ui-handler.ts index a971ba86479..ff2298f268d 100644 --- a/src/ui/starter-select-ui-handler.ts +++ b/src/ui/starter-select-ui-handler.ts @@ -53,7 +53,8 @@ import { Button } from "#enums/buttons"; import { EggSourceType } from "#enums/egg-source-types"; import { DropDown, DropDownLabel, DropDownOption, DropDownState, DropDownType, SortCriteria } from "#app/ui/dropdown"; import { StarterContainer } from "#app/ui/starter-container"; -import { DropDownColumn, FilterBar } from "#app/ui/filter-bar"; +import { FilterBar } from "#app/ui/filter-bar"; +import { DropDownColumn } from "#enums/drop-down-column"; import { ScrollBar } from "#app/ui/scroll-bar"; import { SelectChallengePhase } from "#app/phases/select-challenge-phase"; import { EncounterPhase } from "#app/phases/encounter-phase"; diff --git a/test/abilities/good_as_gold.test.ts b/test/abilities/good_as_gold.test.ts index 09bdaafb11f..9b1d582f64c 100644 --- a/test/abilities/good_as_gold.test.ts +++ b/test/abilities/good_as_gold.test.ts @@ -1,6 +1,6 @@ import { BattlerIndex } from "#app/battle"; -import { allAbilities } from "#app/data/data-lists"; import { ArenaTagSide } from "#app/data/arena-tag"; +import { allAbilities } from "#app/data/data-lists"; import { ArenaTagType } from "#app/enums/arena-tag-type"; import { BattlerTagType } from "#app/enums/battler-tag-type"; import { Stat } from "#app/enums/stat"; @@ -107,35 +107,33 @@ describe("Abilities - Good As Gold", () => { expect(game.scene.getPlayerField()[1].getTag(BattlerTagType.HELPING_HAND)).toBeUndefined(); }); - it("should block the ally's heal bell, but only if the good as gold user is on the field", async () => { - game.override.battleStyle("double"); - game.override.moveset([Moves.HEAL_BELL, Moves.SPLASH]); - game.override.statusEffect(StatusEffect.BURN); - await game.classicMode.startBattle([Species.MAGIKARP, Species.FEEBAS, Species.ABRA]); - const [good_as_gold, ball_fetch] = game.scene.getPlayerField(); - - // Force second pokemon to have ball fetch to isolate to a single mon. - vi.spyOn(ball_fetch, "getAbility").mockReturnValue(allAbilities[Abilities.BALL_FETCH]); + // TODO: re-enable when heal bell is fixed + it.todo("should block the ally's heal bell, but only if the good as gold user is on the field", async () => { + game.override.battleStyle("double").statusEffect(StatusEffect.BURN); + await game.classicMode.startBattle([Species.MILOTIC, Species.FEEBAS, Species.ABRA]); + const [milotic, feebas, abra] = game.scene.getPlayerParty(); + game.field.mockAbility(milotic, Abilities.GOOD_AS_GOLD); + game.field.mockAbility(feebas, Abilities.BALL_FETCH); + game.field.mockAbility(abra, Abilities.BALL_FETCH); // turn 1 - game.move.select(Moves.SPLASH, 0); - game.move.select(Moves.HEAL_BELL, 1); + game.move.use(Moves.SPLASH, 0); + game.move.use(Moves.HEAL_BELL, 1); await game.toNextTurn(); - expect(good_as_gold.status?.effect).toBe(StatusEffect.BURN); + expect(milotic.status?.effect).toBe(StatusEffect.BURN); game.doSwitchPokemon(2); - game.move.select(Moves.HEAL_BELL, 0); + game.move.use(Moves.HEAL_BELL, 1); await game.toNextTurn(); - expect(good_as_gold.status?.effect).toBeUndefined(); + expect(milotic.status?.effect).toBeUndefined(); }); it("should not block field targeted effects like rain dance", async () => { game.override.battleStyle("single"); game.override.enemyMoveset([Moves.RAIN_DANCE]); - game.override.weather(WeatherType.NONE); await game.classicMode.startBattle([Species.MAGIKARP]); - game.move.select(Moves.SPLASH, 0); + game.move.use(Moves.SPLASH, 0); await game.phaseInterceptor.to("BerryPhase"); expect(game.scene.arena.weather?.weatherType).toBe(WeatherType.RAIN); diff --git a/test/moves/alluring_voice.test.ts b/test/moves/alluring_voice.test.ts index 240e008f311..9265c5f970d 100644 --- a/test/moves/alluring_voice.test.ts +++ b/test/moves/alluring_voice.test.ts @@ -29,20 +29,18 @@ describe("Moves - Alluring Voice", () => { .disableCrits() .enemySpecies(Species.MAGIKARP) .enemyAbility(Abilities.ICE_SCALES) - .enemyMoveset([Moves.HOWL]) + .enemyMoveset(Moves.HOWL) .startingLevel(10) .enemyLevel(10) - .starterSpecies(Species.FEEBAS) - .ability(Abilities.BALL_FETCH) - .moveset([Moves.ALLURING_VOICE]); + .ability(Abilities.BALL_FETCH); }); it("should confuse the opponent if their stat stages were raised", async () => { - await game.classicMode.startBattle(); + await game.classicMode.startBattle([Species.FEEBAS]); const enemy = game.scene.getEnemyPokemon()!; - game.move.select(Moves.ALLURING_VOICE); + game.move.use(Moves.ALLURING_VOICE); await game.setTurnOrder([BattlerIndex.ENEMY, BattlerIndex.PLAYER]); await game.phaseInterceptor.to(BerryPhase); diff --git a/test/moves/chloroblast.test.ts b/test/moves/chloroblast.test.ts index 175227bbd5e..b2130da83eb 100644 --- a/test/moves/chloroblast.test.ts +++ b/test/moves/chloroblast.test.ts @@ -1,3 +1,4 @@ +import { MoveResult } from "#app/field/pokemon"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; import { Species } from "#enums/species"; @@ -22,21 +23,23 @@ describe("Moves - Chloroblast", () => { beforeEach(() => { game = new GameManager(phaserGame); game.override - .moveset([Moves.CHLOROBLAST]) .ability(Abilities.BALL_FETCH) .battleStyle("single") .disableCrits() .enemySpecies(Species.MAGIKARP) - .enemyAbility(Abilities.BALL_FETCH) - .enemyMoveset(Moves.PROTECT); + .enemyAbility(Abilities.BALL_FETCH); }); it("should not deal recoil damage if the opponent uses protect", async () => { await game.classicMode.startBattle([Species.FEEBAS]); - game.move.select(Moves.CHLOROBLAST); - await game.phaseInterceptor.to("BerryPhase"); + game.move.use(Moves.CHLOROBLAST); + await game.move.forceEnemyMove(Moves.PROTECT); + await game.toEndOfTurn(); - expect(game.scene.getPlayerPokemon()!.isFullHp()).toBe(true); + const player = game.field.getPlayerPokemon(); + + expect(player.isFullHp()).toBe(true); + expect(player.getLastXMoves()[0]).toMatchObject({ result: MoveResult.MISS, move: Moves.CHLOROBLAST }); }); }); diff --git a/test/moves/powder.test.ts b/test/moves/powder.test.ts index 457beb60f91..c6114db3ff1 100644 --- a/test/moves/powder.test.ts +++ b/test/moves/powder.test.ts @@ -1,15 +1,15 @@ -import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; -import Phaser from "phaser"; -import GameManager from "#test/testUtils/gameManager"; +import { BattlerIndex } from "#app/battle"; +import { MoveResult, PokemonMove } from "#app/field/pokemon"; +import { BerryPhase } from "#app/phases/berry-phase"; +import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; -import { Species } from "#enums/species"; -import { BerryPhase } from "#app/phases/berry-phase"; -import { MoveResult, PokemonMove } from "#app/field/pokemon"; import { PokemonType } from "#enums/pokemon-type"; -import { MoveEffectPhase } from "#app/phases/move-effect-phase"; +import { Species } from "#enums/species"; import { StatusEffect } from "#enums/status-effect"; -import { BattlerIndex } from "#app/battle"; +import GameManager from "#test/testUtils/gameManager"; +import Phaser from "phaser"; +import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; describe("Moves - Powder", () => { let phaserGame: Phaser.Game; @@ -161,7 +161,6 @@ describe("Moves - Powder", () => { game.move.select(Moves.ROAR, 0, BattlerIndex.ENEMY_2); game.move.select(Moves.SPLASH, 1); await game.toNextTurn(); - await game.toNextTurn(); // Requires game.toNextTurn() twice due to double battle // Turn 2: Enemy should activate Powder twice: From using Ember, and from copying Fiery Dance via Dancer playerPokemon.hp = playerPokemon.getMaxHp(); diff --git a/test/testUtils/gameManager.ts b/test/testUtils/gameManager.ts index 8dd90decf1a..07cea3b1328 100644 --- a/test/testUtils/gameManager.ts +++ b/test/testUtils/gameManager.ts @@ -5,6 +5,7 @@ import { getMoveTargets } from "#app/data/moves/move"; import type { EnemyPokemon, PlayerPokemon } from "#app/field/pokemon"; import Trainer from "#app/field/trainer"; import { GameModes, getGameMode } from "#app/game-mode"; +import { globalScene } from "#app/global-scene"; import { ModifierTypeOption, modifierTypes } from "#app/modifier/modifier-type"; import overrides from "#app/overrides"; import { CheckSwitchPhase } from "#app/phases/check-switch-phase"; @@ -22,15 +23,13 @@ import { TitlePhase } from "#app/phases/title-phase"; import { TurnEndPhase } from "#app/phases/turn-end-phase"; import { TurnInitPhase } from "#app/phases/turn-init-phase"; import { TurnStartPhase } from "#app/phases/turn-start-phase"; -import ErrorInterceptor from "#test/testUtils/errorInterceptor"; -import type InputsHandler from "#test/testUtils/inputsHandler"; import type BallUiHandler from "#app/ui/ball-ui-handler"; import type BattleMessageUiHandler from "#app/ui/battle-message-ui-handler"; import type CommandUiHandler from "#app/ui/command-ui-handler"; import type ModifierSelectUiHandler from "#app/ui/modifier-select-ui-handler"; import type PartyUiHandler from "#app/ui/party-ui-handler"; +import type StarterSelectUiHandler from "#app/ui/starter-select-ui-handler"; import type TargetSelectUiHandler from "#app/ui/target-select-ui-handler"; -import { UiMode } from "#enums/ui-mode"; import { isNullOrUndefined } from "#app/utils/common"; import { BattleStyle } from "#enums/battle-style"; import { Button } from "#enums/buttons"; @@ -40,24 +39,26 @@ import type { Moves } from "#enums/moves"; import type { MysteryEncounterType } from "#enums/mystery-encounter-type"; import { PlayerGender } from "#enums/player-gender"; import type { Species } from "#enums/species"; +import { UiMode } from "#enums/ui-mode"; +import ErrorInterceptor from "#test/testUtils/errorInterceptor"; import { generateStarter, waitUntil } from "#test/testUtils/gameManagerUtils"; import GameWrapper from "#test/testUtils/gameWrapper"; import { ChallengeModeHelper } from "#test/testUtils/helpers/challengeModeHelper"; import { ClassicModeHelper } from "#test/testUtils/helpers/classicModeHelper"; import { DailyModeHelper } from "#test/testUtils/helpers/dailyModeHelper"; +import { FieldHelper } from "#test/testUtils/helpers/field-helper"; import { ModifierHelper } from "#test/testUtils/helpers/modifiersHelper"; import { MoveHelper } from "#test/testUtils/helpers/moveHelper"; import { OverridesHelper } from "#test/testUtils/helpers/overridesHelper"; import { ReloadHelper } from "#test/testUtils/helpers/reloadHelper"; import { SettingsHelper } from "#test/testUtils/helpers/settingsHelper"; +import type InputsHandler from "#test/testUtils/inputsHandler"; +import { MockFetch } from "#test/testUtils/mocks/mockFetch"; import PhaseInterceptor from "#test/testUtils/phaseInterceptor"; import TextInterceptor from "#test/testUtils/TextInterceptor"; import { AES, enc } from "crypto-js"; import fs from "node:fs"; import { expect, vi } from "vitest"; -import { globalScene } from "#app/global-scene"; -import type StarterSelectUiHandler from "#app/ui/starter-select-ui-handler"; -import { MockFetch } from "#test/testUtils/mocks/mockFetch"; /** * Class to manage the game state and transitions between phases. @@ -76,6 +77,7 @@ export default class GameManager { public readonly settings: SettingsHelper; public readonly reload: ReloadHelper; public readonly modifiers: ModifierHelper; + public readonly field: FieldHelper; /** * Creates an instance of GameManager. @@ -123,6 +125,7 @@ export default class GameManager { this.settings = new SettingsHelper(this); this.reload = new ReloadHelper(this); this.modifiers = new ModifierHelper(this); + this.field = new FieldHelper(this); this.override.sanitizeOverrides(); // Disables Mystery Encounters on all tests (can be overridden at test level) @@ -383,6 +386,7 @@ export default class GameManager { * @param moveId - The {@linkcode Moves | move} the enemy will use * @param target - The {@linkcode BattlerIndex} of the target against which the enemy will use the given move; * will use normal target selection priorities if omitted. + * @deprecated Use {@linkcode MoveHelper.forceEnemyMove} or {@linkcode MoveHelper.selectEnemyMove} */ async forceEnemyMove(moveId: Moves, target?: BattlerIndex) { // Wait for the next EnemyCommandPhase to start @@ -417,9 +421,15 @@ export default class GameManager { }; } - /** Transition to the next upcoming {@linkcode CommandPhase} */ + /** Transition to the first {@linkcode CommandPhase} of the next turn. */ async toNextTurn() { - await this.phaseInterceptor.to(CommandPhase); + await this.phaseInterceptor.to("TurnInitPhase"); + await this.phaseInterceptor.to("CommandPhase"); + } + + /** Transition to the {@linkcode TurnEndPhase | end of the current turn}. */ + async toEndOfTurn() { + await this.phaseInterceptor.to("TurnEndPhase"); } /** @@ -541,8 +551,8 @@ export default class GameManager { } /** - * Select a pokemon from the party menu during the given phase. - * Only really handles the basic case of "navigate to party slot and press Action twice" - + * Select a pokemon from the party menu during the given phase. + * Only really handles the basic case of "navigate to party slot and press Action twice" - * any menus that come up afterwards are ignored and must be handled separately by the caller. * @param slot - The 0-indexed position of the pokemon in your party to switch to * @param inPhase - Which phase to expect the selection to occur in. Defaults to `SwitchPhase` diff --git a/test/testUtils/helpers/field-helper.ts b/test/testUtils/helpers/field-helper.ts new file mode 100644 index 00000000000..6d762853cad --- /dev/null +++ b/test/testUtils/helpers/field-helper.ts @@ -0,0 +1,87 @@ +// -- start tsdoc imports -- +// biome-ignore lint/correctness/noUnusedImports: TSDoc import +import type { globalScene } from "#app/global-scene"; +// -- end tsdoc imports -- + +import type { BattlerIndex } from "#app/battle"; +import type { Ability } from "#app/data/abilities/ability-class"; +import { allAbilities } from "#app/data/data-lists"; +import type Pokemon from "#app/field/pokemon"; +import type { EnemyPokemon, PlayerPokemon } from "#app/field/pokemon"; +import type { Abilities } from "#enums/abilities"; +import type { PokemonType } from "#enums/pokemon-type"; +import { Stat } from "#enums/stat"; +import { GameManagerHelper } from "#test/testUtils/helpers/gameManagerHelper"; +import { expect, type MockInstance, vi } from "vitest"; + +/** Helper to manage pokemon */ +export class FieldHelper extends GameManagerHelper { + /** + * Passthrough for {@linkcode globalScene.getPlayerPokemon} that adds an `undefined` check for + * the Pokemon so that the return type for the function doesn't have `undefined`. + * This removes the need to add a `!` like when calling `game.scene.getPlayerPokemon()!`. + * @param includeSwitching Whether a pokemon that is currently switching out is valid, default `true` + * @returns The first {@linkcode PlayerPokemon} that is {@linkcode globalScene.getPlayerField on the field} + * and {@linkcode PlayerPokemon.isActive is active} + * (aka {@linkcode PlayerPokemon.isAllowedInBattle is allowed in battle}). + */ + public getPlayerPokemon(includeSwitching = true): PlayerPokemon { + const pokemon = this.game.scene.getPlayerPokemon(includeSwitching); + expect(pokemon).toBeDefined(); + return pokemon!; + } + + /** + * Passthrough for {@linkcode globalScene.getEnemyPokemon} that adds an `undefined` check for + * the Pokemon so that the return type for the function doesn't have `undefined`. + * This removes the need to add a `!` like when calling `game.scene.getEnemyPokemon()!`. + * @param includeSwitching Whether a pokemon that is currently switching out is valid, default `true` + * @returns The first {@linkcode EnemyPokemon} that is {@linkcode globalScene.getEnemyField on the field} + * and {@linkcode EnemyPokemon.isActive is active} + * (aka {@linkcode EnemyPokemon.isAllowedInBattle is allowed in battle}). + */ + public getEnemyPokemon(includeSwitching = true): EnemyPokemon { + const pokemon = this.game.scene.getEnemyPokemon(includeSwitching); + expect(pokemon).toBeDefined(); + return pokemon!; + } + + /** + * @returns The {@linkcode BattlerIndex | indexes} of Pokemon on the field in order of decreasing Speed. + * Speed ties are returned in increasing order of index. + * + * Note: Trick Room does not modify the speed of Pokemon on the field. + */ + public getSpeedOrder(): BattlerIndex[] { + return this.game.scene + .getField(true) + .sort((pA, pB) => pB.getEffectiveStat(Stat.SPD) - pA.getEffectiveStat(Stat.SPD)) + .map(p => p.getBattlerIndex()); + } + + /** + * Mocks a pokemon's ability, overriding its existing ability (takes precedence over global overrides) + * @param pokemon - The pokemon to mock the ability of + * @param ability - The ability to be mocked + * @returns A {@linkcode MockInstance} object + * @see {@linkcode vi.spyOn} + * @see https://vitest.dev/api/mock#mockreturnvalue + */ + public mockAbility(pokemon: Pokemon, ability: Abilities): MockInstance<(baseOnly?: boolean) => Ability> { + return vi.spyOn(pokemon, "getAbility").mockReturnValue(allAbilities[ability]); + } + + /** + * Forces a pokemon to be terastallized. Defaults to the pokemon's primary type if not specified. + * + * This function only mocks the Pokemon's tera-related variables; it does NOT activate any tera-related abilities. + * + * @param pokemon - The pokemon to terastallize. + * @param teraType - (optional) The {@linkcode PokemonType} to terastallize it as. + */ + public forceTera(pokemon: Pokemon, teraType?: PokemonType): void { + vi.spyOn(pokemon, "isTerastallized", "get").mockReturnValue(true); + teraType ??= pokemon.getSpeciesForm(true).type1; + vi.spyOn(pokemon, "teraType", "get").mockReturnValue(teraType); + } +} diff --git a/test/testUtils/helpers/moveHelper.ts b/test/testUtils/helpers/moveHelper.ts index 269cf65ea56..ab10486867d 100644 --- a/test/testUtils/helpers/moveHelper.ts +++ b/test/testUtils/helpers/moveHelper.ts @@ -1,14 +1,16 @@ import type { BattlerIndex } from "#app/battle"; +import { getMoveTargets } from "#app/data/moves/move"; import { Button } from "#app/enums/buttons"; import type Pokemon from "#app/field/pokemon"; import { PokemonMove } from "#app/field/pokemon"; import Overrides from "#app/overrides"; import type { CommandPhase } from "#app/phases/command-phase"; +import type { EnemyCommandPhase } from "#app/phases/enemy-command-phase"; import { LearnMovePhase } from "#app/phases/learn-move-phase"; import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import { Command } from "#app/ui/command-ui-handler"; -import { UiMode } from "#enums/ui-mode"; import { Moves } from "#enums/moves"; +import { UiMode } from "#enums/ui-mode"; import { getMovePosition } from "#test/testUtils/gameManagerUtils"; import { GameManagerHelper } from "#test/testUtils/helpers/gameManagerHelper"; import { vi } from "vitest"; @@ -92,6 +94,35 @@ export class MoveHelper extends GameManagerHelper { } } + /** + * Modifies a player pokemon's moveset to contain only the selected move and then + * selects it to be used during the next {@linkcode CommandPhase}. + * + * Warning: Will disable the player moveset override if it is enabled! + * + * Note: If you need to check for changes in the player's moveset as part of the test, it may be + * best to use {@linkcode changeMoveset} and {@linkcode select} instead. + * @param moveId - the move to use + * @param pkmIndex - the pokemon index. Relevant for double-battles only (defaults to 0) + * @param targetIndex - (optional) The {@linkcode BattlerIndex} of the Pokemon to target for single-target moves, or `null` if a manual call to `selectTarget()` is required + * @param useTera - If `true`, the Pokemon also chooses to Terastallize. This does not require a Tera Orb. Default: `false`. + */ + public use(moveId: Moves, pkmIndex: 0 | 1 = 0, targetIndex?: BattlerIndex | null, useTera = false): void { + if ([Overrides.MOVESET_OVERRIDE].flat().length > 0) { + vi.spyOn(Overrides, "MOVESET_OVERRIDE", "get").mockReturnValue([]); + console.warn("Warning: `use` overwrites the Pokemon's moveset and disables the player moveset override!"); + } + + const pokemon = this.game.scene.getPlayerField()[pkmIndex]; + pokemon.moveset = [new PokemonMove(moveId)]; + + if (useTera) { + this.selectWithTera(moveId, pkmIndex, targetIndex); + return; + } + this.select(moveId, pkmIndex, targetIndex); + } + /** * Forces the Paralysis or Freeze status to activate on the next move by temporarily mocking {@linkcode Overrides.STATUS_ACTIVATION_OVERRIDE}, * advancing to the next `MovePhase`, and then resetting the override to `null` @@ -132,6 +163,77 @@ export class MoveHelper extends GameManagerHelper { console.log(`Pokemon ${pokemon.species.name}'s moveset manually set to ${movesetStr} (=[${moveset.join(", ")}])!`); } + /** + * Forces the next enemy selecting a move to use the given move in its moveset + * against the given target (if applicable). + * @param moveId The {@linkcode MoveId | move} the enemy will use + * @param target (Optional) the {@linkcode BattlerIndex | target} which the enemy will use the given move against + */ + public async selectEnemyMove(moveId: Moves, target?: BattlerIndex) { + // Wait for the next EnemyCommandPhase to start + await this.game.phaseInterceptor.to("EnemyCommandPhase", false); + const enemy = + this.game.scene.getEnemyField()[(this.game.scene.getCurrentPhase() as EnemyCommandPhase).getFieldIndex()]; + const legalTargets = getMoveTargets(enemy, moveId); + + vi.spyOn(enemy, "getNextMove").mockReturnValueOnce({ + move: moveId, + targets: + target !== undefined && !legalTargets.multiple && legalTargets.targets.includes(target) + ? [target] + : enemy.getNextTargets(moveId), + }); + + /** + * Run the EnemyCommandPhase to completion. + * This allows this function to be called consecutively to + * force a move for each enemy in a double battle. + */ + await this.game.phaseInterceptor.to("EnemyCommandPhase"); + } + + /** + * Forces the next enemy selecting a move to use the given move against the given target (if applicable). + * + * Warning: Overwrites the pokemon's moveset and disables the moveset override! + * + * Note: If you need to check for changes in the enemy's moveset as part of the test, it may be + * best to use {@linkcode changeMoveset} and {@linkcode selectEnemyMove} instead. + * @param moveId The {@linkcode MoveId | move} the enemy will use + * @param target (Optional) the {@linkcode BattlerIndex | target} which the enemy will use the given move against + */ + public async forceEnemyMove(moveId: Moves, target?: BattlerIndex) { + // Wait for the next EnemyCommandPhase to start + await this.game.phaseInterceptor.to("EnemyCommandPhase", false); + + const enemy = + this.game.scene.getEnemyField()[(this.game.scene.getCurrentPhase() as EnemyCommandPhase).getFieldIndex()]; + + if ([Overrides.OPP_MOVESET_OVERRIDE].flat().length > 0) { + vi.spyOn(Overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue([]); + console.warn( + "Warning: `forceEnemyMove` overwrites the Pokemon's moveset and disables the enemy moveset override!", + ); + } + enemy.moveset = [new PokemonMove(moveId)]; + const legalTargets = getMoveTargets(enemy, moveId); + + vi.spyOn(enemy, "getNextMove").mockReturnValueOnce({ + move: moveId, + targets: + target !== undefined && !legalTargets.multiple && legalTargets.targets.includes(target) + ? [target] + : enemy.getNextTargets(moveId), + }); + + /** + * Run the EnemyCommandPhase to completion. + * This allows this function to be called consecutively to + * force a move for each enemy in a double battle. + */ + await this.game.phaseInterceptor.to("EnemyCommandPhase"); + } + /** * Simulates learning a move for a player pokemon. * @param move The {@linkcode Moves} being learnt diff --git a/test/ui/pokedex.test.ts b/test/ui/pokedex.test.ts index ff5ca116ba8..007fc43c3b9 100644 --- a/test/ui/pokedex.test.ts +++ b/test/ui/pokedex.test.ts @@ -8,7 +8,7 @@ import { Abilities } from "#enums/abilities"; import { Species } from "#enums/species"; import { allSpecies, getPokemonSpecies, type PokemonForm } from "#app/data/pokemon-species"; import { Button } from "#enums/buttons"; -import { DropDownColumn } from "#app/ui/filter-bar"; +import { DropDownColumn } from "#enums/drop-down-column"; import type PokemonSpecies from "#app/data/pokemon-species"; import { PokemonType } from "#enums/pokemon-type"; import { UiMode } from "#enums/ui-mode";