test(36-04): add Vitest unit and component tests covering all 7 scope picker backstops

This commit is contained in:
Jakub Zych
2026-11-24 20:08:24 +01:00
parent 782f4dc19d
commit 2661e0f363
5 changed files with 1072 additions and 0 deletions

View File

@@ -353,6 +353,129 @@ describe('ApiTokenCreateDialog step 1', () => {
})
})
describe('ApiTokenCreateDialog Phase 36 scope picker backstops (#1, #3, #5)', () => {
const pluginScopes = [
...scopes,
{
name: 'journal',
label: 'Journal',
description: 'Create and edit posts.',
read_only: false,
known: true,
plugin_id: 'acme.journal',
group: 'Journal',
},
{
name: 'journal:read',
label: 'Journal, read-only',
description: 'Read posts.',
read_only: true,
known: true,
plugin_id: 'acme.journal',
group: 'Journal',
},
{
name: 'seo',
label: 'SEO',
description: 'Manage SEO metadata.',
read_only: false,
known: true,
plugin_id: 'acme.seo',
group: 'SEO',
},
]
it('falls back to Full (*) when admin:read is absent from catalog (backstop #1, D-08)', async () => {
const withoutAdminRead = pluginScopes.filter((s) => s.name !== 'admin:read')
await openCreate({ [`GET ${SCOPES}`]: envelope(withoutAdminRead) })
expect(dialog()!.querySelector('[data-access-tier="read_only"]')).toBeNull()
expect(dialog()!.querySelector('[data-access-tier="full"]')!.getAttribute('data-state')).toBe('checked')
})
it('swaps view in place with zero second overlay, keeps max-w-[560px], preserves name/expires on Escape, and suppresses mod+s in picker view (backstop #5, D-02, D-13, T-36-06)', async () => {
const { calls } = await openCreate({ [`GET ${SCOPES}`]: envelope(pluginScopes) })
// Fill name and select 365d expiry before opening ScopePicker.
await fillName('Editorial bot')
const seg365 = [...dialog()!.querySelectorAll<HTMLElement>('[data-expiry-segment]')].find((el) =>
el.textContent?.includes('365 dni'),
)!
seg365.click()
await flushPromises()
expect(dialog()!.textContent).toContain(`Wygasa ${localDatePlus(365)}.`)
// Clicking Custom when empty auto-opens ScopePicker in place inside the same dialog.
await click('[data-access-tier="custom"]', dialog()!)
expect(dialog()!.querySelector('[data-scope-picker]')).toBeTruthy()
expect(document.body.querySelectorAll('[data-dialog-overlay]')).toHaveLength(1)
expect(dialog()!.className).toContain('max-w-[560px]')
// mod+s / mod+enter submit button hotkey is unmounted while in ScopePicker view.
expect(dialog()!.querySelector('[data-hotkey]')).toBeNull()
expect(dialog()!.querySelector('[data-action="create-token-submit"]')).toBeNull()
dialog()!.dispatchEvent(new KeyboardEvent('keydown', { key: 's', ctrlKey: true, bubbles: true }))
await flushPromises()
expect(requestsTo(calls, 'POST', TOKENS)).toHaveLength(0)
// Tick a scope in draft, then press Escape: discards draft, reverts auto-opened Custom back to Read-only,
// stays inside the open dialog (does not close it), and preserves name and 365d expiry.
const journalBox = dialog()!.querySelector<HTMLInputElement>('[data-scope="journal"] input[type="checkbox"]')!
journalBox.checked = true
journalBox.dispatchEvent(new Event('change', { bubbles: true }))
await flushPromises()
keydown(dialog()!, 'Escape')
await flushPromises()
expect(dialog()).toBeTruthy()
expect(dialog()!.querySelector('[data-scope-picker]')).toBeNull()
expect(dialog()!.querySelector('[data-access-tier="read_only"]')!.getAttribute('data-state')).toBe('checked')
expect(dialog()!.querySelector<HTMLInputElement>('#api-token-name')!.value).toBe('Editorial bot')
expect(dialog()!.textContent).toContain(`Wygasa ${localDatePlus(365)}.`)
})
it('remembers applied Custom selection across tier switches and submits exact scopes without admin:read unless opted in (backstop #3, D-09, D-11)', async () => {
const { calls } = await openCreate({ [`GET ${SCOPES}`]: envelope(pluginScopes) })
await fillName('Journal writer')
// Auto-open empty Custom, pick journal, apply.
await click('[data-access-tier="custom"]', dialog()!)
const journalBox = dialog()!.querySelector<HTMLInputElement>('[data-scope="journal"] input[type="checkbox"]')!
journalBox.checked = true
journalBox.dispatchEvent(new Event('change', { bubbles: true }))
await flushPromises()
await click('[data-action="scope-picker-apply"]', dialog()!)
expect(dialog()!.querySelector('[data-scope-picker]')).toBeNull()
expect(dialog()!.querySelector('[data-access-tier="custom"]')!.getAttribute('data-state')).toBe('checked')
expect(dialog()!.querySelector('[data-summary-chip="journal"]')).toBeTruthy()
expect(dialog()!.querySelector('[data-summary-chip="admin:read"]')).toBeNull()
// Switch to Read-only and back to Custom -> restores saved selection without reopening ScopePicker.
await click('[data-access-tier="read_only"]', dialog()!)
expect(dialog()!.querySelector('[data-scope-picker]')).toBeNull()
await click('[data-access-tier="custom"]', dialog()!)
expect(dialog()!.querySelector('[data-scope-picker]')).toBeNull()
expect(dialog()!.querySelector('[data-summary-chip="journal"]')).toBeTruthy()
// Customize scopes button opens in-place swap with zero second overlay; Cancel returns without losing saved selection.
await click('[data-action="customize-scopes"]', dialog()!)
expect(dialog()!.querySelector('[data-scope-picker]')).toBeTruthy()
expect(document.body.querySelectorAll('[data-dialog-overlay]')).toHaveLength(1)
await click('[data-action="scope-picker-discard"]', dialog()!)
expect(dialog()!.querySelector('[data-scope-picker]')).toBeNull()
expect(dialog()!.querySelector('[data-summary-chip="journal"]')).toBeTruthy()
// Submit posts ['journal'] (omitting admin:read).
await click('[data-action="create-token-submit"]', dialog()!)
const [post] = requestsTo(calls, 'POST', TOKENS)
expect(await post!.clone().json()).toEqual({
name: 'Journal writer',
expires: '90d',
scopes: ['journal'],
})
})
})
describe('ApiTokenCreateDialog step 2', () => {
it('copy success flashes for two seconds and announces politely', async () => {
vi.useFakeTimers()