diff --git a/__tests__/priceService.test.ts b/__tests__/priceService.test.ts index 468641f..2ee36f9 100644 --- a/__tests__/priceService.test.ts +++ b/__tests__/priceService.test.ts @@ -94,7 +94,7 @@ describe('Price Service', () => { test('ItemPriceData.toString() returns formatted string', () => { const data = new ItemPriceData() data.sku = mockSku - data.update = mockDate + data.update = mockDate.getTime() data.ttl = mockTtl data.keys = 1 data.metal = 10.66 @@ -102,6 +102,8 @@ describe('Price Service', () => { const result = data.toString() expect(result).toContain(`Price for ${mockSku}`) + expect(result).toContain(mockDate.toString()) + expect(result).toContain(new Date(mockDate.getTime() + mockTtl).toString()) expect(result).toContain(`"keys":${data.keys}`) expect(result).toContain(`"metal":${data.metal}`) expect(result).toContain(`"scmPrice":${data.scmPrice}`) diff --git a/src/content/content.ts b/src/content/content.ts index a8ce368..c41d706 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -250,7 +250,7 @@ async function inject() { }).reverse().forEach((element) => { priceInfoboxHeadingRow.insertAdjacentElement('afterend', element.row); }) - if(!updateTime) { updateTime = new Date() } + if(!updateTime || !(updateTime instanceof Date) || isNaN(+updateTime)) updateTime = new Date() // Footer row const row = document.createElement("tr"); diff --git a/src/content/priceService.ts b/src/content/priceService.ts index 0fe7eaa..5aa675d 100644 --- a/src/content/priceService.ts +++ b/src/content/priceService.ts @@ -7,8 +7,8 @@ import { logDebug, log } from "./utils/log" export class ItemPriceData { /** Item SKU. */ sku: string - /** Date updated. */ - update: Date + /** Date updated, as Unix timestamp. */ + update: number /** TTL in milliseconds. */ ttl: number /** Price in keys. */ @@ -19,7 +19,7 @@ export class ItemPriceData { scmPrice: number toString(): string { - return `Price for ${this.sku}, fetched ${this.update} (expires ${this.update.getTime() + this.ttl})\n` + + return `Price for ${this.sku}, fetched ${new Date(this.update)} (expires ${new Date(this.update + this.ttl)})\n` + JSON.stringify({ keys: this.keys, metal: this.metal, @@ -43,8 +43,8 @@ export async function fetchPrice(token: string, sku: string, update: Date = new return new Promise(async (resolve, reject) => { var data: ItemPriceData | null - const cached = await getStorageValue(storage_priceprefix + sku, null) - if (cached != null && 'keys' in cached && 'metal' in cached) { + const cached: ItemPriceData = await getStorageValue(storage_priceprefix + sku, null) + if (cached != null && 'keys' in cached && 'metal' in cached && !isNaN(cached.update)) { data = cached } @@ -55,7 +55,7 @@ export async function fetchPrice(token: string, sku: string, update: Date = new } data = new ItemPriceData() data.sku = sku - data.update = update + data.update = update.getTime() data.ttl = ttl try {