65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test';
|
|
import { __pricesInternals, getPriceHistory, getQuote } from './prices';
|
|
|
|
describe('price caching', () => {
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
beforeEach(() => {
|
|
__pricesInternals.resetCaches();
|
|
});
|
|
|
|
afterEach(() => {
|
|
globalThis.fetch = originalFetch;
|
|
__pricesInternals.resetCaches();
|
|
});
|
|
|
|
it('reuses the cached quote within the ttl window', async () => {
|
|
const fetchMock = mock(async () => Response.json({
|
|
chart: {
|
|
result: [
|
|
{
|
|
meta: {
|
|
regularMarketPrice: 123.45
|
|
}
|
|
}
|
|
]
|
|
}
|
|
})) as unknown as typeof fetch;
|
|
globalThis.fetch = fetchMock;
|
|
|
|
const first = await getQuote('MSFT');
|
|
const second = await getQuote('MSFT');
|
|
|
|
expect(first).toBe(123.45);
|
|
expect(second).toBe(123.45);
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('reuses cached price history within the ttl window', async () => {
|
|
const fetchMock = mock(async () => Response.json({
|
|
chart: {
|
|
result: [
|
|
{
|
|
timestamp: [1735689600, 1736294400],
|
|
indicators: {
|
|
quote: [
|
|
{
|
|
close: [100, 105]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
})) as unknown as typeof fetch;
|
|
globalThis.fetch = fetchMock;
|
|
|
|
const first = await getPriceHistory('MSFT');
|
|
const second = await getPriceHistory('MSFT');
|
|
|
|
expect(first).toHaveLength(2);
|
|
expect(second).toEqual(first);
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|