Files
ipal-kit/test/adminUx.test.ts
Radosław SmolińskiandClaude Fable 5.2 0a4f001797 IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file
names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import
paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are
not touched. package.json keeps the five old bin names beside the five new ones.
The old names stop working in this commit and come back as aliases in the next; docs follow.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:24:21 +01:00

250 lines
12 KiB
TypeScript

import type { CollectionConfig, Config, Field, GlobalConfig } from 'payload'
import { describe, expect, test } from 'vitest'
import { LIBRARY_BLOCK_SLUGS, libraryBlocks } from '../src/exports/blocks.js'
import { applyAdminI18n } from '../src/modules/admin-i18n/apply.js'
import { adminTexts } from '../src/modules/admin-i18n/index.js'
import { iapl } from '../src/plugin.js'
/**
* docs/dev/admin-ux.md, enforced: every admin string the plugin adds has an
* explicit English and Polish text, within the length limits, without
* developer words.
*/
const LABEL_MAX = 30
const DESCRIPTION_MAX = 120
const OPTION_MAX = 40
const TEXT_MAX = 160
// Product names may push a label past 3 words ("LinkedIn Insight Tag ID").
const PRODUCTS = /Google|Meta|Microsoft|LinkedIn|TikTok|Cloudflare|Bing|Yandex|IndexNow|Search Console|Turnstile|Clarity|Pixel/
// Function calls, JSX tags, ENV_VARS, camelCase words, package/vendor names, "Empty →".
const JARGON = /\w\(|<[A-Z]|\b[A-Z][A-Z0-9]*_[A-Z0-9_]+\b|\b[a-z]+[A-Z][a-z][A-Za-z]*\b|ipal-|Intecion|→/
// Abbreviations that end in a dot but don't end a sentence.
const ABBREVIATIONS = /\b(e\.g|i\.e|np|m\.in|tzw|ok|min|maks|max|etc)\./gi
const i18n = {
defaultLocale: 'pl',
locales: [
{ code: 'pl', label: 'Polski' },
{ code: 'en', label: 'English' },
],
}
const t = (en: string, pl: string) => ({ en, pl })
const project = (): Config =>
({
collections: [
{ slug: 'users', auth: true, fields: [] },
{ slug: 'pages', fields: [{ name: 'title', type: 'text', label: t('Title', 'Tytuł') }] },
{ slug: 'posts', fields: [{ name: 'title', type: 'text', label: t('Title', 'Tytuł') }] },
{ slug: 'media', fields: [], upload: true },
],
secret: 'test',
}) as unknown as Config
const fullConfig = () =>
iapl({
access: { authCollection: 'users' },
consentLog: true,
content: { collections: [{ slug: 'posts' }] },
cookieScanner: { crawl: { serviceUrl: 'http://crawler.test', siteUrl: 'http://site.test', token: 'x' } },
forms: { emailLayout: true },
i18n,
indexNow: true,
lightbox: true,
pages: { slug: 'pages' },
seo: { collections: ['pages', 'posts'] },
stats: true,
twoFactor: false,
} as never)(project())
type Problem = string
type Text = unknown
const isText = (v: Text): v is { en: string; pl: string } =>
typeof v === 'object' && v !== null && typeof (v as { en?: unknown }).en === 'string' && typeof (v as { pl?: unknown }).pl === 'string'
const sentences = (s: string) => s.replace(ABBREVIATIONS, '$1').split(/[.!?](?:\s|$)/).filter((p) => p.trim()).length
function checkText(where: string, kind: 'description' | 'label' | 'option', value: Text, problems: Problem[]) {
if (!isText(value)) {
problems.push(`${where}: ${kind} is not { en, pl } (${JSON.stringify(value)})`)
return
}
for (const lang of ['en', 'pl'] as const) {
const s = value[lang].trim()
if (!s) {problems.push(`${where}: empty ${kind} (${lang})`)}
if (JARGON.test(s)) {problems.push(`${where}: developer words in ${kind} (${lang}): "${s}"`)}
if (kind === 'label') {
const words = s.split(/\s+/).length
if (s.length > LABEL_MAX) {problems.push(`${where}: label over ${LABEL_MAX} chars (${lang}): "${s}"`)}
if (words > (PRODUCTS.test(s) ? 5 : 3)) {problems.push(`${where}: label over 3 words (${lang}): "${s}"`)}
}
if (kind === 'option' && s.length > OPTION_MAX) {problems.push(`${where}: option over ${OPTION_MAX} chars (${lang}): "${s}"`)}
if (kind === 'description') {
if (s.length > DESCRIPTION_MAX) {problems.push(`${where}: description over ${DESCRIPTION_MAX} chars (${lang}): "${s}"`)}
if (sentences(s) > 1) {problems.push(`${where}: description longer than one sentence (${lang}): "${s}"`)}
}
}
}
function walk(fields: Field[], path: string, problems: Problem[]) {
for (const field of fields) {
const f = field as { admin?: Record<string, unknown> } & Field & Record<string, unknown>
const where = `${path}.${'name' in f && typeof f.name === 'string' && f.name ? f.name : f.type}`
if (f.hidden || f.admin?.hidden || f.admin?.disabled) {continue}
if (f.type === 'tabs') {
for (const tab of (f as unknown as { tabs: ({ fields: Field[] } & Record<string, unknown>)[] }).tabs) {
const tabWhere = `${path}.tab(${typeof tab.name === 'string' ? tab.name : JSON.stringify(tab.label)})`
checkText(tabWhere, 'label', tab.label, problems)
if (tab.description) {checkText(tabWhere, 'description', tab.description, problems)}
walk(tab.fields, tabWhere, problems)
}
continue
}
if (f.type === 'row') {
walk(f.fields, path, problems)
continue
}
const visibleUi = f.type === 'ui' ? Boolean(f.label) : true
if (visibleUi && f.label !== false) {checkText(where, 'label', f.label, problems)}
if (f.admin?.description && typeof f.admin.description !== 'function') {checkText(where, 'description', f.admin.description, problems)}
if (Array.isArray(f.options)) {
for (const o of f.options as unknown[]) {
if (typeof o === 'object' && o) {checkText(`${where}[${String((o as { value: string }).value)}]`, 'option', (o as { label: Text }).label, problems)}
else {problems.push(`${where}: option "${String(o)}" has no label`)}
}
}
if (f.type === 'array') {
const labels = f.labels as { plural?: Text; singular?: Text } | undefined
checkText(`${where}.labels.singular`, 'label', labels?.singular, problems)
checkText(`${where}.labels.plural`, 'label', labels?.plural, problems)
}
if (f.type === 'blocks') {
for (const block of f.blocks as { fields: Field[]; labels?: { plural?: Text; singular?: Text }; slug: string }[]) {
checkText(`${where}.${block.slug}.labels.singular`, 'label', block.labels?.singular, problems)
checkText(`${where}.${block.slug}.labels.plural`, 'label', block.labels?.plural, problems)
walk(block.fields, `${where}.${block.slug}`, problems)
}
}
if ('fields' in f && Array.isArray(f.fields) && f.type !== 'blocks') {walk(f.fields as Field[], where, problems)}
}
}
function checkGlobal(global: GlobalConfig, problems: Problem[]) {
checkText(`global ${global.slug}`, 'label', global.label, problems)
if (global.admin?.group) {checkText(`global ${global.slug} group`, 'label', global.admin.group, problems)}
if (global.admin?.description && typeof global.admin.description !== 'function') {checkText(`global ${global.slug}`, 'description', global.admin.description, problems)}
walk(global.fields, global.slug, problems)
}
function checkCollection(collection: CollectionConfig, problems: Problem[]) {
checkText(`collection ${collection.slug} singular`, 'label', collection.labels?.singular, problems)
checkText(`collection ${collection.slug} plural`, 'label', collection.labels?.plural, problems)
if (collection.admin?.group) {checkText(`collection ${collection.slug} group`, 'label', collection.admin.group, problems)}
if (collection.admin?.description && typeof collection.admin.description !== 'function') {checkText(`collection ${collection.slug}`, 'description', collection.admin.description, problems)}
walk(collection.fields, collection.slug, problems)
}
// Collections the plugin creates; fields of other plugins (form-builder, plugin-seo) are checked where IAPL sets them.
const PLUGIN_COLLECTIONS = ['consent-records', 'cookie-review', 'stats-visits']
describe('admin wording (docs/dev/admin-ux.md)', () => {
test('plugin globals: every visible text in en + pl, within limits', async () => {
const config = await fullConfig()
const problems: Problem[] = []
for (const global of config.globals ?? []) {checkGlobal(global, problems)}
expect(problems).toEqual([])
})
test('plugin collections: every visible text in en + pl, within limits', async () => {
const config = await fullConfig()
const problems: Problem[] = []
const slugs = (config.collections ?? []).map((c) => c.slug)
for (const slug of PLUGIN_COLLECTIONS) {
const collection = config.collections?.find((c) => c.slug === slug || c.slug.startsWith(slug))
expect(collection, `${slug} in ${slugs.join(', ')}`).toBeTruthy()
checkCollection(collection!, problems)
}
expect(problems).toEqual([])
})
test('fields injected into project collections: en + pl, within limits', async () => {
const config = await fullConfig()
const problems: Problem[] = []
for (const slug of ['users', 'pages', 'posts']) {
const collection = config.collections!.find((c) => c.slug === slug)!
walk(collection.fields, slug, problems)
}
expect(problems).toEqual([])
})
test('form builder: blocks the plugin defines, en + pl, within limits', async () => {
const config = await fullConfig()
const problems: Problem[] = []
const forms = config.collections!.find((c) => c.slug === 'forms')!
checkText('collection forms singular', 'label', forms.labels?.singular, problems)
checkText('collection forms plural', 'label', forms.labels?.plural, problems)
const blocksField = forms.fields.find((f) => 'name' in f && f.name === 'fields') as { blocks: { fields: Field[]; labels?: { plural?: Text; singular?: Text }; slug: string }[] }
for (const block of blocksField.blocks) {
checkText(`forms.fields.${block.slug}.labels.singular`, 'label', block.labels?.singular, problems)
walk(block.fields, `forms.fields.${block.slug}`, problems)
}
expect(problems).toEqual([])
})
test('block library: every block, en + pl, within limits', () => {
const problems: Problem[] = []
const blocks = libraryBlocks({
...Object.fromEntries(LIBRARY_BLOCK_SLUGS.map((slug) => [slug, true])),
featureGrid: { icons: ['phone'] },
testimonials: { collection: 'testimonials' },
})
walk([{ name: 'layout', type: 'blocks', blocks, label: { en: 'Sections', pl: 'Sekcje' } } as Field], 'pages', problems)
expect(problems).toEqual([])
})
test('component texts: same keys in en and pl, none empty, no developer words', () => {
const en = adminTexts.en as Record<string, string>
const pl = adminTexts.pl as Record<string, string>
expect(Object.keys(pl).sort()).toEqual(Object.keys(en).sort())
const problems: Problem[] = []
for (const [lang, texts] of Object.entries({ en, pl })) {
for (const [key, s] of Object.entries(texts)) {
if (!s.trim()) {problems.push(`${key} (${lang}) is empty`)}
if (s.length > TEXT_MAX) {problems.push(`${key} (${lang}) over ${TEXT_MAX} chars`)}
// The vendor credit (whiteLabel.credit) is the one place that names the vendor.
if (key !== 'brandingCredit' && JARGON.test(s.replace(/\{\{\w+\}\}/g, ''))) {problems.push(`${key} (${lang}) has developer words: "${s}"`)}
}
}
expect(problems).toEqual([])
})
})
describe('admin languages', () => {
test('English and Polish by default, with the plugin texts', async () => {
const config = await fullConfig()
expect(Object.keys(config.i18n?.supportedLanguages ?? {}).sort()).toEqual(['en', 'pl'])
const translations = config.i18n?.translations as Record<string, { ipal: Record<string, string> }>
expect(translations.pl.ipal).toEqual(adminTexts.pl)
expect(translations.en.ipal).toEqual(adminTexts.en)
})
test("the project's languages and translations win", () => {
const de = { dateFNSKey: 'de', translations: {} }
const key = Object.keys(adminTexts.en)[0]
const config = applyAdminI18n({
i18n: { supportedLanguages: { de } as never, translations: { de: { ipal: { [key]: 'Eigener Text' } } } as never },
} as unknown as Config)
expect(Object.keys(config.i18n!.supportedLanguages!)).toEqual(['de'])
const translations = config.i18n!.translations as Record<string, { ipal: Record<string, string> }>
expect(translations.de.ipal[key]).toBe('Eigener Text')
// Another key falls back to English, not to the raw key.
const other = Object.keys(adminTexts.en)[1]
expect(translations.de.ipal[other]).toBe((adminTexts.en as Record<string, string>)[other])
})
})