Skip to content

Commit 5dcd9fc

Browse files
committed
test(cli): pin os validate/build to the flow-cycle corpus (red before the fix)
Claude-Session: https://claude.ai/code/session_01ADHkmSGEsjUPQp4S968p2X Co-authored-by: Claude <noreply@anthropic.com>
1 parent aca58cd commit 5dcd9fc

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎packages/cli/src/utils/flow-region-shape.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import { describe, expect, it } from 'vitest';
1919

20+
import { undeclaredFlowCycleMessage } from '@objectstack/metadata-core';
21+
import { FLOW_CYCLE_CORPUS } from '@objectstack/metadata-core/testing';
22+
2023
import { findFlowRegionRefusals } from './flow-region-shape.js';
2124

2225
/** A body `FlowSchema` refuses: `nodes` must be an array. */
@@ -46,3 +49,40 @@ describe('[#29059] a flow the flow schema refuses is a row, never a silent pass'
4649
expect(located(rows)).toEqual([{ path: 'packages[1].manifest.flows[0]', code: 'VALIDATION_ERROR', flow: 'rgn_broken' }]);
4750
});
4851
});
52+
53+
/**
54+
* [#29051] An undeclared cycle in a flow's TOP-LEVEL graph (ADR-0044) is a row.
55+
*
56+
* The engine refuses such a flow at registration with
57+
* `findUndeclaredFlowCycle` (`@objectstack/metadata-core`), and the walk asks
58+
* the same judge. The cases are the judge's own corpus, the one the engine's
59+
* registration and the runtime publish gate are driven from, so the CLI
60+
* refuses exactly the graphs they refuse, in the engine's sentence.
61+
*/
62+
describe('[#29051] a top-level cycle not declared back is refused, as the engine refuses it', () => {
63+
for (const c of FLOW_CYCLE_CORPUS) {
64+
it(c.title, () => {
65+
const rows = findFlowRegionRefusals({ flows: [c.flow] });
66+
if (c.cycle === null) {
67+
expect(rows).toEqual([]);
68+
return;
69+
}
70+
expect(located(rows)).toEqual([{ path: 'flows[0]', code: 'VALIDATION_ERROR', flow: c.flow.name }]);
71+
expect(rows[0]!.message).toBe(`Flow '${c.flow.name}' would be refused at registration: ${undeclaredFlowCycleMessage(c.cycle)}`);
72+
});
73+
}
74+
75+
it('a flow with both a cycle and a malformed region is refused for the cycle first, in the engine\'s order', () => {
76+
const rework = FLOW_CYCLE_CORPUS.find((c) => c.flow.name === 'cyc_rework_untyped')!;
77+
const flow = {
78+
...rework.flow,
79+
nodes: rework.flow.nodes.map((node) =>
80+
node.id === 'check'
81+
? { ...node, type: 'parallel', config: { branches: [{ nodes: [{ id: 'only', type: 'assignment', label: 'only' }], edges: [] }] } }
82+
: node),
83+
};
84+
const rows = findFlowRegionRefusals({ flows: [flow] });
85+
expect(rows).toHaveLength(1);
86+
expect(rows[0]!.message).toContain(undeclaredFlowCycleMessage(rework.cycle!));
87+
});
88+
});

‎packages/cli/test/validate-flow-region-shape.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { join, resolve } from 'node:path';
2323
import { fileURLToPath } from 'node:url';
2424
import Compile from '../src/commands/compile';
2525
import Validate from '../src/commands/validate';
26+
import { undeclaredFlowCycleMessage } from '@objectstack/metadata-core';
27+
import { FLOW_CYCLE_CORPUS } from '@objectstack/metadata-core/testing';
2628
import { defineStackSource, linkSpec } from './helpers/define-stack-fixture.js';
2729

2830
const 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

144150
let 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

Comments
 (0)