From 9cd8a4478135b5ef85878412c3494fb1b3327dbb Mon Sep 17 00:00:00 2001 From: Marten Klitzke Date: Mon, 16 Nov 2026 10:23:09 +0100 Subject: [PATCH] feat(rsi): read only the upgrade pledges for a CCU sync --- __tests__/message-handler.test.ts | 29 +++++++++++++++++++++++++++++ __tests__/rsi.test.ts | 18 ++++++++++++++++++ lib/message-handler.ts | 14 ++++++++++++-- lib/rsi.ts | 16 ++++++++++++++-- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/__tests__/message-handler.test.ts b/__tests__/message-handler.test.ts index 91e216b..24878f8 100644 --- a/__tests__/message-handler.test.ts +++ b/__tests__/message-handler.test.ts @@ -27,6 +27,7 @@ describe("onMessage", () => { "health", "identify", "sync", + "syncUpgrades", "syncBuyback", "syncBuybackDetail", "syncBuybackPricing", @@ -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("upgrades"), + }; + 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("upgrades"); + + 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); diff --git a/__tests__/rsi.test.ts b/__tests__/rsi.test.ts index 5a3f0c7..80435ad 100644 --- a/__tests__/rsi.test.ts +++ b/__tests__/rsi.test.ts @@ -1,5 +1,6 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import { + fetchUpgradePledges, fetchRSIApi, identify, fetchPledges, @@ -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 }); diff --git a/lib/message-handler.ts b/lib/message-handler.ts index da8dfdf..d0f392c 100644 --- a/lib/message-handler.ts +++ b/lib/message-handler.ts @@ -1,6 +1,7 @@ import { identify, fetchPledges, + fetchUpgradePledges, fetchBuybacks, fetchBuybackDetail, setStoreAuthToken, @@ -37,6 +38,7 @@ export const SUPPORTED_ACTIONS = [ "health", "identify", "sync", + "syncUpgrades", "syncBuyback", "syncBuybackDetail", "syncBuybackPricing", @@ -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( @@ -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(); diff --git a/lib/rsi.ts b/lib/rsi.ts index e661d8a..b6df15d 100644 --- a/lib/rsi.ts +++ b/lib/rsi.ts @@ -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 = {} +) { + const query = new URLSearchParams({ page: String(page), ...filters }); + + return fetch(`${RSI_BASE_URL}/account/${path}?${query}`, { method: "GET", headers: { ...HTML_PAGE_HEADERS, @@ -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); }