|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #24668 — the VIEW door against the WHOLE collection: a runtime write that |
| 5 | + * names no package (an overlay write) of a list view bound to an object that |
| 6 | + * exists nowhere is REFUSED `list-view-object-unknown`, once, at the binding. |
| 7 | + * |
| 8 | + * The overlay twin of #24563 (`runtime-gate.package-closure.test.ts`, the |
| 9 | + * closure verdict). With no closure in force the gate hands the rules every |
| 10 | + * object the host serves, so a binding missing from it exists nowhere — there |
| 11 | + * is no other package for it to come from. What these pins hold: |
| 12 | + * |
| 13 | + * - the refusal itself, on every rung the rule walks, at the binding key; |
| 14 | + * - a binding that resolves keeps its field verdict exactly; |
| 15 | + * - the CLI (`os validate` / `os build` / `os lint`) keeps skip 1 — its stack |
| 16 | + * is one app, and an unresolved binding may be a dependency's object; |
| 17 | + * - an EMPTY collection is no universe, so nothing is judged; |
| 18 | + * - a closure wins over the whole-collection statement; |
| 19 | + * - the differential charges a write only with its own finding: a stored |
| 20 | + * sibling's dangling binding cancels, including when the write creates the |
| 21 | + * object nearest to it (the suggestion rides the hint, not the message). |
| 22 | + * |
| 23 | + * Drafts are not a lint-layer concept: the draft door is pinned where it |
| 24 | + * lives, in `packages/metadata-protocol` (`protocol.runtime-authoring-gate.test.ts`). |
| 25 | + */ |
| 26 | +import { describe, expect, it } from 'vitest'; |
| 27 | +import { runAuthoringRules, splitBySeverity } from './authoring-rules.js'; |
| 28 | +import { validateReferenceIntegrity } from './reference-integrity-suite.js'; |
| 29 | +import { runRuntimeAuthoringRules, type RuntimePackageScope } from './runtime-gate.js'; |
| 30 | +import { |
| 31 | + LIST_VIEW_FIELD_UNKNOWN, |
| 32 | + LIST_VIEW_OBJECT_OUTSIDE_CLOSURE, |
| 33 | + LIST_VIEW_OBJECT_UNKNOWN, |
| 34 | + validateListViewFieldRefs, |
| 35 | +} from './validate-list-view-field-refs.js'; |
| 36 | + |
| 37 | +type AnyRec = Record<string, any>; |
| 38 | + |
| 39 | +const OWN = 'com.acme.crm'; |
| 40 | +const STRANGER = 'com.other.analytics'; |
| 41 | +const PLATFORM = 'com.objectstack.platform'; |
| 42 | + |
| 43 | +/** A minimal, spec-shaped object declaration (every one declares `name`). */ |
| 44 | +function obj(name: string, packageId?: string, extra: AnyRec = {}): AnyRec { |
| 45 | + return { |
| 46 | + name, |
| 47 | + label: name, |
| 48 | + sharingModel: 'private', |
| 49 | + fields: { name: { type: 'text', label: 'Name' } }, |
| 50 | + ...(packageId === undefined ? {} : { _packageId: packageId }), |
| 51 | + ...extra, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +/** The whole collection a host serves: two packages, a platform object, a provenance-less overlay row. */ |
| 56 | +const OBJECTS: readonly AnyRec[] = [ |
| 57 | + obj('crm_account', OWN), |
| 58 | + obj('showcase_task', STRANGER), |
| 59 | + obj('sys_audit_log', PLATFORM), |
| 60 | + obj('tenant_overlay'), |
| 61 | +]; |
| 62 | + |
| 63 | +/** |
| 64 | + * The card's body: a flattened list overlay — the shape `PUT /api/v1/meta/view` |
| 65 | + * carries — bound to `object`, one known and by default one unknown column. |
| 66 | + */ |
| 67 | +function listViewOn(object: string, columns: readonly string[] = ['name', 'zzz_nope']): AnyRec { |
| 68 | + return { |
| 69 | + name: `${object}.probe_list`, |
| 70 | + object, |
| 71 | + viewKind: 'list', |
| 72 | + label: 'Probe', |
| 73 | + type: 'grid', |
| 74 | + data: { provider: 'object', object }, |
| 75 | + columns: columns.map((field) => ({ field })), |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +const overlay = (item: AnyRec, objects: readonly AnyRec[] = OBJECTS) => |
| 80 | + runRuntimeAuthoringRules({ type: 'view', item, context: { objects } }); |
| 81 | +const rules = (fs: readonly AnyRec[]) => fs.map((f) => f.rule); |
| 82 | + |
| 83 | +describe('the view door against the whole collection (#24668)', () => { |
| 84 | + it('REFUSES a binding to an object that exists nowhere — once, at the binding, positions unjudged', () => { |
| 85 | + const result = overlay(listViewOn('nosuch_obj')); |
| 86 | + |
| 87 | + expect(result.advisories).toEqual([]); |
| 88 | + expect(result.errors).toHaveLength(1); |
| 89 | + expect(result.errors[0]).toMatchObject({ severity: 'error', rule: LIST_VIEW_OBJECT_UNKNOWN }); |
| 90 | + expect(result.errors[0].path).toMatch(/^views(\[0\]|\.nosuch_obj\.probe_list)\.data\.object$/); |
| 91 | + expect(result.errors[0].message).toContain('"nosuch_obj"'); |
| 92 | + // ⛔ One finding per list view: the unknown column is not judged against |
| 93 | + // an object there is no field map for. |
| 94 | + expect(rules(result.errors)).not.toContain(LIST_VIEW_FIELD_UNKNOWN); |
| 95 | + }); |
| 96 | + |
| 97 | + it('names the nearest object in the HINT, and leaves the message free of it', () => { |
| 98 | + const result = overlay(listViewOn('crm_acount')); |
| 99 | + |
| 100 | + expect(rules(result.errors)).toEqual([LIST_VIEW_OBJECT_UNKNOWN]); |
| 101 | + expect(result.errors[0].hint).toContain('Did you mean "crm_account"?'); |
| 102 | + expect(result.errors[0].message).not.toContain('crm_account"'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('refuses on every rung the rule walks, each at the key the binding was read from', () => { |
| 106 | + const rungs: Array<[string, AnyRec, RegExp]> = [ |
| 107 | + ['container `list`', { name: 'board', object: 'nosuch_obj', list: { type: 'grid', columns: [{ field: 'name' }] } }, /\.objectName$|\.object$/], |
| 108 | + ['container `list.data.object`', { name: 'board', object: 'crm_account', list: { type: 'grid', data: { provider: 'object', object: 'nosuch_obj' }, columns: [{ field: 'name' }] } }, /\.list\.data\.object$/], |
| 109 | + ['container `listViews.*`', { name: 'board', object: 'crm_account', listViews: { all: { type: 'grid', data: { provider: 'object', object: 'nosuch_obj' }, columns: [{ field: 'name' }] } } }, /\.listViews\.all\.data\.object$/], |
| 110 | + ['ViewItem record', { name: 'board', object: 'nosuch_obj', viewKind: 'list', config: { type: 'grid', columns: [{ field: 'name' }] } }, /\.object$/], |
| 111 | + ]; |
| 112 | + for (const [label, item, path] of rungs) { |
| 113 | + const result = overlay(item); |
| 114 | + expect(rules(result.errors), label).toEqual([LIST_VIEW_OBJECT_UNKNOWN]); |
| 115 | + expect(result.errors[0].path, label).toMatch(path); |
| 116 | + expect(result.advisories, label).toEqual([]); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + it('refuses an OBJECT write whose built-in list view retargets an object that exists nowhere', () => { |
| 121 | + const item = obj('crm_board', undefined, { |
| 122 | + listViews: { foreign: { label: 'Foreign', data: { provider: 'object', object: 'nosuch_obj' }, columns: ['name'] } }, |
| 123 | + }); |
| 124 | + const result = runRuntimeAuthoringRules({ type: 'object', item, context: { objects: OBJECTS } }); |
| 125 | + |
| 126 | + expect(rules(result.errors)).toEqual([LIST_VIEW_OBJECT_UNKNOWN]); |
| 127 | + expect(result.errors[0].path).toMatch(/listViews\.foreign\.data\.object$/); |
| 128 | + }); |
| 129 | + |
| 130 | + it('a binding that RESOLVES keeps its field verdict — package, stranger, platform and overlay objects alike', () => { |
| 131 | + for (const bound of ['crm_account', 'showcase_task', 'sys_audit_log', 'tenant_overlay']) { |
| 132 | + expect(overlay(listViewOn(bound, ['name'])), bound).toMatchObject({ errors: [], advisories: [] }); |
| 133 | + const refused = overlay(listViewOn(bound)); |
| 134 | + expect(rules(refused.errors), bound).toEqual([LIST_VIEW_FIELD_UNKNOWN]); |
| 135 | + expect(refused.errors[0].path, bound).toMatch(/columns\[1\]\.field$/); |
| 136 | + expect(refused.advisories, bound).toEqual([]); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + it('an EMPTY collection is no universe: nothing is judged, exactly as before', () => { |
| 141 | + expect(overlay(listViewOn('nosuch_obj'), [])).toMatchObject({ errors: [], advisories: [] }); |
| 142 | + expect(runRuntimeAuthoringRules({ type: 'view', item: listViewOn('nosuch_obj') })) |
| 143 | + .toMatchObject({ errors: [], advisories: [] }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('a closure wins: the same body written into a package is the #24563 verdict, not this one', () => { |
| 147 | + const scope: RuntimePackageScope = { packageId: OWN, dependencies: [] }; |
| 148 | + const result = runRuntimeAuthoringRules({ type: 'view', item: listViewOn('nosuch_obj'), context: { objects: OBJECTS }, packageScope: scope }); |
| 149 | + |
| 150 | + expect(rules(result.errors)).toEqual([LIST_VIEW_OBJECT_OUTSIDE_CLOSURE]); |
| 151 | + // And at the rule itself, when a caller states both. |
| 152 | + expect( |
| 153 | + validateListViewFieldRefs( |
| 154 | + { objects: OBJECTS, views: [listViewOn('nosuch_obj')] }, |
| 155 | + { packageScope: scope, wholeCollection: true }, |
| 156 | + ).map((f) => f.rule), |
| 157 | + ).toEqual([LIST_VIEW_OBJECT_OUTSIDE_CLOSURE]); |
| 158 | + }); |
| 159 | + |
| 160 | + it('a stored sibling\'s dangling binding is not charged to a flow write', () => { |
| 161 | + const dangling = obj('crm_board', OWN, { |
| 162 | + listViews: { foreign: { label: 'Foreign', data: { provider: 'object', object: 'nosuch_obj' }, columns: ['name'] } }, |
| 163 | + }); |
| 164 | + const flow = { |
| 165 | + name: 'crm_account_touched', |
| 166 | + label: 'Touched', |
| 167 | + status: 'active', |
| 168 | + type: 'autolaunched', |
| 169 | + nodes: [ |
| 170 | + { id: 'start', type: 'start', label: 'On update', config: { objectName: 'crm_account', triggerType: 'record-after-update' } }, |
| 171 | + { id: 'end', type: 'end', label: 'End' }, |
| 172 | + ], |
| 173 | + edges: [{ id: 'e1', source: 'start', target: 'end', type: 'default', isDefault: false }], |
| 174 | + }; |
| 175 | + const result = runRuntimeAuthoringRules({ type: 'flow', item: flow, context: { objects: [...OBJECTS, dangling] } }); |
| 176 | + |
| 177 | + expect(rules([...result.errors, ...result.advisories])).not.toContain(LIST_VIEW_OBJECT_UNKNOWN); |
| 178 | + // Non-vacuous: written itself, the sibling IS refused. |
| 179 | + expect(rules(runRuntimeAuthoringRules({ type: 'object', item: dangling, context: { objects: OBJECTS } }).errors)) |
| 180 | + .toEqual([LIST_VIEW_OBJECT_UNKNOWN]); |
| 181 | + }); |
| 182 | + |
| 183 | + it('…nor to an object write that creates the object NEAREST to it — the suggestion moves, the fingerprint does not', () => { |
| 184 | + const dangling = obj('crm_board', undefined, { |
| 185 | + listViews: { typo: { label: 'Typo', data: { provider: 'object', object: 'crm_invoce' }, columns: ['name'] } }, |
| 186 | + }); |
| 187 | + const created = obj('crm_invoice'); |
| 188 | + const result = runRuntimeAuthoringRules({ type: 'object', item: created, context: { objects: [...OBJECTS, dangling] } }); |
| 189 | + |
| 190 | + expect(result.errors).toEqual([]); |
| 191 | + expect(rules(result.advisories)).not.toContain(LIST_VIEW_OBJECT_UNKNOWN); |
| 192 | + // Non-vacuous: the suggestion really does differ between the two |
| 193 | + // universes the differential compares. |
| 194 | + const before = validateListViewFieldRefs({ objects: [...OBJECTS, dangling] }, { wholeCollection: true }); |
| 195 | + const after = validateListViewFieldRefs({ objects: [...OBJECTS, dangling, created] }, { wholeCollection: true }); |
| 196 | + expect(rules(before)).toEqual([LIST_VIEW_OBJECT_UNKNOWN]); |
| 197 | + expect(rules(after)).toEqual([LIST_VIEW_OBJECT_UNKNOWN]); |
| 198 | + expect(after[0]!.hint).not.toEqual(before[0]!.hint); |
| 199 | + expect(after[0]!.message).toEqual(before[0]!.message); |
| 200 | + }); |
| 201 | +}); |
| 202 | + |
| 203 | +describe('the CLI reading is unchanged (#24668)', () => { |
| 204 | + const stack = { name: 'p', objects: [...OBJECTS], views: [listViewOn('nosuch_obj')] }; |
| 205 | + |
| 206 | + it('the rule with no statement keeps skip 1', () => { |
| 207 | + expect(validateListViewFieldRefs(stack)).toEqual([]); |
| 208 | + }); |
| 209 | + |
| 210 | + it('the suite with no runtime write type keeps skip 1', () => { |
| 211 | + expect(rules(validateReferenceIntegrity(stack))).not.toContain(LIST_VIEW_OBJECT_UNKNOWN); |
| 212 | + // …and the same stack judged as a runtime `view` snapshot is the verdict above. |
| 213 | + expect(rules(validateReferenceIntegrity(stack, { runtimeWriteType: 'view' }))).toContain(LIST_VIEW_OBJECT_UNKNOWN); |
| 214 | + }); |
| 215 | + |
| 216 | + for (const command of ['validate', 'build', 'lint'] as const) { |
| 217 | + it(`\`${command}\` reports nothing for the binding`, () => { |
| 218 | + const { errors, advisories } = splitBySeverity(runAuthoringRules(command, { normalized: stack })); |
| 219 | + expect(rules([...errors, ...advisories])).not.toContain(LIST_VIEW_OBJECT_UNKNOWN); |
| 220 | + }); |
| 221 | + } |
| 222 | +}); |
0 commit comments