|
| 1 | +/** |
| 2 | + * #15663 — an expression envelope whose `source` is NOT a string is refused |
| 3 | + * through `errors[]`, not by throwing a raw `TypeError` out of the validator. |
| 4 | + * |
| 5 | + * ## The acceptance test is not "it no longer throws" |
| 6 | + * |
| 7 | + * `validateExpression`'s own docblock promises it never throws, and every |
| 8 | + * consumer is built on that promise: `AutomationEngine.validateFlowExpressions` |
| 9 | + * collects LOCATED findings and throws one assembled error naming the flow, the |
| 10 | + * node, the slot and the source (ADR-0032 §1d), and `@objectstack/lint`'s stack |
| 11 | + * walk attributes each finding to the hook / sharing rule / action it came from. |
| 12 | + * A `TypeError` escaping from inside the validator bypassed all of it — the |
| 13 | + * author got `source.trim is not a function` and no location at all. |
| 14 | + * |
| 15 | + * So the pin is TWO-sided: the refusal arrives on the `errors[]` channel here, |
| 16 | + * and the caller's location survives to the author (pinned next door, in |
| 17 | + * `@objectstack/lint`'s `validate-expressions-nonstring-source.test.ts`). |
| 18 | + * |
| 19 | + * ## Why the entry, once |
| 20 | + * |
| 21 | + * `validateExpression` is the shared parse every predicate/value slot in the |
| 22 | + * platform goes through, and the value arrives from METADATA — `ExprInput` |
| 23 | + * declares `source?: string`, but every production call site casts, because a |
| 24 | + * declaration is a claim about stored data and not a guarantee about it. The |
| 25 | + * guard therefore belongs at the entry both public functions share, never in |
| 26 | + * each caller's own try/catch (Prime Directive #12's tolerant-consumer shape). |
| 27 | + */ |
| 28 | +import { describe, it, expect } from 'vitest'; |
| 29 | + |
| 30 | +import { validateExpression, inferExpressionType } from './validate'; |
| 31 | + |
| 32 | +/** The refusal sentence, spelled once. Not exported from the package — the |
| 33 | + * published surface does not move for this fix; the text travels `errors[]`. */ |
| 34 | +const REFUSAL = 'an expression envelope carries its expression as a string `source`'; |
| 35 | + |
| 36 | +describe('#15663 — a non-string envelope `source`', () => { |
| 37 | + describe('is refused through `errors[]`, for every role', () => { |
| 38 | + const CASES: Array<[label: string, source: unknown, found: string]> = [ |
| 39 | + ['an object', { nested: 1 }, 'found an object'], |
| 40 | + ['a nested object (the card\'s own value)', { nested: 1 }, 'found an object'], |
| 41 | + ['a number', 1, 'found a number'], |
| 42 | + ['an array', ['a'], 'found an array'], |
| 43 | + ['a boolean', true, 'found a boolean'], |
| 44 | + ]; |
| 45 | + |
| 46 | + for (const role of ['predicate', 'value', 'template'] as const) { |
| 47 | + for (const [label, source, found] of CASES) { |
| 48 | + it(`${role}: refuses ${label} without throwing`, () => { |
| 49 | + const r = validateExpression(role, { source } as never); |
| 50 | + expect(r.ok).toBe(false); |
| 51 | + expect(r.errors).toHaveLength(1); |
| 52 | + expect(r.errors[0].message).toContain(REFUSAL); |
| 53 | + expect(r.errors[0].message).toContain(found); |
| 54 | + // The defect's own signature must be gone from what the author reads. |
| 55 | + expect(r.errors[0].message).not.toContain('is not a function'); |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + it('does not throw — the property the whole located-reporting contract rests on', () => { |
| 61 | + expect(() => validateExpression('predicate', { source: { nested: 1 } } as never)).not.toThrow(); |
| 62 | + expect(() => validateExpression('value', { dialect: 'cel', source: ['a'] } as never)).not.toThrow(); |
| 63 | + expect(() => validateExpression('template', { source: true } as never)).not.toThrow(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('refuses regardless of the declared dialect — the shape is judged first', () => { |
| 67 | + for (const dialect of ['cel', 'template', 'cron', undefined]) { |
| 68 | + const r = validateExpression('predicate', { dialect, source: 1 } as never); |
| 69 | + expect(r.ok).toBe(false); |
| 70 | + expect(r.errors[0].message).toContain(REFUSAL); |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('the message is self-correcting — it names both authorable forms', () => { |
| 76 | + it('a CEL role is shown bare CEL and a `cel` envelope', () => { |
| 77 | + const m = validateExpression('predicate', { source: 1 } as never).errors[0].message; |
| 78 | + expect(m).toContain('record.rating >= 4'); |
| 79 | + expect(m).toContain("dialect: 'cel'"); |
| 80 | + }); |
| 81 | + |
| 82 | + it('the `template` role is shown a TEMPLATE, not a CEL predicate', () => { |
| 83 | + const m = validateExpression('template', { source: 1 } as never).errors[0].message; |
| 84 | + expect(m).toContain('{{ record.name }}'); |
| 85 | + expect(m).toContain("dialect: 'template'"); |
| 86 | + // Prescribing bare CEL to a text template is advice that cannot succeed. |
| 87 | + expect(m).not.toContain('record.rating >= 4'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('attributes to the empty string — the value that WOULD be the location is the value being refused', () => { |
| 91 | + // `source` is declared `string` on `ExprValidationError` and every caller |
| 92 | + // renders it; echoing the offending non-string would put an object into it. |
| 93 | + expect(validateExpression('predicate', { source: { nested: 1 } } as never).errors[0].source).toBe(''); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('`inferExpressionType` — the SECOND consumer of the same entry', () => { |
| 98 | + it('answers `unknown` instead of throwing', () => { |
| 99 | + expect(() => inferExpressionType({ source: 1 } as never)).not.toThrow(); |
| 100 | + expect(inferExpressionType({ source: 1 } as never)).toBe('unknown'); |
| 101 | + expect(inferExpressionType({ dialect: 'cel', source: ['a'] } as never)).toBe('unknown'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('CONTROL — a real numeric expression still infers `number`', () => { |
| 105 | + expect(inferExpressionType({ dialect: 'cel', source: '1 + 1' })).toBe('number'); |
| 106 | + expect(inferExpressionType('1 + 1')).toBe('number'); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + /** |
| 111 | + * CONTROLS. The reject set and the accept set of everything that already |
| 112 | + * RETURNED are unchanged by this fix — only the population that used to |
| 113 | + * produce no verdict at all (a crash) moved, and it moved into the reject set. |
| 114 | + * These are the shapes that must keep their existing verdict exactly. |
| 115 | + */ |
| 116 | + describe('CONTROLS — what must NOT change', () => { |
| 117 | + it('bare text still validates', () => { |
| 118 | + expect(validateExpression('predicate', 'record.rating >= 4').ok).toBe(true); |
| 119 | + expect(validateExpression('value', 'record.amount / 100').ok).toBe(true); |
| 120 | + expect(validateExpression('template', 'Hi {{ record.name }}').ok).toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it('a well-formed envelope still validates', () => { |
| 124 | + expect(validateExpression('predicate', { dialect: 'cel', source: '1 == 1' }).ok).toBe(true); |
| 125 | + expect(validateExpression('predicate', { source: '1 == 1' }).ok).toBe(true); |
| 126 | + }); |
| 127 | + |
| 128 | + it('"not authored" still reads as `ok: true` — absent, null, empty, whitespace', () => { |
| 129 | + expect(validateExpression('predicate', null).ok).toBe(true); |
| 130 | + expect(validateExpression('predicate', undefined).ok).toBe(true); |
| 131 | + expect(validateExpression('predicate', '').ok).toBe(true); |
| 132 | + expect(validateExpression('predicate', ' ').ok).toBe(true); |
| 133 | + expect(validateExpression('predicate', {}).ok).toBe(true); |
| 134 | + expect(validateExpression('predicate', { dialect: 'cel' }).ok).toBe(true); |
| 135 | + expect(validateExpression('predicate', { source: undefined }).ok).toBe(true); |
| 136 | + // A NULL `source` is "not authored" too, and stays so: only a PRESENT |
| 137 | + // non-string is the fourth population this card refuses. |
| 138 | + expect(validateExpression('predicate', { source: null } as never).ok).toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + it('an `{ ast }` envelope carrying no `source` is still admitted', () => { |
| 142 | + // Its admission is `ExpressionSchema`'s rule, not this entry's; the guard |
| 143 | + // must not start refusing it as "a non-string source". |
| 144 | + expect(validateExpression('predicate', { ast: { kind: 'whatever' } } as never).ok).toBe(true); |
| 145 | + }); |
| 146 | + |
| 147 | + it('a malformed STRING still gets its own diagnostic, not the shape refusal', () => { |
| 148 | + const brace = validateExpression('predicate', '{record.rating} >= 4'); |
| 149 | + expect(brace.ok).toBe(false); |
| 150 | + expect(brace.errors[0].message).not.toContain(REFUSAL); |
| 151 | + expect(brace.errors[0].message).toContain('template brace'); |
| 152 | + expect(brace.errors[0].source).toBe('{record.rating} >= 4'); |
| 153 | + |
| 154 | + const dialect = validateExpression('template', { dialect: 'cel', source: 'record.x' }); |
| 155 | + expect(dialect.ok).toBe(false); |
| 156 | + expect(dialect.errors[0].message).not.toContain(REFUSAL); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments