fix(36): guard stale scope catalog responses in ApiTokenCreateDialog
This commit is contained in:
@@ -61,6 +61,7 @@ const copyFailed = ref(false)
|
||||
const copyFlash = ref(false)
|
||||
const announce = ref('')
|
||||
let copyTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let loadSeq = 0
|
||||
|
||||
const nameInput = ref<HTMLInputElement | null>(null)
|
||||
const plaintextInput = ref<HTMLInputElement | null>(null)
|
||||
@@ -139,12 +140,16 @@ function setOpen(open: boolean): void {
|
||||
}
|
||||
|
||||
async function loadScopes(): Promise<void> {
|
||||
const seq = ++loadSeq
|
||||
scopesLoading.value = true
|
||||
scopesFailed.value = false
|
||||
scopes.value = []
|
||||
resetScopeSelection([])
|
||||
try {
|
||||
const result = await api.GET('/account/tokens/scopes')
|
||||
if (seq !== loadSeq || !props.open) {
|
||||
return
|
||||
}
|
||||
if (result.data) {
|
||||
const list = result.data.data ?? []
|
||||
scopes.value = list
|
||||
@@ -154,9 +159,14 @@ async function loadScopes(): Promise<void> {
|
||||
}
|
||||
scopesFailed.value = true
|
||||
} catch {
|
||||
if (seq !== loadSeq || !props.open) {
|
||||
return
|
||||
}
|
||||
scopesFailed.value = true
|
||||
} finally {
|
||||
scopesLoading.value = false
|
||||
if (seq === loadSeq) {
|
||||
scopesLoading.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,6 +177,10 @@ watch(
|
||||
resetForm()
|
||||
void loadScopes()
|
||||
} else {
|
||||
loadSeq += 1
|
||||
scopesLoading.value = false
|
||||
scopesFailed.value = false
|
||||
scopes.value = []
|
||||
clearPlaintext()
|
||||
resetScopeSelection([])
|
||||
step.value = 1
|
||||
@@ -392,6 +406,7 @@ function onHide(): void {
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
loadSeq += 1
|
||||
clearPlaintext()
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -595,4 +595,107 @@ describe('ApiTokenCreateDialog mounted directly', () => {
|
||||
await click('[data-action="hide-token"]', dialog()!)
|
||||
expect(wrapper.emitted('update:open')).toEqual([[false]])
|
||||
})
|
||||
|
||||
it('ignores stale scope catalog responses after rapid close/reopen without resetting Custom picker (WR-01)', async () => {
|
||||
const first = deferred()
|
||||
const second = deferred()
|
||||
let callCount = 0
|
||||
mockApi(
|
||||
tokenRoutes({
|
||||
[`GET ${SCOPES}`]: () => {
|
||||
callCount += 1
|
||||
return callCount === 1 ? first.route() : second.route()
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
const wrapper = mount(ApiTokenCreateDialog, {
|
||||
props: { open: false, confirm: useConfirm() },
|
||||
attachTo: document.body,
|
||||
})
|
||||
|
||||
// First open kicks off request #1 (stays pending), close cancels it, reopen starts request #2.
|
||||
await wrapper.setProps({ open: true })
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ open: false })
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ open: true })
|
||||
await flushPromises()
|
||||
expect(callCount).toBe(2)
|
||||
expect(dialog()!.querySelector('[data-scope-loading]')).toBeTruthy()
|
||||
|
||||
// Request #2 resolves first with plugin scopes; user switches to Custom and ticks journal in ScopePicker.
|
||||
second.release(
|
||||
envelope([
|
||||
...scopes,
|
||||
{
|
||||
name: 'journal',
|
||||
label: 'Journal',
|
||||
description: 'Create and edit posts.',
|
||||
read_only: false,
|
||||
known: true,
|
||||
plugin_id: 'acme.journal',
|
||||
group: 'Journal',
|
||||
},
|
||||
]),
|
||||
)
|
||||
await flushPromises()
|
||||
expect(dialog()!.querySelector('[data-scope-loading]')).toBeNull()
|
||||
|
||||
await click('[data-access-tier="custom"]', dialog()!)
|
||||
expect(dialog()!.querySelector('[data-scope-picker]')).toBeTruthy()
|
||||
const journalBox = dialog()!.querySelector<HTMLInputElement>('[data-scope="journal"] input[type="checkbox"]')!
|
||||
journalBox.checked = true
|
||||
journalBox.dispatchEvent(new Event('change', { bubbles: true }))
|
||||
await flushPromises()
|
||||
|
||||
// Stale request #1 now settles late — must NOT close ScopePicker or reset the draft/tier.
|
||||
first.release(envelope(scopes))
|
||||
await flushPromises()
|
||||
expect(dialog()!.querySelector('[data-scope-picker]')).toBeTruthy()
|
||||
expect(dialog()!.querySelector<HTMLInputElement>('[data-scope="journal"] input[type="checkbox"]')!.checked).toBe(
|
||||
true,
|
||||
)
|
||||
expect(dialog()!.querySelector('[role="alert"]')).toBeNull()
|
||||
})
|
||||
|
||||
it('keeps loading skeleton active if stale first scope request settles while second is still pending (WR-01)', async () => {
|
||||
const first = deferred()
|
||||
const second = deferred()
|
||||
let callCount = 0
|
||||
mockApi(
|
||||
tokenRoutes({
|
||||
[`GET ${SCOPES}`]: () => {
|
||||
callCount += 1
|
||||
return callCount === 1 ? first.route() : second.route()
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
const wrapper = mount(ApiTokenCreateDialog, {
|
||||
props: { open: false, confirm: useConfirm() },
|
||||
attachTo: document.body,
|
||||
})
|
||||
|
||||
await wrapper.setProps({ open: true })
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ open: false })
|
||||
await flushPromises()
|
||||
await wrapper.setProps({ open: true })
|
||||
await flushPromises()
|
||||
expect(dialog()!.querySelector('[data-scope-loading]')).toBeTruthy()
|
||||
|
||||
// Stale request #1 resolves early while request #2 is still in flight -> skeleton must remain visible.
|
||||
first.release({ status: 500, body: { error: { code: 'err', message: 'boom', details: {} } } })
|
||||
await flushPromises()
|
||||
expect(dialog()!.querySelector('[data-scope-loading]')).toBeTruthy()
|
||||
expect(dialog()!.querySelector('[role="alert"]')).toBeNull()
|
||||
expect(dialog()!.querySelector<HTMLButtonElement>('[data-action="create-token-submit"]')!.disabled).toBe(true)
|
||||
|
||||
// Request #2 resolves -> skeleton clears and tiers render normally.
|
||||
second.release(envelope(scopes))
|
||||
await flushPromises()
|
||||
expect(dialog()!.querySelector('[data-scope-loading]')).toBeNull()
|
||||
expect(dialog()!.querySelector('[data-access-tier="read_only"]')!.getAttribute('data-state')).toBe('checked')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user