|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #24814 (ruling A) — an action's person-facing copy is an ADR-0032 `template` |
| 4 | +// slot over the closed scope the console's runner binds: `confirmText` over |
| 5 | +// `record` (and `params` beside static values), `successMessage` over `result` |
| 6 | +// and `record`, `errorMessage` over nothing. `validateStackExpressions` — the |
| 7 | +// rule `objectstack validate`, `objectstack compile` and the runtime publish |
| 8 | +// gate all run — judges every sentence through the template judge's |
| 9 | +// closed-scope entry, then compiles it as a template. Every finding is a |
| 10 | +// WARNING: each of these texts parsed while the keys were plain strings, and |
| 11 | +// refusing them is its own card. So a pin here asserts `severity: 'warning'` |
| 12 | +// and, at the publish gate, an advisory with no error. |
| 13 | +// |
| 14 | +// The gate half drives the REAL dispatch (`runRuntimeAuthoringRules`) with a |
| 15 | +// page write and an action write, because a `runtimeTypes` widening whose |
| 16 | +// walk reads nothing is a no-op that reads as coverage. |
| 17 | + |
| 18 | +import { describe, expect, it } from 'vitest'; |
| 19 | + |
| 20 | +import { runStackExpressionPasses, validateStackExpressions } from './validate-expressions.js'; |
| 21 | +import { runRuntimeAuthoringRules } from './runtime-gate.js'; |
| 22 | + |
| 23 | +const objects = [{ name: 'crm_account', label: 'Account', fields: { name: { type: 'text', label: 'Name' } } }]; |
| 24 | + |
| 25 | +const invite = (patch: Record<string, unknown>) => ({ |
| 26 | + name: 'invite', |
| 27 | + label: 'Invite', |
| 28 | + type: 'api', |
| 29 | + target: '/api/v1/invite', |
| 30 | + objectName: 'crm_account', |
| 31 | + ...patch, |
| 32 | +}); |
| 33 | + |
| 34 | +const copyIssues = (stack: Record<string, unknown>) => |
| 35 | + validateStackExpressions(stack).filter((i) => /confirmText|successMessage|errorMessage/.test(i.where)); |
| 36 | + |
| 37 | +const pageWith = (components: unknown[]) => ({ |
| 38 | + name: 'account_home', |
| 39 | + type: 'record', |
| 40 | + object: 'crm_account', |
| 41 | + regions: [{ name: 'main', components }], |
| 42 | +}); |
| 43 | + |
| 44 | +describe('validateStackExpressions — a registered action\'s copy is judged as the template slot it is (#24814)', () => { |
| 45 | + it('passes plain text and holes over bound roots — bare, as the envelope, or in a locale map', () => { |
| 46 | + for (const patch of [ |
| 47 | + { confirmText: 'Invite {{ record.name }}?' }, |
| 48 | + { successMessage: 'Invitation sent to {{ result.email }}' }, |
| 49 | + { successMessage: 'Sent to {{ result.email }} for {{ record.name | upper }}' }, |
| 50 | + { successMessage: { dialect: 'template', source: 'Sent to {{ result.email }}' } }, |
| 51 | + { successMessage: { en: 'Sent to {{ result.email }}', 'zh-CN': '已发送至 {{ result.email }}' } }, |
| 52 | + { confirmText: 'Invite this contact?', successMessage: 'Invitation sent' }, |
| 53 | + ]) { |
| 54 | + expect(copyIssues({ objects, actions: [invite(patch)] }), JSON.stringify(patch)).toEqual([]); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + it('names a `${…}` token with a WARNING prescribing its `{{ }}` hole, never `${{ … }}`', () => { |
| 59 | + const [issue, ...rest] = copyIssues({ objects, actions: [invite({ successMessage: 'Invitation sent to ${result.email}' })] }); |
| 60 | + expect(rest).toEqual([]); |
| 61 | + expect(issue!.severity).toBe('warning'); |
| 62 | + expect(issue!.where).toBe("stack · action 'invite' successMessage"); |
| 63 | + expect(issue!.message).toContain('Write `Invitation sent to ${result.email}` as `Invitation sent to {{ result.email }}`.'); |
| 64 | + expect(issue!.message).not.toContain('${{'); |
| 65 | + expect(issue!.message).toMatch(/A warning, not a refusal/); |
| 66 | + }); |
| 67 | + |
| 68 | + it('names a hole over a root the runner does not bind — `result` in a confirmText', () => { |
| 69 | + const [issue] = copyIssues({ objects, actions: [invite({ confirmText: 'Send to {{ result.email }}?' })] }); |
| 70 | + expect(issue!.severity).toBe('warning'); |
| 71 | + expect(issue!.message).toContain("An action's `confirmText` binds only `record`"); |
| 72 | + expect(issue!.message).toContain('`{{ result.email }}`'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('judges each value of a locale map at its own location', () => { |
| 76 | + const issues = copyIssues({ objects, actions: [invite({ successMessage: { en: 'Sent', 'zh-CN': '已发送 {result.email}' } })] }); |
| 77 | + expect(issues.map((i) => i.where)).toEqual(["stack · action 'invite' successMessage.zh-CN"]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('names logic in a hole, an unknown formatter and a blank sentence — all warnings', () => { |
| 81 | + for (const successMessage of ['{{ result.count * 2 }} sent', '{{ result.email | shout }}', ' ']) { |
| 82 | + const issues = copyIssues({ objects, actions: [invite({ successMessage })] }); |
| 83 | + expect(issues.length, successMessage).toBeGreaterThan(0); |
| 84 | + expect(issues.every((i) => i.severity === 'warning'), successMessage).toBe(true); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + it('judges an object\'s own actions on an object write, once when also declared top-level', () => { |
| 89 | + const action = invite({ confirmText: 'Invite ${record.name}?' }); |
| 90 | + const stack = { objects: [{ ...objects[0], actions: [action] }], actions: [action] }; |
| 91 | + expect(copyIssues(stack)).toHaveLength(1); |
| 92 | + const onObjectWrite = runStackExpressionPasses({ objects: stack.objects }, { runtimeWriteType: 'object' }) |
| 93 | + .filter((i) => i.where.includes('confirmText')); |
| 94 | + expect(onObjectWrite.map((i) => i.where)).toEqual(["object 'crm_account' · action 'invite' confirmText"]); |
| 95 | + }); |
| 96 | + |
| 97 | + it('control: ActionSchema\'s errorMessage is not in the family — a `${…}` there draws nothing', () => { |
| 98 | + expect(copyIssues({ objects, actions: [invite({ errorMessage: 'Failed for ${record.name}' })] })).toEqual([]); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('validateStackExpressions — a page action row\'s copy (#24814)', () => { |
| 103 | + it('judges an action:button\'s three keys, binding `params` only beside static values', () => { |
| 104 | + const button = (properties: Record<string, unknown>) => ({ type: 'action:button', properties: { label: 'Run', actionType: 'script', target: 'run', ...properties } }); |
| 105 | + const issues = copyIssues({ |
| 106 | + objects, |
| 107 | + pages: [pageWith([ |
| 108 | + button({ confirmText: 'Run for {{ params.tier }}?', params: { tier: 'gold' } }), |
| 109 | + button({ confirmText: 'Run for {{ params.tier }}?', params: [{ name: 'tier', type: 'text' }] }), |
| 110 | + button({ errorMessage: 'Failed for {{ record.name }}' }), |
| 111 | + button({ successMessage: 'Done', errorMessage: 'Could not run' }), |
| 112 | + ])], |
| 113 | + }); |
| 114 | + expect(issues.map((i) => i.where)).toEqual([ |
| 115 | + "page 'account_home'.regions[0].components[1] (action:button) · confirmText", |
| 116 | + "page 'account_home'.regions[0].components[2] (action:button) · errorMessage", |
| 117 | + ]); |
| 118 | + expect(issues[0]!.message).toContain("binds only `record`, so a hole over any other root"); |
| 119 | + expect(issues[1]!.message).toContain('binds no root, so it fills no hole'); |
| 120 | + expect(issues.every((i) => i.severity === 'warning')).toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it('judges each action:group / action:menu member, an action:icon, and an inline `properties.action`', () => { |
| 124 | + const issues = copyIssues({ |
| 125 | + objects, |
| 126 | + pages: [pageWith([ |
| 127 | + { type: 'action:group', properties: { actions: [{ name: 'a', type: 'script', target: 'a', confirmText: 'Run ${record.name}?' }] } }, |
| 128 | + { type: 'action:menu', properties: { actions: [{ name: 'b', type: 'script', target: 'b', successMessage: '{count} done' }] } }, |
| 129 | + { type: 'action:icon', properties: { label: 'X', actionType: 'script', target: 'x', confirmText: '' } }, |
| 130 | + { type: 'element:button', properties: { label: 'Go', action: { type: 'script', target: 'go', successMessage: 'Went to ${result.url}' } } }, |
| 131 | + ])], |
| 132 | + }); |
| 133 | + expect(issues.map((i) => i.where)).toEqual([ |
| 134 | + "page 'account_home'.regions[0].components[0] (action:group) · actions[0] confirmText", |
| 135 | + "page 'account_home'.regions[0].components[1] (action:menu) · actions[0] successMessage", |
| 136 | + "page 'account_home'.regions[0].components[2] (action:icon) · confirmText", |
| 137 | + "page 'account_home'.regions[0].components[3] (element:button) · action successMessage", |
| 138 | + ]); |
| 139 | + expect(issues[0]!.message).toContain('as `Run {{ record.name }}?`'); |
| 140 | + expect(issues[2]!.message).toMatch(/^An action's `confirmText` is blank/); |
| 141 | + }); |
| 142 | + |
| 143 | + it('on a page write runs that pass alone', () => { |
| 144 | + const stack = { |
| 145 | + objects, |
| 146 | + pages: [pageWith([{ type: 'action:button', properties: { label: 'Run', actionType: 'script', target: 'run', confirmText: '{{ x }}' } }])], |
| 147 | + flows: [{ name: 'f', nodes: [{ id: 'n', type: 'notify', config: { title: '{bad}' } }] }], |
| 148 | + }; |
| 149 | + const issues = runStackExpressionPasses(stack, { runtimeWriteType: 'page' }); |
| 150 | + expect(issues).toHaveLength(1); |
| 151 | + expect(issues[0]!.where).toContain('confirmText'); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('the runtime publish gate — an advisory, never a refusal (#24814)', () => { |
| 156 | + const findings = (type: string, item: unknown) => { |
| 157 | + const { errors, advisories } = runRuntimeAuthoringRules({ type, item, context: { objects } }); |
| 158 | + const copy = (f: { where: string }) => /confirmText|successMessage|errorMessage/.test(f.where); |
| 159 | + return { errors: errors.filter(copy), advisories: advisories.filter(copy) }; |
| 160 | + }; |
| 161 | + |
| 162 | + it('a page write carrying a `${…}` confirmText answers one advisory and no error', () => { |
| 163 | + const page = pageWith([{ type: 'action:button', properties: { label: 'Run', actionType: 'script', target: 'run', confirmText: 'Delete ${record.name}?' } }]); |
| 164 | + const { errors, advisories } = findings('page', page); |
| 165 | + expect(errors).toEqual([]); |
| 166 | + expect(advisories).toHaveLength(1); |
| 167 | + expect(advisories[0]!.message).toContain('as `Delete {{ record.name }}?`'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('an action write carrying a `${result.*}` successMessage answers one advisory and no error', () => { |
| 171 | + const { errors, advisories } = findings('action', invite({ successMessage: 'Invitation sent to ${result.email}' })); |
| 172 | + expect(errors).toEqual([]); |
| 173 | + expect(advisories).toHaveLength(1); |
| 174 | + }); |
| 175 | + |
| 176 | + it('control: the converted platform sentence draws nothing', () => { |
| 177 | + expect(findings('action', invite({ successMessage: 'Invitation sent to {{ result.email }}' }))).toEqual({ errors: [], advisories: [] }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments