11/**
22 * Real PostgreSQL coverage for the lint's child workflow checks: the children a Workflow block
3- * calls are read from the workflow and block tables, and a graph is linted against them. A
4- * responding child's `result.<key>` reads nothing unless `<key>` is a Response output, and a
5- * deploy reports a called child with no deployment, which a deployed run cannot call.
3+ * calls are read from the workflow, graph and deployment tables, and a graph is linted against
4+ * them. The `result.<key>` of a child whose every run ends at a Response block reads nothing
5+ * unless `<key>` is a Response output, judged on the version the caller's run calls, and a deploy
6+ * reports a called child with no deployment, which a deployed run cannot call.
67 */
78import { mkdir , writeFile } from 'node:fs/promises'
89import { dirname , resolve } from 'node:path'
910import { db } from '@sim/db'
10- import { user , workflow , workspace } from '@sim/db/schema'
11+ import { user , workflow , workflowDeploymentVersion , workspace } from '@sim/db/schema'
1112import { deleteWorkspaceFixture , insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures'
1213import { getErrorMessage } from '@sim/utils/errors'
1314import { generateId } from '@sim/utils/id'
1415import { saveWorkflowToNormalizedTables } from '@sim/workflow-persistence/save'
1516import type { BlockState , WorkflowState } from '@sim/workflow-types/workflow'
17+ import type { Edge } from '@xyflow/react'
1618import { eq , inArray } from 'drizzle-orm'
1719import { afterAll , beforeAll , expect , it } from 'vitest'
1820import { buildWorkflowLintReport } from '@/lib/workflows/editing/lint-report'
@@ -64,10 +66,69 @@ function block(
6466 } as BlockState
6567}
6668
67- /** Stores a child workflow whose only blocks are a Start and, when given, a Response block. */
69+ function edge ( source : string , target : string , sourceHandle = 'source' ) : Edge {
70+ return { id : generateId ( ) , source, target, sourceHandle, targetHandle : 'target' }
71+ }
72+
73+ /**
74+ * A child graph: a Start trigger wired to a Response block "Reply" (disabled with `'disabled'`),
75+ * or a Start trigger alone with `'none'`. Block ids are fresh, as stored block ids are global.
76+ */
77+ function replyGraph ( reply : 'enabled' | 'disabled' | 'none' ) : WorkflowState {
78+ const startId = generateId ( )
79+ const blocks : Record < string , BlockState > = { [ startId ] : block ( startId , 'start_trigger' , 'Start' ) }
80+ const edges : Edge [ ] = [ ]
81+ if ( reply !== 'none' ) {
82+ const replyId = generateId ( )
83+ blocks [ replyId ] = block (
84+ replyId ,
85+ 'response' ,
86+ 'Reply' ,
87+ { dataMode : 'json' , data : '{"mobile": "1"}' } ,
88+ reply === 'enabled'
89+ )
90+ edges . push ( edge ( startId , replyId ) )
91+ }
92+ return { blocks, edges, loops : { } , parallels : { } }
93+ }
94+
95+ /**
96+ * A child graph whose condition "Gate" sends its `if` branch to a Response block and its else
97+ * branch to `elseTarget`: a second Response block, or a function that ends the run there.
98+ */
99+ function gateGraph ( elseTarget : 'response' | 'function' ) : WorkflowState {
100+ const startId = generateId ( )
101+ const gateId = generateId ( )
102+ const replyId = generateId ( )
103+ const otherId = generateId ( )
104+ const conditions = JSON . stringify ( [
105+ { id : `${ gateId } -if` , title : 'if' , value : 'true' } ,
106+ { id : `${ gateId } -else` , title : 'else' , value : '' } ,
107+ ] )
108+ const blocks : Record < string , BlockState > = {
109+ [ startId ] : block ( startId , 'start_trigger' , 'Start' ) ,
110+ [ gateId ] : block ( gateId , 'condition' , 'Gate' , { conditions } ) ,
111+ [ replyId ] : block ( replyId , 'response' , 'Reply' , { dataMode : 'json' , data : '{"mobile": "1"}' } ) ,
112+ [ otherId ] :
113+ elseTarget === 'response'
114+ ? block ( otherId , 'response' , 'Other' , { dataMode : 'json' , data : '{"mobile": "2"}' } )
115+ : block ( otherId , 'function' , 'Other' , { code : "return { mobile: '2' }" } ) ,
116+ }
117+ const edges = [
118+ edge ( startId , gateId ) ,
119+ edge ( gateId , replyId , `condition-${ gateId } -if` ) ,
120+ edge ( gateId , otherId , `condition-${ gateId } -else` ) ,
121+ ]
122+ return { blocks, edges, loops : { } , parallels : { } }
123+ }
124+
125+ /**
126+ * Stores a child workflow with draft `graph` (a Start trigger alone by default) and, with
127+ * `deployed`, an active deployment of that graph (`true`) or of another one.
128+ */
68129async function seedChild (
69130 name : string ,
70- options : { response ?: 'enabled' | 'disabled' ; deployed ?: boolean ; workspace ?: string } = { }
131+ options : { graph ?: WorkflowState ; deployed ?: boolean | WorkflowState ; workspace ?: string } = { }
71132) : Promise < string > {
72133 const id = generateId ( )
73134 const now = new Date ( )
@@ -76,26 +137,23 @@ async function seedChild(
76137 userId : ownerId ,
77138 workspaceId : options . workspace ?? workspaceId ,
78139 name,
79- isDeployed : options . deployed ?? false ,
140+ isDeployed : Boolean ( options . deployed ) ,
80141 lastSynced : now ,
81142 createdAt : now ,
82143 updatedAt : now ,
83144 } )
84- const startId = generateId ( )
85- const blocks : Record < string , BlockState > = { [ startId ] : block ( startId , 'start_trigger' , 'Start' ) }
86- if ( options . response ) {
87- const replyId = generateId ( )
88- blocks [ replyId ] = block (
89- replyId ,
90- 'response' ,
91- 'Reply' ,
92- { dataMode : 'json' , data : '{"mobile": "1"}' } ,
93- options . response === 'enabled'
94- )
95- }
96- const state : WorkflowState = { blocks, edges : [ ] , loops : { } , parallels : { } }
97- const saved = await saveWorkflowToNormalizedTables ( id , state )
145+ const graph = options . graph ?? replyGraph ( 'none' )
146+ const saved = await saveWorkflowToNormalizedTables ( id , graph )
98147 if ( ! saved . success ) throw new Error ( `Seeding the child failed: ${ saved . error } ` )
148+ if ( options . deployed ) {
149+ await db . insert ( workflowDeploymentVersion ) . values ( {
150+ id : generateId ( ) ,
151+ workflowId : id ,
152+ version : 1 ,
153+ state : options . deployed === true ? graph : options . deployed ,
154+ isActive : true ,
155+ } )
156+ }
99157 return id
100158}
101159
@@ -122,6 +180,14 @@ function lint(graph: ReturnType<typeof parent>, deployed = false) {
122180 )
123181}
124182
183+ /** The references a parent reading `<call.result.mobile>` from `childId` is told read nothing. */
184+ async function childResultValues ( childId : string , deployed = false ) {
185+ const report = await lint ( parent ( childId , 'return <call.result.mobile>' ) , deployed )
186+ return report . unresolvedReferences
187+ . filter ( ( ref ) => ref . kind === 'child-result' )
188+ . map ( ( ref ) => ref . value )
189+ }
190+
125191beforeAll ( async ( ) => {
126192 const now = new Date ( )
127193 await db . insert ( user ) . values ( {
@@ -154,7 +220,7 @@ afterAll(async () => {
154220check (
155221 "a responding child's result.<key> is reported with result.data.<key> as the fix" ,
156222 async ( ) => {
157- const childId = await seedChild ( 'Responder' , { response : 'enabled' , deployed : true } )
223+ const childId = await seedChild ( 'Responder' , { graph : replyGraph ( 'enabled' ) , deployed : true } )
158224
159225 const report = await lint (
160226 parent (
@@ -180,8 +246,11 @@ check(
180246 'a child with no enabled Response block, or in another workspace, is not judged' ,
181247 async ( ) => {
182248 const silent = await seedChild ( 'Silent' )
183- const disabled = await seedChild ( 'Disabled reply' , { response : 'disabled' } )
184- const foreign = await seedChild ( 'Foreign' , { response : 'enabled' , workspace : otherWorkspaceId } )
249+ const disabled = await seedChild ( 'Disabled reply' , { graph : replyGraph ( 'disabled' ) } )
250+ const foreign = await seedChild ( 'Foreign' , {
251+ graph : replyGraph ( 'enabled' ) ,
252+ workspace : otherWorkspaceId ,
253+ } )
185254
186255 for ( const childId of [ silent , disabled , foreign ] ) {
187256 const report = await lint ( parent ( childId , 'return <call.result.mobile>' ) )
@@ -207,3 +276,31 @@ check('a deploy reports a called child that is not deployed; a draft write does
207276 expect ( childFindings ( await lint ( parent ( undeployed , 'return 1' ) ) ) ) . toEqual ( [ ] )
208277 expect ( childFindings ( await lint ( parent ( deployed , 'return 1' ) , true ) ) ) . toEqual ( [ ] )
209278} )
279+
280+ check (
281+ 'a child whose run can end outside a Response block is not judged; one that always responds is' ,
282+ async ( ) => {
283+ const branchy = await seedChild ( 'Branchy' , { graph : gateGraph ( 'function' ) } )
284+ const covered = await seedChild ( 'Covered' , { graph : gateGraph ( 'response' ) } )
285+ expect ( await childResultValues ( branchy ) ) . toEqual ( [ ] )
286+ expect ( await childResultValues ( covered ) ) . toEqual ( [ [ '<call.result.mobile>' ] ] )
287+ }
288+ )
289+
290+ check (
291+ "a deploy judges a child's result by its active deployment; a draft write by its draft" ,
292+ async ( ) => {
293+ const respondsInDraft = await seedChild ( 'Draft responder' , {
294+ graph : replyGraph ( 'enabled' ) ,
295+ deployed : replyGraph ( 'none' ) ,
296+ } )
297+ const respondsDeployed = await seedChild ( 'Deployed responder' , {
298+ graph : replyGraph ( 'none' ) ,
299+ deployed : replyGraph ( 'enabled' ) ,
300+ } )
301+ expect ( await childResultValues ( respondsInDraft ) ) . toEqual ( [ [ '<call.result.mobile>' ] ] )
302+ expect ( await childResultValues ( respondsInDraft , true ) ) . toEqual ( [ ] )
303+ expect ( await childResultValues ( respondsDeployed ) ) . toEqual ( [ ] )
304+ expect ( await childResultValues ( respondsDeployed , true ) ) . toEqual ( [ [ '<call.result.mobile>' ] ] )
305+ }
306+ )
0 commit comments