|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #26886 — load-time diagnostics judge a picklist-bound field as its author |
| 5 | + * wrote it, never with the `options` the runtime resolved onto it. |
| 6 | + * |
| 7 | + * Every object read serves a field that authors `picklist: '<name>'` with the |
| 8 | + * list's options written beside it, and the field schema refuses `picklist` |
| 9 | + * and `options` together. Judging the served copy flagged every shared-picklist |
| 10 | + * field of every app. The resolution marks the array it derived |
| 11 | + * (`markResolvedPicklistOptions`), and only a marked array is left out of the |
| 12 | + * judgement: |
| 13 | + * |
| 14 | + * - a field naming only a picklist, served with resolved options: clean; |
| 15 | + * - a field that AUTHORS both keys (an unmarked array): still flagged, with |
| 16 | + * the rule's own message, at `fields.NAME.options`; |
| 17 | + * - a field with inline `options` only: clean, before and after. |
| 18 | + * |
| 19 | + * The mark never reaches a response or a memo key: a marked document and an |
| 20 | + * unmarked one with the same JSON are two verdicts. |
| 21 | + */ |
| 22 | +import { describe, expect, it } from 'vitest'; |
| 23 | +import { |
| 24 | + computeMetadataDiagnostics, |
| 25 | + decorateMetadataItem, |
| 26 | + isResolvedPicklistOptions, |
| 27 | + markResolvedPicklistOptions, |
| 28 | +} from './metadata-diagnostics.js'; |
| 29 | + |
| 30 | +const BOTH_KEYS = '`picklist` and `options` cannot both be declared'; |
| 31 | + |
| 32 | +const resolved = () => markResolvedPicklistOptions([{ label: 'Tech', value: 'tech' }]); |
| 33 | +const authored = () => [{ label: 'Tech', value: 'tech' }]; |
| 34 | + |
| 35 | +function objectWith(fields: Record<string, unknown>) { |
| 36 | + return { name: 'pk_probe', label: 'Probe', fields: { name: { name: 'name', type: 'text', label: 'Name' }, ...fields } }; |
| 37 | +} |
| 38 | + |
| 39 | +const industry = (options: unknown[]) => |
| 40 | + ({ name: 'industry', type: 'select', label: 'Industry', picklist: 'industry', options }); |
| 41 | + |
| 42 | +function bothKeysPaths(diagnostics: ReturnType<typeof computeMetadataDiagnostics>): string[] { |
| 43 | + return (diagnostics?.errors ?? []).filter((e) => e.message.startsWith(BOTH_KEYS)).map((e) => e.path); |
| 44 | +} |
| 45 | + |
| 46 | +describe('diagnostics — options resolved from a shared picklist', () => { |
| 47 | + it('a field naming only a picklist, served with its resolved options, is clean', () => { |
| 48 | + expect(computeMetadataDiagnostics('object', objectWith({ industry: industry(resolved()) }))).toEqual({ valid: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('a field that authors `options` beside `picklist` is still flagged, at its options path', () => { |
| 52 | + const diagnostics = computeMetadataDiagnostics('object', objectWith({ industry: industry(authored()) })); |
| 53 | + expect(diagnostics?.valid).toBe(false); |
| 54 | + expect(bothKeysPaths(diagnostics)).toEqual(['fields.industry.options']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('a field with inline `options` only is clean', () => { |
| 58 | + const inline = { name: 'stage', type: 'select', label: 'Stage', options: [{ label: 'Open', value: 'open' }] }; |
| 59 | + expect(computeMetadataDiagnostics('object', objectWith({ stage: inline }))).toEqual({ valid: true }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('a marked array on a field with no `picklist` is judged as written', () => { |
| 63 | + // A one-character option value is refused; left out, nothing would be. |
| 64 | + const stray = { name: 'stage', type: 'select', label: 'Stage', options: markResolvedPicklistOptions([{ label: 'X', value: 'x' }]) }; |
| 65 | + const diagnostics = computeMetadataDiagnostics('object', objectWith({ stage: stray })); |
| 66 | + expect(diagnostics?.errors?.map((e) => e.path)).toEqual(['fields.stage.options.0.value']); |
| 67 | + }); |
| 68 | + |
| 69 | + it('a marked and an unmarked document with the same JSON are two verdicts, in either order', () => { |
| 70 | + const marked = objectWith({ industry: industry(resolved()) }); |
| 71 | + const unmarked = objectWith({ industry: industry(authored()) }); |
| 72 | + expect(JSON.stringify(marked)).toBe(JSON.stringify(unmarked)); |
| 73 | + expect(bothKeysPaths(computeMetadataDiagnostics('object', unmarked))).toEqual(['fields.industry.options']); |
| 74 | + expect(computeMetadataDiagnostics('object', marked)).toEqual({ valid: true }); |
| 75 | + expect(bothKeysPaths(computeMetadataDiagnostics('object', unmarked))).toEqual(['fields.industry.options']); |
| 76 | + }); |
| 77 | + |
| 78 | + it('the served item keeps both keys and carries no trace of the mark', () => { |
| 79 | + const served = decorateMetadataItem('object', objectWith({ industry: industry(resolved()) })) as any; |
| 80 | + expect(served._diagnostics).toEqual({ valid: true }); |
| 81 | + expect(served.fields.industry).toMatchObject({ picklist: 'industry', options: [{ label: 'Tech', value: 'tech' }] }); |
| 82 | + expect(JSON.parse(JSON.stringify(served.fields.industry))).toEqual(industry(authored())); |
| 83 | + expect(Object.keys(served.fields.industry.options)).toEqual(['0']); |
| 84 | + }); |
| 85 | + |
| 86 | + it('a copy of a marked array is unmarked: the failure direction is the old error, never a missed one', () => { |
| 87 | + const options = resolved(); |
| 88 | + expect(isResolvedPicklistOptions(options)).toBe(true); |
| 89 | + expect(isResolvedPicklistOptions(options.map((o) => ({ ...o })))).toBe(false); |
| 90 | + expect(isResolvedPicklistOptions(JSON.parse(JSON.stringify(options)))).toBe(false); |
| 91 | + expect(isResolvedPicklistOptions([...options])).toBe(false); |
| 92 | + }); |
| 93 | +}); |
0 commit comments