|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#4352] `action.body` binds a handler ONLY for `type: 'script'`. |
| 5 | + * |
| 6 | + * `ActionSchema.body` has always said "Only used when type is `script`", and |
| 7 | + * its JSDoc is more explicit still ("Only meaningful when `type === 'script'`"). |
| 8 | + * The runtime read neither: `collectBundleActions` collected any named action |
| 9 | + * and `actionBodyRunnerFactory` bound a handler the moment `body` parsed. So a |
| 10 | + * `type: 'url'` action carrying a leftover body was registered in the action |
| 11 | + * registry and executed in the sandbox — the declared ≠ enforced shape of |
| 12 | + * Prime Directive #10, in its nastiest form: an author flips `type` away from |
| 13 | + * `script`, reasonably concludes the body is now dead, and it is not. |
| 14 | + * |
| 15 | + * The sibling tests in `sandbox/body-runner.test.ts` pin the factory in |
| 16 | + * isolation. THIS file pins the composition AppPlugin actually performs — |
| 17 | + * `collectBundleActions(bundle)` → `actionBodyRunnerFactory(...)` → skip when |
| 18 | + * no handler → `ql.registerAction(...)` — because that loop is where the |
| 19 | + * registration decision is really made, and a factory that returns `undefined` |
| 20 | + * only matters if the loop honours it (it does: `if (!handler) continue`). |
| 21 | + * |
| 22 | + * The bind loop is replicated rather than driven through a booted AppPlugin on |
| 23 | + * purpose: booting one needs a kernel, an ObjectQL engine and a QuickJS |
| 24 | + * sandbox, none of which participate in the decision under test. The |
| 25 | + * replication is kept honest by asserting the collector's own output too, so a |
| 26 | + * change to how actions are collected still surfaces here. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect } from 'vitest'; |
| 30 | +import { collectBundleActions } from './app-plugin.js'; |
| 31 | +import { actionBodyRunnerFactory } from './sandbox/body-runner.js'; |
| 32 | +import { QuickJSScriptRunner } from './sandbox/quickjs-runner.js'; |
| 33 | + |
| 34 | +const jsBody = { language: 'js', source: 'return { ran: true };', capabilities: [] } as const; |
| 35 | + |
| 36 | +/** The exact registration loop from `AppPlugin.bindDeclarativeActions`. */ |
| 37 | +function bindActions(bundle: unknown, logger?: { warn: (msg: string) => void }) { |
| 38 | + const registered: Array<{ object: string; name: string }> = []; |
| 39 | + const actions = collectBundleActions(bundle); |
| 40 | + const runner = actionBodyRunnerFactory(new QuickJSScriptRunner(), { |
| 41 | + ql: {}, |
| 42 | + appId: 'crm', |
| 43 | + logger, |
| 44 | + }); |
| 45 | + for (const action of actions) { |
| 46 | + const handler = runner(action); |
| 47 | + if (!handler) continue; |
| 48 | + registered.push({ object: action.object ?? 'global', name: action.name }); |
| 49 | + } |
| 50 | + return { collected: actions, registered }; |
| 51 | +} |
| 52 | + |
| 53 | +describe('#4352 — a non-script action with a body binds no handler', () => { |
| 54 | + it("registers the script action and skips the `type: 'url'` one", () => { |
| 55 | + const warnings: string[] = []; |
| 56 | + const { collected, registered } = bindActions( |
| 57 | + { |
| 58 | + actions: [ |
| 59 | + // The regression population: an explicit non-script type + a body. |
| 60 | + { name: 'open_docs', label: 'Docs', type: 'url', target: 'https://x', body: jsBody }, |
| 61 | + // The overwhelmingly common case — unchanged. |
| 62 | + { name: 'close_deal', label: 'Close', type: 'script', object: 'crm_deal', body: jsBody }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + { warn: (msg: string) => warnings.push(msg) }, |
| 66 | + ); |
| 67 | + |
| 68 | + // The collector stays type-blind by design — it feeds governance surfaces |
| 69 | + // that must see every declared action, bound or not. |
| 70 | + expect(collected.map((a) => a.name)).toEqual(['open_docs', 'close_deal']); |
| 71 | + |
| 72 | + // ...but only the script action becomes an executable handler. |
| 73 | + expect(registered).toEqual([{ object: 'crm_deal', name: 'close_deal' }]); |
| 74 | + |
| 75 | + // And the refusal is audible: silence here would just move the invisibility. |
| 76 | + expect(warnings).toHaveLength(1); |
| 77 | + expect(warnings[0]).toContain('open_docs'); |
| 78 | + expect(warnings[0]).toContain("type: 'url'"); |
| 79 | + }); |
| 80 | + |
| 81 | + it('skips a non-script body declared under an object', () => { |
| 82 | + const { registered } = bindActions({ |
| 83 | + objects: [ |
| 84 | + { |
| 85 | + name: 'crm_lead', |
| 86 | + actions: [ |
| 87 | + { name: 'open_portal', label: 'Portal', type: 'url', target: '/p', body: jsBody }, |
| 88 | + { name: 'score_lead', label: 'Score', type: 'script', body: jsBody }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + ], |
| 92 | + }); |
| 93 | + expect(registered).toEqual([{ object: 'crm_lead', name: 'score_lead' }]); |
| 94 | + }); |
| 95 | + |
| 96 | + it('binds an action that omits `type` — `ActionType.default(\'script\')`', () => { |
| 97 | + // Bundles reach the collector RAW. A `strict: false` `defineStack` and a |
| 98 | + // legacy `manifest.actions[]` never pass through `ActionSchema`, so the |
| 99 | + // schema's default has to be applied here or the common shape breaks. |
| 100 | + const { registered } = bindActions({ |
| 101 | + manifest: { actions: [{ name: 'legacy_untyped', label: 'Legacy', body: jsBody }] }, |
| 102 | + }); |
| 103 | + expect(registered).toEqual([{ object: 'global', name: 'legacy_untyped' }]); |
| 104 | + }); |
| 105 | + |
| 106 | + it('leaves bodyless non-script actions exactly as they were', () => { |
| 107 | + const warnings: string[] = []; |
| 108 | + const { collected, registered } = bindActions( |
| 109 | + { |
| 110 | + actions: [ |
| 111 | + { name: 'open_docs', label: 'Docs', type: 'url', target: 'https://x' }, |
| 112 | + { name: 'convert', label: 'Convert', type: 'flow', target: 'crm_convert' }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + { warn: (msg: string) => warnings.push(msg) }, |
| 116 | + ); |
| 117 | + expect(collected).toHaveLength(2); |
| 118 | + // They never bound a handler before this change either — nothing to warn about. |
| 119 | + expect(registered).toEqual([]); |
| 120 | + expect(warnings).toEqual([]); |
| 121 | + }); |
| 122 | +}); |
0 commit comments