|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#23965] `showcase_approver_bindings` is a DESIGNER specimen: it loads, and |
| 5 | + * it is never armed. |
| 6 | + * |
| 7 | + * Its approval nodes carry empty values over directory kinds a fresh org has |
| 8 | + * no rows for, so an armed copy resolves to nobody — and, under |
| 9 | + * `onEmptyApprovers: 'admin_rescue'`, every Field Zoo update starts a run that |
| 10 | + * parks a pending approval request on the org administrators. It used to ship |
| 11 | + * as `status: 'draft'` on the belief that draft keeps a flow out of the |
| 12 | + * runtime. It does not: the engine disables a flow on `obsolete` / `invalid` |
| 13 | + * only, and binds a draft flow exactly like an `active` one. |
| 14 | + * |
| 15 | + * This is an ENGINE reading, not a reading of the `status` literal. A literal |
| 16 | + * check stays green on any spelling, including one the engine happens not to |
| 17 | + * disable, which is exactly how `draft` shipped. So the shipped definition — |
| 18 | + * the module's own export, which the first case proves is the very object |
| 19 | + * `allFlows` loads, not a copy — is registered on |
| 20 | + * a real `AutomationEngine` with a recording `record_change` trigger standing |
| 21 | + * in for `@objectstack/trigger-record-change` (whose `start()` is what |
| 22 | + * subscribes a flow to the object's write hooks), and the pin asserts what the |
| 23 | + * engine did with it: |
| 24 | + * |
| 25 | + * - it still LOADS: the engine holds the definition and lists it, which is |
| 26 | + * what the metadata door and the designer read; |
| 27 | + * - it is NOT ARMED: the trigger was never handed a binding for it, so an |
| 28 | + * update to `showcase_field_zoo` has nothing to call; |
| 29 | + * - a direct start is refused with `FLOW_DISABLED` and records no run. |
| 30 | + * |
| 31 | + * The positive control registers the SAME definition under `draft` and must |
| 32 | + * see it bound to `showcase_field_zoo` / `record-after-update`. Without it, a |
| 33 | + * harness whose trigger never binds anything would green every assertion here. |
| 34 | + */ |
| 35 | + |
| 36 | +import { describe, it, expect } from 'vitest'; |
| 37 | +import { AutomationEngine } from '@objectstack/service-automation'; |
| 38 | +import type { FlowTrigger, FlowTriggerBinding } from '@objectstack/service-automation'; |
| 39 | + |
| 40 | +import { ApproverBindingsFlow } from '../src/automation/flows/approver-bindings.flow.js'; |
| 41 | +import { allFlows } from '../src/automation/flows/index.js'; |
| 42 | + |
| 43 | +const SPECIMEN = 'showcase_approver_bindings'; |
| 44 | + |
| 45 | +function silentLogger(): any { |
| 46 | + const logger: any = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }; |
| 47 | + logger.child = () => logger; |
| 48 | + return logger; |
| 49 | +} |
| 50 | + |
| 51 | +/** A `record_change` trigger that records every binding the engine hands it. */ |
| 52 | +function recordingRecordChangeTrigger() { |
| 53 | + const started: FlowTriggerBinding[] = []; |
| 54 | + const trigger: FlowTrigger = { |
| 55 | + type: 'record_change', |
| 56 | + start(binding) { |
| 57 | + started.push(binding); |
| 58 | + }, |
| 59 | + stop() {}, |
| 60 | + }; |
| 61 | + return { trigger, started }; |
| 62 | +} |
| 63 | + |
| 64 | +function engineWithTrigger() { |
| 65 | + const engine = new AutomationEngine(silentLogger()); |
| 66 | + const rec = recordingRecordChangeTrigger(); |
| 67 | + engine.registerTrigger(rec.trigger); |
| 68 | + return { engine, started: rec.started }; |
| 69 | +} |
| 70 | + |
| 71 | +describe('#23965 — showcase_approver_bindings loads but is never armed', () => { |
| 72 | + it('is part of the app, so the designer has it to open', () => { |
| 73 | + expect(ApproverBindingsFlow.name).toBe(SPECIMEN); |
| 74 | + expect(allFlows).toContain(ApproverBindingsFlow); |
| 75 | + }); |
| 76 | + |
| 77 | + it('positive control: the same definition under `draft` IS bound to Field Zoo updates', () => { |
| 78 | + const { engine, started } = engineWithTrigger(); |
| 79 | + const control = `${SPECIMEN}_draft_control`; |
| 80 | + engine.registerFlow(control, { ...ApproverBindingsFlow, name: control, status: 'draft' } as never); |
| 81 | + |
| 82 | + expect(started.map((b) => ({ flowName: b.flowName, object: b.object, event: b.event }))).toEqual([ |
| 83 | + { flowName: control, object: 'showcase_field_zoo', event: 'record-after-update' }, |
| 84 | + ]); |
| 85 | + }); |
| 86 | + |
| 87 | + it('the shipped definition registers and lists, but is handed to no trigger', async () => { |
| 88 | + const { engine, started } = engineWithTrigger(); |
| 89 | + engine.registerFlow(SPECIMEN, ApproverBindingsFlow as never); |
| 90 | + |
| 91 | + // Loads: the engine holds and lists it. |
| 92 | + expect(await engine.listFlows()).toContain(SPECIMEN); |
| 93 | + expect(await engine.getFlow(SPECIMEN)).not.toBeNull(); |
| 94 | + |
| 95 | + // Not armed: no binding reached the record-change trigger, so a Field Zoo |
| 96 | + // update has no callback to run it through. |
| 97 | + expect(started.map((b) => b.flowName)).not.toContain(SPECIMEN); |
| 98 | + expect(engine.getActiveTriggerBindings().map((b) => b.flowName)).not.toContain(SPECIMEN); |
| 99 | + |
| 100 | + const state = engine.getFlowRuntimeStates().find((s) => s.name === SPECIMEN); |
| 101 | + expect(state).toMatchObject({ enabled: false, bound: false, status: 'obsolete' }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('a direct start is refused with FLOW_DISABLED and records no run', async () => { |
| 105 | + const { engine } = engineWithTrigger(); |
| 106 | + engine.registerFlow(SPECIMEN, ApproverBindingsFlow as never); |
| 107 | + |
| 108 | + const result = await engine.execute(SPECIMEN); |
| 109 | + expect(result).toMatchObject({ success: false, code: 'FLOW_DISABLED' }); |
| 110 | + expect(await engine.listRuns(SPECIMEN)).toEqual([]); |
| 111 | + }); |
| 112 | +}); |
0 commit comments