diff --git a/test/abilities/intimidate.test.ts b/test/abilities/intimidate.test.ts index 3dcd9bcd129..6790e2b98d3 100644 --- a/test/abilities/intimidate.test.ts +++ b/test/abilities/intimidate.test.ts @@ -3,7 +3,6 @@ import Phaser from "phaser"; import GameManager from "#test/testUtils/gameManager"; import { UiMode } from "#enums/ui-mode"; import { Stat } from "#enums/stat"; -import { getMovePosition } from "#test/testUtils/gameManagerUtils"; import { AbilityId } from "#enums/ability-id"; import { MoveId } from "#enums/move-id"; import { SpeciesId } from "#enums/species-id"; @@ -114,7 +113,7 @@ describe("Abilities - Intimidate", () => { expect(enemyPokemon.getStatStage(Stat.ATK)).toBe(-1); expect(playerPokemon.getStatStage(Stat.ATK)).toBe(-1); - game.move.select(getMovePosition(game.scene, 0, MoveId.SPLASH)); + game.move.select(MoveId.SPLASH); await game.toNextTurn(); enemyPokemon = game.scene.getEnemyPokemon()!; diff --git a/test/abilities/moxie.test.ts b/test/abilities/moxie.test.ts index a85ed081448..1c4acf139e4 100644 --- a/test/abilities/moxie.test.ts +++ b/test/abilities/moxie.test.ts @@ -65,8 +65,7 @@ describe("Abilities - Moxie", () => { secondPokemon.hp = 1; - game.move.select(moveToUse); - game.selectTarget(BattlerIndex.PLAYER_2); + game.move.select(moveToUse, BattlerIndex.PLAYER_2); await game.phaseInterceptor.to(TurnEndPhase); diff --git a/test/testUtils/gameManager.ts b/test/testUtils/gameManager.ts index b9f499d4e0c..f4ba31499d4 100644 --- a/test/testUtils/gameManager.ts +++ b/test/testUtils/gameManager.ts @@ -201,9 +201,8 @@ export default class GameManager { /** * Helper function to run to the final boss encounter as it's a bit tricky due to extra dialogue * Also handles Major/Minor bosses from endless modes - * @param game - The game manager - * @param species - * @param mode + * @param species - Array of {@linkcode SpeciesId}s to start the final battle with. + * @param mode - The {@linkcode GameModes} to spawn the final boss encounter in. */ async runToFinalBossEncounter(species: SpeciesId[], mode: GameModes) { console.log("===to final boss encounter==="); @@ -230,9 +229,9 @@ export default class GameManager { /** * Runs the game to a mystery encounter phase. - * @param encounterType if specified, will expect encounter to have been spawned - * @param species Optional array of species for party. - * @returns A promise that resolves when the EncounterPhase ends. + * @param encounterType - If specified, will expect encounter to be the given type. + * @param species - Optional array of species for party to start with. + * @returns A Promise that resolves when the EncounterPhase ends. */ async runToMysteryEncounter(encounterType?: MysteryEncounterType, species?: SpeciesId[]) { if (!isNullOrUndefined(encounterType)) { @@ -277,6 +276,7 @@ export default class GameManager { * Will trigger during the next {@linkcode SelectTargetPhase} * @param targetIndex - The {@linkcode BattlerIndex} of the attack target, or `undefined` for multi-target attacks * @param movePosition - The 0-indexed position of the move in the pokemon's moveset array + * @throws Immediately fails tests */ selectTarget(movePosition: number, targetIndex?: BattlerIndex) { this.onNextPrompt( @@ -292,7 +292,7 @@ export default class GameManager { handler.setCursor(targetIndex !== undefined ? targetIndex : BattlerIndex.ENEMY); } if (move.isMultiTarget() && targetIndex !== undefined) { - throw new Error(`targetIndex was passed to selectMove() but move ("${move.name}") is not targetted`); + expect.fail(`targetIndex was passed to selectMove() but move ("${move.name}") is not targetted`); } handler.processInput(Button.ACTION); }, diff --git a/test/testUtils/gameManagerUtils.ts b/test/testUtils/gameManagerUtils.ts index 57fd9b91d26..2e1714c90ad 100644 --- a/test/testUtils/gameManagerUtils.ts +++ b/test/testUtils/gameManagerUtils.ts @@ -10,7 +10,7 @@ import { getGameMode } from "#app/game-mode"; import { GameModes } from "#enums/game-modes"; import type { StarterMoveset } from "#app/system/game-data"; import type { Starter } from "#app/ui/starter-select-ui-handler"; -import { MoveId } from "#enums/move-id"; +import type { MoveId } from "#enums/move-id"; import type { SpeciesId } from "#enums/species-id"; /** Function to convert Blob to string */ @@ -98,15 +98,6 @@ export function waitUntil(truth): Promise { }); } -/** Get the index of `move` from the moveset of the pokemon on the player's field at location `pokemonIndex`. */ -export function getMovePosition(scene: BattleScene, pokemonIndex: 0 | 1, move: MoveId): number { - const playerPokemon = scene.getPlayerField()[pokemonIndex]; - const moveSet = playerPokemon.getMoveset(); - const index = moveSet.findIndex(m => m.moveId === move && m.ppUsed < m.getMovePp()); - console.log(`Move position for ${MoveId[move]} (=${move}):`, index); - return index; -} - /** * Useful for populating party, wave index, etc. without having to spin up and run through an entire EncounterPhase */ diff --git a/test/testUtils/helpers/moveHelper.ts b/test/testUtils/helpers/moveHelper.ts index ed1441a6a2f..0c233ddba65 100644 --- a/test/testUtils/helpers/moveHelper.ts +++ b/test/testUtils/helpers/moveHelper.ts @@ -1,4 +1,4 @@ -import type { BattlerIndex } from "#enums/battler-index"; +import { BattlerIndex } from "#enums/battler-index"; import { getMoveTargets } from "#app/data/moves/move-utils"; import type Pokemon from "#app/field/pokemon"; import { PokemonMove } from "#app/data/moves/pokemon-move"; @@ -9,14 +9,13 @@ import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import { Command } from "#enums/command"; import { MoveId } from "#enums/move-id"; import { UiMode } from "#enums/ui-mode"; -import { getMovePosition } from "#test/testUtils/gameManagerUtils"; import { GameManagerHelper } from "#test/testUtils/helpers/gameManagerHelper"; -import { vi } from "vitest"; -import { coerceArray } from "#app/utils/common"; +import { expect, vi } from "vitest"; +import { coerceArray, toReadableString } from "#app/utils/common"; import { MoveUseMode } from "#enums/move-use-mode"; /** - * Helper to handle a Pokemon's move + * Helper to handle using a Pokemon's moves. */ export class MoveHelper extends GameManagerHelper { /** @@ -49,13 +48,25 @@ export class MoveHelper extends GameManagerHelper { } /** - * Select the move to be used by the given Pokemon(-index). Triggers during the next {@linkcode CommandPhase} - * @param move - the move to use - * @param pkmIndex - the pokemon index. Relevant for double-battles only (defaults to 0) - * @param targetIndex - The {@linkcode BattlerIndex} of the Pokemon to target for single-target moves, or `null` if a manual call to `selectTarget()` is required + * Select a move _already in the player's moveset_ to be used during the next {@linkcode CommandPhase}. + * @param move - The {@linkcode MoveId} to use. + * @param pkmIndex - The {@linkcode BattlerIndex} of the player Pokemon using the move. Relevant for double battles only and defaults to {@linkcode BattlerIndex.PLAYER} if not specified. + * @param targetIndex - The {@linkcode BattlerIndex} of the Pokemon to target for single-target moves; should be omitted for multi-target moves. + * If set to `null`, will forgo normal target selection entirely (useful for UI tests). + * @remarks + * Will fail the current test if the move being selected is not in the user's moveset. */ - public select(move: MoveId, pkmIndex: 0 | 1 = 0, targetIndex?: BattlerIndex | null) { - const movePosition = getMovePosition(this.game.scene, pkmIndex, move); + public select( + move: MoveId, + pkmIndex: BattlerIndex.PLAYER | BattlerIndex.PLAYER_2 = BattlerIndex.PLAYER, + targetIndex?: BattlerIndex | null, + ) { + const movePosition = this.getMovePosition(pkmIndex, move); + if (movePosition === -1) { + expect.fail( + `MoveHelper.select called with move ${toReadableString(MoveId[move])} not in moveset; Battler Index: ${BattlerIndex[pkmIndex]}`, + ); + } this.game.onNextPrompt("CommandPhase", UiMode.COMMAND, () => { this.game.scene.ui.setMode( @@ -77,14 +88,24 @@ export class MoveHelper extends GameManagerHelper { } /** - * Select the move to be used by the given Pokemon(-index), **which will also terastallize on this turn**. - * Triggers during the next {@linkcode CommandPhase} - * @param move - the move to use - * @param pkmIndex - the pokemon index. Relevant for double-battles only (defaults to 0) - * @param targetIndex - The {@linkcode BattlerIndex} of the Pokemon to target for single-target moves, or `null` if a manual call to `selectTarget()` is required + * Select a move _already in the player's moveset_ to be used during the next {@linkcode CommandPhase}, **which will also terastallize on this turn**. + * @param move - The {@linkcode MoveId} to use. + * @param pkmIndex - The {@linkcode BattlerIndex} of the player Pokemon using the move. Relevant for double battles only and defaults to {@linkcode BattlerIndex.PLAYER} if not specified. + * @param targetIndex - The {@linkcode BattlerIndex} of the Pokemon to target for single-target moves; should be omitted for multi-target moves. + * If set to `null`, will forgo normal target selection entirely (useful for UI tests) */ - public selectWithTera(move: MoveId, pkmIndex: 0 | 1 = 0, targetIndex?: BattlerIndex | null) { - const movePosition = getMovePosition(this.game.scene, pkmIndex, move); + public selectWithTera( + move: MoveId, + pkmIndex: BattlerIndex.PLAYER | BattlerIndex.PLAYER_2 = BattlerIndex.PLAYER, + targetIndex?: BattlerIndex | null, + ) { + const movePosition = this.getMovePosition(pkmIndex, move); + if (movePosition === -1) { + expect.fail( + `MoveHelper.selectWithTera called with move ${toReadableString(MoveId[move])} not in moveset;\nBattler Index: ${BattlerIndex[pkmIndex]};\nMoveset: ${this.game.scene.getField()[pkmIndex].getMoveset()}`, + ); + } + this.game.scene.getPlayerParty()[pkmIndex].isTerastallized = false; this.game.onNextPrompt("CommandPhase", UiMode.COMMAND, () => { @@ -107,6 +128,15 @@ export class MoveHelper extends GameManagerHelper { } } + /** Helper function to get the index of the selected move in the selected part member's moveset. */ + private getMovePosition(pokemonIndex: BattlerIndex.PLAYER | BattlerIndex.PLAYER_2, move: MoveId): number { + const playerPokemon = this.game.scene.getPlayerField()[pokemonIndex]; + const moveset = playerPokemon.getMoveset(); + const index = moveset.findIndex(m => m.moveId === move && m.ppUsed < m.getMovePp()); + console.log(`Move position for ${MoveId[move]} (=${move}):`, index); + return index; + } + /** * Modifies a player pokemon's moveset to contain only the selected move and then * selects it to be used during the next {@linkcode CommandPhase}. @@ -116,14 +146,19 @@ export class MoveHelper extends GameManagerHelper { * 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`. + * @param pkmIndex - The {@linkcode BattlerIndex} of the player Pokemon using the move. Relevant for double battles only and defaults to {@linkcode BattlerIndex.PLAYER} if not specified. + * @param targetIndex - The {@linkcode BattlerIndex} of the Pokemon to target for single-target moves; should be omitted for multi-target moves. + * @param useTera - If `true`, the Pokemon will attempt to Terastallize even without a Tera Orb; default `false`. */ - public use(moveId: MoveId, pkmIndex: 0 | 1 = 0, targetIndex?: BattlerIndex | null, useTera = false): void { + public use( + moveId: MoveId, + pkmIndex: BattlerIndex.PLAYER | BattlerIndex.PLAYER_2 = BattlerIndex.PLAYER, + targetIndex?: BattlerIndex, + 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!"); + console.warn("Warning: `MoveHelper.use` overwriting player pokemon moveset and disabling moveset override!"); } const pokemon = this.game.scene.getPlayerField()[pkmIndex];