mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
Merge pull request #7839 from ouchihao/codex/bug-scheduled-results-race
fix(admin): keep scheduled test results scoped to the expanded plan
This commit is contained in:
2 files changed
+100
-3
No files matched your search
@@ -463,7 +463,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, watch } from 'vue'
|
||||
import { ref, reactive, watch, onBeforeUnmount } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import BaseDialog from '@/components/common/BaseDialog.vue'
|
||||
import ConfirmDialog from '@/components/common/ConfirmDialog.vue'
|
||||
@@ -497,6 +497,13 @@ const loadingResults = ref(false)
|
||||
const plans = ref<ScheduledTestPlan[]>([])
|
||||
const results = ref<ScheduledTestResult[]>([])
|
||||
const expandedPlanId = ref<number | null>(null)
|
||||
let resultsRequestId = 0
|
||||
watch(expandedPlanId, () => {
|
||||
resultsRequestId++
|
||||
results.value = []
|
||||
loadingResults.value = false
|
||||
}, { flush: 'sync' })
|
||||
onBeforeUnmount(() => { resultsRequestId++ })
|
||||
const expandedResultIds = reactive(new Set<number>())
|
||||
const showAddForm = ref(false)
|
||||
const showDeleteConfirm = ref(false)
|
||||
@@ -664,13 +671,17 @@ const toggleExpand = async (planId: number) => {
|
||||
expandedPlanId.value = planId
|
||||
expandedResultIds.clear()
|
||||
loadingResults.value = true
|
||||
const requestId = resultsRequestId
|
||||
try {
|
||||
results.value = await adminAPI.scheduledTests.listResults(planId, 20)
|
||||
const data = await adminAPI.scheduledTests.listResults(planId, 20)
|
||||
if (requestId !== resultsRequestId) return
|
||||
results.value = data
|
||||
} catch (error: any) {
|
||||
if (requestId !== resultsRequestId) return
|
||||
appStore.showError(error?.message || 'Failed to load results')
|
||||
results.value = []
|
||||
} finally {
|
||||
loadingResults.value = false
|
||||
if (requestId === resultsRequestId) loadingResults.value = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 ScheduledTestsPanel from '../ScheduledTestsPanel.vue'
|
||||
const { listByAccount, listResults, showError } = vi.hoisted(() => ({
|
||||
listByAccount: vi.fn(), listResults: vi.fn(), showError: vi.fn(),
|
||||
}))
|
||||
vi.mock('@/api/admin', () => ({ adminAPI: { scheduledTests: { listByAccount, listResults } } }))
|
||||
vi.mock('@/stores/app', () => ({ useAppStore: () => ({ showError }) }))
|
||||
|
||||
async function openPanel() {
|
||||
const wrapper = shallowMount(ScheduledTestsPanel, {
|
||||
props: { show: false, accountId: 1, modelOptions: [] },
|
||||
global: { stubs: { BaseDialog: { template: '<div><slot /></div>' } } },
|
||||
})
|
||||
await wrapper.setProps({ show: true })
|
||||
await flushPromises()
|
||||
return wrapper
|
||||
}
|
||||
const result = (id: number) => ({ id, latency_ms: id, status: 'success', started_at: '2026-10-03T00:00:00Z' })
|
||||
|
||||
describe('scheduled test result selection', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks()
|
||||
listByAccount.mockResolvedValue([1, 2].map(id => ({ id, model_id: `model-${id}`, enabled: true })))
|
||||
})
|
||||
|
||||
it.each(['resolve', 'reject'] as const)('ignores an old plan request that later %ss', async outcome => {
|
||||
const old = deferred<ReturnType<typeof result>[]>()
|
||||
listResults.mockReturnValueOnce(old.promise).mockResolvedValueOnce([result(22)])
|
||||
const wrapper = await openPanel()
|
||||
const headers = wrapper.findAll('.cursor-pointer')
|
||||
await headers[0].trigger('click')
|
||||
await headers[1].trigger('click')
|
||||
await flushPromises()
|
||||
const current = wrapper.html()
|
||||
if (outcome === 'resolve') old.resolve([result(11)])
|
||||
else old.reject(new Error('obsolete error'))
|
||||
await flushPromises()
|
||||
expect(wrapper.html()).toBe(current)
|
||||
expect(showError).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps loading the current plan when the previous request completes', async () => {
|
||||
const old = deferred<ReturnType<typeof result>[]>()
|
||||
const current = deferred<ReturnType<typeof result>[]>()
|
||||
listResults.mockReturnValueOnce(old.promise).mockReturnValueOnce(current.promise)
|
||||
const wrapper = await openPanel()
|
||||
await wrapper.findAll('.cursor-pointer')[0].trigger('click')
|
||||
await wrapper.findAll('.cursor-pointer')[1].trigger('click')
|
||||
old.resolve([result(11)])
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('common.loading')
|
||||
current.resolve([result(22)])
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).not.toContain('common.loading')
|
||||
})
|
||||
|
||||
it('invalidates a request when the same plan is collapsed and reopened', async () => {
|
||||
const old = deferred<ReturnType<typeof result>[]>()
|
||||
listResults.mockReturnValueOnce(old.promise).mockResolvedValueOnce([result(22)])
|
||||
const wrapper = await openPanel()
|
||||
const header = wrapper.findAll('.cursor-pointer')[0]
|
||||
await header.trigger('click')
|
||||
await header.trigger('click')
|
||||
await header.trigger('click')
|
||||
await flushPromises()
|
||||
const current = wrapper.html()
|
||||
old.resolve([result(11)])
|
||||
await flushPromises()
|
||||
expect(wrapper.html()).toBe(current)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user