@@ -7,20 +7,54 @@ import { createTestTenantId } from '../../utils/testTenant';
77
88describe ( 'Context Config CRUD Routes - Integration Tests' , ( ) => {
99 const projectId = 'default' ;
10+ const testGraphId = 'test-graph' ;
11+
12+ // Helper function to create a test graph for context configs
13+ const createTestGraph = async ( { tenantId } : { tenantId : string } ) => {
14+ const graphData = {
15+ id : testGraphId ,
16+ name : 'Test Graph' ,
17+ description : 'Test graph for context config tests' ,
18+ defaultAgentId : 'test-agent' ,
19+ agents : {
20+ 'test-agent' : {
21+ id : 'test-agent' ,
22+ type : 'internal' ,
23+ name : 'Test Agent' ,
24+ description : 'Test agent' ,
25+ prompt : 'You are a test agent' ,
26+ canUse : [ ] ,
27+ } ,
28+ } ,
29+ } ;
30+
31+ const createRes = await makeRequest ( `/tenants/${ tenantId } /projects/${ projectId } /graph` , {
32+ method : 'POST' ,
33+ body : JSON . stringify ( graphData ) ,
34+ } ) ;
35+
36+ if ( createRes . status !== 201 ) {
37+ const errorBody = await createRes . json ( ) ;
38+ throw new Error ( `Failed to create test graph: ${ JSON . stringify ( errorBody ) } ` ) ;
39+ }
40+ } ;
1041
1142 // Helper function to create test context config data
1243 const createContextConfigData = ( {
1344 suffix = '' ,
1445 tenantId = 'default-tenant' ,
1546 projectId = 'default' ,
47+ graphId = 'test-graph' ,
1648 } : {
1749 suffix ?: string ;
1850 tenantId ?: string ;
1951 projectId ?: string ;
52+ graphId ?: string ;
2053 } = { } ) => ( {
2154 id : `test-context-config${ suffix . toLowerCase ( ) . replace ( / \s + / g, '-' ) } -${ nanoid ( 6 ) } ` ,
2255 tenantId,
2356 projectId,
57+ graphId,
2458 name : `Test Context Config${ suffix } ` ,
2559 description : `Test Description${ suffix } ` ,
2660 requestContextSchema : {
@@ -52,11 +86,27 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
5286 const createTestContextConfig = async ( {
5387 tenantId,
5488 suffix = '' ,
89+ skipGraphCreation = false ,
5590 } : {
5691 tenantId : string ;
5792 suffix ?: string ;
93+ skipGraphCreation ?: boolean ;
5894 } ) => {
59- const contextConfigData = createContextConfigData ( { suffix, tenantId, projectId } ) ;
95+ // Create test graph first (unless skipped)
96+ if ( ! skipGraphCreation ) {
97+ try {
98+ await createTestGraph ( { tenantId } ) ;
99+ } catch ( e ) {
100+ // Graph might already exist, that's ok
101+ }
102+ }
103+
104+ const contextConfigData = createContextConfigData ( {
105+ suffix,
106+ tenantId,
107+ projectId,
108+ graphId : testGraphId ,
109+ } ) ;
60110 const createRes = await makeRequest (
61111 `/tenants/${ tenantId } /projects/${ projectId } /context-configs` ,
62112 {
@@ -80,7 +130,12 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
80130 } ) => {
81131 const contextConfigs : Awaited < ReturnType < typeof createTestContextConfig > > [ ] = [ ] ;
82132 for ( let i = 1 ; i <= count ; i ++ ) {
83- const contextConfig = await createTestContextConfig ( { tenantId, suffix : ` ${ i } ` } ) ;
133+ // Only create graph on first iteration
134+ const contextConfig = await createTestContextConfig ( {
135+ tenantId,
136+ suffix : ` ${ i } ` ,
137+ skipGraphCreation : i > 1 ,
138+ } ) ;
84139 contextConfigs . push ( contextConfig ) ;
85140 }
86141 return contextConfigs ;
@@ -307,15 +362,13 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
307362 it ( 'should create a new context config' , async ( ) => {
308363 const tenantId = createTestTenantId ( 'context-configs-create-success' ) ;
309364 await ensureTestProject ( tenantId , projectId ) ;
365+ await createTestGraph ( { tenantId } ) ;
310366 const contextConfigData = createContextConfigData ( { tenantId, projectId } ) ;
311367
312- const res = await makeRequest (
313- `/tenants/${ tenantId } /projects/${ projectId } /context-configs` ,
314- {
315- method : 'POST' ,
316- body : JSON . stringify ( contextConfigData ) ,
317- }
318- ) ;
368+ const res = await makeRequest ( `/tenants/${ tenantId } /projects/${ projectId } /context-configs` , {
369+ method : 'POST' ,
370+ body : JSON . stringify ( contextConfigData ) ,
371+ } ) ;
319372
320373 expect ( res . status ) . toBe ( 201 ) ;
321374
@@ -333,21 +386,20 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
333386 it ( 'should create a new context config with minimal required fields' , async ( ) => {
334387 const tenantId = createTestTenantId ( 'context-configs-create-minimal' ) ;
335388 await ensureTestProject ( tenantId , projectId ) ;
389+ await createTestGraph ( { tenantId } ) ;
336390 const minimalData = {
337391 id : `minimal-context-config-${ nanoid ( 6 ) } ` ,
338392 tenantId,
339393 projectId,
394+ graphId : testGraphId ,
340395 name : 'Minimal Context Config' ,
341396 description : 'Minimal test description' ,
342397 } ;
343398
344- const res = await makeRequest (
345- `/tenants/${ tenantId } /projects/${ projectId } /context-configs` ,
346- {
347- method : 'POST' ,
348- body : JSON . stringify ( minimalData ) ,
349- }
350- ) ;
399+ const res = await makeRequest ( `/tenants/${ tenantId } /projects/${ projectId } /context-configs` , {
400+ method : 'POST' ,
401+ body : JSON . stringify ( minimalData ) ,
402+ } ) ;
351403
352404 expect ( res . status ) . toBe ( 201 ) ;
353405
@@ -363,10 +415,12 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
363415 it ( 'should create a context config with complex fetch definitions' , async ( ) => {
364416 const tenantId = createTestTenantId ( 'context-configs-create-complex' ) ;
365417 await ensureTestProject ( tenantId , projectId ) ;
418+ await createTestGraph ( { tenantId } ) ;
366419 const complexData = {
367420 id : `complex-context-config-${ nanoid ( 6 ) } ` ,
368421 tenantId,
369422 projectId,
423+ graphId : testGraphId ,
370424 name : 'Complex Context Config' ,
371425 description : 'Context config with multiple fetch definitions' ,
372426 requestContextSchema : {
@@ -404,13 +458,10 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
404458 } ,
405459 } ;
406460
407- const res = await makeRequest (
408- `/tenants/${ tenantId } /projects/${ projectId } /context-configs` ,
409- {
410- method : 'POST' ,
411- body : JSON . stringify ( complexData ) ,
412- }
413- ) ;
461+ const res = await makeRequest ( `/tenants/${ tenantId } /projects/${ projectId } /context-configs` , {
462+ method : 'POST' ,
463+ body : JSON . stringify ( complexData ) ,
464+ } ) ;
414465
415466 expect ( res . status ) . toBe ( 201 ) ;
416467
@@ -422,13 +473,10 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
422473 it ( 'should validate required fields' , async ( ) => {
423474 const tenantId = createTestTenantId ( 'context-configs-create-validation' ) ;
424475 await ensureTestProject ( tenantId , projectId ) ;
425- const res = await makeRequest (
426- `/tenants/${ tenantId } /projects/${ projectId } /context-configs` ,
427- {
428- method : 'POST' ,
429- body : JSON . stringify ( { } ) ,
430- }
431- ) ;
476+ const res = await makeRequest ( `/tenants/${ tenantId } /projects/${ projectId } /context-configs` , {
477+ method : 'POST' ,
478+ body : JSON . stringify ( { } ) ,
479+ } ) ;
432480
433481 expect ( res . status ) . toBe ( 400 ) ;
434482 } ) ;
@@ -580,22 +628,21 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
580628 it ( 'should handle context config with empty context variables object' , async ( ) => {
581629 const tenantId = createTestTenantId ( 'context-configs-empty-context-vars' ) ;
582630 await ensureTestProject ( tenantId , projectId ) ;
631+ await createTestGraph ( { tenantId } ) ;
583632 const configData = {
584633 id : `empty-context-vars-${ nanoid ( 6 ) } ` ,
585634 tenantId,
586635 projectId,
636+ graphId : testGraphId ,
587637 name : 'Config with Empty Context Variables' ,
588638 description : 'Test config with empty object' ,
589639 contextVariables : { } ,
590640 } ;
591641
592- const res = await makeRequest (
593- `/tenants/${ tenantId } /projects/${ projectId } /context-configs` ,
594- {
595- method : 'POST' ,
596- body : JSON . stringify ( configData ) ,
597- }
598- ) ;
642+ const res = await makeRequest ( `/tenants/${ tenantId } /projects/${ projectId } /context-configs` , {
643+ method : 'POST' ,
644+ body : JSON . stringify ( configData ) ,
645+ } ) ;
599646
600647 expect ( res . status ) . toBe ( 201 ) ;
601648 const body = await res . json ( ) ;
@@ -605,22 +652,21 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
605652 it ( 'should handle context config with null requestContext' , async ( ) => {
606653 const tenantId = createTestTenantId ( 'context-configs-null-request-context' ) ;
607654 await ensureTestProject ( tenantId , projectId ) ;
655+ await createTestGraph ( { tenantId } ) ;
608656 const configData = {
609657 id : `null-request-context-${ nanoid ( 6 ) } ` ,
610658 tenantId,
611659 projectId,
660+ graphId : testGraphId ,
612661 name : 'Config with Null Request Context' ,
613662 description : 'Test config with null request context' ,
614663 requestContextSchema : null ,
615664 } ;
616665
617- const res = await makeRequest (
618- `/tenants/${ tenantId } /projects/${ projectId } /context-configs` ,
619- {
620- method : 'POST' ,
621- body : JSON . stringify ( configData ) ,
622- }
623- ) ;
666+ const res = await makeRequest ( `/tenants/${ tenantId } /projects/${ projectId } /context-configs` , {
667+ method : 'POST' ,
668+ body : JSON . stringify ( configData ) ,
669+ } ) ;
624670
625671 expect ( res . status ) . toBe ( 201 ) ;
626672 const body = await res . json ( ) ;
@@ -630,10 +676,12 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
630676 it ( 'should preserve complex nested data structures' , async ( ) => {
631677 const tenantId = createTestTenantId ( 'context-configs-complex-nested' ) ;
632678 await ensureTestProject ( tenantId , projectId ) ;
679+ await createTestGraph ( { tenantId } ) ;
633680 const complexConfig = {
634681 id : `complex-nested-config-${ nanoid ( 6 ) } ` ,
635682 tenantId,
636683 projectId,
684+ graphId : testGraphId ,
637685 name : 'Complex Nested Config' ,
638686 description : 'Config with deeply nested structures' ,
639687 requestContextSchema : {
@@ -683,13 +731,10 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
683731 } ,
684732 } ;
685733
686- const res = await makeRequest (
687- `/tenants/${ tenantId } /projects/${ projectId } /context-configs` ,
688- {
689- method : 'POST' ,
690- body : JSON . stringify ( complexConfig ) ,
691- }
692- ) ;
734+ const res = await makeRequest ( `/tenants/${ tenantId } /projects/${ projectId } /context-configs` , {
735+ method : 'POST' ,
736+ body : JSON . stringify ( complexConfig ) ,
737+ } ) ;
693738 expect ( res . status ) . toBe ( 201 ) ;
694739 const body = await res . json ( ) ;
695740 expect ( body . data ) . toMatchObject ( complexConfig ) ;
@@ -739,10 +784,12 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
739784 it ( 'should create with empty contextVariables treated as null' , async ( ) => {
740785 const tenantId = createTestTenantId ( 'context-configs-create-empty-context-vars' ) ;
741786 await ensureTestProject ( tenantId , projectId ) ;
787+ await createTestGraph ( { tenantId } ) ;
742788 const configData = {
743789 id : `empty-context-vars-config-${ nanoid ( 6 ) } ` ,
744790 tenantId,
745791 projectId,
792+ graphId : testGraphId ,
746793 name : 'Config with Empty Context Variables' ,
747794 description : 'Test config with empty object' ,
748795 contextVariables : { } ,
@@ -821,10 +868,12 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
821868 it ( 'should create with requestContextSchema as null' , async ( ) => {
822869 const tenantId = createTestTenantId ( 'context-configs-create-null-request-schema' ) ;
823870 await ensureTestProject ( tenantId , projectId ) ;
871+ await createTestGraph ( { tenantId } ) ;
824872 const configData = {
825873 id : `null-request-schema-config-${ nanoid ( 6 ) } ` ,
826874 tenantId,
827875 projectId,
876+ graphId : testGraphId ,
828877 name : 'Config with Null Request Schema' ,
829878 description : 'Test config with null request schema' ,
830879 requestContextSchema : null ,
@@ -933,10 +982,12 @@ describe('Context Config CRUD Routes - Integration Tests', () => {
933982 it ( 'should handle creation with minimal data and consistent null defaults' , async ( ) => {
934983 const tenantId = createTestTenantId ( 'context-configs-minimal-with-nulls' ) ;
935984 await ensureTestProject ( tenantId , projectId ) ;
985+ await createTestGraph ( { tenantId } ) ;
936986 const minimalData = {
937987 id : `minimal-null-defaults-config-${ nanoid ( 6 ) } ` ,
938988 tenantId,
939989 projectId,
990+ graphId : testGraphId ,
940991 name : 'Minimal Config' ,
941992 description : 'Minimal config with no optional fields' ,
942993 } ;
0 commit comments