fix(36): guard stale scope catalog responses in ApiTokenCreateDialog

This commit is contained in:
Jakub Zych
2026-11-24 23:45:36 +01:00
parent 0b7e139d18
commit 2f2c33e68e
6 changed files with 129 additions and 11 deletions

View File

@@ -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')
})
})