import { describe, expect, it } from 'vitest' import { readTodaySectionCollapse, writeTodaySectionCollapse } from './today-section-collapse' const defaults = { overdue: false, tasks: false, habits: false } function fakeStorage(initial?: string) { const values = new Map() if (initial !== undefined) values.set('dodo.today-section-collapse.v1', initial) return { getItem: (key: string) => values.get(key) ?? null, setItem: (key: string, value: string) => values.set(key, value), value: () => values.get('dodo.today-section-collapse.v1'), } } describe('Today section collapse persistence', () => { it('defaults every section to expanded when storage is absent or malformed', () => { expect(readTodaySectionCollapse(fakeStorage(), 'dodo.today-section-collapse.v1')).toEqual(defaults) expect(readTodaySectionCollapse(fakeStorage('{bad'), 'dodo.today-section-collapse.v1')).toEqual(defaults) }) it('accepts only boolean fields and falls back invalid or missing fields independently', () => { expect(readTodaySectionCollapse(fakeStorage(JSON.stringify({ overdue: true, tasks: 'yes', extra: true })), 'dodo.today-section-collapse.v1')).toEqual({ overdue: true, tasks: false, habits: false, }) }) it('does not break collapsing when storage rejects a write', () => { const storage = { getItem: () => null, setItem: () => { throw new Error('quota exceeded') }, } expect(() => writeTodaySectionCollapse(storage, 'dodo.today-section-collapse.v1', defaults)).not.toThrow() }) it('writes all three independent section states under the single versioned key', () => { const storage = fakeStorage() writeTodaySectionCollapse(storage, 'dodo.today-section-collapse.v1', { overdue: true, tasks: false, habits: true }) expect(storage.value()).toBe(JSON.stringify({ overdue: true, tasks: false, habits: true })) }) })