deckhearth/test/lib/pricing/geo-cache.test.js

32 lines
1,013 B
JavaScript
Raw Normal View History

import { describe, expect, it } from 'vitest';
import {
STORES_CACHE_TTL_MS,
buildStoresCacheKey,
isStoresCacheFresh,
} from '../../../lib/pricing/geo-cache.js';
describe('geo-cache', () => {
it('builds stable cache keys for the same coordinates', () => {
const a = buildStoresCacheKey(40.12345, -74.98765, 16093);
const b = buildStoresCacheKey(40.12345, -74.98765, 16093);
expect(a).toBe(b);
expect(a.length).toBeLessThanOrEqual(64);
});
it('treats fresh cache entries as valid within TTL', () => {
const fetchedAt = new Date(Date.now() - STORES_CACHE_TTL_MS + 1000);
expect(isStoresCacheFresh(fetchedAt)).toBe(true);
});
it('expires cache entries after TTL', () => {
const fetchedAt = new Date(Date.now() - STORES_CACHE_TTL_MS - 1);
expect(isStoresCacheFresh(fetchedAt)).toBe(false);
});
it('rejects invalid fetched_at values', () => {
expect(isStoresCacheFresh(null)).toBe(false);
expect(isStoresCacheFresh('not-a-date')).toBe(false);
});
});