|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `judgePicklistReferences` — the walk `os validate` (step 2d) and `os build` |
| 5 | + * (step 3a-bis) run over a field's `picklist` reference. |
| 6 | + * |
| 7 | + * Pinned here: which references are judged (the load path's reading — the top |
| 8 | + * level when there is no `packages[]`, each body's own otherwise), what they |
| 9 | + * resolve against (every picklist the stack declares), and which of the two |
| 10 | + * verdicts an unresolved one gets (refused when the declaring package depends |
| 11 | + * on nothing outside the stack; an `info` notice when it does). Asserted by |
| 12 | + * rule id, severity, the field named in `where`, the list named in `message` |
| 13 | + * and the `path` — never by the sentence around them. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, expect, it } from 'vitest'; |
| 17 | +import { |
| 18 | + judgePicklistReferences, |
| 19 | + PICKLIST_REFERENCE_UNKNOWN, |
| 20 | + PICKLIST_REFERENCE_UNVERIFIED, |
| 21 | +} from './picklist-references.js'; |
| 22 | + |
| 23 | +const manifest = (id: string, extra: Record<string, unknown> = {}) => ({ |
| 24 | + id, name: id, version: '1.0.0', type: 'app', ...extra, |
| 25 | +}); |
| 26 | + |
| 27 | +const industry = { |
| 28 | + name: 'industry', |
| 29 | + label: 'Industry', |
| 30 | + options: [{ label: 'Technology', value: 'technology' }], |
| 31 | +}; |
| 32 | + |
| 33 | +const account = (picklist: string) => ({ |
| 34 | + name: 'pk_account', |
| 35 | + label: 'Account', |
| 36 | + fields: { |
| 37 | + name: { type: 'text', label: 'Name' }, |
| 38 | + industry: { type: 'select', label: 'Industry', picklist }, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +describe('judgePicklistReferences — a one-package stack', () => { |
| 43 | + it('CONTROL: a field naming a picklist the stack declares is neither refused nor reported', () => { |
| 44 | + expect(judgePicklistReferences({ |
| 45 | + manifest: manifest('com.example.pk'), |
| 46 | + picklists: [industry], |
| 47 | + objects: [account('industry')], |
| 48 | + })).toEqual({ refusals: [], notices: [] }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('CONTROL: a select with inline options and no `picklist` is not this judge\'s business', () => { |
| 52 | + expect(judgePicklistReferences({ |
| 53 | + manifest: manifest('com.example.pk'), |
| 54 | + objects: [{ |
| 55 | + name: 'pk_account', |
| 56 | + fields: { tier: { type: 'select', options: [{ label: 'Gold', value: 'gold' }] } }, |
| 57 | + }], |
| 58 | + })).toEqual({ refusals: [], notices: [] }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('refuses a `picklist` that names no picklist in the stack, naming the field and the list', () => { |
| 62 | + const { refusals, notices } = judgePicklistReferences({ |
| 63 | + manifest: manifest('com.example.pk'), |
| 64 | + picklists: [industry], |
| 65 | + objects: [account('industy')], |
| 66 | + }); |
| 67 | + expect(notices).toEqual([]); |
| 68 | + expect(refusals).toHaveLength(1); |
| 69 | + expect(refusals[0]).toMatchObject({ |
| 70 | + severity: 'error', |
| 71 | + rule: PICKLIST_REFERENCE_UNKNOWN, |
| 72 | + where: 'field "pk_account.industry"', |
| 73 | + path: 'objects[0].fields.industry.picklist', |
| 74 | + }); |
| 75 | + expect(refusals[0].message).toContain("'industy'"); |
| 76 | + // The lists it could have meant are named, so the typo is visible. |
| 77 | + expect(refusals[0].message).toContain("'industry'"); |
| 78 | + }); |
| 79 | + |
| 80 | + it('refuses a dangling reference in a stack that declares no picklist at all', () => { |
| 81 | + const { refusals } = judgePicklistReferences({ |
| 82 | + manifest: manifest('com.example.pk'), |
| 83 | + objects: [account('industry')], |
| 84 | + }); |
| 85 | + expect(refusals.map((r) => r.rule)).toEqual([PICKLIST_REFERENCE_UNKNOWN]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('judges the fields an `objectExtensions` entry merges into its target too', () => { |
| 89 | + const { refusals } = judgePicklistReferences({ |
| 90 | + manifest: manifest('com.example.pk'), |
| 91 | + picklists: [industry], |
| 92 | + objectExtensions: [{ extend: 'crm_account', fields: { segment: { type: 'select', picklist: 'segment' } } }], |
| 93 | + }); |
| 94 | + expect(refusals).toHaveLength(1); |
| 95 | + expect(refusals[0]).toMatchObject({ |
| 96 | + where: 'field "crm_account.segment"', |
| 97 | + path: 'objectExtensions[0].fields.segment.picklist', |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('REPORTS, never refuses, when the stack depends on a package it does not carry', () => { |
| 102 | + const { refusals, notices } = judgePicklistReferences({ |
| 103 | + manifest: manifest('com.example.pk', { dependencies: { 'com.acme.crm': '^1.0.0' } }), |
| 104 | + objects: [account('industry')], |
| 105 | + }); |
| 106 | + expect(refusals).toEqual([]); |
| 107 | + expect(notices).toHaveLength(1); |
| 108 | + expect(notices[0]).toMatchObject({ |
| 109 | + severity: 'info', |
| 110 | + rule: PICKLIST_REFERENCE_UNVERIFIED, |
| 111 | + path: 'objects[0].fields.industry.picklist', |
| 112 | + }); |
| 113 | + // The notice is printed without its `where`, so it names the field, the |
| 114 | + // list and the dependency it could not read. |
| 115 | + expect(notices[0].message).toContain('pk_account.industry'); |
| 116 | + expect(notices[0].message).toContain("'industry'"); |
| 117 | + expect(notices[0].message).toContain("'com.acme.crm'"); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('judgePicklistReferences — a `packages[]` stack', () => { |
| 122 | + it('resolves a picklist a SIBLING package in the same artifact declares', () => { |
| 123 | + expect(judgePicklistReferences({ |
| 124 | + packages: [ |
| 125 | + { manifest: { ...manifest('com.example.core'), picklists: [industry] } }, |
| 126 | + { |
| 127 | + manifest: { |
| 128 | + ...manifest('com.example.orders', { dependencies: { 'com.example.core': '^1.0.0' } }), |
| 129 | + objects: [account('industry')], |
| 130 | + }, |
| 131 | + }, |
| 132 | + ], |
| 133 | + })).toEqual({ refusals: [], notices: [] }); |
| 134 | + }); |
| 135 | + |
| 136 | + it('refuses when every declared dependency is inside the artifact — none of them declares the list', () => { |
| 137 | + const { refusals, notices } = judgePicklistReferences({ |
| 138 | + packages: [ |
| 139 | + { manifest: { ...manifest('com.example.core'), picklists: [industry] } }, |
| 140 | + { |
| 141 | + manifest: { |
| 142 | + ...manifest('com.example.orders', { dependencies: { 'com.example.core': '^1.0.0' } }), |
| 143 | + objects: [account('region')], |
| 144 | + }, |
| 145 | + }, |
| 146 | + ], |
| 147 | + }); |
| 148 | + expect(notices).toEqual([]); |
| 149 | + expect(refusals).toHaveLength(1); |
| 150 | + expect(refusals[0]).toMatchObject({ |
| 151 | + rule: PICKLIST_REFERENCE_UNKNOWN, |
| 152 | + path: 'packages[1].manifest.objects[0].fields.industry.picklist', |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('reads the declaring package\'s OWN dependencies: an outside dependency of a sibling does not soften it', () => { |
| 157 | + const { refusals, notices } = judgePicklistReferences({ |
| 158 | + packages: [ |
| 159 | + { manifest: { ...manifest('com.example.core', { dependencies: { 'com.acme.crm': '^1.0.0' } }) } }, |
| 160 | + { manifest: { ...manifest('com.example.orders'), objects: [account('region')] } }, |
| 161 | + { |
| 162 | + manifest: { |
| 163 | + ...manifest('com.example.billing', { dependencies: { 'com.acme.crm': '^1.0.0' } }), |
| 164 | + objects: [account('segment')], |
| 165 | + }, |
| 166 | + }, |
| 167 | + ], |
| 168 | + }); |
| 169 | + expect(refusals.map((r) => r.path)).toEqual(['packages[1].manifest.objects[0].fields.industry.picklist']); |
| 170 | + expect(notices.map((n) => n.path)).toEqual(['packages[2].manifest.objects[0].fields.industry.picklist']); |
| 171 | + }); |
| 172 | +}); |
0 commit comments