pokerogue/test/plugins/api/pokerogue-system-savedata-api.test.ts

123 lines
3.9 KiB
TypeScript
Raw 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 {
GetSystemSavedataRequest,
UpdateSystemSavedataRequest,
VerifySystemSavedataRequest,
VerifySystemSavedataResponse,
} from "#app/@types/PokerogueSystemSavedataApi";
import { PokerogueSystemSavedataApi } from "#app/plugins/api/pokerogue-system-savedata-api";
import type { SystemSaveData } from "#app/system/game-data";
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 { http, HttpResponse } from "msw";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const apiBase = getApiBaseUrl();
const systemSavedataApi = new PokerogueSystemSavedataApi(getApiBaseUrl());
const { server } = global;
afterEach(() => {
server.resetHandlers();
});
describe("Pokerogue System Savedata API", () => {
beforeEach(() => {
vi.spyOn(console, "warn");
});
describe("Get", () => {
const params: GetSystemSavedataRequest = {
clientSessionId: "test-session-id",
};
it("should return system-savedata string on SUCCESS", async () => {
server.use(http.get(`${apiBase}/savedata/system/get`, () => HttpResponse.text("TEST SYSTEM SAVEDATA")));
const savedata = await systemSavedataApi.get(params);
expect(savedata).toBe("TEST SYSTEM SAVEDATA");
});
it("should return null and report a warning on ERROR", async () => {
server.use(http.get(`${apiBase}/savedata/system/get`, () => HttpResponse.error()));
const savedata = await systemSavedataApi.get(params);
expect(savedata).toBeNull();
expect(console.warn).toHaveBeenCalledWith("Could not get system savedata!", expect.any(Error));
});
});
describe("Verify", () => {
const params: VerifySystemSavedataRequest = {
clientSessionId: "test-session-id",
};
it("should return null on SUCCESS", async () => {
server.use(
http.get(`${apiBase}/savedata/system/verify`, () =>
HttpResponse.json<VerifySystemSavedataResponse>({
systemData: {
trainerId: 123456789,
} as SystemSaveData,
valid: true,
})
)
);
const savedata = await systemSavedataApi.verify(params);
expect(savedata).toBeNull();
});
it("should return system-savedata and report a warning on FAILURE", async () => {
server.use(
http.get(`${apiBase}/savedata/system/verify`, () =>
HttpResponse.json<VerifySystemSavedataResponse>({
systemData: {
trainerId: 123456789,
} as SystemSaveData,
valid: false,
})
)
);
const savedata = await systemSavedataApi.verify(params);
expect(savedata?.trainerId).toBe(123456789);
expect(console.warn).toHaveBeenCalledWith("Invalid system savedata!");
});
});
describe("Update", () => {
const params: UpdateSystemSavedataRequest = {
clientSessionId: "test-session-id",
secretId: 9876543321,
trainerId: 123456789,
};
it("should return an empty string on SUCCESS", async () => {
server.use(http.post(`${apiBase}/savedata/system/update`, () => HttpResponse.text(null)));
const error = await systemSavedataApi.update(params, "UPDATED SYSTEM SAVEDATA");
expect(error).toBe("");
});
it("should return an error string on FAILURE", async () => {
server.use(http.post(`${apiBase}/savedata/system/update`, () => HttpResponse.text("Failed to update!")));
const error = await systemSavedataApi.update(params, "UPDATED SYSTEM SAVEDATA");
expect(error).toBe("Failed to update!");
});
it("should return 'Unknown Error' and report a warning on ERROR", async () => {
server.use(http.post(`${apiBase}/savedata/system/update`, () => HttpResponse.error()));
const error = await systemSavedataApi.update(params, "UPDATED SYSTEM SAVEDATA");
expect(error).toBe("Unknown Error");
expect(console.warn).toHaveBeenCalledWith("Could not update system savedata!", expect.any(Error));
});
});
});