|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#19900] A scheduled run's platform seeds never answer a screen field. |
| 5 | + * |
| 6 | + * The schedule trigger starts a run with three seeds in `params` — `jobId`, |
| 7 | + * `flowName` and `schedule` — and there is no caller behind it at all. A |
| 8 | + * `screen` node on that headless run asks whether the caller already answered |
| 9 | + * each of its fields (`judgeHeadlessScreen` in `@objectstack/service-automation`). |
| 10 | + * With no statement from the producer, that answer is INFERRED from `params`, |
| 11 | + * and the inference can only rule out the row-id and record seeds the other |
| 12 | + * doors write: the schedule trigger's three names read as the caller's own |
| 13 | + * answers, so a screen with a required field named `schedule` was skipped and |
| 14 | + * the run completed with the cron descriptor as the "answer". |
| 15 | + * |
| 16 | + * The producer now states it: `callerParamKeys: []` — "the caller supplied |
| 17 | + * nothing", which the verdict reads instead of inferring (#19846). |
| 18 | + * |
| 19 | + * The rig is the one `schedule-runas-e2e.test.ts` uses: the REAL |
| 20 | + * `ScheduleTrigger` bound through the REAL `AutomationEngine`, the job fired by |
| 21 | + * hand in place of the cron tick. The result read is the one the engine |
| 22 | + * returned to the trigger's own callback — not a hand-made context. |
| 23 | + * |
| 24 | + * ⚠️ `@objectstack/service-automation` resolves to its `dist/` here (no alias: |
| 25 | + * `KNOWN_UNALIASED_TEST_IMPORTS` in `scripts/check-test-source-alias.mjs`), so |
| 26 | + * the engine half is the built one. The half this file pins — the context the |
| 27 | + * trigger builds — is `./schedule-trigger.js`, read from source. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect, vi } from 'vitest'; |
| 31 | +import { AutomationEngine, installBuiltinNodes } from '@objectstack/service-automation'; |
| 32 | +import type { AutomationContext, JobSchedule, JobHandler } from '@objectstack/spec/contracts'; |
| 33 | +import { ScheduleTrigger } from './schedule-trigger.js'; |
| 34 | +import { withScheduledWorkOn } from './deployment-switch.test-support.js'; |
| 35 | + |
| 36 | +function silentLogger(): any { |
| 37 | + const l: any = { info() {}, warn() {}, error() {}, debug() {} }; |
| 38 | + l.child = () => l; |
| 39 | + return l; |
| 40 | +} |
| 41 | +const flush = () => new Promise<void>((r) => setTimeout(r, 0)); |
| 42 | + |
| 43 | +/** Fake IJobService slice with a deterministic `fire()` standing in for the cron tick. */ |
| 44 | +function fakeJobService() { |
| 45 | + const jobs = new Map<string, { schedule: JobSchedule; handler: JobHandler }>(); |
| 46 | + return { |
| 47 | + service: { |
| 48 | + async schedule(name: string, schedule: JobSchedule, handler: JobHandler) { |
| 49 | + jobs.set(name, { schedule, handler }); |
| 50 | + }, |
| 51 | + async cancel(name: string) { |
| 52 | + jobs.delete(name); |
| 53 | + }, |
| 54 | + }, |
| 55 | + has: (name: string) => jobs.has(name), |
| 56 | + fire: async (name: string, jobId = 'tick-1') => { |
| 57 | + await jobs.get(name)?.handler({ jobId } as never); |
| 58 | + }, |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +type Field = { name: string; label: string; type: string; required?: boolean }; |
| 63 | + |
| 64 | +const CRON: JobSchedule = { type: 'cron', expression: '0 2 * * *' } as JobSchedule; |
| 65 | + |
| 66 | +/** |
| 67 | + * `start → screen_1 → end` on a `type: 'schedule'` flow, each screen field an |
| 68 | + * `isInput` variable — so the seed of the same name IS bound into it, which is |
| 69 | + * the only way the skip could happen. |
| 70 | + */ |
| 71 | +function scheduledScreenFlow(name: string, fields: Field[]) { |
| 72 | + return { |
| 73 | + name, |
| 74 | + label: name, |
| 75 | + type: 'schedule', |
| 76 | + runAs: 'system', |
| 77 | + status: 'active', |
| 78 | + version: 1, |
| 79 | + variables: fields.map((f) => ({ name: f.name, type: 'text', isInput: true, isOutput: true })), |
| 80 | + nodes: [ |
| 81 | + { id: 'start', type: 'start', label: 'Start', config: { schedule: CRON } }, |
| 82 | + { id: 'screen_1', type: 'screen', label: 'Ask', config: { fields } }, |
| 83 | + { id: 'end', type: 'end', label: 'End' }, |
| 84 | + ], |
| 85 | + edges: [ |
| 86 | + { id: 'e1', source: 'start', target: 'screen_1', type: 'default' }, |
| 87 | + { id: 'e2', source: 'screen_1', target: 'end', type: 'default' }, |
| 88 | + ], |
| 89 | + } as never; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Bind the flow through the real trigger, fire one tick, and hand back what |
| 94 | + * the trigger passed to the engine and what the engine answered it with. |
| 95 | + */ |
| 96 | +async function fireOnce(flowName: string, fields: Field[]) { |
| 97 | + const engine = new AutomationEngine(silentLogger()); |
| 98 | + installBuiltinNodes(engine, { logger: silentLogger(), getService: () => undefined } as never); |
| 99 | + const execute = vi.spyOn(engine, 'execute'); |
| 100 | + engine.registerFlow(flowName, scheduledScreenFlow(flowName, fields)); |
| 101 | + const job = fakeJobService(); |
| 102 | + engine.registerTrigger(new ScheduleTrigger(() => job.service, silentLogger())); |
| 103 | + await flush(); // let ScheduleTrigger.start() schedule the job |
| 104 | + |
| 105 | + const jobName = `flow-schedule:${flowName}`; |
| 106 | + expect(job.has(jobName), 'the flow was not bound to the schedule trigger').toBe(true); |
| 107 | + await job.fire(jobName, 'tick-42'); |
| 108 | + |
| 109 | + expect(execute).toHaveBeenCalledTimes(1); |
| 110 | + const context = execute.mock.calls[0][1] as AutomationContext; |
| 111 | + const result = await execute.mock.results[0].value; |
| 112 | + return { engine, context, result }; |
| 113 | +} |
| 114 | + |
| 115 | +// [#17396] The switch is OFF by default in every posture; nothing binds without it. |
| 116 | +withScheduledWorkOn('single'); |
| 117 | + |
| 118 | +describe('[#19900] a scheduled run\'s platform seeds never answer a screen field', () => { |
| 119 | + it('the trigger states that its run has no caller: `callerParamKeys` is an empty list', async () => { |
| 120 | + const { context } = await fireOnce('nightly_ask', [ |
| 121 | + { name: 'schedule', label: 'Schedule', type: 'text', required: true }, |
| 122 | + ]); |
| 123 | + // The seeds are still there — flows read them — they are just not a caller's. |
| 124 | + expect(context.params).toMatchObject({ jobId: 'tick-42', flowName: 'nightly_ask', schedule: CRON }); |
| 125 | + expect(context.callerParamKeys).toEqual([]); |
| 126 | + }); |
| 127 | + |
| 128 | + it.each(['schedule', 'jobId', 'flowName'])( |
| 129 | + 'a required screen field named `%s` pauses the run instead of taking the seed as its answer', |
| 130 | + async (seed) => { |
| 131 | + const { engine, result } = await fireOnce('nightly_ask', [ |
| 132 | + { name: seed, label: seed, type: 'text', required: true }, |
| 133 | + ]); |
| 134 | + // One object, so a skip prints what the run completed WITH. |
| 135 | + expect(result).toMatchObject({ status: 'paused', screen: { nodeId: 'screen_1' } }); |
| 136 | + expect(engine.listSuspendedRuns()).toEqual([ |
| 137 | + expect.objectContaining({ flowName: 'nightly_ask', nodeId: 'screen_1' }), |
| 138 | + ]); |
| 139 | + }, |
| 140 | + ); |
| 141 | + |
| 142 | + it('an all-optional screen named after every seed pauses too — the seeds do not "drive" it', async () => { |
| 143 | + const { result } = await fireOnce('nightly_ask', [ |
| 144 | + { name: 'schedule', label: 'Schedule', type: 'text' }, |
| 145 | + { name: 'jobId', label: 'Job', type: 'text' }, |
| 146 | + { name: 'flowName', label: 'Flow', type: 'text' }, |
| 147 | + ]); |
| 148 | + expect(result).toMatchObject({ status: 'paused', screen: { nodeId: 'screen_1' } }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('control: the same flow on the same engine continues when a context names `schedule` as a caller key', async () => { |
| 152 | + // The pause above is the signal's doing, not "a screen in a schedule flow |
| 153 | + // always pauses": a door that states real caller keys still answers it. |
| 154 | + const engine = new AutomationEngine(silentLogger()); |
| 155 | + installBuiltinNodes(engine, { logger: silentLogger(), getService: () => undefined } as never); |
| 156 | + engine.registerFlow('nightly_ask', scheduledScreenFlow('nightly_ask', [ |
| 157 | + { name: 'schedule', label: 'Schedule', type: 'text', required: true }, |
| 158 | + ])); |
| 159 | + const result = await engine.execute('nightly_ask', { |
| 160 | + event: 'schedule', |
| 161 | + params: { jobId: 'tick-42', flowName: 'nightly_ask', schedule: '0 2 * * *' }, |
| 162 | + callerParamKeys: ['schedule'], |
| 163 | + }); |
| 164 | + expect(result.status).not.toBe('paused'); |
| 165 | + expect(result.success).toBe(true); |
| 166 | + expect(result.output).toMatchObject({ schedule: '0 2 * * *' }); |
| 167 | + expect(engine.listSuspendedRuns()).toEqual([]); |
| 168 | + }); |
| 169 | +}); |
0 commit comments