Files
ipal-kit/test/docsConsistency.test.ts
T
radoslaw.smolinskiandClaude Opus 5.5 81e864bde9 Docs for 1.9.2: import instead of code; docs↔code consistency test; audit accepts one-line routes
- AGENT.md: §7 full capability catalog (need → import), §0b/§3/§6 on RenderForm, SiteProviders,
  createPageRoute, re-exported routes, plugin section fields, resolveLink/resolveNav
- PROGRAMISTA §2.4, §2.7–2.8 rewritten; §6 items 28–33 (28: FormRenderer leaked the Emails tab)
- forms, links, blocks, pages, consent, turnstile, notifications, seo, mcp, i18n, deployment docs
- ipal-form: RenderForm block, formClasses, CustomForm example (FormRenderer template removed),
  seed template handles steps and option labels per language
- ipal-nav, ipal-block, ipal-setup, ipal-upgrade, ipal-seo-check, ipal-consent-check, vibe.md
- ipal-audit: re-export routes and SiteProviders satisfy the mandatory set; flags form-engine copies,
  project section fields / navigation, server entries in client components
- test/docsConsistency: docs imports exist in code; every public export is documented
- CHANGELOG 1.9.2

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:52:29 +02:00

112 lines
5.2 KiB
TypeScript

/**
* Docs ↔ code, both ways — so the instructions agents follow never drift from
* the package:
* 1. every name a doc imports from '@intecion/ipal-kit[/entry]' or '@/lib/content'
* exists there (a renamed/removed API breaks this test, not a client project);
* 2. every public runtime export is described somewhere in docs/ (an API
* nobody knows about gets re-implemented in projects).
*/
import { readdirSync, readFileSync, statSync } from 'node:fs'
import path from 'node:path'
import { describe, expect, test } from 'vitest'
const root = path.resolve(import.meta.dirname, '..')
const read = (p: string) => readFileSync(path.join(root, p), 'utf8')
const ENTRIES: Record<string, string> = {
'': 'src/index.ts',
'/admin': 'src/exports/admin.ts',
'/client': 'src/exports/client.ts',
'/env': 'src/exports/env.ts',
'/next/middleware': 'src/exports/next-middleware.ts',
'/rsc': 'src/exports/rsc.ts',
'/server': 'src/exports/server.ts',
}
/** Names an entry file exports — `export { a, b as c } from …`, `export type { … }`. */
function exportsOf(file: string): { runtime: Set<string>; types: Set<string> } {
const src = read(file).replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '')
const runtime = new Set<string>()
const types = new Set<string>()
for (const m of src.matchAll(/export\s+(type\s+)?\{([^}]*)\}/g)) {
for (const raw of m[2].split(',')) {
const item = raw.trim()
if (!item) {continue}
const isType = Boolean(m[1]) || item.startsWith('type ')
const name = item.replace(/^type\s+/, '').split(/\s+as\s+/).pop()!.trim()
;(isType ? types : runtime).add(name)
}
}
return { runtime, types }
}
/** Keys returned by createContentHelpers — what `@/lib/content` can re-export. */
function contentHelperNames(): Set<string> {
const src = read('src/modules/frontend/createContentHelpers.ts')
const block = src.slice(src.lastIndexOf(' return {'))
return new Set([...block.slice(0, block.indexOf('}')).matchAll(/^\s+(\w+),?$/gm)].map((m) => m[1]))
}
function docFiles(dir = 'docs'): string[] {
return readdirSync(path.join(root, dir)).flatMap((name) => {
const rel = path.join(dir, name)
return statSync(path.join(root, rel)).isDirectory() ? docFiles(rel) : /\.(?:md|ts|tsx|mjs)$/.test(name) ? [rel] : []
})
}
const docs = docFiles()
const exported = Object.fromEntries(Object.entries(ENTRIES).map(([sub, file]) => [sub, exportsOf(file)]))
describe('docs ↔ code', () => {
test('every name imported in the docs exists in the entry it is imported from', () => {
const helpers = contentHelperNames()
const missing: string[] = []
for (const file of docs) {
const text = read(file)
for (const m of text.matchAll(/import\s+(?:type\s+)?\{([^}]*)\}\s+from\s+'(@intecion\/ipal-kit(?:\/[a-z/-]+)?|@\/lib\/content)'/g)) {
const source = m[2]
for (const raw of m[1].split(',')) {
const name = raw.trim().replace(/^type\s+/, '').split(/\s+as\s+/)[0].trim()
if (!name || name.startsWith('…') || name === '...') {continue}
if (source === '@/lib/content') {
if (!helpers.has(name)) {missing.push(`${file}: ${name} from @/lib/content`)}
continue
}
const entry = exported[source.replace('@intecion/ipal-kit', '')]
if (!entry) {continue} // unknown subpath — exports.test.ts reports it
if (!entry.runtime.has(name) && !entry.types.has(name)) {missing.push(`${file}: ${name} from ${source}`)}
}
}
// `export { x as GET } from '@/lib/content'` in route examples
for (const m of text.matchAll(/export\s+\{([^}]*)\}\s+from\s+'@\/lib\/content'/g)) {
for (const raw of m[1].split(',')) {
const name = raw.trim().split(/\s+as\s+/)[0].trim()
if (name && !helpers.has(name)) {missing.push(`${file}: export ${name} from @/lib/content`)}
}
}
}
expect(missing).toEqual([])
})
test('every public runtime export is described in the docs', () => {
// Constants and building blocks the plugin wires itself — documented through what uses them.
const internal = new Set([
'A11Y_DEFAULT', 'ACCEPT_ALL_CONSENT', 'buildConsentLogCollection', 'buildConsentLogEndpoint', 'buildFormsPlugin', 'buildIndexNowHook',
'buildIntegrationStatusBanner', 'buildIntegrationStatusField', 'checkboxGroupBlock', 'CONSENT_LOG_SLUG', 'CONSENT_MAX_AGE',
'CONSENT_VERSION', 'DEFAULT_CONSENT', 'DEFAULT_SITEMAP_CREDIT', 'INDEXNOW_KEY_ENV', 'INDEXNOW_KEY_PATH',
'injectAutoFillMeta', 'IntecionCredit', 'IntegrationStatusBanner', 'MARKDOWN_PREFIX', 'matchAcceptLanguage',
'readConsentRecord', 'recordIntegrationStatus', 'REJECT_ALL_CONSENT', 'serializeA11y',
'serializeConsent', 'SITE_INTEGRATIONS_SLUG', 'SITE_SETTINGS_SLUG', 'SITEMAP_STYLE_PARAM', 'stepBlock', 'TestEmailButton',
])
const all = docs.map(read).join('\n')
const undocumented: string[] = []
for (const [sub, { runtime }] of Object.entries(exported)) {
for (const name of runtime) {
if (internal.has(name)) {continue}
if (!new RegExp(`\\b${name}\\b`).test(all)) {undocumented.push(`${name} (@intecion/ipal-kit${sub})`)}
}
}
expect(undocumented).toEqual([])
})
})