|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * `normalizeSectionField` ↔ `@objectstack/spec` FormFieldSchema coverage gate |
| 11 | + * (#3090, objectstack#4115). |
| 12 | + * |
| 13 | + * The normalizer is THE chokepoint between the two form-field vocabularies: |
| 14 | + * the spec's authored shape (`field` = object-field reference, presentation |
| 15 | + * deltas only) and the runtime shape (`name` = data path, self-contained |
| 16 | + * widget config). It cannot be a derivation — every key is a *translation* |
| 17 | + * with a destination of its own — so the gate is behavioral instead: |
| 18 | + * |
| 19 | + * - TABLE below holds one row per spec key: a sample authored value and an |
| 20 | + * assertion that the normalized output reflects it at its documented |
| 21 | + * destination. Deleting a mapping line in `sectionFields.ts` fails the |
| 22 | + * row — the rows are mutation-tested coverage, not a mirror list. |
| 23 | + * - The key sets are then compared BOTH ways against the spec's own |
| 24 | + * enumeration: a key the spec adds fails as UNMAPPED (decide: map it or |
| 25 | + * exempt it with a reason); a key the spec retires fails as STALE. |
| 26 | + * |
| 27 | + * Until this gate existed the drift was real: the ADR-0089 canonical |
| 28 | + * `visibleWhen` spelling, `dependsOn`, `keyField` and `disclosure` were all |
| 29 | + * silently dropped while the DEPRECATED `visibleOn` spelling worked. |
| 30 | + */ |
| 31 | + |
| 32 | +import { describe, it, expect } from 'vitest'; |
| 33 | +import { FormFieldSchema as SpecFormFieldSchema } from '@objectstack/spec/ui'; |
| 34 | +import { mapFieldTypeToFormType } from '@object-ui/fields'; |
| 35 | +import { normalizeSectionField } from './sectionFields'; |
| 36 | + |
| 37 | +const objectSchema = { |
| 38 | + name: 'crm_account', |
| 39 | + fields: { |
| 40 | + industry: { |
| 41 | + type: 'select', |
| 42 | + label: 'Industry', |
| 43 | + options: [{ label: 'Tech', value: 'tech' }], |
| 44 | + }, |
| 45 | + }, |
| 46 | +}; |
| 47 | + |
| 48 | +const ctx = { |
| 49 | + objectSchema, |
| 50 | + objectName: 'crm_account', |
| 51 | + fieldLabel: (_obj: string, _name: string, fallback: string) => fallback || _name, |
| 52 | +}; |
| 53 | + |
| 54 | +/** Normalize a spec field def against a schema that does NOT define `ghost`, |
| 55 | + * so every override is observable verbatim (no object-metadata interference). */ |
| 56 | +const norm = (def: Record<string, unknown>) => |
| 57 | + normalizeSectionField({ field: 'ghost', ...def }, ctx) as any; |
| 58 | + |
| 59 | +/** |
| 60 | + * One row per spec FormFieldSchema key: sample authored value → where the |
| 61 | + * runtime FormField must carry it. Non-obvious destinations are the point: |
| 62 | + * `helpText`→`description`, `readonly`→`disabled`, and BOTH visibility |
| 63 | + * spellings→the view-level `visibleOn` slot (the runtime `visibleWhen` slot |
| 64 | + * belongs to the object-level rule and must not be clobbered). |
| 65 | + */ |
| 66 | +const TABLE: Record<string, (f: (def: Record<string, unknown>) => any) => void> = { |
| 67 | + field: () => { |
| 68 | + const out = normalizeSectionField({ field: 'industry' }, ctx) as any; |
| 69 | + expect(out.name).toBe('industry'); // identity key translated to the data path |
| 70 | + expect(out.type).toBe(mapFieldTypeToFormType('select')); // object schema merged in |
| 71 | + }, |
| 72 | + type: (f) => expect(f({ type: 'text' }).type).toBe(mapFieldTypeToFormType('text')), |
| 73 | + options: (f) => |
| 74 | + expect(f({ options: [{ label: 'A', value: 'a' }] }).options).toEqual([ |
| 75 | + { label: 'A', value: 'a' }, |
| 76 | + ]), |
| 77 | + reference: (f) => { |
| 78 | + const out = f({ reference: 'accounts' }); |
| 79 | + expect(out.reference).toBe('accounts'); |
| 80 | + expect(out.reference_to).toBe('accounts'); // both spellings stamped (#2407) |
| 81 | + }, |
| 82 | + maxLength: (f) => expect(f({ maxLength: 10 }).maxLength).toBe(10), |
| 83 | + minLength: (f) => expect(f({ minLength: 2 }).minLength).toBe(2), |
| 84 | + min: (f) => expect(f({ min: 1 }).min).toBe(1), |
| 85 | + max: (f) => expect(f({ max: 9 }).max).toBe(9), |
| 86 | + precision: (f) => expect(f({ precision: 10 }).precision).toBe(10), |
| 87 | + scale: (f) => expect(f({ scale: 2 }).scale).toBe(2), |
| 88 | + multiple: (f) => expect(f({ multiple: true }).multiple).toBe(true), |
| 89 | + label: (f) => expect(f({ label: 'Custom' }).label).toBe('Custom'), |
| 90 | + placeholder: (f) => expect(f({ placeholder: 'Type…' }).placeholder).toBe('Type…'), |
| 91 | + helpText: (f) => expect(f({ helpText: 'Hint' }).description).toBe('Hint'), |
| 92 | + readonly: (f) => expect(f({ readonly: true }).disabled).toBe(true), |
| 93 | + immutable: (f) => expect(f({ immutable: true }).immutable).toBe(true), |
| 94 | + required: (f) => expect(f({ required: true }).required).toBe(true), |
| 95 | + hidden: (f) => expect(f({ hidden: true }).hidden).toBe(true), |
| 96 | + colSpan: (f) => expect(f({ colSpan: 2 }).colSpan).toBe(2), |
| 97 | + span: (f) => expect(f({ span: 'full' }).span).toBe('full'), |
| 98 | + widget: (f) => expect(f({ widget: 'rating' }).widget).toBe('rating'), |
| 99 | + language: (f) => expect(f({ language: 'sql' }).language).toBe('sql'), |
| 100 | + fields: (f) => |
| 101 | + expect(f({ fields: [{ field: 'inner' }] }).fields).toEqual([{ field: 'inner' }]), |
| 102 | + keyField: (f) => |
| 103 | + expect(f({ keyField: { field: 'name' } }).keyField).toEqual({ field: 'name' }), |
| 104 | + dependsOn: (f) => expect(f({ dependsOn: 'country' }).dependsOn).toBe('country'), |
| 105 | + visibleWhen: (f) => |
| 106 | + expect(f({ visibleWhen: 'record.a == 1' }).visibleOn).toBe('record.a == 1'), |
| 107 | + visibleOn: (f) => |
| 108 | + expect(f({ visibleOn: 'record.b == 2' }).visibleOn).toBe('record.b == 2'), |
| 109 | + disclosure: (f) => expect(f({ disclosure: 'popover' }).disclosure).toBe('popover'), |
| 110 | +}; |
| 111 | + |
| 112 | +/** Spec keys the normalizer deliberately does not carry, each with a reason. |
| 113 | + * Empty today — add entries here (never silently) when the spec grows a key |
| 114 | + * that genuinely has no runtime destination. */ |
| 115 | +const EXEMPT: Record<string, string> = {}; |
| 116 | + |
| 117 | +describe('normalizeSectionField covers the spec FormFieldSchema key set', () => { |
| 118 | + // `.strict().transform()` wraps the object in a ZodPipe; `.in` is the |
| 119 | + // strict object carrying the authoring shape. |
| 120 | + const specKeys = Object.keys((SpecFormFieldSchema as any).in.shape).sort(); |
| 121 | + |
| 122 | + it('has a behavioral row (or an exemption with a reason) for every spec key', () => { |
| 123 | + const covered = [...Object.keys(TABLE), ...Object.keys(EXEMPT)].sort(); |
| 124 | + expect(covered).toEqual(specKeys); |
| 125 | + }); |
| 126 | + |
| 127 | + it('has no stale rows for keys the spec has retired', () => { |
| 128 | + for (const key of [...Object.keys(TABLE), ...Object.keys(EXEMPT)]) { |
| 129 | + expect(specKeys, `'${key}' is no longer a spec FormField key`).toContain(key); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + for (const [key, assertRow] of Object.entries(TABLE)) { |
| 134 | + it(`carries spec '${key}' to its runtime destination`, () => { |
| 135 | + assertRow(norm); |
| 136 | + }); |
| 137 | + } |
| 138 | +}); |
0 commit comments