2024-09-26 15:55:08 -05:00
|
|
|
import * as BattleScene from "#app/battle-scene";
|
|
|
|
import { SessionSaveData } from "#app/system/game-data";
|
|
|
|
import { Abilities } from "#enums/abilities";
|
|
|
|
import { Moves } from "#enums/moves";
|
|
|
|
import GameManager from "#test/utils/gameManager";
|
|
|
|
import { http, HttpResponse } from "msw";
|
|
|
|
import { setupServer } from "msw/node";
|
|
|
|
import Phaser from "phaser";
|
|
|
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import * as account from "../../account";
|
|
|
|
|
|
|
|
const apiBase = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8001";
|
|
|
|
|
2024-10-09 13:01:49 -07:00
|
|
|
/** We need a custom server. For some reasons I can't extend the listeners of {@linkcode global.i18nServer} with {@linkcode global.i18nServer.use} */
|
|
|
|
const server = setupServer();
|
2024-09-26 15:55:08 -05:00
|
|
|
|
|
|
|
describe("System - Game Data", () => {
|
|
|
|
let phaserGame: Phaser.Game;
|
|
|
|
let game: GameManager;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
2024-10-09 13:01:49 -07:00
|
|
|
global.i18nServer.close();
|
2024-09-26 15:55:08 -05:00
|
|
|
server.listen();
|
|
|
|
phaserGame = new Phaser.Game({
|
|
|
|
type: Phaser.HEADLESS,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
server.close();
|
2024-10-09 13:01:49 -07:00
|
|
|
global.i18nServer.listen();
|
2024-09-26 15:55:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
game = new GameManager(phaserGame);
|
|
|
|
game.override
|
2024-10-04 13:08:31 +08:00
|
|
|
.moveset([ Moves.SPLASH ])
|
2024-09-26 15:55:08 -05:00
|
|
|
.battleType("single")
|
|
|
|
.enemyAbility(Abilities.BALL_FETCH)
|
|
|
|
.enemyMoveset(Moves.SPLASH);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
server.resetHandlers();
|
|
|
|
game.phaseInterceptor.restoreOg();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("tryClearSession", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
vi.spyOn(BattleScene, "bypassLogin", "get").mockReturnValue(false);
|
|
|
|
vi.spyOn(game.scene.gameData, "getSessionSaveData").mockReturnValue({} as SessionSaveData);
|
2024-10-04 13:08:31 +08:00
|
|
|
vi.spyOn(account, "updateUserInfo").mockImplementation(async () => [ true, 1 ]);
|
2024-09-26 15:55:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-10-04 13:08:31 +08:00
|
|
|
expect(result).toEqual([ true, true ]);
|
2024-09-26 15:55:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return [true, true] if successful", async () => {
|
|
|
|
server.use(http.post(`${apiBase}/savedata/session/clear`, () => HttpResponse.json({ success: true })));
|
|
|
|
|
|
|
|
const result = await game.scene.gameData.tryClearSession(game.scene, 0);
|
|
|
|
|
2024-10-04 13:08:31 +08:00
|
|
|
expect(result).toEqual([ true, true ]);
|
2024-09-26 15:55:08 -05:00
|
|
|
expect(account.updateUserInfo).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return [true, false] if not successful", async () => {
|
|
|
|
server.use(http.post(`${apiBase}/savedata/session/clear`, () => HttpResponse.json({ success: false })));
|
|
|
|
|
|
|
|
const result = await game.scene.gameData.tryClearSession(game.scene, 0);
|
|
|
|
|
2024-10-04 13:08:31 +08:00
|
|
|
expect(result).toEqual([ true, false ]);
|
2024-09-26 15:55:08 -05:00
|
|
|
expect(account.updateUserInfo).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return [false, false] session is out of date", async () => {
|
|
|
|
server.use(
|
|
|
|
http.post(`${apiBase}/savedata/session/clear`, () => HttpResponse.json({ error: "session out of date" }))
|
|
|
|
);
|
|
|
|
|
|
|
|
const result = await game.scene.gameData.tryClearSession(game.scene, 0);
|
|
|
|
|
2024-10-04 13:08:31 +08:00
|
|
|
expect(result).toEqual([ false, false ]);
|
2024-09-26 15:55:08 -05:00
|
|
|
expect(account.updateUserInfo).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|