@elevasis/core 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,314 +1,315 @@
1
- /**
2
- * Test: Registry Validation Utilities
3
- * Tests for ExecutionInterface-to-inputSchema validation
4
- */
5
-
6
- import { afterEach, describe, it, expect, vi } from 'vitest'
1
+ /**
2
+ * Test: Registry Validation Utilities
3
+ * Tests for ExecutionInterface-to-inputSchema validation
4
+ */
5
+
6
+ import { afterEach, describe, it, expect, vi } from 'vitest'
7
7
  import { z } from 'zod'
8
8
  import {
9
9
  validateExecutionInterface,
10
10
  validateDeclaredSystemInterfaceReadiness,
11
11
  validateResourceGovernance,
12
+ detectMissingApiInterfaceDeclarations,
12
13
  RegistryValidationError
13
14
  } from '../validation'
14
- import { ResourceRegistry } from '../resource-registry'
15
- import type { WorkflowDefinition } from '../../../execution/engine/workflow/types'
16
- import type { AgentDefinition } from '../../../execution/engine/agent/core/types'
17
- import type { OrganizationRegistry } from '../resource-registry'
18
- import type { ResourceEntry } from '../../../organization-model/domains/resources'
19
-
20
- describe('validateExecutionInterface', () => {
21
- describe('required field validation', () => {
22
- it('should pass when all required schema fields have form fields', () => {
23
- const inputSchema = z.object({
24
- name: z.string(),
25
- email: z.string()
26
- })
27
-
28
- const executionInterface = {
29
- form: {
30
- fields: [
31
- { name: 'name', required: true },
32
- { name: 'email', required: true }
33
- ]
34
- }
35
- }
36
-
37
- expect(() => {
38
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
39
- }).not.toThrow()
40
- })
41
-
42
- it('should throw when required schema field is missing from form', () => {
43
- const inputSchema = z.object({
44
- name: z.string(),
45
- email: z.string()
46
- })
47
-
48
- const executionInterface = {
49
- form: {
50
- fields: [
51
- { name: 'name', required: true }
52
- // Missing 'email' field
53
- ]
54
- }
55
- }
56
-
57
- expect(() => {
58
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
59
- }).toThrow(RegistryValidationError)
60
- expect(() => {
61
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
62
- }).toThrow('missing required field "email"')
63
- })
64
-
65
- it('should throw when form field is not marked required but schema field is required', () => {
66
- const inputSchema = z.object({
67
- name: z.string() // Required
68
- })
69
-
70
- const executionInterface = {
71
- form: {
72
- fields: [
73
- { name: 'name', required: false } // Not marked required
74
- ]
75
- }
76
- }
77
-
78
- expect(() => {
79
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
80
- }).toThrow(RegistryValidationError)
81
- expect(() => {
82
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
83
- }).toThrow('should be required')
84
- })
85
-
86
- it('should pass when optional schema field is missing from form', () => {
87
- const inputSchema = z.object({
88
- name: z.string(),
89
- description: z.string().optional()
90
- })
91
-
92
- const executionInterface = {
93
- form: {
94
- fields: [
95
- { name: 'name', required: true }
96
- // 'description' is optional, so OK to omit
97
- ]
98
- }
99
- }
100
-
101
- expect(() => {
102
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
103
- }).not.toThrow()
104
- })
105
-
106
- it('should pass when optional schema field has non-required form field', () => {
107
- const inputSchema = z.object({
108
- name: z.string(),
109
- description: z.string().optional()
110
- })
111
-
112
- const executionInterface = {
113
- form: {
114
- fields: [
115
- { name: 'name', required: true },
116
- { name: 'description', required: false }
117
- ]
118
- }
119
- }
120
-
121
- expect(() => {
122
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
123
- }).not.toThrow()
124
- })
125
- })
126
-
127
- describe('field mapping validation', () => {
128
- it('should pass with valid field mappings', () => {
129
- const inputSchema = z.object({
130
- taskName: z.string(),
131
- priority: z.string()
132
- })
133
-
134
- const executionInterface = {
135
- form: {
136
- fields: [
137
- { name: 'task_name', required: true },
138
- { name: 'prio', required: true }
139
- ],
140
- fieldMappings: {
141
- task_name: 'taskName',
142
- prio: 'priority'
143
- }
144
- }
145
- }
146
-
147
- expect(() => {
148
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
149
- }).not.toThrow()
150
- })
151
-
152
- it('should throw when field mapping points to non-existent schema field', () => {
153
- const inputSchema = z.object({
154
- name: z.string()
155
- })
156
-
157
- // Include a valid mapping for 'name' so we get past the required field check,
158
- // then add an extra field that maps to a non-existent schema field
159
- const executionInterface = {
160
- form: {
161
- fields: [
162
- { name: 'userName', required: true },
163
- { name: 'extraField', required: false }
164
- ],
165
- fieldMappings: {
166
- userName: 'name', // Valid mapping
167
- extraField: 'nonExistentField' // Invalid mapping
168
- }
169
- }
170
- }
171
-
172
- expect(() => {
173
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
174
- }).toThrow(RegistryValidationError)
175
- expect(() => {
176
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
177
- }).toThrow('maps to non-existent schema field "nonExistentField"')
178
- })
179
-
180
- it('should throw when form field without mapping has no matching schema field', () => {
181
- const inputSchema = z.object({
182
- name: z.string()
183
- })
184
-
185
- const executionInterface = {
186
- form: {
187
- fields: [
188
- { name: 'name', required: true },
189
- { name: 'unknownField', required: false }
190
- ]
191
- }
192
- }
193
-
194
- expect(() => {
195
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
196
- }).toThrow(RegistryValidationError)
197
- expect(() => {
198
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
199
- }).toThrow('maps to non-existent schema field "unknownField"')
200
- })
201
-
202
- it('should throw with flatten suggestion when form field uses nested notation', () => {
203
- const inputSchema = z.object({
204
- name: z.string()
205
- })
206
-
207
- const executionInterface = {
208
- form: {
209
- fields: [
210
- { name: 'name', required: true },
211
- { name: 'criteria.targetTitles', required: true }
212
- ]
213
- }
214
- }
215
-
216
- expect(() => {
217
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
218
- }).toThrow(RegistryValidationError)
219
- expect(() => {
220
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
221
- }).toThrow('uses nested notation')
222
- expect(() => {
223
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
224
- }).toThrow('Flatten the inputSchema')
225
- })
226
- })
227
-
228
- describe('default values (effectively optional)', () => {
229
- it('should treat fields with defaults as optional', () => {
230
- const inputSchema = z.object({
231
- name: z.string(),
232
- priority: z.string().default('medium')
233
- })
234
-
235
- const executionInterface = {
236
- form: {
237
- fields: [
238
- { name: 'name', required: true }
239
- // 'priority' has default, so OK to omit
240
- ]
241
- }
242
- }
243
-
244
- expect(() => {
245
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
246
- }).not.toThrow()
247
- })
248
- })
249
-
250
- describe('enum fields', () => {
251
- it('should validate enum fields correctly', () => {
252
- const inputSchema = z.object({
253
- priority: z.enum(['low', 'medium', 'high'])
254
- })
255
-
256
- const executionInterface = {
257
- form: {
258
- fields: [{ name: 'priority', required: true }]
259
- }
260
- }
261
-
262
- expect(() => {
263
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
264
- }).not.toThrow()
265
- })
266
- })
267
-
268
- describe('empty schema', () => {
269
- it('should pass with empty schema and no form fields', () => {
270
- const inputSchema = z.object({})
271
-
272
- const executionInterface = {
273
- form: {
274
- fields: []
275
- }
276
- }
277
-
278
- expect(() => {
279
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
280
- }).not.toThrow()
281
- })
282
- })
283
- })
284
-
15
+ import { ResourceRegistry } from '../resource-registry'
16
+ import type { WorkflowDefinition } from '../../../execution/engine/workflow/types'
17
+ import type { AgentDefinition } from '../../../execution/engine/agent/core/types'
18
+ import type { OrganizationRegistry } from '../resource-registry'
19
+ import type { ResourceEntry } from '../../../organization-model/domains/resources'
20
+
21
+ describe('validateExecutionInterface', () => {
22
+ describe('required field validation', () => {
23
+ it('should pass when all required schema fields have form fields', () => {
24
+ const inputSchema = z.object({
25
+ name: z.string(),
26
+ email: z.string()
27
+ })
28
+
29
+ const executionInterface = {
30
+ form: {
31
+ fields: [
32
+ { name: 'name', required: true },
33
+ { name: 'email', required: true }
34
+ ]
35
+ }
36
+ }
37
+
38
+ expect(() => {
39
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
40
+ }).not.toThrow()
41
+ })
42
+
43
+ it('should throw when required schema field is missing from form', () => {
44
+ const inputSchema = z.object({
45
+ name: z.string(),
46
+ email: z.string()
47
+ })
48
+
49
+ const executionInterface = {
50
+ form: {
51
+ fields: [
52
+ { name: 'name', required: true }
53
+ // Missing 'email' field
54
+ ]
55
+ }
56
+ }
57
+
58
+ expect(() => {
59
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
60
+ }).toThrow(RegistryValidationError)
61
+ expect(() => {
62
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
63
+ }).toThrow('missing required field "email"')
64
+ })
65
+
66
+ it('should throw when form field is not marked required but schema field is required', () => {
67
+ const inputSchema = z.object({
68
+ name: z.string() // Required
69
+ })
70
+
71
+ const executionInterface = {
72
+ form: {
73
+ fields: [
74
+ { name: 'name', required: false } // Not marked required
75
+ ]
76
+ }
77
+ }
78
+
79
+ expect(() => {
80
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
81
+ }).toThrow(RegistryValidationError)
82
+ expect(() => {
83
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
84
+ }).toThrow('should be required')
85
+ })
86
+
87
+ it('should pass when optional schema field is missing from form', () => {
88
+ const inputSchema = z.object({
89
+ name: z.string(),
90
+ description: z.string().optional()
91
+ })
92
+
93
+ const executionInterface = {
94
+ form: {
95
+ fields: [
96
+ { name: 'name', required: true }
97
+ // 'description' is optional, so OK to omit
98
+ ]
99
+ }
100
+ }
101
+
102
+ expect(() => {
103
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
104
+ }).not.toThrow()
105
+ })
106
+
107
+ it('should pass when optional schema field has non-required form field', () => {
108
+ const inputSchema = z.object({
109
+ name: z.string(),
110
+ description: z.string().optional()
111
+ })
112
+
113
+ const executionInterface = {
114
+ form: {
115
+ fields: [
116
+ { name: 'name', required: true },
117
+ { name: 'description', required: false }
118
+ ]
119
+ }
120
+ }
121
+
122
+ expect(() => {
123
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
124
+ }).not.toThrow()
125
+ })
126
+ })
127
+
128
+ describe('field mapping validation', () => {
129
+ it('should pass with valid field mappings', () => {
130
+ const inputSchema = z.object({
131
+ taskName: z.string(),
132
+ priority: z.string()
133
+ })
134
+
135
+ const executionInterface = {
136
+ form: {
137
+ fields: [
138
+ { name: 'task_name', required: true },
139
+ { name: 'prio', required: true }
140
+ ],
141
+ fieldMappings: {
142
+ task_name: 'taskName',
143
+ prio: 'priority'
144
+ }
145
+ }
146
+ }
147
+
148
+ expect(() => {
149
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
150
+ }).not.toThrow()
151
+ })
152
+
153
+ it('should throw when field mapping points to non-existent schema field', () => {
154
+ const inputSchema = z.object({
155
+ name: z.string()
156
+ })
157
+
158
+ // Include a valid mapping for 'name' so we get past the required field check,
159
+ // then add an extra field that maps to a non-existent schema field
160
+ const executionInterface = {
161
+ form: {
162
+ fields: [
163
+ { name: 'userName', required: true },
164
+ { name: 'extraField', required: false }
165
+ ],
166
+ fieldMappings: {
167
+ userName: 'name', // Valid mapping
168
+ extraField: 'nonExistentField' // Invalid mapping
169
+ }
170
+ }
171
+ }
172
+
173
+ expect(() => {
174
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
175
+ }).toThrow(RegistryValidationError)
176
+ expect(() => {
177
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
178
+ }).toThrow('maps to non-existent schema field "nonExistentField"')
179
+ })
180
+
181
+ it('should throw when form field without mapping has no matching schema field', () => {
182
+ const inputSchema = z.object({
183
+ name: z.string()
184
+ })
185
+
186
+ const executionInterface = {
187
+ form: {
188
+ fields: [
189
+ { name: 'name', required: true },
190
+ { name: 'unknownField', required: false }
191
+ ]
192
+ }
193
+ }
194
+
195
+ expect(() => {
196
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
197
+ }).toThrow(RegistryValidationError)
198
+ expect(() => {
199
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
200
+ }).toThrow('maps to non-existent schema field "unknownField"')
201
+ })
202
+
203
+ it('should throw with flatten suggestion when form field uses nested notation', () => {
204
+ const inputSchema = z.object({
205
+ name: z.string()
206
+ })
207
+
208
+ const executionInterface = {
209
+ form: {
210
+ fields: [
211
+ { name: 'name', required: true },
212
+ { name: 'criteria.targetTitles', required: true }
213
+ ]
214
+ }
215
+ }
216
+
217
+ expect(() => {
218
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
219
+ }).toThrow(RegistryValidationError)
220
+ expect(() => {
221
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
222
+ }).toThrow('uses nested notation')
223
+ expect(() => {
224
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
225
+ }).toThrow('Flatten the inputSchema')
226
+ })
227
+ })
228
+
229
+ describe('default values (effectively optional)', () => {
230
+ it('should treat fields with defaults as optional', () => {
231
+ const inputSchema = z.object({
232
+ name: z.string(),
233
+ priority: z.string().default('medium')
234
+ })
235
+
236
+ const executionInterface = {
237
+ form: {
238
+ fields: [
239
+ { name: 'name', required: true }
240
+ // 'priority' has default, so OK to omit
241
+ ]
242
+ }
243
+ }
244
+
245
+ expect(() => {
246
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
247
+ }).not.toThrow()
248
+ })
249
+ })
250
+
251
+ describe('enum fields', () => {
252
+ it('should validate enum fields correctly', () => {
253
+ const inputSchema = z.object({
254
+ priority: z.enum(['low', 'medium', 'high'])
255
+ })
256
+
257
+ const executionInterface = {
258
+ form: {
259
+ fields: [{ name: 'priority', required: true }]
260
+ }
261
+ }
262
+
263
+ expect(() => {
264
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
265
+ }).not.toThrow()
266
+ })
267
+ })
268
+
269
+ describe('empty schema', () => {
270
+ it('should pass with empty schema and no form fields', () => {
271
+ const inputSchema = z.object({})
272
+
273
+ const executionInterface = {
274
+ form: {
275
+ fields: []
276
+ }
277
+ }
278
+
279
+ expect(() => {
280
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
281
+ }).not.toThrow()
282
+ })
283
+ })
284
+ })
285
+
285
286
  describe('validateResourceGovernance', () => {
286
- afterEach(() => {
287
- vi.unstubAllEnvs()
288
- })
289
-
290
- const systemA = {
291
- id: 'sys.lead-gen',
292
- title: 'Lead Gen',
293
- description: 'Lead generation system.',
294
- kind: 'operational' as const,
295
- governedByKnowledge: [],
296
- drivesGoals: [],
297
- status: 'active' as const,
298
- order: 10
299
- }
300
-
301
- const systemB = {
302
- id: 'sys.crm',
303
- title: 'CRM',
304
- description: 'CRM system.',
305
- kind: 'operational' as const,
306
- governedByKnowledge: [],
307
- drivesGoals: [],
308
- status: 'active' as const,
309
- order: 20
310
- }
311
-
287
+ afterEach(() => {
288
+ vi.unstubAllEnvs()
289
+ })
290
+
291
+ const systemA = {
292
+ id: 'sys.lead-gen',
293
+ title: 'Lead Gen',
294
+ description: 'Lead generation system.',
295
+ kind: 'operational' as const,
296
+ governedByKnowledge: [],
297
+ drivesGoals: [],
298
+ status: 'active' as const,
299
+ order: 10
300
+ }
301
+
302
+ const systemB = {
303
+ id: 'sys.crm',
304
+ title: 'CRM',
305
+ description: 'CRM system.',
306
+ kind: 'operational' as const,
307
+ governedByKnowledge: [],
308
+ drivesGoals: [],
309
+ status: 'active' as const,
310
+ order: 20
311
+ }
312
+
312
313
  const workflowResource: ResourceEntry = {
313
314
  id: 'lead-import',
314
315
  kind: 'workflow',
@@ -327,56 +328,56 @@ describe('validateResourceGovernance', () => {
327
328
  emits: ['sys.lead-gen:event/imported']
328
329
  }
329
330
  }
330
-
331
- const agentResource: ResourceEntry = {
332
- id: 'lead-import',
333
- kind: 'agent',
334
- systemPath: 'sys.lead-gen',
335
- status: 'active',
336
- agentKind: 'specialist',
337
- sessionCapable: false,
338
- order: 10
339
- }
340
-
341
- const createGovernedWorkflow = (
342
- resource = workflowResource,
343
- overrides: Partial<WorkflowDefinition['config']> = {}
344
- ): WorkflowDefinition => ({
345
- config: {
346
- resourceId: resource.id,
347
- name: `Workflow ${resource.id}`,
348
- description: `Test workflow ${resource.id}`,
349
- version: '1.0.0',
350
- type: 'workflow',
351
- status: 'dev',
352
- resource: resource as Extract<ResourceEntry, { kind: 'workflow' }>,
353
- ...overrides
354
- },
355
- contract: {
356
- inputSchema: z.object({ data: z.string() }),
357
- outputSchema: z.object({ result: z.boolean() })
358
- },
359
- steps: {},
360
- entryPoint: 'start'
361
- })
362
-
363
- const createRawWorkflow = (resourceId = workflowResource.id): WorkflowDefinition => ({
364
- config: {
365
- resourceId,
366
- name: `Workflow ${resourceId}`,
367
- description: `Test workflow ${resourceId}`,
368
- version: '1.0.0',
369
- type: 'workflow',
370
- status: 'dev'
371
- },
372
- contract: {
373
- inputSchema: z.object({ data: z.string() }),
374
- outputSchema: z.object({ result: z.boolean() })
375
- },
376
- steps: {},
377
- entryPoint: 'start'
378
- })
379
-
331
+
332
+ const agentResource: ResourceEntry = {
333
+ id: 'lead-import',
334
+ kind: 'agent',
335
+ systemPath: 'sys.lead-gen',
336
+ status: 'active',
337
+ agentKind: 'specialist',
338
+ sessionCapable: false,
339
+ order: 10
340
+ }
341
+
342
+ const createGovernedWorkflow = (
343
+ resource = workflowResource,
344
+ overrides: Partial<WorkflowDefinition['config']> = {}
345
+ ): WorkflowDefinition => ({
346
+ config: {
347
+ resourceId: resource.id,
348
+ name: `Workflow ${resource.id}`,
349
+ description: `Test workflow ${resource.id}`,
350
+ version: '1.0.0',
351
+ type: 'workflow',
352
+ status: 'dev',
353
+ resource: resource as Extract<ResourceEntry, { kind: 'workflow' }>,
354
+ ...overrides
355
+ },
356
+ contract: {
357
+ inputSchema: z.object({ data: z.string() }),
358
+ outputSchema: z.object({ result: z.boolean() })
359
+ },
360
+ steps: {},
361
+ entryPoint: 'start'
362
+ })
363
+
364
+ const createRawWorkflow = (resourceId = workflowResource.id): WorkflowDefinition => ({
365
+ config: {
366
+ resourceId,
367
+ name: `Workflow ${resourceId}`,
368
+ description: `Test workflow ${resourceId}`,
369
+ version: '1.0.0',
370
+ type: 'workflow',
371
+ status: 'dev'
372
+ },
373
+ contract: {
374
+ inputSchema: z.object({ data: z.string() }),
375
+ outputSchema: z.object({ result: z.boolean() })
376
+ },
377
+ steps: {},
378
+ entryPoint: 'start'
379
+ })
380
+
380
381
  const validOntology = {
381
382
  objectTypes: {
382
383
  'sys.lead-gen:object/company': {
@@ -413,18 +414,18 @@ describe('validateResourceGovernance', () => {
413
414
  resources: Object.fromEntries(resources.map((r) => [r.id, r])),
414
415
  ...extras
415
416
  })
416
-
417
+
417
418
  it('passes when code resources are descriptor-backed and match active OM Resources and Systems', () => {
418
419
  const result = validateResourceGovernance(
419
420
  'test-org',
420
421
  {
421
- version: '1.0.0',
422
- workflows: [createGovernedWorkflow()]
423
- },
424
- createModel(),
425
- { mode: 'strict' }
426
- )
427
-
422
+ version: '1.0.0',
423
+ workflows: [createGovernedWorkflow()]
424
+ },
425
+ createModel(),
426
+ { mode: 'strict' }
427
+ )
428
+
428
429
  expect(result.valid).toBe(true)
429
430
  expect(result.issues).toEqual([])
430
431
  })
@@ -457,122 +458,122 @@ describe('validateResourceGovernance', () => {
457
458
  it('throws in strict mode when an active OM resource has no code-side resource', () => {
458
459
  expect(() =>
459
460
  validateResourceGovernance(
460
- 'test-org',
461
- {
462
- version: '1.0.0',
463
- workflows: []
464
- },
465
- createModel(),
466
- { mode: 'strict' }
467
- )
468
- ).toThrow("OM resource 'lead-import' has no matching code-side resource")
469
- })
470
-
471
- it('defaults to strict mode after cutover', () => {
472
- expect(() =>
473
- validateResourceGovernance(
474
- 'test-org',
475
- {
476
- version: '1.0.0',
477
- workflows: []
478
- },
479
- createModel()
480
- )
481
- ).toThrow("OM resource 'lead-import' has no matching code-side resource")
482
- })
483
-
484
- it('downgrades strict failures to warnings in warn-only mode', () => {
485
- const warnings: string[] = []
486
-
487
- const result = validateResourceGovernance(
488
- 'test-org',
489
- {
490
- version: '1.0.0',
491
- workflows: []
492
- },
493
- createModel(),
494
- {
495
- mode: 'warn-only',
496
- onWarning: (issue) => warnings.push(issue.message)
497
- }
498
- )
499
-
500
- expect(result.valid).toBe(false)
501
- expect(result.issues.map((issue) => issue.type)).toContain('missing-code-resource')
502
- expect(warnings).toEqual(["[test-org] OM resource 'lead-import' has no matching code-side resource."])
503
- })
504
-
505
- it('honors ELEVASIS_RESOURCE_VALIDATOR=warn-only as the permanent escape hatch', () => {
506
- vi.stubEnv('ELEVASIS_RESOURCE_VALIDATOR', 'warn-only')
507
- const warnings: string[] = []
508
-
509
- const result = validateResourceGovernance(
510
- 'test-org',
511
- {
512
- version: '1.0.0',
513
- workflows: []
514
- },
515
- createModel(),
516
- {
517
- onWarning: (issue) => warnings.push(issue.message)
518
- }
519
- )
520
-
521
- expect(result.valid).toBe(false)
522
- expect(result.mode).toBe('warn-only')
523
- expect(result.issues.map((issue) => issue.type)).toContain('missing-code-resource')
524
- expect(warnings).toEqual(["[test-org] OM resource 'lead-import' has no matching code-side resource."])
525
- })
526
-
527
- it('reports a code-side resource with no active OM resource', () => {
528
- const extraResource: ResourceEntry = {
529
- ...workflowResource,
530
- id: 'extra-workflow'
531
- }
532
-
533
- expect(() =>
534
- validateResourceGovernance(
535
- 'test-org',
536
- {
537
- version: '1.0.0',
538
- workflows: [createGovernedWorkflow(extraResource)]
539
- },
540
- createModel([], [systemA]),
541
- { mode: 'strict' }
542
- )
543
- ).toThrow("Code-side resource 'extra-workflow' has no active OM Resource descriptor")
544
- })
545
-
546
- it('reports runtime/OM type mismatches', () => {
547
- expect(() =>
548
- validateResourceGovernance(
549
- 'test-org',
550
- {
551
- version: '1.0.0',
552
- workflows: [createRawWorkflow('lead-import')]
553
- },
554
- createModel([agentResource]),
555
- { mode: 'strict' }
556
- )
557
- ).toThrow("Resource 'lead-import' type mismatch: code has 'workflow', OM has 'agent'")
558
- })
559
-
461
+ 'test-org',
462
+ {
463
+ version: '1.0.0',
464
+ workflows: []
465
+ },
466
+ createModel(),
467
+ { mode: 'strict' }
468
+ )
469
+ ).toThrow("OM resource 'lead-import' has no matching code-side resource")
470
+ })
471
+
472
+ it('defaults to strict mode after cutover', () => {
473
+ expect(() =>
474
+ validateResourceGovernance(
475
+ 'test-org',
476
+ {
477
+ version: '1.0.0',
478
+ workflows: []
479
+ },
480
+ createModel()
481
+ )
482
+ ).toThrow("OM resource 'lead-import' has no matching code-side resource")
483
+ })
484
+
485
+ it('downgrades strict failures to warnings in warn-only mode', () => {
486
+ const warnings: string[] = []
487
+
488
+ const result = validateResourceGovernance(
489
+ 'test-org',
490
+ {
491
+ version: '1.0.0',
492
+ workflows: []
493
+ },
494
+ createModel(),
495
+ {
496
+ mode: 'warn-only',
497
+ onWarning: (issue) => warnings.push(issue.message)
498
+ }
499
+ )
500
+
501
+ expect(result.valid).toBe(false)
502
+ expect(result.issues.map((issue) => issue.type)).toContain('missing-code-resource')
503
+ expect(warnings).toEqual(["[test-org] OM resource 'lead-import' has no matching code-side resource."])
504
+ })
505
+
506
+ it('honors ELEVASIS_RESOURCE_VALIDATOR=warn-only as the permanent escape hatch', () => {
507
+ vi.stubEnv('ELEVASIS_RESOURCE_VALIDATOR', 'warn-only')
508
+ const warnings: string[] = []
509
+
510
+ const result = validateResourceGovernance(
511
+ 'test-org',
512
+ {
513
+ version: '1.0.0',
514
+ workflows: []
515
+ },
516
+ createModel(),
517
+ {
518
+ onWarning: (issue) => warnings.push(issue.message)
519
+ }
520
+ )
521
+
522
+ expect(result.valid).toBe(false)
523
+ expect(result.mode).toBe('warn-only')
524
+ expect(result.issues.map((issue) => issue.type)).toContain('missing-code-resource')
525
+ expect(warnings).toEqual(["[test-org] OM resource 'lead-import' has no matching code-side resource."])
526
+ })
527
+
528
+ it('reports a code-side resource with no active OM resource', () => {
529
+ const extraResource: ResourceEntry = {
530
+ ...workflowResource,
531
+ id: 'extra-workflow'
532
+ }
533
+
534
+ expect(() =>
535
+ validateResourceGovernance(
536
+ 'test-org',
537
+ {
538
+ version: '1.0.0',
539
+ workflows: [createGovernedWorkflow(extraResource)]
540
+ },
541
+ createModel([], [systemA]),
542
+ { mode: 'strict' }
543
+ )
544
+ ).toThrow("Code-side resource 'extra-workflow' has no active OM Resource descriptor")
545
+ })
546
+
547
+ it('reports runtime/OM type mismatches', () => {
548
+ expect(() =>
549
+ validateResourceGovernance(
550
+ 'test-org',
551
+ {
552
+ version: '1.0.0',
553
+ workflows: [createRawWorkflow('lead-import')]
554
+ },
555
+ createModel([agentResource]),
556
+ { mode: 'strict' }
557
+ )
558
+ ).toThrow("Resource 'lead-import' type mismatch: code has 'workflow', OM has 'agent'")
559
+ })
560
+
560
561
  it('reports descriptor/OM system mismatches', () => {
561
- const codeDescriptor: ResourceEntry = {
562
- ...workflowResource,
563
- systemPath: 'sys.crm'
564
- }
565
-
566
- expect(() =>
567
- validateResourceGovernance(
568
- 'test-org',
569
- {
570
- version: '1.0.0',
571
- workflows: [createGovernedWorkflow(codeDescriptor)]
572
- },
573
- createModel([workflowResource], [systemA, systemB]),
574
- { mode: 'strict' }
575
- )
562
+ const codeDescriptor: ResourceEntry = {
563
+ ...workflowResource,
564
+ systemPath: 'sys.crm'
565
+ }
566
+
567
+ expect(() =>
568
+ validateResourceGovernance(
569
+ 'test-org',
570
+ {
571
+ version: '1.0.0',
572
+ workflows: [createGovernedWorkflow(codeDescriptor)]
573
+ },
574
+ createModel([workflowResource], [systemA, systemB]),
575
+ { mode: 'strict' }
576
+ )
576
577
  ).toThrow("Resource 'lead-import' system mismatch: code descriptor has 'sys.crm', OM has 'sys.lead-gen'")
577
578
  })
578
579
 
@@ -923,39 +924,39 @@ describe('validateResourceGovernance', () => {
923
924
  )
924
925
  ).toThrow("Topology relationship 'missing-trigger-starts-import' from references missing trigger 'missing-trigger'")
925
926
  })
926
-
927
- it('reports active OM resources that reference missing Systems', () => {
928
- const resourceWithMissingSystem: ResourceEntry = {
929
- ...workflowResource,
930
- systemPath: 'sys.missing'
931
- }
932
-
933
- expect(() =>
934
- validateResourceGovernance(
935
- 'test-org',
936
- {
937
- version: '1.0.0',
938
- workflows: [createGovernedWorkflow(resourceWithMissingSystem)]
939
- },
940
- createModel([resourceWithMissingSystem], [systemA]),
941
- { mode: 'strict' }
942
- )
943
- ).toThrow("OM resource 'lead-import' references missing system path 'sys.missing'.")
944
- })
945
-
946
- it('reports raw runtime resourceId authoring when a descriptor is required', () => {
947
- expect(() =>
948
- validateResourceGovernance(
949
- 'test-org',
950
- {
951
- version: '1.0.0',
952
- workflows: [createRawWorkflow()]
953
- },
954
- createModel(),
955
- { mode: 'strict' }
956
- )
957
- ).toThrow("Code-side resource 'lead-import' authors raw resourceId/type values")
958
- })
927
+
928
+ it('reports active OM resources that reference missing Systems', () => {
929
+ const resourceWithMissingSystem: ResourceEntry = {
930
+ ...workflowResource,
931
+ systemPath: 'sys.missing'
932
+ }
933
+
934
+ expect(() =>
935
+ validateResourceGovernance(
936
+ 'test-org',
937
+ {
938
+ version: '1.0.0',
939
+ workflows: [createGovernedWorkflow(resourceWithMissingSystem)]
940
+ },
941
+ createModel([resourceWithMissingSystem], [systemA]),
942
+ { mode: 'strict' }
943
+ )
944
+ ).toThrow("OM resource 'lead-import' references missing system path 'sys.missing'.")
945
+ })
946
+
947
+ it('reports raw runtime resourceId authoring when a descriptor is required', () => {
948
+ expect(() =>
949
+ validateResourceGovernance(
950
+ 'test-org',
951
+ {
952
+ version: '1.0.0',
953
+ workflows: [createRawWorkflow()]
954
+ },
955
+ createModel(),
956
+ { mode: 'strict' }
957
+ )
958
+ ).toThrow("Code-side resource 'lead-import' authors raw resourceId/type values")
959
+ })
959
960
  })
960
961
 
961
962
  describe('validateDeclaredSystemInterfaceReadiness', () => {
@@ -983,812 +984,897 @@ describe('validateDeclaredSystemInterfaceReadiness', () => {
983
984
  expect((error as RegistryValidationError).field).toBe('systems.sales.apiInterface.readinessProfile')
984
985
  })
985
986
  })
986
-
987
- describe('ResourceRegistry - ExecutionInterface validation integration', () => {
988
- it('should throw on registry construction when interface misses required field', () => {
989
- const workflow: WorkflowDefinition = {
990
- config: {
991
- resourceId: 'test-workflow',
992
- name: 'Test Workflow',
993
- description: 'Test',
994
- version: '1.0.0',
995
- type: 'workflow',
996
- status: 'dev'
997
- },
998
- contract: {
999
- inputSchema: z.object({
1000
- taskName: z.string(),
1001
- priority: z.string()
1002
- }),
1003
- outputSchema: z.object({})
1004
- },
1005
- steps: {},
1006
- entryPoint: 'start',
1007
- interface: {
1008
- form: {
1009
- fields: [
1010
- { name: 'taskName', label: 'Task Name', type: 'text', required: true }
1011
- // Missing 'priority' field
1012
- ]
1013
- }
1014
- }
1015
- }
1016
-
1017
- const registry: OrganizationRegistry = {
1018
- 'test-org': {
1019
- workflows: [workflow]
1020
- }
1021
- }
1022
-
1023
- expect(() => {
1024
- new ResourceRegistry(registry)
1025
- }).toThrow('missing required field "priority"')
1026
- })
1027
-
1028
- it('should throw when agent interface misses required field', () => {
1029
- const agent: AgentDefinition = {
1030
- config: {
1031
- resourceId: 'test-agent',
1032
- name: 'Test Agent',
1033
- description: 'Test',
1034
- version: '1.0.0',
1035
- type: 'agent',
1036
- status: 'dev',
1037
- systemPrompt: 'You are a test agent'
1038
- },
1039
- contract: {
1040
- inputSchema: z.object({
1041
- task: z.string(),
1042
- context: z.string()
1043
- }),
1044
- outputSchema: z.object({})
1045
- },
1046
- tools: [],
1047
- modelConfig: {
1048
- provider: 'mock',
1049
- model: 'mock',
1050
- apiKey: 'test'
1051
- },
1052
- interface: {
1053
- form: {
1054
- fields: [
1055
- { name: 'task', label: 'Task', type: 'textarea', required: true }
1056
- // Missing 'context' field
1057
- ]
1058
- }
1059
- }
1060
- }
1061
-
1062
- const registry: OrganizationRegistry = {
1063
- 'test-org': {
1064
- agents: [agent]
1065
- }
1066
- }
1067
-
1068
- expect(() => {
1069
- new ResourceRegistry(registry)
1070
- }).toThrow('missing required field "context"')
1071
- })
1072
-
1073
- it('should pass when interface correctly matches schema', () => {
1074
- const workflow: WorkflowDefinition = {
1075
- config: {
1076
- resourceId: 'valid-workflow',
1077
- name: 'Valid Workflow',
1078
- description: 'Test',
1079
- version: '1.0.0',
1080
- type: 'workflow',
1081
- status: 'dev'
1082
- },
1083
- contract: {
1084
- inputSchema: z.object({
1085
- taskName: z.string(),
1086
- priority: z.enum(['low', 'medium', 'high']),
1087
- description: z.string().optional()
1088
- }),
1089
- outputSchema: z.object({})
1090
- },
1091
- steps: {},
1092
- entryPoint: 'start',
1093
- interface: {
1094
- form: {
1095
- title: 'Run Workflow',
1096
- fields: [
1097
- { name: 'taskName', label: 'Task Name', type: 'text', required: true },
1098
- { name: 'priority', label: 'Priority', type: 'select', required: true },
1099
- { name: 'description', label: 'Description', type: 'textarea', required: false }
1100
- ]
1101
- }
1102
- }
1103
- }
1104
-
1105
- const registry: OrganizationRegistry = {
1106
- 'test-org': {
1107
- workflows: [workflow]
1108
- }
1109
- }
1110
-
1111
- expect(() => {
1112
- new ResourceRegistry(registry)
1113
- }).not.toThrow()
1114
- })
1115
-
1116
- it('should pass when workflow has no interface (optional)', () => {
1117
- const workflow: WorkflowDefinition = {
1118
- config: {
1119
- resourceId: 'no-interface-workflow',
1120
- name: 'No Interface Workflow',
1121
- description: 'Test',
1122
- version: '1.0.0',
1123
- type: 'workflow',
1124
- status: 'dev'
1125
- },
1126
- contract: {
1127
- inputSchema: z.object({ data: z.string() }),
1128
- outputSchema: z.object({})
1129
- },
1130
- steps: {},
1131
- entryPoint: 'start'
1132
- // No interface - should pass
1133
- }
1134
-
1135
- const registry: OrganizationRegistry = {
1136
- 'test-org': {
1137
- workflows: [workflow]
1138
- }
1139
- }
1140
-
1141
- expect(() => {
1142
- new ResourceRegistry(registry)
1143
- }).not.toThrow()
1144
- })
1145
- })
1146
-
1147
- describe('Relationship Validation', () => {
1148
- // Helper to create minimal mock resources
1149
- const createMockWorkflow = (resourceId: string): WorkflowDefinition => ({
1150
- config: {
1151
- resourceId,
1152
- name: `Workflow ${resourceId}`,
1153
- description: `Test workflow ${resourceId}`,
1154
- version: '1.0.0',
1155
- type: 'workflow',
1156
- status: 'dev'
1157
- },
1158
- contract: {
1159
- inputSchema: z.object({ data: z.string() }),
1160
- outputSchema: z.object({ result: z.boolean() })
1161
- },
1162
- steps: {},
1163
- entryPoint: 'start'
1164
- })
1165
-
1166
- const createMockAgent = (resourceId: string): AgentDefinition => ({
1167
- config: {
1168
- resourceId,
1169
- name: `Agent ${resourceId}`,
1170
- description: `Test agent ${resourceId}`,
1171
- version: '1.0.0',
1172
- type: 'agent',
1173
- status: 'dev',
1174
- systemPrompt: 'You are a test agent'
1175
- },
1176
- contract: {
1177
- inputSchema: z.object({ query: z.string() }),
1178
- outputSchema: z.object({ response: z.string() })
1179
- },
1180
- tools: [],
1181
- modelConfig: {
1182
- provider: 'mock',
1183
- model: 'mock',
1184
- apiKey: 'test-key'
1185
- }
1186
- })
1187
-
1188
- describe('validateTriggers (new field names)', () => {
1189
- it('validates trigger.triggers.agents exist (using resourceId)', () => {
1190
- const agent = createMockAgent('test-agent')
1191
- const registry: OrganizationRegistry = {
1192
- 'test-org': {
1193
- agents: [agent],
1194
- triggers: [
1195
- {
1196
- resourceId: 'test-trigger',
1197
- type: 'trigger',
1198
- triggerType: 'webhook',
1199
- name: 'Test Trigger',
1200
- description: 'Test trigger',
1201
- version: '1.0.0',
1202
- status: 'dev',
1203
- triggers: { agents: ['test-agent'] }
1204
- }
1205
- ]
1206
- }
1207
- }
1208
-
1209
- expect(() => {
1210
- new ResourceRegistry(registry)
1211
- }).not.toThrow()
1212
- })
1213
-
1214
- it('validates trigger.triggers.workflows exist (using resourceId)', () => {
1215
- const workflow = createMockWorkflow('test-workflow')
1216
- const registry: OrganizationRegistry = {
1217
- 'test-org': {
1218
- workflows: [workflow],
1219
- triggers: [
1220
- {
1221
- resourceId: 'test-trigger',
1222
- type: 'trigger',
1223
- triggerType: 'schedule',
1224
- name: 'Test Trigger',
1225
- description: 'Test trigger',
1226
- version: '1.0.0',
1227
- status: 'dev',
1228
- triggers: { workflows: ['test-workflow'] }
1229
- }
1230
- ]
1231
- }
1232
- }
1233
-
1234
- expect(() => {
1235
- new ResourceRegistry(registry)
1236
- }).not.toThrow()
1237
- })
1238
-
1239
- it('throws for trigger triggering non-existent agent (via relationships)', () => {
1240
- // Note: Trigger relationships are now declared via ResourceRelationships
1241
- const registry: OrganizationRegistry = {
1242
- 'test-org': {
1243
- triggers: [
1244
- {
1245
- resourceId: 'test-trigger',
1246
- type: 'trigger',
1247
- triggerType: 'webhook',
1248
- name: 'Test Trigger',
1249
- description: 'Test trigger',
1250
- version: '1.0.0',
1251
- status: 'dev'
1252
- }
1253
- ],
1254
- relationships: {
1255
- 'test-trigger': {
1256
- triggers: { agents: ['non-existent-agent'] }
1257
- }
1258
- }
1259
- }
1260
- }
1261
-
1262
- expect(() => {
1263
- new ResourceRegistry(registry)
1264
- }).toThrow('non-existent-agent')
1265
- })
1266
-
1267
- it('throws for trigger triggering non-existent workflow (via relationships)', () => {
1268
- // Note: Trigger relationships are now declared via ResourceRelationships
1269
- const registry: OrganizationRegistry = {
1270
- 'test-org': {
1271
- triggers: [
1272
- {
1273
- resourceId: 'test-trigger',
1274
- type: 'trigger',
1275
- triggerType: 'event',
1276
- name: 'Test Trigger',
1277
- description: 'Test trigger',
1278
- version: '1.0.0',
1279
- status: 'dev'
1280
- }
1281
- ],
1282
- relationships: {
1283
- 'test-trigger': {
1284
- triggers: { workflows: ['non-existent-workflow'] }
1285
- }
1286
- }
1287
- }
1288
- }
1289
-
1290
- expect(() => {
1291
- new ResourceRegistry(registry)
1292
- }).toThrow('non-existent-workflow')
1293
- })
1294
-
1295
- it('allows trigger with no relationships (triggers are metadata only)', () => {
1296
- // Note: Triggers without relationships are now valid - they're just metadata
1297
- // The trigger's targets are defined in ResourceRelationships, not on the trigger itself
1298
- const registry: OrganizationRegistry = {
1299
- 'test-org': {
1300
- triggers: [
1301
- {
1302
- resourceId: 'test-trigger',
1303
- type: 'trigger',
1304
- triggerType: 'webhook',
1305
- name: 'Test Trigger',
1306
- description: 'Test trigger',
1307
- version: '1.0.0',
1308
- status: 'dev'
1309
- }
1310
- ]
1311
- // No relationships - trigger is just metadata
1312
- }
1313
- }
1314
-
1315
- expect(() => {
1316
- new ResourceRegistry(registry)
1317
- }).not.toThrow()
1318
- })
1319
-
1320
- it('allows trigger with empty relationships object', () => {
1321
- // Note: Empty relationships are valid - triggers without targets are allowed
1322
- const registry: OrganizationRegistry = {
1323
- 'test-org': {
1324
- triggers: [
1325
- {
1326
- resourceId: 'test-trigger',
1327
- type: 'trigger',
1328
- triggerType: 'manual',
1329
- name: 'Test Trigger',
1330
- description: 'Test trigger',
1331
- version: '1.0.0',
1332
- status: 'dev'
1333
- }
1334
- ],
1335
- relationships: {}
1336
- }
1337
- }
1338
-
1339
- expect(() => {
1340
- new ResourceRegistry(registry)
1341
- }).not.toThrow()
1342
- })
1343
- })
1344
-
1345
- describe('validateResourceRelationships', () => {
1346
- it('validates resource.triggers.agents exist', () => {
1347
- const agent1 = createMockAgent('agent-1')
1348
- const agent2 = createMockAgent('agent-2')
1349
- const registry: OrganizationRegistry = {
1350
- 'test-org': {
1351
- agents: [agent1, agent2],
1352
- relationships: {
1353
- 'agent-1': {
1354
- triggers: { agents: ['agent-2'] }
1355
- }
1356
- }
1357
- }
1358
- }
1359
-
1360
- expect(() => {
1361
- new ResourceRegistry(registry)
1362
- }).not.toThrow()
1363
- })
1364
-
1365
- it('validates resource.triggers.workflows exist', () => {
1366
- const agent = createMockAgent('test-agent')
1367
- const workflow = createMockWorkflow('test-workflow')
1368
- const registry: OrganizationRegistry = {
1369
- 'test-org': {
1370
- agents: [agent],
1371
- workflows: [workflow],
1372
- relationships: {
1373
- 'test-agent': {
1374
- triggers: { workflows: ['test-workflow'] }
1375
- }
1376
- }
1377
- }
1378
- }
1379
-
1380
- expect(() => {
1381
- new ResourceRegistry(registry)
1382
- }).not.toThrow()
1383
- })
1384
-
1385
- it('validates resource.uses.integrations exist', () => {
1386
- const workflow = createMockWorkflow('test-workflow')
1387
- const registry: OrganizationRegistry = {
1388
- 'test-org': {
1389
- workflows: [workflow],
1390
- integrations: [
1391
- {
1392
- resourceId: 'integration-shopify',
1393
- type: 'integration',
1394
- provider: 'custom',
1395
- credentialName: 'shopify-prod',
1396
- name: 'Shopify Integration',
1397
- description: 'E-commerce integration',
1398
- version: '1.0.0',
1399
- status: 'prod'
1400
- }
1401
- ],
1402
- relationships: {
1403
- 'test-workflow': {
1404
- uses: { integrations: ['integration-shopify'] }
1405
- }
1406
- }
1407
- }
1408
- }
1409
-
1410
- expect(() => {
1411
- new ResourceRegistry(registry)
1412
- }).not.toThrow()
1413
- })
1414
-
1415
- it('throws for relationship declared for non-existent resource', () => {
1416
- const registry: OrganizationRegistry = {
1417
- 'test-org': {
1418
- relationships: {
1419
- 'non-existent-resource': {
1420
- triggers: { agents: ['some-agent'] }
1421
- }
1422
- }
1423
- }
1424
- }
1425
-
1426
- expect(() => {
1427
- new ResourceRegistry(registry)
1428
- }).toThrow('[test-org] Relationship declared for non-existent resource: non-existent-resource')
1429
- })
1430
-
1431
- it('throws for relationship triggering non-existent agent', () => {
1432
- const workflow = createMockWorkflow('test-workflow')
1433
- const registry: OrganizationRegistry = {
1434
- 'test-org': {
1435
- workflows: [workflow],
1436
- relationships: {
1437
- 'test-workflow': {
1438
- triggers: { agents: ['non-existent-agent'] }
1439
- }
1440
- }
1441
- }
1442
- }
1443
-
1444
- expect(() => {
1445
- new ResourceRegistry(registry)
1446
- }).toThrow("[test-org] Resource 'test-workflow' triggers non-existent agent: non-existent-agent")
1447
- })
1448
-
1449
- it('throws for relationship using non-existent integration', () => {
1450
- const agent = createMockAgent('test-agent')
1451
- const registry: OrganizationRegistry = {
1452
- 'test-org': {
1453
- agents: [agent],
1454
- relationships: {
1455
- 'test-agent': {
1456
- uses: { integrations: ['non-existent-integration'] }
1457
- }
1458
- }
1459
- }
1460
- }
1461
-
1462
- expect(() => {
1463
- new ResourceRegistry(registry)
1464
- }).toThrow("[test-org] Resource 'test-agent' uses non-existent integration: non-existent-integration")
1465
- })
1466
- })
1467
-
1468
- describe('validateExternalResources (forward-only)', () => {
1469
- it('validates external.triggers.agents exist', () => {
1470
- const agent = createMockAgent('test-agent')
1471
- const registry: OrganizationRegistry = {
1472
- 'test-org': {
1473
- agents: [agent],
1474
- externalResources: [
1475
- {
1476
- resourceId: 'external-n8n-workflow',
1477
- type: 'external',
1478
- platform: 'n8n',
1479
- name: 'N8N Workflow',
1480
- description: 'External automation',
1481
- version: '1.0.0',
1482
- status: 'prod',
1483
- triggers: { agents: ['test-agent'] }
1484
- }
1485
- ]
1486
- }
1487
- }
1488
-
1489
- expect(() => {
1490
- new ResourceRegistry(registry)
1491
- }).not.toThrow()
1492
- })
1493
-
1494
- it('validates external.triggers.workflows exist', () => {
1495
- const workflow = createMockWorkflow('test-workflow')
1496
- const registry: OrganizationRegistry = {
1497
- 'test-org': {
1498
- workflows: [workflow],
1499
- externalResources: [
1500
- {
1501
- resourceId: 'external-zapier-zap',
1502
- type: 'external',
1503
- platform: 'zapier',
1504
- name: 'Zapier Zap',
1505
- description: 'External automation',
1506
- version: '1.0.0',
1507
- status: 'prod',
1508
- triggers: { workflows: ['test-workflow'] }
1509
- }
1510
- ]
1511
- }
1512
- }
1513
-
1514
- expect(() => {
1515
- new ResourceRegistry(registry)
1516
- }).not.toThrow()
1517
- })
1518
-
1519
- it('validates external.uses.integrations exist', () => {
1520
- const registry: OrganizationRegistry = {
1521
- 'test-org': {
1522
- integrations: [
1523
- {
1524
- resourceId: 'integration-slack',
1525
- type: 'integration',
1526
- provider: 'custom',
1527
- credentialName: 'slack-prod',
1528
- name: 'Slack Integration',
1529
- description: 'Chat integration',
1530
- version: '1.0.0',
1531
- status: 'prod'
1532
- }
1533
- ],
1534
- externalResources: [
1535
- {
1536
- resourceId: 'external-make-scenario',
1537
- type: 'external',
1538
- platform: 'make',
1539
- name: 'Make Scenario',
1540
- description: 'External automation',
1541
- version: '1.0.0',
1542
- status: 'prod',
1543
- uses: { integrations: ['integration-slack'] }
1544
- }
1545
- ]
1546
- }
1547
- }
1548
-
1549
- expect(() => {
1550
- new ResourceRegistry(registry)
1551
- }).not.toThrow()
1552
- })
1553
-
1554
- it('validates external resourceId does not conflict with internal resources', () => {
1555
- const workflow = createMockWorkflow('conflicting-id')
1556
- const registry: OrganizationRegistry = {
1557
- 'test-org': {
1558
- workflows: [workflow],
1559
- externalResources: [
1560
- {
1561
- resourceId: 'conflicting-id',
1562
- type: 'external',
1563
- platform: 'other',
1564
- name: 'Conflicting External',
1565
- description: 'Should conflict',
1566
- version: '1.0.0',
1567
- status: 'dev'
1568
- }
1569
- ]
1570
- }
1571
- }
1572
-
1573
- expect(() => {
1574
- new ResourceRegistry(registry)
1575
- }).toThrow("[test-org] External resource ID 'conflicting-id' conflicts with internal resource ID")
1576
- })
1577
-
1578
- it('does NOT validate triggeredBy (field removed)', () => {
1579
- // This test ensures we don't validate triggeredBy field (it's been removed)
1580
- const workflow = createMockWorkflow('test-workflow')
1581
- const registry: OrganizationRegistry = {
1582
- 'test-org': {
1583
- workflows: [workflow],
1584
- externalResources: [
1585
- {
1586
- resourceId: 'external-resource',
1587
- type: 'external',
1588
- platform: 'n8n',
1589
- name: 'External Resource',
1590
- description: 'Has no triggeredBy field',
1591
- version: '1.0.0',
1592
- status: 'prod',
1593
- triggers: { workflows: ['test-workflow'] }
1594
- // No triggeredBy field - this is correct and should pass
1595
- }
1596
- ]
1597
- }
1598
- }
1599
-
1600
- expect(() => {
1601
- new ResourceRegistry(registry)
1602
- }).not.toThrow()
1603
- })
1604
-
1605
- it('throws for external resource with conflicting ID', () => {
1606
- const agent = createMockAgent('duplicate-id')
1607
- const registry: OrganizationRegistry = {
1608
- 'test-org': {
1609
- agents: [agent],
1610
- externalResources: [
1611
- {
1612
- resourceId: 'duplicate-id',
1613
- type: 'external',
1614
- platform: 'n8n',
1615
- name: 'Duplicate ID',
1616
- description: 'Conflicts with agent',
1617
- version: '1.0.0',
1618
- status: 'dev'
1619
- }
1620
- ]
1621
- }
1622
- }
1623
-
1624
- expect(() => {
1625
- new ResourceRegistry(registry)
1626
- }).toThrow("[test-org] External resource ID 'duplicate-id' conflicts with internal resource ID")
1627
- })
1628
- })
1629
-
1630
- describe('validateHumanCheckpoints', () => {
1631
- it('validates requestedBy.agents exist', () => {
1632
- const agent = createMockAgent('test-agent')
1633
- const registry: OrganizationRegistry = {
1634
- 'test-org': {
1635
- agents: [agent],
1636
- humanCheckpoints: [
1637
- {
1638
- resourceId: 'approval-queue',
1639
- type: 'human',
1640
- name: 'Approval Queue',
1641
- description: 'Human approval checkpoint',
1642
- version: '1.0.0',
1643
- status: 'prod',
1644
- requestedBy: { agents: ['test-agent'] }
1645
- }
1646
- ]
1647
- }
1648
- }
1649
-
1650
- expect(() => {
1651
- new ResourceRegistry(registry)
1652
- }).not.toThrow()
1653
- })
1654
-
1655
- it('validates requestedBy.workflows exist', () => {
1656
- const workflow = createMockWorkflow('test-workflow')
1657
- const registry: OrganizationRegistry = {
1658
- 'test-org': {
1659
- workflows: [workflow],
1660
- humanCheckpoints: [
1661
- {
1662
- resourceId: 'review-queue',
1663
- type: 'human',
1664
- name: 'Review Queue',
1665
- description: 'Human review checkpoint',
1666
- version: '1.0.0',
1667
- status: 'prod',
1668
- requestedBy: { workflows: ['test-workflow'] }
1669
- }
1670
- ]
1671
- }
1672
- }
1673
-
1674
- expect(() => {
1675
- new ResourceRegistry(registry)
1676
- }).not.toThrow()
1677
- })
1678
-
1679
- it('validates routesTo.agents exist', () => {
1680
- const agent = createMockAgent('fulfillment-agent')
1681
- const registry: OrganizationRegistry = {
1682
- 'test-org': {
1683
- agents: [agent],
1684
- humanCheckpoints: [
1685
- {
1686
- resourceId: 'approval-queue',
1687
- type: 'human',
1688
- name: 'Approval Queue',
1689
- description: 'Human approval checkpoint',
1690
- version: '1.0.0',
1691
- status: 'prod',
1692
- routesTo: { agents: ['fulfillment-agent'] }
1693
- }
1694
- ]
1695
- }
1696
- }
1697
-
1698
- expect(() => {
1699
- new ResourceRegistry(registry)
1700
- }).not.toThrow()
1701
- })
1702
-
1703
- it('validates routesTo.workflows exist', () => {
1704
- const workflow = createMockWorkflow('fulfillment-workflow')
1705
- const registry: OrganizationRegistry = {
1706
- 'test-org': {
1707
- workflows: [workflow],
1708
- humanCheckpoints: [
1709
- {
1710
- resourceId: 'approval-queue',
1711
- type: 'human',
1712
- name: 'Approval Queue',
1713
- description: 'Human approval checkpoint',
1714
- version: '1.0.0',
1715
- status: 'prod',
1716
- routesTo: { workflows: ['fulfillment-workflow'] }
1717
- }
1718
- ]
1719
- }
1720
- }
1721
-
1722
- expect(() => {
1723
- new ResourceRegistry(registry)
1724
- }).not.toThrow()
1725
- })
1726
-
1727
- it('throws for requestedBy referencing non-existent agent', () => {
1728
- const registry: OrganizationRegistry = {
1729
- 'test-org': {
1730
- humanCheckpoints: [
1731
- {
1732
- resourceId: 'approval-queue',
1733
- type: 'human',
1734
- name: 'Approval Queue',
1735
- description: 'Human approval checkpoint',
1736
- version: '1.0.0',
1737
- status: 'prod',
1738
- requestedBy: { agents: ['non-existent-agent'] }
1739
- }
1740
- ]
1741
- }
1742
- }
1743
-
1744
- expect(() => {
1745
- new ResourceRegistry(registry)
1746
- }).toThrow("[test-org] Human checkpoint 'approval-queue' requestedBy non-existent agent: non-existent-agent")
1747
- })
1748
-
1749
- it('throws for routesTo referencing non-existent workflow', () => {
1750
- const registry: OrganizationRegistry = {
1751
- 'test-org': {
1752
- humanCheckpoints: [
1753
- {
1754
- resourceId: 'approval-queue',
1755
- type: 'human',
1756
- name: 'Approval Queue',
1757
- description: 'Human approval checkpoint',
1758
- version: '1.0.0',
1759
- status: 'prod',
1760
- routesTo: { workflows: ['non-existent-workflow'] }
1761
- }
1762
- ]
1763
- }
1764
- }
1765
-
1766
- expect(() => {
1767
- new ResourceRegistry(registry)
1768
- }).toThrow("[test-org] Human checkpoint 'approval-queue' routesTo non-existent workflow: non-existent-workflow")
1769
- })
1770
-
1771
- it('throws for human checkpoint with conflicting ID', () => {
1772
- const workflow = createMockWorkflow('conflicting-id')
1773
- const registry: OrganizationRegistry = {
1774
- 'test-org': {
1775
- workflows: [workflow],
1776
- humanCheckpoints: [
1777
- {
1778
- resourceId: 'conflicting-id',
1779
- type: 'human',
1780
- name: 'Conflicting Checkpoint',
1781
- description: 'Conflicts with workflow',
1782
- version: '1.0.0',
1783
- status: 'dev'
1784
- }
1785
- ]
1786
- }
1787
- }
1788
-
1789
- expect(() => {
1790
- new ResourceRegistry(registry)
1791
- }).toThrow("[test-org] Human checkpoint ID 'conflicting-id' conflicts with internal resource ID")
1792
- })
1793
- })
1794
- })
987
+
988
+ describe('ResourceRegistry - ExecutionInterface validation integration', () => {
989
+ it('should throw on registry construction when interface misses required field', () => {
990
+ const workflow: WorkflowDefinition = {
991
+ config: {
992
+ resourceId: 'test-workflow',
993
+ name: 'Test Workflow',
994
+ description: 'Test',
995
+ version: '1.0.0',
996
+ type: 'workflow',
997
+ status: 'dev'
998
+ },
999
+ contract: {
1000
+ inputSchema: z.object({
1001
+ taskName: z.string(),
1002
+ priority: z.string()
1003
+ }),
1004
+ outputSchema: z.object({})
1005
+ },
1006
+ steps: {},
1007
+ entryPoint: 'start',
1008
+ interface: {
1009
+ form: {
1010
+ fields: [
1011
+ { name: 'taskName', label: 'Task Name', type: 'text', required: true }
1012
+ // Missing 'priority' field
1013
+ ]
1014
+ }
1015
+ }
1016
+ }
1017
+
1018
+ const registry: OrganizationRegistry = {
1019
+ 'test-org': {
1020
+ workflows: [workflow]
1021
+ }
1022
+ }
1023
+
1024
+ expect(() => {
1025
+ new ResourceRegistry(registry)
1026
+ }).toThrow('missing required field "priority"')
1027
+ })
1028
+
1029
+ it('should throw when agent interface misses required field', () => {
1030
+ const agent: AgentDefinition = {
1031
+ config: {
1032
+ resourceId: 'test-agent',
1033
+ name: 'Test Agent',
1034
+ description: 'Test',
1035
+ version: '1.0.0',
1036
+ type: 'agent',
1037
+ status: 'dev',
1038
+ systemPrompt: 'You are a test agent'
1039
+ },
1040
+ contract: {
1041
+ inputSchema: z.object({
1042
+ task: z.string(),
1043
+ context: z.string()
1044
+ }),
1045
+ outputSchema: z.object({})
1046
+ },
1047
+ tools: [],
1048
+ modelConfig: {
1049
+ provider: 'mock',
1050
+ model: 'mock',
1051
+ apiKey: 'test'
1052
+ },
1053
+ interface: {
1054
+ form: {
1055
+ fields: [
1056
+ { name: 'task', label: 'Task', type: 'textarea', required: true }
1057
+ // Missing 'context' field
1058
+ ]
1059
+ }
1060
+ }
1061
+ }
1062
+
1063
+ const registry: OrganizationRegistry = {
1064
+ 'test-org': {
1065
+ agents: [agent]
1066
+ }
1067
+ }
1068
+
1069
+ expect(() => {
1070
+ new ResourceRegistry(registry)
1071
+ }).toThrow('missing required field "context"')
1072
+ })
1073
+
1074
+ it('should pass when interface correctly matches schema', () => {
1075
+ const workflow: WorkflowDefinition = {
1076
+ config: {
1077
+ resourceId: 'valid-workflow',
1078
+ name: 'Valid Workflow',
1079
+ description: 'Test',
1080
+ version: '1.0.0',
1081
+ type: 'workflow',
1082
+ status: 'dev'
1083
+ },
1084
+ contract: {
1085
+ inputSchema: z.object({
1086
+ taskName: z.string(),
1087
+ priority: z.enum(['low', 'medium', 'high']),
1088
+ description: z.string().optional()
1089
+ }),
1090
+ outputSchema: z.object({})
1091
+ },
1092
+ steps: {},
1093
+ entryPoint: 'start',
1094
+ interface: {
1095
+ form: {
1096
+ title: 'Run Workflow',
1097
+ fields: [
1098
+ { name: 'taskName', label: 'Task Name', type: 'text', required: true },
1099
+ { name: 'priority', label: 'Priority', type: 'select', required: true },
1100
+ { name: 'description', label: 'Description', type: 'textarea', required: false }
1101
+ ]
1102
+ }
1103
+ }
1104
+ }
1105
+
1106
+ const registry: OrganizationRegistry = {
1107
+ 'test-org': {
1108
+ workflows: [workflow]
1109
+ }
1110
+ }
1111
+
1112
+ expect(() => {
1113
+ new ResourceRegistry(registry)
1114
+ }).not.toThrow()
1115
+ })
1116
+
1117
+ it('should pass when workflow has no interface (optional)', () => {
1118
+ const workflow: WorkflowDefinition = {
1119
+ config: {
1120
+ resourceId: 'no-interface-workflow',
1121
+ name: 'No Interface Workflow',
1122
+ description: 'Test',
1123
+ version: '1.0.0',
1124
+ type: 'workflow',
1125
+ status: 'dev'
1126
+ },
1127
+ contract: {
1128
+ inputSchema: z.object({ data: z.string() }),
1129
+ outputSchema: z.object({})
1130
+ },
1131
+ steps: {},
1132
+ entryPoint: 'start'
1133
+ // No interface - should pass
1134
+ }
1135
+
1136
+ const registry: OrganizationRegistry = {
1137
+ 'test-org': {
1138
+ workflows: [workflow]
1139
+ }
1140
+ }
1141
+
1142
+ expect(() => {
1143
+ new ResourceRegistry(registry)
1144
+ }).not.toThrow()
1145
+ })
1146
+ })
1147
+
1148
+ describe('Relationship Validation', () => {
1149
+ // Helper to create minimal mock resources
1150
+ const createMockWorkflow = (resourceId: string): WorkflowDefinition => ({
1151
+ config: {
1152
+ resourceId,
1153
+ name: `Workflow ${resourceId}`,
1154
+ description: `Test workflow ${resourceId}`,
1155
+ version: '1.0.0',
1156
+ type: 'workflow',
1157
+ status: 'dev'
1158
+ },
1159
+ contract: {
1160
+ inputSchema: z.object({ data: z.string() }),
1161
+ outputSchema: z.object({ result: z.boolean() })
1162
+ },
1163
+ steps: {},
1164
+ entryPoint: 'start'
1165
+ })
1166
+
1167
+ const createMockAgent = (resourceId: string): AgentDefinition => ({
1168
+ config: {
1169
+ resourceId,
1170
+ name: `Agent ${resourceId}`,
1171
+ description: `Test agent ${resourceId}`,
1172
+ version: '1.0.0',
1173
+ type: 'agent',
1174
+ status: 'dev',
1175
+ systemPrompt: 'You are a test agent'
1176
+ },
1177
+ contract: {
1178
+ inputSchema: z.object({ query: z.string() }),
1179
+ outputSchema: z.object({ response: z.string() })
1180
+ },
1181
+ tools: [],
1182
+ modelConfig: {
1183
+ provider: 'mock',
1184
+ model: 'mock',
1185
+ apiKey: 'test-key'
1186
+ }
1187
+ })
1188
+
1189
+ describe('validateTriggers (new field names)', () => {
1190
+ it('validates trigger.triggers.agents exist (using resourceId)', () => {
1191
+ const agent = createMockAgent('test-agent')
1192
+ const registry: OrganizationRegistry = {
1193
+ 'test-org': {
1194
+ agents: [agent],
1195
+ triggers: [
1196
+ {
1197
+ resourceId: 'test-trigger',
1198
+ type: 'trigger',
1199
+ triggerType: 'webhook',
1200
+ name: 'Test Trigger',
1201
+ description: 'Test trigger',
1202
+ version: '1.0.0',
1203
+ status: 'dev',
1204
+ triggers: { agents: ['test-agent'] }
1205
+ }
1206
+ ]
1207
+ }
1208
+ }
1209
+
1210
+ expect(() => {
1211
+ new ResourceRegistry(registry)
1212
+ }).not.toThrow()
1213
+ })
1214
+
1215
+ it('validates trigger.triggers.workflows exist (using resourceId)', () => {
1216
+ const workflow = createMockWorkflow('test-workflow')
1217
+ const registry: OrganizationRegistry = {
1218
+ 'test-org': {
1219
+ workflows: [workflow],
1220
+ triggers: [
1221
+ {
1222
+ resourceId: 'test-trigger',
1223
+ type: 'trigger',
1224
+ triggerType: 'schedule',
1225
+ name: 'Test Trigger',
1226
+ description: 'Test trigger',
1227
+ version: '1.0.0',
1228
+ status: 'dev',
1229
+ triggers: { workflows: ['test-workflow'] }
1230
+ }
1231
+ ]
1232
+ }
1233
+ }
1234
+
1235
+ expect(() => {
1236
+ new ResourceRegistry(registry)
1237
+ }).not.toThrow()
1238
+ })
1239
+
1240
+ it('throws for trigger triggering non-existent agent (via relationships)', () => {
1241
+ // Note: Trigger relationships are now declared via ResourceRelationships
1242
+ const registry: OrganizationRegistry = {
1243
+ 'test-org': {
1244
+ triggers: [
1245
+ {
1246
+ resourceId: 'test-trigger',
1247
+ type: 'trigger',
1248
+ triggerType: 'webhook',
1249
+ name: 'Test Trigger',
1250
+ description: 'Test trigger',
1251
+ version: '1.0.0',
1252
+ status: 'dev'
1253
+ }
1254
+ ],
1255
+ relationships: {
1256
+ 'test-trigger': {
1257
+ triggers: { agents: ['non-existent-agent'] }
1258
+ }
1259
+ }
1260
+ }
1261
+ }
1262
+
1263
+ expect(() => {
1264
+ new ResourceRegistry(registry)
1265
+ }).toThrow('non-existent-agent')
1266
+ })
1267
+
1268
+ it('throws for trigger triggering non-existent workflow (via relationships)', () => {
1269
+ // Note: Trigger relationships are now declared via ResourceRelationships
1270
+ const registry: OrganizationRegistry = {
1271
+ 'test-org': {
1272
+ triggers: [
1273
+ {
1274
+ resourceId: 'test-trigger',
1275
+ type: 'trigger',
1276
+ triggerType: 'event',
1277
+ name: 'Test Trigger',
1278
+ description: 'Test trigger',
1279
+ version: '1.0.0',
1280
+ status: 'dev'
1281
+ }
1282
+ ],
1283
+ relationships: {
1284
+ 'test-trigger': {
1285
+ triggers: { workflows: ['non-existent-workflow'] }
1286
+ }
1287
+ }
1288
+ }
1289
+ }
1290
+
1291
+ expect(() => {
1292
+ new ResourceRegistry(registry)
1293
+ }).toThrow('non-existent-workflow')
1294
+ })
1295
+
1296
+ it('allows trigger with no relationships (triggers are metadata only)', () => {
1297
+ // Note: Triggers without relationships are now valid - they're just metadata
1298
+ // The trigger's targets are defined in ResourceRelationships, not on the trigger itself
1299
+ const registry: OrganizationRegistry = {
1300
+ 'test-org': {
1301
+ triggers: [
1302
+ {
1303
+ resourceId: 'test-trigger',
1304
+ type: 'trigger',
1305
+ triggerType: 'webhook',
1306
+ name: 'Test Trigger',
1307
+ description: 'Test trigger',
1308
+ version: '1.0.0',
1309
+ status: 'dev'
1310
+ }
1311
+ ]
1312
+ // No relationships - trigger is just metadata
1313
+ }
1314
+ }
1315
+
1316
+ expect(() => {
1317
+ new ResourceRegistry(registry)
1318
+ }).not.toThrow()
1319
+ })
1320
+
1321
+ it('allows trigger with empty relationships object', () => {
1322
+ // Note: Empty relationships are valid - triggers without targets are allowed
1323
+ const registry: OrganizationRegistry = {
1324
+ 'test-org': {
1325
+ triggers: [
1326
+ {
1327
+ resourceId: 'test-trigger',
1328
+ type: 'trigger',
1329
+ triggerType: 'manual',
1330
+ name: 'Test Trigger',
1331
+ description: 'Test trigger',
1332
+ version: '1.0.0',
1333
+ status: 'dev'
1334
+ }
1335
+ ],
1336
+ relationships: {}
1337
+ }
1338
+ }
1339
+
1340
+ expect(() => {
1341
+ new ResourceRegistry(registry)
1342
+ }).not.toThrow()
1343
+ })
1344
+ })
1345
+
1346
+ describe('validateResourceRelationships', () => {
1347
+ it('validates resource.triggers.agents exist', () => {
1348
+ const agent1 = createMockAgent('agent-1')
1349
+ const agent2 = createMockAgent('agent-2')
1350
+ const registry: OrganizationRegistry = {
1351
+ 'test-org': {
1352
+ agents: [agent1, agent2],
1353
+ relationships: {
1354
+ 'agent-1': {
1355
+ triggers: { agents: ['agent-2'] }
1356
+ }
1357
+ }
1358
+ }
1359
+ }
1360
+
1361
+ expect(() => {
1362
+ new ResourceRegistry(registry)
1363
+ }).not.toThrow()
1364
+ })
1365
+
1366
+ it('validates resource.triggers.workflows exist', () => {
1367
+ const agent = createMockAgent('test-agent')
1368
+ const workflow = createMockWorkflow('test-workflow')
1369
+ const registry: OrganizationRegistry = {
1370
+ 'test-org': {
1371
+ agents: [agent],
1372
+ workflows: [workflow],
1373
+ relationships: {
1374
+ 'test-agent': {
1375
+ triggers: { workflows: ['test-workflow'] }
1376
+ }
1377
+ }
1378
+ }
1379
+ }
1380
+
1381
+ expect(() => {
1382
+ new ResourceRegistry(registry)
1383
+ }).not.toThrow()
1384
+ })
1385
+
1386
+ it('validates resource.uses.integrations exist', () => {
1387
+ const workflow = createMockWorkflow('test-workflow')
1388
+ const registry: OrganizationRegistry = {
1389
+ 'test-org': {
1390
+ workflows: [workflow],
1391
+ integrations: [
1392
+ {
1393
+ resourceId: 'integration-shopify',
1394
+ type: 'integration',
1395
+ provider: 'custom',
1396
+ credentialName: 'shopify-prod',
1397
+ name: 'Shopify Integration',
1398
+ description: 'E-commerce integration',
1399
+ version: '1.0.0',
1400
+ status: 'prod'
1401
+ }
1402
+ ],
1403
+ relationships: {
1404
+ 'test-workflow': {
1405
+ uses: { integrations: ['integration-shopify'] }
1406
+ }
1407
+ }
1408
+ }
1409
+ }
1410
+
1411
+ expect(() => {
1412
+ new ResourceRegistry(registry)
1413
+ }).not.toThrow()
1414
+ })
1415
+
1416
+ it('throws for relationship declared for non-existent resource', () => {
1417
+ const registry: OrganizationRegistry = {
1418
+ 'test-org': {
1419
+ relationships: {
1420
+ 'non-existent-resource': {
1421
+ triggers: { agents: ['some-agent'] }
1422
+ }
1423
+ }
1424
+ }
1425
+ }
1426
+
1427
+ expect(() => {
1428
+ new ResourceRegistry(registry)
1429
+ }).toThrow('[test-org] Relationship declared for non-existent resource: non-existent-resource')
1430
+ })
1431
+
1432
+ it('throws for relationship triggering non-existent agent', () => {
1433
+ const workflow = createMockWorkflow('test-workflow')
1434
+ const registry: OrganizationRegistry = {
1435
+ 'test-org': {
1436
+ workflows: [workflow],
1437
+ relationships: {
1438
+ 'test-workflow': {
1439
+ triggers: { agents: ['non-existent-agent'] }
1440
+ }
1441
+ }
1442
+ }
1443
+ }
1444
+
1445
+ expect(() => {
1446
+ new ResourceRegistry(registry)
1447
+ }).toThrow("[test-org] Resource 'test-workflow' triggers non-existent agent: non-existent-agent")
1448
+ })
1449
+
1450
+ it('throws for relationship using non-existent integration', () => {
1451
+ const agent = createMockAgent('test-agent')
1452
+ const registry: OrganizationRegistry = {
1453
+ 'test-org': {
1454
+ agents: [agent],
1455
+ relationships: {
1456
+ 'test-agent': {
1457
+ uses: { integrations: ['non-existent-integration'] }
1458
+ }
1459
+ }
1460
+ }
1461
+ }
1462
+
1463
+ expect(() => {
1464
+ new ResourceRegistry(registry)
1465
+ }).toThrow("[test-org] Resource 'test-agent' uses non-existent integration: non-existent-integration")
1466
+ })
1467
+ })
1468
+
1469
+ describe('validateExternalResources (forward-only)', () => {
1470
+ it('validates external.triggers.agents exist', () => {
1471
+ const agent = createMockAgent('test-agent')
1472
+ const registry: OrganizationRegistry = {
1473
+ 'test-org': {
1474
+ agents: [agent],
1475
+ externalResources: [
1476
+ {
1477
+ resourceId: 'external-n8n-workflow',
1478
+ type: 'external',
1479
+ platform: 'n8n',
1480
+ name: 'N8N Workflow',
1481
+ description: 'External automation',
1482
+ version: '1.0.0',
1483
+ status: 'prod',
1484
+ triggers: { agents: ['test-agent'] }
1485
+ }
1486
+ ]
1487
+ }
1488
+ }
1489
+
1490
+ expect(() => {
1491
+ new ResourceRegistry(registry)
1492
+ }).not.toThrow()
1493
+ })
1494
+
1495
+ it('validates external.triggers.workflows exist', () => {
1496
+ const workflow = createMockWorkflow('test-workflow')
1497
+ const registry: OrganizationRegistry = {
1498
+ 'test-org': {
1499
+ workflows: [workflow],
1500
+ externalResources: [
1501
+ {
1502
+ resourceId: 'external-zapier-zap',
1503
+ type: 'external',
1504
+ platform: 'zapier',
1505
+ name: 'Zapier Zap',
1506
+ description: 'External automation',
1507
+ version: '1.0.0',
1508
+ status: 'prod',
1509
+ triggers: { workflows: ['test-workflow'] }
1510
+ }
1511
+ ]
1512
+ }
1513
+ }
1514
+
1515
+ expect(() => {
1516
+ new ResourceRegistry(registry)
1517
+ }).not.toThrow()
1518
+ })
1519
+
1520
+ it('validates external.uses.integrations exist', () => {
1521
+ const registry: OrganizationRegistry = {
1522
+ 'test-org': {
1523
+ integrations: [
1524
+ {
1525
+ resourceId: 'integration-slack',
1526
+ type: 'integration',
1527
+ provider: 'custom',
1528
+ credentialName: 'slack-prod',
1529
+ name: 'Slack Integration',
1530
+ description: 'Chat integration',
1531
+ version: '1.0.0',
1532
+ status: 'prod'
1533
+ }
1534
+ ],
1535
+ externalResources: [
1536
+ {
1537
+ resourceId: 'external-make-scenario',
1538
+ type: 'external',
1539
+ platform: 'make',
1540
+ name: 'Make Scenario',
1541
+ description: 'External automation',
1542
+ version: '1.0.0',
1543
+ status: 'prod',
1544
+ uses: { integrations: ['integration-slack'] }
1545
+ }
1546
+ ]
1547
+ }
1548
+ }
1549
+
1550
+ expect(() => {
1551
+ new ResourceRegistry(registry)
1552
+ }).not.toThrow()
1553
+ })
1554
+
1555
+ it('validates external resourceId does not conflict with internal resources', () => {
1556
+ const workflow = createMockWorkflow('conflicting-id')
1557
+ const registry: OrganizationRegistry = {
1558
+ 'test-org': {
1559
+ workflows: [workflow],
1560
+ externalResources: [
1561
+ {
1562
+ resourceId: 'conflicting-id',
1563
+ type: 'external',
1564
+ platform: 'other',
1565
+ name: 'Conflicting External',
1566
+ description: 'Should conflict',
1567
+ version: '1.0.0',
1568
+ status: 'dev'
1569
+ }
1570
+ ]
1571
+ }
1572
+ }
1573
+
1574
+ expect(() => {
1575
+ new ResourceRegistry(registry)
1576
+ }).toThrow("[test-org] External resource ID 'conflicting-id' conflicts with internal resource ID")
1577
+ })
1578
+
1579
+ it('does NOT validate triggeredBy (field removed)', () => {
1580
+ // This test ensures we don't validate triggeredBy field (it's been removed)
1581
+ const workflow = createMockWorkflow('test-workflow')
1582
+ const registry: OrganizationRegistry = {
1583
+ 'test-org': {
1584
+ workflows: [workflow],
1585
+ externalResources: [
1586
+ {
1587
+ resourceId: 'external-resource',
1588
+ type: 'external',
1589
+ platform: 'n8n',
1590
+ name: 'External Resource',
1591
+ description: 'Has no triggeredBy field',
1592
+ version: '1.0.0',
1593
+ status: 'prod',
1594
+ triggers: { workflows: ['test-workflow'] }
1595
+ // No triggeredBy field - this is correct and should pass
1596
+ }
1597
+ ]
1598
+ }
1599
+ }
1600
+
1601
+ expect(() => {
1602
+ new ResourceRegistry(registry)
1603
+ }).not.toThrow()
1604
+ })
1605
+
1606
+ it('throws for external resource with conflicting ID', () => {
1607
+ const agent = createMockAgent('duplicate-id')
1608
+ const registry: OrganizationRegistry = {
1609
+ 'test-org': {
1610
+ agents: [agent],
1611
+ externalResources: [
1612
+ {
1613
+ resourceId: 'duplicate-id',
1614
+ type: 'external',
1615
+ platform: 'n8n',
1616
+ name: 'Duplicate ID',
1617
+ description: 'Conflicts with agent',
1618
+ version: '1.0.0',
1619
+ status: 'dev'
1620
+ }
1621
+ ]
1622
+ }
1623
+ }
1624
+
1625
+ expect(() => {
1626
+ new ResourceRegistry(registry)
1627
+ }).toThrow("[test-org] External resource ID 'duplicate-id' conflicts with internal resource ID")
1628
+ })
1629
+ })
1630
+
1631
+ describe('validateHumanCheckpoints', () => {
1632
+ it('validates requestedBy.agents exist', () => {
1633
+ const agent = createMockAgent('test-agent')
1634
+ const registry: OrganizationRegistry = {
1635
+ 'test-org': {
1636
+ agents: [agent],
1637
+ humanCheckpoints: [
1638
+ {
1639
+ resourceId: 'approval-queue',
1640
+ type: 'human',
1641
+ name: 'Approval Queue',
1642
+ description: 'Human approval checkpoint',
1643
+ version: '1.0.0',
1644
+ status: 'prod',
1645
+ requestedBy: { agents: ['test-agent'] }
1646
+ }
1647
+ ]
1648
+ }
1649
+ }
1650
+
1651
+ expect(() => {
1652
+ new ResourceRegistry(registry)
1653
+ }).not.toThrow()
1654
+ })
1655
+
1656
+ it('validates requestedBy.workflows exist', () => {
1657
+ const workflow = createMockWorkflow('test-workflow')
1658
+ const registry: OrganizationRegistry = {
1659
+ 'test-org': {
1660
+ workflows: [workflow],
1661
+ humanCheckpoints: [
1662
+ {
1663
+ resourceId: 'review-queue',
1664
+ type: 'human',
1665
+ name: 'Review Queue',
1666
+ description: 'Human review checkpoint',
1667
+ version: '1.0.0',
1668
+ status: 'prod',
1669
+ requestedBy: { workflows: ['test-workflow'] }
1670
+ }
1671
+ ]
1672
+ }
1673
+ }
1674
+
1675
+ expect(() => {
1676
+ new ResourceRegistry(registry)
1677
+ }).not.toThrow()
1678
+ })
1679
+
1680
+ it('validates routesTo.agents exist', () => {
1681
+ const agent = createMockAgent('fulfillment-agent')
1682
+ const registry: OrganizationRegistry = {
1683
+ 'test-org': {
1684
+ agents: [agent],
1685
+ humanCheckpoints: [
1686
+ {
1687
+ resourceId: 'approval-queue',
1688
+ type: 'human',
1689
+ name: 'Approval Queue',
1690
+ description: 'Human approval checkpoint',
1691
+ version: '1.0.0',
1692
+ status: 'prod',
1693
+ routesTo: { agents: ['fulfillment-agent'] }
1694
+ }
1695
+ ]
1696
+ }
1697
+ }
1698
+
1699
+ expect(() => {
1700
+ new ResourceRegistry(registry)
1701
+ }).not.toThrow()
1702
+ })
1703
+
1704
+ it('validates routesTo.workflows exist', () => {
1705
+ const workflow = createMockWorkflow('fulfillment-workflow')
1706
+ const registry: OrganizationRegistry = {
1707
+ 'test-org': {
1708
+ workflows: [workflow],
1709
+ humanCheckpoints: [
1710
+ {
1711
+ resourceId: 'approval-queue',
1712
+ type: 'human',
1713
+ name: 'Approval Queue',
1714
+ description: 'Human approval checkpoint',
1715
+ version: '1.0.0',
1716
+ status: 'prod',
1717
+ routesTo: { workflows: ['fulfillment-workflow'] }
1718
+ }
1719
+ ]
1720
+ }
1721
+ }
1722
+
1723
+ expect(() => {
1724
+ new ResourceRegistry(registry)
1725
+ }).not.toThrow()
1726
+ })
1727
+
1728
+ it('throws for requestedBy referencing non-existent agent', () => {
1729
+ const registry: OrganizationRegistry = {
1730
+ 'test-org': {
1731
+ humanCheckpoints: [
1732
+ {
1733
+ resourceId: 'approval-queue',
1734
+ type: 'human',
1735
+ name: 'Approval Queue',
1736
+ description: 'Human approval checkpoint',
1737
+ version: '1.0.0',
1738
+ status: 'prod',
1739
+ requestedBy: { agents: ['non-existent-agent'] }
1740
+ }
1741
+ ]
1742
+ }
1743
+ }
1744
+
1745
+ expect(() => {
1746
+ new ResourceRegistry(registry)
1747
+ }).toThrow("[test-org] Human checkpoint 'approval-queue' requestedBy non-existent agent: non-existent-agent")
1748
+ })
1749
+
1750
+ it('throws for routesTo referencing non-existent workflow', () => {
1751
+ const registry: OrganizationRegistry = {
1752
+ 'test-org': {
1753
+ humanCheckpoints: [
1754
+ {
1755
+ resourceId: 'approval-queue',
1756
+ type: 'human',
1757
+ name: 'Approval Queue',
1758
+ description: 'Human approval checkpoint',
1759
+ version: '1.0.0',
1760
+ status: 'prod',
1761
+ routesTo: { workflows: ['non-existent-workflow'] }
1762
+ }
1763
+ ]
1764
+ }
1765
+ }
1766
+
1767
+ expect(() => {
1768
+ new ResourceRegistry(registry)
1769
+ }).toThrow("[test-org] Human checkpoint 'approval-queue' routesTo non-existent workflow: non-existent-workflow")
1770
+ })
1771
+
1772
+ it('throws for human checkpoint with conflicting ID', () => {
1773
+ const workflow = createMockWorkflow('conflicting-id')
1774
+ const registry: OrganizationRegistry = {
1775
+ 'test-org': {
1776
+ workflows: [workflow],
1777
+ humanCheckpoints: [
1778
+ {
1779
+ resourceId: 'conflicting-id',
1780
+ type: 'human',
1781
+ name: 'Conflicting Checkpoint',
1782
+ description: 'Conflicts with workflow',
1783
+ version: '1.0.0',
1784
+ status: 'dev'
1785
+ }
1786
+ ]
1787
+ }
1788
+ }
1789
+
1790
+ expect(() => {
1791
+ new ResourceRegistry(registry)
1792
+ }).toThrow("[test-org] Human checkpoint ID 'conflicting-id' conflicts with internal resource ID")
1793
+ })
1794
+ })
1795
+ })
1796
+
1797
+ describe('detectMissingApiInterfaceDeclarations', () => {
1798
+ const makeSystem = (id: string, apiInterface?: object) => ({
1799
+ id,
1800
+ title: id,
1801
+ description: `System ${id}`,
1802
+ kind: 'operational' as const,
1803
+ governedByKnowledge: [],
1804
+ drivesGoals: [],
1805
+ status: 'active' as const,
1806
+ order: 10,
1807
+ ...(apiInterface !== undefined ? { apiInterface } : {})
1808
+ })
1809
+
1810
+ const makeResource = (id: string, systemPath: string, ontology?: object): ResourceEntry => ({
1811
+ id,
1812
+ kind: 'workflow' as const,
1813
+ systemPath,
1814
+ status: 'active' as const,
1815
+ order: 10,
1816
+ ...(ontology !== undefined ? { ontology } : {})
1817
+ })
1818
+
1819
+ it('returns [] when organizationModel is undefined', () => {
1820
+ const result = detectMissingApiInterfaceDeclarations('test-org', undefined)
1821
+ expect(result).toEqual([])
1822
+ })
1823
+
1824
+ it('returns [] when a system declares apiInterface and has a matching resource with ontology bindings', () => {
1825
+ const model = {
1826
+ systems: {
1827
+ 'sales.lead-gen': makeSystem('sales.lead-gen', {
1828
+ lifecycle: 'active',
1829
+ readinessProfile: 'sales.lead-gen.api'
1830
+ })
1831
+ },
1832
+ resources: {
1833
+ 'lead-import': makeResource('lead-import', 'sales.lead-gen', {
1834
+ actions: ['sales.lead-gen:action/import-leads'],
1835
+ writes: ['sales.lead-gen:object/lead']
1836
+ })
1837
+ }
1838
+ }
1839
+
1840
+ const result = detectMissingApiInterfaceDeclarations('test-org', model as never)
1841
+ expect(result).toEqual([])
1842
+ })
1843
+
1844
+ it('returns one gap when a system lacks apiInterface and has a matching resource with non-empty ontology', () => {
1845
+ const model = {
1846
+ systems: {
1847
+ 'sales.lead-gen': makeSystem('sales.lead-gen')
1848
+ },
1849
+ resources: {
1850
+ 'lead-import': makeResource('lead-import', 'sales.lead-gen', {
1851
+ actions: ['sales.lead-gen:action/import-leads'],
1852
+ writes: ['sales.lead-gen:object/lead']
1853
+ })
1854
+ }
1855
+ }
1856
+
1857
+ const result = detectMissingApiInterfaceDeclarations('test-org', model as never)
1858
+ expect(result).toHaveLength(1)
1859
+ expect(result[0].systemPath).toBe('sales.lead-gen')
1860
+ expect(result[0].resourceIds).toEqual(['lead-import'])
1861
+ expect(result[0].message).toContain('sales.lead-gen')
1862
+ expect(result[0].message).toContain('lead-import')
1863
+ })
1864
+
1865
+ it('returns [] when a system lacks apiInterface but its matching resource has empty ontology (no actions or writes)', () => {
1866
+ const model = {
1867
+ systems: {
1868
+ 'sales.lead-gen': makeSystem('sales.lead-gen')
1869
+ },
1870
+ resources: {
1871
+ 'lead-import': makeResource('lead-import', 'sales.lead-gen', {
1872
+ reads: ['sales.lead-gen:object/company']
1873
+ })
1874
+ }
1875
+ }
1876
+
1877
+ const result = detectMissingApiInterfaceDeclarations('test-org', model as never)
1878
+ expect(result).toEqual([])
1879
+ })
1880
+ })