mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
Merge pull request #7372 from ouchihao/codex/bug-admin-settings-retry
fix(settings): allow retrying failed initial admin settings loads
This commit is contained in:
2 files changed
+51
-1
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { useAdminSettingsStore } from '../adminSettings'
|
||||
|
||||
const mocks = vi.hoisted(() => ({ getSettings: vi.fn(), getConfig: vi.fn() }))
|
||||
vi.mock('@/api', () => ({ adminAPI: { settings: { getSettings: mocks.getSettings }, payment: { getConfig: mocks.getConfig } } }))
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
localStorage.clear()
|
||||
setActivePinia(createPinia())
|
||||
mocks.getSettings.mockResolvedValue({ ops_monitoring_enabled: true, custom_menu_items: [{ id: 'custom', title: 'Custom' }] })
|
||||
mocks.getConfig.mockResolvedValue({ data: { enabled: true } })
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
})
|
||||
afterEach(() => { vi.restoreAllMocks(); localStorage.clear() })
|
||||
|
||||
describe('admin settings fetch retry', () => {
|
||||
it.each(['settings', 'payment'])('retries a failed initial %s request without forcing a refresh', async (source) => {
|
||||
const request = source === 'settings' ? mocks.getSettings : mocks.getConfig
|
||||
request.mockRejectedValueOnce(new Error('Temporarily unavailable'))
|
||||
const store = useAdminSettingsStore()
|
||||
await store.fetch()
|
||||
expect(store.loaded).toBe(false)
|
||||
expect(store.loading).toBe(false)
|
||||
await store.fetch()
|
||||
expect(mocks.getSettings).toHaveBeenCalledTimes(2)
|
||||
expect(mocks.getConfig).toHaveBeenCalledTimes(2)
|
||||
expect(store.loaded).toBe(true)
|
||||
expect(store.paymentEnabled).toBe(true)
|
||||
expect(store.customMenuItems).toEqual([{ id: 'custom', title: 'Custom' }])
|
||||
})
|
||||
|
||||
it('keeps cached values visible when the initial request fails', async () => {
|
||||
localStorage.setItem('ops_monitoring_enabled_cached', 'false')
|
||||
localStorage.setItem('payment_enabled_cached', 'true')
|
||||
mocks.getSettings.mockRejectedValueOnce(new Error('Offline'))
|
||||
const store = useAdminSettingsStore()
|
||||
await store.fetch()
|
||||
expect(store.opsMonitoringEnabled).toBe(false)
|
||||
expect(store.paymentEnabled).toBe(true)
|
||||
expect(localStorage.getItem('payment_enabled_cached')).toBe('true')
|
||||
})
|
||||
|
||||
it('still reuses successfully loaded settings', async () => {
|
||||
const store = useAdminSettingsStore()
|
||||
await store.fetch()
|
||||
await store.fetch()
|
||||
expect(mocks.getSettings).toHaveBeenCalledTimes(1)
|
||||
expect(mocks.getConfig).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
@@ -78,7 +78,6 @@ export const useAdminSettingsStore = defineStore('adminSettings', () => {
|
||||
loaded.value = true
|
||||
} catch (err) {
|
||||
// Keep cached/default value: do not "flip" the UI based on a transient fetch failure.
|
||||
loaded.value = true
|
||||
console.error('[adminSettings] Failed to fetch settings:', err)
|
||||
} finally {
|
||||
loading.value = false
|
||||
|
||||
Reference in new issue
Block a user