/** * 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 = { '': '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; types: Set } { const src = read(file).replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '') const runtime = new Set() const types = new Set() 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 { 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([]) }) })