weather_app/tests/lib/stores/forecast-cache.test.js
hermes-explorigin ae71c36acc
All checks were successful
Test, Build & Deploy / test-and-build (push) Successful in 53s
Test, Build & Deploy / deploy (push) Successful in 24s
feat(weather): 5-min forecast cache + location-change loading indicator
2026-10-17 16:49:51 +00:00

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()
})
})