@elevasis/core 0.20.0 → 0.22.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.
Files changed (77) hide show
  1. package/dist/index.d.ts +524 -6
  2. package/dist/index.js +417 -42
  3. package/dist/knowledge/index.d.ts +151 -1
  4. package/dist/organization-model/index.d.ts +524 -6
  5. package/dist/organization-model/index.js +417 -42
  6. package/dist/test-utils/index.d.ts +270 -1
  7. package/dist/test-utils/index.js +407 -41
  8. package/package.json +5 -5
  9. package/src/_gen/__tests__/__snapshots__/contracts.md.snap +501 -303
  10. package/src/auth/multi-tenancy/permissions.ts +20 -8
  11. package/src/business/README.md +2 -2
  12. package/src/business/acquisition/api-schemas.test.ts +198 -0
  13. package/src/business/acquisition/api-schemas.ts +250 -9
  14. package/src/business/acquisition/build-templates.test.ts +28 -0
  15. package/src/business/acquisition/build-templates.ts +20 -8
  16. package/src/business/acquisition/index.ts +12 -0
  17. package/src/business/acquisition/types.ts +6 -1
  18. package/src/business/clients/api-schemas.test.ts +115 -0
  19. package/src/business/clients/api-schemas.ts +158 -0
  20. package/src/business/clients/index.ts +1 -0
  21. package/src/business/deals/api-schemas.ts +8 -0
  22. package/src/business/index.ts +5 -2
  23. package/src/business/projects/types.ts +19 -0
  24. package/src/execution/engine/__tests__/fixtures/test-agents.ts +10 -8
  25. package/src/execution/engine/agent/core/__tests__/agent.test.ts +16 -12
  26. package/src/execution/engine/agent/core/__tests__/error-passthrough.test.ts +4 -3
  27. package/src/execution/engine/agent/core/types.ts +25 -15
  28. package/src/execution/engine/agent/index.ts +6 -4
  29. package/src/execution/engine/agent/reasoning/__tests__/request-builder.test.ts +24 -18
  30. package/src/execution/engine/index.ts +3 -0
  31. package/src/execution/engine/tools/integration/server/adapters/apify/apify-adapter.test.ts +55 -0
  32. package/src/execution/engine/tools/integration/server/adapters/apify/apify-adapter.ts +107 -41
  33. package/src/execution/engine/tools/integration/server/adapters/apollo/apollo-adapter.test.ts +48 -0
  34. package/src/execution/engine/tools/integration/server/adapters/apollo/apollo-adapter.ts +99 -0
  35. package/src/execution/engine/tools/integration/server/adapters/apollo/index.ts +1 -0
  36. package/src/execution/engine/tools/integration/server/adapters/clickup/clickup-adapter.test.ts +18 -0
  37. package/src/execution/engine/tools/integration/server/adapters/clickup/clickup-adapter.ts +194 -0
  38. package/src/execution/engine/tools/integration/server/adapters/clickup/index.ts +7 -0
  39. package/src/execution/engine/workflow/types.ts +7 -0
  40. package/src/integrations/credentials/api-schemas.ts +21 -2
  41. package/src/integrations/credentials/schemas.ts +200 -164
  42. package/src/organization-model/README.md +10 -3
  43. package/src/organization-model/__tests__/defaults.test.ts +6 -0
  44. package/src/organization-model/__tests__/domains/resources.test.ts +188 -0
  45. package/src/organization-model/__tests__/domains/roles.test.ts +402 -347
  46. package/src/organization-model/__tests__/domains/systems.test.ts +193 -0
  47. package/src/organization-model/__tests__/knowledge.test.ts +39 -0
  48. package/src/organization-model/__tests__/prospecting-ssot.test.ts +7 -4
  49. package/src/organization-model/__tests__/resolve.test.ts +1 -1
  50. package/src/organization-model/defaults.ts +24 -3
  51. package/src/organization-model/domains/knowledge.ts +3 -2
  52. package/src/organization-model/domains/prospecting.ts +182 -25
  53. package/src/organization-model/domains/resources.ts +88 -0
  54. package/src/organization-model/domains/roles.ts +93 -55
  55. package/src/organization-model/domains/sales.ts +24 -3
  56. package/src/organization-model/domains/systems.ts +46 -0
  57. package/src/organization-model/icons.ts +1 -0
  58. package/src/organization-model/index.ts +2 -0
  59. package/src/organization-model/organization-model.mdx +33 -14
  60. package/src/organization-model/published.ts +52 -1
  61. package/src/organization-model/schema.ts +121 -0
  62. package/src/organization-model/types.ts +46 -1
  63. package/src/platform/api/types.ts +38 -35
  64. package/src/platform/constants/versions.ts +1 -1
  65. package/src/platform/registry/__tests__/resource-registry.test.ts +2051 -2005
  66. package/src/platform/registry/__tests__/validation.test.ts +1343 -1086
  67. package/src/platform/registry/index.ts +14 -0
  68. package/src/platform/registry/resource-registry.ts +40 -2
  69. package/src/platform/registry/serialization.ts +241 -202
  70. package/src/platform/registry/serialized-types.ts +1 -0
  71. package/src/platform/registry/types.ts +411 -361
  72. package/src/platform/registry/validation.ts +743 -513
  73. package/src/projects/api-schemas.ts +290 -267
  74. package/src/reference/_generated/contracts.md +501 -303
  75. package/src/reference/glossary.md +8 -3
  76. package/src/server.ts +2 -0
  77. package/src/supabase/database.types.ts +121 -0
@@ -1,1086 +1,1343 @@
1
- /**
2
- * Test: Registry Validation Utilities
3
- * Tests for ExecutionInterface-to-inputSchema validation
4
- */
5
-
6
- import { describe, it, expect } from 'vitest'
7
- import { z } from 'zod'
8
- import { validateExecutionInterface, RegistryValidationError } from '../validation'
9
- import { ResourceRegistry } from '../resource-registry'
10
- import type { WorkflowDefinition } from '../../../execution/engine/workflow/types'
11
- import type { AgentDefinition } from '../../../execution/engine/agent/core/types'
12
- import type { OrganizationRegistry } from '../resource-registry'
13
-
14
- describe('validateExecutionInterface', () => {
15
- describe('required field validation', () => {
16
- it('should pass when all required schema fields have form fields', () => {
17
- const inputSchema = z.object({
18
- name: z.string(),
19
- email: z.string()
20
- })
21
-
22
- const executionInterface = {
23
- form: {
24
- fields: [
25
- { name: 'name', required: true },
26
- { name: 'email', required: true }
27
- ]
28
- }
29
- }
30
-
31
- expect(() => {
32
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
33
- }).not.toThrow()
34
- })
35
-
36
- it('should throw when required schema field is missing from form', () => {
37
- const inputSchema = z.object({
38
- name: z.string(),
39
- email: z.string()
40
- })
41
-
42
- const executionInterface = {
43
- form: {
44
- fields: [
45
- { name: 'name', required: true }
46
- // Missing 'email' field
47
- ]
48
- }
49
- }
50
-
51
- expect(() => {
52
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
53
- }).toThrow(RegistryValidationError)
54
- expect(() => {
55
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
56
- }).toThrow('missing required field "email"')
57
- })
58
-
59
- it('should throw when form field is not marked required but schema field is required', () => {
60
- const inputSchema = z.object({
61
- name: z.string() // Required
62
- })
63
-
64
- const executionInterface = {
65
- form: {
66
- fields: [
67
- { name: 'name', required: false } // Not marked required
68
- ]
69
- }
70
- }
71
-
72
- expect(() => {
73
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
74
- }).toThrow(RegistryValidationError)
75
- expect(() => {
76
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
77
- }).toThrow('should be required')
78
- })
79
-
80
- it('should pass when optional schema field is missing from form', () => {
81
- const inputSchema = z.object({
82
- name: z.string(),
83
- description: z.string().optional()
84
- })
85
-
86
- const executionInterface = {
87
- form: {
88
- fields: [
89
- { name: 'name', required: true }
90
- // 'description' is optional, so OK to omit
91
- ]
92
- }
93
- }
94
-
95
- expect(() => {
96
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
97
- }).not.toThrow()
98
- })
99
-
100
- it('should pass when optional schema field has non-required form field', () => {
101
- const inputSchema = z.object({
102
- name: z.string(),
103
- description: z.string().optional()
104
- })
105
-
106
- const executionInterface = {
107
- form: {
108
- fields: [
109
- { name: 'name', required: true },
110
- { name: 'description', required: false }
111
- ]
112
- }
113
- }
114
-
115
- expect(() => {
116
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
117
- }).not.toThrow()
118
- })
119
- })
120
-
121
- describe('field mapping validation', () => {
122
- it('should pass with valid field mappings', () => {
123
- const inputSchema = z.object({
124
- taskName: z.string(),
125
- priority: z.string()
126
- })
127
-
128
- const executionInterface = {
129
- form: {
130
- fields: [
131
- { name: 'task_name', required: true },
132
- { name: 'prio', required: true }
133
- ],
134
- fieldMappings: {
135
- task_name: 'taskName',
136
- prio: 'priority'
137
- }
138
- }
139
- }
140
-
141
- expect(() => {
142
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
143
- }).not.toThrow()
144
- })
145
-
146
- it('should throw when field mapping points to non-existent schema field', () => {
147
- const inputSchema = z.object({
148
- name: z.string()
149
- })
150
-
151
- // Include a valid mapping for 'name' so we get past the required field check,
152
- // then add an extra field that maps to a non-existent schema field
153
- const executionInterface = {
154
- form: {
155
- fields: [
156
- { name: 'userName', required: true },
157
- { name: 'extraField', required: false }
158
- ],
159
- fieldMappings: {
160
- userName: 'name', // Valid mapping
161
- extraField: 'nonExistentField' // Invalid mapping
162
- }
163
- }
164
- }
165
-
166
- expect(() => {
167
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
168
- }).toThrow(RegistryValidationError)
169
- expect(() => {
170
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
171
- }).toThrow('maps to non-existent schema field "nonExistentField"')
172
- })
173
-
174
- it('should throw when form field without mapping has no matching schema field', () => {
175
- const inputSchema = z.object({
176
- name: z.string()
177
- })
178
-
179
- const executionInterface = {
180
- form: {
181
- fields: [
182
- { name: 'name', required: true },
183
- { name: 'unknownField', required: false }
184
- ]
185
- }
186
- }
187
-
188
- expect(() => {
189
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
190
- }).toThrow(RegistryValidationError)
191
- expect(() => {
192
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
193
- }).toThrow('maps to non-existent schema field "unknownField"')
194
- })
195
-
196
- it('should throw with flatten suggestion when form field uses nested notation', () => {
197
- const inputSchema = z.object({
198
- name: z.string()
199
- })
200
-
201
- const executionInterface = {
202
- form: {
203
- fields: [
204
- { name: 'name', required: true },
205
- { name: 'criteria.targetTitles', required: true }
206
- ]
207
- }
208
- }
209
-
210
- expect(() => {
211
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
212
- }).toThrow(RegistryValidationError)
213
- expect(() => {
214
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
215
- }).toThrow('uses nested notation')
216
- expect(() => {
217
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
218
- }).toThrow('Flatten the inputSchema')
219
- })
220
- })
221
-
222
- describe('default values (effectively optional)', () => {
223
- it('should treat fields with defaults as optional', () => {
224
- const inputSchema = z.object({
225
- name: z.string(),
226
- priority: z.string().default('medium')
227
- })
228
-
229
- const executionInterface = {
230
- form: {
231
- fields: [
232
- { name: 'name', required: true }
233
- // 'priority' has default, so OK to omit
234
- ]
235
- }
236
- }
237
-
238
- expect(() => {
239
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
240
- }).not.toThrow()
241
- })
242
- })
243
-
244
- describe('enum fields', () => {
245
- it('should validate enum fields correctly', () => {
246
- const inputSchema = z.object({
247
- priority: z.enum(['low', 'medium', 'high'])
248
- })
249
-
250
- const executionInterface = {
251
- form: {
252
- fields: [{ name: 'priority', required: true }]
253
- }
254
- }
255
-
256
- expect(() => {
257
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
258
- }).not.toThrow()
259
- })
260
- })
261
-
262
- describe('empty schema', () => {
263
- it('should pass with empty schema and no form fields', () => {
264
- const inputSchema = z.object({})
265
-
266
- const executionInterface = {
267
- form: {
268
- fields: []
269
- }
270
- }
271
-
272
- expect(() => {
273
- validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
274
- }).not.toThrow()
275
- })
276
- })
277
- })
278
-
279
- describe('ResourceRegistry - ExecutionInterface validation integration', () => {
280
- it('should throw on registry construction when interface misses required field', () => {
281
- const workflow: WorkflowDefinition = {
282
- config: {
283
- resourceId: 'test-workflow',
284
- name: 'Test Workflow',
285
- description: 'Test',
286
- version: '1.0.0',
287
- type: 'workflow',
288
- status: 'dev'
289
- },
290
- contract: {
291
- inputSchema: z.object({
292
- taskName: z.string(),
293
- priority: z.string()
294
- }),
295
- outputSchema: z.object({})
296
- },
297
- steps: {},
298
- entryPoint: 'start',
299
- interface: {
300
- form: {
301
- fields: [
302
- { name: 'taskName', label: 'Task Name', type: 'text', required: true }
303
- // Missing 'priority' field
304
- ]
305
- }
306
- }
307
- }
308
-
309
- const registry: OrganizationRegistry = {
310
- 'test-org': {
311
- workflows: [workflow]
312
- }
313
- }
314
-
315
- expect(() => {
316
- new ResourceRegistry(registry)
317
- }).toThrow('missing required field "priority"')
318
- })
319
-
320
- it('should throw when agent interface misses required field', () => {
321
- const agent: AgentDefinition = {
322
- config: {
323
- resourceId: 'test-agent',
324
- name: 'Test Agent',
325
- description: 'Test',
326
- version: '1.0.0',
327
- type: 'agent',
328
- status: 'dev',
329
- systemPrompt: 'You are a test agent'
330
- },
331
- contract: {
332
- inputSchema: z.object({
333
- task: z.string(),
334
- context: z.string()
335
- }),
336
- outputSchema: z.object({})
337
- },
338
- tools: [],
339
- modelConfig: {
340
- provider: 'mock',
341
- model: 'mock',
342
- apiKey: 'test'
343
- },
344
- interface: {
345
- form: {
346
- fields: [
347
- { name: 'task', label: 'Task', type: 'textarea', required: true }
348
- // Missing 'context' field
349
- ]
350
- }
351
- }
352
- }
353
-
354
- const registry: OrganizationRegistry = {
355
- 'test-org': {
356
- agents: [agent]
357
- }
358
- }
359
-
360
- expect(() => {
361
- new ResourceRegistry(registry)
362
- }).toThrow('missing required field "context"')
363
- })
364
-
365
- it('should pass when interface correctly matches schema', () => {
366
- const workflow: WorkflowDefinition = {
367
- config: {
368
- resourceId: 'valid-workflow',
369
- name: 'Valid Workflow',
370
- description: 'Test',
371
- version: '1.0.0',
372
- type: 'workflow',
373
- status: 'dev'
374
- },
375
- contract: {
376
- inputSchema: z.object({
377
- taskName: z.string(),
378
- priority: z.enum(['low', 'medium', 'high']),
379
- description: z.string().optional()
380
- }),
381
- outputSchema: z.object({})
382
- },
383
- steps: {},
384
- entryPoint: 'start',
385
- interface: {
386
- form: {
387
- title: 'Run Workflow',
388
- fields: [
389
- { name: 'taskName', label: 'Task Name', type: 'text', required: true },
390
- { name: 'priority', label: 'Priority', type: 'select', required: true },
391
- { name: 'description', label: 'Description', type: 'textarea', required: false }
392
- ]
393
- }
394
- }
395
- }
396
-
397
- const registry: OrganizationRegistry = {
398
- 'test-org': {
399
- workflows: [workflow]
400
- }
401
- }
402
-
403
- expect(() => {
404
- new ResourceRegistry(registry)
405
- }).not.toThrow()
406
- })
407
-
408
- it('should pass when workflow has no interface (optional)', () => {
409
- const workflow: WorkflowDefinition = {
410
- config: {
411
- resourceId: 'no-interface-workflow',
412
- name: 'No Interface Workflow',
413
- description: 'Test',
414
- version: '1.0.0',
415
- type: 'workflow',
416
- status: 'dev'
417
- },
418
- contract: {
419
- inputSchema: z.object({ data: z.string() }),
420
- outputSchema: z.object({})
421
- },
422
- steps: {},
423
- entryPoint: 'start'
424
- // No interface - should pass
425
- }
426
-
427
- const registry: OrganizationRegistry = {
428
- 'test-org': {
429
- workflows: [workflow]
430
- }
431
- }
432
-
433
- expect(() => {
434
- new ResourceRegistry(registry)
435
- }).not.toThrow()
436
- })
437
- })
438
-
439
- describe('Relationship Validation', () => {
440
- // Helper to create minimal mock resources
441
- const createMockWorkflow = (resourceId: string): WorkflowDefinition => ({
442
- config: {
443
- resourceId,
444
- name: `Workflow ${resourceId}`,
445
- description: `Test workflow ${resourceId}`,
446
- version: '1.0.0',
447
- type: 'workflow',
448
- status: 'dev'
449
- },
450
- contract: {
451
- inputSchema: z.object({ data: z.string() }),
452
- outputSchema: z.object({ result: z.boolean() })
453
- },
454
- steps: {},
455
- entryPoint: 'start'
456
- })
457
-
458
- const createMockAgent = (resourceId: string): AgentDefinition => ({
459
- config: {
460
- resourceId,
461
- name: `Agent ${resourceId}`,
462
- description: `Test agent ${resourceId}`,
463
- version: '1.0.0',
464
- type: 'agent',
465
- status: 'dev',
466
- systemPrompt: 'You are a test agent'
467
- },
468
- contract: {
469
- inputSchema: z.object({ query: z.string() }),
470
- outputSchema: z.object({ response: z.string() })
471
- },
472
- tools: [],
473
- modelConfig: {
474
- provider: 'mock',
475
- model: 'mock',
476
- apiKey: 'test-key'
477
- }
478
- })
479
-
480
- describe('validateTriggers (new field names)', () => {
481
- it('validates trigger.triggers.agents exist (using resourceId)', () => {
482
- const agent = createMockAgent('test-agent')
483
- const registry: OrganizationRegistry = {
484
- 'test-org': {
485
- agents: [agent],
486
- triggers: [
487
- {
488
- resourceId: 'test-trigger',
489
- type: 'trigger',
490
- triggerType: 'webhook',
491
- name: 'Test Trigger',
492
- description: 'Test trigger',
493
- version: '1.0.0',
494
- status: 'dev',
495
- triggers: { agents: ['test-agent'] }
496
- }
497
- ]
498
- }
499
- }
500
-
501
- expect(() => {
502
- new ResourceRegistry(registry)
503
- }).not.toThrow()
504
- })
505
-
506
- it('validates trigger.triggers.workflows exist (using resourceId)', () => {
507
- const workflow = createMockWorkflow('test-workflow')
508
- const registry: OrganizationRegistry = {
509
- 'test-org': {
510
- workflows: [workflow],
511
- triggers: [
512
- {
513
- resourceId: 'test-trigger',
514
- type: 'trigger',
515
- triggerType: 'schedule',
516
- name: 'Test Trigger',
517
- description: 'Test trigger',
518
- version: '1.0.0',
519
- status: 'dev',
520
- triggers: { workflows: ['test-workflow'] }
521
- }
522
- ]
523
- }
524
- }
525
-
526
- expect(() => {
527
- new ResourceRegistry(registry)
528
- }).not.toThrow()
529
- })
530
-
531
- it('throws for trigger triggering non-existent agent (via relationships)', () => {
532
- // Note: Trigger relationships are now declared via ResourceRelationships
533
- const registry: OrganizationRegistry = {
534
- 'test-org': {
535
- triggers: [
536
- {
537
- resourceId: 'test-trigger',
538
- type: 'trigger',
539
- triggerType: 'webhook',
540
- name: 'Test Trigger',
541
- description: 'Test trigger',
542
- version: '1.0.0',
543
- status: 'dev'
544
- }
545
- ],
546
- relationships: {
547
- 'test-trigger': {
548
- triggers: { agents: ['non-existent-agent'] }
549
- }
550
- }
551
- }
552
- }
553
-
554
- expect(() => {
555
- new ResourceRegistry(registry)
556
- }).toThrow('non-existent-agent')
557
- })
558
-
559
- it('throws for trigger triggering non-existent workflow (via relationships)', () => {
560
- // Note: Trigger relationships are now declared via ResourceRelationships
561
- const registry: OrganizationRegistry = {
562
- 'test-org': {
563
- triggers: [
564
- {
565
- resourceId: 'test-trigger',
566
- type: 'trigger',
567
- triggerType: 'event',
568
- name: 'Test Trigger',
569
- description: 'Test trigger',
570
- version: '1.0.0',
571
- status: 'dev'
572
- }
573
- ],
574
- relationships: {
575
- 'test-trigger': {
576
- triggers: { workflows: ['non-existent-workflow'] }
577
- }
578
- }
579
- }
580
- }
581
-
582
- expect(() => {
583
- new ResourceRegistry(registry)
584
- }).toThrow('non-existent-workflow')
585
- })
586
-
587
- it('allows trigger with no relationships (triggers are metadata only)', () => {
588
- // Note: Triggers without relationships are now valid - they're just metadata
589
- // The trigger's targets are defined in ResourceRelationships, not on the trigger itself
590
- const registry: OrganizationRegistry = {
591
- 'test-org': {
592
- triggers: [
593
- {
594
- resourceId: 'test-trigger',
595
- type: 'trigger',
596
- triggerType: 'webhook',
597
- name: 'Test Trigger',
598
- description: 'Test trigger',
599
- version: '1.0.0',
600
- status: 'dev'
601
- }
602
- ]
603
- // No relationships - trigger is just metadata
604
- }
605
- }
606
-
607
- expect(() => {
608
- new ResourceRegistry(registry)
609
- }).not.toThrow()
610
- })
611
-
612
- it('allows trigger with empty relationships object', () => {
613
- // Note: Empty relationships are valid - triggers without targets are allowed
614
- const registry: OrganizationRegistry = {
615
- 'test-org': {
616
- triggers: [
617
- {
618
- resourceId: 'test-trigger',
619
- type: 'trigger',
620
- triggerType: 'manual',
621
- name: 'Test Trigger',
622
- description: 'Test trigger',
623
- version: '1.0.0',
624
- status: 'dev'
625
- }
626
- ],
627
- relationships: {}
628
- }
629
- }
630
-
631
- expect(() => {
632
- new ResourceRegistry(registry)
633
- }).not.toThrow()
634
- })
635
- })
636
-
637
- describe('validateResourceRelationships', () => {
638
- it('validates resource.triggers.agents exist', () => {
639
- const agent1 = createMockAgent('agent-1')
640
- const agent2 = createMockAgent('agent-2')
641
- const registry: OrganizationRegistry = {
642
- 'test-org': {
643
- agents: [agent1, agent2],
644
- relationships: {
645
- 'agent-1': {
646
- triggers: { agents: ['agent-2'] }
647
- }
648
- }
649
- }
650
- }
651
-
652
- expect(() => {
653
- new ResourceRegistry(registry)
654
- }).not.toThrow()
655
- })
656
-
657
- it('validates resource.triggers.workflows exist', () => {
658
- const agent = createMockAgent('test-agent')
659
- const workflow = createMockWorkflow('test-workflow')
660
- const registry: OrganizationRegistry = {
661
- 'test-org': {
662
- agents: [agent],
663
- workflows: [workflow],
664
- relationships: {
665
- 'test-agent': {
666
- triggers: { workflows: ['test-workflow'] }
667
- }
668
- }
669
- }
670
- }
671
-
672
- expect(() => {
673
- new ResourceRegistry(registry)
674
- }).not.toThrow()
675
- })
676
-
677
- it('validates resource.uses.integrations exist', () => {
678
- const workflow = createMockWorkflow('test-workflow')
679
- const registry: OrganizationRegistry = {
680
- 'test-org': {
681
- workflows: [workflow],
682
- integrations: [
683
- {
684
- resourceId: 'integration-shopify',
685
- type: 'integration',
686
- provider: 'custom',
687
- credentialName: 'shopify-prod',
688
- name: 'Shopify Integration',
689
- description: 'E-commerce integration',
690
- version: '1.0.0',
691
- status: 'prod'
692
- }
693
- ],
694
- relationships: {
695
- 'test-workflow': {
696
- uses: { integrations: ['integration-shopify'] }
697
- }
698
- }
699
- }
700
- }
701
-
702
- expect(() => {
703
- new ResourceRegistry(registry)
704
- }).not.toThrow()
705
- })
706
-
707
- it('throws for relationship declared for non-existent resource', () => {
708
- const registry: OrganizationRegistry = {
709
- 'test-org': {
710
- relationships: {
711
- 'non-existent-resource': {
712
- triggers: { agents: ['some-agent'] }
713
- }
714
- }
715
- }
716
- }
717
-
718
- expect(() => {
719
- new ResourceRegistry(registry)
720
- }).toThrow('[test-org] Relationship declared for non-existent resource: non-existent-resource')
721
- })
722
-
723
- it('throws for relationship triggering non-existent agent', () => {
724
- const workflow = createMockWorkflow('test-workflow')
725
- const registry: OrganizationRegistry = {
726
- 'test-org': {
727
- workflows: [workflow],
728
- relationships: {
729
- 'test-workflow': {
730
- triggers: { agents: ['non-existent-agent'] }
731
- }
732
- }
733
- }
734
- }
735
-
736
- expect(() => {
737
- new ResourceRegistry(registry)
738
- }).toThrow('[test-org] Resource \'test-workflow\' triggers non-existent agent: non-existent-agent')
739
- })
740
-
741
- it('throws for relationship using non-existent integration', () => {
742
- const agent = createMockAgent('test-agent')
743
- const registry: OrganizationRegistry = {
744
- 'test-org': {
745
- agents: [agent],
746
- relationships: {
747
- 'test-agent': {
748
- uses: { integrations: ['non-existent-integration'] }
749
- }
750
- }
751
- }
752
- }
753
-
754
- expect(() => {
755
- new ResourceRegistry(registry)
756
- }).toThrow('[test-org] Resource \'test-agent\' uses non-existent integration: non-existent-integration')
757
- })
758
- })
759
-
760
- describe('validateExternalResources (forward-only)', () => {
761
- it('validates external.triggers.agents exist', () => {
762
- const agent = createMockAgent('test-agent')
763
- const registry: OrganizationRegistry = {
764
- 'test-org': {
765
- agents: [agent],
766
- externalResources: [
767
- {
768
- resourceId: 'external-n8n-workflow',
769
- type: 'external',
770
- platform: 'n8n',
771
- name: 'N8N Workflow',
772
- description: 'External automation',
773
- version: '1.0.0',
774
- status: 'prod',
775
- triggers: { agents: ['test-agent'] }
776
- }
777
- ]
778
- }
779
- }
780
-
781
- expect(() => {
782
- new ResourceRegistry(registry)
783
- }).not.toThrow()
784
- })
785
-
786
- it('validates external.triggers.workflows exist', () => {
787
- const workflow = createMockWorkflow('test-workflow')
788
- const registry: OrganizationRegistry = {
789
- 'test-org': {
790
- workflows: [workflow],
791
- externalResources: [
792
- {
793
- resourceId: 'external-zapier-zap',
794
- type: 'external',
795
- platform: 'zapier',
796
- name: 'Zapier Zap',
797
- description: 'External automation',
798
- version: '1.0.0',
799
- status: 'prod',
800
- triggers: { workflows: ['test-workflow'] }
801
- }
802
- ]
803
- }
804
- }
805
-
806
- expect(() => {
807
- new ResourceRegistry(registry)
808
- }).not.toThrow()
809
- })
810
-
811
- it('validates external.uses.integrations exist', () => {
812
- const registry: OrganizationRegistry = {
813
- 'test-org': {
814
- integrations: [
815
- {
816
- resourceId: 'integration-slack',
817
- type: 'integration',
818
- provider: 'custom',
819
- credentialName: 'slack-prod',
820
- name: 'Slack Integration',
821
- description: 'Chat integration',
822
- version: '1.0.0',
823
- status: 'prod'
824
- }
825
- ],
826
- externalResources: [
827
- {
828
- resourceId: 'external-make-scenario',
829
- type: 'external',
830
- platform: 'make',
831
- name: 'Make Scenario',
832
- description: 'External automation',
833
- version: '1.0.0',
834
- status: 'prod',
835
- uses: { integrations: ['integration-slack'] }
836
- }
837
- ]
838
- }
839
- }
840
-
841
- expect(() => {
842
- new ResourceRegistry(registry)
843
- }).not.toThrow()
844
- })
845
-
846
- it('validates external resourceId does not conflict with internal resources', () => {
847
- const workflow = createMockWorkflow('conflicting-id')
848
- const registry: OrganizationRegistry = {
849
- 'test-org': {
850
- workflows: [workflow],
851
- externalResources: [
852
- {
853
- resourceId: 'conflicting-id',
854
- type: 'external',
855
- platform: 'other',
856
- name: 'Conflicting External',
857
- description: 'Should conflict',
858
- version: '1.0.0',
859
- status: 'dev'
860
- }
861
- ]
862
- }
863
- }
864
-
865
- expect(() => {
866
- new ResourceRegistry(registry)
867
- }).toThrow('[test-org] External resource ID \'conflicting-id\' conflicts with internal resource ID')
868
- })
869
-
870
- it('does NOT validate triggeredBy (field removed)', () => {
871
- // This test ensures we don't validate triggeredBy field (it's been removed)
872
- const workflow = createMockWorkflow('test-workflow')
873
- const registry: OrganizationRegistry = {
874
- 'test-org': {
875
- workflows: [workflow],
876
- externalResources: [
877
- {
878
- resourceId: 'external-resource',
879
- type: 'external',
880
- platform: 'n8n',
881
- name: 'External Resource',
882
- description: 'Has no triggeredBy field',
883
- version: '1.0.0',
884
- status: 'prod',
885
- triggers: { workflows: ['test-workflow'] }
886
- // No triggeredBy field - this is correct and should pass
887
- }
888
- ]
889
- }
890
- }
891
-
892
- expect(() => {
893
- new ResourceRegistry(registry)
894
- }).not.toThrow()
895
- })
896
-
897
- it('throws for external resource with conflicting ID', () => {
898
- const agent = createMockAgent('duplicate-id')
899
- const registry: OrganizationRegistry = {
900
- 'test-org': {
901
- agents: [agent],
902
- externalResources: [
903
- {
904
- resourceId: 'duplicate-id',
905
- type: 'external',
906
- platform: 'n8n',
907
- name: 'Duplicate ID',
908
- description: 'Conflicts with agent',
909
- version: '1.0.0',
910
- status: 'dev'
911
- }
912
- ]
913
- }
914
- }
915
-
916
- expect(() => {
917
- new ResourceRegistry(registry)
918
- }).toThrow('[test-org] External resource ID \'duplicate-id\' conflicts with internal resource ID')
919
- })
920
- })
921
-
922
- describe('validateHumanCheckpoints', () => {
923
- it('validates requestedBy.agents exist', () => {
924
- const agent = createMockAgent('test-agent')
925
- const registry: OrganizationRegistry = {
926
- 'test-org': {
927
- agents: [agent],
928
- humanCheckpoints: [
929
- {
930
- resourceId: 'approval-queue',
931
- type: 'human',
932
- name: 'Approval Queue',
933
- description: 'Human approval checkpoint',
934
- version: '1.0.0',
935
- status: 'prod',
936
- requestedBy: { agents: ['test-agent'] }
937
- }
938
- ]
939
- }
940
- }
941
-
942
- expect(() => {
943
- new ResourceRegistry(registry)
944
- }).not.toThrow()
945
- })
946
-
947
- it('validates requestedBy.workflows exist', () => {
948
- const workflow = createMockWorkflow('test-workflow')
949
- const registry: OrganizationRegistry = {
950
- 'test-org': {
951
- workflows: [workflow],
952
- humanCheckpoints: [
953
- {
954
- resourceId: 'review-queue',
955
- type: 'human',
956
- name: 'Review Queue',
957
- description: 'Human review checkpoint',
958
- version: '1.0.0',
959
- status: 'prod',
960
- requestedBy: { workflows: ['test-workflow'] }
961
- }
962
- ]
963
- }
964
- }
965
-
966
- expect(() => {
967
- new ResourceRegistry(registry)
968
- }).not.toThrow()
969
- })
970
-
971
- it('validates routesTo.agents exist', () => {
972
- const agent = createMockAgent('fulfillment-agent')
973
- const registry: OrganizationRegistry = {
974
- 'test-org': {
975
- agents: [agent],
976
- humanCheckpoints: [
977
- {
978
- resourceId: 'approval-queue',
979
- type: 'human',
980
- name: 'Approval Queue',
981
- description: 'Human approval checkpoint',
982
- version: '1.0.0',
983
- status: 'prod',
984
- routesTo: { agents: ['fulfillment-agent'] }
985
- }
986
- ]
987
- }
988
- }
989
-
990
- expect(() => {
991
- new ResourceRegistry(registry)
992
- }).not.toThrow()
993
- })
994
-
995
- it('validates routesTo.workflows exist', () => {
996
- const workflow = createMockWorkflow('fulfillment-workflow')
997
- const registry: OrganizationRegistry = {
998
- 'test-org': {
999
- workflows: [workflow],
1000
- humanCheckpoints: [
1001
- {
1002
- resourceId: 'approval-queue',
1003
- type: 'human',
1004
- name: 'Approval Queue',
1005
- description: 'Human approval checkpoint',
1006
- version: '1.0.0',
1007
- status: 'prod',
1008
- routesTo: { workflows: ['fulfillment-workflow'] }
1009
- }
1010
- ]
1011
- }
1012
- }
1013
-
1014
- expect(() => {
1015
- new ResourceRegistry(registry)
1016
- }).not.toThrow()
1017
- })
1018
-
1019
- it('throws for requestedBy referencing non-existent agent', () => {
1020
- const registry: OrganizationRegistry = {
1021
- 'test-org': {
1022
- humanCheckpoints: [
1023
- {
1024
- resourceId: 'approval-queue',
1025
- type: 'human',
1026
- name: 'Approval Queue',
1027
- description: 'Human approval checkpoint',
1028
- version: '1.0.0',
1029
- status: 'prod',
1030
- requestedBy: { agents: ['non-existent-agent'] }
1031
- }
1032
- ]
1033
- }
1034
- }
1035
-
1036
- expect(() => {
1037
- new ResourceRegistry(registry)
1038
- }).toThrow('[test-org] Human checkpoint \'approval-queue\' requestedBy non-existent agent: non-existent-agent')
1039
- })
1040
-
1041
- it('throws for routesTo referencing non-existent workflow', () => {
1042
- const registry: OrganizationRegistry = {
1043
- 'test-org': {
1044
- humanCheckpoints: [
1045
- {
1046
- resourceId: 'approval-queue',
1047
- type: 'human',
1048
- name: 'Approval Queue',
1049
- description: 'Human approval checkpoint',
1050
- version: '1.0.0',
1051
- status: 'prod',
1052
- routesTo: { workflows: ['non-existent-workflow'] }
1053
- }
1054
- ]
1055
- }
1056
- }
1057
-
1058
- expect(() => {
1059
- new ResourceRegistry(registry)
1060
- }).toThrow('[test-org] Human checkpoint \'approval-queue\' routesTo non-existent workflow: non-existent-workflow')
1061
- })
1062
-
1063
- it('throws for human checkpoint with conflicting ID', () => {
1064
- const workflow = createMockWorkflow('conflicting-id')
1065
- const registry: OrganizationRegistry = {
1066
- 'test-org': {
1067
- workflows: [workflow],
1068
- humanCheckpoints: [
1069
- {
1070
- resourceId: 'conflicting-id',
1071
- type: 'human',
1072
- name: 'Conflicting Checkpoint',
1073
- description: 'Conflicts with workflow',
1074
- version: '1.0.0',
1075
- status: 'dev'
1076
- }
1077
- ]
1078
- }
1079
- }
1080
-
1081
- expect(() => {
1082
- new ResourceRegistry(registry)
1083
- }).toThrow('[test-org] Human checkpoint ID \'conflicting-id\' conflicts with internal resource ID')
1084
- })
1085
- })
1086
- })
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
+ import { z } from 'zod'
8
+ import { validateExecutionInterface, validateResourceGovernance, RegistryValidationError } from '../validation'
9
+ import { ResourceRegistry } from '../resource-registry'
10
+ import type { WorkflowDefinition } from '../../../execution/engine/workflow/types'
11
+ import type { AgentDefinition } from '../../../execution/engine/agent/core/types'
12
+ import type { OrganizationRegistry } from '../resource-registry'
13
+ import type { ResourceEntry } from '../../../organization-model/domains/resources'
14
+
15
+ describe('validateExecutionInterface', () => {
16
+ describe('required field validation', () => {
17
+ it('should pass when all required schema fields have form fields', () => {
18
+ const inputSchema = z.object({
19
+ name: z.string(),
20
+ email: z.string()
21
+ })
22
+
23
+ const executionInterface = {
24
+ form: {
25
+ fields: [
26
+ { name: 'name', required: true },
27
+ { name: 'email', required: true }
28
+ ]
29
+ }
30
+ }
31
+
32
+ expect(() => {
33
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
34
+ }).not.toThrow()
35
+ })
36
+
37
+ it('should throw when required schema field is missing from form', () => {
38
+ const inputSchema = z.object({
39
+ name: z.string(),
40
+ email: z.string()
41
+ })
42
+
43
+ const executionInterface = {
44
+ form: {
45
+ fields: [
46
+ { name: 'name', required: true }
47
+ // Missing 'email' field
48
+ ]
49
+ }
50
+ }
51
+
52
+ expect(() => {
53
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
54
+ }).toThrow(RegistryValidationError)
55
+ expect(() => {
56
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
57
+ }).toThrow('missing required field "email"')
58
+ })
59
+
60
+ it('should throw when form field is not marked required but schema field is required', () => {
61
+ const inputSchema = z.object({
62
+ name: z.string() // Required
63
+ })
64
+
65
+ const executionInterface = {
66
+ form: {
67
+ fields: [
68
+ { name: 'name', required: false } // Not marked required
69
+ ]
70
+ }
71
+ }
72
+
73
+ expect(() => {
74
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
75
+ }).toThrow(RegistryValidationError)
76
+ expect(() => {
77
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
78
+ }).toThrow('should be required')
79
+ })
80
+
81
+ it('should pass when optional schema field is missing from form', () => {
82
+ const inputSchema = z.object({
83
+ name: z.string(),
84
+ description: z.string().optional()
85
+ })
86
+
87
+ const executionInterface = {
88
+ form: {
89
+ fields: [
90
+ { name: 'name', required: true }
91
+ // 'description' is optional, so OK to omit
92
+ ]
93
+ }
94
+ }
95
+
96
+ expect(() => {
97
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
98
+ }).not.toThrow()
99
+ })
100
+
101
+ it('should pass when optional schema field has non-required form field', () => {
102
+ const inputSchema = z.object({
103
+ name: z.string(),
104
+ description: z.string().optional()
105
+ })
106
+
107
+ const executionInterface = {
108
+ form: {
109
+ fields: [
110
+ { name: 'name', required: true },
111
+ { name: 'description', required: false }
112
+ ]
113
+ }
114
+ }
115
+
116
+ expect(() => {
117
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
118
+ }).not.toThrow()
119
+ })
120
+ })
121
+
122
+ describe('field mapping validation', () => {
123
+ it('should pass with valid field mappings', () => {
124
+ const inputSchema = z.object({
125
+ taskName: z.string(),
126
+ priority: z.string()
127
+ })
128
+
129
+ const executionInterface = {
130
+ form: {
131
+ fields: [
132
+ { name: 'task_name', required: true },
133
+ { name: 'prio', required: true }
134
+ ],
135
+ fieldMappings: {
136
+ task_name: 'taskName',
137
+ prio: 'priority'
138
+ }
139
+ }
140
+ }
141
+
142
+ expect(() => {
143
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
144
+ }).not.toThrow()
145
+ })
146
+
147
+ it('should throw when field mapping points to non-existent schema field', () => {
148
+ const inputSchema = z.object({
149
+ name: z.string()
150
+ })
151
+
152
+ // Include a valid mapping for 'name' so we get past the required field check,
153
+ // then add an extra field that maps to a non-existent schema field
154
+ const executionInterface = {
155
+ form: {
156
+ fields: [
157
+ { name: 'userName', required: true },
158
+ { name: 'extraField', required: false }
159
+ ],
160
+ fieldMappings: {
161
+ userName: 'name', // Valid mapping
162
+ extraField: 'nonExistentField' // Invalid mapping
163
+ }
164
+ }
165
+ }
166
+
167
+ expect(() => {
168
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
169
+ }).toThrow(RegistryValidationError)
170
+ expect(() => {
171
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
172
+ }).toThrow('maps to non-existent schema field "nonExistentField"')
173
+ })
174
+
175
+ it('should throw when form field without mapping has no matching schema field', () => {
176
+ const inputSchema = z.object({
177
+ name: z.string()
178
+ })
179
+
180
+ const executionInterface = {
181
+ form: {
182
+ fields: [
183
+ { name: 'name', required: true },
184
+ { name: 'unknownField', required: false }
185
+ ]
186
+ }
187
+ }
188
+
189
+ expect(() => {
190
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
191
+ }).toThrow(RegistryValidationError)
192
+ expect(() => {
193
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
194
+ }).toThrow('maps to non-existent schema field "unknownField"')
195
+ })
196
+
197
+ it('should throw with flatten suggestion when form field uses nested notation', () => {
198
+ const inputSchema = z.object({
199
+ name: z.string()
200
+ })
201
+
202
+ const executionInterface = {
203
+ form: {
204
+ fields: [
205
+ { name: 'name', required: true },
206
+ { name: 'criteria.targetTitles', required: true }
207
+ ]
208
+ }
209
+ }
210
+
211
+ expect(() => {
212
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
213
+ }).toThrow(RegistryValidationError)
214
+ expect(() => {
215
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
216
+ }).toThrow('uses nested notation')
217
+ expect(() => {
218
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
219
+ }).toThrow('Flatten the inputSchema')
220
+ })
221
+ })
222
+
223
+ describe('default values (effectively optional)', () => {
224
+ it('should treat fields with defaults as optional', () => {
225
+ const inputSchema = z.object({
226
+ name: z.string(),
227
+ priority: z.string().default('medium')
228
+ })
229
+
230
+ const executionInterface = {
231
+ form: {
232
+ fields: [
233
+ { name: 'name', required: true }
234
+ // 'priority' has default, so OK to omit
235
+ ]
236
+ }
237
+ }
238
+
239
+ expect(() => {
240
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
241
+ }).not.toThrow()
242
+ })
243
+ })
244
+
245
+ describe('enum fields', () => {
246
+ it('should validate enum fields correctly', () => {
247
+ const inputSchema = z.object({
248
+ priority: z.enum(['low', 'medium', 'high'])
249
+ })
250
+
251
+ const executionInterface = {
252
+ form: {
253
+ fields: [{ name: 'priority', required: true }]
254
+ }
255
+ }
256
+
257
+ expect(() => {
258
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
259
+ }).not.toThrow()
260
+ })
261
+ })
262
+
263
+ describe('empty schema', () => {
264
+ it('should pass with empty schema and no form fields', () => {
265
+ const inputSchema = z.object({})
266
+
267
+ const executionInterface = {
268
+ form: {
269
+ fields: []
270
+ }
271
+ }
272
+
273
+ expect(() => {
274
+ validateExecutionInterface('test-org', 'test-resource', executionInterface, inputSchema)
275
+ }).not.toThrow()
276
+ })
277
+ })
278
+ })
279
+
280
+ describe('validateResourceGovernance', () => {
281
+ afterEach(() => {
282
+ vi.unstubAllEnvs()
283
+ })
284
+
285
+ const systemA = {
286
+ id: 'sys.lead-gen',
287
+ title: 'Lead Gen',
288
+ description: 'Lead generation system.',
289
+ kind: 'operational' as const,
290
+ governedByKnowledge: [],
291
+ drivesGoals: [],
292
+ status: 'active' as const
293
+ }
294
+
295
+ const systemB = {
296
+ id: 'sys.crm',
297
+ title: 'CRM',
298
+ description: 'CRM system.',
299
+ kind: 'operational' as const,
300
+ governedByKnowledge: [],
301
+ drivesGoals: [],
302
+ status: 'active' as const
303
+ }
304
+
305
+ const workflowResource: ResourceEntry = {
306
+ id: 'lead-import',
307
+ kind: 'workflow',
308
+ systemId: 'sys.lead-gen',
309
+ status: 'active'
310
+ }
311
+
312
+ const agentResource: ResourceEntry = {
313
+ id: 'lead-import',
314
+ kind: 'agent',
315
+ systemId: 'sys.lead-gen',
316
+ status: 'active',
317
+ agentKind: 'specialist',
318
+ sessionCapable: false
319
+ }
320
+
321
+ const createGovernedWorkflow = (
322
+ resource = workflowResource,
323
+ overrides: Partial<WorkflowDefinition['config']> = {}
324
+ ): WorkflowDefinition => ({
325
+ config: {
326
+ resourceId: resource.id,
327
+ name: `Workflow ${resource.id}`,
328
+ description: `Test workflow ${resource.id}`,
329
+ version: '1.0.0',
330
+ type: 'workflow',
331
+ status: 'dev',
332
+ resource: resource as Extract<ResourceEntry, { kind: 'workflow' }>,
333
+ ...overrides
334
+ },
335
+ contract: {
336
+ inputSchema: z.object({ data: z.string() }),
337
+ outputSchema: z.object({ result: z.boolean() })
338
+ },
339
+ steps: {},
340
+ entryPoint: 'start'
341
+ })
342
+
343
+ const createRawWorkflow = (resourceId = workflowResource.id): WorkflowDefinition => ({
344
+ config: {
345
+ resourceId,
346
+ name: `Workflow ${resourceId}`,
347
+ description: `Test workflow ${resourceId}`,
348
+ version: '1.0.0',
349
+ type: 'workflow',
350
+ status: 'dev'
351
+ },
352
+ contract: {
353
+ inputSchema: z.object({ data: z.string() }),
354
+ outputSchema: z.object({ result: z.boolean() })
355
+ },
356
+ steps: {},
357
+ entryPoint: 'start'
358
+ })
359
+
360
+ const createModel = (resources: ResourceEntry[] = [workflowResource], systems = [systemA]) => ({
361
+ systems: { systems },
362
+ resources: { entries: resources }
363
+ })
364
+
365
+ it('passes when code resources are descriptor-backed and match active OM Resources and Systems', () => {
366
+ const result = validateResourceGovernance(
367
+ 'test-org',
368
+ {
369
+ version: '1.0.0',
370
+ workflows: [createGovernedWorkflow()]
371
+ },
372
+ createModel(),
373
+ { mode: 'strict' }
374
+ )
375
+
376
+ expect(result.valid).toBe(true)
377
+ expect(result.issues).toEqual([])
378
+ })
379
+
380
+ it('throws in strict mode when an active OM resource has no code-side resource', () => {
381
+ expect(() =>
382
+ validateResourceGovernance(
383
+ 'test-org',
384
+ {
385
+ version: '1.0.0',
386
+ workflows: []
387
+ },
388
+ createModel(),
389
+ { mode: 'strict' }
390
+ )
391
+ ).toThrow("OM resource 'lead-import' has no matching code-side resource")
392
+ })
393
+
394
+ it('defaults to strict mode after cutover', () => {
395
+ expect(() =>
396
+ validateResourceGovernance(
397
+ 'test-org',
398
+ {
399
+ version: '1.0.0',
400
+ workflows: []
401
+ },
402
+ createModel()
403
+ )
404
+ ).toThrow("OM resource 'lead-import' has no matching code-side resource")
405
+ })
406
+
407
+ it('downgrades strict failures to warnings in warn-only mode', () => {
408
+ const warnings: string[] = []
409
+
410
+ const result = validateResourceGovernance(
411
+ 'test-org',
412
+ {
413
+ version: '1.0.0',
414
+ workflows: []
415
+ },
416
+ createModel(),
417
+ {
418
+ mode: 'warn-only',
419
+ onWarning: (issue) => warnings.push(issue.message)
420
+ }
421
+ )
422
+
423
+ expect(result.valid).toBe(false)
424
+ expect(result.issues.map((issue) => issue.type)).toContain('missing-code-resource')
425
+ expect(warnings).toEqual(["[test-org] OM resource 'lead-import' has no matching code-side resource."])
426
+ })
427
+
428
+ it('honors ELEVASIS_RESOURCE_VALIDATOR=warn-only as the permanent escape hatch', () => {
429
+ vi.stubEnv('ELEVASIS_RESOURCE_VALIDATOR', 'warn-only')
430
+ const warnings: string[] = []
431
+
432
+ const result = validateResourceGovernance(
433
+ 'test-org',
434
+ {
435
+ version: '1.0.0',
436
+ workflows: []
437
+ },
438
+ createModel(),
439
+ {
440
+ onWarning: (issue) => warnings.push(issue.message)
441
+ }
442
+ )
443
+
444
+ expect(result.valid).toBe(false)
445
+ expect(result.mode).toBe('warn-only')
446
+ expect(result.issues.map((issue) => issue.type)).toContain('missing-code-resource')
447
+ expect(warnings).toEqual(["[test-org] OM resource 'lead-import' has no matching code-side resource."])
448
+ })
449
+
450
+ it('reports a code-side resource with no active OM resource', () => {
451
+ const extraResource: ResourceEntry = {
452
+ ...workflowResource,
453
+ id: 'extra-workflow'
454
+ }
455
+
456
+ expect(() =>
457
+ validateResourceGovernance(
458
+ 'test-org',
459
+ {
460
+ version: '1.0.0',
461
+ workflows: [createGovernedWorkflow(extraResource)]
462
+ },
463
+ createModel([], [systemA]),
464
+ { mode: 'strict' }
465
+ )
466
+ ).toThrow("Code-side resource 'extra-workflow' has no active OM Resource descriptor")
467
+ })
468
+
469
+ it('reports runtime/OM type mismatches', () => {
470
+ expect(() =>
471
+ validateResourceGovernance(
472
+ 'test-org',
473
+ {
474
+ version: '1.0.0',
475
+ workflows: [createRawWorkflow('lead-import')]
476
+ },
477
+ createModel([agentResource]),
478
+ { mode: 'strict' }
479
+ )
480
+ ).toThrow("Resource 'lead-import' type mismatch: code has 'workflow', OM has 'agent'")
481
+ })
482
+
483
+ it('reports descriptor/OM system mismatches', () => {
484
+ const codeDescriptor: ResourceEntry = {
485
+ ...workflowResource,
486
+ systemId: 'sys.crm'
487
+ }
488
+
489
+ expect(() =>
490
+ validateResourceGovernance(
491
+ 'test-org',
492
+ {
493
+ version: '1.0.0',
494
+ workflows: [createGovernedWorkflow(codeDescriptor)]
495
+ },
496
+ createModel([workflowResource], [systemA, systemB]),
497
+ { mode: 'strict' }
498
+ )
499
+ ).toThrow("Resource 'lead-import' system mismatch: code descriptor has 'sys.crm', OM has 'sys.lead-gen'")
500
+ })
501
+
502
+ it('reports active OM resources that reference missing Systems', () => {
503
+ const resourceWithMissingSystem: ResourceEntry = {
504
+ ...workflowResource,
505
+ systemId: 'sys.missing'
506
+ }
507
+
508
+ expect(() =>
509
+ validateResourceGovernance(
510
+ 'test-org',
511
+ {
512
+ version: '1.0.0',
513
+ workflows: [createGovernedWorkflow(resourceWithMissingSystem)]
514
+ },
515
+ createModel([resourceWithMissingSystem], [systemA]),
516
+ { mode: 'strict' }
517
+ )
518
+ ).toThrow("OM resource 'lead-import' references missing System 'sys.missing'")
519
+ })
520
+
521
+ it('reports raw runtime resourceId authoring when a descriptor is required', () => {
522
+ expect(() =>
523
+ validateResourceGovernance(
524
+ 'test-org',
525
+ {
526
+ version: '1.0.0',
527
+ workflows: [createRawWorkflow()]
528
+ },
529
+ createModel(),
530
+ { mode: 'strict' }
531
+ )
532
+ ).toThrow("Code-side resource 'lead-import' authors raw resourceId/type values")
533
+ })
534
+ })
535
+
536
+ describe('ResourceRegistry - ExecutionInterface validation integration', () => {
537
+ it('should throw on registry construction when interface misses required field', () => {
538
+ const workflow: WorkflowDefinition = {
539
+ config: {
540
+ resourceId: 'test-workflow',
541
+ name: 'Test Workflow',
542
+ description: 'Test',
543
+ version: '1.0.0',
544
+ type: 'workflow',
545
+ status: 'dev'
546
+ },
547
+ contract: {
548
+ inputSchema: z.object({
549
+ taskName: z.string(),
550
+ priority: z.string()
551
+ }),
552
+ outputSchema: z.object({})
553
+ },
554
+ steps: {},
555
+ entryPoint: 'start',
556
+ interface: {
557
+ form: {
558
+ fields: [
559
+ { name: 'taskName', label: 'Task Name', type: 'text', required: true }
560
+ // Missing 'priority' field
561
+ ]
562
+ }
563
+ }
564
+ }
565
+
566
+ const registry: OrganizationRegistry = {
567
+ 'test-org': {
568
+ workflows: [workflow]
569
+ }
570
+ }
571
+
572
+ expect(() => {
573
+ new ResourceRegistry(registry)
574
+ }).toThrow('missing required field "priority"')
575
+ })
576
+
577
+ it('should throw when agent interface misses required field', () => {
578
+ const agent: AgentDefinition = {
579
+ config: {
580
+ resourceId: 'test-agent',
581
+ name: 'Test Agent',
582
+ description: 'Test',
583
+ version: '1.0.0',
584
+ type: 'agent',
585
+ status: 'dev',
586
+ systemPrompt: 'You are a test agent'
587
+ },
588
+ contract: {
589
+ inputSchema: z.object({
590
+ task: z.string(),
591
+ context: z.string()
592
+ }),
593
+ outputSchema: z.object({})
594
+ },
595
+ tools: [],
596
+ modelConfig: {
597
+ provider: 'mock',
598
+ model: 'mock',
599
+ apiKey: 'test'
600
+ },
601
+ interface: {
602
+ form: {
603
+ fields: [
604
+ { name: 'task', label: 'Task', type: 'textarea', required: true }
605
+ // Missing 'context' field
606
+ ]
607
+ }
608
+ }
609
+ }
610
+
611
+ const registry: OrganizationRegistry = {
612
+ 'test-org': {
613
+ agents: [agent]
614
+ }
615
+ }
616
+
617
+ expect(() => {
618
+ new ResourceRegistry(registry)
619
+ }).toThrow('missing required field "context"')
620
+ })
621
+
622
+ it('should pass when interface correctly matches schema', () => {
623
+ const workflow: WorkflowDefinition = {
624
+ config: {
625
+ resourceId: 'valid-workflow',
626
+ name: 'Valid Workflow',
627
+ description: 'Test',
628
+ version: '1.0.0',
629
+ type: 'workflow',
630
+ status: 'dev'
631
+ },
632
+ contract: {
633
+ inputSchema: z.object({
634
+ taskName: z.string(),
635
+ priority: z.enum(['low', 'medium', 'high']),
636
+ description: z.string().optional()
637
+ }),
638
+ outputSchema: z.object({})
639
+ },
640
+ steps: {},
641
+ entryPoint: 'start',
642
+ interface: {
643
+ form: {
644
+ title: 'Run Workflow',
645
+ fields: [
646
+ { name: 'taskName', label: 'Task Name', type: 'text', required: true },
647
+ { name: 'priority', label: 'Priority', type: 'select', required: true },
648
+ { name: 'description', label: 'Description', type: 'textarea', required: false }
649
+ ]
650
+ }
651
+ }
652
+ }
653
+
654
+ const registry: OrganizationRegistry = {
655
+ 'test-org': {
656
+ workflows: [workflow]
657
+ }
658
+ }
659
+
660
+ expect(() => {
661
+ new ResourceRegistry(registry)
662
+ }).not.toThrow()
663
+ })
664
+
665
+ it('should pass when workflow has no interface (optional)', () => {
666
+ const workflow: WorkflowDefinition = {
667
+ config: {
668
+ resourceId: 'no-interface-workflow',
669
+ name: 'No Interface Workflow',
670
+ description: 'Test',
671
+ version: '1.0.0',
672
+ type: 'workflow',
673
+ status: 'dev'
674
+ },
675
+ contract: {
676
+ inputSchema: z.object({ data: z.string() }),
677
+ outputSchema: z.object({})
678
+ },
679
+ steps: {},
680
+ entryPoint: 'start'
681
+ // No interface - should pass
682
+ }
683
+
684
+ const registry: OrganizationRegistry = {
685
+ 'test-org': {
686
+ workflows: [workflow]
687
+ }
688
+ }
689
+
690
+ expect(() => {
691
+ new ResourceRegistry(registry)
692
+ }).not.toThrow()
693
+ })
694
+ })
695
+
696
+ describe('Relationship Validation', () => {
697
+ // Helper to create minimal mock resources
698
+ const createMockWorkflow = (resourceId: string): WorkflowDefinition => ({
699
+ config: {
700
+ resourceId,
701
+ name: `Workflow ${resourceId}`,
702
+ description: `Test workflow ${resourceId}`,
703
+ version: '1.0.0',
704
+ type: 'workflow',
705
+ status: 'dev'
706
+ },
707
+ contract: {
708
+ inputSchema: z.object({ data: z.string() }),
709
+ outputSchema: z.object({ result: z.boolean() })
710
+ },
711
+ steps: {},
712
+ entryPoint: 'start'
713
+ })
714
+
715
+ const createMockAgent = (resourceId: string): AgentDefinition => ({
716
+ config: {
717
+ resourceId,
718
+ name: `Agent ${resourceId}`,
719
+ description: `Test agent ${resourceId}`,
720
+ version: '1.0.0',
721
+ type: 'agent',
722
+ status: 'dev',
723
+ systemPrompt: 'You are a test agent'
724
+ },
725
+ contract: {
726
+ inputSchema: z.object({ query: z.string() }),
727
+ outputSchema: z.object({ response: z.string() })
728
+ },
729
+ tools: [],
730
+ modelConfig: {
731
+ provider: 'mock',
732
+ model: 'mock',
733
+ apiKey: 'test-key'
734
+ }
735
+ })
736
+
737
+ describe('validateTriggers (new field names)', () => {
738
+ it('validates trigger.triggers.agents exist (using resourceId)', () => {
739
+ const agent = createMockAgent('test-agent')
740
+ const registry: OrganizationRegistry = {
741
+ 'test-org': {
742
+ agents: [agent],
743
+ triggers: [
744
+ {
745
+ resourceId: 'test-trigger',
746
+ type: 'trigger',
747
+ triggerType: 'webhook',
748
+ name: 'Test Trigger',
749
+ description: 'Test trigger',
750
+ version: '1.0.0',
751
+ status: 'dev',
752
+ triggers: { agents: ['test-agent'] }
753
+ }
754
+ ]
755
+ }
756
+ }
757
+
758
+ expect(() => {
759
+ new ResourceRegistry(registry)
760
+ }).not.toThrow()
761
+ })
762
+
763
+ it('validates trigger.triggers.workflows exist (using resourceId)', () => {
764
+ const workflow = createMockWorkflow('test-workflow')
765
+ const registry: OrganizationRegistry = {
766
+ 'test-org': {
767
+ workflows: [workflow],
768
+ triggers: [
769
+ {
770
+ resourceId: 'test-trigger',
771
+ type: 'trigger',
772
+ triggerType: 'schedule',
773
+ name: 'Test Trigger',
774
+ description: 'Test trigger',
775
+ version: '1.0.0',
776
+ status: 'dev',
777
+ triggers: { workflows: ['test-workflow'] }
778
+ }
779
+ ]
780
+ }
781
+ }
782
+
783
+ expect(() => {
784
+ new ResourceRegistry(registry)
785
+ }).not.toThrow()
786
+ })
787
+
788
+ it('throws for trigger triggering non-existent agent (via relationships)', () => {
789
+ // Note: Trigger relationships are now declared via ResourceRelationships
790
+ const registry: OrganizationRegistry = {
791
+ 'test-org': {
792
+ triggers: [
793
+ {
794
+ resourceId: 'test-trigger',
795
+ type: 'trigger',
796
+ triggerType: 'webhook',
797
+ name: 'Test Trigger',
798
+ description: 'Test trigger',
799
+ version: '1.0.0',
800
+ status: 'dev'
801
+ }
802
+ ],
803
+ relationships: {
804
+ 'test-trigger': {
805
+ triggers: { agents: ['non-existent-agent'] }
806
+ }
807
+ }
808
+ }
809
+ }
810
+
811
+ expect(() => {
812
+ new ResourceRegistry(registry)
813
+ }).toThrow('non-existent-agent')
814
+ })
815
+
816
+ it('throws for trigger triggering non-existent workflow (via relationships)', () => {
817
+ // Note: Trigger relationships are now declared via ResourceRelationships
818
+ const registry: OrganizationRegistry = {
819
+ 'test-org': {
820
+ triggers: [
821
+ {
822
+ resourceId: 'test-trigger',
823
+ type: 'trigger',
824
+ triggerType: 'event',
825
+ name: 'Test Trigger',
826
+ description: 'Test trigger',
827
+ version: '1.0.0',
828
+ status: 'dev'
829
+ }
830
+ ],
831
+ relationships: {
832
+ 'test-trigger': {
833
+ triggers: { workflows: ['non-existent-workflow'] }
834
+ }
835
+ }
836
+ }
837
+ }
838
+
839
+ expect(() => {
840
+ new ResourceRegistry(registry)
841
+ }).toThrow('non-existent-workflow')
842
+ })
843
+
844
+ it('allows trigger with no relationships (triggers are metadata only)', () => {
845
+ // Note: Triggers without relationships are now valid - they're just metadata
846
+ // The trigger's targets are defined in ResourceRelationships, not on the trigger itself
847
+ const registry: OrganizationRegistry = {
848
+ 'test-org': {
849
+ triggers: [
850
+ {
851
+ resourceId: 'test-trigger',
852
+ type: 'trigger',
853
+ triggerType: 'webhook',
854
+ name: 'Test Trigger',
855
+ description: 'Test trigger',
856
+ version: '1.0.0',
857
+ status: 'dev'
858
+ }
859
+ ]
860
+ // No relationships - trigger is just metadata
861
+ }
862
+ }
863
+
864
+ expect(() => {
865
+ new ResourceRegistry(registry)
866
+ }).not.toThrow()
867
+ })
868
+
869
+ it('allows trigger with empty relationships object', () => {
870
+ // Note: Empty relationships are valid - triggers without targets are allowed
871
+ const registry: OrganizationRegistry = {
872
+ 'test-org': {
873
+ triggers: [
874
+ {
875
+ resourceId: 'test-trigger',
876
+ type: 'trigger',
877
+ triggerType: 'manual',
878
+ name: 'Test Trigger',
879
+ description: 'Test trigger',
880
+ version: '1.0.0',
881
+ status: 'dev'
882
+ }
883
+ ],
884
+ relationships: {}
885
+ }
886
+ }
887
+
888
+ expect(() => {
889
+ new ResourceRegistry(registry)
890
+ }).not.toThrow()
891
+ })
892
+ })
893
+
894
+ describe('validateResourceRelationships', () => {
895
+ it('validates resource.triggers.agents exist', () => {
896
+ const agent1 = createMockAgent('agent-1')
897
+ const agent2 = createMockAgent('agent-2')
898
+ const registry: OrganizationRegistry = {
899
+ 'test-org': {
900
+ agents: [agent1, agent2],
901
+ relationships: {
902
+ 'agent-1': {
903
+ triggers: { agents: ['agent-2'] }
904
+ }
905
+ }
906
+ }
907
+ }
908
+
909
+ expect(() => {
910
+ new ResourceRegistry(registry)
911
+ }).not.toThrow()
912
+ })
913
+
914
+ it('validates resource.triggers.workflows exist', () => {
915
+ const agent = createMockAgent('test-agent')
916
+ const workflow = createMockWorkflow('test-workflow')
917
+ const registry: OrganizationRegistry = {
918
+ 'test-org': {
919
+ agents: [agent],
920
+ workflows: [workflow],
921
+ relationships: {
922
+ 'test-agent': {
923
+ triggers: { workflows: ['test-workflow'] }
924
+ }
925
+ }
926
+ }
927
+ }
928
+
929
+ expect(() => {
930
+ new ResourceRegistry(registry)
931
+ }).not.toThrow()
932
+ })
933
+
934
+ it('validates resource.uses.integrations exist', () => {
935
+ const workflow = createMockWorkflow('test-workflow')
936
+ const registry: OrganizationRegistry = {
937
+ 'test-org': {
938
+ workflows: [workflow],
939
+ integrations: [
940
+ {
941
+ resourceId: 'integration-shopify',
942
+ type: 'integration',
943
+ provider: 'custom',
944
+ credentialName: 'shopify-prod',
945
+ name: 'Shopify Integration',
946
+ description: 'E-commerce integration',
947
+ version: '1.0.0',
948
+ status: 'prod'
949
+ }
950
+ ],
951
+ relationships: {
952
+ 'test-workflow': {
953
+ uses: { integrations: ['integration-shopify'] }
954
+ }
955
+ }
956
+ }
957
+ }
958
+
959
+ expect(() => {
960
+ new ResourceRegistry(registry)
961
+ }).not.toThrow()
962
+ })
963
+
964
+ it('throws for relationship declared for non-existent resource', () => {
965
+ const registry: OrganizationRegistry = {
966
+ 'test-org': {
967
+ relationships: {
968
+ 'non-existent-resource': {
969
+ triggers: { agents: ['some-agent'] }
970
+ }
971
+ }
972
+ }
973
+ }
974
+
975
+ expect(() => {
976
+ new ResourceRegistry(registry)
977
+ }).toThrow('[test-org] Relationship declared for non-existent resource: non-existent-resource')
978
+ })
979
+
980
+ it('throws for relationship triggering non-existent agent', () => {
981
+ const workflow = createMockWorkflow('test-workflow')
982
+ const registry: OrganizationRegistry = {
983
+ 'test-org': {
984
+ workflows: [workflow],
985
+ relationships: {
986
+ 'test-workflow': {
987
+ triggers: { agents: ['non-existent-agent'] }
988
+ }
989
+ }
990
+ }
991
+ }
992
+
993
+ expect(() => {
994
+ new ResourceRegistry(registry)
995
+ }).toThrow("[test-org] Resource 'test-workflow' triggers non-existent agent: non-existent-agent")
996
+ })
997
+
998
+ it('throws for relationship using non-existent integration', () => {
999
+ const agent = createMockAgent('test-agent')
1000
+ const registry: OrganizationRegistry = {
1001
+ 'test-org': {
1002
+ agents: [agent],
1003
+ relationships: {
1004
+ 'test-agent': {
1005
+ uses: { integrations: ['non-existent-integration'] }
1006
+ }
1007
+ }
1008
+ }
1009
+ }
1010
+
1011
+ expect(() => {
1012
+ new ResourceRegistry(registry)
1013
+ }).toThrow("[test-org] Resource 'test-agent' uses non-existent integration: non-existent-integration")
1014
+ })
1015
+ })
1016
+
1017
+ describe('validateExternalResources (forward-only)', () => {
1018
+ it('validates external.triggers.agents exist', () => {
1019
+ const agent = createMockAgent('test-agent')
1020
+ const registry: OrganizationRegistry = {
1021
+ 'test-org': {
1022
+ agents: [agent],
1023
+ externalResources: [
1024
+ {
1025
+ resourceId: 'external-n8n-workflow',
1026
+ type: 'external',
1027
+ platform: 'n8n',
1028
+ name: 'N8N Workflow',
1029
+ description: 'External automation',
1030
+ version: '1.0.0',
1031
+ status: 'prod',
1032
+ triggers: { agents: ['test-agent'] }
1033
+ }
1034
+ ]
1035
+ }
1036
+ }
1037
+
1038
+ expect(() => {
1039
+ new ResourceRegistry(registry)
1040
+ }).not.toThrow()
1041
+ })
1042
+
1043
+ it('validates external.triggers.workflows exist', () => {
1044
+ const workflow = createMockWorkflow('test-workflow')
1045
+ const registry: OrganizationRegistry = {
1046
+ 'test-org': {
1047
+ workflows: [workflow],
1048
+ externalResources: [
1049
+ {
1050
+ resourceId: 'external-zapier-zap',
1051
+ type: 'external',
1052
+ platform: 'zapier',
1053
+ name: 'Zapier Zap',
1054
+ description: 'External automation',
1055
+ version: '1.0.0',
1056
+ status: 'prod',
1057
+ triggers: { workflows: ['test-workflow'] }
1058
+ }
1059
+ ]
1060
+ }
1061
+ }
1062
+
1063
+ expect(() => {
1064
+ new ResourceRegistry(registry)
1065
+ }).not.toThrow()
1066
+ })
1067
+
1068
+ it('validates external.uses.integrations exist', () => {
1069
+ const registry: OrganizationRegistry = {
1070
+ 'test-org': {
1071
+ integrations: [
1072
+ {
1073
+ resourceId: 'integration-slack',
1074
+ type: 'integration',
1075
+ provider: 'custom',
1076
+ credentialName: 'slack-prod',
1077
+ name: 'Slack Integration',
1078
+ description: 'Chat integration',
1079
+ version: '1.0.0',
1080
+ status: 'prod'
1081
+ }
1082
+ ],
1083
+ externalResources: [
1084
+ {
1085
+ resourceId: 'external-make-scenario',
1086
+ type: 'external',
1087
+ platform: 'make',
1088
+ name: 'Make Scenario',
1089
+ description: 'External automation',
1090
+ version: '1.0.0',
1091
+ status: 'prod',
1092
+ uses: { integrations: ['integration-slack'] }
1093
+ }
1094
+ ]
1095
+ }
1096
+ }
1097
+
1098
+ expect(() => {
1099
+ new ResourceRegistry(registry)
1100
+ }).not.toThrow()
1101
+ })
1102
+
1103
+ it('validates external resourceId does not conflict with internal resources', () => {
1104
+ const workflow = createMockWorkflow('conflicting-id')
1105
+ const registry: OrganizationRegistry = {
1106
+ 'test-org': {
1107
+ workflows: [workflow],
1108
+ externalResources: [
1109
+ {
1110
+ resourceId: 'conflicting-id',
1111
+ type: 'external',
1112
+ platform: 'other',
1113
+ name: 'Conflicting External',
1114
+ description: 'Should conflict',
1115
+ version: '1.0.0',
1116
+ status: 'dev'
1117
+ }
1118
+ ]
1119
+ }
1120
+ }
1121
+
1122
+ expect(() => {
1123
+ new ResourceRegistry(registry)
1124
+ }).toThrow("[test-org] External resource ID 'conflicting-id' conflicts with internal resource ID")
1125
+ })
1126
+
1127
+ it('does NOT validate triggeredBy (field removed)', () => {
1128
+ // This test ensures we don't validate triggeredBy field (it's been removed)
1129
+ const workflow = createMockWorkflow('test-workflow')
1130
+ const registry: OrganizationRegistry = {
1131
+ 'test-org': {
1132
+ workflows: [workflow],
1133
+ externalResources: [
1134
+ {
1135
+ resourceId: 'external-resource',
1136
+ type: 'external',
1137
+ platform: 'n8n',
1138
+ name: 'External Resource',
1139
+ description: 'Has no triggeredBy field',
1140
+ version: '1.0.0',
1141
+ status: 'prod',
1142
+ triggers: { workflows: ['test-workflow'] }
1143
+ // No triggeredBy field - this is correct and should pass
1144
+ }
1145
+ ]
1146
+ }
1147
+ }
1148
+
1149
+ expect(() => {
1150
+ new ResourceRegistry(registry)
1151
+ }).not.toThrow()
1152
+ })
1153
+
1154
+ it('throws for external resource with conflicting ID', () => {
1155
+ const agent = createMockAgent('duplicate-id')
1156
+ const registry: OrganizationRegistry = {
1157
+ 'test-org': {
1158
+ agents: [agent],
1159
+ externalResources: [
1160
+ {
1161
+ resourceId: 'duplicate-id',
1162
+ type: 'external',
1163
+ platform: 'n8n',
1164
+ name: 'Duplicate ID',
1165
+ description: 'Conflicts with agent',
1166
+ version: '1.0.0',
1167
+ status: 'dev'
1168
+ }
1169
+ ]
1170
+ }
1171
+ }
1172
+
1173
+ expect(() => {
1174
+ new ResourceRegistry(registry)
1175
+ }).toThrow("[test-org] External resource ID 'duplicate-id' conflicts with internal resource ID")
1176
+ })
1177
+ })
1178
+
1179
+ describe('validateHumanCheckpoints', () => {
1180
+ it('validates requestedBy.agents exist', () => {
1181
+ const agent = createMockAgent('test-agent')
1182
+ const registry: OrganizationRegistry = {
1183
+ 'test-org': {
1184
+ agents: [agent],
1185
+ humanCheckpoints: [
1186
+ {
1187
+ resourceId: 'approval-queue',
1188
+ type: 'human',
1189
+ name: 'Approval Queue',
1190
+ description: 'Human approval checkpoint',
1191
+ version: '1.0.0',
1192
+ status: 'prod',
1193
+ requestedBy: { agents: ['test-agent'] }
1194
+ }
1195
+ ]
1196
+ }
1197
+ }
1198
+
1199
+ expect(() => {
1200
+ new ResourceRegistry(registry)
1201
+ }).not.toThrow()
1202
+ })
1203
+
1204
+ it('validates requestedBy.workflows exist', () => {
1205
+ const workflow = createMockWorkflow('test-workflow')
1206
+ const registry: OrganizationRegistry = {
1207
+ 'test-org': {
1208
+ workflows: [workflow],
1209
+ humanCheckpoints: [
1210
+ {
1211
+ resourceId: 'review-queue',
1212
+ type: 'human',
1213
+ name: 'Review Queue',
1214
+ description: 'Human review checkpoint',
1215
+ version: '1.0.0',
1216
+ status: 'prod',
1217
+ requestedBy: { workflows: ['test-workflow'] }
1218
+ }
1219
+ ]
1220
+ }
1221
+ }
1222
+
1223
+ expect(() => {
1224
+ new ResourceRegistry(registry)
1225
+ }).not.toThrow()
1226
+ })
1227
+
1228
+ it('validates routesTo.agents exist', () => {
1229
+ const agent = createMockAgent('fulfillment-agent')
1230
+ const registry: OrganizationRegistry = {
1231
+ 'test-org': {
1232
+ agents: [agent],
1233
+ humanCheckpoints: [
1234
+ {
1235
+ resourceId: 'approval-queue',
1236
+ type: 'human',
1237
+ name: 'Approval Queue',
1238
+ description: 'Human approval checkpoint',
1239
+ version: '1.0.0',
1240
+ status: 'prod',
1241
+ routesTo: { agents: ['fulfillment-agent'] }
1242
+ }
1243
+ ]
1244
+ }
1245
+ }
1246
+
1247
+ expect(() => {
1248
+ new ResourceRegistry(registry)
1249
+ }).not.toThrow()
1250
+ })
1251
+
1252
+ it('validates routesTo.workflows exist', () => {
1253
+ const workflow = createMockWorkflow('fulfillment-workflow')
1254
+ const registry: OrganizationRegistry = {
1255
+ 'test-org': {
1256
+ workflows: [workflow],
1257
+ humanCheckpoints: [
1258
+ {
1259
+ resourceId: 'approval-queue',
1260
+ type: 'human',
1261
+ name: 'Approval Queue',
1262
+ description: 'Human approval checkpoint',
1263
+ version: '1.0.0',
1264
+ status: 'prod',
1265
+ routesTo: { workflows: ['fulfillment-workflow'] }
1266
+ }
1267
+ ]
1268
+ }
1269
+ }
1270
+
1271
+ expect(() => {
1272
+ new ResourceRegistry(registry)
1273
+ }).not.toThrow()
1274
+ })
1275
+
1276
+ it('throws for requestedBy referencing non-existent agent', () => {
1277
+ const registry: OrganizationRegistry = {
1278
+ 'test-org': {
1279
+ humanCheckpoints: [
1280
+ {
1281
+ resourceId: 'approval-queue',
1282
+ type: 'human',
1283
+ name: 'Approval Queue',
1284
+ description: 'Human approval checkpoint',
1285
+ version: '1.0.0',
1286
+ status: 'prod',
1287
+ requestedBy: { agents: ['non-existent-agent'] }
1288
+ }
1289
+ ]
1290
+ }
1291
+ }
1292
+
1293
+ expect(() => {
1294
+ new ResourceRegistry(registry)
1295
+ }).toThrow("[test-org] Human checkpoint 'approval-queue' requestedBy non-existent agent: non-existent-agent")
1296
+ })
1297
+
1298
+ it('throws for routesTo referencing non-existent workflow', () => {
1299
+ const registry: OrganizationRegistry = {
1300
+ 'test-org': {
1301
+ humanCheckpoints: [
1302
+ {
1303
+ resourceId: 'approval-queue',
1304
+ type: 'human',
1305
+ name: 'Approval Queue',
1306
+ description: 'Human approval checkpoint',
1307
+ version: '1.0.0',
1308
+ status: 'prod',
1309
+ routesTo: { workflows: ['non-existent-workflow'] }
1310
+ }
1311
+ ]
1312
+ }
1313
+ }
1314
+
1315
+ expect(() => {
1316
+ new ResourceRegistry(registry)
1317
+ }).toThrow("[test-org] Human checkpoint 'approval-queue' routesTo non-existent workflow: non-existent-workflow")
1318
+ })
1319
+
1320
+ it('throws for human checkpoint with conflicting ID', () => {
1321
+ const workflow = createMockWorkflow('conflicting-id')
1322
+ const registry: OrganizationRegistry = {
1323
+ 'test-org': {
1324
+ workflows: [workflow],
1325
+ humanCheckpoints: [
1326
+ {
1327
+ resourceId: 'conflicting-id',
1328
+ type: 'human',
1329
+ name: 'Conflicting Checkpoint',
1330
+ description: 'Conflicts with workflow',
1331
+ version: '1.0.0',
1332
+ status: 'dev'
1333
+ }
1334
+ ]
1335
+ }
1336
+ }
1337
+
1338
+ expect(() => {
1339
+ new ResourceRegistry(registry)
1340
+ }).toThrow("[test-org] Human checkpoint ID 'conflicting-id' conflicts with internal resource ID")
1341
+ })
1342
+ })
1343
+ })