pokerogue/test/plugins/api/pokerogue-daily-api.test.ts

90 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

[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 a97dafe8b2479e9b2ddd49d4dc9710814d7c7b67. * 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 12:57:21 -08:00
import type { GetDailyRankingsPageCountRequest, GetDailyRankingsRequest } from "#app/@types/PokerogueDailyApi";
import { PokerogueDailyApi } from "#app/plugins/api/pokerogue-daily-api";
import { getApiBaseUrl } from "#test/testUtils/testUtils";
[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 a97dafe8b2479e9b2ddd49d4dc9710814d7c7b67. * 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 12:57:21 -08:00
import { ScoreboardCategory, type RankingEntry } from "#app/ui/daily-run-scoreboard";
import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const apiBase = getApiBaseUrl();
const dailyApi = new PokerogueDailyApi(apiBase);
const { server } = global;
afterEach(() => {
server.resetHandlers();
});
describe("Pokerogue Daily API", () => {
beforeEach(() => {
vi.spyOn(console, "warn");
});
describe("Get Seed", () => {
it("should return seed string on SUCCESS", async () => {
server.use(http.get(`${apiBase}/daily/seed`, () => HttpResponse.text("this-is-a-test-seed")));
const seed = await dailyApi.getSeed();
expect(seed).toBe("this-is-a-test-seed");
});
it("should return null and report a warning on ERROR", async () => {
server.use(http.get(`${apiBase}/daily/seed`, () => HttpResponse.error()));
const seed = await dailyApi.getSeed();
expect(seed).toBeNull();
expect(console.warn).toHaveBeenCalledWith("Could not get daily-run seed!", expect.any(Error));
});
});
describe("Get Rankings", () => {
const params: GetDailyRankingsRequest = {
category: ScoreboardCategory.DAILY,
};
it("should return ranking entries on SUCCESS", async () => {
const expectedRankings: RankingEntry[] = [
{ rank: 1, score: 999, username: "Player 1", wave: 200 },
{ rank: 2, score: 10, username: "Player 2", wave: 1 },
];
server.use(http.get(`${apiBase}/daily/rankings`, () => HttpResponse.json(expectedRankings)));
const rankings = await dailyApi.getRankings(params);
expect(rankings).toStrictEqual(expectedRankings);
});
it("should return null and report a warning on ERROR", async () => {
server.use(http.get(`${apiBase}/daily/rankings`, () => HttpResponse.error()));
const rankings = await dailyApi.getRankings(params);
expect(rankings).toBeNull();
expect(console.warn).toHaveBeenCalledWith("Could not get daily rankings!", expect.any(Error));
});
});
describe("Get Rankings Page Count", () => {
const params: GetDailyRankingsPageCountRequest = {
category: ScoreboardCategory.DAILY,
};
it("should return a number on SUCCESS", async () => {
server.use(http.get(`${apiBase}/daily/rankingpagecount`, () => HttpResponse.json(5)));
const pageCount = await dailyApi.getRankingsPageCount(params);
expect(pageCount).toBe(5);
});
it("should return 1 and report a warning on ERROR", async () => {
server.use(http.get(`${apiBase}/daily/rankingpagecount`, () => HttpResponse.error()));
const pageCount = await dailyApi.getRankingsPageCount(params);
expect(pageCount).toBe(1);
expect(console.warn).toHaveBeenCalledWith("Could not get daily rankings page count!", expect.any(Error));
});
});
});