|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #26646 — the build and the OBJECT write door judge a field group's |
| 5 | + * `visibleWhen`. |
| 6 | + * |
| 7 | + * ## The state this closes |
| 8 | + * |
| 9 | + * `ObjectFieldGroupSchema.visibleWhen` is a live slot: the form renderer gates |
| 10 | + * the whole group on it, header included, and a predicate that faults hides |
| 11 | + * the group (fail-closed). No pass of `validateStackExpressions` read it, so |
| 12 | + * the card's body — `record.no_such_field == 'x'` on a group — saved with a |
| 13 | + * 200 and the group vanished from every form, while the same text on a |
| 14 | + * field's `visibleWhen` or an action's `visible` was refused 422 at the same |
| 15 | + * door. |
| 16 | + * |
| 17 | + * ## The crossing |
| 18 | + * |
| 19 | + * No registry change: the entry already declares `object`. The field-group |
| 20 | + * pass runs in the object walk, after the field walk, with the field |
| 21 | + * `visibleWhen` slot's verdict (the `record`-scoped check and the root |
| 22 | + * verdict). It is not fenced on an object write, so the door's finding IS the |
| 23 | + * build's finding — rule, location, message and hint — located at the group's |
| 24 | + * key. |
| 25 | + * |
| 26 | + * The protocol-level half — the same verdict through the real `saveMetaItem` — |
| 27 | + * is the #26646 block of |
| 28 | + * `packages/metadata-protocol/src/protocol.runtime-authoring-gate.test.ts`. |
| 29 | + */ |
| 30 | +import { describe, expect, it } from 'vitest'; |
| 31 | +import { EXPRESSION_INVALID, runAuthoringRules } from './authoring-rules.js'; |
| 32 | +import { runRuntimeAuthoringRules, runtimeAuthoringRulesFor } from './runtime-gate.js'; |
| 33 | + |
| 34 | +/** |
| 35 | + * The card's probe object, verbatim but for the predicate: `stage` sits in |
| 36 | + * group `g1`, whose `visibleWhen` is the body under test. |
| 37 | + */ |
| 38 | +const fxGroup = (visibleWhen: unknown, name = 'qa_fg_probe') => ({ |
| 39 | + name, |
| 40 | + label: 'QA', |
| 41 | + sharingModel: 'private', |
| 42 | + fields: { |
| 43 | + name: { type: 'text', label: 'Name' }, |
| 44 | + stage: { type: 'text', label: 'Stage', group: 'g1' }, |
| 45 | + }, |
| 46 | + fieldGroups: [{ key: 'g1', label: 'G1', visibleWhen }], |
| 47 | +}); |
| 48 | + |
| 49 | +const WHERE = "object 'qa_fg_probe' · field group 'g1' visibleWhen"; |
| 50 | + |
| 51 | +/** The card's body: a read of a field the object does not declare. */ |
| 52 | +const CARD_BODY = "record.no_such_field == 'x'"; |
| 53 | + |
| 54 | +/** |
| 55 | + * One refused body per finding the pass gives. Each `subject` is the named |
| 56 | + * subject of the build's finding (what the author typed), not its prose. |
| 57 | + */ |
| 58 | +const REFUSED = [ |
| 59 | + { body: CARD_BODY, subject: 'unknown field `no_such_field`' }, |
| 60 | + { body: "stage == 'won'", subject: 'bare reference `stage`' }, |
| 61 | + { body: 'sqrt(record.stage) > 1', subject: '`sqrt`' }, |
| 62 | + { body: 'record.stage ==', subject: 'invalid CEL predicate' }, |
| 63 | + // The root verdict: a root the field-rule family never binds. |
| 64 | + { body: "'org_admin' in current_user.positions", subject: 'reads `current_user`' }, |
| 65 | +] as const; |
| 66 | + |
| 67 | +/** Bodies the build accepts on a field's `visibleWhen`, and so must accept on a group. */ |
| 68 | +const ACCEPTED = [ |
| 69 | + "record.stage == 'won'", |
| 70 | + "record.stage != null && record.name != ''", |
| 71 | + "previous.stage != record.stage", |
| 72 | +] as const; |
| 73 | + |
| 74 | +const gateObject = (item: unknown, objects: unknown[] = []) => |
| 75 | + runRuntimeAuthoringRules({ type: 'object', item, context: { objects } }); |
| 76 | + |
| 77 | +const expressionFindings = <T extends { rule: string }>(fs: readonly T[]): T[] => |
| 78 | + fs.filter((f) => f.rule === EXPRESSION_INVALID); |
| 79 | + |
| 80 | +const buildFindings = (...objects: unknown[]) => { |
| 81 | + const stack = { objects }; |
| 82 | + return expressionFindings(runAuthoringRules('build', { normalized: stack, parsed: stack })); |
| 83 | +}; |
| 84 | + |
| 85 | +const dump = (r: unknown) => JSON.stringify(r, null, 2); |
| 86 | + |
| 87 | +describe('#26646 — the object door gives the build\'s field group `visibleWhen` verdict', () => { |
| 88 | + it('needs no registry change: `validateStackExpressions` is already on the object door', () => { |
| 89 | + expect(runtimeAuthoringRulesFor('object').map((r) => r.name)).toContain('validateStackExpressions'); |
| 90 | + }); |
| 91 | + |
| 92 | + for (const { body, subject } of REFUSED) { |
| 93 | + it(`⭐ LIT — a group's \`visibleWhen: ${body}\` is REFUSED, located at the group's key`, () => { |
| 94 | + const result = gateObject(fxGroup(body)); |
| 95 | + |
| 96 | + expect(result.rulesRun).toContain('validateStackExpressions'); |
| 97 | + const errs = expressionFindings(result.errors); |
| 98 | + expect(errs, dump(result)).toHaveLength(1); |
| 99 | + expect(errs[0]).toMatchObject({ severity: 'error', where: WHERE, path: WHERE }); |
| 100 | + expect(errs[0]!.message).toContain(subject); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + for (const body of ACCEPTED) { |
| 105 | + it(`⭐ CONTROL — a group's \`visibleWhen: ${body}\` still publishes clean, and the build agrees`, () => { |
| 106 | + const result = gateObject(fxGroup(body)); |
| 107 | + |
| 108 | + expect(result.rulesRun).toContain('validateStackExpressions'); |
| 109 | + expect(expressionFindings(result.errors), dump(result)).toEqual([]); |
| 110 | + expect(expressionFindings(result.advisories), dump(result)).toEqual([]); |
| 111 | + expect(buildFindings(fxGroup(body))).toEqual([]); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + it('⭐ CONTROL — a group with no `visibleWhen` publishes clean', () => { |
| 116 | + const result = gateObject(fxGroup(undefined)); |
| 117 | + |
| 118 | + expect(expressionFindings(result.errors), dump(result)).toEqual([]); |
| 119 | + expect(expressionFindings(result.advisories), dump(result)).toEqual([]); |
| 120 | + }); |
| 121 | + |
| 122 | + it('⭐ PARITY — the card\'s body earns the same finding on the group as on the field\'s own `visibleWhen`', () => { |
| 123 | + const onGroup = expressionFindings(gateObject(fxGroup(CARD_BODY)).errors); |
| 124 | + const onField = expressionFindings( |
| 125 | + gateObject({ |
| 126 | + ...fxGroup(undefined), |
| 127 | + fields: { ...fxGroup(undefined).fields, stage: { type: 'text', label: 'Stage', group: 'g1', visibleWhen: CARD_BODY } }, |
| 128 | + }).errors, |
| 129 | + ); |
| 130 | + |
| 131 | + expect(onField.map((f) => f.where)).toEqual(["object 'qa_fg_probe' · field 'stage' visibleWhen"]); |
| 132 | + expect(onGroup.map((f) => f.message), dump(onGroup)).toEqual(onField.map((f) => f.message)); |
| 133 | + }); |
| 134 | + |
| 135 | + it('⭐ PARITY — for each refused body the door findings ARE the build findings', () => { |
| 136 | + for (const { body } of REFUSED) { |
| 137 | + const atBuild = buildFindings(fxGroup(body)); |
| 138 | + const atDoor = expressionFindings(gateObject(fxGroup(body)).errors); |
| 139 | + |
| 140 | + // Non-vacuous: the build refuses each of them. |
| 141 | + expect(atBuild.length, body).toBeGreaterThan(0); |
| 142 | + expect(atDoor, body).toEqual(atBuild); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + it('the root verdict is worded for the group, and names its fail-closed consequence', () => { |
| 147 | + const errs = expressionFindings(gateObject(fxGroup(REFUSED[4].body)).errors); |
| 148 | + |
| 149 | + expect(errs, dump(errs)).toHaveLength(1); |
| 150 | + expect(errs[0]!.message).toMatch(/^`visibleWhen` reads `current_user`, but a field group's section predicate binds only `record`/); |
| 151 | + expect(errs[0]!.message).toContain('fails CLOSED'); |
| 152 | + // The prescription is the group's: field-level security or a `record` |
| 153 | + // rewrite, never the select-option move a field's message offers. |
| 154 | + expect(errs[0]!.message).toContain('field-level security'); |
| 155 | + expect(errs[0]!.message).not.toContain('CHOICES of a select'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('a stored sibling\'s broken group is not this write\'s to answer for (the differential)', () => { |
| 159 | + const sibling = fxGroup(CARD_BODY, 'qa_fg_sibling'); |
| 160 | + // Non-vacuous: the sibling is refused at the build. |
| 161 | + expect(buildFindings(sibling).length).toBeGreaterThan(0); |
| 162 | + |
| 163 | + const result = gateObject(fxGroup(ACCEPTED[0]), [sibling]); |
| 164 | + |
| 165 | + expect(expressionFindings(result.errors), dump(result)).toEqual([]); |
| 166 | + }); |
| 167 | +}); |
0 commit comments