|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [#29059] The boot banner counts declared flows the engine REFUSED at |
| 4 | +// registration, beside the declared count. |
| 5 | +// |
| 6 | +// A flow `registerFlow` refuses is logged once at WARN and the boot goes on |
| 7 | +// without it (#28893's second ask; that is unchanged here). Before this the |
| 8 | +// banner's Flows line counted only what armed, so a refused flow showed only |
| 9 | +// as a per-flow line under Boot diagnostics, and a boot whose every declared |
| 10 | +// flow was refused printed no Flows line at all. |
| 11 | +// |
| 12 | +// The count is read off the engine's own record: the host's sync calls |
| 13 | +// `recordFlowRefusal` when `registerFlow` throws, and the banner asks |
| 14 | +// `getFlowRegistration(name)` for each declared name, the read the metadata |
| 15 | +// door already serves. So the engine here is the REAL one (dist, already in |
| 16 | +// `KNOWN_UNALIASED_TEST_IMPORTS['@objectstack/cli']`), refusing a real |
| 17 | +// malformed region, not a fake that answers `refused` by fiat. Locally: build |
| 18 | +// `@objectstack/service-automation` before running this. |
| 19 | + |
| 20 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 21 | +import { AutomationEngine } from '@objectstack/service-automation'; |
| 22 | +import { collectAutomationSummary } from './serve.js'; |
| 23 | +import { printServerReady, type ServerReadyOptions } from '../utils/format.js'; |
| 24 | + |
| 25 | +const quiet = () => ({ debug() {}, info() {}, warn() {}, error() {} }) as never; |
| 26 | + |
| 27 | +const kernelWith = (automation: unknown) => ({ |
| 28 | + getService(name: string) { |
| 29 | + if (name === 'automation') return automation; |
| 30 | + throw new Error(`Service '${name}' not found`); |
| 31 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +const n = (id: string, type = 'assignment', extra: Record<string, unknown> = {}) => ({ id, type, label: id, ...extra }); |
| 35 | +const e = (id: string, source: string, target: string) => ({ id, source, target, type: 'default' }); |
| 36 | + |
| 37 | +/** An autolaunched flow: start → <middle> → end. */ |
| 38 | +const flow = (name: string, middle: Record<string, unknown>) => ({ |
| 39 | + name, label: name, type: 'autolaunched', status: 'active', |
| 40 | + nodes: [n('start', 'start'), middle, n('end', 'end')], |
| 41 | + edges: [e('e1', 'start', String(middle.id)), e('e2', String(middle.id), 'end')], |
| 42 | +}); |
| 43 | + |
| 44 | +/** A `parallel` with one branch: `validateControlFlow` refuses it at registration. */ |
| 45 | +const refusedFlow = (name: string) => flow(name, n('fan', 'parallel', { config: { branches: [{ nodes: [n('only')], edges: [] }] } })); |
| 46 | +const goodFlow = (name: string) => flow(name, n('work')); |
| 47 | + |
| 48 | +/** Register each flow the way the host's sync does: a refusal is recorded, never thrown on. */ |
| 49 | +function engineWith(flows: Array<{ name: string }>): AutomationEngine { |
| 50 | + const engine = new AutomationEngine(quiet()); |
| 51 | + for (const def of flows) { |
| 52 | + try { |
| 53 | + engine.registerFlow(def.name, def as never); |
| 54 | + } catch (err) { |
| 55 | + engine.recordFlowRefusal(def.name, err); |
| 56 | + } |
| 57 | + } |
| 58 | + return engine; |
| 59 | +} |
| 60 | + |
| 61 | +const base: ServerReadyOptions = { |
| 62 | + externalBaseOrigin: 'http://localhost:3000', |
| 63 | + httpServer: 'composed', |
| 64 | + configFile: 'objectstack.config.ts', |
| 65 | + isDev: true, |
| 66 | + pluginCount: 1, |
| 67 | +}; |
| 68 | + |
| 69 | +describe('[#29059] the boot banner counts flows refused at registration', () => { |
| 70 | + let lines: string[]; |
| 71 | + let spy: ReturnType<typeof vi.spyOn>; |
| 72 | + beforeEach(() => { |
| 73 | + lines = []; |
| 74 | + // stderr, not stdout (#7915) — the whole banner is a diagnostic. |
| 75 | + spy = vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => { |
| 76 | + lines.push(args.join(' ')); |
| 77 | + }); |
| 78 | + }); |
| 79 | + afterEach(() => spy.mockRestore()); |
| 80 | + |
| 81 | + const flowsLines = () => lines.filter((l) => l.includes('Flows:')); |
| 82 | + |
| 83 | + it('one of two declared flows refused: the count rides beside the declared count', () => { |
| 84 | + const declared = [goodFlow('ok_flow'), refusedFlow('fan_flow')]; |
| 85 | + const engine = engineWith(declared); |
| 86 | + // The engine really refused it: the record is its own, not the test's. |
| 87 | + expect(engine.getFlowRegistration('fan_flow').state).toBe('refused'); |
| 88 | + |
| 89 | + const summary = collectAutomationSummary(kernelWith(engine), declared.length, declared.map((f) => f.name))!; |
| 90 | + expect(summary).toMatchObject({ declaredFlowCount: 2, refusedCount: 1, flowCount: 1 }); |
| 91 | + |
| 92 | + printServerReady({ ...base, automation: summary }); |
| 93 | + expect(flowsLines().filter((l) => l.includes('2 flow(s) declared, 1 refused at registration'))).toHaveLength(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it('every declared flow refused: the banner still says so, though nothing registered', () => { |
| 97 | + const declared = [refusedFlow('fan_flow')]; |
| 98 | + const summary = collectAutomationSummary(kernelWith(engineWith(declared)), 1, ['fan_flow'])!; |
| 99 | + expect(summary).toMatchObject({ declaredFlowCount: 1, refusedCount: 1, flowCount: 0 }); |
| 100 | + |
| 101 | + printServerReady({ ...base, automation: summary }); |
| 102 | + expect(flowsLines().filter((l) => l.includes('1 flow(s) declared, 1 refused at registration'))).toHaveLength(1); |
| 103 | + }); |
| 104 | + |
| 105 | + it('CONTROL: nothing refused prints no refused line', () => { |
| 106 | + const declared = [goodFlow('ok_flow')]; |
| 107 | + const summary = collectAutomationSummary(kernelWith(engineWith(declared)), 1, ['ok_flow'])!; |
| 108 | + expect(summary.refusedCount).toBe(0); |
| 109 | + |
| 110 | + printServerReady({ ...base, automation: summary }); |
| 111 | + expect(lines.filter((l) => l.includes('refused at registration'))).toEqual([]); |
| 112 | + expect(flowsLines()).toHaveLength(1); |
| 113 | + }); |
| 114 | + |
| 115 | + it('an engine that does not answer the read degrades to a zero count, not a crash', () => { |
| 116 | + const older = { getFlowRuntimeStates: () => [{ name: 'ok_flow', enabled: true, bound: false }] }; |
| 117 | + const summary = collectAutomationSummary(kernelWith(older), 2, ['ok_flow', 'fan_flow'])!; |
| 118 | + expect(summary.refusedCount).toBe(0); |
| 119 | + }); |
| 120 | +}); |
0 commit comments