/** * supplied-code.test.ts — the create-only SmartCodeField injection. * * The pagespec flag `codedEntity: { supplied, codeKey, codeInputFields }` * (stamped by derive-code-specs) mounts the primitive on the CREATE mode of * the generated form — never a fields[] entry, never on edit. Opt-in STRICT: * without the supplied flag the form output is byte-identical. */ import { describe, it, expect } from 'vitest' import { generate } from '../generate.js' import type { ScaffoldComponentInput } from '../types.js' function fixture(overrides: Partial = {}): ScaffoldComponentInput { return { module: 'crm', appCode: 'TestV2', entity: 'Budget', section: 'budgets', views: ['form'], fields: [ { name: 'label', type: 'string', required: true }, { name: 'clientName', type: 'string', required: false }, ], projectPath: '/web', ...overrides, } } const SUPPLIED = { supplied: true, codeKey: 'crm.budget', codeInputFields: ['clientName'] } function formPage(overrides: Partial = {}): string { const files = generate(fixture(overrides), {}) const f = files.find((x) => x.path.endsWith('BudgetFormPage.tsx')) if (!f) throw new Error('missing BudgetFormPage.tsx') return f.content } describe('scaffold-component / supplied-on-create — SmartCodeField injection', () => { it('mounts the primitive CREATE-only ({!isEdit && …}) with codeKey + watched inputs', () => { const c = formPage({ codedEntity: SUPPLIED }) expect(c).toContain("import { SmartCodeField } from '@/components/ui/SmartCodeField'") expect(c).toMatch(/\{!isEdit && \(\s* { const c = formPage({ codedEntity: SUPPLIED }) expect(c).toContain("...(suppliedCode.trim() !== '' ? { code: suppliedCode.trim() } : {})") // The update mutation never carries the supplied code. expect(c).toMatch(/updateMutation\.mutateAsync\(\{ id, data: (?!.*suppliedCode)/) }) // The pageSpec.codedEntity fallback shares the exact `??` read with the // validate gate and the i18n label (both covered) — the per-page pageSpec // pipeline itself is out of this suite's scope. it('OPT-IN STRICT: boolean flag / label-only object / supplied without codeKey render byte-identically', () => { const base = formPage() expect(formPage({ codedEntity: true })).toBe(base) expect(formPage({ codedEntity: { label: { fr: 'Référence' } } as never })).toBe(base) // supplied WITHOUT codeKey: the primitive cannot mount (no endpoint to // call) — the injection degrades to nothing rather than guessing a key. expect(formPage({ codedEntity: { supplied: true } as never })).toBe(base) }) it('a codeInputField absent from the form fields is dropped from the watched inputs', () => { const c = formPage({ codedEntity: { ...SUPPLIED, codeInputFields: ['clientName', 'GhostField'] } }) expect(c).toContain("'clientName'") expect(c).not.toContain('GhostField') }) })