import { describe, it, expect, beforeEach } from 'vitest' import { getLocations, addLocation, updateLocation, deleteLocation, setCurrentLocation, clearAllLocations, loadSettings, saveSettings, DEFAULT_SETTINGS, } from '../../../src/lib/storage/db.js' describe('db - locations', () => { beforeEach(async () => { await clearAllLocations() }) it('starts with empty locations', async () => { const locs = await getLocations() expect(locs).toEqual([]) }) it('adds and retrieves a location', async () => { const id = await addLocation({ name: 'Berlin', lat: 52.52, lon: 13.41, isCurrent: false, }) expect(id).toBeTruthy() const locs = await getLocations() expect(locs).toHaveLength(1) expect(locs[0].name).toBe('Berlin') expect(locs[0].lat).toBe(52.52) expect(locs[0].lon).toBe(13.41) expect(locs[0].isCurrent).toBe(false) expect(locs[0].order).toBe(1) }) it('adds multiple locations with incrementing order', async () => { await addLocation({ name: 'Berlin', lat: 52.52, lon: 13.41, isCurrent: false }) await addLocation({ name: 'Paris', lat: 48.85, lon: 2.35, isCurrent: false }) await addLocation({ name: 'Tokyo', lat: 35.68, lon: 139.76, isCurrent: false }) const locs = await getLocations() expect(locs).toHaveLength(3) expect(locs[0].order).toBe(1) expect(locs[1].order).toBe(2) expect(locs[2].order).toBe(3) }) it('respects custom order', async () => { await addLocation({ name: 'First', lat: 1, lon: 1, isCurrent: false, order: 10 }) await addLocation({ name: 'Second', lat: 2, lon: 2, isCurrent: false }) const locs = await getLocations() expect(locs[0].name).toBe('First') expect(locs[1].name).toBe('Second') }) it('updates a location', async () => { const id = await addLocation({ name: 'Berlin', lat: 52.52, lon: 13.41, isCurrent: false }) await updateLocation({ id, name: 'Berlin Updated', lat: 52.52, lon: 13.41, isCurrent: true, order: 5 }) const locs = await getLocations() expect(locs[0].name).toBe('Berlin Updated') expect(locs[0].isCurrent).toBe(true) expect(locs[0].order).toBe(5) }) it('throws when updating non-existent location', async () => { await expect( updateLocation({ id: 'nonexistent', name: 'Nope', lat: 0, lon: 0, isCurrent: false }) ).rejects.toThrow('Location not found') }) it('deletes a location', async () => { const id = await addLocation({ name: 'Berlin', lat: 52.52, lon: 13.41, isCurrent: false }) await deleteLocation(id) const locs = await getLocations() expect(locs).toHaveLength(0) }) it('setCurrentLocation updates the current flag', async () => { const id1 = await addLocation({ name: 'Berlin', lat: 52.52, lon: 13.41, isCurrent: true }) const id2 = await addLocation({ name: 'Paris', lat: 48.85, lon: 2.35, isCurrent: false }) await setCurrentLocation(id2) const locs = await getLocations() const berlin = locs.find((l) => l.id === id1) const paris = locs.find((l) => l.id === id2) expect(berlin.isCurrent).toBe(false) expect(paris.isCurrent).toBe(true) }) it('clearAllLocations removes all', async () => { await addLocation({ name: 'Berlin', lat: 52.52, lon: 13.41, isCurrent: false }) await addLocation({ name: 'Paris', lat: 48.85, lon: 2.35, isCurrent: false }) await clearAllLocations() const locs = await getLocations() expect(locs).toHaveLength(0) }) }) describe('db - settings', () => { it('loads default settings when none saved', async () => { const settings = await loadSettings() expect(settings.units).toBe('metric') expect(settings.alertsEnabled).toBe(true) expect(settings.alertThresholds.precip).toBe(70) }) it('saves and loads settings', async () => { await saveSettings({ units: 'imperial', alertsEnabled: false, alertThresholds: { precip: 80, windGust: 50, uvIndex: 8, tempHigh: 40, tempLow: -5, thunderstorm: false }, }) const settings = await loadSettings() expect(settings.units).toBe('imperial') expect(settings.alertsEnabled).toBe(false) expect(settings.alertThresholds.precip).toBe(80) expect(settings.alertThresholds.thunderstorm).toBe(false) }) it('merges partial settings with defaults', async () => { await saveSettings({ units: 'imperial' }) const settings = await loadSettings() expect(settings.units).toBe('imperial') expect(settings.alertsEnabled).toBe(true) // default preserved }) })