|
| 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 | + * objectui#6152 round 1 — the `ObjectFormSchema` members the TypeScript face |
| 9 | + * declared and the zod mirror had never heard of. |
| 10 | + * |
| 11 | + * ## The defect |
| 12 | + * |
| 13 | + * `UnmirroredDeclared['objectql.zod.ts#ObjectFormSchema']` in |
| 14 | + * `zod-mirror-parity.test.ts` recorded keys `../objectql.ts` invites an author |
| 15 | + * to write and `../zod/objectql.zod.ts` did not declare. Two faces paid for it: |
| 16 | + * |
| 17 | + * - the TOLERANT face (`BaseSchema` is `.passthrough()`) kept any value at |
| 18 | + * those keys unexamined, so `{ formType: 'carousel' }` parsed green and |
| 19 | + * `ObjectForm` fell through to its simple layout; |
| 20 | + * - the STRICT authoring face (`StrictAnyComponentSchema`, objectui#8345, |
| 21 | + * derived from the mirrors) refused the keys outright, although `tsc` |
| 22 | + * accepted them — objectui#5250's M3 class (iii), the refusals |
| 23 | + * `objectui validate` would print wrongly once it judges strict. |
| 24 | + * |
| 25 | + * Each key was measured before it was mirrored: authored (a document, the |
| 26 | + * object-view `form` slot, or a form view relayed into the node), and READ by a |
| 27 | + * shipped renderer (`ObjectForm` in `@object-ui/plugin-form`). The readings are |
| 28 | + * in objectui#6152's round-1 report; this file pins the result, not the census. |
| 29 | + * |
| 30 | + * ## What each row asserts |
| 31 | + * |
| 32 | + * - a VALID authored value parses on BOTH faces — the strict leg is the one |
| 33 | + * that moved (refused → accepted), the tolerant leg shows the shape is not a |
| 34 | + * broken schema; |
| 35 | + * - a WRONG-TYPED value is refused AT THE KEY on the tolerant face — the leg |
| 36 | + * that moved the other way (kept unexamined → refused). |
| 37 | + * |
| 38 | + * `open` and `submitHandler` stay out of the mirror on purpose (their routes are |
| 39 | + * open on objectui#6152; objectui#6182 rules out a string handler), and a row |
| 40 | + * holds them out so a later card cannot mirror them without that ruling. |
| 41 | + */ |
| 42 | +import { describe, it, expect } from 'vitest'; |
| 43 | +import { readFileSync } from 'node:fs'; |
| 44 | +import { fileURLToPath } from 'node:url'; |
| 45 | +import { dirname, join } from 'node:path'; |
| 46 | + |
| 47 | +import { ObjectFormSchema as ObjectFormMirror } from '../zod/objectql.zod.js'; |
| 48 | +import { AnyComponentSchema, StrictAnyComponentSchema } from '../zod/index.zod.js'; |
| 49 | + |
| 50 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 51 | +const REPO_ROOT = join(HERE, '..', '..', '..', '..'); |
| 52 | + |
| 53 | +/** A minimal document both faces accept; every row below is a delta on it. */ |
| 54 | +const BASE = { type: 'object-form', objectName: 'order', mode: 'create' } as const; |
| 55 | + |
| 56 | +/** Each mirrored key: one authored value, and one value its declaration refuses. */ |
| 57 | +const ROWS: ReadonlyArray<{ key: string; valid: unknown; wrong: unknown }> = [ |
| 58 | + { key: 'formType', valid: 'wizard', wrong: 'carousel' }, |
| 59 | + { |
| 60 | + key: 'sections', |
| 61 | + valid: [ |
| 62 | + { name: 'basics', label: 'Basics', columns: 2, fields: ['name', { name: 'email', type: 'email' }] }, |
| 63 | + { group: 'contact_info', pane: 'secondary', visibleWhen: { dialect: 'cel', source: 'record.kind == "b2b"' } }, |
| 64 | + ], |
| 65 | + wrong: 'basics', |
| 66 | + }, |
| 67 | + { key: 'defaultTab', valid: 'basics', wrong: 3 }, |
| 68 | + { key: 'tabPosition', valid: 'left', wrong: 'middle' }, |
| 69 | + { key: 'allowSkip', valid: true, wrong: 'yes' }, |
| 70 | + { key: 'showStepIndicator', valid: false, wrong: 'no' }, |
| 71 | + { key: 'nextText', valid: { en: 'Next', fr: 'Suivant' }, wrong: 42 }, |
| 72 | + { key: 'prevText', valid: 'Back', wrong: 42 }, |
| 73 | + { key: 'splitDirection', valid: 'vertical', wrong: 'diagonal' }, |
| 74 | + { key: 'splitSize', valid: 40, wrong: '40%' }, |
| 75 | + { key: 'splitResizable', valid: false, wrong: 'false' }, |
| 76 | + { key: 'drawerSide', valid: 'left', wrong: 'center' }, |
| 77 | + { key: 'drawerWidth', valid: '40%', wrong: 40 }, |
| 78 | + { key: 'modalSize', valid: 'lg', wrong: 'huge' }, |
| 79 | + { key: 'modalCloseButton', valid: false, wrong: 'no' }, |
| 80 | + { key: 'mobile', valid: { stickyActions: true, stepper: 'auto', stepperMinFields: 6 }, wrong: { stepper: 'sometimes' } }, |
| 81 | + { key: 'buttons', valid: { submit: { show: true, label: 'Save' }, reset: { show: false } }, wrong: { submit: { show: 'yes' } } }, |
| 82 | + { key: 'defaults', valid: { status: 'open', priority: 2 }, wrong: ['status'] }, |
| 83 | + { key: 'subforms', valid: [{ childObject: 'order_line', minRows: 1 }], wrong: [{ relationshipField: 'order' }] }, |
| 84 | +]; |
| 85 | + |
| 86 | +describe('objectui#6152 round 1 — `object-form` members the mirror now declares', () => { |
| 87 | + it('CONTROL: the minimal document parses on both faces', () => { |
| 88 | + expect(AnyComponentSchema.safeParse(BASE).success).toBe(true); |
| 89 | + expect(StrictAnyComponentSchema.safeParse(BASE).success).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it.each(ROWS)('`$key` is a member of the mirror', ({ key }) => { |
| 93 | + expect(key in (ObjectFormMirror.shape as Record<string, unknown>)).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it.each(ROWS)('an authored `$key` parses on the strict face and the tolerant face', ({ key, valid }) => { |
| 97 | + const doc = { ...BASE, [key]: valid }; |
| 98 | + const strict = StrictAnyComponentSchema.safeParse(doc); |
| 99 | + expect(strict.success, JSON.stringify(strict.error?.issues)).toBe(true); |
| 100 | + const tolerant = AnyComponentSchema.safeParse(doc); |
| 101 | + expect(tolerant.success, JSON.stringify(tolerant.error?.issues)).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it.each(ROWS)('a wrong-typed `$key` is refused at the key on the tolerant face', ({ key, wrong }) => { |
| 105 | + const parsed = ObjectFormMirror.safeParse({ ...BASE, [key]: wrong }); |
| 106 | + expect(parsed.success).toBe(false); |
| 107 | + for (const issue of parsed.error?.issues ?? []) expect(issue.path[0]).toBe(key); |
| 108 | + }); |
| 109 | + |
| 110 | + it('the object-view `form` slot takes the same members (it is this mirror minus the identity keys)', () => { |
| 111 | + const doc = { type: 'object-view', objectName: 'order', form: { formType: 'drawer', drawerSide: 'left', sections: [{ fields: ['name'] }] } }; |
| 112 | + const strict = StrictAnyComponentSchema.safeParse(doc); |
| 113 | + expect(strict.success, JSON.stringify(strict.error?.issues)).toBe(true); |
| 114 | + expect(AnyComponentSchema.safeParse({ ...doc, form: { formType: 'carousel' } }).success).toBe(false); |
| 115 | + }); |
| 116 | + |
| 117 | + it('a section is a closed shape on the strict face: an undeclared section key is named there', () => { |
| 118 | + // `className` is deliberately undeclared on `ObjectFormSection` (objectui#7200). |
| 119 | + const strict = StrictAnyComponentSchema.safeParse({ ...BASE, sections: [{ name: 'a', className: 'p-4', fields: ['a'] }] }); |
| 120 | + expect(strict.success).toBe(false); |
| 121 | + expect(JSON.stringify(strict.error?.issues)).toContain('"className"'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('the catalog document objectui#5250 M3 charged with three of these keys now parses on the strict face', () => { |
| 125 | + const doc = JSON.parse(readFileSync(join(REPO_ROOT, 'examples/schema-catalog/src/schemas/plugin-form/object-form-tabbed-sections.json'), 'utf8')); |
| 126 | + // Non-vacuity: the document really carries the keys this row is about. |
| 127 | + expect(Object.keys(doc)).toEqual(expect.arrayContaining(['formType', 'defaultTab', 'sections'])); |
| 128 | + const strict = StrictAnyComponentSchema.safeParse(doc); |
| 129 | + expect(strict.success, JSON.stringify(strict.error?.issues)).toBe(true); |
| 130 | + }); |
| 131 | + |
| 132 | + it('`open` and `submitHandler` stay out of the mirror until their routes are ruled', () => { |
| 133 | + const shape = ObjectFormMirror.shape as Record<string, unknown>; |
| 134 | + expect('open' in shape).toBe(false); |
| 135 | + expect('submitHandler' in shape).toBe(false); |
| 136 | + }); |
| 137 | +}); |
0 commit comments