Files
wordpress-theme-aksrt/tests/widget-system/admin-ui-contract.test.cjs
T
2026-09-01 18:43:21 +08:00

85 lines
3.9 KiB
JavaScript

const test = require('node:test')
const assert = require('node:assert/strict')
const fs = require('node:fs')
const path = require('node:path')
const root = path.resolve(__dirname, '../..')
const read = file => fs.readFileSync(path.join(root, file), 'utf8')
const app = read('assets/admin/src/App.vue')
const styles = read('assets/admin/src/style.css')
const field = read('assets/admin/src/components/form/AksField.vue')
const fieldGroup = read('assets/admin/src/components/form/AksFieldGroup.vue')
const formRenderer = read('assets/admin/src/components/form/AksFormRenderer.vue')
test('admin theme exposes the approved white, grey, and green tokens', () => {
const requiredTokens = {
'--aksrt-color-primary': '#18A058',
'--aksrt-color-primary-soft': '#EAF7EF',
'--aksrt-color-canvas': '#F5F7F6',
'--aksrt-color-surface': '#FFFFFF',
'--aksrt-color-text': '#1F2923',
'--aksrt-color-muted': '#6F7A72',
'--aksrt-color-border': '#E2E8E3',
}
for (const [token, value] of Object.entries(requiredTokens)) {
assert.match(styles, new RegExp(`${token}:\\s*${value}`, 'i'), `missing ${token}`)
}
})
test('app uses Naive UI overrides and the compact responsive shell', () => {
assert.match(app, /:theme-overrides="adminThemeOverrides"/)
assert.match(app, /const adminThemeOverrides = \{/)
assert.match(app, /:width="232"/)
assert.match(app, /const MOBILE_BREAKPOINT = 960/)
assert.match(app, /aksrt-logo__version/)
assert.doesNotMatch(app, /aksrt-csf-(?:shell|header|version)/)
})
test('rejected gradient banner and split-row form overrides do not return', () => {
const source = `${app}\n${styles}`
assert.doesNotMatch(source, /#f97794|#623aa2|#0991f5|#0997f5/i)
assert.doesNotMatch(source, /linear-gradient/i)
assert.doesNotMatch(styles, /\.aksrt-field\s*>\s*:not/)
assert.doesNotMatch(styles, /\.aksrt-field__label-row\s*\{[^}]*grid-column/s)
})
test('shared forms use card surfaces and design tokens', () => {
assert.match(fieldGroup, /border:\s*1px solid var\(--aksrt-color-border\)/)
assert.match(fieldGroup, /border-radius:\s*12px/)
assert.match(fieldGroup, /box-shadow:\s*var\(--aksrt-shadow-card\)/)
assert.match(field, /color:\s*var\(--aksrt-color-primary\)/)
assert.match(field, /background:\s*var\(--aksrt-color-primary-soft\)/)
assert.match(formRenderer, /background:\s*var\(--aksrt-color-surface\)/)
})
test('navigation, focus, responsive layout, and reduced motion are contracted', () => {
assert.match(styles, /\.n-menu-item-content--selected::after/)
assert.match(styles, /:focus-visible/)
assert.match(styles, /@media \(max-width:\s*960px\)/)
assert.match(styles, /@media \(max-width:\s*768px\)/)
assert.match(styles, /@media \(max-width:\s*480px\)/)
assert.match(styles, /@media \(prefers-reduced-motion:\s*reduce\)/)
})
test('captcha scene whitelist matches both admin schemas and excludes dead email verification', () => {
const captcha = read('inc/modules/captcha.php')
const phpSchema = read('src/Admin/Schema/SchemaPermissions.php')
const jsSchema = read('assets/admin/src/schema/sections/permissions/index.js')
assert.doesNotMatch(captcha, /'verify_email'\s*=>/)
assert.doesNotMatch(phpSchema, /'verify_email'/)
assert.doesNotMatch(jsSchema, /value:\s*'verify_email'/)
const sceneBlock = captcha.match(/function aksrt_captcha_scenes\(\): array \{[\s\S]*?return \[([\s\S]*?)\n\t\];/)
const phpField = phpSchema.match(/'key' => 'enabled_scenarios'[\s\S]*?'options' => \[([\s\S]*?)\]\],/)
const jsField = jsSchema.match(/key: 'enabled_scenarios'[\s\S]*?options: \[([\s\S]*?)\n\s*\],/)
assert.ok(sceneBlock && phpField && jsField, 'captcha scene contracts must be discoverable')
const values = (text, pattern) => [...text.matchAll(pattern)].map((match) => match[1]).sort()
const scenes = values(sceneBlock[1], /'([^']+)'\s*=>/g)
assert.deepStrictEqual(values(phpField[1], /'value'\s*=>\s*'([^']+)'/g), scenes)
assert.deepStrictEqual(values(jsField[1], /value:\s*'([^']+)'/g), scenes)
})