/** * scaffold-routes — CLI entry contract. `index.ts` used to hand the RAW spec * to generate(): an entity without `views` (default list/detail/create) * crashed normalizeViews() with a stack trace instead of an envelope. * * QC 2026-09-24 (lot 13): the lot-1 "parity" test compared generate(parse(x)) * to generate(parse(parse(x))) — schema output on both sides, so a schema * stripping a key the renderer reads could never be detected. The parity * block now holds two REAL properties (lib/__tests__/helpers/spec-parity): * no-strip against the RAW example, defaults-only against the schema's * DECLARED defaults — each proven to fire. */ import { describe, it, expect } from 'vitest' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { z } from 'zod' import { runCli } from '../../../../../../lib/__tests__/helpers/run-cli.js' import { specParity } from '../../../../../../lib/__tests__/helpers/spec-parity.js' import { validate } from '../validate.js' import { generate } from '../generate.js' import { RouteEntitySchema, ScaffoldRoutesInputSchema } from '../types.js' const INDEX = resolve(dirname(fileURLToPath(import.meta.url)), '../index.ts') /** The documented SKILL example WITHOUT `views`. */ const SPEC = { module: 'hrm', appCode: 'myapp', entities: [{ name: 'Employee', section: 'employees' }], projectPath: '/path', } describe('scaffold-routes — the CLI entry runs the PARSED spec', () => { it('runs an entity without `views` in --dry_run (the raw spec threw `views is not iterable`)', () => { const r = runCli(INDEX, ['--spec', JSON.stringify(SPEC), '--dry_run']) expect(r.envelope, r.stderr).not.toBeNull() expect(r.envelope.success, JSON.stringify(r.envelope.errors)).toBe(true) expect(r.status).toBe(0) const files: string[] = r.envelope.data.files expect(files.some(f => f.endsWith('Registry.ts'))).toBe(true) expect(files.some(f => f.endsWith('Routes.ts'))).toBe(true) }) it('validate() returns the parsed spec with the documented `views` default', () => { const v = validate(SPEC) expect(v.valid).toBe(true) expect(v.data?.entities[0]?.views).toEqual(['list', 'detail', 'create']) }) }) describe('scaffold-routes — parity RAW ↔ PARSED (real properties, not schema-vs-schema)', () => { const parsed = () => validate(SPEC).data! it('(a) no-strip: every key path of the documented example survives the parse with its value', () => { const report = specParity(SPEC, parsed(), ScaffoldRoutesInputSchema) expect(report.rawPaths).toEqual(['module', 'appCode', 'entities', 'entities[0]', 'entities[0].name', 'entities[0].section', 'projectPath']) expect(report.stripped).toEqual([]) expect(report.changed).toEqual([]) }) it('(b) defaults-only: the only key the parse adds is the DECLARED `entities[].views` default', () => { const report = specParity(SPEC, parsed(), ScaffoldRoutesInputSchema) expect(report.added.map(a => a.path)).toEqual(['entities[0].views']) expect(report.undeclared).toEqual([]) }) it('secondary: generate() reads nothing a JSON round-trip of the parsed spec would lose', () => { const p = parsed() expect(generate(p)).toEqual(generate(JSON.parse(JSON.stringify(p)))) }) it('FIRES (a): a strip-mode copy of the schema without `entities[].section` is reported at that path', () => { const stripping = z.object({ ...ScaffoldRoutesInputSchema.shape, entities: z.array(RouteEntitySchema.omit({ section: true })), }) const report = specParity(SPEC, stripping.parse(SPEC), stripping) expect(report.stripped).toEqual(['entities[0].section']) expect(report.changed).toEqual([]) }) it('FIRES (b): a key injected after the parse, or a nested default applied with another value, is reported', () => { const p = parsed() expect(specParity(SPEC, { ...p, defaultPwa: { support: 'adapted' } }, ScaffoldRoutesInputSchema).undeclared).toEqual(['defaultPwa']) const wrongViews = { ...p, entities: [{ ...p.entities[0], views: ['list'] }] } expect(specParity(SPEC, wrongViews, ScaffoldRoutesInputSchema).undeclared).toEqual(['entities[0].views']) }) })