@elevasis/core 0.48.1 → 0.49.1

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,1013 +1,1010 @@
1
- /**
2
- * ResourceRegistry Integration Tests
3
- *
4
- * Tests the full registry lifecycle with real organization configs.
5
- * Pattern: CI-Safe Service Integration (Pattern 3) - runs in CI, no external deps.
6
- *
7
- * Tests:
8
- * - Organization config compliance with new ResourceDefinition schema
9
- * - Command View edge generation from consolidated vocabulary
10
- * - Serialization roundtrip
11
- * - Validation integration
12
- * - Multi-organization isolation
13
- */
14
-
15
- import { describe, it, expect, beforeAll } from 'vitest'
16
- import { z } from 'zod'
17
- import { ResourceRegistry } from '../resource-registry'
18
- import type { DeploymentSpec } from '../resource-registry'
19
- import type {
20
- TriggerDefinition,
21
- IntegrationDefinition,
22
- ExternalResourceDefinition,
23
- HumanCheckpointDefinition,
24
- ResourceRelationships
25
- } from '../types'
26
- import type { WorkflowDefinition } from '../../../execution/engine/workflow/types'
27
- import type { AgentDefinition } from '../../../execution/engine/agent/core/types'
28
- import type { ModelConfig } from '../../../execution/engine/llm/model-info'
29
-
30
- describe('ResourceRegistry Integration', () => {
31
- // ============================================================================
32
- // Mock Data Helpers
33
- // ============================================================================
34
-
35
- const createMockWorkflow = (resourceId: string, status: 'dev' | 'prod' = 'dev'): WorkflowDefinition => ({
36
- config: {
37
- resourceId,
38
- name: `Workflow ${resourceId}`,
39
- description: `Test workflow ${resourceId}`,
40
- version: '1.0.0',
41
- type: 'workflow',
42
- status
43
- },
44
- contract: {
45
- inputSchema: z.object({ data: z.string() }),
46
- outputSchema: z.object({ result: z.boolean() })
47
- },
48
- steps: {
49
- step1: {
50
- id: 'step1',
51
- name: 'First Step',
52
- description: 'First step',
53
- handler: async () => ({ result: true }),
54
- inputSchema: z.object({ data: z.string() }),
55
- outputSchema: z.object({ result: z.boolean() }),
56
- next: null
57
- }
58
- },
59
- entryPoint: 'step1'
60
- })
61
-
62
- const createMockAgent = (resourceId: string, status: 'dev' | 'prod' = 'dev'): AgentDefinition => ({
63
- config: {
64
- resourceId,
65
- name: `Agent ${resourceId}`,
66
- description: `Test agent ${resourceId}`,
67
- version: '1.0.0',
68
- type: 'agent',
69
- status,
70
- systemPrompt: 'You are a test agent'
71
- },
72
- contract: {
73
- inputSchema: z.object({ query: z.string() }),
74
- outputSchema: z.object({ response: z.string() })
75
- },
76
- tools: [],
77
- modelConfig: {
78
- provider: 'mock',
79
- apiKey: 'test-key',
80
- model: 'mock'
81
- } as ModelConfig
82
- })
83
-
84
- /**
85
- * Create comprehensive test organization exercising all resource types
86
- * and edge generation patterns
87
- */
88
- const createTestOrganization = (): DeploymentSpec => {
89
- const workflows = [
90
- createMockWorkflow('order-workflow', 'prod'),
91
- createMockWorkflow('support-workflow', 'dev')
92
- ]
93
-
94
- const agents = [
95
- createMockAgent('order-agent', 'prod'),
96
- createMockAgent('support-agent', 'dev')
97
- ]
98
-
99
- const triggers: TriggerDefinition[] = [
100
- {
101
- resourceId: 'trigger-webhook-order',
102
- type: 'trigger',
103
- triggerType: 'webhook',
104
- version: '1.0.0',
105
- name: 'Order Webhook',
106
- description: 'Webhook from Shopify on new orders',
107
- status: 'prod',
108
- webhookPath: '/webhooks/shopify/orders'
109
- },
110
- {
111
- resourceId: 'trigger-schedule-support',
112
- type: 'trigger',
113
- triggerType: 'schedule',
114
- version: '1.0.0',
115
- name: 'Support Ticket Check',
116
- description: 'Check for new support tickets every hour',
117
- status: 'dev',
118
- schedule: '0 * * * *'
119
- },
120
- {
121
- resourceId: 'trigger-manual-test',
122
- type: 'trigger',
123
- triggerType: 'manual',
124
- version: '1.0.0',
125
- name: 'Manual Test',
126
- description: 'Manual trigger for testing',
127
- status: 'dev'
128
- }
129
- ]
130
-
131
- const integrations: IntegrationDefinition[] = [
132
- {
133
- resourceId: 'integration-shopify',
134
- type: 'integration',
135
- version: '1.0.0',
136
- status: 'prod',
137
- provider: 'shopify',
138
- credentialName: 'shopify-prod',
139
- name: 'Shopify Production',
140
- description: 'E-commerce platform'
141
- },
142
- {
143
- resourceId: 'integration-zendesk',
144
- type: 'integration',
145
- version: '1.0.0',
146
- status: 'dev',
147
- provider: 'zendesk',
148
- credentialName: 'zendesk-dev',
149
- name: 'Zendesk Support',
150
- description: 'Support ticket system'
151
- }
152
- ]
153
-
154
- const externalResources: ExternalResourceDefinition[] = [
155
- {
156
- resourceId: 'external-n8n-sync',
157
- type: 'external',
158
- version: '1.0.0',
159
- platform: 'n8n',
160
- name: 'n8n Data Sync',
161
- description: 'Legacy n8n workflow for syncing data',
162
- status: 'dev',
163
- platformUrl: 'https://n8n.example.com/workflow/123',
164
- externalId: 'workflow-123',
165
- triggers: { workflows: ['support-workflow'] },
166
- uses: { integrations: ['integration-zendesk'] }
167
- },
168
- {
169
- resourceId: 'external-zapier-notify',
170
- type: 'external',
171
- version: '1.0.0',
172
- platform: 'zapier',
173
- name: 'Zapier Notifications',
174
- description: 'Zapier zap for sending notifications',
175
- status: 'prod',
176
- platformUrl: 'https://zapier.com/app/zap/456'
177
- // No triggeredBy - removed per relationship-consolidation design
178
- }
179
- ]
180
-
181
- const humanCheckpoints: HumanCheckpointDefinition[] = [
182
- {
183
- resourceId: 'approval-high-value',
184
- type: 'human',
185
- version: '1.0.0',
186
- name: 'High Value Order Approval',
187
- description: 'Approve orders over $10,000',
188
- status: 'prod',
189
- requestedBy: { agents: ['order-agent'] },
190
- routesTo: { workflows: ['order-workflow'] }
191
- },
192
- {
193
- resourceId: 'approval-escalation',
194
- type: 'human',
195
- version: '1.0.0',
196
- name: 'Support Escalation',
197
- description: 'Escalated support tickets requiring review',
198
- status: 'dev',
199
- requestedBy: { workflows: ['support-workflow'] }
200
- // No routesTo - optional field
201
- }
202
- ]
203
-
204
- const relationships: ResourceRelationships = {
205
- // Trigger relationships (moved from TriggerDefinition.triggers)
206
- 'trigger-webhook-order': {
207
- triggers: { workflows: ['order-workflow'] }
208
- },
209
- 'trigger-schedule-support': {
210
- triggers: { agents: ['support-agent'] }
211
- },
212
- 'trigger-manual-test': {
213
- triggers: { workflows: ['order-workflow'] }
214
- },
215
- // Agent/Workflow relationships
216
- 'order-agent': {
217
- uses: { integrations: ['integration-shopify'] },
218
- triggers: { workflows: ['order-workflow'] }
219
- },
220
- 'support-agent': {
221
- uses: { integrations: ['integration-zendesk'] }
222
- },
223
- 'order-workflow': {
224
- triggers: { agents: ['support-agent'] }
225
- }
226
- }
227
-
228
- return {
229
- workflows,
230
- agents,
231
- triggers,
232
- integrations,
233
- externalResources,
234
- humanCheckpoints,
235
- relationships
236
- }
237
- }
238
-
239
- // ============================================================================
240
- // Organization Config Compliance Tests
241
- // ============================================================================
242
-
243
- describe('Organization Config Compliance', () => {
244
- it('creates registry with all resource types without throwing', () => {
245
- const testOrg = createTestOrganization()
246
-
247
- expect(() => {
248
- new ResourceRegistry({ 'test-org': testOrg })
249
- }).not.toThrow()
250
- })
251
-
252
- it('all triggers have resourceId, version, type discriminator, triggerType', () => {
253
- const testOrg = createTestOrganization()
254
- const registry = new ResourceRegistry({ 'test-org': testOrg })
255
-
256
- const triggers = registry.getTriggers('test-org')
257
-
258
- expect(triggers.length).toBeGreaterThan(0)
259
- for (const trigger of triggers) {
260
- expect(trigger.resourceId).toBeDefined()
261
- expect(trigger.version).toBeDefined()
262
- expect(trigger.type).toBe('trigger')
263
- expect(trigger.triggerType).toBeDefined()
264
- expect(['webhook', 'schedule', 'manual', 'event']).toContain(trigger.triggerType)
265
- }
266
- })
267
-
268
- it('all integrations have resourceId, version, type, status', () => {
269
- const testOrg = createTestOrganization()
270
- const registry = new ResourceRegistry({ 'test-org': testOrg })
271
-
272
- const integrations = registry.getIntegrations('test-org')
273
-
274
- expect(integrations.length).toBeGreaterThan(0)
275
- for (const integration of integrations) {
276
- expect(integration.resourceId).toBeDefined()
277
- expect(integration.version).toBeDefined()
278
- expect(integration.type).toBe('integration')
279
- expect(integration.status).toBeDefined()
280
- expect(['dev', 'prod']).toContain(integration.status)
281
- }
282
- })
283
-
284
- it('all external resources have resourceId, version, type (no triggeredBy)', () => {
285
- const testOrg = createTestOrganization()
286
- const registry = new ResourceRegistry({ 'test-org': testOrg })
287
-
288
- const externalResources = registry.getExternalResources('test-org')
289
-
290
- expect(externalResources.length).toBeGreaterThan(0)
291
- for (const external of externalResources) {
292
- expect(external.resourceId).toBeDefined()
293
- expect(external.version).toBeDefined()
294
- expect(external.type).toBe('external')
295
- // Ensure triggeredBy field doesn't exist (removed in refactor)
296
- expect((external as unknown as { triggeredBy?: unknown }).triggeredBy).toBeUndefined()
297
- }
298
- })
299
-
300
- it('all human checkpoints have resourceId, version, type, description (required)', () => {
301
- const testOrg = createTestOrganization()
302
- const registry = new ResourceRegistry({ 'test-org': testOrg })
303
-
304
- const humanCheckpoints = registry.getHumanCheckpoints('test-org')
305
-
306
- expect(humanCheckpoints.length).toBeGreaterThan(0)
307
- for (const checkpoint of humanCheckpoints) {
308
- expect(checkpoint.resourceId).toBeDefined()
309
- expect(checkpoint.version).toBeDefined()
310
- expect(checkpoint.type).toBe('human')
311
- expect(checkpoint.description).toBeDefined()
312
- expect(checkpoint.description.length).toBeGreaterThan(0)
313
- }
314
- })
315
- })
316
-
317
- // ============================================================================
318
- // Command View Data Generation Tests
319
- // ============================================================================
320
-
321
- describe('Command View Data Generation', () => {
322
- let commandViewData: ReturnType<ResourceRegistry['getCommandViewData']>
323
-
324
- beforeAll(() => {
325
- const testOrg = createTestOrganization()
326
- const registry = new ResourceRegistry({ 'test-org': testOrg })
327
- commandViewData = registry.getCommandViewData('test-org')
328
- })
329
-
330
- it('generates edges from trigger.triggers declarations', () => {
331
- // trigger-webhook-order triggers order-workflow
332
- const edge1 = commandViewData.edges.find(
333
- (e) => e.source === 'trigger-webhook-order' && e.target === 'order-workflow'
334
- )
335
- expect(edge1).toBeDefined()
336
- expect(edge1?.relationship).toBe('triggers')
337
-
338
- // trigger-schedule-support triggers support-agent
339
- const edge2 = commandViewData.edges.find(
340
- (e) => e.source === 'trigger-schedule-support' && e.target === 'support-agent'
341
- )
342
- expect(edge2).toBeDefined()
343
- expect(edge2?.relationship).toBe('triggers')
344
-
345
- // trigger-manual-test triggers order-workflow
346
- const edge3 = commandViewData.edges.find(
347
- (e) => e.source === 'trigger-manual-test' && e.target === 'order-workflow'
348
- )
349
- expect(edge3).toBeDefined()
350
- expect(edge3?.relationship).toBe('triggers')
351
- })
352
-
353
- it('generates edges from resource relationships.triggers', () => {
354
- // order-agent triggers order-workflow
355
- const edge1 = commandViewData.edges.find((e) => e.source === 'order-agent' && e.target === 'order-workflow')
356
- expect(edge1).toBeDefined()
357
- expect(edge1?.relationship).toBe('triggers')
358
-
359
- // order-workflow triggers support-agent
360
- const edge2 = commandViewData.edges.find((e) => e.source === 'order-workflow' && e.target === 'support-agent')
361
- expect(edge2).toBeDefined()
362
- expect(edge2?.relationship).toBe('triggers')
363
- })
364
-
365
- it('generates edges from external.triggers declarations', () => {
366
- // external-n8n-sync triggers support-workflow
367
- const edge = commandViewData.edges.find(
368
- (e) => e.source === 'external-n8n-sync' && e.target === 'support-workflow'
369
- )
370
- expect(edge).toBeDefined()
371
- expect(edge?.relationship).toBe('triggers')
372
- })
373
-
374
- it('generates approval edges from humanCheckpoint.requestedBy', () => {
375
- // order-agent -> approval-high-value (approval edge)
376
- const edge1 = commandViewData.edges.find((e) => e.source === 'order-agent' && e.target === 'approval-high-value')
377
- expect(edge1).toBeDefined()
378
- expect(edge1?.relationship).toBe('approval')
379
-
380
- // support-workflow -> approval-escalation (approval edge)
381
- const edge2 = commandViewData.edges.find(
382
- (e) => e.source === 'support-workflow' && e.target === 'approval-escalation'
383
- )
384
- expect(edge2).toBeDefined()
385
- expect(edge2?.relationship).toBe('approval')
386
- })
387
-
388
- it('generates triggers edges from humanCheckpoint.routesTo', () => {
389
- // approval-high-value -> order-workflow (triggers edge after approval)
390
- const edge = commandViewData.edges.find(
391
- (e) => e.source === 'approval-high-value' && e.target === 'order-workflow'
392
- )
393
- expect(edge).toBeDefined()
394
- expect(edge?.relationship).toBe('triggers')
395
- })
396
-
397
- it('does NOT generate edges from triggeredBy (removed)', () => {
398
- // No edges should exist with triggeredBy as source field (field was removed)
399
- const allEdges = commandViewData.edges
400
-
401
- // Verify no edge uses triggeredBy logic
402
- // In old system, external-zapier-notify might have had triggeredBy: { agents: ['order-agent'] }
403
- // which would create order-agent -> external-zapier-notify edge
404
- // This should NOT exist now
405
- const oldStyleEdge = allEdges.find((e) => e.source === 'order-agent' && e.target === 'external-zapier-notify')
406
- expect(oldStyleEdge).toBeUndefined()
407
- })
408
-
409
- it('all edges have valid relationship types (triggers, uses, approval)', () => {
410
- const validRelationships = ['triggers', 'uses', 'approval']
411
-
412
- for (const edge of commandViewData.edges) {
413
- expect(validRelationships).toContain(edge.relationship)
414
- }
415
- })
416
-
417
- it('no edges have relationship type invokes', () => {
418
- // 'invokes' was renamed to 'triggers' - ensure no old relationship type exists
419
- const invokesEdges = commandViewData.edges.filter(
420
- (e) => (e as unknown as { relationship: string }).relationship === 'invokes'
421
- )
422
- expect(invokesEdges).toHaveLength(0)
423
- })
424
-
425
- it('generates uses edges from relationships.uses', () => {
426
- // order-agent uses integration-shopify
427
- const edge1 = commandViewData.edges.find((e) => e.source === 'order-agent' && e.target === 'integration-shopify')
428
- expect(edge1).toBeDefined()
429
- expect(edge1?.relationship).toBe('uses')
430
-
431
- // support-agent uses integration-zendesk
432
- const edge2 = commandViewData.edges.find(
433
- (e) => e.source === 'support-agent' && e.target === 'integration-zendesk'
434
- )
435
- expect(edge2).toBeDefined()
436
- expect(edge2?.relationship).toBe('uses')
437
- })
438
-
439
- it('generates uses edges from external.uses', () => {
440
- // external-n8n-sync uses integration-zendesk
441
- const edge = commandViewData.edges.find(
442
- (e) => e.source === 'external-n8n-sync' && e.target === 'integration-zendesk'
443
- )
444
- expect(edge).toBeDefined()
445
- expect(edge?.relationship).toBe('uses')
446
- })
447
- })
448
-
449
- // ============================================================================
450
- // Serialization Roundtrip Tests
451
- // ============================================================================
452
-
453
- describe('Serialization Roundtrip', () => {
454
- let registry: ResourceRegistry
455
-
456
- beforeAll(() => {
457
- const testOrg = createTestOrganization()
458
- registry = new ResourceRegistry({ 'test-org': testOrg })
459
- })
460
-
461
- it('serialized data preserves all ResourceDefinition fields', () => {
462
- const resourceList = registry.getResourceList('test-org')
463
-
464
- for (const workflow of resourceList.workflows) {
465
- expect(workflow.resourceId).toBeDefined()
466
- expect(workflow.name).toBeDefined()
467
- expect(workflow.description).toBeDefined()
468
- expect(workflow.version).toBeDefined()
469
- expect(workflow.type).toBe('workflow')
470
- expect(workflow.status).toBeDefined()
471
- }
472
-
473
- for (const agent of resourceList.agents) {
474
- expect(agent.resourceId).toBeDefined()
475
- expect(agent.name).toBeDefined()
476
- expect(agent.description).toBeDefined()
477
- expect(agent.version).toBeDefined()
478
- expect(agent.type).toBe('agent')
479
- expect(agent.status).toBeDefined()
480
- }
481
- })
482
-
483
- it('command view workflows have resourceId, version, type', () => {
484
- const commandViewData = registry.getCommandViewData('test-org')
485
-
486
- expect(commandViewData.workflows.length).toBeGreaterThan(0)
487
- for (const workflow of commandViewData.workflows) {
488
- expect(workflow.resourceId).toBeDefined()
489
- expect(workflow.version).toBeDefined()
490
- expect(workflow.type).toBe('workflow')
491
- }
492
- })
493
-
494
- it('command view agents have resourceId, version, type', () => {
495
- const commandViewData = registry.getCommandViewData('test-org')
496
-
497
- expect(commandViewData.agents.length).toBeGreaterThan(0)
498
- for (const agent of commandViewData.agents) {
499
- expect(agent.resourceId).toBeDefined()
500
- expect(agent.version).toBeDefined()
501
- expect(agent.type).toBe('agent')
502
- }
503
- })
504
-
505
- it('command view data includes all node types', () => {
506
- const commandViewData = registry.getCommandViewData('test-org')
507
-
508
- expect(commandViewData.workflows.length).toBeGreaterThan(0)
509
- expect(commandViewData.agents.length).toBeGreaterThan(0)
510
- expect(commandViewData.triggers.length).toBeGreaterThan(0)
511
- expect(commandViewData.integrations.length).toBeGreaterThan(0)
512
- expect(commandViewData.externalResources.length).toBeGreaterThan(0)
513
- expect(commandViewData.humanCheckpoints.length).toBeGreaterThan(0)
514
- })
515
-
516
- it('command view edges reference valid node resourceIds', () => {
517
- const commandViewData = registry.getCommandViewData('test-org')
518
-
519
- // Collect all valid node IDs
520
- const validNodeIds = new Set([
521
- ...commandViewData.workflows.map((w) => w.resourceId),
522
- ...commandViewData.agents.map((a) => a.resourceId),
523
- ...commandViewData.triggers.map((t) => t.resourceId),
524
- ...commandViewData.integrations.map((i) => i.resourceId),
525
- ...commandViewData.externalResources.map((e) => e.resourceId),
526
- ...commandViewData.humanCheckpoints.map((h) => h.resourceId)
527
- ])
528
-
529
- // Verify all edges reference valid nodes
530
- for (const edge of commandViewData.edges) {
531
- expect(validNodeIds.has(edge.source)).toBe(true)
532
- expect(validNodeIds.has(edge.target)).toBe(true)
533
- }
534
- })
535
-
536
- it('serialized definitions match original config', () => {
537
- const workflowDef = registry.getSerializedDefinition('test-org', 'order-workflow')
538
- expect(workflowDef).toBeDefined()
539
- expect(workflowDef?.config.resourceId).toBe('order-workflow')
540
- expect(workflowDef?.config.name).toBe('Workflow order-workflow')
541
- expect(workflowDef?.config.type).toBe('workflow')
542
-
543
- const agentDef = registry.getSerializedDefinition('test-org', 'order-agent')
544
- expect(agentDef).toBeDefined()
545
- expect(agentDef?.config.resourceId).toBe('order-agent')
546
- expect(agentDef?.config.name).toBe('Agent order-agent')
547
- expect(agentDef?.config.type).toBe('agent')
548
- })
549
- })
550
-
551
- // ============================================================================
552
- // Validation Integration Tests
553
- // ============================================================================
554
-
555
- describe('Validation Integration', () => {
556
- it('throws for trigger triggering non-existent resource', () => {
557
- const invalidOrg: DeploymentSpec = {
558
- workflows: [createMockWorkflow('valid-workflow')],
559
- triggers: [
560
- {
561
- resourceId: 'trigger-invalid',
562
- type: 'trigger',
563
- triggerType: 'webhook',
564
- version: '1.0.0',
565
- name: 'Invalid Trigger',
566
- description: 'Triggers non-existent workflow',
567
- status: 'dev'
568
- }
569
- ],
570
- relationships: {
571
- 'trigger-invalid': {
572
- triggers: { workflows: ['non-existent-workflow'] }
573
- }
574
- }
575
- }
576
-
577
- expect(() => {
578
- new ResourceRegistry({ 'test-org': invalidOrg })
579
- }).toThrow()
580
- expect(() => {
581
- new ResourceRegistry({ 'test-org': invalidOrg })
582
- }).toThrow('non-existent-workflow')
583
- })
584
-
585
- it('throws for external resource with conflicting ID', () => {
586
- const invalidOrg: DeploymentSpec = {
587
- workflows: [createMockWorkflow('conflict-id')],
588
- externalResources: [
589
- {
590
- resourceId: 'conflict-id', // Same as workflow
591
- type: 'external',
592
- version: '1.0.0',
593
- platform: 'n8n',
594
- name: 'Conflicting External',
595
- description: 'Has same ID as workflow',
596
- status: 'dev'
597
- }
598
- ]
599
- }
600
-
601
- expect(() => {
602
- new ResourceRegistry({ 'test-org': invalidOrg })
603
- }).toThrow("External resource ID 'conflict-id' conflicts with internal resource ID")
604
- })
605
-
606
- it('throws for human checkpoint with conflicting ID', () => {
607
- const invalidOrg: DeploymentSpec = {
608
- workflows: [createMockWorkflow('conflict-id')], // Changed to workflow
609
- humanCheckpoints: [
610
- {
611
- resourceId: 'conflict-id', // Same as workflow
612
- type: 'human',
613
- version: '1.0.0',
614
- name: 'Conflicting Checkpoint',
615
- description: 'Has same ID as workflow',
616
- status: 'dev'
617
- }
618
- ]
619
- }
620
-
621
- expect(() => {
622
- new ResourceRegistry({ 'test-org': invalidOrg })
623
- }).toThrow("Human checkpoint ID 'conflict-id' conflicts with internal resource ID")
624
- })
625
-
626
- it('throws for relationship to non-existent integration', () => {
627
- const invalidOrg: DeploymentSpec = {
628
- agents: [createMockAgent('test-agent')],
629
- relationships: {
630
- 'test-agent': {
631
- uses: { integrations: ['non-existent-integration'] }
632
- }
633
- }
634
- }
635
-
636
- expect(() => {
637
- new ResourceRegistry({ 'test-org': invalidOrg })
638
- }).toThrow()
639
- expect(() => {
640
- new ResourceRegistry({ 'test-org': invalidOrg })
641
- }).toThrow('non-existent-integration')
642
- })
643
-
644
- it('validates relationship graph integrity', () => {
645
- const invalidOrg: DeploymentSpec = {
646
- workflows: [createMockWorkflow('workflow-1')],
647
- relationships: {
648
- 'workflow-1': {
649
- triggers: { workflows: ['non-existent-workflow-2'] }
650
- }
651
- }
652
- }
653
-
654
- expect(() => {
655
- new ResourceRegistry({ 'test-org': invalidOrg })
656
- }).toThrow()
657
- })
658
-
659
- it('allows valid relationships without errors', () => {
660
- const validOrg: DeploymentSpec = {
661
- workflows: [createMockWorkflow('workflow-1'), createMockWorkflow('workflow-2')],
662
- agents: [createMockAgent('agent-1')],
663
- integrations: [
664
- {
665
- resourceId: 'integration-1',
666
- type: 'integration',
667
- version: '1.0.0',
668
- status: 'dev',
669
- provider: 'webhook',
670
- credentialName: 'webhook-cred',
671
- name: 'Webhook Integration',
672
- description: 'Test webhook'
673
- }
674
- ],
675
- relationships: {
676
- 'workflow-1': {
677
- triggers: { workflows: ['workflow-2'], agents: ['agent-1'] },
678
- uses: { integrations: ['integration-1'] }
679
- }
680
- }
681
- }
682
-
683
- expect(() => {
684
- new ResourceRegistry({ 'test-org': validOrg })
685
- }).not.toThrow()
686
- })
687
- })
688
-
689
- // ============================================================================
690
- // Multi-Organization Isolation Tests
691
- // ============================================================================
692
-
693
- describe('Multi-Organization Isolation', () => {
694
- it('resources from different orgs are isolated', () => {
695
- const org1: DeploymentSpec = {
696
- workflows: [createMockWorkflow('workflow-org1')],
697
- agents: [createMockAgent('agent-org1')]
698
- }
699
-
700
- const org2: DeploymentSpec = {
701
- workflows: [createMockWorkflow('workflow-org2')],
702
- agents: [createMockAgent('agent-org2')]
703
- }
704
-
705
- const registry = new ResourceRegistry({
706
- org1,
707
- org2
708
- })
709
-
710
- // Org1 resources
711
- expect(registry.getResourceDefinition('org1', 'workflow-org1')).toBeDefined()
712
- expect(registry.getResourceDefinition('org1', 'agent-org1')).toBeDefined()
713
- expect(registry.getResourceDefinition('org1', 'workflow-org2')).toBeNull()
714
- expect(registry.getResourceDefinition('org1', 'agent-org2')).toBeNull()
715
-
716
- // Org2 resources
717
- expect(registry.getResourceDefinition('org2', 'workflow-org2')).toBeDefined()
718
- expect(registry.getResourceDefinition('org2', 'agent-org2')).toBeDefined()
719
- expect(registry.getResourceDefinition('org2', 'workflow-org1')).toBeNull()
720
- expect(registry.getResourceDefinition('org2', 'agent-org1')).toBeNull()
721
- })
722
-
723
- it('triggers can only reference resources in same org', () => {
724
- const org1: DeploymentSpec = {
725
- workflows: [createMockWorkflow('workflow-org1')],
726
- triggers: [
727
- {
728
- resourceId: 'trigger-org1',
729
- type: 'trigger',
730
- triggerType: 'manual',
731
- version: '1.0.0',
732
- name: 'Org1 Trigger',
733
- description: 'Triggers workflow in org2 (invalid)',
734
- status: 'dev'
735
- }
736
- ],
737
- relationships: {
738
- 'trigger-org1': {
739
- triggers: { workflows: ['workflow-org2'] } // References org2 workflow - should fail
740
- }
741
- }
742
- }
743
-
744
- const org2: DeploymentSpec = {
745
- workflows: [createMockWorkflow('workflow-org2')]
746
- }
747
-
748
- expect(() => {
749
- new ResourceRegistry({ org1, org2 })
750
- }).toThrow('workflow-org2')
751
- })
752
-
753
- it('command view data is organization-specific', () => {
754
- // Create different orgs with different resource IDs
755
- const org1: DeploymentSpec = {
756
- workflows: [createMockWorkflow('org1-workflow')],
757
- agents: [createMockAgent('org1-agent')]
758
- }
759
-
760
- const org2: DeploymentSpec = {
761
- workflows: [createMockWorkflow('org2-workflow')],
762
- agents: [createMockAgent('org2-agent')]
763
- }
764
-
765
- const registry = new ResourceRegistry({ org1, org2 })
766
-
767
- const org1Data = registry.getCommandViewData('org1')
768
- const org2Data = registry.getCommandViewData('org2')
769
-
770
- // Both orgs should have same number of resources
771
- // but data should be independent instances
772
- expect(org1Data).not.toBe(org2Data)
773
- expect(org1Data.workflows.length).toBe(1)
774
- expect(org2Data.workflows.length).toBe(1)
775
-
776
- // Verify isolation - org1 nodes should not appear in org2 data
777
- const org1NodeIds = new Set([
778
- ...org1Data.workflows.map((w) => w.resourceId),
779
- ...org1Data.agents.map((a) => a.resourceId)
780
- ])
781
-
782
- const org2NodeIds = new Set([
783
- ...org2Data.workflows.map((w) => w.resourceId),
784
- ...org2Data.agents.map((a) => a.resourceId)
785
- ])
786
-
787
- // Verify no overlap in resource IDs
788
- for (const nodeId of org1NodeIds) {
789
- expect(org2NodeIds.has(nodeId)).toBe(false)
790
- }
791
- for (const nodeId of org2NodeIds) {
792
- expect(org1NodeIds.has(nodeId)).toBe(false)
793
- }
794
-
795
- // Verify edges reference only their own org's nodes
796
- for (const edge of org1Data.edges) {
797
- // org1 edges should only reference org1 nodes
798
- expect(
799
- org1NodeIds.has(edge.source) ||
800
- org1Data.triggers.some((t) => t.resourceId === edge.source) ||
801
- org1Data.integrations.some((i) => i.resourceId === edge.source)
802
- ).toBe(true)
803
- expect(
804
- org1NodeIds.has(edge.target) ||
805
- org1Data.triggers.some((t) => t.resourceId === edge.target) ||
806
- org1Data.integrations.some((i) => i.resourceId === edge.target)
807
- ).toBe(true)
808
- }
809
-
810
- for (const edge of org2Data.edges) {
811
- // org2 edges should not reference org1 nodes
812
- expect(org1NodeIds.has(edge.source)).toBe(false)
813
- expect(org1NodeIds.has(edge.target)).toBe(false)
814
- }
815
- })
816
-
817
- it('allows same resourceId across different organizations', () => {
818
- const org1: DeploymentSpec = {
819
- workflows: [createMockWorkflow('shared-id')]
820
- }
821
-
822
- const org2: DeploymentSpec = {
823
- workflows: [createMockWorkflow('shared-id')]
824
- }
825
-
826
- expect(() => {
827
- new ResourceRegistry({ org1, org2 })
828
- }).not.toThrow()
829
-
830
- const registry = new ResourceRegistry({ org1, org2 })
831
-
832
- const org1Resource = registry.getResourceDefinition('org1', 'shared-id')
833
- const org2Resource = registry.getResourceDefinition('org2', 'shared-id')
834
-
835
- expect(org1Resource).toBeDefined()
836
- expect(org2Resource).toBeDefined()
837
- expect(org1Resource).not.toBe(org2Resource) // Different instances
838
- })
839
- })
840
-
841
- // ============================================================================
842
- // Runtime Registration Relationship Tests
843
- // ============================================================================
844
-
845
- describe('Runtime Registration Relationships', () => {
846
- const remoteConfig = {
847
- storagePath: 'test-org-id/test-deploy-id/bundle.js',
848
- deploymentId: 'deploy-123'
849
- }
850
-
851
- it('registerOrganization merges relationships from stubs into existing org', () => {
852
- // Start with a static org that has a workflow and relationships
853
- const staticOrg: DeploymentSpec = {
854
- workflows: [createMockWorkflow('static-workflow')],
855
- agents: [createMockAgent('static-agent')],
856
- integrations: [
857
- {
858
- resourceId: 'integration-crm',
859
- type: 'integration',
860
- version: '1.0.0',
861
- status: 'dev',
862
- provider: 'crm',
863
- credentialName: 'crm-cred',
864
- name: 'CRM',
865
- description: 'CRM integration'
866
- }
867
- ],
868
- relationships: {
869
- 'static-workflow': {
870
- triggers: { agents: ['static-agent'] }
871
- }
872
- }
873
- }
874
-
875
- const registry = new ResourceRegistry({ 'test-org': staticOrg })
876
-
877
- // Deploy stubs with their own relationships
878
- const stubs: DeploymentSpec = {
879
- workflows: [createMockWorkflow('remote-workflow')],
880
- integrations: [
881
- {
882
- resourceId: 'integration-crm',
883
- type: 'integration',
884
- version: '1.0.0',
885
- status: 'dev',
886
- provider: 'crm',
887
- credentialName: 'crm-cred',
888
- name: 'CRM',
889
- description: 'CRM integration'
890
- }
891
- ],
892
- relationships: {
893
- 'remote-workflow': {
894
- uses: { integrations: ['integration-crm'] }
895
- }
896
- }
897
- }
898
-
899
- registry.registerOrganization('test-org', stubs, remoteConfig)
900
-
901
- // Both static and remote relationships should exist
902
- const relationships = registry.getRelationships('test-org')
903
- expect(relationships).toBeDefined()
904
- expect(relationships!['static-workflow']).toBeDefined()
905
- expect(relationships!['static-workflow'].triggers?.agents).toContain('static-agent')
906
- expect(relationships!['remote-workflow']).toBeDefined()
907
- expect(relationships!['remote-workflow'].uses?.integrations).toContain('integration-crm')
908
- })
909
-
910
- it('unregisterOrganization removes remote resource relationships, preserves static ones', () => {
911
- const staticOrg: DeploymentSpec = {
912
- workflows: [createMockWorkflow('static-workflow')],
913
- agents: [createMockAgent('static-agent')],
914
- relationships: {
915
- 'static-workflow': {
916
- triggers: { agents: ['static-agent'] }
917
- }
918
- }
919
- }
920
-
921
- const registry = new ResourceRegistry({ 'test-org': staticOrg })
922
-
923
- // Register remote resources with relationships
924
- const stubs: DeploymentSpec = {
925
- workflows: [createMockWorkflow('remote-workflow')],
926
- relationships: {
927
- 'remote-workflow': {
928
- triggers: { agents: ['static-agent'] }
929
- }
930
- }
931
- }
932
-
933
- registry.registerOrganization('test-org', stubs, remoteConfig)
934
-
935
- // Verify both exist
936
- expect(registry.getRelationships('test-org')!['remote-workflow']).toBeDefined()
937
- expect(registry.getRelationships('test-org')!['static-workflow']).toBeDefined()
938
-
939
- // Unregister remote resources
940
- registry.unregisterOrganization('test-org')
941
-
942
- // Static relationships preserved, remote relationships removed
943
- const relationships = registry.getRelationships('test-org')
944
- expect(relationships).toBeDefined()
945
- expect(relationships!['static-workflow']).toBeDefined()
946
- expect(relationships!['remote-workflow']).toBeUndefined()
947
- })
948
-
949
- it('getCommandViewData returns correct edges after registering with relationships', () => {
950
- const staticOrg: DeploymentSpec = {
951
- workflows: [createMockWorkflow('static-workflow')],
952
- agents: [createMockAgent('static-agent')],
953
- integrations: [
954
- {
955
- resourceId: 'integration-crm',
956
- type: 'integration',
957
- version: '1.0.0',
958
- status: 'dev',
959
- provider: 'crm',
960
- credentialName: 'crm-cred',
961
- name: 'CRM',
962
- description: 'CRM integration'
963
- }
964
- ]
965
- }
966
-
967
- const registry = new ResourceRegistry({ 'test-org': staticOrg })
968
-
969
- // No relationship edges before registration
970
- const beforeData = registry.getCommandViewData('test-org')
971
- const beforeEdges = beforeData.edges.filter(
972
- (e) => e.source === 'remote-workflow' || e.target === 'remote-workflow'
973
- )
974
- expect(beforeEdges).toHaveLength(0)
975
-
976
- // Register remote stubs with relationships
977
- const stubs: DeploymentSpec = {
978
- workflows: [createMockWorkflow('remote-workflow')],
979
- integrations: [
980
- {
981
- resourceId: 'integration-crm',
982
- type: 'integration',
983
- version: '1.0.0',
984
- status: 'dev',
985
- provider: 'crm',
986
- credentialName: 'crm-cred',
987
- name: 'CRM',
988
- description: 'CRM integration'
989
- }
990
- ],
991
- relationships: {
992
- 'remote-workflow': {
993
- triggers: { workflows: ['static-workflow'] },
994
- uses: { integrations: ['integration-crm'] }
995
- }
996
- }
997
- }
998
-
999
- registry.registerOrganization('test-org', stubs, remoteConfig)
1000
-
1001
- // Command View should now include edges from remote relationships
1002
- const afterData = registry.getCommandViewData('test-org')
1003
-
1004
- const triggersEdge = afterData.edges.find((e) => e.source === 'remote-workflow' && e.target === 'static-workflow')
1005
- expect(triggersEdge).toBeDefined()
1006
- expect(triggersEdge?.relationship).toBe('triggers')
1007
-
1008
- const usesEdge = afterData.edges.find((e) => e.source === 'remote-workflow' && e.target === 'integration-crm')
1009
- expect(usesEdge).toBeDefined()
1010
- expect(usesEdge?.relationship).toBe('uses')
1011
- })
1012
- })
1013
- })
1
+ /**
2
+ * ResourceRegistry Integration Tests
3
+ *
4
+ * Tests the full registry lifecycle with real organization configs.
5
+ * Pattern: CI-Safe Service Integration (Pattern 3) - runs in CI, no external deps.
6
+ *
7
+ * Tests:
8
+ * - Organization config compliance with new ResourceDefinition schema
9
+ * - Command View edge generation from consolidated vocabulary
10
+ * - Serialization roundtrip
11
+ * - Validation integration
12
+ * - Multi-organization isolation
13
+ */
14
+
15
+ import { describe, it, expect, beforeAll } from 'vitest'
16
+ import { z } from 'zod'
17
+ import { ResourceRegistry } from '../resource-registry'
18
+ import type { DeploymentSpec } from '../resource-registry'
19
+ import type {
20
+ TriggerDefinition,
21
+ IntegrationDefinition,
22
+ ExternalResourceDefinition,
23
+ HumanCheckpointDefinition,
24
+ ResourceRelationships
25
+ } from '../types'
26
+ import type { WorkflowDefinition } from '../../../execution/engine/workflow/types'
27
+ import type { AgentDefinition } from '../../../execution/engine/agent/core/types'
28
+ import type { ModelConfig } from '../../../execution/engine/llm/model-info'
29
+
30
+ describe('ResourceRegistry Integration', () => {
31
+ // ============================================================================
32
+ // Mock Data Helpers
33
+ // ============================================================================
34
+
35
+ const createMockWorkflow = (resourceId: string, status: 'dev' | 'prod' = 'dev'): WorkflowDefinition => ({
36
+ config: {
37
+ resourceId,
38
+ name: `Workflow ${resourceId}`,
39
+ description: `Test workflow ${resourceId}`,
40
+ version: '1.0.0',
41
+ type: 'workflow',
42
+ status
43
+ },
44
+ contract: {
45
+ inputSchema: z.object({ data: z.string() }),
46
+ outputSchema: z.object({ result: z.boolean() })
47
+ },
48
+ steps: {
49
+ step1: {
50
+ id: 'step1',
51
+ name: 'First Step',
52
+ description: 'First step',
53
+ handler: async () => ({ result: true }),
54
+ inputSchema: z.object({ data: z.string() }),
55
+ outputSchema: z.object({ result: z.boolean() }),
56
+ next: null
57
+ }
58
+ },
59
+ entryPoint: 'step1'
60
+ })
61
+
62
+ const createMockAgent = (resourceId: string, status: 'dev' | 'prod' = 'dev'): AgentDefinition => ({
63
+ config: {
64
+ resourceId,
65
+ name: `Agent ${resourceId}`,
66
+ description: `Test agent ${resourceId}`,
67
+ version: '1.0.0',
68
+ type: 'agent',
69
+ status,
70
+ systemPrompt: 'You are a test agent'
71
+ },
72
+ contract: {
73
+ inputSchema: z.object({ query: z.string() }),
74
+ outputSchema: z.object({ response: z.string() })
75
+ },
76
+ tools: [],
77
+ modelConfig: {
78
+ provider: 'mock',
79
+ apiKey: 'test-key',
80
+ model: 'mock'
81
+ } as ModelConfig
82
+ })
83
+
84
+ /**
85
+ * Create comprehensive test organization exercising all resource types
86
+ * and edge generation patterns
87
+ */
88
+ const createTestOrganization = (): DeploymentSpec => {
89
+ const workflows = [createMockWorkflow('order-workflow', 'prod'), createMockWorkflow('support-workflow', 'dev')]
90
+
91
+ const agents = [createMockAgent('order-agent', 'prod'), createMockAgent('support-agent', 'dev')]
92
+
93
+ const triggers: TriggerDefinition[] = [
94
+ {
95
+ resourceId: 'trigger-webhook-order',
96
+ type: 'trigger',
97
+ triggerType: 'webhook',
98
+ version: '1.0.0',
99
+ name: 'Order Webhook',
100
+ description: 'Webhook from Shopify on new orders',
101
+ status: 'prod',
102
+ webhookPath: '/webhooks/shopify/orders'
103
+ },
104
+ {
105
+ resourceId: 'trigger-schedule-support',
106
+ type: 'trigger',
107
+ triggerType: 'schedule',
108
+ version: '1.0.0',
109
+ name: 'Support Ticket Check',
110
+ description: 'Check for new support tickets every hour',
111
+ status: 'dev',
112
+ schedule: '0 * * * *'
113
+ },
114
+ {
115
+ resourceId: 'trigger-manual-test',
116
+ type: 'trigger',
117
+ triggerType: 'manual',
118
+ version: '1.0.0',
119
+ name: 'Manual Test',
120
+ description: 'Manual trigger for testing',
121
+ status: 'dev'
122
+ }
123
+ ]
124
+
125
+ const integrations: IntegrationDefinition[] = [
126
+ {
127
+ resourceId: 'integration-shopify',
128
+ type: 'integration',
129
+ version: '1.0.0',
130
+ status: 'prod',
131
+ provider: 'shopify',
132
+ credentialName: 'shopify-prod',
133
+ name: 'Shopify Production',
134
+ description: 'E-commerce platform'
135
+ },
136
+ {
137
+ resourceId: 'integration-zendesk',
138
+ type: 'integration',
139
+ version: '1.0.0',
140
+ status: 'dev',
141
+ provider: 'zendesk',
142
+ credentialName: 'zendesk-dev',
143
+ name: 'Zendesk Support',
144
+ description: 'Support ticket system'
145
+ }
146
+ ]
147
+
148
+ const externalResources: ExternalResourceDefinition[] = [
149
+ {
150
+ resourceId: 'external-n8n-sync',
151
+ type: 'external',
152
+ version: '1.0.0',
153
+ platform: 'n8n',
154
+ name: 'n8n Data Sync',
155
+ description: 'Legacy n8n workflow for syncing data',
156
+ status: 'dev',
157
+ platformUrl: 'https://n8n.example.com/workflow/123',
158
+ externalId: 'workflow-123',
159
+ triggers: { workflows: ['support-workflow'] },
160
+ uses: { integrations: ['integration-zendesk'] }
161
+ },
162
+ {
163
+ resourceId: 'external-zapier-notify',
164
+ type: 'external',
165
+ version: '1.0.0',
166
+ platform: 'zapier',
167
+ name: 'Zapier Notifications',
168
+ description: 'Zapier zap for sending notifications',
169
+ status: 'prod',
170
+ platformUrl: 'https://zapier.com/app/zap/456'
171
+ // No triggeredBy - removed per relationship-consolidation design
172
+ }
173
+ ]
174
+
175
+ const humanCheckpoints: HumanCheckpointDefinition[] = [
176
+ {
177
+ resourceId: 'approval-high-value',
178
+ type: 'human',
179
+ version: '1.0.0',
180
+ name: 'High Value Order Approval',
181
+ description: 'Approve orders over $10,000',
182
+ status: 'prod',
183
+ requestedBy: { agents: ['order-agent'] },
184
+ routesTo: { workflows: ['order-workflow'] }
185
+ },
186
+ {
187
+ resourceId: 'approval-escalation',
188
+ type: 'human',
189
+ version: '1.0.0',
190
+ name: 'Support Escalation',
191
+ description: 'Escalated support tickets requiring review',
192
+ status: 'dev',
193
+ requestedBy: { workflows: ['support-workflow'] }
194
+ // No routesTo - optional field
195
+ }
196
+ ]
197
+
198
+ const relationships: ResourceRelationships = {
199
+ // Trigger relationships (moved from TriggerDefinition.triggers)
200
+ 'trigger-webhook-order': {
201
+ triggers: { workflows: ['order-workflow'] }
202
+ },
203
+ 'trigger-schedule-support': {
204
+ triggers: { agents: ['support-agent'] }
205
+ },
206
+ 'trigger-manual-test': {
207
+ triggers: { workflows: ['order-workflow'] }
208
+ },
209
+ // Agent/Workflow relationships
210
+ 'order-agent': {
211
+ uses: { integrations: ['integration-shopify'] },
212
+ triggers: { workflows: ['order-workflow'] }
213
+ },
214
+ 'support-agent': {
215
+ uses: { integrations: ['integration-zendesk'] }
216
+ },
217
+ 'order-workflow': {
218
+ triggers: { agents: ['support-agent'] }
219
+ }
220
+ }
221
+
222
+ return {
223
+ workflows,
224
+ agents,
225
+ triggers,
226
+ integrations,
227
+ externalResources,
228
+ humanCheckpoints,
229
+ relationships
230
+ }
231
+ }
232
+
233
+ // ============================================================================
234
+ // Organization Config Compliance Tests
235
+ // ============================================================================
236
+
237
+ describe('Organization Config Compliance', () => {
238
+ it('creates registry with all resource types without throwing', () => {
239
+ const testOrg = createTestOrganization()
240
+
241
+ expect(() => {
242
+ new ResourceRegistry({ 'test-org': testOrg })
243
+ }).not.toThrow()
244
+ })
245
+
246
+ it('all triggers have resourceId, version, type discriminator, triggerType', () => {
247
+ const testOrg = createTestOrganization()
248
+ const registry = new ResourceRegistry({ 'test-org': testOrg })
249
+
250
+ const triggers = registry.getTriggers('test-org')
251
+
252
+ expect(triggers.length).toBeGreaterThan(0)
253
+ for (const trigger of triggers) {
254
+ expect(trigger.resourceId).toBeDefined()
255
+ expect(trigger.version).toBeDefined()
256
+ expect(trigger.type).toBe('trigger')
257
+ expect(trigger.triggerType).toBeDefined()
258
+ expect(['webhook', 'schedule', 'manual', 'event']).toContain(trigger.triggerType)
259
+ }
260
+ })
261
+
262
+ it('all integrations have resourceId, version, type, status', () => {
263
+ const testOrg = createTestOrganization()
264
+ const registry = new ResourceRegistry({ 'test-org': testOrg })
265
+
266
+ const integrations = registry.getIntegrations('test-org')
267
+
268
+ expect(integrations.length).toBeGreaterThan(0)
269
+ for (const integration of integrations) {
270
+ expect(integration.resourceId).toBeDefined()
271
+ expect(integration.version).toBeDefined()
272
+ expect(integration.type).toBe('integration')
273
+ expect(integration.status).toBeDefined()
274
+ expect(['dev', 'prod']).toContain(integration.status)
275
+ }
276
+ })
277
+
278
+ it('all external resources have resourceId, version, type (no triggeredBy)', () => {
279
+ const testOrg = createTestOrganization()
280
+ const registry = new ResourceRegistry({ 'test-org': testOrg })
281
+
282
+ const externalResources = registry.getExternalResources('test-org')
283
+
284
+ expect(externalResources.length).toBeGreaterThan(0)
285
+ for (const external of externalResources) {
286
+ expect(external.resourceId).toBeDefined()
287
+ expect(external.version).toBeDefined()
288
+ expect(external.type).toBe('external')
289
+ // Ensure triggeredBy field doesn't exist (removed in refactor)
290
+ expect((external as unknown as { triggeredBy?: unknown }).triggeredBy).toBeUndefined()
291
+ }
292
+ })
293
+
294
+ it('all human checkpoints have resourceId, version, type, description (required)', () => {
295
+ const testOrg = createTestOrganization()
296
+ const registry = new ResourceRegistry({ 'test-org': testOrg })
297
+
298
+ const humanCheckpoints = registry.getHumanCheckpoints('test-org')
299
+
300
+ expect(humanCheckpoints.length).toBeGreaterThan(0)
301
+ for (const checkpoint of humanCheckpoints) {
302
+ expect(checkpoint.resourceId).toBeDefined()
303
+ expect(checkpoint.version).toBeDefined()
304
+ expect(checkpoint.type).toBe('human')
305
+ expect(checkpoint.description).toBeDefined()
306
+ expect(checkpoint.description.length).toBeGreaterThan(0)
307
+ }
308
+ })
309
+ })
310
+
311
+ // ============================================================================
312
+ // Command View Data Generation Tests
313
+ // ============================================================================
314
+
315
+ describe('Command View Data Generation', () => {
316
+ let commandViewData: ReturnType<ResourceRegistry['getCommandViewData']>
317
+
318
+ beforeAll(() => {
319
+ const testOrg = createTestOrganization()
320
+ const registry = new ResourceRegistry({ 'test-org': testOrg })
321
+ commandViewData = registry.getCommandViewData('test-org')
322
+ })
323
+
324
+ it('generates edges from trigger.triggers declarations', () => {
325
+ // trigger-webhook-order triggers order-workflow
326
+ const edge1 = commandViewData.edges.find(
327
+ (e) => e.source === 'trigger-webhook-order' && e.target === 'order-workflow'
328
+ )
329
+ expect(edge1).toBeDefined()
330
+ expect(edge1?.relationship).toBe('triggers')
331
+
332
+ // trigger-schedule-support triggers support-agent
333
+ const edge2 = commandViewData.edges.find(
334
+ (e) => e.source === 'trigger-schedule-support' && e.target === 'support-agent'
335
+ )
336
+ expect(edge2).toBeDefined()
337
+ expect(edge2?.relationship).toBe('triggers')
338
+
339
+ // trigger-manual-test triggers order-workflow
340
+ const edge3 = commandViewData.edges.find(
341
+ (e) => e.source === 'trigger-manual-test' && e.target === 'order-workflow'
342
+ )
343
+ expect(edge3).toBeDefined()
344
+ expect(edge3?.relationship).toBe('triggers')
345
+ })
346
+
347
+ it('generates edges from resource relationships.triggers', () => {
348
+ // order-agent triggers order-workflow
349
+ const edge1 = commandViewData.edges.find((e) => e.source === 'order-agent' && e.target === 'order-workflow')
350
+ expect(edge1).toBeDefined()
351
+ expect(edge1?.relationship).toBe('triggers')
352
+
353
+ // order-workflow triggers support-agent
354
+ const edge2 = commandViewData.edges.find((e) => e.source === 'order-workflow' && e.target === 'support-agent')
355
+ expect(edge2).toBeDefined()
356
+ expect(edge2?.relationship).toBe('triggers')
357
+ })
358
+
359
+ it('generates edges from external.triggers declarations', () => {
360
+ // external-n8n-sync triggers support-workflow
361
+ const edge = commandViewData.edges.find(
362
+ (e) => e.source === 'external-n8n-sync' && e.target === 'support-workflow'
363
+ )
364
+ expect(edge).toBeDefined()
365
+ expect(edge?.relationship).toBe('triggers')
366
+ })
367
+
368
+ it('generates approval edges from humanCheckpoint.requestedBy', () => {
369
+ // order-agent -> approval-high-value (approval edge)
370
+ const edge1 = commandViewData.edges.find((e) => e.source === 'order-agent' && e.target === 'approval-high-value')
371
+ expect(edge1).toBeDefined()
372
+ expect(edge1?.relationship).toBe('approval')
373
+
374
+ // support-workflow -> approval-escalation (approval edge)
375
+ const edge2 = commandViewData.edges.find(
376
+ (e) => e.source === 'support-workflow' && e.target === 'approval-escalation'
377
+ )
378
+ expect(edge2).toBeDefined()
379
+ expect(edge2?.relationship).toBe('approval')
380
+ })
381
+
382
+ it('generates triggers edges from humanCheckpoint.routesTo', () => {
383
+ // approval-high-value -> order-workflow (triggers edge after approval)
384
+ const edge = commandViewData.edges.find(
385
+ (e) => e.source === 'approval-high-value' && e.target === 'order-workflow'
386
+ )
387
+ expect(edge).toBeDefined()
388
+ expect(edge?.relationship).toBe('triggers')
389
+ })
390
+
391
+ it('does NOT generate edges from triggeredBy (removed)', () => {
392
+ // No edges should exist with triggeredBy as source field (field was removed)
393
+ const allEdges = commandViewData.edges
394
+
395
+ // Verify no edge uses triggeredBy logic
396
+ // In old system, external-zapier-notify might have had triggeredBy: { agents: ['order-agent'] }
397
+ // which would create order-agent -> external-zapier-notify edge
398
+ // This should NOT exist now
399
+ const oldStyleEdge = allEdges.find((e) => e.source === 'order-agent' && e.target === 'external-zapier-notify')
400
+ expect(oldStyleEdge).toBeUndefined()
401
+ })
402
+
403
+ it('all edges have valid relationship types (triggers, uses, approval)', () => {
404
+ const validRelationships = ['triggers', 'uses', 'approval']
405
+
406
+ for (const edge of commandViewData.edges) {
407
+ expect(validRelationships).toContain(edge.relationship)
408
+ }
409
+ })
410
+
411
+ it('no edges have relationship type invokes', () => {
412
+ // 'invokes' was renamed to 'triggers' - ensure no old relationship type exists
413
+ const invokesEdges = commandViewData.edges.filter(
414
+ (e) => (e as unknown as { relationship: string }).relationship === 'invokes'
415
+ )
416
+ expect(invokesEdges).toHaveLength(0)
417
+ })
418
+
419
+ it('generates uses edges from relationships.uses', () => {
420
+ // order-agent uses integration-shopify
421
+ const edge1 = commandViewData.edges.find((e) => e.source === 'order-agent' && e.target === 'integration-shopify')
422
+ expect(edge1).toBeDefined()
423
+ expect(edge1?.relationship).toBe('uses')
424
+
425
+ // support-agent uses integration-zendesk
426
+ const edge2 = commandViewData.edges.find(
427
+ (e) => e.source === 'support-agent' && e.target === 'integration-zendesk'
428
+ )
429
+ expect(edge2).toBeDefined()
430
+ expect(edge2?.relationship).toBe('uses')
431
+ })
432
+
433
+ it('generates uses edges from external.uses', () => {
434
+ // external-n8n-sync uses integration-zendesk
435
+ const edge = commandViewData.edges.find(
436
+ (e) => e.source === 'external-n8n-sync' && e.target === 'integration-zendesk'
437
+ )
438
+ expect(edge).toBeDefined()
439
+ expect(edge?.relationship).toBe('uses')
440
+ })
441
+ })
442
+
443
+ // ============================================================================
444
+ // Serialization Roundtrip Tests
445
+ // ============================================================================
446
+
447
+ describe('Serialization Roundtrip', () => {
448
+ let registry: ResourceRegistry
449
+
450
+ beforeAll(() => {
451
+ const testOrg = createTestOrganization()
452
+ registry = new ResourceRegistry({ 'test-org': testOrg })
453
+ })
454
+
455
+ it('serialized data preserves all ResourceDefinition fields', () => {
456
+ const resourceList = registry.getResourceList('test-org')
457
+
458
+ for (const workflow of resourceList.workflows) {
459
+ expect(workflow.resourceId).toBeDefined()
460
+ expect(workflow.name).toBeDefined()
461
+ expect(workflow.description).toBeDefined()
462
+ expect(workflow.version).toBeDefined()
463
+ expect(workflow.type).toBe('workflow')
464
+ expect(workflow.status).toBeDefined()
465
+ }
466
+
467
+ for (const agent of resourceList.agents) {
468
+ expect(agent.resourceId).toBeDefined()
469
+ expect(agent.name).toBeDefined()
470
+ expect(agent.description).toBeDefined()
471
+ expect(agent.version).toBeDefined()
472
+ expect(agent.type).toBe('agent')
473
+ expect(agent.status).toBeDefined()
474
+ // sessionCapable must survive serialization onto the resource list (not just
475
+ // command-view) so the external /resources endpoint and `agent:list` can read it.
476
+ expect(typeof agent.sessionCapable).toBe('boolean')
477
+ }
478
+ })
479
+
480
+ it('command view workflows have resourceId, version, type', () => {
481
+ const commandViewData = registry.getCommandViewData('test-org')
482
+
483
+ expect(commandViewData.workflows.length).toBeGreaterThan(0)
484
+ for (const workflow of commandViewData.workflows) {
485
+ expect(workflow.resourceId).toBeDefined()
486
+ expect(workflow.version).toBeDefined()
487
+ expect(workflow.type).toBe('workflow')
488
+ }
489
+ })
490
+
491
+ it('command view agents have resourceId, version, type', () => {
492
+ const commandViewData = registry.getCommandViewData('test-org')
493
+
494
+ expect(commandViewData.agents.length).toBeGreaterThan(0)
495
+ for (const agent of commandViewData.agents) {
496
+ expect(agent.resourceId).toBeDefined()
497
+ expect(agent.version).toBeDefined()
498
+ expect(agent.type).toBe('agent')
499
+ }
500
+ })
501
+
502
+ it('command view data includes all node types', () => {
503
+ const commandViewData = registry.getCommandViewData('test-org')
504
+
505
+ expect(commandViewData.workflows.length).toBeGreaterThan(0)
506
+ expect(commandViewData.agents.length).toBeGreaterThan(0)
507
+ expect(commandViewData.triggers.length).toBeGreaterThan(0)
508
+ expect(commandViewData.integrations.length).toBeGreaterThan(0)
509
+ expect(commandViewData.externalResources.length).toBeGreaterThan(0)
510
+ expect(commandViewData.humanCheckpoints.length).toBeGreaterThan(0)
511
+ })
512
+
513
+ it('command view edges reference valid node resourceIds', () => {
514
+ const commandViewData = registry.getCommandViewData('test-org')
515
+
516
+ // Collect all valid node IDs
517
+ const validNodeIds = new Set([
518
+ ...commandViewData.workflows.map((w) => w.resourceId),
519
+ ...commandViewData.agents.map((a) => a.resourceId),
520
+ ...commandViewData.triggers.map((t) => t.resourceId),
521
+ ...commandViewData.integrations.map((i) => i.resourceId),
522
+ ...commandViewData.externalResources.map((e) => e.resourceId),
523
+ ...commandViewData.humanCheckpoints.map((h) => h.resourceId)
524
+ ])
525
+
526
+ // Verify all edges reference valid nodes
527
+ for (const edge of commandViewData.edges) {
528
+ expect(validNodeIds.has(edge.source)).toBe(true)
529
+ expect(validNodeIds.has(edge.target)).toBe(true)
530
+ }
531
+ })
532
+
533
+ it('serialized definitions match original config', () => {
534
+ const workflowDef = registry.getSerializedDefinition('test-org', 'order-workflow')
535
+ expect(workflowDef).toBeDefined()
536
+ expect(workflowDef?.config.resourceId).toBe('order-workflow')
537
+ expect(workflowDef?.config.name).toBe('Workflow order-workflow')
538
+ expect(workflowDef?.config.type).toBe('workflow')
539
+
540
+ const agentDef = registry.getSerializedDefinition('test-org', 'order-agent')
541
+ expect(agentDef).toBeDefined()
542
+ expect(agentDef?.config.resourceId).toBe('order-agent')
543
+ expect(agentDef?.config.name).toBe('Agent order-agent')
544
+ expect(agentDef?.config.type).toBe('agent')
545
+ })
546
+ })
547
+
548
+ // ============================================================================
549
+ // Validation Integration Tests
550
+ // ============================================================================
551
+
552
+ describe('Validation Integration', () => {
553
+ it('throws for trigger triggering non-existent resource', () => {
554
+ const invalidOrg: DeploymentSpec = {
555
+ workflows: [createMockWorkflow('valid-workflow')],
556
+ triggers: [
557
+ {
558
+ resourceId: 'trigger-invalid',
559
+ type: 'trigger',
560
+ triggerType: 'webhook',
561
+ version: '1.0.0',
562
+ name: 'Invalid Trigger',
563
+ description: 'Triggers non-existent workflow',
564
+ status: 'dev'
565
+ }
566
+ ],
567
+ relationships: {
568
+ 'trigger-invalid': {
569
+ triggers: { workflows: ['non-existent-workflow'] }
570
+ }
571
+ }
572
+ }
573
+
574
+ expect(() => {
575
+ new ResourceRegistry({ 'test-org': invalidOrg })
576
+ }).toThrow()
577
+ expect(() => {
578
+ new ResourceRegistry({ 'test-org': invalidOrg })
579
+ }).toThrow('non-existent-workflow')
580
+ })
581
+
582
+ it('throws for external resource with conflicting ID', () => {
583
+ const invalidOrg: DeploymentSpec = {
584
+ workflows: [createMockWorkflow('conflict-id')],
585
+ externalResources: [
586
+ {
587
+ resourceId: 'conflict-id', // Same as workflow
588
+ type: 'external',
589
+ version: '1.0.0',
590
+ platform: 'n8n',
591
+ name: 'Conflicting External',
592
+ description: 'Has same ID as workflow',
593
+ status: 'dev'
594
+ }
595
+ ]
596
+ }
597
+
598
+ expect(() => {
599
+ new ResourceRegistry({ 'test-org': invalidOrg })
600
+ }).toThrow("External resource ID 'conflict-id' conflicts with internal resource ID")
601
+ })
602
+
603
+ it('throws for human checkpoint with conflicting ID', () => {
604
+ const invalidOrg: DeploymentSpec = {
605
+ workflows: [createMockWorkflow('conflict-id')], // Changed to workflow
606
+ humanCheckpoints: [
607
+ {
608
+ resourceId: 'conflict-id', // Same as workflow
609
+ type: 'human',
610
+ version: '1.0.0',
611
+ name: 'Conflicting Checkpoint',
612
+ description: 'Has same ID as workflow',
613
+ status: 'dev'
614
+ }
615
+ ]
616
+ }
617
+
618
+ expect(() => {
619
+ new ResourceRegistry({ 'test-org': invalidOrg })
620
+ }).toThrow("Human checkpoint ID 'conflict-id' conflicts with internal resource ID")
621
+ })
622
+
623
+ it('throws for relationship to non-existent integration', () => {
624
+ const invalidOrg: DeploymentSpec = {
625
+ agents: [createMockAgent('test-agent')],
626
+ relationships: {
627
+ 'test-agent': {
628
+ uses: { integrations: ['non-existent-integration'] }
629
+ }
630
+ }
631
+ }
632
+
633
+ expect(() => {
634
+ new ResourceRegistry({ 'test-org': invalidOrg })
635
+ }).toThrow()
636
+ expect(() => {
637
+ new ResourceRegistry({ 'test-org': invalidOrg })
638
+ }).toThrow('non-existent-integration')
639
+ })
640
+
641
+ it('validates relationship graph integrity', () => {
642
+ const invalidOrg: DeploymentSpec = {
643
+ workflows: [createMockWorkflow('workflow-1')],
644
+ relationships: {
645
+ 'workflow-1': {
646
+ triggers: { workflows: ['non-existent-workflow-2'] }
647
+ }
648
+ }
649
+ }
650
+
651
+ expect(() => {
652
+ new ResourceRegistry({ 'test-org': invalidOrg })
653
+ }).toThrow()
654
+ })
655
+
656
+ it('allows valid relationships without errors', () => {
657
+ const validOrg: DeploymentSpec = {
658
+ workflows: [createMockWorkflow('workflow-1'), createMockWorkflow('workflow-2')],
659
+ agents: [createMockAgent('agent-1')],
660
+ integrations: [
661
+ {
662
+ resourceId: 'integration-1',
663
+ type: 'integration',
664
+ version: '1.0.0',
665
+ status: 'dev',
666
+ provider: 'webhook',
667
+ credentialName: 'webhook-cred',
668
+ name: 'Webhook Integration',
669
+ description: 'Test webhook'
670
+ }
671
+ ],
672
+ relationships: {
673
+ 'workflow-1': {
674
+ triggers: { workflows: ['workflow-2'], agents: ['agent-1'] },
675
+ uses: { integrations: ['integration-1'] }
676
+ }
677
+ }
678
+ }
679
+
680
+ expect(() => {
681
+ new ResourceRegistry({ 'test-org': validOrg })
682
+ }).not.toThrow()
683
+ })
684
+ })
685
+
686
+ // ============================================================================
687
+ // Multi-Organization Isolation Tests
688
+ // ============================================================================
689
+
690
+ describe('Multi-Organization Isolation', () => {
691
+ it('resources from different orgs are isolated', () => {
692
+ const org1: DeploymentSpec = {
693
+ workflows: [createMockWorkflow('workflow-org1')],
694
+ agents: [createMockAgent('agent-org1')]
695
+ }
696
+
697
+ const org2: DeploymentSpec = {
698
+ workflows: [createMockWorkflow('workflow-org2')],
699
+ agents: [createMockAgent('agent-org2')]
700
+ }
701
+
702
+ const registry = new ResourceRegistry({
703
+ org1,
704
+ org2
705
+ })
706
+
707
+ // Org1 resources
708
+ expect(registry.getResourceDefinition('org1', 'workflow-org1')).toBeDefined()
709
+ expect(registry.getResourceDefinition('org1', 'agent-org1')).toBeDefined()
710
+ expect(registry.getResourceDefinition('org1', 'workflow-org2')).toBeNull()
711
+ expect(registry.getResourceDefinition('org1', 'agent-org2')).toBeNull()
712
+
713
+ // Org2 resources
714
+ expect(registry.getResourceDefinition('org2', 'workflow-org2')).toBeDefined()
715
+ expect(registry.getResourceDefinition('org2', 'agent-org2')).toBeDefined()
716
+ expect(registry.getResourceDefinition('org2', 'workflow-org1')).toBeNull()
717
+ expect(registry.getResourceDefinition('org2', 'agent-org1')).toBeNull()
718
+ })
719
+
720
+ it('triggers can only reference resources in same org', () => {
721
+ const org1: DeploymentSpec = {
722
+ workflows: [createMockWorkflow('workflow-org1')],
723
+ triggers: [
724
+ {
725
+ resourceId: 'trigger-org1',
726
+ type: 'trigger',
727
+ triggerType: 'manual',
728
+ version: '1.0.0',
729
+ name: 'Org1 Trigger',
730
+ description: 'Triggers workflow in org2 (invalid)',
731
+ status: 'dev'
732
+ }
733
+ ],
734
+ relationships: {
735
+ 'trigger-org1': {
736
+ triggers: { workflows: ['workflow-org2'] } // References org2 workflow - should fail
737
+ }
738
+ }
739
+ }
740
+
741
+ const org2: DeploymentSpec = {
742
+ workflows: [createMockWorkflow('workflow-org2')]
743
+ }
744
+
745
+ expect(() => {
746
+ new ResourceRegistry({ org1, org2 })
747
+ }).toThrow('workflow-org2')
748
+ })
749
+
750
+ it('command view data is organization-specific', () => {
751
+ // Create different orgs with different resource IDs
752
+ const org1: DeploymentSpec = {
753
+ workflows: [createMockWorkflow('org1-workflow')],
754
+ agents: [createMockAgent('org1-agent')]
755
+ }
756
+
757
+ const org2: DeploymentSpec = {
758
+ workflows: [createMockWorkflow('org2-workflow')],
759
+ agents: [createMockAgent('org2-agent')]
760
+ }
761
+
762
+ const registry = new ResourceRegistry({ org1, org2 })
763
+
764
+ const org1Data = registry.getCommandViewData('org1')
765
+ const org2Data = registry.getCommandViewData('org2')
766
+
767
+ // Both orgs should have same number of resources
768
+ // but data should be independent instances
769
+ expect(org1Data).not.toBe(org2Data)
770
+ expect(org1Data.workflows.length).toBe(1)
771
+ expect(org2Data.workflows.length).toBe(1)
772
+
773
+ // Verify isolation - org1 nodes should not appear in org2 data
774
+ const org1NodeIds = new Set([
775
+ ...org1Data.workflows.map((w) => w.resourceId),
776
+ ...org1Data.agents.map((a) => a.resourceId)
777
+ ])
778
+
779
+ const org2NodeIds = new Set([
780
+ ...org2Data.workflows.map((w) => w.resourceId),
781
+ ...org2Data.agents.map((a) => a.resourceId)
782
+ ])
783
+
784
+ // Verify no overlap in resource IDs
785
+ for (const nodeId of org1NodeIds) {
786
+ expect(org2NodeIds.has(nodeId)).toBe(false)
787
+ }
788
+ for (const nodeId of org2NodeIds) {
789
+ expect(org1NodeIds.has(nodeId)).toBe(false)
790
+ }
791
+
792
+ // Verify edges reference only their own org's nodes
793
+ for (const edge of org1Data.edges) {
794
+ // org1 edges should only reference org1 nodes
795
+ expect(
796
+ org1NodeIds.has(edge.source) ||
797
+ org1Data.triggers.some((t) => t.resourceId === edge.source) ||
798
+ org1Data.integrations.some((i) => i.resourceId === edge.source)
799
+ ).toBe(true)
800
+ expect(
801
+ org1NodeIds.has(edge.target) ||
802
+ org1Data.triggers.some((t) => t.resourceId === edge.target) ||
803
+ org1Data.integrations.some((i) => i.resourceId === edge.target)
804
+ ).toBe(true)
805
+ }
806
+
807
+ for (const edge of org2Data.edges) {
808
+ // org2 edges should not reference org1 nodes
809
+ expect(org1NodeIds.has(edge.source)).toBe(false)
810
+ expect(org1NodeIds.has(edge.target)).toBe(false)
811
+ }
812
+ })
813
+
814
+ it('allows same resourceId across different organizations', () => {
815
+ const org1: DeploymentSpec = {
816
+ workflows: [createMockWorkflow('shared-id')]
817
+ }
818
+
819
+ const org2: DeploymentSpec = {
820
+ workflows: [createMockWorkflow('shared-id')]
821
+ }
822
+
823
+ expect(() => {
824
+ new ResourceRegistry({ org1, org2 })
825
+ }).not.toThrow()
826
+
827
+ const registry = new ResourceRegistry({ org1, org2 })
828
+
829
+ const org1Resource = registry.getResourceDefinition('org1', 'shared-id')
830
+ const org2Resource = registry.getResourceDefinition('org2', 'shared-id')
831
+
832
+ expect(org1Resource).toBeDefined()
833
+ expect(org2Resource).toBeDefined()
834
+ expect(org1Resource).not.toBe(org2Resource) // Different instances
835
+ })
836
+ })
837
+
838
+ // ============================================================================
839
+ // Runtime Registration Relationship Tests
840
+ // ============================================================================
841
+
842
+ describe('Runtime Registration Relationships', () => {
843
+ const remoteConfig = {
844
+ storagePath: 'test-org-id/test-deploy-id/bundle.js',
845
+ deploymentId: 'deploy-123'
846
+ }
847
+
848
+ it('registerOrganization merges relationships from stubs into existing org', () => {
849
+ // Start with a static org that has a workflow and relationships
850
+ const staticOrg: DeploymentSpec = {
851
+ workflows: [createMockWorkflow('static-workflow')],
852
+ agents: [createMockAgent('static-agent')],
853
+ integrations: [
854
+ {
855
+ resourceId: 'integration-crm',
856
+ type: 'integration',
857
+ version: '1.0.0',
858
+ status: 'dev',
859
+ provider: 'crm',
860
+ credentialName: 'crm-cred',
861
+ name: 'CRM',
862
+ description: 'CRM integration'
863
+ }
864
+ ],
865
+ relationships: {
866
+ 'static-workflow': {
867
+ triggers: { agents: ['static-agent'] }
868
+ }
869
+ }
870
+ }
871
+
872
+ const registry = new ResourceRegistry({ 'test-org': staticOrg })
873
+
874
+ // Deploy stubs with their own relationships
875
+ const stubs: DeploymentSpec = {
876
+ workflows: [createMockWorkflow('remote-workflow')],
877
+ integrations: [
878
+ {
879
+ resourceId: 'integration-crm',
880
+ type: 'integration',
881
+ version: '1.0.0',
882
+ status: 'dev',
883
+ provider: 'crm',
884
+ credentialName: 'crm-cred',
885
+ name: 'CRM',
886
+ description: 'CRM integration'
887
+ }
888
+ ],
889
+ relationships: {
890
+ 'remote-workflow': {
891
+ uses: { integrations: ['integration-crm'] }
892
+ }
893
+ }
894
+ }
895
+
896
+ registry.registerOrganization('test-org', stubs, remoteConfig)
897
+
898
+ // Both static and remote relationships should exist
899
+ const relationships = registry.getRelationships('test-org')
900
+ expect(relationships).toBeDefined()
901
+ expect(relationships!['static-workflow']).toBeDefined()
902
+ expect(relationships!['static-workflow'].triggers?.agents).toContain('static-agent')
903
+ expect(relationships!['remote-workflow']).toBeDefined()
904
+ expect(relationships!['remote-workflow'].uses?.integrations).toContain('integration-crm')
905
+ })
906
+
907
+ it('unregisterOrganization removes remote resource relationships, preserves static ones', () => {
908
+ const staticOrg: DeploymentSpec = {
909
+ workflows: [createMockWorkflow('static-workflow')],
910
+ agents: [createMockAgent('static-agent')],
911
+ relationships: {
912
+ 'static-workflow': {
913
+ triggers: { agents: ['static-agent'] }
914
+ }
915
+ }
916
+ }
917
+
918
+ const registry = new ResourceRegistry({ 'test-org': staticOrg })
919
+
920
+ // Register remote resources with relationships
921
+ const stubs: DeploymentSpec = {
922
+ workflows: [createMockWorkflow('remote-workflow')],
923
+ relationships: {
924
+ 'remote-workflow': {
925
+ triggers: { agents: ['static-agent'] }
926
+ }
927
+ }
928
+ }
929
+
930
+ registry.registerOrganization('test-org', stubs, remoteConfig)
931
+
932
+ // Verify both exist
933
+ expect(registry.getRelationships('test-org')!['remote-workflow']).toBeDefined()
934
+ expect(registry.getRelationships('test-org')!['static-workflow']).toBeDefined()
935
+
936
+ // Unregister remote resources
937
+ registry.unregisterOrganization('test-org')
938
+
939
+ // Static relationships preserved, remote relationships removed
940
+ const relationships = registry.getRelationships('test-org')
941
+ expect(relationships).toBeDefined()
942
+ expect(relationships!['static-workflow']).toBeDefined()
943
+ expect(relationships!['remote-workflow']).toBeUndefined()
944
+ })
945
+
946
+ it('getCommandViewData returns correct edges after registering with relationships', () => {
947
+ const staticOrg: DeploymentSpec = {
948
+ workflows: [createMockWorkflow('static-workflow')],
949
+ agents: [createMockAgent('static-agent')],
950
+ integrations: [
951
+ {
952
+ resourceId: 'integration-crm',
953
+ type: 'integration',
954
+ version: '1.0.0',
955
+ status: 'dev',
956
+ provider: 'crm',
957
+ credentialName: 'crm-cred',
958
+ name: 'CRM',
959
+ description: 'CRM integration'
960
+ }
961
+ ]
962
+ }
963
+
964
+ const registry = new ResourceRegistry({ 'test-org': staticOrg })
965
+
966
+ // No relationship edges before registration
967
+ const beforeData = registry.getCommandViewData('test-org')
968
+ const beforeEdges = beforeData.edges.filter(
969
+ (e) => e.source === 'remote-workflow' || e.target === 'remote-workflow'
970
+ )
971
+ expect(beforeEdges).toHaveLength(0)
972
+
973
+ // Register remote stubs with relationships
974
+ const stubs: DeploymentSpec = {
975
+ workflows: [createMockWorkflow('remote-workflow')],
976
+ integrations: [
977
+ {
978
+ resourceId: 'integration-crm',
979
+ type: 'integration',
980
+ version: '1.0.0',
981
+ status: 'dev',
982
+ provider: 'crm',
983
+ credentialName: 'crm-cred',
984
+ name: 'CRM',
985
+ description: 'CRM integration'
986
+ }
987
+ ],
988
+ relationships: {
989
+ 'remote-workflow': {
990
+ triggers: { workflows: ['static-workflow'] },
991
+ uses: { integrations: ['integration-crm'] }
992
+ }
993
+ }
994
+ }
995
+
996
+ registry.registerOrganization('test-org', stubs, remoteConfig)
997
+
998
+ // Command View should now include edges from remote relationships
999
+ const afterData = registry.getCommandViewData('test-org')
1000
+
1001
+ const triggersEdge = afterData.edges.find((e) => e.source === 'remote-workflow' && e.target === 'static-workflow')
1002
+ expect(triggersEdge).toBeDefined()
1003
+ expect(triggersEdge?.relationship).toBe('triggers')
1004
+
1005
+ const usesEdge = afterData.edges.find((e) => e.source === 'remote-workflow' && e.target === 'integration-crm')
1006
+ expect(usesEdge).toBeDefined()
1007
+ expect(usesEdge?.relationship).toBe('uses')
1008
+ })
1009
+ })
1010
+ })