|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 4 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 5 | +import { ObjectQL } from './engine.js'; |
| 6 | + |
| 7 | +describe('ObjectStackProtocolImplementation - Dynamic Service Discovery', () => { |
| 8 | + let protocol: ObjectStackProtocolImplementation; |
| 9 | + let engine: ObjectQL; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + engine = new ObjectQL(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return unavailable auth service when no services registered', async () => { |
| 16 | + // Create protocol without service registry |
| 17 | + protocol = new ObjectStackProtocolImplementation(engine); |
| 18 | + |
| 19 | + const discovery = await protocol.getDiscovery(); |
| 20 | + |
| 21 | + expect(discovery.services.auth).toBeDefined(); |
| 22 | + expect(discovery.services.auth.enabled).toBe(false); |
| 23 | + expect(discovery.services.auth.status).toBe('unavailable'); |
| 24 | + expect(discovery.services.auth.message).toContain('plugin-auth'); |
| 25 | + expect(discovery.capabilities.workflow).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return available auth service when auth is registered', async () => { |
| 29 | + // Mock service registry with auth service |
| 30 | + const mockServices = new Map<string, any>(); |
| 31 | + mockServices.set('auth', { /* mock auth service */ }); |
| 32 | + |
| 33 | + protocol = new ObjectStackProtocolImplementation(engine, () => mockServices); |
| 34 | + |
| 35 | + const discovery = await protocol.getDiscovery(); |
| 36 | + |
| 37 | + expect(discovery.services.auth).toBeDefined(); |
| 38 | + expect(discovery.services.auth.enabled).toBe(true); |
| 39 | + expect(discovery.services.auth.status).toBe('available'); |
| 40 | + expect(discovery.services.auth.route).toBe('/api/v1/auth'); |
| 41 | + expect(discovery.services.auth.provider).toBe('plugin-auth'); |
| 42 | + expect(discovery.endpoints.auth).toBe('/api/v1/auth'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return available workflow when automation service is registered', async () => { |
| 46 | + const mockServices = new Map<string, any>(); |
| 47 | + mockServices.set('automation', { /* mock automation service */ }); |
| 48 | + |
| 49 | + protocol = new ObjectStackProtocolImplementation(engine, () => mockServices); |
| 50 | + |
| 51 | + const discovery = await protocol.getDiscovery(); |
| 52 | + |
| 53 | + expect(discovery.services.automation).toBeDefined(); |
| 54 | + expect(discovery.services.automation.enabled).toBe(true); |
| 55 | + expect(discovery.services.automation.status).toBe('available'); |
| 56 | + expect(discovery.capabilities.workflow).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should return multiple available services when registered', async () => { |
| 60 | + const mockServices = new Map<string, any>(); |
| 61 | + mockServices.set('auth', {}); |
| 62 | + mockServices.set('realtime', {}); |
| 63 | + mockServices.set('ai', {}); |
| 64 | + |
| 65 | + protocol = new ObjectStackProtocolImplementation(engine, () => mockServices); |
| 66 | + |
| 67 | + const discovery = await protocol.getDiscovery(); |
| 68 | + |
| 69 | + // Check auth |
| 70 | + expect(discovery.services.auth.enabled).toBe(true); |
| 71 | + expect(discovery.services.auth.status).toBe('available'); |
| 72 | + |
| 73 | + // Check realtime |
| 74 | + expect(discovery.services.realtime.enabled).toBe(true); |
| 75 | + expect(discovery.services.realtime.status).toBe('available'); |
| 76 | + expect(discovery.capabilities.websockets).toBe(true); |
| 77 | + |
| 78 | + // Check AI |
| 79 | + expect(discovery.services.ai.enabled).toBe(true); |
| 80 | + expect(discovery.services.ai.status).toBe('available'); |
| 81 | + expect(discovery.capabilities.ai).toBe(true); |
| 82 | + |
| 83 | + // Endpoints should include available services |
| 84 | + expect(discovery.endpoints.auth).toBe('/api/v1/auth'); |
| 85 | + expect(discovery.endpoints.realtime).toBe('/api/v1/realtime'); |
| 86 | + expect(discovery.endpoints.ai).toBe('/api/v1/ai'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should always show core services as available', async () => { |
| 90 | + protocol = new ObjectStackProtocolImplementation(engine); |
| 91 | + |
| 92 | + const discovery = await protocol.getDiscovery(); |
| 93 | + |
| 94 | + // Core services should always be available |
| 95 | + expect(discovery.services.metadata.enabled).toBe(true); |
| 96 | + expect(discovery.services.metadata.status).toBe('degraded'); |
| 97 | + expect(discovery.services.data.enabled).toBe(true); |
| 98 | + expect(discovery.services.data.status).toBe('available'); |
| 99 | + expect(discovery.services.analytics.enabled).toBe(true); |
| 100 | + expect(discovery.services.analytics.status).toBe('available'); |
| 101 | + |
| 102 | + // Core capabilities |
| 103 | + expect(discovery.capabilities.analytics).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should map file-storage service to storage endpoint', async () => { |
| 107 | + const mockServices = new Map<string, any>(); |
| 108 | + mockServices.set('file-storage', {}); |
| 109 | + |
| 110 | + protocol = new ObjectStackProtocolImplementation(engine, () => mockServices); |
| 111 | + |
| 112 | + const discovery = await protocol.getDiscovery(); |
| 113 | + |
| 114 | + expect(discovery.services['file-storage'].enabled).toBe(true); |
| 115 | + expect(discovery.services['file-storage'].status).toBe('available'); |
| 116 | + expect(discovery.endpoints.storage).toBe('/api/v1/storage'); |
| 117 | + expect(discovery.capabilities.files).toBe(true); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle workflow capability from either automation or workflow service', async () => { |
| 121 | + // Test with workflow service |
| 122 | + const mockServicesWithWorkflow = new Map<string, any>(); |
| 123 | + mockServicesWithWorkflow.set('workflow', {}); |
| 124 | + |
| 125 | + protocol = new ObjectStackProtocolImplementation(engine, () => mockServicesWithWorkflow); |
| 126 | + let discovery = await protocol.getDiscovery(); |
| 127 | + expect(discovery.capabilities.workflow).toBe(true); |
| 128 | + |
| 129 | + // Test with automation service |
| 130 | + const mockServicesWithAutomation = new Map<string, any>(); |
| 131 | + mockServicesWithAutomation.set('automation', {}); |
| 132 | + |
| 133 | + protocol = new ObjectStackProtocolImplementation(engine, () => mockServicesWithAutomation); |
| 134 | + discovery = await protocol.getDiscovery(); |
| 135 | + expect(discovery.capabilities.workflow).toBe(true); |
| 136 | + }); |
| 137 | +}); |
0 commit comments