pokerogue/src/test/system/game_data.test.ts
flx-sta 7a0c88e661
[Refactor] Pokerogue API client (#4583)
* start migrating Utils.apiFetch to api class

* move dailyranking to api

* use api in title-ui-handler

* remove: Utils.apiFetch

* migrate `updateSystemSavedata` to api

* migrate clear session savedata to api

* migrate updateAllSavedata to api

* migrate `updateSessionSavedata` to api

* rename `api` to `pokerogue-api`

* migrate unlink discord to pokerogue-api

* migrate unlink google to pokerogue-api

* update pokerogue-api login

* migrate register account to pokerogue-api

* remove Utils.apiPost

* reset overrides.ts

* chore: cleanup

* fix env.development

* fix circular dependencies with api

* fix gamedata verify missing await

* fix daily api calls in daily-run-scorebard

* fix discord-link request body being empty

there was a double `toUrlSearchParams()` call involved

* add pokerogue-api test coverge

* add test-utils `getApiBaseUrl()` method

* add pokerogue-admin-api test coverage

* add pokerogue-account-api test coverage

* add pokerogue-daily-api test coverage

* add pokerogue-savedata-api test coverage

* fix some test describes

* add pokerogue-session-savedata-api test coverage

* add pokerogue-system-savedata-api test coverage

* fix tests

* fix tryExportData

thanks @MokaStitcher

* chore: fix menu-ui-handlers.ts

* fix admin-ui-handler (types)

* extend test-coverage for admin-api

* remove outdated code

* skip some clowning-around-encounter tests if events are active

this is not a permanent solution

* Update src/system/game-data.ts

Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>

* Revert "skip some clowning-around-encounter tests if events are active"

This reverts commit a97dafe8b2.

* mark `localServerUrl` and `apiUrl` as deprecated

in `utils.ts`

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
2024-11-04 15:57:21 -05:00

77 lines
2.6 KiB
TypeScript

import * as BattleScene from "#app/battle-scene";
import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
import { SessionSaveData } from "#app/system/game-data";
import { Abilities } from "#enums/abilities";
import { Moves } from "#enums/moves";
import GameManager from "#test/utils/gameManager";
import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import * as account from "../../account";
describe("System - Game Data", () => {
let phaserGame: Phaser.Game;
let game: GameManager;
beforeAll(() => {
phaserGame = new Phaser.Game({
type: Phaser.HEADLESS,
});
});
beforeEach(() => {
game = new GameManager(phaserGame);
game.override
.moveset([ Moves.SPLASH ])
.battleType("single")
.enemyAbility(Abilities.BALL_FETCH)
.enemyMoveset(Moves.SPLASH);
});
afterEach(() => {
game.phaseInterceptor.restoreOg();
});
describe("tryClearSession", () => {
beforeEach(() => {
vi.spyOn(BattleScene, "bypassLogin", "get").mockReturnValue(false);
vi.spyOn(game.scene.gameData, "getSessionSaveData").mockReturnValue({} as SessionSaveData);
vi.spyOn(account, "updateUserInfo").mockImplementation(async () => [ true, 1 ]);
});
it("should return [true, true] if bypassLogin is true", async () => {
vi.spyOn(BattleScene, "bypassLogin", "get").mockReturnValue(true);
const result = await game.scene.gameData.tryClearSession(game.scene, 0);
expect(result).toEqual([ true, true ]);
});
it("should return [true, true] if successful", async () => {
vi.spyOn(pokerogueApi.savedata.session, "clear").mockResolvedValue({ success: true });
const result = await game.scene.gameData.tryClearSession(game.scene, 0);
expect(result).toEqual([ true, true ]);
expect(account.updateUserInfo).toHaveBeenCalled();
});
it("should return [true, false] if not successful", async () => {
vi.spyOn(pokerogueApi.savedata.session, "clear").mockResolvedValue({ success: false });
const result = await game.scene.gameData.tryClearSession(game.scene, 0);
expect(result).toEqual([ true, false ]);
expect(account.updateUserInfo).toHaveBeenCalled();
});
it("should return [false, false] session is out of date", async () => {
vi.spyOn(pokerogueApi.savedata.session, "clear").mockResolvedValue({ error: "session out of date" });
const result = await game.scene.gameData.tryClearSession(game.scene, 0);
expect(result).toEqual([ false, false ]);
expect(account.updateUserInfo).toHaveBeenCalled();
});
});
});