@@ -23,6 +23,8 @@ import { join, resolve } from 'node:path';
2323import { fileURLToPath } from 'node:url' ;
2424import Compile from '../src/commands/compile' ;
2525import Validate from '../src/commands/validate' ;
26+ import { undeclaredFlowCycleMessage } from '@objectstack/metadata-core' ;
27+ import { FLOW_CYCLE_CORPUS } from '@objectstack/metadata-core/testing' ;
2628import { defineStackSource , linkSpec } from './helpers/define-stack-fixture.js' ;
2729
2830const CLI_ROOT = resolve ( fileURLToPath ( import . meta. url ) , '..' , '..' ) ;
@@ -139,6 +141,10 @@ const CASES: Record<string, { stack: Record<string, unknown>; refusal?: string }
139141 stack : assembledWith ( flow ( 'rgn_ok' , guard ( fanOut ( true ) ) ) , flow ( 'rgn_ops_lead_time' , guard ( fanOut ( false ) ) ) ) ,
140142 refusal : "try_catch 'guard' catch: region must be single-exit but has 3: x, y, z" ,
141143 } ,
144+ // [#29051] The flow-cycle judge's whole corpus in one stack, and the cases it
145+ // must pass on their own.
146+ cycles : { stack : stackWith ( FLOW_CYCLE_CORPUS . map ( ( c ) => c . flow ) ) } ,
147+ cyclesDeclared : { stack : stackWith ( FLOW_CYCLE_CORPUS . filter ( ( c ) => c . cycle === null ) . map ( ( c ) => c . flow ) ) } ,
142148} ;
143149
144150let root = '' ;
@@ -211,3 +217,48 @@ describe('[#28893] validate and build refuse a flow region the engine refuses, w
211217 expect ( built . code ) . toBe ( 0 ) ;
212218 } , 60_000 ) ;
213219} ) ;
220+
221+ /**
222+ * [#29051] A flow whose TOP-LEVEL graph closes a cycle with no `type: 'back'`
223+ * edge (ADR-0044). The engine refuses it at registration with
224+ * `findUndeclaredFlowCycle` (`@objectstack/metadata-core`); before this,
225+ * `os validate` and `os build` passed it with exit 0 and the flow never ran.
226+ * Driven from the judge's own corpus, so the doors refuse exactly the cases
227+ * the engine and the publish gate refuse, each at its own flow, in the
228+ * engine's sentence.
229+ */
230+ describe ( '[#29051] validate and build refuse an undeclared top-level flow cycle, with the engine\'s sentence' , ( ) => {
231+ const refused = FLOW_CYCLE_CORPUS
232+ . map ( ( c , i ) => ( { c, i } ) )
233+ . filter ( ( { c } ) => c . cycle !== null )
234+ . map ( ( { c, i } ) => ( { path : `flows[${ i } ]` , code : 'VALIDATION_ERROR' , flow : c . flow . name , cycle : undeclaredFlowCycleMessage ( c . cycle ! ) } ) ) ;
235+ const rowsOf = ( errors : Array < Record < string , unknown > > ) =>
236+ errors . filter ( ( r ) => String ( r . message ) . includes ( 'Flow contains a cycle' ) ) ;
237+ const expectRefused = ( errors : Array < Record < string , unknown > > ) => {
238+ const rows = rowsOf ( errors ) ;
239+ expect ( rows . map ( ( { path, code, flow } ) => ( { path, code, flow } ) ) , JSON . stringify ( errors , null , 2 ) )
240+ . toEqual ( refused . map ( ( { path, code, flow } ) => ( { path, code, flow } ) ) ) ;
241+ rows . forEach ( ( row , k ) => expect ( String ( row . message ) ) . toContain ( refused [ k ] ! . cycle ) ) ;
242+ } ;
243+
244+ it ( 'validate exits 1 with one row per refused corpus flow; build refuses the same and writes nothing' , async ( ) => {
245+ const validated = await validate ( 'cycles' , '--json' ) ;
246+ expect ( validated . code , validated . stdout ) . toBe ( 1 ) ;
247+ expectRefused ( json ( validated , 'validate cycles' ) . errors as Array < Record < string , unknown > > ) ;
248+
249+ const built = await build ( 'cycles' , '--json' ) ;
250+ expect ( built . code , built . stdout ) . toBe ( 1 ) ;
251+ const payload = json ( built , 'build cycles' ) ;
252+ expect ( payload . success ) . toBe ( false ) ;
253+ expectRefused ( payload . errors as Array < Record < string , unknown > > ) ;
254+ expect ( existsSync ( fixtures . cycles . dist ) ) . toBe ( false ) ;
255+ } , 60_000 ) ;
256+
257+ it ( 'CONTROL: the corpus flows whose loops are declared back (and the acyclic ones) validate and build' , async ( ) => {
258+ const validated = await validate ( 'cyclesDeclared' , '--json' ) ;
259+ expect ( validated . code , validated . stdout ) . toBe ( 0 ) ;
260+ const built = await build ( 'cyclesDeclared' , '--json' ) ;
261+ expect ( json ( built , 'build cyclesDeclared' ) . success , built . stdout ) . toBe ( true ) ;
262+ expect ( built . code ) . toBe ( 0 ) ;
263+ } , 60_000 ) ;
264+ } ) ;
0 commit comments