test: update tests for new pricing service
All checks were successful
CI / build (push) Successful in 13s

This commit is contained in:
2026-05-02 15:59:24 -04:00
parent 148b2e9c6f
commit 537c3fe06e

View File

@@ -1,22 +1,21 @@
import { describe, expect, test, jest, mock, beforeEach } from "bun:test";
import { ItemPriceData, fetchPrice, fetchKeyPrice } from '../src/content/priceService'
import { PricesResponse, priceUsingPricesTF } from '../src/content/pricing/pricestf'
import { PricesResponse, priceUsingPricedb } from '../src/content/pricing/pricedb'
import { getStorageValue, setStorageValue } from '../src/content/storage'
import { defindex_key } from "../src/content/config";
// Mock the storage module
// Mock storage module
mock.module('../src/content/storage', () => ({
getStorageValue: jest.fn(),
setStorageValue: jest.fn()
}))
// Mock the pricing module
mock.module('../src/content/pricing/pricestf', () => ({
priceUsingPricesTF: jest.fn()
// Mock pricing module
mock.module('../src/content/pricing/pricedb', () => ({
priceUsingPricedb: jest.fn()
}))
describe('Price Service', () => {
const mockToken = 'test-token'
const mockDefIndex = 105 // Brigade Helm
const mockQuality = 11 // Strange
const mockSku = `${mockDefIndex};${mockQuality}`
@@ -49,7 +48,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(mockDefIndex + ";" + mockQuality)
expect(getStorageValue).toHaveBeenCalledWith(expect.stringContaining(mockSku), null)
expect(result).toEqual(mockCachedData)
@@ -58,34 +57,30 @@ describe('Price Service', () => {
test('fetchPrice fetches new data when cache is expired', async () => {
const expiredCache: ItemPriceData = { ...mockCachedData, update: new Date(Date.now() - 2 * mockTtl).getTime() };
(getStorageValue as jest.Mock).mockResolvedValue(expiredCache);
(priceUsingPricesTF as jest.Mock).mockResolvedValue(mockPriceResponse);
(priceUsingPricedb as jest.Mock).mockResolvedValue(mockPriceResponse);
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockPriceResponse);
const result = await fetchPrice(mockToken, mockDefIndex + ";" + mockQuality)
const result = await fetchPrice(mockDefIndex + ";" + mockQuality)
expect(setStorageValue).toHaveBeenCalled()
expect(result.metal).not.toBe(mockCachedData.metal)
expect(result.metal).toBe(mockPriceResponse.metal)
})
test('fetchPrice rejects with 401 when no token provided', async () => {
await expect(fetchPrice('', mockDefIndex + ";" + mockQuality)).rejects.toThrow()
})
test('fetchPrice handles pricing API errors', async () => {
(chrome.runtime.sendMessage as jest.Fn).mockResolvedValue(null);
(priceUsingPricesTF as jest.Mock).mockImplementation(() => Promise.reject(new Error('500 Internal Server Error')));
(priceUsingPricedb as jest.Mock).mockImplementation(() => Promise.reject(new Error('500 Internal Server Error')));
(getStorageValue as jest.Mock).mockResolvedValue(null)
await expect(fetchPrice(mockToken, mockDefIndex + ";" + mockQuality)).rejects.toThrow()
await expect(fetchPrice(mockDefIndex + ";" + mockQuality)).rejects.toThrow()
})
test('fetchKeyPrice uses correct parameters', async () => {
(getStorageValue as jest.Mock).mockResolvedValue(null);
(priceUsingPricesTF as jest.Mock).mockResolvedValue(mockKeyPriceResponse);
(priceUsingPricedb as jest.Mock).mockResolvedValue(mockKeyPriceResponse);
(chrome.runtime.sendMessage as jest.Fn).mockImplementation(() => mockKeyPriceResponse);
const result = await fetchKeyPrice(mockToken)
const result = await fetchKeyPrice()
expect(result.keys).toBe(0) // A key cannot cost a key :P
expect(result.metal).toBe(mockKeyPriceResponse.metal)