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

@@ -0,0 +1,204 @@
// Component tests for ScopeAccessControl.vue (Phase 36 backstop #7 + tier controls).
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { enableAutoUnmount, mount } from '@vue/test-utils'
import type { AdminAPITokenScope } from '../../src/api/types'
import { setBundle } from '../../src/app/i18n'
import ScopeAccessControl from '../../src/components/scopes/ScopeAccessControl.vue'
import { langFixture } from '../fixtures/typed'
import { resetState } from '../helpers'
const strings = {
'backend::lang.api_tokens.access_level': { other: 'Access level' },
'backend::lang.api_tokens.tier_full': { other: 'Full access' },
'backend::lang.api_tokens.tier_read_only': { other: 'Read-only' },
'backend::lang.api_tokens.tier_custom': { other: 'Custom' },
'backend::lang.api_tokens.read_only': { other: 'Read-only' },
'backend::lang.api_tokens.scope_customize': { other: 'Customize scopes…' },
'backend::lang.api_tokens.scope_summary_empty': { other: 'No scopes selected yet. Choose at least one scope to continue.' },
'backend::lang.api_tokens.scope_more': { other: '+:count more' },
'backend::lang.api_tokens.scope_count': { one: ':count scope', other: ':count scopes' },
'backend::lang.api_tokens.scope_write_count': { one: ':count write scope', other: ':count write scopes' },
'backend::lang.api_tokens.scope_plugin_count': { one: ':count plugin', other: ':count plugins' },
'backend::lang.api_tokens.scope_summary_line': { other: ':scopes across :plugins' },
'backend::lang.api_tokens.scope_summary_admin_read_plus': { other: 'Read-only admin + :scopes across :plugins' },
}
function makeScope(overrides: Partial<AdminAPITokenScope> & Pick<AdminAPITokenScope, 'name'>): AdminAPITokenScope {
return {
label: overrides.name,
description: `Description for ${overrides.name}`,
read_only: overrides.name.endsWith(':read'),
known: true,
plugin_id: 'acme.demo',
group: 'Demo',
...overrides,
}
}
const STAR = makeScope({ name: '*', label: 'Full access', read_only: false, plugin_id: '', group: '' })
const ADMIN_READ = makeScope({ name: 'admin:read', label: 'Read-only admin', read_only: true, plugin_id: '', group: '' })
beforeEach(() => {
resetState()
setBundle({ ...langFixture.data, ...strings }, 'en')
})
afterEach(() => {
document.body.innerHTML = ''
})
enableAutoUnmount(afterEach)
describe('ScopeAccessControl.vue', () => {
it('renders 3-tier RadioGroupRoot, omits missing tiers, and hides RadioGroupRoot when <= 1 tier', () => {
const blog = makeScope({ name: 'blog', label: 'Blog', read_only: false, plugin_id: 'acme.blog', group: 'Blog' })
const allThree = mount(ScopeAccessControl, {
props: {
catalog: [STAR, ADMIN_READ, blog],
availableTiers: ['full', 'read_only', 'custom'],
tier: 'read_only',
custom: { adminRead: false, scopes: new Set<string>() },
idPrefix: 'test-3',
},
attachTo: document.body,
})
expect(allThree.findAll('[data-access-tier]').map((el) => el.attributes('data-access-tier'))).toEqual([
'full',
'read_only',
'custom',
])
const twoTiers = mount(ScopeAccessControl, {
props: {
catalog: [STAR, ADMIN_READ],
availableTiers: ['full', 'read_only'],
tier: 'read_only',
custom: { adminRead: false, scopes: new Set<string>() },
idPrefix: 'test-2',
},
attachTo: document.body,
})
expect(twoTiers.findAll('[data-access-tier]').map((el) => el.attributes('data-access-tier'))).toEqual([
'full',
'read_only',
])
const oneTier = mount(ScopeAccessControl, {
props: {
catalog: [STAR],
availableTiers: ['full'],
tier: 'full',
custom: { adminRead: false, scopes: new Set<string>() },
idPrefix: 'test-1',
},
attachTo: document.body,
})
expect(oneTier.find('[data-access-tiers]').exists()).toBe(false)
expect(oneTier.find('[data-scope-all-note]').exists()).toBe(true)
})
it('renders elevated-first chips capped at 6 + clickable +2 more chip that emits customize (backstop #7, D-06)', async () => {
const catalog: AdminAPITokenScope[] = [
STAR,
ADMIN_READ,
makeScope({ name: 'p1:read', label: 'P1 Read', read_only: true, plugin_id: 'acme.p1', group: 'P1' }),
makeScope({ name: 'p2:read', label: 'P2 Read', read_only: true, plugin_id: 'acme.p2', group: 'P2' }),
makeScope({ name: 'p3:write', label: 'P3 Write', read_only: false, plugin_id: 'acme.p3', group: 'P3' }),
makeScope({ name: 'p4:write', label: 'P4 Write', read_only: false, plugin_id: 'acme.p4', group: 'P4' }),
makeScope({ name: 'p5:read', label: 'P5 Read', read_only: true, plugin_id: 'acme.p5', group: 'P5' }),
makeScope({ name: 'p6:write', label: 'P6 Write', read_only: false, plugin_id: 'acme.p6', group: 'P6' }),
makeScope({ name: 'p7:read', label: 'P7 Read', read_only: true, plugin_id: 'acme.p7', group: 'P7' }),
makeScope({ name: 'p8:read', label: 'P8 Read', read_only: true, plugin_id: 'acme.p8', group: 'P8' }),
]
// 8 selected plugin scopes (3 writes + 5 reads) -> writes sorted before reads, capped at 6 + "+2 more"
const wrapper = mount(ScopeAccessControl, {
props: {
catalog,
availableTiers: ['full', 'read_only', 'custom'],
tier: 'custom',
custom: {
adminRead: false,
scopes: new Set(['p1:read', 'p2:read', 'p3:write', 'p4:write', 'p5:read', 'p6:write', 'p7:read', 'p8:read']),
},
idPrefix: 'cap-test',
},
attachTo: document.body,
})
const chips = wrapper.findAll('[data-summary-chip]')
expect(chips).toHaveLength(6)
expect(chips.map((c) => c.attributes('data-summary-chip'))).toEqual([
'p3:write',
'p4:write',
'p6:write',
'p1:read',
'p2:read',
'p5:read',
])
const moreBtn = wrapper.find('[data-action="customize-scopes-more"]')
expect(moreBtn.exists()).toBe(true)
expect(moreBtn.text()).toBe('+2 more')
await moreBtn.trigger('click')
expect(wrapper.emitted('customize')).toHaveLength(1)
// With admin:read ON + 7 writes -> admin:read chip renders FIRST, capped at 6 + "+2 more"
const writesCatalog: AdminAPITokenScope[] = [
STAR,
ADMIN_READ,
...Array.from({ length: 7 }, (_, i) =>
makeScope({
name: `w${i + 1}`,
label: `Write ${i + 1}`,
read_only: false,
plugin_id: `acme.w${i + 1}`,
group: `W${i + 1}`,
}),
),
]
const adminReadFirst = mount(ScopeAccessControl, {
props: {
catalog: writesCatalog,
availableTiers: ['full', 'read_only', 'custom'],
tier: 'custom',
custom: {
adminRead: true,
scopes: new Set(['w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7']),
},
idPrefix: 'admin-read-first',
},
attachTo: document.body,
})
const arChips = adminReadFirst.findAll('[data-summary-chip]')
expect(arChips).toHaveLength(6)
expect(arChips[0]!.attributes('data-summary-chip')).toBe('admin:read')
expect(adminReadFirst.find('[data-action="customize-scopes-more"]').text()).toBe('+2 more')
})
it('renders empty state, invalid styling, and disabled controls accurately', async () => {
const blog = makeScope({ name: 'blog', label: 'Blog', read_only: false, plugin_id: 'acme.blog', group: 'Blog' })
const wrapper = mount(ScopeAccessControl, {
props: {
catalog: [STAR, ADMIN_READ, blog],
availableTiers: ['full', 'read_only', 'custom'],
tier: 'custom',
custom: { adminRead: false, scopes: new Set<string>() },
invalid: true,
disabled: true,
idPrefix: 'state-test',
},
attachTo: document.body,
})
const summary = wrapper.find('[data-scope-summary]')
expect(summary.attributes('aria-invalid')).toBe('true')
expect(summary.classes()).toContain('border-danger')
expect(wrapper.find('[data-scope-summary-count]').text()).toBe(
'No scopes selected yet. Choose at least one scope to continue.',
)
expect(wrapper.find('[data-scope-summary-chips]').exists()).toBe(false)
expect((wrapper.find('[data-action="customize-scopes"]').element as HTMLButtonElement).disabled).toBe(true)
})
})