|
| 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 | + * objectui#11405 — a bind-only `list` is accepted: `ListSchema.items` is |
| 11 | + * optional on both published faces, and the zod face requires AT LEAST ONE of |
| 12 | + * `bind` / `items`. |
| 13 | + * |
| 14 | + * ## The defect |
| 15 | + * |
| 16 | + * `list.tsx` reads `useDataScope(schema.bind)` first and falls back to |
| 17 | + * `schema.items` when the bound value is not an array, so a `list` carrying only |
| 18 | + * `bind` draws one entry per element of the bound array. Both published faces |
| 19 | + * nevertheless REQUIRED `items`: `safeValidateSchema({ type: 'list', bind: |
| 20 | + * 'customerNames' })` was refused with `invalid_type` at `items`, the strict face |
| 21 | + * agreed, and the TypeScript face refused the same literal (`TS2741`). Four |
| 22 | + * marked fences in two published guides author exactly that shape, and their |
| 23 | + * marker only checks that they parse, so nothing noticed. |
| 24 | + * |
| 25 | + * Triage's ruling (comment 5941529511): the arm follows the renderer, and the |
| 26 | + * guides are not edited. The rule is at least one and ⛔ not exactly one, |
| 27 | + * because `items` is the renderer's real fallback beside `bind`. |
| 28 | + * |
| 29 | + * ## What this file pins |
| 30 | + * |
| 31 | + * - the zod face, through the arm itself, through `safeValidateSchema` (the |
| 32 | + * function `objectui validate` calls) and through the strict face: a bind-only |
| 33 | + * `list`, an items-only one and one carrying both are accepted, and one with |
| 34 | + * neither is refused ONCE, at the node, keyed `LIST_ENTRIES_REQUIRED`; |
| 35 | + * - the TypeScript face: the bind-only literal compiles. That half is judged by |
| 36 | + * `tsc -p tsconfig.test.json` (`pnpm --filter @object-ui/types type-check`), |
| 37 | + * not by vitest, which strips types. The TypeScript face cannot spell "at |
| 38 | + * least one" with an optional member, so a literal with neither key still |
| 39 | + * compiles; the rule lives on the mirror, as it does for `object-kanban` |
| 40 | + * (objectui#7780); |
| 41 | + * - the guides: every marked fence that authors a bind-only `list` is READ OFF |
| 42 | + * DISK with `scanSkillFences`, the reader the marker's own gate uses, and fed |
| 43 | + * to `safeValidateSchema`. A copy here would not notice a guide change. A fence |
| 44 | + * the guide itself marks ❌ is wrong for another reason (its `children` and |
| 45 | + * its `${item.name}`), so for it this file pins only that `items` is no longer |
| 46 | + * what refuses it. |
| 47 | + */ |
| 48 | + |
| 49 | +import { describe, it, expect } from 'vitest'; |
| 50 | +import { readFileSync } from 'node:fs'; |
| 51 | +import { dirname, resolve } from 'node:path'; |
| 52 | +import { fileURLToPath } from 'node:url'; |
| 53 | +// @ts-expect-error -- plain-JS shared helper, intentionally untyped (`allowJs: false`) |
| 54 | +import { scanSkillFences as scanUntyped, stripJsonComments as stripUntyped } from '../../../../scripts/check-skill-examples.mjs'; |
| 55 | +import { ListSchema, safeValidateSchema, StrictAnyComponentSchema } from '../zod/index.zod'; |
| 56 | +import type { ListSchema as TsListSchema } from '../data-display'; |
| 57 | + |
| 58 | +/** Local annotations, since the import above is untyped — the call sites stay checked. */ |
| 59 | +type ScannedFence = { kind: 'ts' | 'json'; language: string; fenceLine: number; body: string; marked: boolean }; |
| 60 | +const scanSkillFences: (source: string) => { fences: ScannedFence[]; orphans: number[] } = scanUntyped; |
| 61 | +const stripJsonComments: (source: string) => string = stripUntyped; |
| 62 | + |
| 63 | +/** Rooted at THIS file, never at `process.cwd()`. */ |
| 64 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 65 | +const REPO_ROOT = resolve(HERE, '..', '..', '..', '..'); |
| 66 | + |
| 67 | +const GUIDES = [ |
| 68 | + 'skills/objectui/guides/data-integration.md', |
| 69 | + 'skills/objectui/guides/schema-expressions.md', |
| 70 | +] as const; |
| 71 | + |
| 72 | +/* ── The TypeScript face (judged by `tsc -p tsconfig.test.json`) ── */ |
| 73 | + |
| 74 | +// Refused with TS2741 (`items` is missing) before objectui#11405. |
| 75 | +const BIND_ONLY: TsListSchema = { type: 'list', bind: 'customerNames' }; |
| 76 | +const ITEMS_ONLY: TsListSchema = { type: 'list', items: [{ content: 'Ada' }] }; |
| 77 | +const BOTH: TsListSchema = { type: 'list', bind: 'rows', items: [{ content: 'Ada' }] }; |
| 78 | + |
| 79 | +type Issue = { code: string; path: PropertyKey[]; params?: Record<string, unknown> }; |
| 80 | + |
| 81 | +/** The issues a parse raised, or `[]` when it passed. */ |
| 82 | +function issuesOf(result: { success: boolean; error?: { issues: readonly unknown[] } }): Issue[] { |
| 83 | + return result.success ? [] : ((result.error?.issues ?? []) as Issue[]); |
| 84 | +} |
| 85 | + |
| 86 | +/** Every face a node goes through: the arm, the published validator, the strict face. */ |
| 87 | +const FACES = { |
| 88 | + arm: (node: unknown) => ListSchema.safeParse(node), |
| 89 | + safeValidateSchema: (node: unknown) => safeValidateSchema(node), |
| 90 | + strict: (node: unknown) => StrictAnyComponentSchema.safeParse(node), |
| 91 | +} as const; |
| 92 | + |
| 93 | +describe('a `list` needs at least one of `bind` / `items` (objectui#11405)', () => { |
| 94 | + for (const [face, parse] of Object.entries(FACES)) { |
| 95 | + it(`${face}: bind-only, items-only and both are accepted`, () => { |
| 96 | + for (const node of [BIND_ONLY, ITEMS_ONLY, BOTH]) { |
| 97 | + expect(issuesOf(parse(node)), JSON.stringify(node)).toEqual([]); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + it(`${face}: a list with neither is refused once, at the node, keyed LIST_ENTRIES_REQUIRED`, () => { |
| 102 | + const issues = issuesOf(parse({ type: 'list', ordered: true })); |
| 103 | + expect(issues).toHaveLength(1); |
| 104 | + expect(issues[0]).toMatchObject({ code: 'custom', path: [], params: { code: 'LIST_ENTRIES_REQUIRED' } }); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + it('the rule is reported BESIDE an unrelated fault on the same node, not hidden by it', () => { |
| 109 | + const issues = issuesOf(safeValidateSchema({ type: 'list', ordered: 'yes' })); |
| 110 | + expect(issues.map((i) => [i.code, i.path.join('.')])).toEqual([ |
| 111 | + ['invalid_type', 'ordered'], |
| 112 | + ['custom', ''], |
| 113 | + ]); |
| 114 | + }); |
| 115 | + |
| 116 | + it('a `bind` that fails its own type is refused at `bind`, and the rule does not fire twice', () => { |
| 117 | + const issues = issuesOf(safeValidateSchema({ type: 'list', bind: 42 })); |
| 118 | + expect(issues.map((i) => [i.code, i.path.join('.')])).toEqual([['invalid_type', 'bind']]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('a nested list with neither is refused at its own path', () => { |
| 122 | + const issues = issuesOf(safeValidateSchema({ type: 'div', children: [{ type: 'list' }] })); |
| 123 | + expect(issues).toHaveLength(1); |
| 124 | + expect(issues[0]).toMatchObject({ code: 'custom', path: ['children', 0], params: { code: 'LIST_ENTRIES_REQUIRED' } }); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +/* ── The guides' marked fences, read off disk ── */ |
| 129 | + |
| 130 | +type Fence = { guide: string; fenceLine: number; teachesWrong: boolean; node: Record<string, unknown> }; |
| 131 | + |
| 132 | +/** Every marked json/jsonc fence in the two guides whose root is a `list` with `bind` and no `items`. */ |
| 133 | +function bindOnlyListFences(): Fence[] { |
| 134 | + const out: Fence[] = []; |
| 135 | + for (const guide of GUIDES) { |
| 136 | + const { fences } = scanSkillFences(readFileSync(resolve(REPO_ROOT, guide), 'utf8')); |
| 137 | + for (const fence of fences) { |
| 138 | + if (!fence.marked || fence.kind !== 'json') continue; |
| 139 | + // The marker's own gate already requires every marked fence to parse. |
| 140 | + const node = JSON.parse(fence.language === 'jsonc' ? stripJsonComments(fence.body) : fence.body); |
| 141 | + if (!node || typeof node !== 'object' || node.type !== 'list') continue; |
| 142 | + if (node.bind === undefined || node.items !== undefined) continue; |
| 143 | + out.push({ guide, fenceLine: fence.fenceLine, teachesWrong: fence.body.includes('❌'), node }); |
| 144 | + } |
| 145 | + } |
| 146 | + return out; |
| 147 | +} |
| 148 | + |
| 149 | +describe('the guides\' bind-only `list` fences (objectui#11405)', () => { |
| 150 | + const fences = bindOnlyListFences(); |
| 151 | + |
| 152 | + it('COUNTER-PROBE: the reader reaches a bind-only list in each guide, and the ❌ example among them', () => { |
| 153 | + for (const guide of GUIDES) { |
| 154 | + expect(fences.filter((f) => f.guide === guide).length, guide).toBeGreaterThan(0); |
| 155 | + } |
| 156 | + expect(fences.some((f) => f.teachesWrong)).toBe(true); |
| 157 | + }); |
| 158 | + |
| 159 | + it('every fence taught as valid passes safeValidateSchema and the strict face', () => { |
| 160 | + const taught = fences.filter((f) => !f.teachesWrong); |
| 161 | + expect(taught.length).toBeGreaterThan(0); |
| 162 | + for (const { guide, fenceLine, node } of taught) { |
| 163 | + const where = `${guide}, fence opened on its line ${fenceLine}`; |
| 164 | + expect(issuesOf(safeValidateSchema(node)), where).toEqual([]); |
| 165 | + expect(issuesOf(StrictAnyComponentSchema.safeParse(node)), where).toEqual([]); |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + it('the ❌ example is still refused, and never for a missing `items`', () => { |
| 170 | + for (const { guide, fenceLine, node, teachesWrong } of fences) { |
| 171 | + if (!teachesWrong) continue; |
| 172 | + const where = `${guide}, fence opened on its line ${fenceLine}`; |
| 173 | + const issues = issuesOf(safeValidateSchema(node)); |
| 174 | + expect(issues.length, where).toBeGreaterThan(0); |
| 175 | + expect(issues.filter((i) => i.path[0] === 'items'), where).toEqual([]); |
| 176 | + expect(issues.filter((i) => i.params?.code === 'LIST_ENTRIES_REQUIRED'), where).toEqual([]); |
| 177 | + } |
| 178 | + }); |
| 179 | +}); |
0 commit comments