add pokerogue-system-savedata-api test coverage

This commit is contained in:
flx-sta 2024-10-09 10:45:19 -07:00
parent 63e05aa760
commit db10ca72e8
2 changed files with 143 additions and 13 deletions

View File

@ -35,23 +35,22 @@ export class PokerogueSystemSavedataApi extends ApiBase {
* If not the {@linkcode SystemSaveData} is returned.
* @param params The {@linkcode VerifySystemSavedataRequest} to send
* @returns A {@linkcode SystemSaveData} if **NOT** valid, otherwise `null`.
*
* TODO: add handling for errors
*/
public async verify(params: VerifySystemSavedataRequest) {
try {
const urlSearchParams = this.toUrlSearchParams(params);
const response = await this.doGet(`/savedata/system/verify?${urlSearchParams}`);
const urlSearchParams = this.toUrlSearchParams(params);
const response = await this.doGet(`/savedata/system/verify?${urlSearchParams}`);
if (response.ok) {
const verifySavedata = (await response.json()) as VerifySystemSavedataResponse;
if (response.ok) {
const verifySavedata = (await response.json()) as VerifySystemSavedataResponse;
if (!verifySavedata.valid) {
return verifySavedata.systemData;
} else {
console.warn("Invalid system savedata!");
}
if (!verifySavedata.valid) {
console.warn("Invalid system savedata!");
return verifySavedata.systemData;
}
} catch (err) {
console.warn("Could not verify system savedata!", err);
} else {
console.warn("System savedata verification failed!", response.status, response.statusText);
}
return null;
@ -73,6 +72,6 @@ export class PokerogueSystemSavedataApi extends ApiBase {
console.warn("Could not update system savedata!", err);
}
return null;
return "Unknown Error";
}
}

View File

@ -0,0 +1,131 @@
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 "#app/test/utils/testUtils";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const apiBase = getApiBaseUrl();
const systemSavedataApi = new PokerogueSystemSavedataApi(apiBase);
const server = setupServer();
beforeAll(() => {
server.listen({ onUnhandledRequest: "error" });
});
afterAll(() => {
server.close();
});
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));
});
});
});