|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { lowerCallables } from './lower-callables.js'; |
| 5 | + |
| 6 | +// ── #3713: `target` vs the deprecated `execute` alias — one precedence, one slot ── |
| 7 | +// |
| 8 | +// `execute` is the deprecated alias of `target`. Before this was aligned, three |
| 9 | +// readers resolved "the author declared both" in two directions: the spec |
| 10 | +// transform kept `target`, objectui's ActionRunner did `execute || target`, and |
| 11 | +// THIS compile step preferred `execute`. The compile-step half was the nastiest: |
| 12 | +// it bundled the `execute` function and then overwrote `action.target` with the |
| 13 | +// resulting ref, so the function the author actually declared on `target` was |
| 14 | +// silently dropped from the build. |
| 15 | +describe('lowerCallables — action handler slot precedence (#3713)', () => { |
| 16 | + const actionsOf = (result: { lowered: Record<string, unknown> }) => |
| 17 | + (result.lowered as { actions: Array<Record<string, unknown>> }).actions; |
| 18 | + |
| 19 | + it('binds the canonical `target` function when both slots carry a callable', () => { |
| 20 | + const result = lowerCallables({ |
| 21 | + actions: [{ |
| 22 | + name: 'convert', |
| 23 | + label: 'Convert', |
| 24 | + type: 'script', |
| 25 | + target: function preferredHandler() { return 'preferred'; }, |
| 26 | + execute: function legacyHandler() { return 'legacy'; }, |
| 27 | + }], |
| 28 | + }); |
| 29 | + |
| 30 | + const [action] = actionsOf(result); |
| 31 | + const ref = action.target as string; |
| 32 | + expect(typeof ref).toBe('string'); |
| 33 | + // The bundled function must be the one from `target`, not from `execute`. |
| 34 | + expect(result.functions[ref]()).toBe('preferred'); |
| 35 | + expect(Object.values(result.functions).map((fn) => fn())).not.toContain('legacy'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('drops the `execute` alias once a ref is bound to `target`', () => { |
| 39 | + const result = lowerCallables({ |
| 40 | + actions: [{ |
| 41 | + name: 'convert', |
| 42 | + label: 'Convert', |
| 43 | + type: 'script', |
| 44 | + target: function preferredHandler() { return 'preferred'; }, |
| 45 | + execute: function legacyHandler() { return 'legacy'; }, |
| 46 | + }], |
| 47 | + }); |
| 48 | + |
| 49 | + const [action] = actionsOf(result); |
| 50 | + // A leftover alias is stale by construction — and a function-valued one |
| 51 | + // would fail `ActionSchema` (it expects a string) further down the pipeline. |
| 52 | + expect('execute' in action).toBe(false); |
| 53 | + // The lowered stack must be JSON-safe: no function values survive. |
| 54 | + expect(() => JSON.stringify(result.lowered)).not.toThrow(); |
| 55 | + expect(JSON.stringify(result.lowered)).not.toContain('legacy'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('still lowers an `execute`-only callable (back-compat) onto `target`', () => { |
| 59 | + const result = lowerCallables({ |
| 60 | + actions: [{ |
| 61 | + name: 'legacy_only', |
| 62 | + label: 'Legacy', |
| 63 | + type: 'script', |
| 64 | + execute: function legacyHandler() { return 'legacy'; }, |
| 65 | + }], |
| 66 | + }); |
| 67 | + |
| 68 | + const [action] = actionsOf(result); |
| 69 | + const ref = action.target as string; |
| 70 | + expect(typeof ref).toBe('string'); |
| 71 | + expect(result.functions[ref]()).toBe('legacy'); |
| 72 | + expect('execute' in action).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it('leaves a string-valued handler pair untouched (the spec transform owns it)', () => { |
| 76 | + // No callable to lower here, so the compile step must not editorialise: the |
| 77 | + // spec's `ActionSchema` transform is the single place that resolves a |
| 78 | + // string/string pair (keeping `target`, dropping `execute`). |
| 79 | + const result = lowerCallables({ |
| 80 | + actions: [{ |
| 81 | + name: 'strings', |
| 82 | + label: 'Strings', |
| 83 | + type: 'script', |
| 84 | + target: 'preferredHandler', |
| 85 | + execute: 'legacyHandler', |
| 86 | + }], |
| 87 | + }); |
| 88 | + |
| 89 | + const [action] = actionsOf(result); |
| 90 | + expect(action.target).toBe('preferredHandler'); |
| 91 | + expect(action.execute).toBe('legacyHandler'); |
| 92 | + expect(result.count).toBe(0); |
| 93 | + }); |
| 94 | + |
| 95 | + it('applies the same precedence to actions nested under an object', () => { |
| 96 | + const result = lowerCallables({ |
| 97 | + objects: [{ |
| 98 | + name: 'crm_deal', |
| 99 | + actions: [{ |
| 100 | + name: 'convert', |
| 101 | + label: 'Convert', |
| 102 | + type: 'script', |
| 103 | + target: function preferredHandler() { return 'preferred'; }, |
| 104 | + execute: function legacyHandler() { return 'legacy'; }, |
| 105 | + }], |
| 106 | + }], |
| 107 | + }); |
| 108 | + |
| 109 | + const objects = (result.lowered as { objects: Array<{ actions: Array<Record<string, unknown>> }> }).objects; |
| 110 | + const [action] = objects[0].actions; |
| 111 | + const ref = action.target as string; |
| 112 | + expect(result.functions[ref]()).toBe('preferred'); |
| 113 | + expect('execute' in action).toBe(false); |
| 114 | + }); |
| 115 | +}); |
0 commit comments