|
| 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 3 — the declared-but-unmirrored keys this round closed on |
| 9 | + * four pairs, and the `page` spelling it retired. |
| 10 | + * |
| 11 | + * ## What moved |
| 12 | + * |
| 13 | + * `UnmirroredDeclared` in `zod-mirror-parity.test.ts` recorded keys the |
| 14 | + * TypeScript face invites an author to write and the zod mirror had never heard |
| 15 | + * of. Two faces paid for each one: |
| 16 | + * |
| 17 | + * - the TOLERANT face (`BaseSchema` is `.passthrough()`) kept any value at the |
| 18 | + * key unexamined, so a wrong-typed value parsed green; |
| 19 | + * - the STRICT authoring face (`StrictAnyComponentSchema`, derived from the |
| 20 | + * mirrors) refused the key outright although `tsc` accepted it. |
| 21 | + * |
| 22 | + * Each key below was measured READ before it was mirrored (a type-checker census |
| 23 | + * over every package's sources; the readings are in objectui#6152's round-3 |
| 24 | + * report). This file pins the result, not the census: |
| 25 | + * |
| 26 | + * - a VALID value parses on BOTH faces (the strict leg is the one that moved: |
| 27 | + * refused, now accepted); |
| 28 | + * - a WRONG-TYPED value is refused AT THE KEY on the tolerant face (the leg that |
| 29 | + * moved the other way: kept unexamined, now refused). |
| 30 | + * |
| 31 | + * `PaginationSchema.page` is the other half of the pagination row: a second |
| 32 | + * spelling of `currentPage`, RETIRED on both faces in the same change, so it is |
| 33 | + * refused by name on the mirror and is a `tsc` error on the interface. |
| 34 | + */ |
| 35 | +import { describe, it, expect } from 'vitest'; |
| 36 | +import { readFileSync } from 'node:fs'; |
| 37 | +import { fileURLToPath } from 'node:url'; |
| 38 | +import { dirname, join } from 'node:path'; |
| 39 | + |
| 40 | +import type { PaginationSchema as TsPaginationSchema } from '../navigation'; |
| 41 | +import { PaginationSchema as PaginationMirror } from '../zod/navigation.zod.js'; |
| 42 | +import { FormSchema as FormMirror } from '../zod/form.zod.js'; |
| 43 | +import { ReportComponentSchema as ReportMirror } from '../zod/reports.zod.js'; |
| 44 | +import { DetailViewSchema as DetailViewMirror } from '../zod/views.zod.js'; |
| 45 | +import { AnyComponentSchema, StrictAnyComponentSchema } from '../zod/index.zod.js'; |
| 46 | + |
| 47 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 48 | +const REPO_ROOT = join(HERE, '..', '..', '..', '..'); |
| 49 | + |
| 50 | +type Row = { key: string; valid: unknown; wrong: unknown }; |
| 51 | +type Pair = { |
| 52 | + name: string; |
| 53 | + mirror: { shape: Record<string, unknown>; safeParse: (v: unknown) => { success: boolean; error?: { issues: Array<{ path: PropertyKey[] }> } } }; |
| 54 | + base: Record<string, unknown>; |
| 55 | + rows: Row[]; |
| 56 | +}; |
| 57 | + |
| 58 | +const PAIRS: Pair[] = [ |
| 59 | + { |
| 60 | + name: 'pagination', |
| 61 | + mirror: PaginationMirror as unknown as Pair['mirror'], |
| 62 | + base: { type: 'pagination', totalPages: 10 }, |
| 63 | + rows: [{ key: 'currentPage', valid: 3, wrong: 'three' }], |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'form', |
| 67 | + mirror: FormMirror as unknown as Pair['mirror'], |
| 68 | + base: { type: 'form', fields: [{ name: 'first_name', label: 'First name' }, { name: 'last_name', label: 'Last name' }] }, |
| 69 | + rows: [ |
| 70 | + { key: 'mobileStickyActions', valid: true, wrong: 'yes' }, |
| 71 | + { key: 'fieldContainerClass', valid: 'grid grid-cols-2 gap-4', wrong: 2 }, |
| 72 | + { |
| 73 | + key: 'fieldTabs', |
| 74 | + valid: [ |
| 75 | + { key: 'a', label: 'A', fields: ['first_name'] }, |
| 76 | + { key: 'b', fields: ['last_name'], containerClass: 'space-y-2', visibleWhen: { dialect: 'cel', source: 'record.kind == "b2b"' } }, |
| 77 | + ], |
| 78 | + wrong: [{ label: 'no key', fields: ['first_name'] }], |
| 79 | + }, |
| 80 | + { key: 'defaultFieldTab', valid: 'b', wrong: 2 }, |
| 81 | + { key: 'fieldTabsPosition', valid: 'left', wrong: 'middle' }, |
| 82 | + { |
| 83 | + key: 'fieldPanes', |
| 84 | + valid: [ |
| 85 | + { key: 'left', fields: ['first_name'], defaultSize: 40, minSize: 20 }, |
| 86 | + { key: 'right', fields: ['last_name'] }, |
| 87 | + ], |
| 88 | + wrong: [{ key: 'left', fields: 'first_name' }], |
| 89 | + }, |
| 90 | + { key: 'fieldPanesOrientation', valid: 'vertical', wrong: 'diagonal' }, |
| 91 | + { key: 'fieldPanesResizable', valid: false, wrong: 'false' }, |
| 92 | + ], |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'report', |
| 96 | + mirror: ReportMirror as unknown as Pair['mirror'], |
| 97 | + base: { type: 'report', title: 'Pipeline' }, |
| 98 | + rows: [ |
| 99 | + { |
| 100 | + key: 'conditionalFormatting', |
| 101 | + valid: [{ field: 'amount', operator: 'greater_than', value: 1000, backgroundColor: '#fee2e2', textColor: '#991b1b' }], |
| 102 | + wrong: [{ field: 'amount', operator: 'between', value: 1000 }], |
| 103 | + }, |
| 104 | + ], |
| 105 | + }, |
| 106 | + { |
| 107 | + name: 'detail-view', |
| 108 | + mirror: DetailViewMirror as unknown as Pair['mirror'], |
| 109 | + base: { type: 'detail-view', objectName: 'account' }, |
| 110 | + rows: [ |
| 111 | + { key: 'primaryField', valid: 'name', wrong: 7 }, |
| 112 | + { key: 'summaryFields', valid: ['status', 'owner'], wrong: 'status' }, |
| 113 | + { key: 'autoTabs', valid: true, wrong: 'on' }, |
| 114 | + { key: 'defaultTab', valid: 'activity', wrong: 1 }, |
| 115 | + { |
| 116 | + key: 'sectionGroups', |
| 117 | + valid: [{ title: 'Company', collapsible: true, defaultCollapsed: false, sections: [{ title: 'Basics', fields: [{ name: 'name' }] }] }], |
| 118 | + wrong: [{ description: 'no title', sections: [] }], |
| 119 | + }, |
| 120 | + { |
| 121 | + key: 'highlightFields', |
| 122 | + valid: [{ name: 'industry', label: 'Industry', type: 'select', icon: 'Factory', readonly: true }], |
| 123 | + wrong: [{ name: 'industry', label: 'Industry', type: 'hologram' }], |
| 124 | + }, |
| 125 | + ], |
| 126 | + }, |
| 127 | +]; |
| 128 | + |
| 129 | +const ROWS = PAIRS.flatMap((pair) => pair.rows.map((row) => ({ ...row, pair }))); |
| 130 | + |
| 131 | +describe('objectui#6152 round 3 — declared keys the mirrors now declare', () => { |
| 132 | + it.each(PAIRS)('CONTROL: the minimal `$name` document parses on both faces', ({ base }) => { |
| 133 | + expect(AnyComponentSchema.safeParse(base).success).toBe(true); |
| 134 | + const strict = StrictAnyComponentSchema.safeParse(base); |
| 135 | + expect(strict.success, JSON.stringify(strict.error?.issues)).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + it.each(ROWS)('`$pair.name`.`$key` is a member of the mirror', ({ key, pair }) => { |
| 139 | + expect(key in pair.mirror.shape).toBe(true); |
| 140 | + }); |
| 141 | + |
| 142 | + it.each(ROWS)('an authored `$pair.name`.`$key` parses on the strict face and the tolerant face', ({ key, valid, pair }) => { |
| 143 | + const doc = { ...pair.base, [key]: valid }; |
| 144 | + const strict = StrictAnyComponentSchema.safeParse(doc); |
| 145 | + expect(strict.success, JSON.stringify(strict.error?.issues)).toBe(true); |
| 146 | + const tolerant = AnyComponentSchema.safeParse(doc); |
| 147 | + expect(tolerant.success, JSON.stringify(tolerant.error?.issues)).toBe(true); |
| 148 | + }); |
| 149 | + |
| 150 | + it.each(ROWS)('a wrong-typed `$pair.name`.`$key` is refused at the key on the tolerant face', ({ key, wrong, pair }) => { |
| 151 | + const parsed = pair.mirror.safeParse({ ...pair.base, [key]: wrong }); |
| 152 | + expect(parsed.success).toBe(false); |
| 153 | + const issues = parsed.error?.issues ?? []; |
| 154 | + expect(issues.length).toBeGreaterThan(0); |
| 155 | + for (const issue of issues) expect(issue.path[0]).toBe(key); |
| 156 | + }); |
| 157 | + |
| 158 | + it('a `report-viewer` node judges the rules on the report it carries — the reader `ReportViewer` evaluates', () => { |
| 159 | + const rules = [{ field: 'amount', operator: 'less_than', value: 0, textColor: '#b91c1c' }]; |
| 160 | + const doc = { type: 'report-viewer', report: { type: 'report', title: 'Pipeline', conditionalFormatting: rules } }; |
| 161 | + const strict = StrictAnyComponentSchema.safeParse(doc); |
| 162 | + expect(strict.success, JSON.stringify(strict.error?.issues)).toBe(true); |
| 163 | + const wrong = AnyComponentSchema.safeParse({ ...doc, report: { ...doc.report, conditionalFormatting: [{ ...rules[0], operator: 'between' }] } }); |
| 164 | + expect(wrong.success).toBe(false); |
| 165 | + }); |
| 166 | + |
| 167 | + it('the catalog pagination document objectui#5250 M3 charged with `currentPage` now parses on the strict face', () => { |
| 168 | + const doc = JSON.parse(readFileSync(join(REPO_ROOT, 'examples/schema-catalog/src/schemas/components-basic-pagination/basic-pagination.json'), 'utf8')); |
| 169 | + // Non-vacuity: the document really authors the key this row is about. |
| 170 | + expect(doc).toMatchObject({ type: 'pagination', currentPage: 1 }); |
| 171 | + const strict = StrictAnyComponentSchema.safeParse(doc); |
| 172 | + expect(strict.success, JSON.stringify(strict.error?.issues)).toBe(true); |
| 173 | + }); |
| 174 | +}); |
| 175 | + |
| 176 | +describe('objectui#6152 round 3 — `page` is a RETIRED second spelling of `currentPage`', () => { |
| 177 | + const BASE = { type: 'pagination', totalPages: 10 } as const; |
| 178 | + |
| 179 | + it('stays a member of the mirror, so an authored value is refused by name rather than kept', () => { |
| 180 | + expect('page' in PaginationMirror.shape).toBe(true); |
| 181 | + }); |
| 182 | + |
| 183 | + it.each([ |
| 184 | + ['the mirror', (v: unknown) => PaginationMirror.safeParse(v)], |
| 185 | + ['the tolerant face', (v: unknown) => AnyComponentSchema.safeParse(v)], |
| 186 | + ['the strict face', (v: unknown) => StrictAnyComponentSchema.safeParse(v)], |
| 187 | + ] as const)('an authored `page` is refused at the key on %s, and the refusal names `currentPage`', (_face, parse) => { |
| 188 | + const parsed = parse({ ...BASE, page: 2 }); |
| 189 | + expect(parsed.success).toBe(false); |
| 190 | + const atPage = (parsed.error?.issues ?? []).filter((issue) => issue.path[0] === 'page'); |
| 191 | + expect(atPage.length, JSON.stringify(parsed.error?.issues)).toBeGreaterThan(0); |
| 192 | + expect(atPage.some((issue) => issue.message.includes('`currentPage`')), JSON.stringify(atPage)).toBe(true); |
| 193 | + }); |
| 194 | + |
| 195 | + it('is a `tsc` error on the interface, while `currentPage` is the live spelling', () => { |
| 196 | + // The directive is real enforcement: this package type-checks its tests |
| 197 | + // (`tsconfig.test.json`), so re-declaring `page` fails on the unused directive. |
| 198 | + // @ts-expect-error `page` is RETIRED (objectui#6152) — rename the key to `currentPage` |
| 199 | + const retired: TsPaginationSchema = { type: 'pagination', totalPages: 10, page: 2 }; |
| 200 | + const live: TsPaginationSchema = { type: 'pagination', totalPages: 10, currentPage: 2 }; |
| 201 | + expect(retired.type).toBe('pagination'); |
| 202 | + expect(live.currentPage).toBe(2); |
| 203 | + }); |
| 204 | +}); |
0 commit comments