import { describe, it, expect, vi, beforeEach } from 'vitest' import { setActivePinia, createPinia } from 'pinia' import { useAIPermissionsStore, AI_PERMISSION_CATEGORIES } from '../aiPermissions' const STORAGE_KEY = 'archipelago-ai-permissions' describe('useAIPermissionsStore', () => { beforeEach(() => { setActivePinia(createPinia()) localStorage.clear() vi.clearAllMocks() }) it('starts with empty permissions when no localStorage', () => { const store = useAIPermissionsStore() expect(store.enabled.size).toBe(0) expect(store.noneEnabled).toBe(true) expect(store.allEnabled).toBe(false) }) it('loads valid categories from localStorage', () => { localStorage.setItem(STORAGE_KEY, JSON.stringify(['apps', 'system'])) setActivePinia(createPinia()) const store = useAIPermissionsStore() expect(store.isEnabled('apps')).toBe(true) expect(store.isEnabled('system')).toBe(true) expect(store.enabled.size).toBe(2) }) it('filters invalid categories from localStorage', () => { localStorage.setItem(STORAGE_KEY, JSON.stringify(['apps', 'invalid-category', 'system'])) setActivePinia(createPinia()) const store = useAIPermissionsStore() expect(store.enabled.size).toBe(2) expect(store.isEnabled('apps')).toBe(true) expect(store.isEnabled('system')).toBe(true) }) it('handles corrupt localStorage gracefully', () => { localStorage.setItem(STORAGE_KEY, 'not-valid-json{') setActivePinia(createPinia()) const store = useAIPermissionsStore() expect(store.enabled.size).toBe(0) }) it('toggle adds a category', () => { const store = useAIPermissionsStore() store.toggle('bitcoin') expect(store.isEnabled('bitcoin')).toBe(true) }) it('toggle removes an enabled category', () => { const store = useAIPermissionsStore() store.toggle('bitcoin') store.toggle('bitcoin') expect(store.isEnabled('bitcoin')).toBe(false) }) it('toggle persists to localStorage', () => { const store = useAIPermissionsStore() store.toggle('apps') const stored = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]') expect(stored).toContain('apps') }) it('enableAll enables all categories', () => { const store = useAIPermissionsStore() store.enableAll() expect(store.allEnabled).toBe(true) expect(store.enabled.size).toBe(AI_PERMISSION_CATEGORIES.length) for (const cat of AI_PERMISSION_CATEGORIES) { expect(store.isEnabled(cat.id)).toBe(true) } }) it('disableAll disables all categories', () => { const store = useAIPermissionsStore() store.enableAll() store.disableAll() expect(store.noneEnabled).toBe(true) expect(store.enabled.size).toBe(0) }) it('enabledCategories returns array of enabled IDs', () => { const store = useAIPermissionsStore() store.toggle('apps') store.toggle('network') expect(store.enabledCategories).toContain('apps') expect(store.enabledCategories).toContain('network') expect(store.enabledCategories.length).toBe(2) }) it('AI_PERMISSION_CATEGORIES has 10 categories', () => { expect(AI_PERMISSION_CATEGORIES.length).toBe(10) }) it('all categories have required fields', () => { for (const cat of AI_PERMISSION_CATEGORIES) { expect(cat.id).toBeTruthy() expect(cat.label).toBeTruthy() expect(cat.description).toBeTruthy() expect(cat.icon).toBeTruthy() expect(cat.group).toBeTruthy() } }) })