v1.0.7 (fixes #17)

This commit is contained in:
Maurus Decimus
2026-07-30 17:16:05 +02:00
parent f18f3012aa
commit 189e270785
31 changed files with 1478 additions and 695 deletions
+54
View File
@@ -6,6 +6,7 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { useAuthStore } from './authStore';
import { useCacheStore } from './cacheStore';
const initialState = {
accessToken: null,
@@ -21,6 +22,7 @@ const initialState = {
describe('authStore', () => {
beforeEach(() => {
useAuthStore.setState(initialState);
useCacheStore.getState().clearAll();
vi.restoreAllMocks();
});
@@ -124,6 +126,29 @@ describe('authStore', () => {
expect(useAuthStore.getState().activeAccountId).toBe('acc-1');
});
it('preserves an activeAccountId that exists in the new accounts', () => {
useAuthStore.setState({ activeAccountId: 'acc-2' });
useAuthStore.getState().setSession(
{
'acc-1': { name: 'Personal', isPersonal: true },
'acc-2': { name: 'Group', isPersonal: false },
},
'acc-1',
'https://api',
);
expect(useAuthStore.getState().activeAccountId).toBe('acc-2');
});
it('falls back to primaryAccountId when the previous activeAccountId is unknown', () => {
useAuthStore.setState({ activeAccountId: 'gone' });
useAuthStore.getState().setSession({ 'acc-1': { name: 'A', isPersonal: true } }, 'acc-1', 'https://api');
expect(useAuthStore.getState().activeAccountId).toBe('acc-1');
});
});
describe('switchAccount', () => {
@@ -149,6 +174,35 @@ describe('authStore', () => {
useAuthStore.getState().switchAccount('nonexistent');
expect(useAuthStore.getState().activeAccountId).toBe('a1');
});
it('clears cached objects when the account changes', () => {
useCacheStore.getState().setDisplayNames('Mailbox', { m1: 'Inbox' });
useCacheStore.getState().setObjectList('Mailbox', [{ id: 'm1', label: 'Inbox' }]);
useAuthStore.setState({
accounts: {
a1: { name: 'A1', isPersonal: true },
a2: { name: 'A2', isPersonal: false },
},
activeAccountId: 'a1',
});
useAuthStore.getState().switchAccount('a2');
expect(useCacheStore.getState().displayNames).toEqual({});
expect(useCacheStore.getState().objectLists).toEqual({});
});
it('keeps the cache when switching to the already active account', () => {
useCacheStore.getState().setDisplayNames('Mailbox', { m1: 'Inbox' });
useAuthStore.setState({
accounts: { a1: { name: 'A1', isPersonal: true } },
activeAccountId: 'a1',
});
useAuthStore.getState().switchAccount('a1');
expect(useCacheStore.getState().getDisplayName('Mailbox', 'm1')).toBe('Inbox');
});
});
describe('logout', () => {
+7 -3
View File
@@ -6,6 +6,7 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import { useCacheStore } from '@/stores/cacheStore';
interface AccountInfo {
name: string;
@@ -71,10 +72,11 @@ export const useAuthStore = create<AuthState>()(
},
setSession: (accounts, primaryAccountId, apiUrl, maxObjectsInGet, maxObjectsInSet) => {
const current = get().activeAccountId;
set({
accounts,
primaryAccountId,
activeAccountId: primaryAccountId,
activeAccountId: current && accounts[current] ? current : primaryAccountId,
apiUrl,
...(maxObjectsInGet !== undefined ? { maxObjectsInGet } : {}),
...(maxObjectsInSet !== undefined ? { maxObjectsInSet } : {}),
@@ -82,9 +84,10 @@ export const useAuthStore = create<AuthState>()(
},
switchAccount: (accountId) => {
const { accounts } = get();
if (accounts[accountId]) {
const { accounts, activeAccountId } = get();
if (accounts[accountId] && accountId !== activeAccountId) {
set({ activeAccountId: accountId });
useCacheStore.getState().clearAll();
}
},
@@ -134,6 +137,7 @@ export const useAuthStore = create<AuthState>()(
tokenExpiresAt: state.tokenExpiresAt,
tokenEndpoint: state.tokenEndpoint,
endSessionEndpoint: state.endSessionEndpoint,
activeAccountId: state.activeAccountId,
}) as AuthState,
},
),
+5
View File
@@ -24,6 +24,7 @@ interface CacheState {
getObjectList: (key: string) => ObjectListEntry[] | undefined;
invalidateObjectList: (key: string) => void;
invalidateAllObjectLists: () => void;
clearAll: () => void;
}
export const useCacheStore = create<CacheState>()((set, get) => ({
@@ -75,4 +76,8 @@ export const useCacheStore = create<CacheState>()((set, get) => ({
invalidateAllObjectLists: () => {
set({ objectLists: {} });
},
clearAll: () => {
set({ displayNames: {}, objectLists: {} });
},
}));