import { describe, it, expect, beforeEach } from 'vitest' import { updateFromCot, getActiveEntities, clearCotStore } from '../../../server/utils/cotStore.js' describe('cotStore', () => { beforeEach(() => { clearCotStore() }) it('upserts entity by id', async () => { await updateFromCot({ id: 'uid-1', lat: 37.7, lng: -122.4, label: 'Alpha' }) const active = await getActiveEntities() expect(active).toHaveLength(1) expect(active[0].id).toBe('uid-1') expect(active[0].lat).toBe(37.7) expect(active[0].lng).toBe(-122.4) expect(active[0].label).toBe('Alpha') }) it('updates same uid', async () => { await updateFromCot({ id: 'uid-1', lat: 37.7, lng: -122.4 }) await updateFromCot({ id: 'uid-1', lat: 38, lng: -123, label: 'Updated' }) const active = await getActiveEntities() expect(active).toHaveLength(1) expect(active[0].lat).toBe(38) expect(active[0].lng).toBe(-123) expect(active[0].label).toBe('Updated') }) it('ignores invalid parsed (no id)', async () => { await updateFromCot({ lat: 37, lng: -122 }) const active = await getActiveEntities() expect(active).toHaveLength(0) }) it('ignores invalid parsed (bad coords)', async () => { await updateFromCot({ id: 'x', lat: Number.NaN, lng: -122 }) await updateFromCot({ id: 'y', lat: 37, lng: Infinity }) const active = await getActiveEntities() expect(active).toHaveLength(0) }) it('prunes expired entities after getActiveEntities', async () => { await updateFromCot({ id: 'uid-1', lat: 37, lng: -122 }) const active1 = await getActiveEntities(100) expect(active1).toHaveLength(1) await new Promise(r => setTimeout(r, 150)) const active2 = await getActiveEntities(100) expect(active2).toHaveLength(0) }) it('returns multiple active entities within TTL', async () => { await updateFromCot({ id: 'a', lat: 1, lng: 2, label: 'A' }) await updateFromCot({ id: 'b', lat: 3, lng: 4, label: 'B' }) const active = await getActiveEntities() expect(active).toHaveLength(2) expect(active.map(e => e.id).sort()).toEqual(['a', 'b']) }) })