2024-08-22 13:49:33 +00:00
|
|
|
import * as battleScene from "#app/battle-scene";
|
2024-11-04 20:57:21 +00:00
|
|
|
import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
|
2024-07-03 02:00:44 +00:00
|
|
|
import { describe, expect, it, vi } from "vitest";
|
2025-02-23 04:52:07 +00:00
|
|
|
import { initLoggedInUser, loggedInUser, updateUserInfo } from "#app/account";
|
2024-07-03 02:00:44 +00:00
|
|
|
|
|
|
|
describe("account", () => {
|
|
|
|
describe("initLoggedInUser", () => {
|
|
|
|
it("should set loggedInUser to Guest and lastSessionSlot to -1", () => {
|
|
|
|
initLoggedInUser();
|
|
|
|
|
2024-08-07 16:23:12 +00:00
|
|
|
expect(loggedInUser!.username).toBe("Guest");
|
|
|
|
expect(loggedInUser!.lastSessionSlot).toBe(-1);
|
2024-07-03 02:00:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("updateUserInfo", () => {
|
2024-08-07 16:23:12 +00:00
|
|
|
it("should set loggedInUser! to Guest if bypassLogin is true", async () => {
|
2024-07-03 02:00:44 +00:00
|
|
|
vi.spyOn(battleScene, "bypassLogin", "get").mockReturnValue(true);
|
|
|
|
|
2024-10-04 05:08:31 +00:00
|
|
|
const [ success, status ] = await updateUserInfo();
|
2024-07-03 02:00:44 +00:00
|
|
|
|
|
|
|
expect(success).toBe(true);
|
|
|
|
expect(status).toBe(200);
|
2024-08-07 16:23:12 +00:00
|
|
|
expect(loggedInUser!.username).toBe("Guest");
|
|
|
|
expect(loggedInUser!.lastSessionSlot).toBe(-1);
|
2024-07-03 02:00:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should fetch user info from the API if bypassLogin is false", async () => {
|
|
|
|
vi.spyOn(battleScene, "bypassLogin", "get").mockReturnValue(false);
|
2024-11-04 20:57:21 +00:00
|
|
|
vi.spyOn(pokerogueApi.account, "getInfo").mockResolvedValue([
|
|
|
|
{
|
|
|
|
username: "test",
|
|
|
|
lastSessionSlot: 99,
|
|
|
|
discordId: "",
|
|
|
|
googleId: "",
|
|
|
|
hasAdminRole: false,
|
|
|
|
},
|
|
|
|
200,
|
|
|
|
]);
|
2024-07-03 02:00:44 +00:00
|
|
|
|
2024-10-04 05:08:31 +00:00
|
|
|
const [ success, status ] = await updateUserInfo();
|
2024-07-03 02:00:44 +00:00
|
|
|
|
|
|
|
expect(success).toBe(true);
|
|
|
|
expect(status).toBe(200);
|
2024-08-07 16:23:12 +00:00
|
|
|
expect(loggedInUser!.username).toBe("test");
|
|
|
|
expect(loggedInUser!.lastSessionSlot).toBe(99);
|
2024-07-03 02:00:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle resolved API errors", async () => {
|
|
|
|
vi.spyOn(battleScene, "bypassLogin", "get").mockReturnValue(false);
|
2024-11-04 20:57:21 +00:00
|
|
|
vi.spyOn(pokerogueApi.account, "getInfo").mockResolvedValue([ null, 401 ]);
|
2024-07-03 02:00:44 +00:00
|
|
|
|
2024-10-04 05:08:31 +00:00
|
|
|
const [ success, status ] = await updateUserInfo();
|
2024-07-03 02:00:44 +00:00
|
|
|
|
|
|
|
expect(success).toBe(false);
|
|
|
|
expect(status).toBe(401);
|
|
|
|
});
|
|
|
|
|
2024-11-04 20:57:21 +00:00
|
|
|
it("should handle 500 API errors", async () => {
|
2024-07-03 02:00:44 +00:00
|
|
|
vi.spyOn(battleScene, "bypassLogin", "get").mockReturnValue(false);
|
2024-11-04 20:57:21 +00:00
|
|
|
vi.spyOn(pokerogueApi.account, "getInfo").mockResolvedValue([ null, 500 ]);
|
2024-07-03 02:00:44 +00:00
|
|
|
|
2024-10-04 05:08:31 +00:00
|
|
|
const [ success, status ] = await updateUserInfo();
|
2024-07-03 02:00:44 +00:00
|
|
|
|
|
|
|
expect(success).toBe(false);
|
|
|
|
expect(status).toBe(500);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|