From 77a831b89e3cb34623b4f298aa15569851f30800 Mon Sep 17 00:00:00 2001 From: xenticore Date: Fri, 28 Mar 2025 23:35:39 -0400 Subject: [PATCH] refactor: `fetchPrice` now expects an SKU string --- __tests__/priceService.test.ts | 8 ++++---- src/content/content.ts | 2 +- src/content/priceService.ts | 5 ++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/__tests__/priceService.test.ts b/__tests__/priceService.test.ts index 580bd12..468641f 100644 --- a/__tests__/priceService.test.ts +++ b/__tests__/priceService.test.ts @@ -49,7 +49,7 @@ describe('Price Service', () => { test('fetchPrice returns cached data if available and not expired', async () => { (getStorageValue as jest.Mock).mockResolvedValue(mockCachedData) - const result = await fetchPrice(mockToken, mockDefIndex, mockQuality) + const result = await fetchPrice(mockToken, mockDefIndex + ";" + mockQuality) expect(getStorageValue).toHaveBeenCalledWith(expect.stringContaining(mockSku), null) expect(result).toEqual(mockCachedData) @@ -60,7 +60,7 @@ describe('Price Service', () => { (getStorageValue as jest.Mock).mockResolvedValue(expiredCache); (priceUsingPricesTF as jest.Mock).mockResolvedValue(mockPriceResponse) - const result = await fetchPrice(mockToken, mockDefIndex, mockQuality) + const result = await fetchPrice(mockToken, mockDefIndex + ";" + mockQuality) expect(priceUsingPricesTF).toHaveBeenCalledWith(mockToken, `${mockDefIndex};${mockQuality}`) expect(setStorageValue).toHaveBeenCalled() @@ -69,7 +69,7 @@ describe('Price Service', () => { }) test('fetchPrice rejects with 401 when no token provided', async () => { - await expect(fetchPrice('', mockDefIndex, mockQuality)).rejects.toBe(401) + await expect(fetchPrice('', mockDefIndex + ";" + mockQuality)).rejects.toBe(401) }) test('fetchPrice handles pricing API errors', async () => { @@ -77,7 +77,7 @@ describe('Price Service', () => { (priceUsingPricesTF as jest.Mock).mockRejectedValue(testError); (getStorageValue as jest.Mock).mockResolvedValue(null) - await expect(fetchPrice(mockToken, mockDefIndex, mockQuality)).rejects.toBe(testError) + await expect(fetchPrice(mockToken, mockDefIndex + ";" + mockQuality)).rejects.toBe(testError) }) test('fetchKeyPrice uses correct parameters', async () => { diff --git a/src/content/content.ts b/src/content/content.ts index 066b717..d0eb5bf 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -206,7 +206,7 @@ async function inject() { var data: ItemPriceData | null try { - data = await fetchPrice(token, itemIndex, quality, currentTime); + data = await fetchPrice(token, itemIndex + ";" + quality, currentTime); updateTime = new Date(data.update) } catch { log(`${qualifiedName} is unpriced or unavailable, skipping...`) diff --git a/src/content/priceService.ts b/src/content/priceService.ts index 767ebb0..0fe7eaa 100644 --- a/src/content/priceService.ts +++ b/src/content/priceService.ts @@ -30,7 +30,7 @@ export class ItemPriceData { export async function fetchKeyPrice(token: string) { - return fetchPrice(token, defindex_key, 6, new Date(), 86400000) + return fetchPrice(token, `${defindex_key};6`, new Date(), 86400000) } /** @@ -39,9 +39,8 @@ export async function fetchKeyPrice(token: string) { * @param update Date retrieved. * @param ttl Time to cache results in milliseconds. 30 minutes by default. */ -export async function fetchPrice(token: string, defIndex: number, quality: number, update: Date = new Date(), ttl: number = 30 * 60 * 1000): Promise { +export async function fetchPrice(token: string, sku: string, update: Date = new Date(), ttl: number = 30 * 60 * 1000): Promise { return new Promise(async (resolve, reject) => { - const sku = defIndex.toString() + ";" + quality.toString(); var data: ItemPriceData | null const cached = await getStorageValue(storage_priceprefix + sku, null)