|
| 1 | +/** |
| 2 | + * Tests for message servers API routes |
| 3 | + * |
| 4 | + * These tests verify the route naming consistency and response formats |
| 5 | + * for the /message-servers endpoints. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect } from 'bun:test'; |
| 9 | + |
| 10 | +describe('Message Servers API Routes', () => { |
| 11 | + describe('Route naming conventions', () => { |
| 12 | + it('should use /message-servers (plural) for collection endpoints', () => { |
| 13 | + const collectionRoutes = ['GET /message-servers', 'POST /message-servers']; |
| 14 | + |
| 15 | + collectionRoutes.forEach((route) => { |
| 16 | + expect(route).toContain('/message-servers'); |
| 17 | + expect(route).not.toContain('/servers'); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should use /message-server (singular) for current instance endpoint', () => { |
| 22 | + const currentRoute = 'GET /message-server/current'; |
| 23 | + expect(currentRoute).toContain('/message-server/current'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should use :messageServerId parameter consistently', () => { |
| 27 | + const parameterizedRoutes = [ |
| 28 | + '/message-servers/:messageServerId/agents', |
| 29 | + '/message-servers/:messageServerId/agents/:agentId', |
| 30 | + ]; |
| 31 | + |
| 32 | + parameterizedRoutes.forEach((route) => { |
| 33 | + expect(route).toContain(':messageServerId'); |
| 34 | + expect(route).not.toContain(':serverId'); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should use /agents/:agentId/message-servers for reverse lookup', () => { |
| 39 | + const reverseRoute = '/agents/:agentId/message-servers'; |
| 40 | + expect(reverseRoute).toContain('/message-servers'); |
| 41 | + expect(reverseRoute).not.toContain('/servers'); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('Response format for GET /message-server/current', () => { |
| 46 | + it('should return messageServerId in response', () => { |
| 47 | + const mockResponse = { |
| 48 | + success: true, |
| 49 | + data: { |
| 50 | + messageServerId: '00000000-0000-0000-0000-000000000000', |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + expect(mockResponse.success).toBe(true); |
| 55 | + expect(mockResponse.data).toHaveProperty('messageServerId'); |
| 56 | + expect(mockResponse.data.messageServerId).toMatch( |
| 57 | + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/ |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('Response format for GET /message-servers', () => { |
| 63 | + it('should return messageServers array in response', () => { |
| 64 | + const mockResponse = { |
| 65 | + success: true, |
| 66 | + data: { |
| 67 | + messageServers: [ |
| 68 | + { |
| 69 | + id: '00000000-0000-0000-0000-000000000000', |
| 70 | + name: 'Default Server', |
| 71 | + sourceType: 'eliza_default', |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + }; |
| 76 | + |
| 77 | + expect(mockResponse.success).toBe(true); |
| 78 | + expect(mockResponse.data).toHaveProperty('messageServers'); |
| 79 | + expect(Array.isArray(mockResponse.data.messageServers)).toBe(true); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('Response format for GET /message-servers/:messageServerId/agents', () => { |
| 84 | + it('should return messageServerId and agents array', () => { |
| 85 | + const mockResponse = { |
| 86 | + success: true, |
| 87 | + data: { |
| 88 | + messageServerId: '00000000-0000-0000-0000-000000000000', |
| 89 | + agents: ['11111111-1111-1111-1111-111111111111'], |
| 90 | + }, |
| 91 | + }; |
| 92 | + |
| 93 | + expect(mockResponse.success).toBe(true); |
| 94 | + expect(mockResponse.data).toHaveProperty('messageServerId'); |
| 95 | + expect(mockResponse.data).toHaveProperty('agents'); |
| 96 | + expect(Array.isArray(mockResponse.data.agents)).toBe(true); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('Response format for POST /message-servers/:messageServerId/agents', () => { |
| 101 | + it('should return messageServerId and agentId in response', () => { |
| 102 | + const mockResponse = { |
| 103 | + success: true, |
| 104 | + data: { |
| 105 | + messageServerId: '00000000-0000-0000-0000-000000000000', |
| 106 | + agentId: '11111111-1111-1111-1111-111111111111', |
| 107 | + message: 'Agent added to message server successfully', |
| 108 | + }, |
| 109 | + }; |
| 110 | + |
| 111 | + expect(mockResponse.success).toBe(true); |
| 112 | + expect(mockResponse.data).toHaveProperty('messageServerId'); |
| 113 | + expect(mockResponse.data).toHaveProperty('agentId'); |
| 114 | + expect(mockResponse.data).toHaveProperty('message'); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('Response format for GET /agents/:agentId/message-servers', () => { |
| 119 | + it('should return agentId and messageServers array', () => { |
| 120 | + const mockResponse = { |
| 121 | + success: true, |
| 122 | + data: { |
| 123 | + agentId: '11111111-1111-1111-1111-111111111111', |
| 124 | + messageServers: ['00000000-0000-0000-0000-000000000000'], |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + expect(mockResponse.success).toBe(true); |
| 129 | + expect(mockResponse.data).toHaveProperty('agentId'); |
| 130 | + expect(mockResponse.data).toHaveProperty('messageServers'); |
| 131 | + expect(Array.isArray(mockResponse.data.messageServers)).toBe(true); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('Deprecated routes', () => { |
| 136 | + it('should map deprecated /central-servers to /message-servers', () => { |
| 137 | + const deprecatedRoute = '/central-servers'; |
| 138 | + const newRoute = '/message-servers'; |
| 139 | + |
| 140 | + expect(deprecatedRoute).not.toBe(newRoute); |
| 141 | + expect(newRoute).toContain('message-servers'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should map deprecated /servers to /message-servers', () => { |
| 145 | + const deprecatedRoute = '/servers'; |
| 146 | + const newRoute = '/message-servers'; |
| 147 | + |
| 148 | + expect(deprecatedRoute).not.toBe(newRoute); |
| 149 | + expect(newRoute).toContain('message-servers'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should map deprecated :serverId to :messageServerId', () => { |
| 153 | + const deprecatedParam = ':serverId'; |
| 154 | + const newParam = ':messageServerId'; |
| 155 | + |
| 156 | + expect(deprecatedParam).not.toBe(newParam); |
| 157 | + expect(newParam).toContain('messageServerId'); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('RLS Security checks', () => { |
| 162 | + it('should return 403 when accessing agents for different server', () => { |
| 163 | + const errorResponse = { |
| 164 | + success: false, |
| 165 | + error: 'Cannot access agents for a different server', |
| 166 | + }; |
| 167 | + |
| 168 | + expect(errorResponse.success).toBe(false); |
| 169 | + expect(errorResponse.error).toContain('different server'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should return 403 when modifying agents for different server', () => { |
| 173 | + const errorResponse = { |
| 174 | + success: false, |
| 175 | + error: 'Cannot modify agents for a different server', |
| 176 | + }; |
| 177 | + |
| 178 | + expect(errorResponse.success).toBe(false); |
| 179 | + expect(errorResponse.error).toContain('different server'); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('Validation errors', () => { |
| 184 | + it('should return 400 for invalid messageServerId format', () => { |
| 185 | + const errorResponse = { |
| 186 | + success: false, |
| 187 | + error: 'Invalid messageServerId format', |
| 188 | + }; |
| 189 | + |
| 190 | + expect(errorResponse.success).toBe(false); |
| 191 | + expect(errorResponse.error).toContain('Invalid messageServerId'); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should return 400 for invalid agentId format', () => { |
| 195 | + const errorResponse = { |
| 196 | + success: false, |
| 197 | + error: 'Invalid messageServerId or agentId format', |
| 198 | + }; |
| 199 | + |
| 200 | + expect(errorResponse.success).toBe(false); |
| 201 | + expect(errorResponse.error).toContain('agentId'); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should return 400 for missing required fields on create', () => { |
| 205 | + const errorResponse = { |
| 206 | + success: false, |
| 207 | + error: 'Missing required fields: name, sourceType', |
| 208 | + }; |
| 209 | + |
| 210 | + expect(errorResponse.success).toBe(false); |
| 211 | + expect(errorResponse.error).toContain('Missing required fields'); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments