Files
summercms/admin/tests/app/scopeCatalog.test.ts

281 lines
12 KiB
TypeScript

// Pure unit tests for admin/src/app/scopeCatalog.ts (Phase 36 backstops #1, #2, #4, #7).
import { describe, expect, it } from 'vitest'
import type { AdminAPITokenScope } from '../../src/api/types'
import {
buildSummaryModel,
computeDefaultExpanded,
computeSubmittedScopes,
groupPluginScopes,
isEffectivelySelected,
matchesScopeQuery,
partitionCatalog,
resolveInitialState,
} from '../../src/app/scopeCatalog'
function scope(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 = scope({ name: '*', label: 'Full access', read_only: false, plugin_id: '', group: '' })
const ADMIN_READ = scope({ name: 'admin:read', label: 'Read-only, whole admin', read_only: true, plugin_id: '', group: '' })
describe('partitionCatalog', () => {
it('splits *, admin:read, and plugin scopes preserving catalog order', () => {
const blog = scope({ name: 'blog', plugin_id: 'acme.blog', group: 'Blog' })
const blogRead = scope({ name: 'blog:read', plugin_id: 'acme.blog', group: 'Blog' })
const seo = scope({ name: 'seo', plugin_id: 'acme.seo', group: 'SEO' })
const result = partitionCatalog([STAR, ADMIN_READ, blog, blogRead, seo])
expect(result.fullScope).toEqual(STAR)
expect(result.adminReadScope).toEqual(ADMIN_READ)
expect(result.pluginScopes).toEqual([blog, blogRead, seo])
})
it('handles missing framework scopes cleanly', () => {
const blog = scope({ name: 'blog', plugin_id: 'acme.blog', group: 'Blog' })
const result = partitionCatalog([blog])
expect(result.fullScope).toBeNull()
expect(result.adminReadScope).toBeNull()
expect(result.pluginScopes).toEqual([blog])
})
})
describe('groupPluginScopes', () => {
it('keys groups by (plugin_id, group) to prevent collisions and tie-breaks by plugin_id (D-01, D-17)', () => {
const zetaWrite = scope({ name: 'zeta:write', plugin_id: 'zeta.blog', group: 'Blog' })
const zetaRead = scope({ name: 'zeta:read', plugin_id: 'zeta.blog', group: 'Blog' })
const alphaWrite = scope({ name: 'alpha:write', plugin_id: 'alpha.blog', group: 'Blog' })
const alphaRead = scope({ name: 'alpha:read', plugin_id: 'alpha.blog', group: 'Blog' })
const groups = groupPluginScopes([zetaWrite, zetaRead, alphaWrite, alphaRead], 'en', 'Other')
expect(groups).toHaveLength(2)
expect(groups.map((g) => g.key)).toEqual(['alpha.blog\0Blog', 'zeta.blog\0Blog'])
expect(groups.map((g) => g.label)).toEqual(['Blog', 'Blog'])
// Intra-group declared scope order is preserved verbatim.
expect(groups[0]!.scopes.map((s) => s.name)).toEqual(['alpha:write', 'alpha:read'])
expect(groups[1]!.scopes.map((s) => s.name)).toEqual(['zeta:write', 'zeta:read'])
})
it('sorts groups alphabetically using locale collation (pl vs en) and applies fallback label', () => {
const a = scope({ name: 's1', plugin_id: 'acme.l1', group: 'Lampa' })
const b = scope({ name: 's2', plugin_id: 'acme.l2', group: 'Łódź' })
const c = scope({ name: 's3', plugin_id: 'acme.l3', group: 'Mapa' })
const fallback = scope({ name: 's4', plugin_id: '', group: '' })
const plGroups = groupPluginScopes([c, b, a], 'pl', 'Inne')
expect(plGroups.map((g) => g.label)).toEqual(['Lampa', 'Łódź', 'Mapa'])
const withFallback = groupPluginScopes([fallback], 'en', 'Other')
expect(withFallback[0]!.label).toBe('Other')
})
})
describe('matchesScopeQuery', () => {
const s = scope({
name: 'journal:write',
label: 'Publish articles',
description: 'Create and update blog entries.',
plugin_id: 'golem15.journal',
group: 'Editorial',
})
it('matches case-insensitively across name, label, description, plugin_id, and groupLabel', () => {
expect(matchesScopeQuery(s, 'Editorial', '')).toBe(true)
expect(matchesScopeQuery(s, 'Editorial', ' ')).toBe(true)
expect(matchesScopeQuery(s, 'Editorial', 'JOURNAL:WRITE')).toBe(true)
expect(matchesScopeQuery(s, 'Editorial', 'publish')).toBe(true)
expect(matchesScopeQuery(s, 'Editorial', 'blog entries')).toBe(true)
expect(matchesScopeQuery(s, 'Editorial', 'golem15.journal')).toBe(true)
expect(matchesScopeQuery(s, 'Editorial', 'editor')).toBe(true)
expect(matchesScopeQuery(s, 'Editorial', 'sitemap')).toBe(false)
})
})
describe('isEffectivelySelected and computeDefaultExpanded', () => {
function makeLargeGroups(groupCount: number, scopesPerGroup: number) {
const items: AdminAPITokenScope[] = []
for (let g = 1; g <= groupCount; g++) {
for (let i = 1; i <= scopesPerGroup; i++) {
items.push(
scope({
name: `p${g}:s${i}`,
read_only: i % 2 === 0,
plugin_id: `acme.p${g}`,
group: `Plugin ${g}`,
}),
)
}
}
return { items, groups: groupPluginScopes(items, 'en', 'Other') }
}
it('marks read_only scopes effectively selected when admin:read is ON', () => {
const rw = scope({ name: 'blog', read_only: false })
const ro = scope({ name: 'blog:read', read_only: true })
expect(isEffectivelySelected(ro, { adminRead: true, scopes: new Set() })).toBe(true)
expect(isEffectivelySelected(rw, { adminRead: true, scopes: new Set() })).toBe(false)
expect(isEffectivelySelected(rw, { adminRead: false, scopes: new Set(['blog']) })).toBe(true)
})
it('expands all groups for small catalogs (<= 3 groups or <= 12 scopes) (D-07)', () => {
// 3 groups x 5 scopes = 15 scopes, <= 3 groups -> expand all
const threeGroups = makeLargeGroups(3, 5)
expect(
computeDefaultExpanded(threeGroups.groups, threeGroups.items.length, { adminRead: false, scopes: new Set() }, false),
).toEqual(new Set(threeGroups.groups.map((g) => g.key)))
// 4 groups x 3 scopes = 12 scopes -> <= 12 scopes -> expand all
const twelveScopes = makeLargeGroups(4, 3)
expect(
computeDefaultExpanded(twelveScopes.groups, twelveScopes.items.length, { adminRead: false, scopes: new Set() }, false),
).toEqual(new Set(twelveScopes.groups.map((g) => g.key)))
})
it('expands only selected groups for large catalogs (> 3 groups and > 12 scopes), or all when searching (D-07)', () => {
// 4 groups x 4 scopes = 16 scopes (> 3 groups AND > 12 scopes)
const large = makeLargeGroups(4, 4)
// None selected -> none expanded
expect(
computeDefaultExpanded(large.groups, large.items.length, { adminRead: false, scopes: new Set() }, false),
).toEqual(new Set())
// One scope in Plugin 2 selected -> only Plugin 2 expanded
expect(
computeDefaultExpanded(large.groups, large.items.length, { adminRead: false, scopes: new Set(['p2:s1']) }, false),
).toEqual(new Set(['acme.p2\0Plugin 2']))
// Searching -> all matching groups expanded regardless of selection
expect(
computeDefaultExpanded(large.groups, large.items.length, { adminRead: false, scopes: new Set() }, true),
).toEqual(new Set(large.groups.map((g) => g.key)))
})
})
describe('buildSummaryModel', () => {
it('orders admin:read first, then write scopes, then read-only scopes, and caps at 6 + overflow (D-06)', () => {
const catalog: AdminAPITokenScope[] = [
STAR,
ADMIN_READ,
scope({ name: 'a:read', label: 'A Read', read_only: true, plugin_id: 'acme.a', group: 'A' }),
scope({ name: 'b:read', label: 'B Read', read_only: true, plugin_id: 'acme.b', group: 'B' }),
scope({ name: 'c:write', label: 'C Write', read_only: false, plugin_id: 'acme.c', group: 'C' }),
scope({ name: 'd:write', label: 'D Write', read_only: false, plugin_id: 'acme.d', group: 'D' }),
scope({ name: 'e:read', label: 'E Read', read_only: true, plugin_id: 'acme.e', group: 'E' }),
scope({ name: 'f:write', label: 'F Write', read_only: false, plugin_id: 'acme.f', group: 'F' }),
scope({ name: 'g:read', label: 'G Read', read_only: true, plugin_id: 'acme.g', group: 'G' }),
scope({ name: 'h:read', label: 'H Read', read_only: true, plugin_id: 'acme.h', group: 'H' }),
]
// Without admin:read: 3 writes first, then 5 reads = 8 total -> 6 visible + overflow 2
const withoutAdminRead = buildSummaryModel(catalog, {
adminRead: false,
scopes: new Set(['a:read', 'b:read', 'c:write', 'd:write', 'e:read', 'f:write', 'g:read', 'h:read']),
})
expect(withoutAdminRead.chips.map((c) => c.name)).toEqual([
'c:write',
'd:write',
'f:write',
'a:read',
'b:read',
'e:read',
'g:read',
'h:read',
])
expect(withoutAdminRead.visible).toHaveLength(6)
expect(withoutAdminRead.overflowCount).toBe(2)
expect(withoutAdminRead.selectedWriteCount).toBe(3)
expect(withoutAdminRead.selectedPluginCount).toBe(8)
expect(withoutAdminRead.distinctPluginCount).toBe(8)
// With admin:read ON: admin:read first, then writes only (redundant :read scopes omitted from chips)
const withAdminRead = buildSummaryModel(catalog, {
adminRead: true,
scopes: new Set(['a:read', 'c:write', 'd:write', 'f:write']),
})
expect(withAdminRead.chips.map((c) => [c.name, c.kind])).toEqual([
['admin:read', 'admin_read'],
['c:write', 'write'],
['d:write', 'write'],
['f:write', 'write'],
])
expect(withAdminRead.selectedWriteCount).toBe(3)
expect(withAdminRead.selectedPluginCount).toBe(3)
expect(withAdminRead.distinctPluginCount).toBe(3)
})
})
describe('computeSubmittedScopes', () => {
const catalog = [
STAR,
ADMIN_READ,
scope({ name: 'blog', read_only: false, plugin_id: 'acme.blog', group: 'Blog' }),
scope({ name: 'blog:read', read_only: true, plugin_id: 'acme.blog', group: 'Blog' }),
scope({ name: 'seo', read_only: false, plugin_id: 'acme.seo', group: 'SEO' }),
]
it('omits admin:read in custom when adminRead is false (T-36-04)', () => {
expect(
computeSubmittedScopes('custom', catalog, { adminRead: false, scopes: new Set(['blog', 'blog:read']) }),
).toEqual(['blog', 'blog:read'])
})
it('emits ["admin:read", ...writes] and strips redundant :read codes when adminRead is true (D-04)', () => {
expect(
computeSubmittedScopes('custom', catalog, { adminRead: true, scopes: new Set(['blog', 'blog:read', 'seo']) }),
).toEqual(['admin:read', 'blog', 'seo'])
})
it('emits ["*"] for full and ["admin:read"] for read_only', () => {
expect(computeSubmittedScopes('full', catalog, { adminRead: false, scopes: new Set() })).toEqual(['*'])
expect(computeSubmittedScopes('read_only', catalog, { adminRead: false, scopes: new Set() })).toEqual(['admin:read'])
})
})
describe('resolveInitialState', () => {
const blog = scope({ name: 'blog', read_only: false, plugin_id: 'acme.blog', group: 'Blog' })
const seo = scope({ name: 'seo', read_only: false, plugin_id: 'acme.seo', group: 'SEO' })
it('resolves ConnectView defaults (D-03): Read-only when grantable, Custom for narrow subset, Full for *-only', () => {
expect(resolveInitialState([STAR, ADMIN_READ, blog], 'connect')).toEqual({
tier: 'read_only',
custom: { adminRead: false, scopes: new Set() },
})
expect(resolveInitialState([blog, seo], 'connect')).toEqual({
tier: 'custom',
custom: { adminRead: false, scopes: new Set(['blog', 'seo']) },
})
expect(resolveInitialState([STAR], 'connect')).toEqual({
tier: 'full',
custom: { adminRead: false, scopes: new Set() },
})
})
it('resolves ApiTokenCreateDialog defaults (D-08): Read-only when grantable, Full (*) fallback when admin:read absent', () => {
expect(resolveInitialState([STAR, ADMIN_READ, blog], 'token')).toEqual({
tier: 'read_only',
custom: { adminRead: false, scopes: new Set() },
})
expect(resolveInitialState([STAR, blog], 'token')).toEqual({
tier: 'full',
custom: { adminRead: false, scopes: new Set() },
})
expect(resolveInitialState([blog], 'token')).toEqual({
tier: 'custom',
custom: { adminRead: false, scopes: new Set() },
})
})
})