Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions __tests__/message-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("onMessage", () => {
"health",
"identify",
"sync",
"syncUpgrades",
"syncBuyback",
"syncBuybackDetail",
"syncBuybackPricing",
Expand Down Expand Up @@ -100,6 +101,34 @@ describe("onMessage", () => {
);
});

it("responds to syncUpgrades action with the upgrade pledges", async () => {
const sendResponse = vi.fn();
const getToken = vi.fn().mockResolvedValue("test-token");

const mockResponse = {
status: 200,
text: vi.fn().mockResolvedValue("<html>upgrades</html>"),
};
vi.spyOn(globalThis, "fetch").mockResolvedValue(mockResponse as any);

await onMessage(
JSON.stringify({ action: "syncUpgrades", page: 2 }),
sendResponse,
getToken,
"1.0.0"
);

const result = JSON.parse(sendResponse.mock.calls[0]![0]);
expect(result.code).toBe(200);
expect(result.action).toBe("syncUpgrades");
expect(result.payload).toBe("<html>upgrades</html>");

expect(globalThis.fetch).toHaveBeenCalledWith(
"https://robertsspaceindustries.com/account/pledges?page=2&product-type=upgrade",
expect.objectContaining({ method: "GET" })
);
});

it("responds to sync action without token", async () => {
const sendResponse = vi.fn();
const getToken = vi.fn().mockResolvedValue(null);
Expand Down
18 changes: 18 additions & 0 deletions __tests__/rsi.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import {
fetchUpgradePledges,
fetchRSIApi,
identify,
fetchPledges,
Expand Down Expand Up @@ -96,6 +97,23 @@ describe("fetchPledges", () => {
});
});

describe("fetchUpgradePledges", () => {
it("fetches only the upgrade pledges", async () => {
const mockResponse = new Response("html", { status: 200 });
vi.spyOn(globalThis, "fetch").mockResolvedValue(mockResponse);

await fetchUpgradePledges("test-token", 3);

expect(globalThis.fetch).toHaveBeenCalledWith(
"https://robertsspaceindustries.com/account/pledges?page=3&product-type=upgrade",
expect.objectContaining({
method: "GET",
headers: expect.objectContaining({ "X-Rsi-Token": "test-token" }),
})
);
});
});

describe("fetchBuybacks", () => {
it("fetches buybacks with default page 1", async () => {
const mockResponse = new Response("html", { status: 200 });
Expand Down
14 changes: 12 additions & 2 deletions lib/message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
identify,
fetchPledges,
fetchUpgradePledges,
fetchBuybacks,
fetchBuybackDetail,
setStoreAuthToken,
Expand Down Expand Up @@ -37,6 +38,7 @@ export const SUPPORTED_ACTIONS = [
"health",
"identify",
"sync",
"syncUpgrades",
"syncBuyback",
"syncBuybackDetail",
"syncBuybackPricing",
Expand Down Expand Up @@ -493,7 +495,11 @@ export async function onMessage(

sendResponse(JSON.stringify({ action: message.action, ...result }));
}
} else if (message?.action == "sync" || message?.action == "syncBuyback") {
} else if (
message?.action == "sync" ||
message?.action == "syncUpgrades" ||
message?.action == "syncBuyback"
) {
const token = await getToken();
if (!token) {
sendResponse(
Expand All @@ -505,7 +511,11 @@ export async function onMessage(
);
} else {
const fetchPage =
message.action == "syncBuyback" ? fetchBuybacks : fetchPledges;
message.action == "syncBuyback"
? fetchBuybacks
: message.action == "syncUpgrades"
? fetchUpgradePledges
: fetchPledges;
const response = await fetchPage(token, message.page);
const payload = await response.text();

Expand Down
16 changes: 14 additions & 2 deletions lib/rsi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ const HTML_PAGE_HEADERS = {
"Cache-Control": "max-age=0",
};

function fetchAccountPage(path: string, token: string, page: number) {
return fetch(`${RSI_BASE_URL}/account/${path}?page=${page}`, {
function fetchAccountPage(
path: string,
token: string,
page: number,
filters: Record<string, string> = {}
) {
const query = new URLSearchParams({ page: String(page), ...filters });

return fetch(`${RSI_BASE_URL}/account/${path}?${query}`, {
method: "GET",
headers: {
...HTML_PAGE_HEADERS,
Expand All @@ -52,6 +59,11 @@ export function fetchPledges(token: string, page = 1) {
return fetchAccountPage("pledges", token, page);
}

// Only the upgrades, which is all a CCU sync needs and a fraction of the pages.
export function fetchUpgradePledges(token: string, page = 1) {
return fetchAccountPage("pledges", token, page, { "product-type": "upgrade" });
}

export function fetchBuybacks(token: string, page = 1) {
return fetchAccountPage("buy-back-pledges", token, page);
}
Expand Down
Loading