|
| 1 | +import http from 'http'; |
| 2 | + |
| 3 | +import { createMockContext } from '@shopify/jest-koa-mocks'; |
| 4 | + |
| 5 | +import WorkflowExecutorProxyRoute from '../../../src/routes/workflow/workflow-executor-proxy'; |
| 6 | +import { RouteType } from '../../../src/types'; |
| 7 | +import * as factories from '../../__factories__'; |
| 8 | + |
| 9 | +describe('WorkflowExecutorProxyRoute', () => { |
| 10 | + const options = factories.forestAdminHttpDriverOptions.build(); |
| 11 | + const services = factories.forestAdminHttpDriverServices.build(); |
| 12 | + const router = factories.router.mockAllMethods().build(); |
| 13 | + |
| 14 | + let executorServer: http.Server; |
| 15 | + let executorPort: number; |
| 16 | + |
| 17 | + // Start a real HTTP server to act as the workflow executor |
| 18 | + beforeAll(async () => { |
| 19 | + executorServer = http.createServer((req, res) => { |
| 20 | + const chunks: Uint8Array[] = []; |
| 21 | + req.on('data', chunk => chunks.push(chunk)); |
| 22 | + req.on('end', () => { |
| 23 | + const body = Buffer.concat(chunks).toString('utf-8'); |
| 24 | + |
| 25 | + res.setHeader('Content-Type', 'application/json'); |
| 26 | + |
| 27 | + if (req.url?.includes('not-found')) { |
| 28 | + res.writeHead(404); |
| 29 | + res.end(JSON.stringify({ error: 'Run not found or unavailable' })); |
| 30 | + } else if (req.method === 'GET' && req.url?.match(/^\/runs\/[\w-]+$/)) { |
| 31 | + res.writeHead(200); |
| 32 | + res.end(JSON.stringify({ steps: [{ stepId: 's1', status: 'success' }] })); |
| 33 | + } else if (req.method === 'POST' && req.url?.match(/^\/runs\/[\w-]+\/trigger$/)) { |
| 34 | + res.writeHead(200); |
| 35 | + res.end(JSON.stringify({ triggered: true })); |
| 36 | + } else if ( |
| 37 | + req.method === 'PATCH' && |
| 38 | + req.url?.match(/^\/runs\/[\w-]+\/steps\/\d+\/pending-data$/) |
| 39 | + ) { |
| 40 | + const parsed = body ? JSON.parse(body) : {}; |
| 41 | + res.writeHead(200); |
| 42 | + res.end(JSON.stringify({ updated: true, received: parsed })); |
| 43 | + } else { |
| 44 | + res.writeHead(404); |
| 45 | + res.end(JSON.stringify({ error: 'Not found' })); |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + await new Promise<void>((resolve, reject) => { |
| 51 | + executorServer.listen(0, () => { |
| 52 | + executorPort = (executorServer.address() as { port: number }).port; |
| 53 | + resolve(); |
| 54 | + }); |
| 55 | + executorServer.on('error', reject); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + afterAll(async () => { |
| 60 | + await new Promise<void>((resolve, reject) => { |
| 61 | + executorServer.close(err => (err ? reject(err) : resolve())); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + beforeEach(() => { |
| 66 | + jest.clearAllMocks(); |
| 67 | + }); |
| 68 | + |
| 69 | + const buildOptions = (url: string) => |
| 70 | + factories.forestAdminHttpDriverOptions.build({ workflowExecutorUrl: url }); |
| 71 | + |
| 72 | + describe('constructor', () => { |
| 73 | + test('should have RouteType.PrivateRoute', () => { |
| 74 | + const route = new WorkflowExecutorProxyRoute(services, buildOptions('http://localhost:4001')); |
| 75 | + |
| 76 | + expect(route.type).toBe(RouteType.PrivateRoute); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('setupRoutes', () => { |
| 81 | + test('should register GET, POST and PATCH routes', () => { |
| 82 | + const route = new WorkflowExecutorProxyRoute(services, buildOptions('http://localhost:4001')); |
| 83 | + route.setupRoutes(router); |
| 84 | + |
| 85 | + expect(router.get).toHaveBeenCalledWith( |
| 86 | + '/_internal/workflow-executions/:runId', |
| 87 | + expect.any(Function), |
| 88 | + ); |
| 89 | + expect(router.post).toHaveBeenCalledWith( |
| 90 | + '/_internal/workflow-executions/:runId/trigger', |
| 91 | + expect.any(Function), |
| 92 | + ); |
| 93 | + expect(router.patch).toHaveBeenCalledWith( |
| 94 | + '/_internal/workflow-executions/:runId/steps/:stepIndex/pending-data', |
| 95 | + expect.any(Function), |
| 96 | + ); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('handleProxy', () => { |
| 101 | + test('should forward GET /runs/:runId and return executor response', async () => { |
| 102 | + const route = new WorkflowExecutorProxyRoute( |
| 103 | + services, |
| 104 | + buildOptions(`http://localhost:${executorPort}`), |
| 105 | + ); |
| 106 | + |
| 107 | + const context = createMockContext({ |
| 108 | + customProperties: { params: { runId: 'run-123' } }, |
| 109 | + }); |
| 110 | + Object.defineProperty(context, 'path', { |
| 111 | + value: '/_internal/workflow-executions/run-123', |
| 112 | + }); |
| 113 | + |
| 114 | + await (route as any).handleProxy(context); |
| 115 | + |
| 116 | + expect(context.response.status).toBe(200); |
| 117 | + expect(context.response.body).toEqual({ |
| 118 | + steps: [{ stepId: 's1', status: 'success' }], |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + test('should forward POST /runs/:runId/trigger and return executor response', async () => { |
| 123 | + const route = new WorkflowExecutorProxyRoute( |
| 124 | + services, |
| 125 | + buildOptions(`http://localhost:${executorPort}`), |
| 126 | + ); |
| 127 | + |
| 128 | + const context = createMockContext({ |
| 129 | + method: 'POST', |
| 130 | + customProperties: { params: { runId: 'run-456' } }, |
| 131 | + }); |
| 132 | + Object.defineProperty(context, 'path', { |
| 133 | + value: '/_internal/workflow-executions/run-456/trigger', |
| 134 | + }); |
| 135 | + |
| 136 | + await (route as any).handleProxy(context); |
| 137 | + |
| 138 | + expect(context.response.status).toBe(200); |
| 139 | + expect(context.response.body).toEqual({ triggered: true }); |
| 140 | + }); |
| 141 | + |
| 142 | + test('should forward error status from executor', async () => { |
| 143 | + const route = new WorkflowExecutorProxyRoute( |
| 144 | + services, |
| 145 | + buildOptions(`http://localhost:${executorPort}`), |
| 146 | + ); |
| 147 | + |
| 148 | + const context = createMockContext({ |
| 149 | + customProperties: { params: { runId: 'not-found' } }, |
| 150 | + }); |
| 151 | + Object.defineProperty(context, 'path', { |
| 152 | + value: '/_internal/workflow-executions/not-found', |
| 153 | + }); |
| 154 | + |
| 155 | + await (route as any).handleProxy(context); |
| 156 | + |
| 157 | + expect(context.response.status).toBe(404); |
| 158 | + expect(context.response.body).toEqual({ error: 'Run not found or unavailable' }); |
| 159 | + }); |
| 160 | + |
| 161 | + test('should forward PATCH pending-data and pass request body', async () => { |
| 162 | + const route = new WorkflowExecutorProxyRoute( |
| 163 | + services, |
| 164 | + buildOptions(`http://localhost:${executorPort}`), |
| 165 | + ); |
| 166 | + |
| 167 | + const context = createMockContext({ |
| 168 | + method: 'PATCH', |
| 169 | + customProperties: { params: { runId: 'run-789', stepIndex: '2' } }, |
| 170 | + requestBody: { fieldValues: { name: 'updated' } }, |
| 171 | + }); |
| 172 | + Object.defineProperty(context, 'path', { |
| 173 | + value: '/_internal/workflow-executions/run-789/steps/2/pending-data', |
| 174 | + }); |
| 175 | + |
| 176 | + await (route as any).handleProxy(context); |
| 177 | + |
| 178 | + expect(context.response.status).toBe(200); |
| 179 | + expect(context.response.body).toEqual({ |
| 180 | + updated: true, |
| 181 | + received: { fieldValues: { name: 'updated' } }, |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + test('should reject when executor is unreachable', async () => { |
| 186 | + const route = new WorkflowExecutorProxyRoute( |
| 187 | + services, |
| 188 | + buildOptions('http://localhost:1'), // port that should be unreachable |
| 189 | + ); |
| 190 | + |
| 191 | + const context = createMockContext({ |
| 192 | + customProperties: { params: { runId: 'run-789' } }, |
| 193 | + }); |
| 194 | + Object.defineProperty(context, 'path', { |
| 195 | + value: '/_internal/workflow-executions/run-789', |
| 196 | + }); |
| 197 | + |
| 198 | + await expect((route as any).handleProxy(context)).rejects.toThrow(); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments