[Test(refactor)]: Refactor tests game manager (#2062)

* refactor tests game manager

* add setPosition to Mock Text class
This commit is contained in:
Dmitriy K 2024-06-10 17:35:24 -04:00 committed by GitHub
parent 942c119814
commit ee4d130ebb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 96 additions and 88 deletions

View File

@ -300,7 +300,7 @@ describe("Test Battle Phase", () => {
vi.spyOn(overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue([Moves.TACKLE,Moves.TACKLE,Moves.TACKLE,Moves.TACKLE]); vi.spyOn(overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue([Moves.TACKLE,Moves.TACKLE,Moves.TACKLE,Moves.TACKLE]);
await game.startBattle(); await game.startBattle();
const turn = game.scene.currentBattle.turn; const turn = game.scene.currentBattle.turn;
await game.doAttack(0); game.doAttack(0);
await game.toNextTurn(); await game.toNextTurn();
expect(game.scene.currentBattle.turn).toBeGreaterThan(turn); expect(game.scene.currentBattle.turn).toBeGreaterThan(turn);
}, 20000); }, 20000);
@ -318,7 +318,7 @@ describe("Test Battle Phase", () => {
vi.spyOn(overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue([Moves.TACKLE,Moves.TACKLE,Moves.TACKLE,Moves.TACKLE]); vi.spyOn(overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue([Moves.TACKLE,Moves.TACKLE,Moves.TACKLE,Moves.TACKLE]);
await game.startBattle(); await game.startBattle();
const waveIndex = game.scene.currentBattle.waveIndex; const waveIndex = game.scene.currentBattle.waveIndex;
await game.doAttack(0); game.doAttack(0);
await game.doKillOpponents(); await game.doKillOpponents();
await game.toNextWave(); await game.toNextWave();
expect(game.scene.currentBattle.waveIndex).toBeGreaterThan(waveIndex); expect(game.scene.currentBattle.waveIndex).toBeGreaterThan(waveIndex);

View File

@ -37,7 +37,7 @@ describe("Test Battle Phase", () => {
it.skip("to next turn", async() => { it.skip("to next turn", async() => {
await game.startBattle(); await game.startBattle();
const turn = game.scene.currentBattle.turn; const turn = game.scene.currentBattle.turn;
await game.doAttack(0); game.doAttack(0);
await game.toNextTurn(); await game.toNextTurn();
expect(game.scene.currentBattle.turn).toBeGreaterThan(turn); expect(game.scene.currentBattle.turn).toBeGreaterThan(turn);
}, 20000); }, 20000);

View File

@ -3,7 +3,6 @@ import {Mode} from "#app/ui/ui";
import {generateStarter, waitUntil} from "#app/test/utils/gameManagerUtils"; import {generateStarter, waitUntil} from "#app/test/utils/gameManagerUtils";
import { import {
CommandPhase, CommandPhase,
DamagePhase,
EncounterPhase, EncounterPhase,
FaintPhase, FaintPhase,
LoginPhase, NewBattlePhase, LoginPhase, NewBattlePhase,
@ -98,23 +97,23 @@ export default class GameManager {
* Runs the game to the title phase. * Runs the game to the title phase.
* @returns A promise that resolves when the title phase is reached. * @returns A promise that resolves when the title phase is reached.
*/ */
runToTitle(): Promise<void> { async runToTitle(): Promise<void> {
return new Promise(async(resolve, reject) => { await this.phaseInterceptor.run(LoginPhase);
await this.phaseInterceptor.run(LoginPhase).catch((e) => reject(e));
this.onNextPrompt("SelectGenderPhase", Mode.OPTION_SELECT, () => { this.onNextPrompt("SelectGenderPhase", Mode.OPTION_SELECT, () => {
this.scene.gameData.gender = PlayerGender.MALE; this.scene.gameData.gender = PlayerGender.MALE;
this.endPhase(); this.endPhase();
}, () => this.isCurrentPhase(TitlePhase)); }, () => this.isCurrentPhase(TitlePhase));
await this.phaseInterceptor.run(SelectGenderPhase, () => this.isCurrentPhase(TitlePhase)).catch((e) => reject(e));
await this.phaseInterceptor.run(TitlePhase).catch((e) => reject(e)); await this.phaseInterceptor.run(SelectGenderPhase, () => this.isCurrentPhase(TitlePhase));
await this.phaseInterceptor.run(TitlePhase);
this.scene.gameSpeed = 5; this.scene.gameSpeed = 5;
this.scene.moveAnimations = false; this.scene.moveAnimations = false;
this.scene.showLevelUpStats = false; this.scene.showLevelUpStats = false;
this.scene.expGainsSpeed = 3; this.scene.expGainsSpeed = 3;
this.scene.expParty = ExpNotification.SKIP; this.scene.expParty = ExpNotification.SKIP;
this.scene.hpBarSpeed = 3; this.scene.hpBarSpeed = 3;
resolve();
});
} }
/** /**
@ -122,9 +121,9 @@ export default class GameManager {
* @param species - Optional array of species to summon. * @param species - Optional array of species to summon.
* @returns A promise that resolves when the summon phase is reached. * @returns A promise that resolves when the summon phase is reached.
*/ */
runToSummon(species?: Species[]): Promise<void> { async runToSummon(species?: Species[]) {
return new Promise(async(resolve, reject) => { await this.runToTitle();
await this.runToTitle().catch((e) => reject(e));
this.onNextPrompt("TitlePhase", Mode.TITLE, () => { this.onNextPrompt("TitlePhase", Mode.TITLE, () => {
this.scene.gameMode = getGameMode(GameModes.CLASSIC); this.scene.gameMode = getGameMode(GameModes.CLASSIC);
const starters = generateStarter(this.scene, species); const starters = generateStarter(this.scene, species);
@ -132,82 +131,81 @@ export default class GameManager {
this.scene.pushPhase(new EncounterPhase(this.scene, false)); this.scene.pushPhase(new EncounterPhase(this.scene, false));
selectStarterPhase.initBattle(starters); selectStarterPhase.initBattle(starters);
}); });
await this.phaseInterceptor.run(EncounterPhase).catch((e) => reject(e));
resolve(); await this.phaseInterceptor.run(EncounterPhase);
});
} }
/** /**
* Starts a battle. * Transitions to the start of a battle.
* @param species - Optional array of species to start the battle with. * @param species - Optional array of species to start the battle with.
* @returns A promise that resolves when the battle is started. * @returns A promise that resolves when the battle is started.
*/ */
startBattle(species?: Species[]): Promise<void> { async startBattle(species?: Species[]) {
return new Promise(async(resolve, reject) => { await this.runToSummon(species);
await this.runToSummon(species).catch((e) => reject(e));
this.onNextPrompt("CheckSwitchPhase", Mode.CONFIRM, () => { this.onNextPrompt("CheckSwitchPhase", Mode.CONFIRM, () => {
this.setMode(Mode.MESSAGE); this.setMode(Mode.MESSAGE);
this.endPhase(); this.endPhase();
}, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(TurnInitPhase)); }, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(TurnInitPhase));
this.onNextPrompt("CheckSwitchPhase", Mode.CONFIRM, () => { this.onNextPrompt("CheckSwitchPhase", Mode.CONFIRM, () => {
this.setMode(Mode.MESSAGE); this.setMode(Mode.MESSAGE);
this.endPhase(); this.endPhase();
}, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(TurnInitPhase)); }, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(TurnInitPhase));
await this.phaseInterceptor.to(CommandPhase).catch((e) => reject(e));
await this.phaseInterceptor.to(CommandPhase);
console.log("==================[New Turn]=================="); console.log("==================[New Turn]==================");
return resolve();
});
} }
doAttack(moveIndex: integer): Promise<void> { /**
* Emulate a player attack
* @param movePosition the index of the move in the pokemon's moveset array
*/
doAttack(movePosition: integer) {
this.onNextPrompt("CommandPhase", Mode.COMMAND, () => { this.onNextPrompt("CommandPhase", Mode.COMMAND, () => {
this.scene.ui.setMode(Mode.FIGHT, (this.scene.getCurrentPhase() as CommandPhase).getFieldIndex()); this.scene.ui.setMode(Mode.FIGHT, (this.scene.getCurrentPhase() as CommandPhase).getFieldIndex());
}); });
this.onNextPrompt("CommandPhase", Mode.FIGHT, () => { this.onNextPrompt("CommandPhase", Mode.FIGHT, () => {
(this.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, moveIndex, false); (this.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, movePosition, false);
}); });
return this.phaseInterceptor.to(DamagePhase);
} }
doKillOpponents() { /** Faint all opponents currently on the field */
return new Promise<void>(async(resolve, reject) => { async doKillOpponents() {
await this.killPokemon(this.scene.currentBattle.enemyParty[0]).catch((e) => reject(e)); await this.killPokemon(this.scene.currentBattle.enemyParty[0]);
if (this.scene.currentBattle.double) { if (this.scene.currentBattle.double) {
await this.killPokemon(this.scene.currentBattle.enemyParty[1]).catch((e) => reject(e)); await this.killPokemon(this.scene.currentBattle.enemyParty[1]);
} }
return resolve();
});
} }
toNextTurn(): Promise<void> { /** Emulate selecting a modifier (item) */
return new Promise<void>(async(resolve, reject) => { doSelectModifier() {
await this.phaseInterceptor.to(CommandPhase).catch((e) => reject(e));
return resolve();
});
}
toNextWave(): Promise<void> {
return new Promise<void>(async(resolve, reject) => {
this.onNextPrompt("SelectModifierPhase", Mode.MODIFIER_SELECT, () => { this.onNextPrompt("SelectModifierPhase", Mode.MODIFIER_SELECT, () => {
const handler = this.scene.ui.getHandler() as ModifierSelectUiHandler; const handler = this.scene.ui.getHandler() as ModifierSelectUiHandler;
handler.processInput(Button.CANCEL); handler.processInput(Button.CANCEL);
}, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(NewBattlePhase), true); }, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(NewBattlePhase), true);
this.onNextPrompt("SelectModifierPhase", Mode.CONFIRM, () => { this.onNextPrompt("SelectModifierPhase", Mode.CONFIRM, () => {
const handler = this.scene.ui.getHandler() as ModifierSelectUiHandler; const handler = this.scene.ui.getHandler() as ModifierSelectUiHandler;
handler.processInput(Button.ACTION); handler.processInput(Button.ACTION);
}, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(NewBattlePhase)); }, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(NewBattlePhase));
this.onNextPrompt("CheckSwitchPhase", Mode.CONFIRM, () => { }
this.setMode(Mode.MESSAGE);
this.endPhase();
}, () => this.isCurrentPhase(TurnInitPhase));
this.onNextPrompt("CheckSwitchPhase", Mode.CONFIRM, () => {
this.setMode(Mode.MESSAGE);
this.endPhase();
}, () => this.isCurrentPhase(TurnInitPhase));
await this.phaseInterceptor.to(CommandPhase).catch((e) => reject(e));
return resolve(); /** Transition to the next upcoming {@linkcode CommandPhase} */
}); async toNextTurn() {
await this.phaseInterceptor.to(CommandPhase);
}
/** Emulate selecting a modifier (item) and transition to the next upcoming {@linkcode CommandPhase} */
async toNextWave() {
this.doSelectModifier();
this.onNextPrompt("CheckSwitchPhase", Mode.CONFIRM, () => {
this.setMode(Mode.MESSAGE);
this.endPhase();
}, () => this.isCurrentPhase(TurnInitPhase));
await this.toNextTurn();
} }
/** /**

View File

@ -8,6 +8,7 @@ export default class MockText {
private textureManager; private textureManager;
public list = []; public list = [];
public style; public style;
constructor(textureManager, x, y, content, styleOptions) { constructor(textureManager, x, y, content, styleOptions) {
this.scene = textureManager.scene; this.scene = textureManager.scene;
this.textureManager = textureManager; this.textureManager = textureManager;
@ -138,6 +139,15 @@ export default class MockText {
// return this.phaserText.setX(x); // return this.phaserText.setX(x);
} }
/**
* Sets the position of this Game Object.
* @param x The x position of this Game Object. Default 0.
* @param y The y position of this Game Object. If not set it will use the `x` value. Default x.
* @param z The z position of this Game Object. Default 0.
* @param w The w position of this Game Object. Default 0.
*/
setPosition(x?: number, y?: number, z?: number, w?: number) { }
setText(text) { setText(text) {
// Sets the text this Game Object will display. // Sets the text this Game Object will display.
// return this.phaserText.setText(text); // return this.phaserText.setText(text);