/** * scaffold-api-client — CLI entry contract. `index.ts` used to hand the RAW * spec to generate(): `scope: 'row'`, `responseType: 'void'`, `fields: []`, * `required: true` never existed at runtime — a custom action lost its * `/${id}` segment and an entity without `fields` crashed with a stack trace. * * 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 { ApiEntitySchema, ScaffoldApiClientInputSchema } from '../types.js' const INDEX = resolve(dirname(fileURLToPath(import.meta.url)), '../index.ts') /** Entity WITHOUT `fields`, with a custom action declaring only its code. */ const SPEC = { module: 'hrm', appCode: 'myapp', entities: [{ name: 'Employee', section: 'employees', customActions: [{ code: 'archive' }] }], projectPath: '/path', } describe('scaffold-api-client — the CLI entry runs the PARSED spec', () => { it('runs an entity without `fields` in --dry_run (the raw spec crashed on `fields.map`)', () => { 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) expect(r.envelope.data.files.some((f: string) => f.endsWith('employeeService.ts'))).toBe(true) }) it('validate() returns the parsed spec with the documented defaults applied', () => { const v = validate(SPEC) expect(v.valid).toBe(true) expect(v.data?.entities[0]?.fields).toEqual([]) expect(v.data?.entities[0]?.customActions[0]?.scope).toBe('row') expect(v.data?.entities[0]?.customActions[0]?.responseType).toBe('void') expect(v.data?.routeMode).toBe('integration') }) it('a custom action declaring only its code targets the ROW URL once parsed (scope default row)', () => { const service = generate(validate(SPEC).data!).find(f => f.path.endsWith('employeeService.ts')) expect(service).toBeDefined() expect(service!.content).toContain('/${id}/archive') }) }) describe('scaffold-api-client — parity RAW ↔ PARSED (real properties, not schema-vs-schema)', () => { const parsed = () => validate(SPEC).data! it('(a) no-strip: every key path of the spec survives the parse with its value', () => { const report = specParity(SPEC, parsed(), ScaffoldApiClientInputSchema) expect(report.rawPaths).toContain('entities[0].customActions[0].code') // 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(SPEC, parsed(), ScaffoldApiClientInputSchema) expect(report.added.map(a => a.path)).toEqual(expect.arrayContaining([ 'routeMode', 'entities[0].fields', 'entities[0].customActions[0].scope', 'entities[0].customActions[0].responseType', ])) 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({ ...ScaffoldApiClientInputSchema.shape, entities: z.array(ApiEntitySchema.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 default applied with another value, is reported', () => { expect(specParity(SPEC, { ...parsed(), webRoot: 'web/myapp-web' }, ScaffoldApiClientInputSchema).undeclared).toEqual(['webRoot']) expect(specParity(SPEC, { ...parsed(), routeMode: 'screens' }, ScaffoldApiClientInputSchema).undeclared).toEqual(['routeMode']) }) })