31 lines
992 B
JavaScript
31 lines
992 B
JavaScript
import { describe, it, expect, beforeEach } from 'vitest'
|
|
import {
|
|
cacheKey, getCachedForecast, setCachedForecast, clearForecastCache, TTL_MS,
|
|
} from '../../../src/lib/stores/forecast-cache.js'
|
|
|
|
describe('forecast cache', () => {
|
|
beforeEach(() => clearForecastCache())
|
|
|
|
it('builds a stable key from coords/source/units', () => {
|
|
expect(cacheKey({ lat: 36.35, lon: -95.81 }, 'open-meteo', 'metric'))
|
|
.toBe('36.35,-95.81|open-meteo|metric')
|
|
})
|
|
|
|
it('returns data within TTL', () => {
|
|
const now = 1_000_000
|
|
setCachedForecast('k', { hi: 1 }, now)
|
|
expect(getCachedForecast('k', now + TTL_MS - 1)).toEqual({ hi: 1 })
|
|
})
|
|
|
|
it('expires and deletes after TTL', () => {
|
|
const now = 1_000_000
|
|
setCachedForecast('k', { hi: 1 }, now)
|
|
expect(getCachedForecast('k', now + TTL_MS + 1)).toBeNull()
|
|
expect(getCachedForecast('k', now + TTL_MS + 2)).toBeNull()
|
|
})
|
|
|
|
it('misses on unknown key', () => {
|
|
expect(getCachedForecast('nope')).toBeNull()
|
|
})
|
|
})
|