mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
fix(ops): require loaded settings before saving
This commit is contained in:
1 parent
b8dece9000
commit
81bd85ec55
2 files changed
+91
-1
No files matched your search
@@ -21,6 +21,7 @@ const emit = defineEmits<{
|
||||
}>()
|
||||
|
||||
const loading = ref(false)
|
||||
const settingsLoaded = ref(false)
|
||||
const saving = ref(false)
|
||||
|
||||
// 运行时设置
|
||||
@@ -39,6 +40,7 @@ const metricThresholds = ref<OpsMetricThresholds>({
|
||||
|
||||
// 加载所有配置
|
||||
async function loadAllSettings() {
|
||||
settingsLoaded.value = false
|
||||
loading.value = true
|
||||
try {
|
||||
const [runtime, email, advanced, thresholds] = await Promise.all([
|
||||
@@ -63,6 +65,7 @@ async function loadAllSettings() {
|
||||
upstream_error_rate_percent_max: thresholds.upstream_error_rate_percent_max ?? 5
|
||||
}
|
||||
}
|
||||
settingsLoaded.value = true
|
||||
} catch (err: any) {
|
||||
console.error('[OpsSettingsDialog] Failed to load settings', err)
|
||||
appStore.showError(err?.response?.data?.detail || t('admin.ops.settings.loadFailed'))
|
||||
@@ -197,6 +200,7 @@ const validation = computed(() => {
|
||||
|
||||
// 保存所有配置
|
||||
async function saveAllSettings() {
|
||||
if (loading.value || !settingsLoaded.value || saving.value) return
|
||||
if (!validation.value.valid) {
|
||||
appStore.showError(validation.value.errors[0])
|
||||
return
|
||||
@@ -641,7 +645,7 @@ async function saveAllSettings() {
|
||||
<template #footer>
|
||||
<div class="flex justify-end gap-2">
|
||||
<button class="btn btn-secondary" @click="emit('close')">{{ t('common.cancel') }}</button>
|
||||
<button class="btn btn-primary" :disabled="saving || !validation.valid" @click="saveAllSettings">
|
||||
<button class="btn btn-primary" :disabled="loading || !settingsLoaded || saving || !validation.valid" @click="saveAllSettings">
|
||||
{{ saving ? t('common.saving') : t('common.save') }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { enableAutoUnmount, flushPromises, shallowMount } from '@vue/test-utils'
|
||||
|
||||
vi.mock('vue-i18n', async (importOriginal) => ({
|
||||
...await importOriginal<typeof import('vue-i18n')>(),
|
||||
useI18n: () => ({ t: (key: string) => key }),
|
||||
}))
|
||||
enableAutoUnmount(afterEach)
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void
|
||||
let reject!: (reason: unknown) => void
|
||||
const promise = new Promise<T>((res, rej) => { resolve = res; reject = rej })
|
||||
return { promise, resolve, reject }
|
||||
}
|
||||
|
||||
import OpsSettingsDialog from '../OpsSettingsDialog.vue'
|
||||
const { opsAPI, showError } = vi.hoisted(() => ({
|
||||
opsAPI: {
|
||||
getAlertRuntimeSettings: vi.fn(), getEmailNotificationConfig: vi.fn(),
|
||||
getAdvancedSettings: vi.fn(), getMetricThresholds: vi.fn(),
|
||||
updateAlertRuntimeSettings: vi.fn(), updateEmailNotificationConfig: vi.fn(),
|
||||
updateAdvancedSettings: vi.fn(), updateMetricThresholds: vi.fn(),
|
||||
}, showError: vi.fn(),
|
||||
}))
|
||||
vi.mock('@/api/admin/ops', () => ({ opsAPI }))
|
||||
vi.mock('@/stores/app', () => ({ useAppStore: () => ({ showError, showSuccess: vi.fn() }) }))
|
||||
async function openDialog() {
|
||||
const wrapper = shallowMount(OpsSettingsDialog, {
|
||||
props: { show: false },
|
||||
global: { stubs: { BaseDialog: { template: '<div><slot name="footer" /></div>' } } },
|
||||
})
|
||||
await wrapper.setProps({ show: true })
|
||||
return wrapper
|
||||
}
|
||||
type View = { saveAllSettings: () => Promise<void> }
|
||||
describe('ops settings load guard', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks()
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
opsAPI.getAlertRuntimeSettings.mockResolvedValue({ evaluation_interval_seconds: 30 })
|
||||
opsAPI.getEmailNotificationConfig.mockResolvedValue({ alert: { enabled: false }, report: { enabled: false } })
|
||||
opsAPI.getAdvancedSettings.mockResolvedValue({ data_retention: { error_log_retention_days: 7, minute_metrics_retention_days: 7, hourly_metrics_retention_days: 7 } })
|
||||
opsAPI.getMetricThresholds.mockResolvedValue({ sla_percent_min: 98 })
|
||||
})
|
||||
afterEach(() => { vi.restoreAllMocks() })
|
||||
it('blocks saving while any settings request is still loading', async () => {
|
||||
const pending = deferred<object>()
|
||||
opsAPI.getMetricThresholds.mockReturnValueOnce(pending.promise)
|
||||
const wrapper = await openDialog()
|
||||
expect(wrapper.get('button.btn-primary').attributes('disabled')).toBeDefined()
|
||||
await (wrapper.vm as unknown as View).saveAllSettings()
|
||||
expect(opsAPI.updateMetricThresholds).not.toHaveBeenCalled()
|
||||
pending.resolve({ sla_percent_min: 98 })
|
||||
await flushPromises()
|
||||
expect(wrapper.get('button.btn-primary').attributes('disabled')).toBeUndefined()
|
||||
await wrapper.get('button.btn-primary').trigger('click')
|
||||
await flushPromises()
|
||||
expect(opsAPI.updateMetricThresholds).toHaveBeenCalledWith(expect.objectContaining({ sla_percent_min: 98 }))
|
||||
expect(wrapper.emitted('saved')).toHaveLength(1)
|
||||
})
|
||||
it('does not save default thresholds after a failed initial load', async () => {
|
||||
opsAPI.getMetricThresholds.mockRejectedValueOnce(new Error('load failed'))
|
||||
const wrapper = await openDialog()
|
||||
await flushPromises()
|
||||
expect(wrapper.get('button.btn-primary').attributes('disabled')).toBeDefined()
|
||||
await (wrapper.vm as unknown as View).saveAllSettings()
|
||||
expect(opsAPI.updateMetricThresholds).not.toHaveBeenCalled()
|
||||
expect(wrapper.emitted('saved')).toBeUndefined()
|
||||
})
|
||||
it('blocks saving stale settings after a failed reopen and allows a later retry', async () => {
|
||||
const wrapper = await openDialog()
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ show: false })
|
||||
opsAPI.getMetricThresholds.mockRejectedValueOnce(new Error('refresh failed'))
|
||||
await wrapper.setProps({ show: true })
|
||||
await flushPromises()
|
||||
expect(wrapper.get('button.btn-primary').attributes('disabled')).toBeDefined()
|
||||
await (wrapper.vm as unknown as View).saveAllSettings()
|
||||
expect(opsAPI.updateAlertRuntimeSettings).not.toHaveBeenCalled()
|
||||
await wrapper.setProps({ show: false })
|
||||
await wrapper.setProps({ show: true })
|
||||
await flushPromises()
|
||||
expect(wrapper.get('button.btn-primary').attributes('disabled')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user