/** * scaffold-controller — CLI entry contract. `index.ts` used to validate with * Zod and then hand the RAW spec to generate(): every documented default * (`actions`, `customActions`, `fields[].required`, action httpMethod/scope…) * was absent at runtime and the SKILL's own example crashed with a stack * trace. These tests run the real entry point on the documented invocation. * * 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 { ControllerFieldSchema, ScaffoldControllerInputSchema } from '../types.js' const INDEX = resolve(dirname(fileURLToPath(import.meta.url)), '../index.ts') /** The invocation documented in backend/controller/SKILL.md (## Invocation), verbatim. */ const DOC_EXAMPLE = { name: 'Employee', module: 'hrm', appCode: 'MyApp', applicationCode: 'hr', section: 'employees', navRoute: 'hrm.employees', fields: [{ name: 'firstName', type: 'string', required: true }, { name: 'lastName', type: 'string', required: true }], projectPath: '/path', } describe('scaffold-controller — the CLI entry runs the PARSED spec', () => { it('runs the documented SKILL example in --dry_run (the raw spec crashed on `actions.map`)', () => { const r = runCli(INDEX, ['--spec', JSON.stringify(DOC_EXAMPLE), '--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) expect(r.envelope.data.dryRun).toBe(true) expect(r.envelope.data.files.some((f: string) => f.endsWith('EmployeesController.cs'))).toBe(true) }) it('validate() returns the parsed spec with the documented defaults applied', () => { const v = validate(DOC_EXAMPLE) expect(v.valid).toBe(true) expect(v.data?.actions).toEqual(['read', 'create', 'update', 'delete']) expect(v.data?.customActions).toEqual([]) expect(v.data?.versioned).toBe(false) }) it('validate() carries no data when the spec is refused', () => { const v = validate({ ...DOC_EXAMPLE, navRoute: 'bad' }) expect(v.valid).toBe(false) expect(v.data).toBeUndefined() }) }) describe('scaffold-controller — parity RAW ↔ PARSED (real properties, not schema-vs-schema)', () => { const parsed = () => validate(DOC_EXAMPLE).data! it('(a) no-strip: every key path of the documented example survives the parse with its value', () => { const report = specParity(DOC_EXAMPLE, parsed(), ScaffoldControllerInputSchema) expect(report.rawPaths).toContain('fields[1].required') // the walk reaches the nested keys expect(report.stripped).toEqual([]) expect(report.changed).toEqual([]) }) it('(b) defaults-only: every key the parse adds is a default DECLARED at that path, with that value', () => { const report = specParity(DOC_EXAMPLE, parsed(), ScaffoldControllerInputSchema) expect(report.added.map(a => a.path)).toEqual(expect.arrayContaining(['actions', 'customActions', 'versioned', 'codedEntity'])) 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 `fields[].required` is reported at both paths', () => { const stripping = z.object({ ...ScaffoldControllerInputSchema.shape, fields: z.array(ControllerFieldSchema.omit({ required: true })), }) const report = specParity(DOC_EXAMPLE, stripping.parse(DOC_EXAMPLE), stripping) expect(report.stripped).toEqual(['fields[0].required', 'fields[1].required']) expect(report.changed).toEqual([]) }) it('FIRES (b): a key injected after the parse, or a default applied with another value, is reported', () => { expect(specParity(DOC_EXAMPLE, { ...parsed(), derivedRoute: 'x' }, ScaffoldControllerInputSchema).undeclared).toEqual(['derivedRoute']) expect(specParity(DOC_EXAMPLE, { ...parsed(), versioned: true }, ScaffoldControllerInputSchema).undeclared).toEqual(['versioned']) }) })