[Dev] Move function from testUtils.ts to gameManager.ts (2/3) (#3432)

* move mockTurnOrder to gameManager

* change name

* fix test failing

* fix purify test

* fix typo
This commit is contained in:
Adrian T. 2024-08-09 23:07:55 +08:00 committed by GitHub
parent dcbdc511cf
commit bfdcd4fc1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 46 additions and 52 deletions

View File

@ -11,7 +11,6 @@ import { Moves } from "#enums/moves";
import { Species } from "#enums/species";
import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { mockTurnOrder } from "../utils/testUtils";
import { BattlerIndex } from "#app/battle.js";
@ -57,7 +56,7 @@ describe("Abilities - Serene Grace", () => {
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
// Check chance of Air Slash without Serene Grace
@ -90,7 +89,7 @@ describe("Abilities - Serene Grace", () => {
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
// Check chance of Air Slash with Serene Grace

View File

@ -11,7 +11,6 @@ import { Moves } from "#enums/moves";
import { Species } from "#enums/species";
import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { mockTurnOrder } from "../utils/testUtils";
import { BattlerIndex } from "#app/battle.js";
@ -58,7 +57,7 @@ describe("Abilities - Sheer Force", () => {
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
const phase = game.scene.getCurrentPhase() as MoveEffectPhase;
@ -97,7 +96,7 @@ describe("Abilities - Sheer Force", () => {
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
const phase = game.scene.getCurrentPhase() as MoveEffectPhase;
@ -136,7 +135,7 @@ describe("Abilities - Sheer Force", () => {
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
const phase = game.scene.getCurrentPhase() as MoveEffectPhase;
@ -177,7 +176,7 @@ describe("Abilities - Sheer Force", () => {
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
const phase = game.scene.getCurrentPhase() as MoveEffectPhase;

View File

@ -12,7 +12,6 @@ import { Species } from "#enums/species";
import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { BattlerIndex } from "#app/battle.js";
import { mockTurnOrder } from "../utils/testUtils";
describe("Abilities - Shield Dust", () => {
@ -58,7 +57,7 @@ describe("Abilities - Shield Dust", () => {
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
// Shield Dust negates secondary effect

View File

@ -12,7 +12,6 @@ import { Species } from "#enums/species";
import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, test } from "vitest";
import { BattlerIndex } from "#app/battle.js";
import { mockTurnOrder } from "../utils/testUtils";
const TIMEOUT = 20 * 1000;
@ -58,7 +57,8 @@ describe("Abilities - ZEN MODE", () => {
const movePosition = getMovePosition(game.scene, 0, moveToUse);
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.ENEMY, BattlerIndex.PLAYER]);
await game.setTurnOrder([BattlerIndex.ENEMY, BattlerIndex.PLAYER]);
await game.phaseInterceptor.to(DamagePhase, false);
// await game.phaseInterceptor.runFrom(DamagePhase).to(DamagePhase, false);
const damagePhase = game.scene.getCurrentPhase() as DamagePhase;
@ -86,7 +86,8 @@ describe("Abilities - ZEN MODE", () => {
const movePosition = getMovePosition(game.scene, 0, moveToUse);
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.ENEMY, BattlerIndex.PLAYER]);
await game.setTurnOrder([BattlerIndex.ENEMY, BattlerIndex.PLAYER]);
await game.phaseInterceptor.to(QuietFormChangePhase);
await game.phaseInterceptor.to(TurnInitPhase, false);
expect(game.scene.getParty()[0].hp).not.toBe(100);
@ -111,7 +112,8 @@ describe("Abilities - ZEN MODE", () => {
const movePosition = getMovePosition(game.scene, 0, moveToUse);
(game.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
});
await mockTurnOrder(game, [BattlerIndex.ENEMY, BattlerIndex.PLAYER]);
await game.setTurnOrder([BattlerIndex.ENEMY, BattlerIndex.PLAYER]);
await game.phaseInterceptor.to(DamagePhase, false);
// await game.phaseInterceptor.runFrom(DamagePhase).to(DamagePhase, false);
const damagePhase = game.scene.getCurrentPhase() as DamagePhase;

View File

@ -8,7 +8,6 @@ import { Moves } from "#enums/moves";
import { Species } from "#enums/species";
import Phase from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { mockTurnOrder } from "#test/utils/testUtils";
describe("Items - Leek", () => {
let phaserGame: Phaser.Game;
@ -44,7 +43,7 @@ describe("Items - Leek", () => {
game.doAttack(0);
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEffectPhase);

View File

@ -8,7 +8,6 @@ import { Moves } from "#enums/moves";
import { Species } from "#enums/species";
import Phase from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { mockTurnOrder } from "#test/utils/testUtils";
describe("Items - Scope Lens", () => {
let phaserGame: Phaser.Game;
@ -43,7 +42,8 @@ describe("Items - Scope Lens", () => {
]);
game.doAttack(0);
await mockTurnOrder(game, [ BattlerIndex.PLAYER, BattlerIndex.ENEMY ]);
await game.setTurnOrder([ BattlerIndex.PLAYER, BattlerIndex.ENEMY ]);
await game.phaseInterceptor.to(MoveEffectPhase);

View File

@ -8,7 +8,6 @@ import { allMoves } from "#app/data/move";
import { BattlerIndex } from "#app/battle";
import { Species } from "#enums/species";
import { Moves } from "#enums/moves";
import { mockTurnOrder } from "#test/utils/testUtils";
describe("Moves - Fusion Flare and Fusion Bolt", () => {
let phaserGame: Phaser.Game;
@ -56,7 +55,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.doSelectTarget(BattlerIndex.ENEMY);
// Force user party to act before enemy party
await mockTurnOrder(game, [ BattlerIndex.PLAYER, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2 ]);
await game.setTurnOrder([ BattlerIndex.PLAYER, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2 ]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
expect((game.scene.getCurrentPhase() as MoveEffectPhase).move.moveId).toBe(fusionFlare.id);
@ -82,7 +81,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.doSelectTarget(BattlerIndex.ENEMY);
// Force user party to act before enemy party
await mockTurnOrder(game, [ BattlerIndex.PLAYER, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2 ]);
await game.setTurnOrder([ BattlerIndex.PLAYER, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2 ]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
expect((game.scene.getCurrentPhase() as MoveEffectPhase).move.moveId).toBe(fusionBolt.id);
@ -108,7 +107,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.doSelectTarget(0);
// Force first enemy to act (and fail) in between party
await mockTurnOrder(game, [ BattlerIndex.PLAYER, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY ]);
await game.setTurnOrder([ BattlerIndex.PLAYER, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY ]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
expect((game.scene.getCurrentPhase() as MoveEffectPhase).move.moveId).toBe(fusionFlare.id);
@ -140,7 +139,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.doSelectTarget(BattlerIndex.ENEMY);
// Force first enemy to act in between party
await mockTurnOrder(game, [ BattlerIndex.PLAYER, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY ]);
await game.setTurnOrder([ BattlerIndex.PLAYER, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY ]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
expect((game.scene.getCurrentPhase() as MoveEffectPhase).move.moveId).toBe(fusionFlare.id);
@ -170,7 +169,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.doSelectTarget(BattlerIndex.PLAYER);
// Force user party to act before enemy party
await mockTurnOrder(game, [ BattlerIndex.PLAYER, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2 ]);
await game.setTurnOrder([ BattlerIndex.PLAYER, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2 ]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
expect((game.scene.getCurrentPhase() as MoveEffectPhase).move.moveId).toBe(fusionBolt.id);
@ -222,7 +221,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.doSelectTarget(BattlerIndex.ENEMY);
// Force first enemy to act in between party
await mockTurnOrder(game, [ BattlerIndex.PLAYER, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY ]);
await game.setTurnOrder([ BattlerIndex.PLAYER, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY ]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
expect((game.scene.getCurrentPhase() as MoveEffectPhase).move.moveId).toBe(fusionBolt.id);
@ -284,7 +283,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => {
game.doSelectTarget(BattlerIndex.PLAYER);
// Force first enemy to act in between party
await mockTurnOrder(game, [ BattlerIndex.PLAYER, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY ]);
await game.setTurnOrder([ BattlerIndex.PLAYER, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2, BattlerIndex.ENEMY ]);
await game.phaseInterceptor.to(MoveEffectPhase, false);
expect((game.scene.getCurrentPhase() as MoveEffectPhase).move.moveId).toBe(fusionBolt.id);

View File

@ -6,7 +6,7 @@ import { MoveResult } from "#app/field/pokemon.js";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import GameManager from "#test/utils/gameManager";
import { getMovePosition } from "#test/utils/gameManagerUtils";
import { mockTurnOrder, SPLASH_ONLY } from "#test/utils/testUtils";
import { SPLASH_ONLY } from "#test/utils/testUtils";
const TIMEOUT = 20 * 1000;
@ -75,7 +75,7 @@ describe("Moves - Gastro Acid", () => {
game.doAttack(getMovePosition(game.scene, 0, Moves.CORE_ENFORCER));
// Force player to be slower to enable Core Enforcer to proc its suppression effect
await mockTurnOrder(game, [BattlerIndex.ENEMY, BattlerIndex.PLAYER]);
await game.setTurnOrder([BattlerIndex.ENEMY, BattlerIndex.PLAYER]);
await game.phaseInterceptor.to("TurnInitPhase");

View File

@ -2,7 +2,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import Phaser from "phaser";
import GameManager from "#test/utils/gameManager";
import { Species } from "#app/enums/species.js";
import { mockTurnOrder, SPLASH_ONLY } from "../utils/testUtils";
import { SPLASH_ONLY } from "../utils/testUtils";
import { Moves } from "#app/enums/moves.js";
import { getMovePosition } from "../utils/gameManagerUtils";
import { MoveEffectPhase } from "#app/phases.js";
@ -39,7 +39,7 @@ describe("Internals", () => {
const enemy = game.scene.getEnemyPokemon()!;
game.doAttack(getMovePosition(game.scene, 0, Moves.CONFUSION));
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.toNextTurn();
expect(enemy.hp).toBe(enemy.getMaxHp());

View File

@ -7,7 +7,6 @@ import { Moves } from "#enums/moves";
import { Species } from "#enums/species";
import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, test } from "vitest";
import { mockTurnOrder } from "../utils/testUtils";
import { BattlerIndex } from "#app/battle.js";
const TIMEOUT = 20 * 1000;
@ -51,7 +50,7 @@ describe("Moves - Purify", () => {
enemyPokemon.status = new Status(StatusEffect.BURN);
game.doAttack(getMovePosition(game.scene, 0, Moves.PURIFY));
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEndPhase);
expect(enemyPokemon.status).toBeNull();
@ -71,7 +70,7 @@ describe("Moves - Purify", () => {
const playerInitialHp = playerPokemon.hp;
game.doAttack(getMovePosition(game.scene, 0, Moves.PURIFY));
await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY]);
await game.phaseInterceptor.to(MoveEndPhase);
expect(playerPokemon.hp).toBe(playerInitialHp);

View File

@ -29,6 +29,7 @@ import overrides from "#app/overrides.js";
import { removeEnemyHeldItems } from "./testUtils";
import ModifierSelectUiHandler from "#app/ui/modifier-select-ui-handler.js";
import { MoveHelper } from "./moveHelper";
import { vi } from "vitest";
/**
* Class to manage the game state and transitions between phases.
@ -357,4 +358,19 @@ export default class GameManager {
partyHandler.processInput(Button.ACTION); // send out (or whatever option is at the top)
});
}
/**
* Intercepts `TurnStartPhase` and mocks the getOrder's return value {@linkcode TurnStartPhase.getOrder}
* Used to modify the turn order.
* @param {BattlerIndex[]} order The turn order to set
* @example
* ```ts
* await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2]);
* ```
*/
async setTurnOrder(order: BattlerIndex[]): Promise<void> {
await this.phaseInterceptor.to(TurnStartPhase, false);
vi.spyOn(this.scene.getCurrentPhase() as TurnStartPhase, "getOrder").mockReturnValue(order);
}
}

View File

@ -20,7 +20,7 @@ export class MoveHelper extends GameManagerHelper {
* Intercepts `MoveEffectPhase` and mocks the hitCheck's
* return value to `false` {@linkcode MoveEffectPhase.hitCheck}.
* Used to force a move to miss.
* @param firstTargetOnly Whether the move should force miss on the first target only, in the case of multi-hit moves.
* @param firstTargetOnly Whether the move should force miss on the first target only, in the case of multi-target moves.
*/
async forceMiss(firstTargetOnly: boolean = false): Promise<void> {
await this.game.phaseInterceptor.to(MoveEffectPhase, false);

View File

@ -2,8 +2,6 @@ import { Moves } from "#app/enums/moves.js";
import i18next, { type ParseKeys } from "i18next";
import { vi } from "vitest";
import GameManager from "./gameManager";
import { BattlerIndex } from "#app/battle.js";
import { TurnStartPhase } from "#app/phases.js";
/** Ready to use array of Moves.SPLASH x4 */
export const SPLASH_ONLY = [Moves.SPLASH, Moves.SPLASH, Moves.SPLASH, Moves.SPLASH];
@ -38,19 +36,3 @@ export function removeEnemyHeldItems(game: GameManager): void {
game.scene.clearEnemyModifiers();
console.log("Enemy held items removed");
}
/**
* Intercepts `TurnStartPhase` and mocks the getOrder's return value {@linkcode TurnStartPhase.getOrder}
* Used to modify the turn order.
* @param {GameManager} game The GameManager instance
* @param {BattlerIndex[]} order The turn order to set
* @example
* ```ts
* await mockTurnOrder(game, [BattlerIndex.PLAYER, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2]);
* ```
*/
export async function mockTurnOrder(game: GameManager, order: BattlerIndex[]): Promise<void> {
await game.phaseInterceptor.to(TurnStartPhase, false);
vi.spyOn(game.scene.getCurrentPhase() as TurnStartPhase, "getOrder").mockReturnValue(order);
}