This commit is contained in:
Bertie690 2025-09-22 21:44:46 -04:00 committed by GitHub
commit e9d74c578e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
34 changed files with 999 additions and 785 deletions

View File

@ -6,7 +6,7 @@ import { SpeciesId } from "#enums/species-id";
import { GameManager } from "#test/test-utils/game-manager"; import { GameManager } from "#test/test-utils/game-manager";
import i18next from "i18next"; import i18next from "i18next";
import Phaser from "phaser"; import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { beforeAll, beforeEach, describe, expect, it } from "vitest";
describe("{{description}}", () => { describe("{{description}}", () => {
let phaserGame: Phaser.Game; let phaserGame: Phaser.Game;
@ -18,10 +18,6 @@ describe("{{description}}", () => {
}); });
}); });
afterEach(() => {
game.phaseInterceptor.restoreOg();
});
beforeEach(() => { beforeEach(() => {
game = new GameManager(phaserGame); game = new GameManager(phaserGame);
game.override game.override

View File

@ -171,6 +171,7 @@ export class BattleScene extends SceneBase {
public sessionPlayTime: number | null = null; public sessionPlayTime: number | null = null;
public lastSavePlayTime: number | null = null; public lastSavePlayTime: number | null = null;
// TODO: move these settings into a settings helper object
public masterVolume = 0.5; public masterVolume = 0.5;
public bgmVolume = 1; public bgmVolume = 1;
public fieldVolume = 1; public fieldVolume = 1;
@ -358,7 +359,11 @@ export class BattleScene extends SceneBase {
); );
} }
async preload() { /**
* Load game assets necessary for the scene to run.
* Called by Phaser on new game start.
*/
public async preload(): Promise<void> {
if (DEBUG_RNG) { if (DEBUG_RNG) {
const originalRealInRange = Phaser.Math.RND.realInRange; const originalRealInRange = Phaser.Math.RND.realInRange;
Phaser.Math.RND.realInRange = function (min: number, max: number): number { Phaser.Math.RND.realInRange = function (min: number, max: number): number {
@ -389,7 +394,11 @@ export class BattleScene extends SceneBase {
}); });
} }
create() { /**
* Create game objects with loaded assets.
* Called by Phaser on new game start.
*/
public create(): void {
this.scene.remove(LoadingScene.KEY); this.scene.remove(LoadingScene.KEY);
initGameSpeed.apply(this); initGameSpeed.apply(this);
this.inputController = new InputsController(); this.inputController = new InputsController();
@ -414,6 +423,7 @@ export class BattleScene extends SceneBase {
this.ui?.update(); this.ui?.update();
} }
// TODO: Split this up into multiple sub-methods
launchBattle() { launchBattle() {
this.arenaBg = this.add.sprite(0, 0, "plains_bg"); this.arenaBg = this.add.sprite(0, 0, "plains_bg");
this.arenaBg.setName("sprite-arena-bg"); this.arenaBg.setName("sprite-arena-bg");
@ -594,6 +604,8 @@ export class BattleScene extends SceneBase {
this.arenaNextEnemy.setVisible(false); this.arenaNextEnemy.setVisible(false);
for (const a of [this.arenaPlayer, this.arenaPlayerTransition, this.arenaEnemy, this.arenaNextEnemy]) { for (const a of [this.arenaPlayer, this.arenaPlayerTransition, this.arenaEnemy, this.arenaNextEnemy]) {
// TODO: This seems questionable - we just initialized the arena sprites and then have to manually check if they're a sprite?
// This is likely the result of either extreme laziness or confusion
if (a instanceof Phaser.GameObjects.Sprite) { if (a instanceof Phaser.GameObjects.Sprite) {
a.setOrigin(0, 0); a.setOrigin(0, 0);
} }
@ -1122,6 +1134,7 @@ export class BattleScene extends SceneBase {
return this.currentBattle?.randSeedInt(range, min); return this.currentBattle?.randSeedInt(range, min);
} }
// TODO: Break up function - this does far too much in 1 sitting
reset(clearScene = false, clearData = false, reloadI18n = false): void { reset(clearScene = false, clearData = false, reloadI18n = false): void {
if (clearData) { if (clearData) {
this.gameData = new GameData(); this.gameData = new GameData();
@ -1241,6 +1254,7 @@ export class BattleScene extends SceneBase {
this.uiContainer.remove(this.ui, true); this.uiContainer.remove(this.ui, true);
this.uiContainer.destroy(); this.uiContainer.destroy();
this.children.removeAll(true); this.children.removeAll(true);
// TODO: Do we even need this?
this.game.domContainer.innerHTML = ""; this.game.domContainer.innerHTML = "";
// TODO: `launchBattle` calls `reset(false, false, true)` // TODO: `launchBattle` calls `reset(false, false, true)`
this.launchBattle(); this.launchBattle();

View File

@ -16,6 +16,7 @@ export const NEW_TURN_COLOR = "#ffad00ff" as const;
export const UI_MSG_COLOR = "#009dffff" as const; export const UI_MSG_COLOR = "#009dffff" as const;
export const OVERRIDES_COLOR = "#b0b01eff" as const; export const OVERRIDES_COLOR = "#b0b01eff" as const;
export const SETTINGS_COLOR = "#008844ff" as const; export const SETTINGS_COLOR = "#008844ff" as const;
export const PHASE_INTERCEPTOR_COLOR = "#ff7f50" as const;
// Colors used for Vitest-related test utils // Colors used for Vitest-related test utils
export const TEST_NAME_COLOR = "#008886ff" as const; export const TEST_NAME_COLOR = "#008886ff" as const;

View File

@ -1,4 +1,4 @@
/** Defines the speed of gaining experience. */ /** Enum regulating the speed of EXP bar animations. */
export enum ExpGainsSpeed { export enum ExpGainsSpeed {
/** The normal speed. */ /** The normal speed. */
DEFAULT, DEFAULT,

View File

@ -346,6 +346,10 @@ export class PhaseManager {
} }
/** /**
* Helper method to start and log the current phase. * Helper method to start and log the current phase.
*
* @privateRemarks
* This is disabled during tests by `phase-interceptor.ts` to allow for pausing execution at specific phases.
* As such, **do not remove or split this method** as it will break integration tests.
*/ */
private startCurrentPhase(): void { 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};`);

View File

@ -169,13 +169,13 @@ export class TitlePhase extends Phase {
globalScene.ui.setMode(UiMode.TITLE, config); globalScene.ui.setMode(UiMode.TITLE, config);
} }
loadSaveSlot(slotId: number): void { // TODO: Make callers actually wait for the save slot to load
private async loadSaveSlot(slotId: number): Promise<void> {
globalScene.sessionSlotId = slotId > -1 || !loggedInUser ? slotId : loggedInUser.lastSessionSlot; globalScene.sessionSlotId = slotId > -1 || !loggedInUser ? slotId : loggedInUser.lastSessionSlot;
globalScene.ui.setMode(UiMode.MESSAGE); globalScene.ui.setMode(UiMode.MESSAGE);
globalScene.ui.resetModeChain(); globalScene.ui.resetModeChain();
globalScene.gameData try {
.loadSession(slotId, slotId === -1 ? this.lastSessionData : undefined) const success = await globalScene.gameData.loadSession(slotId, slotId === -1 ? this.lastSessionData : undefined);
.then((success: boolean) => {
if (success) { if (success) {
this.loaded = true; this.loaded = true;
if (loggedInUser) { if (loggedInUser) {
@ -185,11 +185,10 @@ export class TitlePhase extends Phase {
} else { } else {
this.end(); this.end();
} }
}) } catch (err) {
.catch(err => {
console.error(err); console.error(err);
globalScene.ui.showText(i18next.t("menu:failedToLoadSession"), null); globalScene.ui.showText(i18next.t("menu:failedToLoadSession"), null);
}); }
} }
initDailyRun(): void { initDailyRun(): void {

View File

@ -520,6 +520,7 @@ export class UI extends Phaser.GameObjects.Container {
} }
private setModeInternal( private setModeInternal(
this: UI,
mode: UiMode, mode: UiMode,
clear: boolean, clear: boolean,
forceTransition: boolean, forceTransition: boolean,

View File

@ -1,9 +1,5 @@
import { AbilityId } from "#enums/ability-id"; import { AbilityId } from "#enums/ability-id";
import { BattleStyle } from "#enums/battle-style";
import { SpeciesId } from "#enums/species-id"; import { SpeciesId } from "#enums/species-id";
import { UiMode } from "#enums/ui-mode";
import { CommandPhase } from "#phases/command-phase";
import { TurnInitPhase } from "#phases/turn-init-phase";
import i18next from "#plugins/i18n"; import i18next from "#plugins/i18n";
import { GameManager } from "#test/test-utils/game-manager"; import { GameManager } from "#test/test-utils/game-manager";
import Phaser from "phaser"; import Phaser from "phaser";
@ -35,20 +31,9 @@ describe("Ability Timing", () => {
}); });
it("should trigger after switch check", async () => { it("should trigger after switch check", async () => {
game.settings.battleStyle = BattleStyle.SWITCH;
await game.classicMode.runToSummon([SpeciesId.EEVEE, SpeciesId.FEEBAS]); await game.classicMode.runToSummon([SpeciesId.EEVEE, SpeciesId.FEEBAS]);
await game.classicMode.startBattleWithSwitch(1);
game.onNextPrompt(
"CheckSwitchPhase",
UiMode.CONFIRM,
() => {
game.setMode(UiMode.MESSAGE);
game.endPhase();
},
() => game.isCurrentPhase(CommandPhase) || game.isCurrentPhase(TurnInitPhase),
);
await game.phaseInterceptor.to("MessagePhase");
expect(i18next.t).toHaveBeenCalledWith("battle:statFell", expect.objectContaining({ count: 1 })); expect(i18next.t).toHaveBeenCalledWith("battle:statFell", expect.objectContaining({ count: 1 }));
}); });
}); });

View File

@ -102,7 +102,7 @@ describe("Abilities - Good As Gold", () => {
game.move.select(MoveId.HELPING_HAND, 0); game.move.select(MoveId.HELPING_HAND, 0);
game.move.select(MoveId.TACKLE, 1); game.move.select(MoveId.TACKLE, 1);
await game.phaseInterceptor.to("MoveEndPhase", true); await game.phaseInterceptor.to("MoveEndPhase");
expect(game.scene.getPlayerField()[1].getTag(BattlerTagType.HELPING_HAND)).toBeUndefined(); expect(game.scene.getPlayerField()[1].getTag(BattlerTagType.HELPING_HAND)).toBeUndefined();
}); });

View File

@ -58,9 +58,7 @@ describe("Abilities - Mimicry", () => {
expect(playerPokemon.getTypes()).toEqual([PokemonType.PSYCHIC]); expect(playerPokemon.getTypes()).toEqual([PokemonType.PSYCHIC]);
if (game.scene.arena.terrain) { game.scene.arena.terrain!.turnsLeft = 1;
game.scene.arena.terrain.turnsLeft = 1;
}
game.move.use(MoveId.SPLASH); game.move.use(MoveId.SPLASH);
await game.move.forceEnemyMove(MoveId.SPLASH); await game.move.forceEnemyMove(MoveId.SPLASH);

View File

@ -1,9 +1,8 @@
import { AbilityId } from "#enums/ability-id"; import { AbilityId } from "#enums/ability-id";
import { ArenaTagSide } from "#enums/arena-tag-side";
import { ArenaTagType } from "#enums/arena-tag-type"; import { ArenaTagType } from "#enums/arena-tag-type";
import { MoveId } from "#enums/move-id";
import { SpeciesId } from "#enums/species-id"; import { SpeciesId } from "#enums/species-id";
import { PostSummonPhase } from "#phases/post-summon-phase"; import { WeatherType } from "#enums/weather-type";
import { TurnEndPhase } from "#phases/turn-end-phase";
import { GameManager } from "#test/test-utils/game-manager"; import { GameManager } from "#test/test-utils/game-manager";
import Phaser from "phaser"; import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
@ -24,57 +23,45 @@ describe("Abilities - Screen Cleaner", () => {
beforeEach(() => { beforeEach(() => {
game = new GameManager(phaserGame); game = new GameManager(phaserGame);
game.override.battleStyle("single").ability(AbilityId.SCREEN_CLEANER).enemySpecies(SpeciesId.SHUCKLE); game.override
.battleStyle("single")
.ability(AbilityId.SCREEN_CLEANER)
.enemySpecies(SpeciesId.SHUCKLE)
.weather(WeatherType.SNOW);
}); });
it("removes Aurora Veil", async () => { // TODO: Screen cleaner doesn't remove both sides' tags if both players have them (as do a LOT of other things)
game.override.enemyMoveset(MoveId.AURORA_VEIL); it.todo.each([
{ name: "Reflect", tagType: ArenaTagType.REFLECT },
{ name: "Light Screen", tagType: ArenaTagType.LIGHT_SCREEN },
{ name: "Aurora Veil", tagType: ArenaTagType.AURORA_VEIL },
])("should remove all instances of $name on entrance", async ({ tagType }) => {
game.scene.arena.addTag(tagType, 0, 0, 0, ArenaTagSide.PLAYER);
game.scene.arena.addTag(tagType, 0, 0, 0, ArenaTagSide.ENEMY);
game.scene.arena.addTag(tagType, 0, 0, 0, ArenaTagSide.BOTH);
expect(game).toHaveArenaTag(tagType);
await game.classicMode.startBattle([SpeciesId.MAGIKARP, SpeciesId.MAGIKARP]); await game.classicMode.startBattle([SpeciesId.SLOWKING]);
game.move.use(MoveId.HAIL); const slowking = game.field.getPlayerPokemon();
await game.phaseInterceptor.to(TurnEndPhase); expect(slowking).toHaveAbilityApplied(AbilityId.SCREEN_CLEANER);
expect(game).not.toHaveArenaTag(tagType);
expect(game.scene.arena.getTag(ArenaTagType.AURORA_VEIL)).toBeDefined();
await game.toNextTurn();
game.doSwitchPokemon(1);
await game.phaseInterceptor.to(PostSummonPhase);
expect(game.scene.arena.getTag(ArenaTagType.AURORA_VEIL)).toBeUndefined();
}); });
it("removes Light Screen", async () => { it("should remove all tag types at once", async () => {
game.override.enemyMoveset(MoveId.LIGHT_SCREEN); game.scene.arena.addTag(ArenaTagType.REFLECT, 0, 0, 0);
game.scene.arena.addTag(ArenaTagType.LIGHT_SCREEN, 0, 0, 0);
game.scene.arena.addTag(ArenaTagType.AURORA_VEIL, 0, 0, 0);
expect(game).toHaveArenaTag(ArenaTagType.REFLECT);
expect(game).toHaveArenaTag(ArenaTagType.LIGHT_SCREEN);
expect(game).toHaveArenaTag(ArenaTagType.AURORA_VEIL);
await game.classicMode.startBattle([SpeciesId.MAGIKARP, SpeciesId.MAGIKARP]); await game.classicMode.startBattle([SpeciesId.SLOWKING]);
game.move.use(MoveId.SPLASH); const slowking = game.field.getPlayerPokemon();
await game.phaseInterceptor.to(TurnEndPhase); expect(slowking).toHaveAbilityApplied(AbilityId.SCREEN_CLEANER);
expect(game).not.toHaveArenaTag(ArenaTagType.REFLECT);
expect(game.scene.arena.getTag(ArenaTagType.LIGHT_SCREEN)).toBeDefined(); expect(game).not.toHaveArenaTag(ArenaTagType.LIGHT_SCREEN);
expect(game).not.toHaveArenaTag(ArenaTagType.AURORA_VEIL);
await game.toNextTurn();
game.doSwitchPokemon(1);
await game.phaseInterceptor.to(PostSummonPhase);
expect(game.scene.arena.getTag(ArenaTagType.LIGHT_SCREEN)).toBeUndefined();
});
it("removes Reflect", async () => {
game.override.enemyMoveset(MoveId.REFLECT);
await game.classicMode.startBattle([SpeciesId.MAGIKARP, SpeciesId.MAGIKARP]);
game.move.use(MoveId.SPLASH);
await game.phaseInterceptor.to(TurnEndPhase);
expect(game.scene.arena.getTag(ArenaTagType.REFLECT)).toBeDefined();
await game.toNextTurn();
game.doSwitchPokemon(1);
await game.phaseInterceptor.to(PostSummonPhase);
expect(game.scene.arena.getTag(ArenaTagType.REFLECT)).toBeUndefined();
}); });
}); });

View File

@ -3,7 +3,6 @@ import { MoveId } from "#enums/move-id";
import { SpeciesId } from "#enums/species-id"; import { SpeciesId } from "#enums/species-id";
import { Stat } from "#enums/stat"; import { Stat } from "#enums/stat";
import type { EnemyPokemon, PlayerPokemon } from "#field/pokemon"; import type { EnemyPokemon, PlayerPokemon } from "#field/pokemon";
import { DamageAnimPhase } from "#phases/damage-anim-phase";
import { TurnEndPhase } from "#phases/turn-end-phase"; import { TurnEndPhase } from "#phases/turn-end-phase";
import { GameManager } from "#test/test-utils/game-manager"; import { GameManager } from "#test/test-utils/game-manager";
import Phaser from "phaser"; import Phaser from "phaser";
@ -50,7 +49,7 @@ describe("Moves - Fissure", () => {
game.override.ability(AbilityId.NO_GUARD).enemyAbility(AbilityId.FUR_COAT); game.override.ability(AbilityId.NO_GUARD).enemyAbility(AbilityId.FUR_COAT);
game.move.select(MoveId.FISSURE); game.move.select(MoveId.FISSURE);
await game.phaseInterceptor.to(DamageAnimPhase, true); await game.phaseInterceptor.to("DamageAnimPhase");
expect(enemyPokemon.isFainted()).toBe(true); expect(enemyPokemon.isFainted()).toBe(true);
}); });

View File

@ -122,9 +122,8 @@ describe("Moves - Focus Punch", () => {
await game.classicMode.startBattle([SpeciesId.CHARIZARD]); await game.classicMode.startBattle([SpeciesId.CHARIZARD]);
game.move.select(MoveId.FOCUS_PUNCH); game.move.select(MoveId.FOCUS_PUNCH);
await game.phaseInterceptor.to("MoveEndPhase", true); await game.phaseInterceptor.to("MoveEndPhase");
await game.phaseInterceptor.to("MessagePhase", false); await game.phaseInterceptor.to("MoveEndPhase");
await game.phaseInterceptor.to("MoveEndPhase", true);
expect(game.textInterceptor.logs).toContain(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")); expect(game.textInterceptor.logs).not.toContain(i18next.t("battle:attackFailed"));
}); });

View File

@ -73,31 +73,9 @@ describe("Moves - Parting Shot", () => {
SpeciesId.ABRA, SpeciesId.ABRA,
]); ]);
// use Memento 3 times to debuff enemy
game.move.select(MoveId.MEMENTO);
await game.phaseInterceptor.to("FaintPhase");
expect(game.field.getPlayerPokemon().isFainted()).toBe(true);
game.doSelectPartyPokemon(1);
await game.phaseInterceptor.to("TurnInitPhase", false);
game.move.select(MoveId.MEMENTO);
await game.phaseInterceptor.to("FaintPhase");
expect(game.field.getPlayerPokemon().isFainted()).toBe(true);
game.doSelectPartyPokemon(2);
await game.phaseInterceptor.to("TurnInitPhase", false);
game.move.select(MoveId.MEMENTO);
await game.phaseInterceptor.to("FaintPhase");
expect(game.field.getPlayerPokemon().isFainted()).toBe(true);
game.doSelectPartyPokemon(3);
// set up done
await game.phaseInterceptor.to("TurnInitPhase", false);
const enemyPokemon = game.field.getEnemyPokemon(); const enemyPokemon = game.field.getEnemyPokemon();
expect(enemyPokemon).toBeDefined(); enemyPokemon.setStatStage(Stat.ATK, -6);
enemyPokemon.setStatStage(Stat.SPATK, -6);
expect(enemyPokemon.getStatStage(Stat.ATK)).toBe(-6);
expect(enemyPokemon.getStatStage(Stat.SPATK)).toBe(-6);
// now parting shot should fail // now parting shot should fail
game.move.select(MoveId.PARTING_SHOT); game.move.select(MoveId.PARTING_SHOT);
@ -136,7 +114,6 @@ describe("Moves - Parting Shot", () => {
await game.classicMode.startBattle([SpeciesId.SNORLAX, SpeciesId.MEOWTH]); await game.classicMode.startBattle([SpeciesId.SNORLAX, SpeciesId.MEOWTH]);
const enemyPokemon = game.field.getEnemyPokemon(); const enemyPokemon = game.field.getEnemyPokemon();
expect(enemyPokemon).toBeDefined();
game.move.select(MoveId.PARTING_SHOT); game.move.select(MoveId.PARTING_SHOT);

View File

@ -1,16 +1,11 @@
import { Status } from "#data/status-effect"; import { Status } from "#data/status-effect";
import { BattleStyle } from "#enums/battle-style";
import { Button } from "#enums/buttons"; import { Button } from "#enums/buttons";
import { StatusEffect } from "#enums/status-effect"; import { StatusEffect } from "#enums/status-effect";
import { UiMode } from "#enums/ui-mode"; import { UiMode } from "#enums/ui-mode";
// biome-ignore lint/performance/noNamespaceImport: Necessary for mocks // biome-ignore lint/performance/noNamespaceImport: Necessary for mocks
import * as EncounterPhaseUtils from "#mystery-encounters/encounter-phase-utils"; import * as EncounterPhaseUtils from "#mystery-encounters/encounter-phase-utils";
import { CommandPhase } from "#phases/command-phase"; import { MysteryEncounterBattlePhase, MysteryEncounterRewardsPhase } from "#phases/mystery-encounter-phases";
import { MessagePhase } from "#phases/message-phase";
import {
MysteryEncounterBattlePhase,
MysteryEncounterOptionSelectedPhase,
MysteryEncounterRewardsPhase,
} from "#phases/mystery-encounter-phases";
import { VictoryPhase } from "#phases/victory-phase"; import { VictoryPhase } from "#phases/victory-phase";
import type { GameManager } from "#test/test-utils/game-manager"; import type { GameManager } from "#test/test-utils/game-manager";
import type { MessageUiHandler } from "#ui/message-ui-handler"; import type { MessageUiHandler } from "#ui/message-ui-handler";
@ -46,50 +41,14 @@ export async function runMysteryEncounterToEnd(
() => game.isCurrentPhase(MysteryEncounterBattlePhase) || game.isCurrentPhase(MysteryEncounterRewardsPhase), () => game.isCurrentPhase(MysteryEncounterBattlePhase) || game.isCurrentPhase(MysteryEncounterRewardsPhase),
); );
if (isBattle) { if (!isBattle) {
game.onNextPrompt( return await game.phaseInterceptor.to("MysteryEncounterRewardsPhase");
"CheckSwitchPhase",
UiMode.CONFIRM,
() => {
game.setMode(UiMode.MESSAGE);
game.endPhase();
},
() => game.isCurrentPhase(CommandPhase),
);
game.onNextPrompt(
"CheckSwitchPhase",
UiMode.MESSAGE,
() => {
game.setMode(UiMode.MESSAGE);
game.endPhase();
},
() => game.isCurrentPhase(CommandPhase),
);
// If a battle is started, fast forward to end of the battle
game.onNextPrompt("CommandPhase", UiMode.COMMAND, () => {
game.scene.phaseManager.clearPhaseQueue();
game.scene.phaseManager.unshiftPhase(new VictoryPhase(0));
game.endPhase();
});
// Handle end of battle trainer messages
game.onNextPrompt("TrainerVictoryPhase", UiMode.MESSAGE, () => {
const uiHandler = game.scene.ui.getHandler<MessageUiHandler>();
uiHandler.processInput(Button.ACTION);
});
// Handle egg hatch dialogue
game.onNextPrompt("EggLapsePhase", UiMode.MESSAGE, () => {
const uiHandler = game.scene.ui.getHandler<MessageUiHandler>();
uiHandler.processInput(Button.ACTION);
});
await game.toNextTurn();
} else {
await game.phaseInterceptor.to("MysteryEncounterRewardsPhase");
} }
if (game.scene.battleStyle === BattleStyle.SWITCH) {
console.warn("BattleStyle.SWITCH was used during ME battle, swapping to set mode...");
game.settings.battleStyle(BattleStyle.SET);
}
await game.toNextTurn();
} }
export async function runSelectMysteryEncounterOption( export async function runSelectMysteryEncounterOption(
@ -105,10 +64,10 @@ export async function runSelectMysteryEncounterOption(
const uiHandler = game.scene.ui.getHandler<MessageUiHandler>(); const uiHandler = game.scene.ui.getHandler<MessageUiHandler>();
uiHandler.processInput(Button.ACTION); uiHandler.processInput(Button.ACTION);
}, },
() => game.isCurrentPhase(MysteryEncounterOptionSelectedPhase), () => game.isCurrentPhase("MysteryEncounterOptionSelectedPhase", "CommandPhase", "TurnInitPhase"),
); );
if (game.isCurrentPhase(MessagePhase)) { if (game.isCurrentPhase("MessagePhase")) {
await game.phaseInterceptor.to("MessagePhase"); await game.phaseInterceptor.to("MessagePhase");
} }
@ -120,10 +79,10 @@ export async function runSelectMysteryEncounterOption(
const uiHandler = game.scene.ui.getHandler<MysteryEncounterUiHandler>(); const uiHandler = game.scene.ui.getHandler<MysteryEncounterUiHandler>();
uiHandler.processInput(Button.ACTION); uiHandler.processInput(Button.ACTION);
}, },
() => game.isCurrentPhase(MysteryEncounterOptionSelectedPhase), () => game.isCurrentPhase("MysteryEncounterOptionSelectedPhase", "CommandPhase", "TurnInitPhase"),
); );
await game.phaseInterceptor.to("MysteryEncounterPhase", true); await game.phaseInterceptor.to("MysteryEncounterPhase");
// select the desired option // select the desired option
const uiHandler = game.scene.ui.getHandler<MysteryEncounterUiHandler>(); const uiHandler = game.scene.ui.getHandler<MysteryEncounterUiHandler>();
@ -193,7 +152,10 @@ async function handleSecondaryOptionSelect(game: GameManager, pokemonNo: number,
* @param game * @param game
* @param runRewardsPhase * @param runRewardsPhase
*/ */
export async function skipBattleRunMysteryEncounterRewardsPhase(game: GameManager, runRewardsPhase = true) { export async function skipBattleRunMysteryEncounterRewardsPhase(
game: GameManager,
runRewardsPhase?: false | undefined,
) {
game.scene.phaseManager.clearPhaseQueue(); game.scene.phaseManager.clearPhaseQueue();
game.scene.getEnemyParty().forEach(p => { game.scene.getEnemyParty().forEach(p => {
p.hp = 0; p.hp = 0;

View File

@ -364,7 +364,6 @@ describe("Bug-Type Superfan - Mystery Encounter", () => {
await skipBattleRunMysteryEncounterRewardsPhase(game, false); await skipBattleRunMysteryEncounterRewardsPhase(game, false);
expect(game).toBeAtPhase("MysteryEncounterRewardsPhase"); expect(game).toBeAtPhase("MysteryEncounterRewardsPhase");
game.phaseInterceptor["prompts"] = []; // Clear out prompt handlers
game.onNextPrompt("MysteryEncounterRewardsPhase", UiMode.OPTION_SELECT, () => { game.onNextPrompt("MysteryEncounterRewardsPhase", UiMode.OPTION_SELECT, () => {
game.endPhase(); game.endPhase();
}); });
@ -372,6 +371,7 @@ describe("Bug-Type Superfan - Mystery Encounter", () => {
expect(selectOptionSpy).toHaveBeenCalledTimes(1); expect(selectOptionSpy).toHaveBeenCalledTimes(1);
const optionData = selectOptionSpy.mock.calls[0][0]; const optionData = selectOptionSpy.mock.calls[0][0];
// TODO: This is a bad way to check moves tests
expect(PHYSICAL_TUTOR_MOVES.some(move => new PokemonMove(move).getName() === optionData[0].label)).toBe(true); expect(PHYSICAL_TUTOR_MOVES.some(move => new PokemonMove(move).getName() === optionData[0].label)).toBe(true);
expect(SPECIAL_TUTOR_MOVES.some(move => new PokemonMove(move).getName() === optionData[1].label)).toBe(true); expect(SPECIAL_TUTOR_MOVES.some(move => new PokemonMove(move).getName() === optionData[1].label)).toBe(true);
expect(STATUS_TUTOR_MOVES.some(move => new PokemonMove(move).getName() === optionData[2].label)).toBe(true); expect(STATUS_TUTOR_MOVES.some(move => new PokemonMove(move).getName() === optionData[2].label)).toBe(true);

View File

@ -8,7 +8,6 @@ import * as EncounterPhaseUtils from "#mystery-encounters/encounter-phase-utils"
import { LostAtSeaEncounter } from "#mystery-encounters/lost-at-sea-encounter"; import { LostAtSeaEncounter } from "#mystery-encounters/lost-at-sea-encounter";
import * as MysteryEncounters from "#mystery-encounters/mystery-encounters"; import * as MysteryEncounters from "#mystery-encounters/mystery-encounters";
import { MysteryEncounterPhase } from "#phases/mystery-encounter-phases"; import { MysteryEncounterPhase } from "#phases/mystery-encounter-phases";
import { PartyExpPhase } from "#phases/party-exp-phase";
import { import {
runMysteryEncounterToEnd, runMysteryEncounterToEnd,
runSelectMysteryEncounterOption, runSelectMysteryEncounterOption,
@ -119,7 +118,7 @@ describe("Lost at Sea - Mystery Encounter", () => {
const expBefore = blastoise!.exp; const expBefore = blastoise!.exp;
await runMysteryEncounterToEnd(game, 1); await runMysteryEncounterToEnd(game, 1);
await game.phaseInterceptor.to(PartyExpPhase); await game.phaseInterceptor.to("ShowPartyExpBarPhase");
expect(blastoise?.exp).toBe(expBefore + Math.floor((laprasSpecies.baseExp * defaultWave) / 5 + 1)); expect(blastoise?.exp).toBe(expBefore + Math.floor((laprasSpecies.baseExp * defaultWave) / 5 + 1));
}); });
@ -184,7 +183,7 @@ describe("Lost at Sea - Mystery Encounter", () => {
const expBefore = pidgeot!.exp; const expBefore = pidgeot!.exp;
await runMysteryEncounterToEnd(game, 2); await runMysteryEncounterToEnd(game, 2);
await game.phaseInterceptor.to(PartyExpPhase); await game.phaseInterceptor.to("ShowPartyExpBarPhase");
expect(pidgeot!.exp).toBe(expBefore + Math.floor((laprasBaseExp * defaultWave) / 5 + 1)); expect(pidgeot!.exp).toBe(expBefore + Math.floor((laprasBaseExp * defaultWave) / 5 + 1));
}); });

View File

@ -3,7 +3,6 @@ import { MysteryEncounterTier } from "#enums/mystery-encounter-tier";
import { MysteryEncounterType } from "#enums/mystery-encounter-type"; import { MysteryEncounterType } from "#enums/mystery-encounter-type";
import { SpeciesId } from "#enums/species-id"; import { SpeciesId } from "#enums/species-id";
import { UiMode } from "#enums/ui-mode"; import { UiMode } from "#enums/ui-mode";
import { MysteryEncounterOptionSelectedPhase } from "#phases/mystery-encounter-phases";
import { GameManager } from "#test/test-utils/game-manager"; import { GameManager } from "#test/test-utils/game-manager";
import type { MessageUiHandler } from "#ui/message-ui-handler"; import type { MessageUiHandler } from "#ui/message-ui-handler";
import type { MysteryEncounterUiHandler } from "#ui/mystery-encounter-ui-handler"; import type { MysteryEncounterUiHandler } from "#ui/mystery-encounter-ui-handler";
@ -83,11 +82,7 @@ describe("Mystery Encounter Phases", () => {
handler.processInput(Button.ACTION); handler.processInput(Button.ACTION);
// Waitfor required so that option select messages and preOptionPhase logic are handled // Waitfor required so that option select messages and preOptionPhase logic are handled
await vi.waitFor(() => await vi.waitFor(() => expect(game).toBeAtPhase("MysteryEncounterOptionSelectedPhase"));
expect(game.scene.phaseManager.getCurrentPhase()?.constructor.name).toBe(
MysteryEncounterOptionSelectedPhase.name,
),
);
expect(ui.getMode()).toBe(UiMode.MESSAGE); expect(ui.getMode()).toBe(UiMode.MESSAGE);
expect(ui.showDialogue).toHaveBeenCalledTimes(1); expect(ui.showDialogue).toHaveBeenCalledTimes(1);
expect(ui.showText).toHaveBeenCalledTimes(2); expect(ui.showText).toHaveBeenCalledTimes(2);

View File

@ -1,4 +1,5 @@
import "vitest-canvas-mock"; import "vitest-canvas-mock";
import { PromptHandler } from "#test/test-utils/helpers/prompt-handler";
import { MockConsole } from "#test/test-utils/mocks/mock-console/mock-console"; import { MockConsole } from "#test/test-utils/mocks/mock-console/mock-console";
import { logTestEnd, logTestStart } from "#test/test-utils/setup/test-end-log"; import { logTestEnd, logTestStart } from "#test/test-utils/setup/test-end-log";
import { initTests } from "#test/test-utils/test-file-initialization"; import { initTests } from "#test/test-utils/test-file-initialization";
@ -56,21 +57,28 @@ vi.mock(import("i18next"), async importOriginal => {
return await importOriginal(); return await importOriginal();
}); });
global.testFailed = false; //#endregion Mocking
//#region Hooks
beforeAll(() => { beforeAll(() => {
initTests(); initTests();
}); });
beforeEach(context => {
logTestStart(context.task);
});
afterEach(context => {
logTestEnd(context.task);
});
afterAll(() => { afterAll(() => {
global.server.close(); global.server.close();
MockConsole.printPostTestWarnings(); MockConsole.printPostTestWarnings();
console.log(chalk.hex("#dfb8d8")("Closing i18n MSW server!")); console.log(chalk.hex("#dfb8d8")("Closing i18n MSW server!"));
}); });
beforeEach(context => {
logTestStart(context.task);
});
afterEach(context => {
logTestEnd(context.task);
clearInterval(PromptHandler.runInterval);
PromptHandler.runInterval = undefined;
});
//#endregion Hooks

View File

@ -1,49 +0,0 @@
export class ErrorInterceptor {
private static instance: ErrorInterceptor;
public running;
constructor() {
this.running = [];
}
public static getInstance(): ErrorInterceptor {
if (!ErrorInterceptor.instance) {
ErrorInterceptor.instance = new ErrorInterceptor();
}
return ErrorInterceptor.instance;
}
clear() {
this.running = [];
}
add(obj) {
this.running.push(obj);
}
remove(obj) {
const index = this.running.indexOf(obj);
if (index !== -1) {
this.running.splice(index, 1);
}
}
}
process.on("uncaughtException", error => {
console.log(error);
const toStop = ErrorInterceptor.getInstance().running;
for (const elm of toStop) {
elm.rejectAll(error);
}
global.testFailed = true;
});
// Global error handler for unhandled promise rejections
process.on("unhandledRejection", (reason, _promise) => {
console.log(reason);
const toStop = ErrorInterceptor.getInstance().running;
for (const elm of toStop) {
elm.rejectAll(reason);
}
global.testFailed = true;
});

View File

@ -6,11 +6,8 @@ import overrides from "#app/overrides";
import { modifierTypes } from "#data/data-lists"; import { modifierTypes } from "#data/data-lists";
import { BattlerIndex } from "#enums/battler-index"; import { BattlerIndex } from "#enums/battler-index";
import { Button } from "#enums/buttons"; import { Button } from "#enums/buttons";
import { ExpGainsSpeed } from "#enums/exp-gains-speed";
import { ExpNotification } from "#enums/exp-notification";
import { GameModes } from "#enums/game-modes"; import { GameModes } from "#enums/game-modes";
import type { MysteryEncounterType } from "#enums/mystery-encounter-type"; import type { MysteryEncounterType } from "#enums/mystery-encounter-type";
import { PlayerGender } from "#enums/player-gender";
import type { PokeballType } from "#enums/pokeball"; import type { PokeballType } from "#enums/pokeball";
import type { SpeciesId } from "#enums/species-id"; import type { SpeciesId } from "#enums/species-id";
import { UiMode } from "#enums/ui-mode"; import { UiMode } from "#enums/ui-mode";
@ -26,9 +23,7 @@ import { NewBattlePhase } from "#phases/new-battle-phase";
import { SelectStarterPhase } from "#phases/select-starter-phase"; import { SelectStarterPhase } from "#phases/select-starter-phase";
import type { SelectTargetPhase } from "#phases/select-target-phase"; import type { SelectTargetPhase } from "#phases/select-target-phase";
import { TurnEndPhase } from "#phases/turn-end-phase"; import { TurnEndPhase } from "#phases/turn-end-phase";
import { TurnInitPhase } from "#phases/turn-init-phase";
import { TurnStartPhase } from "#phases/turn-start-phase"; import { TurnStartPhase } from "#phases/turn-start-phase";
import { ErrorInterceptor } from "#test/test-utils/error-interceptor";
import { generateStarters } from "#test/test-utils/game-manager-utils"; import { generateStarters } from "#test/test-utils/game-manager-utils";
import { GameWrapper } from "#test/test-utils/game-wrapper"; import { GameWrapper } from "#test/test-utils/game-wrapper";
import { ChallengeModeHelper } from "#test/test-utils/helpers/challenge-mode-helper"; import { ChallengeModeHelper } from "#test/test-utils/helpers/challenge-mode-helper";
@ -38,6 +33,7 @@ import { FieldHelper } from "#test/test-utils/helpers/field-helper";
import { ModifierHelper } from "#test/test-utils/helpers/modifiers-helper"; import { ModifierHelper } from "#test/test-utils/helpers/modifiers-helper";
import { MoveHelper } from "#test/test-utils/helpers/move-helper"; import { MoveHelper } from "#test/test-utils/helpers/move-helper";
import { OverridesHelper } from "#test/test-utils/helpers/overrides-helper"; import { OverridesHelper } from "#test/test-utils/helpers/overrides-helper";
import { PromptHandler } from "#test/test-utils/helpers/prompt-handler";
import { ReloadHelper } from "#test/test-utils/helpers/reload-helper"; import { ReloadHelper } from "#test/test-utils/helpers/reload-helper";
import { SettingsHelper } from "#test/test-utils/helpers/settings-helper"; import { SettingsHelper } from "#test/test-utils/helpers/settings-helper";
import type { InputsHandler } from "#test/test-utils/inputs-handler"; import type { InputsHandler } from "#test/test-utils/inputs-handler";
@ -65,6 +61,7 @@ export class GameManager {
public phaseInterceptor: PhaseInterceptor; public phaseInterceptor: PhaseInterceptor;
public textInterceptor: TextInterceptor; public textInterceptor: TextInterceptor;
public inputsHandler: InputsHandler; public inputsHandler: InputsHandler;
public readonly promptHandler: PromptHandler;
public readonly override: OverridesHelper; public readonly override: OverridesHelper;
public readonly move: MoveHelper; public readonly move: MoveHelper;
public readonly classicMode: ClassicModeHelper; public readonly classicMode: ClassicModeHelper;
@ -82,7 +79,6 @@ export class GameManager {
*/ */
constructor(phaserGame: Phaser.Game, bypassLogin = true) { constructor(phaserGame: Phaser.Game, bypassLogin = true) {
localStorage.clear(); localStorage.clear();
ErrorInterceptor.getInstance().clear();
// Simulate max rolls on RNG functions // Simulate max rolls on RNG functions
// TODO: Create helpers for disabling/enabling battle RNG // TODO: Create helpers for disabling/enabling battle RNG
BattleScene.prototype.randBattleSeedInt = (range, min = 0) => min + range - 1; BattleScene.prototype.randBattleSeedInt = (range, min = 0) => min + range - 1;
@ -102,6 +98,7 @@ export class GameManager {
} }
this.textInterceptor = new TextInterceptor(this.scene); this.textInterceptor = new TextInterceptor(this.scene);
this.promptHandler = new PromptHandler(this);
this.override = new OverridesHelper(this); this.override = new OverridesHelper(this);
this.move = new MoveHelper(this); this.move = new MoveHelper(this);
this.classicMode = new ClassicModeHelper(this); this.classicMode = new ClassicModeHelper(this);
@ -118,8 +115,14 @@ export class GameManager {
global.fetch = vi.fn(MockFetch) as any; global.fetch = vi.fn(MockFetch) as any;
} }
/** Reset a prior `BattleScene` instance to the proper initial state. */ /**
* Reset a prior `BattleScene` instance to the proper initial state.
* @todo Review why our UI doesn't reset between runs and why we need to do it manually
*/
private resetScene(): void { private resetScene(): void {
// NB: We can't pass `clearScene=true` to `reset` as it will only launch the battle after a fadeout tween
// (along with initializing a bunch of sprites we don't really care about)
this.scene.reset(false, true); this.scene.reset(false, true);
(this.scene.ui.handlers[UiMode.STARTER_SELECT] as StarterSelectUiHandler).clearStarterPreferences(); (this.scene.ui.handlers[UiMode.STARTER_SELECT] as StarterSelectUiHandler).clearStarterPreferences();
@ -132,7 +135,7 @@ export class GameManager {
/** /**
* Initialize various default overrides for starting tests, typically to alleviate randomness. * Initialize various default overrides for starting tests, typically to alleviate randomness.
*/ */
// TODO: This should not be here // TODO: Move this to overrides-helper.ts
private initDefaultOverrides(): void { private initDefaultOverrides(): void {
// Disables Mystery Encounters on all tests (can be overridden at test level) // Disables Mystery Encounters on all tests (can be overridden at test level)
this.override.mysteryEncounterChance(0); this.override.mysteryEncounterChance(0);
@ -157,7 +160,8 @@ export class GameManager {
} }
/** /**
* End the currently running phase immediately. * End the current phase immediately.
* @see {@linkcode PhaseInterceptor.shiftPhase} Function to skip the next upcoming phase
*/ */
endPhase() { endPhase() {
this.scene.phaseManager.getCurrentPhase().end(); this.scene.phaseManager.getCurrentPhase().end();
@ -170,15 +174,18 @@ export class GameManager {
* @param mode - The mode to wait for. * @param mode - The mode to wait for.
* @param callback - The callback function to execute on next prompt. * @param callback - The callback function to execute on next prompt.
* @param expireFn - Optional function to determine if the prompt has expired. * @param expireFn - Optional function to determine if the prompt has expired.
* @param awaitingActionInput - If true, will prevent the prompt from activating until the current {@linkcode AwaitableUiHandler}
* is awaiting input; default `false`
* @deprecated Remove in favor of {@linkcode PromptHandler.addToNextPrompt}
*/ */
onNextPrompt( onNextPrompt(
phaseTarget: string, phaseTarget: PhaseString,
mode: UiMode, mode: UiMode,
callback: () => void, callback: () => void,
expireFn?: () => void, expireFn?: () => boolean,
awaitingActionInput = false, awaitingActionInput = false,
) { ) {
this.phaseInterceptor.addToNextPrompt(phaseTarget, mode, callback, expireFn, awaitingActionInput); this.promptHandler.addToNextPrompt(phaseTarget, mode, callback, expireFn, awaitingActionInput);
} }
/** /**
@ -188,20 +195,8 @@ export class GameManager {
async runToTitle(): Promise<void> { async runToTitle(): Promise<void> {
// Go to login phase and skip past it // Go to login phase and skip past it
await this.phaseInterceptor.to("LoginPhase", false); await this.phaseInterceptor.to("LoginPhase", false);
this.phaseInterceptor.shiftPhase(true); this.phaseInterceptor.shiftPhase();
await this.phaseInterceptor.to("TitlePhase"); await this.phaseInterceptor.to("TitlePhase");
// TODO: This should be moved to a separate initialization method
this.scene.gameSpeed = 5;
this.scene.moveAnimations = false;
this.scene.showLevelUpStats = false;
this.scene.expGainsSpeed = ExpGainsSpeed.SKIP;
this.scene.expParty = ExpNotification.SKIP;
this.scene.hpBarSpeed = 3;
this.scene.enableTutorials = false;
this.scene.gameData.gender = PlayerGender.MALE; // set initial player gender
this.scene.battleStyle = this.settings.battleStyle;
this.scene.fieldVolume = 0;
} }
/** /**
@ -365,14 +360,14 @@ export class GameManager {
* Transition to the first {@linkcode CommandPhase} of the next turn. * Transition to the first {@linkcode CommandPhase} of the next turn.
* @returns A promise that resolves once the next {@linkcode CommandPhase} has been reached. * @returns A promise that resolves once the next {@linkcode CommandPhase} has been reached.
*/ */
async toNextTurn() { async toNextTurn(): Promise<void> {
await this.phaseInterceptor.to("TurnInitPhase"); await this.phaseInterceptor.to("TurnInitPhase");
await this.phaseInterceptor.to("CommandPhase"); await this.phaseInterceptor.to("CommandPhase");
console.log("==================[New Turn]=================="); console.log("==================[New Turn]==================");
} }
/** Transition to the {@linkcode TurnEndPhase | end of the current turn}. */ /** Transition to the {@linkcode TurnEndPhase | end of the current turn}. */
async toEndOfTurn() { async toEndOfTurn(): Promise<void> {
await this.phaseInterceptor.to("TurnEndPhase"); await this.phaseInterceptor.to("TurnEndPhase");
console.log("==================[End of Turn]=================="); console.log("==================[End of Turn]==================");
} }
@ -381,20 +376,9 @@ export class GameManager {
* Queue up button presses to skip taking an item on the next {@linkcode SelectModifierPhase}, * Queue up button presses to skip taking an item on the next {@linkcode SelectModifierPhase},
* and then transition to the next {@linkcode CommandPhase}. * and then transition to the next {@linkcode CommandPhase}.
*/ */
async toNextWave() { async toNextWave(): Promise<void> {
this.doSelectModifier(); this.doSelectModifier();
// forcibly end the message box for switching pokemon
this.onNextPrompt(
"CheckSwitchPhase",
UiMode.CONFIRM,
() => {
this.setMode(UiMode.MESSAGE);
this.endPhase();
},
() => this.isCurrentPhase(TurnInitPhase),
);
await this.phaseInterceptor.to("TurnInitPhase"); await this.phaseInterceptor.to("TurnInitPhase");
await this.phaseInterceptor.to("CommandPhase"); await this.phaseInterceptor.to("CommandPhase");
console.log("==================[New Wave]=================="); console.log("==================[New Wave]==================");
@ -404,28 +388,37 @@ export class GameManager {
* Check if the player has won the battle. * Check if the player has won the battle.
* @returns whether the player has won the battle (all opposing Pokemon have been fainted) * @returns whether the player has won the battle (all opposing Pokemon have been fainted)
*/ */
isVictory() { isVictory(): boolean {
return this.scene.currentBattle.enemyParty.every(pokemon => pokemon.isFainted()); return this.scene.currentBattle.enemyParty.every(pokemon => pokemon.isFainted());
} }
/** /**
* Checks if the current phase matches the target phase. * Checks if the current phase matches the target phase.
* @param phaseTarget - The target phase. * @param phaseTargets - The target phase(s) to check
* @returns Whether the current phase matches the target phase * @returns Whether the current phase matches any of the target phases
* @todo Remove `phaseClass` from signature * @todo Remove `phaseClass` from signature
* @todo Convert existing calls of `game.isCurrentPhase(A) || game.isCurrentPhase(B)` to pass them together in 1 call
*/ */
isCurrentPhase(phaseTarget: PhaseClass | PhaseString) { public isCurrentPhase(...phaseTargets: [PhaseString, ...PhaseString[]]): boolean;
const targetName = typeof phaseTarget === "string" ? phaseTarget : phaseTarget.name; /**
return this.scene.phaseManager.getCurrentPhase().phaseName === targetName; * Checks if the current phase matches the target phase.
* @param phaseTargets - The target phase to check
* @returns Whether the current phase matches the target phase
* @deprecated Use `PhaseString` instead
*/
public isCurrentPhase(phaseTargets: PhaseClass): boolean;
public isCurrentPhase(...phaseTargets: (PhaseString | PhaseClass)[]): boolean {
const phase = this.scene.phaseManager.getCurrentPhase();
return phaseTargets.some(p => phase.is(typeof p === "string" ? p : (p.name as PhaseString)));
} }
/** /**
* Checks if the current mode matches the target mode. * Check if the current `UiMode` matches the target mode.
* @param mode - The target {@linkcode UiMode} to check. * @param mode - The target {@linkcode UiMode} to check.
* @returns Whether the current mode matches the target mode. * @returns Whether the current mode matches the target mode.
*/ */
isCurrentMode(mode: UiMode) { isCurrentMode(mode: UiMode): boolean {
return this.scene.ui?.getMode() === mode; return this.scene.ui.getMode() === mode;
} }
/** /**
@ -443,10 +436,10 @@ export class GameManager {
/** /**
* Imports game data from a file. * Imports game data from a file.
* @param path - The path to the data file. * @param path - The path to the data file
* @returns A promise that resolves with a tuple containing a boolean indicating success and an integer status code. * @returns A promise that resolves with a tuple containing a boolean indicating success and an integer status code.
*/ */
async importData(path): Promise<[boolean, number]> { async importData(path: string): Promise<[boolean, number]> {
const saveKey = "x0i2O7WRiANTqPmZ"; const saveKey = "x0i2O7WRiANTqPmZ";
const dataRaw = fs.readFileSync(path, { encoding: "utf8", flag: "r" }); const dataRaw = fs.readFileSync(path, { encoding: "utf8", flag: "r" });
let dataStr = AES.decrypt(dataRaw, saveKey).toString(enc.Utf8); let dataStr = AES.decrypt(dataRaw, saveKey).toString(enc.Utf8);
@ -507,7 +500,7 @@ export class GameManager {
* @param inPhase - Which phase to expect the selection to occur in. Defaults to `SwitchPhase` * @param inPhase - Which phase to expect the selection to occur in. Defaults to `SwitchPhase`
* (which is where the majority of non-command switch operations occur). * (which is where the majority of non-command switch operations occur).
*/ */
doSelectPartyPokemon(slot: number, inPhase = "SwitchPhase") { doSelectPartyPokemon(slot: number, inPhase: PhaseString = "SwitchPhase") {
this.onNextPrompt(inPhase, UiMode.PARTY, () => { this.onNextPrompt(inPhase, UiMode.PARTY, () => {
const partyHandler = this.scene.ui.getHandler() as PartyUiHandler; const partyHandler = this.scene.ui.getHandler() as PartyUiHandler;

View File

@ -29,12 +29,14 @@ export class GameWrapper {
public scene: BattleScene; public scene: BattleScene;
constructor(phaserGame: Phaser.Game, bypassLogin: boolean) { constructor(phaserGame: Phaser.Game, bypassLogin: boolean) {
// TODO: Figure out how to actually set RNG states correctly
Phaser.Math.RND.sow(["test"]); Phaser.Math.RND.sow(["test"]);
// vi.spyOn(Utils, "apiFetch", "get").mockReturnValue(fetch); // vi.spyOn(Utils, "apiFetch", "get").mockReturnValue(fetch);
if (bypassLogin) { if (bypassLogin) {
vi.spyOn(bypassLoginModule, "bypassLogin", "get").mockReturnValue(true); vi.spyOn(bypassLoginModule, "bypassLogin", "get").mockReturnValue(true);
} }
this.game = phaserGame; this.game = phaserGame;
// TODO: Move these mocks elsewhere
MoveAnim.prototype.getAnim = () => ({ MoveAnim.prototype.getAnim = () => ({
frames: {}, frames: {},
}); });
@ -53,14 +55,26 @@ export class GameWrapper {
PokedexMonContainer.prototype.remove = MockContainer.prototype.remove; PokedexMonContainer.prototype.remove = MockContainer.prototype.remove;
} }
setScene(scene: BattleScene) { /**
* Initialize the given {@linkcode BattleScene} and override various properties to avoid crashes with headless games.
* @param scene - The {@linkcode BattleScene} to initialize
* @returns A Promise that resolves once the initialization process has completed.
* @todo Is loading files actually necessary for a headless renderer?
*/
public async setScene(scene: BattleScene): Promise<void> {
this.scene = scene; this.scene = scene;
this.injectMandatory(); this.injectMandatory();
this.scene.preload?.();
this.scene.preload();
this.scene.create(); this.scene.create();
} }
injectMandatory() { /**
* Override this scene and stub out various properties to avoid crashes with headless games.
* @todo Review what parts of this are actually NEEDED
* @todo Overhaul this to work with a multi-scene project
*/
private injectMandatory(): void {
this.game.config = { this.game.config = {
seed: ["test"], seed: ["test"],
gameVersion: version, gameVersion: version,
@ -135,9 +149,12 @@ export class GameWrapper {
this.scene.scale = this.game.scale; this.scene.scale = this.game.scale;
this.scene.textures = this.game.textures; this.scene.textures = this.game.textures;
this.scene.events = this.game.events; this.scene.events = this.game.events;
// TODO: Why is this needed? The `manager` property isn't used anywhere
this.scene.manager = new InputManager(this.game, {}); this.scene.manager = new InputManager(this.game, {});
this.scene.manager.keyboard = new KeyboardManager(this.scene); this.scene.manager.keyboard = new KeyboardManager(this.scene);
this.scene.pluginEvents = new EventEmitter(); this.scene.pluginEvents = new EventEmitter();
this.game.domContainer = {} as HTMLDivElement;
// TODO: scenes don't have dom containers
this.scene.domContainer = {} as HTMLDivElement; this.scene.domContainer = {} as HTMLDivElement;
this.scene.spritePipeline = {}; this.scene.spritePipeline = {};
this.scene.fieldSpritePipeline = {}; this.scene.fieldSpritePipeline = {};
@ -179,7 +196,7 @@ export class GameWrapper {
this.scene.sys.updateList = new UpdateList(this.scene); this.scene.sys.updateList = new UpdateList(this.scene);
this.scene.systems = this.scene.sys; this.scene.systems = this.scene.sys;
this.scene.input = this.game.input; this.scene.input = this.game.input;
this.scene.scene = this.scene; this.scene.scene = this.scene; // TODO: This seems wacky
this.scene.input.keyboard = new KeyboardPlugin(this.scene); this.scene.input.keyboard = new KeyboardPlugin(this.scene);
this.scene.input.gamepad = new GamepadPlugin(this.scene); this.scene.input.gamepad = new GamepadPlugin(this.scene);
this.scene.cachedFetch = (url, _init) => { this.scene.cachedFetch = (url, _init) => {

View File

@ -110,7 +110,10 @@ export class ClassicModeHelper extends GameManagerHelper {
* Queue inputs to switch at the start of the next battle, and then start it. * Queue inputs to switch at the start of the next battle, and then start it.
* @param pokemonIndex - The 0-indexed position of the party pokemon to switch to. * @param pokemonIndex - The 0-indexed position of the party pokemon to switch to.
* Should never be called with 0 as that will select the currently active pokemon and freeze * Should never be called with 0 as that will select the currently active pokemon and freeze
* @returns A Promise that resolves once the battle has been started and the switch prompt resolved * @returns A Promise that resolves once the battle has been started and the switch prompt resolved.
* @remarks
* This will temporarily set the current {@linkcode BattleStyle} to `SWITCH` for the duration
* of the `CheckSwitchPhase`.
* @todo Make this work for double battles * @todo Make this work for double battles
* @example * @example
* ```ts * ```ts
@ -119,7 +122,7 @@ export class ClassicModeHelper extends GameManagerHelper {
* ``` * ```
*/ */
public async startBattleWithSwitch(pokemonIndex: number): Promise<void> { public async startBattleWithSwitch(pokemonIndex: number): Promise<void> {
this.game.scene.battleStyle = BattleStyle.SWITCH; this.game.settings.battleStyle(BattleStyle.SWITCH);
this.game.onNextPrompt( this.game.onNextPrompt(
"CheckSwitchPhase", "CheckSwitchPhase",
UiMode.CONFIRM, UiMode.CONFIRM,
@ -133,5 +136,6 @@ export class ClassicModeHelper extends GameManagerHelper {
await this.game.phaseInterceptor.to("CommandPhase"); await this.game.phaseInterceptor.to("CommandPhase");
console.log("==================[New Battle (Initial Switch)]=================="); console.log("==================[New Battle (Initial Switch)]==================");
this.game.settings.battleStyle(BattleStyle.SET);
} }
} }

View File

@ -0,0 +1,170 @@
import { UiMode } from "#enums/ui-mode";
import type { GameManager } from "#test/test-utils/game-manager";
import { GameManagerHelper } from "#test/test-utils/helpers/game-manager-helper";
import { getEnumStr } from "#test/test-utils/string-utils";
import type { PhaseString } from "#types/phase-types";
import type { AwaitableUiHandler } from "#ui/handlers/awaitable-ui-handler";
import type { UI } from "#ui/ui";
import chalk from "chalk";
import { vi } from "vitest";
interface UIPrompt {
/** The {@linkcode PhaseString | name} of the Phase during which to execute the callback. */
phaseTarget: PhaseString;
/** The {@linkcode UIMode} to wait for. */
mode: UiMode;
/** The callback function to execute. */
callback: () => void;
/**
* An optional callback function to determine if the prompt has expired and should be removed.
* Expired prompts are removed upon the next UI mode change without executing their callback.
*/
expireFn?: () => boolean;
/**
* If `true`, restricts the prompt to only activate when the current {@linkcode AwaitableUiHandler} is waiting for input.
* @defaultValue `false`
*/
awaitingActionInput: boolean;
}
/**
* Array of phases that hang whiile waiting for player input.
* Changing UI modes during these phases will halt the phase interceptor.
* @todo This is an extremely unintuitive solution that only works on a select few phases
* and does not account for UI handlers not accepting input
*/
const endBySetMode: ReadonlyArray<PhaseString> = [
"CommandPhase",
"TitlePhase",
"SelectGenderPhase",
"SelectStarterPhase",
"SelectModifierPhase",
"MysteryEncounterPhase",
"PostMysteryEncounterPhase",
];
/**
* Helper class to handle executing prompts upon UI mode changes.
* @todo Remove once a UI overhaul occurs -
* using this correctly effectively requires one to know the entire phase heiarchy
*/
export class PromptHandler extends GameManagerHelper {
/** An array of {@linkcode UIPrompt | prompts} with associated callbacks. */
private prompts: UIPrompt[] = [];
/** The original `setModeInternal` function, stored for use in {@linkcode setMode}. */
private originalSetModeInternal: (typeof this.game.scene.ui)["setModeInternal"];
/** A {@linkcode NodeJS.Timeout | Timeout} containing an interval used to check prompts. */
public static runInterval?: NodeJS.Timeout;
constructor(game: GameManager) {
super(game);
this.originalSetModeInternal = this.game.scene.ui["setModeInternal"];
// `any` assertion needed as we are mocking private property
vi.spyOn(
this.game.scene.ui as UI & Pick<{ setModeInternal: UI["setModeInternal"] }, "setModeInternal">,
"setModeInternal",
).mockImplementation((...args) => this.setMode(args));
// Set an interval to repeatedly check the current prompt.
if (PromptHandler.runInterval) {
throw new Error("Prompt handler run interval was not properly cleared on test end!");
}
PromptHandler.runInterval = setInterval(() => this.doPromptCheck());
}
/**
* Helper method to wrap UI mode changing.
* @param args - Arguments being passed to the original method
* @returns The original return value.
* @todo Make this wait for the actual UI mode setting
*/
private setMode(
args: Parameters<typeof this.originalSetModeInternal>,
): ReturnType<typeof this.originalSetModeInternal> {
const mode = args[0];
this.doLog(
`UI mode changed from ${getEnumStr(UiMode, this.game.scene.ui.getMode())} to ${getEnumStr(UiMode, mode)}!`,
);
// TODO: Add `await` to this
const ret = this.originalSetModeInternal.apply(this.game.scene.ui, args);
const currentPhase = this.game.scene.phaseManager.getCurrentPhase()?.phaseName!;
if (endBySetMode.includes(currentPhase)) {
this.game.phaseInterceptor.checkMode();
}
return ret;
}
/**
* Method to perform prompt handling every so often.
*/
private doPromptCheck(): void {
if (this.prompts.length === 0) {
return;
}
const prompt = this.prompts[0];
// remove expired prompts
if (prompt.expireFn?.()) {
this.prompts.shift();
return;
}
const currentPhase = this.game.scene.phaseManager.getCurrentPhase()?.phaseName;
const currentHandler = this.game.scene.ui.getHandler();
const mode = this.game.scene.ui.getMode();
// If the current mode, phase, and handler match the expected values, execute the callback and continue.
// If not, leave it there.
if (
mode === prompt.mode
&& currentPhase === prompt.phaseTarget
&& currentHandler.active
&& !(prompt.awaitingActionInput && !(currentHandler as AwaitableUiHandler)["awaitingActionInput"])
) {
prompt.callback();
this.prompts.shift();
}
}
/**
* Queue a callback to be executed on the next UI mode change.
* This can be used to (among other things) simulate inputs or run callbacks mid-phase.
* @param phaseTarget - The {@linkcode PhaseString | name} of the Phase during which the callback will be executed
* @param mode - The {@linkcode UiMode} to wait for
* @param callback - The callback function to execute
* @param expireFn - Optional function to determine if the prompt has expired
* @param awaitingActionInput - If `true`, restricts the prompt to only activate when the current {@linkcode AwaitableUiHandler} is waiting for input; default `false`
* @remarks
* If multiple prompts are queued up in succession, each will be checked in turn **until the first prompt that neither expires nor matches**.
* @todo Review all uses of this function to check if they can be made synchronous
*/
public addToNextPrompt(
phaseTarget: PhaseString,
mode: UiMode,
callback: () => void,
expireFn?: () => boolean,
awaitingActionInput = false,
) {
this.prompts.push({
phaseTarget,
mode,
callback,
expireFn,
awaitingActionInput,
});
}
/**
* Wrapper function to add coloration to phase logs.
* @param args - Arguments to original logging function
*/
// TODO: Move this to colors.ts & change color after mock console PR
private doLog(...args: unknown[]): void {
console.log(chalk.hex("#008B8B")(...args));
}
}

View File

@ -1,8 +1,4 @@
import { BattleStyle } from "#enums/battle-style";
import { UiMode } from "#enums/ui-mode";
import { CommandPhase } from "#phases/command-phase";
import { TitlePhase } from "#phases/title-phase"; import { TitlePhase } from "#phases/title-phase";
import { TurnInitPhase } from "#phases/turn-init-phase";
import type { GameManager } from "#test/test-utils/game-manager"; import type { GameManager } from "#test/test-utils/game-manager";
import { GameManagerHelper } from "#test/test-utils/helpers/game-manager-helper"; import { GameManagerHelper } from "#test/test-utils/helpers/game-manager-helper";
import type { SessionSaveData } from "#types/save-data"; import type { SessionSaveData } from "#types/save-data";
@ -18,11 +14,9 @@ export class ReloadHelper extends GameManagerHelper {
super(game); super(game);
// Whenever the game saves the session, save it to the reloadHelper instead // Whenever the game saves the session, save it to the reloadHelper instead
vi.spyOn(game.scene.gameData, "saveAll").mockImplementation(() => { vi.spyOn(game.scene.gameData, "saveAll").mockImplementation(async () => {
return new Promise<boolean>((resolve, _reject) => {
this.sessionData = game.scene.gameData.getSessionSaveData(); this.sessionData = game.scene.gameData.getSessionSaveData();
resolve(true); return true;
});
}); });
} }
@ -38,11 +32,7 @@ export class ReloadHelper extends GameManagerHelper {
scene.phaseManager.clearPhaseQueue(); scene.phaseManager.clearPhaseQueue();
// Set the last saved session to the desired session data // Set the last saved session to the desired session data
vi.spyOn(scene.gameData, "getSession").mockReturnValue( vi.spyOn(scene.gameData, "getSession").mockReturnValue(Promise.resolve(this.sessionData));
new Promise((resolve, _reject) => {
resolve(this.sessionData);
}),
);
scene.phaseManager.unshiftPhase(titlePhase); scene.phaseManager.unshiftPhase(titlePhase);
this.game.endPhase(); // End the currently ongoing battle this.game.endPhase(); // End the currently ongoing battle
@ -56,33 +46,9 @@ export class ReloadHelper extends GameManagerHelper {
); );
this.game.scene.modifiers = []; this.game.scene.modifiers = [];
} }
titlePhase.loadSaveSlot(-1); // Load the desired session data await titlePhase["loadSaveSlot"](-1); // Load the desired session data
this.game.phaseInterceptor.shiftPhase(); // Loading the save slot also ended TitlePhase, clean it up
// Run through prompts for switching Pokemon, copied from classicModeHelper.ts await this.game.phaseInterceptor.to("CommandPhase");
if (this.game.scene.battleStyle === BattleStyle.SWITCH) {
this.game.onNextPrompt(
"CheckSwitchPhase",
UiMode.CONFIRM,
() => {
this.game.setMode(UiMode.MESSAGE);
this.game.endPhase();
},
() => this.game.isCurrentPhase(CommandPhase) || this.game.isCurrentPhase(TurnInitPhase),
);
this.game.onNextPrompt(
"CheckSwitchPhase",
UiMode.CONFIRM,
() => {
this.game.setMode(UiMode.MESSAGE);
this.game.endPhase();
},
() => this.game.isCurrentPhase(CommandPhase) || this.game.isCurrentPhase(TurnInitPhase),
);
}
await this.game.phaseInterceptor.to(CommandPhase);
console.log("==================[New Turn (Reloaded)]=================="); console.log("==================[New Turn (Reloaded)]==================");
} }
} }

View File

@ -1,53 +1,81 @@
import { SETTINGS_COLOR } from "#app/constants/colors"; import { SETTINGS_COLOR } from "#app/constants/colors";
import { BattleStyle } from "#enums/battle-style"; import { BattleStyle } from "#enums/battle-style";
import { ExpGainsSpeed } from "#enums/exp-gains-speed"; import { ExpGainsSpeed } from "#enums/exp-gains-speed";
import { ExpNotification } from "#enums/exp-notification";
import { PlayerGender } from "#enums/player-gender"; import { PlayerGender } from "#enums/player-gender";
import type { GameManager } from "#test/test-utils/game-manager";
import { GameManagerHelper } from "#test/test-utils/helpers/game-manager-helper"; import { GameManagerHelper } from "#test/test-utils/helpers/game-manager-helper";
import { getEnumStr } from "#test/test-utils/string-utils";
import chalk from "chalk"; import chalk from "chalk";
/** /**
* Helper to handle settings for tests * Helper to handle changing game settings for tests.
*/ */
export class SettingsHelper extends GameManagerHelper { export class SettingsHelper extends GameManagerHelper {
private _battleStyle: BattleStyle = BattleStyle.SET; constructor(game: GameManager) {
super(game);
get battleStyle(): BattleStyle { this.initDefaultSettings();
return this._battleStyle;
} }
/** /**
* Change the battle style to Switch or Set mode (tests default to {@linkcode BattleStyle.SET}) * Initialize default settings upon starting a new test case.
* @param mode {@linkcode BattleStyle.SWITCH} or {@linkcode BattleStyle.SET}
*/ */
set battleStyle(mode: BattleStyle.SWITCH | BattleStyle.SET) { private initDefaultSettings(): void {
this._battleStyle = mode; this.game.scene.gameSpeed = 5;
this.game.scene.moveAnimations = false;
this.game.scene.showLevelUpStats = false;
this.game.scene.expGainsSpeed = ExpGainsSpeed.SKIP;
this.game.scene.expParty = ExpNotification.SKIP;
this.game.scene.hpBarSpeed = 3;
this.game.scene.enableTutorials = false;
this.game.scene.battleStyle = BattleStyle.SET;
this.game.scene.gameData.gender = PlayerGender.MALE; // set initial player gender;
this.game.scene.fieldVolume = 0;
} }
/** /**
* Disable/Enable type hints settings * Change the current {@linkcode BattleStyle}.
* @param enable true to enabled, false to disabled * @param style - The `BattleStyle` to set
* @returns `this`
*/ */
typeHints(enable: boolean): void { public battleStyle(style: BattleStyle): this {
this.game.scene.battleStyle = style;
this.log(`Battle Style set to ${getEnumStr(BattleStyle, style)}!`);
return this;
}
/**
* Toggle the availability of type hints.
* @param enable - Whether to enable or disable type hints
* @returns `this`
*/
public typeHints(enable: boolean): this {
this.game.scene.typeHints = enable; this.game.scene.typeHints = enable;
this.log(`Type Hints ${enable ? "enabled" : "disabled"}`); this.log(`Type Hints ${enable ? "enabled" : "disabled"}!`);
return this;
} }
/** /**
* Change the player gender * Change the player character's selected gender.
* @param gender the {@linkcode PlayerGender} to set * @param gender - The {@linkcode PlayerGender} to set
* @returns `this`
*/ */
playerGender(gender: PlayerGender) { public playerGender(gender: PlayerGender): this {
this.game.scene.gameData.gender = gender; this.game.scene.gameData.gender = gender;
this.log(`Gender set to: ${PlayerGender[gender]} (=${gender})`); this.log(`Gender set to ${getEnumStr(PlayerGender, gender)}!`);
return this;
} }
/** /**
* Change the exp gains speed * Change the current {@linkcode ExpGainsSpeed}.
* @param speed the {@linkcode ExpGainsSpeed} to set * @param speed - The speed to set
* @returns `this`
*/ */
expGainsSpeed(speed: ExpGainsSpeed) { public expGainsSpeed(speed: ExpGainsSpeed): this {
this.game.scene.expGainsSpeed = speed; this.game.scene.expGainsSpeed = speed;
this.log(`Exp Gains Speed set to: ${ExpGainsSpeed[speed]} (=${speed})`); this.log(`EXP Gain bar speed set to ${getEnumStr(ExpGainsSpeed, speed)}!`);
return this;
} }
private log(...params: any[]) { private log(...params: any[]) {

View File

@ -0,0 +1,12 @@
import { Phase } from "#app/phase";
/**
* A rudimentary mock of a phase used for unit tests.
* Ends upon starting by default.
*/
export abstract class mockPhase extends Phase {
public phaseName: any;
public override start() {
this.end();
}
}

View File

@ -1,450 +1,229 @@
import type { PhaseManager, PhaseString } from "#app/@types/phase-types";
import type { BattleScene } from "#app/battle-scene"; import type { BattleScene } from "#app/battle-scene";
import { Phase } from "#app/phase"; import { PHASE_INTERCEPTOR_COLOR, PHASE_START_COLOR } from "#app/constants/colors";
import type { Phase } from "#app/phase";
import type { Constructor } from "#app/utils/common";
import { UiMode } from "#enums/ui-mode"; import { UiMode } from "#enums/ui-mode";
import { AttemptCapturePhase } from "#phases/attempt-capture-phase"; // biome-ignore-start lint/correctness/noUnusedImports: TSDoc imports
import { AttemptRunPhase } from "#phases/attempt-run-phase"; import type { GameManager } from "#test/test-utils/game-manager";
import { BattleEndPhase } from "#phases/battle-end-phase"; import type { PromptHandler } from "#test/test-utils/helpers/prompt-handler";
import { BerryPhase } from "#phases/berry-phase"; // biome-ignore-end lint/correctness/noUnusedImports: TSDoc imports
import { CheckSwitchPhase } from "#phases/check-switch-phase"; import { inspect } from "util";
import { CommandPhase } from "#phases/command-phase"; import chalk from "chalk";
import { DamageAnimPhase } from "#phases/damage-anim-phase"; import { vi } from "vitest";
import { EggLapsePhase } from "#phases/egg-lapse-phase"; import { getEnumStr } from "./string-utils";
import { EncounterPhase } from "#phases/encounter-phase";
import { EndEvolutionPhase } from "#phases/end-evolution-phase";
import { EnemyCommandPhase } from "#phases/enemy-command-phase";
import { EvolutionPhase } from "#phases/evolution-phase";
import { ExpPhase } from "#phases/exp-phase";
import { FaintPhase } from "#phases/faint-phase";
import { FormChangePhase } from "#phases/form-change-phase";
import { GameOverModifierRewardPhase } from "#phases/game-over-modifier-reward-phase";
import { GameOverPhase } from "#phases/game-over-phase";
import { LearnMovePhase } from "#phases/learn-move-phase";
import { LevelCapPhase } from "#phases/level-cap-phase";
import { LoginPhase } from "#phases/login-phase";
import { MessagePhase } from "#phases/message-phase";
import { ModifierRewardPhase } from "#phases/modifier-reward-phase";
import { MoveEffectPhase } from "#phases/move-effect-phase";
import { MoveEndPhase } from "#phases/move-end-phase";
import { MovePhase } from "#phases/move-phase";
import {
MysteryEncounterBattlePhase,
MysteryEncounterOptionSelectedPhase,
MysteryEncounterPhase,
MysteryEncounterRewardsPhase,
PostMysteryEncounterPhase,
} from "#phases/mystery-encounter-phases";
import { NewBattlePhase } from "#phases/new-battle-phase";
import { NewBiomeEncounterPhase } from "#phases/new-biome-encounter-phase";
import { NextEncounterPhase } from "#phases/next-encounter-phase";
import { PartyExpPhase } from "#phases/party-exp-phase";
import { PartyHealPhase } from "#phases/party-heal-phase";
import { PokemonHealPhase } from "#phases/pokemon-heal-phase";
import { PokemonTransformPhase } from "#phases/pokemon-transform-phase";
import { PositionalTagPhase } from "#phases/positional-tag-phase";
import { PostGameOverPhase } from "#phases/post-game-over-phase";
import { PostSummonPhase } from "#phases/post-summon-phase";
import { QuietFormChangePhase } from "#phases/quiet-form-change-phase";
import { RevivalBlessingPhase } from "#phases/revival-blessing-phase";
import { RibbonModifierRewardPhase } from "#phases/ribbon-modifier-reward-phase";
import { SelectBiomePhase } from "#phases/select-biome-phase";
import { SelectGenderPhase } from "#phases/select-gender-phase";
import { SelectModifierPhase } from "#phases/select-modifier-phase";
import { SelectStarterPhase } from "#phases/select-starter-phase";
import { SelectTargetPhase } from "#phases/select-target-phase";
import { ShinySparklePhase } from "#phases/shiny-sparkle-phase";
import { ShowAbilityPhase } from "#phases/show-ability-phase";
import { StatStageChangePhase } from "#phases/stat-stage-change-phase";
import { SummonPhase } from "#phases/summon-phase";
import { SwitchPhase } from "#phases/switch-phase";
import { SwitchSummonPhase } from "#phases/switch-summon-phase";
import { TitlePhase } from "#phases/title-phase";
import { ToggleDoublePositionPhase } from "#phases/toggle-double-position-phase";
import { TurnEndPhase } from "#phases/turn-end-phase";
import { TurnInitPhase } from "#phases/turn-init-phase";
import { TurnStartPhase } from "#phases/turn-start-phase";
import { UnavailablePhase } from "#phases/unavailable-phase";
import { UnlockPhase } from "#phases/unlock-phase";
import { VictoryPhase } from "#phases/victory-phase";
import { ErrorInterceptor } from "#test/test-utils/error-interceptor";
import type { PhaseClass, PhaseString } from "#types/phase-types";
import type { AwaitableUiHandler } from "#ui/awaitable-ui-handler";
import { UI } from "#ui/ui";
export interface PromptHandler { /**
phaseTarget?: string; * The interceptor's current state.
mode?: UiMode; * Possible values are the following:
callback?: () => void; * - `running`: The interceptor is currently running a phase.
expireFn?: () => void; * - `interrupted`: The interceptor has been interrupted by a UI prompt or similar mechanism,
awaitingActionInput?: boolean; * and is currently waiting for the current phase to end.
} * - `idling`: The interceptor is not currently running a phase and is ready to start a new one.
*/
type PhaseInterceptorPhase = PhaseClass | PhaseString; type StateType = "running" | "interrupted" | "idling";
interface PhaseStub {
start(): void;
endBySetMode: boolean;
}
interface InProgressStub {
name: string;
callback(): void;
onError(error: any): void;
}
interface onHoldStub {
name: string;
call(): void;
}
/**
* The PhaseInterceptor is a wrapper around the `BattleScene`'s {@linkcode PhaseManager}.
* It allows tests to exert finer control over the phase system, providing logging, manual advancing, and other helpful utilities.
*/
export class PhaseInterceptor { export class PhaseInterceptor {
public scene: BattleScene; private scene: BattleScene;
// @ts-expect-error: initialized in `initPhases`
public phases: Record<PhaseString, PhaseStub> = {};
public log: PhaseString[];
/** /**
* TODO: This should not be an array; * A log containing all phases having been executed in FIFO order. \
* Our linear phase system means only 1 phase is ever started at once (if any) * Entries are appended each time {@linkcode run} is called, and can be cleared manually with {@linkcode clearLogs}.
*/ */
private onHold: onHoldStub[]; public log: PhaseString[] = [];
private interval: NodeJS.Timeout;
private promptInterval: NodeJS.Timeout;
private intervalRun: NodeJS.Timeout;
private prompts: PromptHandler[];
private inProgress?: InProgressStub;
private originalSetMode: typeof UI.prototype.setMode;
private originalSuperEnd: typeof Phase.prototype.end;
/** /**
* List of phases with their corresponding start methods. * The interceptor's current state.
* * Possible values are the following:
* CAUTION: If a phase and its subclasses (if any) both appear in this list, * - `running`: The interceptor is currently running a phase.
* make sure that this list contains said phase AFTER all of its subclasses. * - `interrupted`: The interceptor has been interrupted by a UI prompt and is waiting for the caller to end it.
* This way, the phase's `prototype.start` is properly preserved during * - `idling`: The interceptor is not currently running a phase and is ready to start a new one.
* `initPhases()` so that its subclasses can use `super.start()` properly. * @defaultValue `idling`
*/ */
private PHASES = [ private state: StateType = "idling";
LoginPhase, /** The current target that is being ran to. */
TitlePhase, private target: PhaseString;
SelectGenderPhase,
NewBiomeEncounterPhase,
SelectStarterPhase,
PostSummonPhase,
SummonPhase,
ToggleDoublePositionPhase,
CheckSwitchPhase,
ShowAbilityPhase,
MessagePhase,
TurnInitPhase,
CommandPhase,
EnemyCommandPhase,
TurnStartPhase,
MovePhase,
MoveEffectPhase,
DamageAnimPhase,
FaintPhase,
BerryPhase,
TurnEndPhase,
BattleEndPhase,
EggLapsePhase,
SelectModifierPhase,
NextEncounterPhase,
NewBattlePhase,
VictoryPhase,
LearnMovePhase,
MoveEndPhase,
StatStageChangePhase,
ShinySparklePhase,
SelectTargetPhase,
UnavailablePhase,
QuietFormChangePhase,
SwitchPhase,
SwitchSummonPhase,
PartyHealPhase,
FormChangePhase,
EvolutionPhase,
EndEvolutionPhase,
LevelCapPhase,
AttemptRunPhase,
SelectBiomePhase,
PositionalTagPhase,
PokemonTransformPhase,
MysteryEncounterPhase,
MysteryEncounterOptionSelectedPhase,
MysteryEncounterBattlePhase,
MysteryEncounterRewardsPhase,
PostMysteryEncounterPhase,
RibbonModifierRewardPhase,
GameOverModifierRewardPhase,
ModifierRewardPhase,
PartyExpPhase,
ExpPhase,
EncounterPhase,
GameOverPhase,
UnlockPhase,
PostGameOverPhase,
RevivalBlessingPhase,
PokemonHealPhase,
AttemptCapturePhase,
];
private endBySetMode = [
TitlePhase,
SelectGenderPhase,
CommandPhase,
SelectStarterPhase,
SelectModifierPhase,
MysteryEncounterPhase,
PostMysteryEncounterPhase,
];
/** /**
* Constructor to initialize the scene and properties, and to start the phase handling. * Initialize a new PhaseInterceptor.
* @param scene - The scene to be managed. * @param scene - The scene to be managed
* @todo This should take a GameManager instance once multi scene stuff becomes a reality
* @remarks
* This overrides {@linkcode PhaseManager.startCurrentPhase} to toggle the interceptor's state
* instead of immediately starting the next phase.
*/ */
constructor(scene: BattleScene) { constructor(scene: BattleScene) {
this.scene = scene; this.scene = scene;
this.onHold = []; vi.spyOn(
this.prompts = []; this.scene.phaseManager as PhaseManager &
this.clearLogs(); Pick<
this.startPromptHandler(); {
this.initPhases(); startCurrentPhase: PhaseManager["startCurrentPhase"];
} },
"startCurrentPhase"
/** >,
* Clears phase logs "startCurrentPhase",
*/ ).mockImplementation(() => {
clearLogs() { this.state = "idling";
this.log = []; });
}
rejectAll(error) {
if (this.inProgress) {
clearInterval(this.promptInterval);
clearInterval(this.interval);
clearInterval(this.intervalRun);
this.inProgress.onError(error);
}
} }
/** /**
* Method to transition to a target phase. * Method to transition to a target phase.
* @param phaseTo - The phase to transition to. * @param target - The name of the {@linkcode Phase} to transition to
* @param runTarget - Whether or not to run the target phase; default `true`. * @param runTarget - Whether or not to run the target phase before resolving; default `true`
* @returns A promise that resolves when the transition is complete. * @returns A Promise that resolves once `target` has been reached.
* @remarks
* This will not resolve for _any_ reason until the target phase has been reached.
* @example
* ```ts
* await game.phaseInterceptor.to("MoveEffectPhase", false);
* ```
*/ */
async to(phaseTo: PhaseInterceptorPhase, runTarget = true): Promise<void> { public async to(target: PhaseString, runTarget?: boolean): Promise<void>;
return new Promise(async (resolve, reject) => { /**
ErrorInterceptor.getInstance().add(this); * @deprecated Use `PhaseString` instead for `target`
const targetName = typeof phaseTo === "string" ? phaseTo : phaseTo.name; */
this.intervalRun = setInterval(async () => { public async to(target: Constructor<Phase>, runTarget?: boolean): Promise<void>;
const currentPhase = this.onHold?.length > 0 && this.onHold[0]; public async to(target: PhaseString | Constructor<Phase>, runTarget = true): Promise<void> {
if (!currentPhase) { this.target = typeof target === "string" ? target : (target.name as PhaseString);
// No current phase means the manager either hasn't started yet
// or we were interrupted by prompt; wait for phase to finish const pm = this.scene.phaseManager;
return;
let currentPhase = pm.getCurrentPhase();
let didLog = false;
// NB: This has to use an interval to wait for UI prompts to activate
// since our UI code effectively stalls when waiting for input.
// This entire function can likely be made synchronous once UI code is moved to a separate scene.
await vi.waitUntil(
async () => {
// If we were interrupted by a UI prompt, we assume that the calling code will queue inputs to
// end the current phase manually, so we just wait for the phase to end from the caller.
if (this.state === "interrupted") {
if (!didLog) {
this.doLog("PhaseInterceptor.to: Waiting for phase to end after being interrupted!");
didLog = true;
}
return false;
} }
// If current phase is different, run it and wait for it to finish. currentPhase = pm.getCurrentPhase();
if (currentPhase.name !== targetName) { if (currentPhase.is(this.target)) {
await this.run().catch(e => { return true;
clearInterval(this.intervalRun);
return reject(e);
});
return;
} }
// Hit target phase; run it and resolve // Current phase is different; run and wait for it to finish.
clearInterval(this.intervalRun); await this.run(currentPhase);
return false;
},
{ interval: 0, timeout: 20_000 },
);
// We hit the target; run as applicable and wrap up.
if (!runTarget) { if (!runTarget) {
return resolve(); this.doLog(`PhaseInterceptor.to: Stopping before running ${this.target}`);
} return;
await this.run().catch(e => {
clearInterval(this.intervalRun);
return reject(e);
});
return resolve();
});
});
} }
/** await this.run(currentPhase);
* Method to run the current phase with an optional skip function. this.doLog(
* @returns A promise that resolves when the phase is run. `PhaseInterceptor.to: Stopping ${this.state === "interrupted" ? `after reaching ${getEnumStr(UiMode, this.scene.ui.getMode())} during` : "on completion of"} ${this.target}`,
*/
private run(): Promise<void> {
// @ts-expect-error: This is apparently mandatory to avoid a crash; review if this is needed
this.scene.moveAnimations = null;
return new Promise(async (resolve, reject) => {
ErrorInterceptor.getInstance().add(this);
const interval = setInterval(async () => {
const currentPhase = this.onHold.shift();
if (currentPhase) {
clearInterval(interval);
this.inProgress = {
name: currentPhase.name,
callback: () => {
ErrorInterceptor.getInstance().remove(this);
resolve();
},
onError: error => reject(error),
};
currentPhase.call();
}
});
});
}
/**
* Remove the current phase from the phase interceptor.
*
* Do not call this unless absolutely necessary. This function is intended
* for cleaning up the phase interceptor when, for whatever reason, a phase
* is manually ended without using the phase interceptor.
*
* @param shouldRun Whether or not the current scene should also be run.
*/
shiftPhase(shouldRun = false): void {
this.onHold.shift();
if (shouldRun) {
this.scene.phaseManager.shiftPhase();
}
}
/**
* Method to initialize phases and their corresponding methods.
*/
initPhases() {
this.originalSetMode = UI.prototype.setMode;
this.originalSuperEnd = Phase.prototype.end;
UI.prototype.setMode = (mode, ...args) => this.setMode.call(this, mode, ...args);
Phase.prototype.end = () => this.superEndPhase.call(this);
for (const phase of this.PHASES) {
const originalStart = phase.prototype.start;
this.phases[phase.name] = {
start: originalStart,
endBySetMode: this.endBySetMode.some(elm => elm.name === phase.name),
};
phase.prototype.start = () => this.startPhase.call(this, phase);
}
}
/**
* Method to start a phase and log it.
* @param phase - The phase to start.
*/
startPhase(phase: PhaseClass) {
this.log.push(phase.name as PhaseString);
const instance = this.scene.phaseManager.getCurrentPhase();
this.onHold.push({
name: phase.name,
call: () => {
this.phases[phase.name].start.apply(instance);
},
});
}
/**
* Method to end a phase and log it.
* @param phase - The phase to start.
*/
private superEndPhase() {
const instance = this.scene.phaseManager.getCurrentPhase();
this.originalSuperEnd.apply(instance);
this.inProgress?.callback();
this.inProgress = undefined;
}
/**
* m2m to set mode.
* @param mode - The {@linkcode UiMode} to set.
* @param args - Additional arguments to pass to the original method.
*/
setMode(mode: UiMode, ...args: unknown[]): Promise<void> {
// TODO: remove the `!` in PR 6243 / after PR 6243 is merged
const currentPhase = this.scene.phaseManager.getCurrentPhase()!;
const instance = this.scene.ui;
console.log("setMode", `${UiMode[mode]} (=${mode})`, args);
const ret = this.originalSetMode.apply(instance, [mode, ...args]);
if (!this.phases[currentPhase.constructor.name]) {
throw new Error(
`missing ${currentPhase.constructor.name} in phaseInterceptor PHASES list --- Add it to PHASES inside of /test/utils/phaseInterceptor.ts`,
); );
} }
if (this.phases[currentPhase.constructor.name].endBySetMode) {
this.inProgress?.callback();
this.inProgress = undefined;
}
return ret;
}
/** /**
* Method to start the prompt handler. * Internal wrapper method to start a phase and wait until it finishes.
* @param currentPhase - The {@linkcode Phase} to run
* @returns A Promise that resolves when the phase has completed running.
*/ */
startPromptHandler() { private async run(currentPhase: Phase): Promise<void> {
this.promptInterval = setInterval(() => { try {
if (this.prompts.length > 0) { this.state = "running";
const actionForNextPrompt = this.prompts[0]; this.logPhase(currentPhase.phaseName);
const expireFn = actionForNextPrompt.expireFn?.(); currentPhase.start();
const currentMode = this.scene.ui.getMode(); await vi.waitUntil(
const currentPhase = this.scene.phaseManager.getCurrentPhase().phaseName; () => this.state !== "running",
const currentHandler = this.scene.ui.getHandler(); { interval: 50, timeout: 20_000 }, // TODO: Figure out an appropriate timeout for individual phases
if (expireFn) { );
this.prompts.shift(); } catch (error) {
} else if ( throw error instanceof Error
currentMode === actionForNextPrompt.mode ? error
&& currentPhase === actionForNextPrompt.phaseTarget : new Error(`Unknown error occurred while running phase ${currentPhase.phaseName}!\nError: ${inspect(error)}`);
&& currentHandler.active
&& (!actionForNextPrompt.awaitingActionInput
|| (actionForNextPrompt.awaitingActionInput
&& (currentHandler as AwaitableUiHandler)["awaitingActionInput"]))
) {
const prompt = this.prompts.shift();
if (prompt?.callback) {
prompt.callback();
} }
} }
}
});
}
/** /**
* Method to add an action to the next prompt. * If this is at the target phase, unlock the interceptor and
* @param phaseTarget - The target phase for the prompt. * return control back to the caller once the calling phase has finished.
* @param mode - The mode of the UI. * @remarks
* @param callback - The callback function to execute. * This should not be called by anything other than {@linkcode PromptHandler}.
* @param expireFn - The function to determine if the prompt has expired.
* @param awaitingActionInput - ???; default `false`
*/ */
addToNextPrompt( public checkMode(): void {
phaseTarget: string, const currentPhase = this.scene.phaseManager.getCurrentPhase();
mode: UiMode, if (!currentPhase.is(this.target) || this.state === "interrupted") {
callback: () => void, // Wrong phase / already interrupted = do nothing
expireFn?: () => void, return;
awaitingActionInput = false, }
) {
this.prompts.push({ // Interrupt the phase and return control to the caller
phaseTarget, this.state = "interrupted";
mode,
callback,
expireFn,
awaitingActionInput,
});
} }
/** /**
* Restores the original state of phases and clears intervals. * Skip the next upcoming phase.
* @throws Error if currently running a phase.
* @remarks
* This function should be used for skipping phases _not yet started_.
* To end ones already in the process of running, use {@linkcode GameManager.endPhase}.
* @example
* await game.phaseInterceptor.to("LoginPhase", false);
* game.phaseInterceptor.shiftPhase(); // skips LoginPhase without starting it
*/
public shiftPhase(): void {
const phaseName = this.scene.phaseManager.getCurrentPhase().phaseName;
if (this.state !== "idling") {
throw new Error(`PhaseInterceptor.shiftPhase attempted to skip phase ${phaseName} mid-execution!`);
}
this.doLog(`Skipping current phase: ${phaseName}`);
this.scene.phaseManager.shiftPhase();
}
/**
* Deprecated no-op function.
* *
* This function iterates through all phases and resets their `start` method to the original * This was previously used to reset timers created using `setInterval` to wait for phases to end
* function stored in `this.phases`. Additionally, it clears the `promptInterval` and `interval`. * and undo various method stubs after each test run. \
* However, since we now use {@linkcode vi.waitUntil} and {@linkcode vi.spyOn} to perform these tasks
* respectively, this function has become no longer needed.
* @deprecated This is no longer needed and will be removed in a future PR
*/ */
restoreOg() { public restoreOg() {}
for (const phase of this.PHASES) {
phase.prototype.start = this.phases[phase.name].start; /**
* Method to log the start of a phase.
* Called in place of {@linkcode PhaseManager.startCurrentPhase} to allow for manual intervention.
* @param phaseName - The name of the phase to log
*/
private logPhase(phaseName: PhaseString): void {
console.log(`%cStart Phase: ${phaseName}`, `color:${PHASE_START_COLOR}`);
this.log.push(phaseName);
} }
UI.prototype.setMode = this.originalSetMode;
Phase.prototype.end = this.originalSuperEnd; /**
clearInterval(this.promptInterval); * Clear all prior phase logs.
clearInterval(this.interval); */
clearInterval(this.intervalRun); public clearLogs(): void {
this.log = [];
}
/**
* Wrapper function to add coral coloration to phase logs.
* @param args - Arguments to original logging function
*/
private doLog(...args: unknown[]): void {
console.log(chalk.hex(PHASE_INTERCEPTOR_COLOR)(...args));
} }
} }

View File

@ -0,0 +1,156 @@
import type { Phase } from "#app/phase";
import { UiMode } from "#enums/ui-mode";
import type { GameManager } from "#test/test-utils/game-manager";
import { PromptHandler } from "#test/test-utils/helpers/prompt-handler";
import type { PhaseInterceptor } from "#test/test-utils/phase-interceptor";
import type { PhaseString } from "#types/phase-types";
import type { AwaitableUiHandler } from "#ui/handlers/awaitable-ui-handler";
import type { UI } from "#ui/ui";
import { beforeAll, beforeEach, describe, expect, it, type Mock, vi } from "vitest";
describe("Test Utils - PromptHandler", () => {
let promptHandler: PromptHandler;
let handler: AwaitableUiHandler;
let callback1: Mock;
let callback2: Mock;
let setModeCallback: Mock;
let checkModeCallback: Mock;
beforeAll(() => {
setModeCallback = vi.fn();
checkModeCallback = vi.fn();
callback1 = vi.fn(() => console.log("callback 1 called!")).mockName("callback 1");
callback2 = vi.fn(() => console.log("callback 2 called!")).mockName("callback 2");
});
beforeEach(() => {
handler = {
active: true,
show: () => {},
awaitingActionInput: true,
} as unknown as AwaitableUiHandler;
promptHandler = new PromptHandler({
scene: {
ui: {
getHandler: () => handler,
setModeInternal: () => {
setModeCallback();
return Promise.resolve();
},
getMode: () => UiMode.TEST_DIALOGUE,
} as unknown as UI,
phaseManager: {
getCurrentPhase: () =>
({
phaseName: "testDialoguePhase",
}) as unknown as Phase,
},
},
phaseInterceptor: {
checkMode: () => {
checkModeCallback();
},
} as PhaseInterceptor,
} as GameManager);
});
// Wrapper func to ignore incorrect typing on `PhaseString`
function onNextPrompt(
target: string,
mode: UiMode,
callback: () => void,
expireFn?: () => boolean,
awaitingActionInput = false,
) {
promptHandler.addToNextPrompt(target as unknown as PhaseString, mode, callback, expireFn, awaitingActionInput);
}
describe("setMode", () => {
it("should wrap and pass along original function arguments from setModeInternal", async () => {
const setModeSpy = vi.spyOn(promptHandler as any, "setMode");
await promptHandler["game"].scene.ui["setModeInternal"](UiMode.PARTY, false, false, false, []);
expect(setModeSpy).toHaveBeenCalledExactlyOnceWith([UiMode.PARTY, false, false, false, []]);
expect(setModeCallback).toHaveBeenCalledAfter(setModeSpy);
});
it("should call PhaseInterceptor.checkMode if current phase in `endBySetMode`", async () => {
promptHandler["game"]["scene"]["phaseManager"]["getCurrentPhase"] = () =>
({ phaseName: "CommandPhase" }) as Phase;
await promptHandler["game"].scene.ui["setModeInternal"](UiMode.PARTY, false, false, false, []);
expect(checkModeCallback).toHaveBeenCalledOnce();
});
});
describe("doPromptCheck", () => {
it("should check and remove the first prompt matching criteria", () => {
onNextPrompt("testDialoguePhase", UiMode.TEST_DIALOGUE, () => callback1());
onNextPrompt("testDialoguePhase", UiMode.TEST_DIALOGUE, () => callback2());
promptHandler["doPromptCheck"]();
expect(callback1).toHaveBeenCalled();
expect(callback2).not.toHaveBeenCalled();
expect(promptHandler["prompts"]).toHaveLength(1);
});
it.each<{ reason: string; callback: () => void }>([
{
reason: "wrong UI mode",
callback: () => onNextPrompt("testDialoguePhase", UiMode.ACHIEVEMENTS, () => callback1()),
},
{
reason: "wrong phase",
callback: () => onNextPrompt("wrong phase", UiMode.TEST_DIALOGUE, () => callback1()),
},
{
reason: "UI handler is inactive",
callback: () => {
handler.active = false;
onNextPrompt("testDialoguePhase", UiMode.TEST_DIALOGUE, () => callback1());
},
},
{
reason: "UI handler is not awaiting input",
callback: () => {
handler["awaitingActionInput"] = false;
onNextPrompt("testDialoguePhase", UiMode.TEST_DIALOGUE, () => callback1(), undefined, true);
},
},
])("should skip callback and keep in queue if $reason", ({ callback }) => {
callback();
onNextPrompt("testDialoguePhase", UiMode.TEST_DIALOGUE, () => callback2);
promptHandler["doPromptCheck"]();
expect(callback1).not.toHaveBeenCalled();
expect(callback2).not.toHaveBeenCalled();
expect(promptHandler["prompts"]).toHaveLength(2);
});
it("should remove expired prompts without blocking", () => {
onNextPrompt(
"testDialoguePhase",
UiMode.TEST_DIALOGUE,
() => callback1(),
() => true,
);
onNextPrompt(
"testDialoguePhase",
UiMode.TEST_DIALOGUE,
() => callback2(),
() => false,
);
promptHandler["doPromptCheck"]();
expect(callback1).not.toHaveBeenCalled();
expect(callback2).not.toHaveBeenCalled();
expect(promptHandler["prompts"]).toHaveLength(1);
promptHandler["doPromptCheck"]();
expect(callback2).toHaveBeenCalledOnce();
expect(promptHandler["prompts"]).toHaveLength(0);
});
});
});

View File

@ -0,0 +1,62 @@
import { MoveId } from "#enums/move-id";
import { SpeciesId } from "#enums/species-id";
import { UiMode } from "#enums/ui-mode";
import { GameManager } from "#test/test-utils/game-manager";
import Phaser from "phaser";
import { beforeAll, beforeEach, describe, expect, it } from "vitest";
describe("Utils - Phase Interceptor - Integration", () => {
let phaserGame: Phaser.Game;
let game: GameManager;
beforeAll(() => {
phaserGame = new Phaser.Game({
type: Phaser.HEADLESS,
});
});
beforeEach(() => {
game = new GameManager(phaserGame);
});
it("runToTitle", async () => {
await game.runToTitle();
expect(game.scene.ui.getMode()).toBe(UiMode.TITLE);
expect(game).toBeAtPhase("TitlePhase");
});
it("runToSummon", async () => {
await game.classicMode.runToSummon([SpeciesId.ABOMASNOW]);
expect(game).toBeAtPhase("SummonPhase");
});
it("startBattle", async () => {
await game.classicMode.startBattle([SpeciesId.RABOOT]);
expect(game.scene.ui.getMode()).toBe(UiMode.COMMAND);
expect(game).toBeAtPhase("CommandPhase");
});
it("1 Full Turn", async () => {
await game.classicMode.startBattle([SpeciesId.REGIELEKI]);
game.move.use(MoveId.SPLASH);
await game.move.forceEnemyMove(MoveId.SPLASH);
await game.toNextTurn();
expect(game.scene.ui.getMode()).toBe(UiMode.COMMAND);
expect(game).toBeAtPhase("CommandPhase");
});
it("should not break when phase ended early via prompt", async () => {
await game.classicMode.startBattle([SpeciesId.REGIELEKI]);
game.onNextPrompt("CommandPhase", UiMode.COMMAND, () => {
game.endPhase();
});
game.move.use(MoveId.BOUNCE);
await game.phaseInterceptor.to("EnemyCommandPhase");
});
});

View File

@ -0,0 +1,150 @@
import { globalScene } from "#app/global-scene";
import type { Phase } from "#app/phase";
import type { Constructor } from "#app/utils/common";
import { GameManager } from "#test/test-utils/game-manager";
import { mockPhase } from "#test/test-utils/mocks/mock-phase";
import type { PhaseString } from "#types/phase-types";
import Phaser from "phaser";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
// TODO: Move these to `mock-phase.ts` if/when unit tests for the phase manager are created
class applePhase extends mockPhase {
public readonly phaseName = "applePhase";
}
class bananaPhase extends mockPhase {
public readonly phaseName = "bananaPhase";
}
class coconutPhase extends mockPhase {
public readonly phaseName = "coconutPhase";
}
class oneSecTimerPhase extends mockPhase {
public readonly phaseName = "oneSecTimerPhase";
start() {
setTimeout(() => {
console.log("1 sec passed!");
this.end();
}, 1000);
}
}
class unshifterPhase extends mockPhase {
public readonly phaseName = "unshifterPhase";
start() {
globalScene.phaseManager.unshiftPhase(new applePhase() as unknown as Phase);
globalScene.phaseManager.unshiftPhase(new bananaPhase() as unknown as Phase);
globalScene.phaseManager.unshiftPhase(new coconutPhase() as unknown as Phase);
this.end();
}
}
describe("Utils - Phase Interceptor - Unit", () => {
let phaserGame: Phaser.Game;
let game: GameManager;
beforeAll(() => {
phaserGame = new Phaser.Game({
type: Phaser.HEADLESS,
});
});
beforeEach(() => {
game = new GameManager(phaserGame);
setPhases(applePhase, bananaPhase, coconutPhase, bananaPhase, coconutPhase);
});
/**
* Helper function to set the phase manager's phases to the specified values and start the first one.
* @param phases - An array of constructors to {@linkcode Phase}s to set.
* Constructors must have no arguments.
*/
function setPhases(...phases: [Constructor<mockPhase>, ...Constructor<mockPhase>[]]) {
game.scene.phaseManager.clearAllPhases();
for (const phase of phases) {
game.scene.phaseManager.unshiftPhase(new phase());
}
game.scene.phaseManager.shiftPhase(); // start the thing going
}
function getQueuedPhases(): string[] {
return game.scene.phaseManager["phaseQueue"]["levels"].flat(2).map(p => p.phaseName);
}
function expectAtPhase(phaseName: string) {
expect(game).toBeAtPhase(phaseName as PhaseString);
}
/** Wrapper function to make TS not complain about incompatible argument typing on `PhaseString`. */
function to(phaseName: string, runTarget?: false): Promise<void> {
return game.phaseInterceptor.to(phaseName as unknown as PhaseString, runTarget);
}
describe("to", () => {
it("should start the specified phase and resolve after it ends", async () => {
await to("applePhase");
expectAtPhase("bananaPhase");
expect(getQueuedPhases()).toEqual(["coconutPhase", "bananaPhase", "coconutPhase"]);
expect(game.phaseInterceptor.log).toEqual(["applePhase"]);
});
it("should run to the specified phase without starting/logging", async () => {
await to("applePhase", false);
expectAtPhase("applePhase");
expect(getQueuedPhases()).toEqual(["bananaPhase", "coconutPhase", "bananaPhase", "coconutPhase"]);
expect(game.phaseInterceptor.log).toEqual([]);
await to("applePhase", false);
// should not do anything
expectAtPhase("applePhase");
expect(getQueuedPhases()).toEqual(["bananaPhase", "coconutPhase", "bananaPhase", "coconutPhase"]);
expect(game.phaseInterceptor.log).toEqual([]);
});
it("should run all phases between start and the first instance of target", async () => {
await to("coconutPhase");
expectAtPhase("bananaPhase");
expect(getQueuedPhases()).toEqual(["coconutPhase"]);
expect(game.phaseInterceptor.log).toEqual(["applePhase", "bananaPhase", "coconutPhase"]);
});
it("should work on newly unshifted phases", async () => {
setPhases(unshifterPhase, coconutPhase); // adds applePhase, bananaPhase and coconutPhase to queue
await to("bananaPhase");
expectAtPhase("coconutPhase");
expect(getQueuedPhases()).toEqual(["coconutPhase"]);
expect(game.phaseInterceptor.log).toEqual(["unshifterPhase", "applePhase", "bananaPhase"]);
});
it("should wait for asynchronous phases to end", async () => {
setPhases(oneSecTimerPhase, coconutPhase);
const callback = vi.fn(() => console.log("fffffff"));
const spy = vi.spyOn(oneSecTimerPhase.prototype, "end");
setTimeout(() => {
callback();
}, 500);
await to("coconutPhase");
expect(callback).toHaveBeenCalled();
expect(spy).toHaveBeenCalled();
});
});
describe("shift", () => {
it("should skip the next phase in line without starting it", async () => {
const startSpy = vi.spyOn(applePhase.prototype, "start");
game.phaseInterceptor.shiftPhase();
expectAtPhase("bananaPhase");
expect(getQueuedPhases()).toEqual(["coconutPhase", "bananaPhase", "coconutPhase"]);
expect(startSpy).not.toHaveBeenCalled();
expect(game.phaseInterceptor.log).toEqual([]);
});
});
});

View File

@ -2,7 +2,6 @@ import { AbilityId } from "#enums/ability-id";
import { ExpGainsSpeed } from "#enums/exp-gains-speed"; import { ExpGainsSpeed } from "#enums/exp-gains-speed";
import { MoveId } from "#enums/move-id"; import { MoveId } from "#enums/move-id";
import { SpeciesId } from "#enums/species-id"; import { SpeciesId } from "#enums/species-id";
import { ExpPhase } from "#phases/exp-phase";
import { GameManager } from "#test/test-utils/game-manager"; import { GameManager } from "#test/test-utils/game-manager";
import Phaser from "phaser"; import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
@ -14,7 +13,8 @@ vi.mock("../data/exp", ({}) => {
}; };
}); });
describe("UI - Battle Info", () => { // TODO: These are jank and need to be redone
describe.todo("UI - Battle Info", () => {
let phaserGame: Phaser.Game; let phaserGame: Phaser.Game;
let game: GameManager; let game: GameManager;
@ -48,7 +48,7 @@ describe("UI - Battle Info", () => {
game.move.select(MoveId.SPLASH); game.move.select(MoveId.SPLASH);
await game.doKillOpponents(); await game.doKillOpponents();
await game.phaseInterceptor.to(ExpPhase, true); await game.phaseInterceptor.to("ExpPhase");
expect(Math.pow).not.toHaveBeenCalledWith(2, expGainsSpeed); expect(Math.pow).not.toHaveBeenCalledWith(2, expGainsSpeed);
}, },

View File

@ -10,7 +10,8 @@ import { type PartyUiHandler, PartyUiMode } from "#ui/party-ui-handler";
import Phaser from "phaser"; import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
describe("UI - Transfer Items", () => { // TODO: Resolve issues with UI test state corruption
describe.todo("UI - Transfer Items", () => {
let phaserGame: Phaser.Game; let phaserGame: Phaser.Game;
let game: GameManager; let game: GameManager;

View File

@ -16,7 +16,8 @@ import i18next from "i18next";
import Phaser from "phaser"; import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
describe("UI - Starter select", () => { // TODO: Resolve issues with UI test state corruption
describe.todo("UI - Starter select", () => {
let phaserGame: Phaser.Game; let phaserGame: Phaser.Game;
let game: GameManager; let game: GameManager;