|
| 1 | +import { dehydrateWorkflowArguments } from '@workflow/core/serialization'; |
| 2 | +import { describe, expect, test } from 'vitest'; |
| 3 | + |
| 4 | +const deploymentUrl = process.env.DEPLOYMENT_URL || 'http://localhost:3000'; |
| 5 | + |
| 6 | +async function triggerWorkflow( |
| 7 | + workflow: string | { workflowFile: string; workflowFn: string }, |
| 8 | + args: any[] |
| 9 | +): Promise<{ runId: string }> { |
| 10 | + const url = new URL('/api/trigger', deploymentUrl); |
| 11 | + const workflowFn = |
| 12 | + typeof workflow === 'string' ? workflow : workflow.workflowFn; |
| 13 | + const workflowFile = |
| 14 | + typeof workflow === 'string' |
| 15 | + ? 'workflows/99_e2e.ts' |
| 16 | + : workflow.workflowFile; |
| 17 | + |
| 18 | + url.searchParams.set('workflowFile', workflowFile); |
| 19 | + url.searchParams.set('workflowFn', workflowFn); |
| 20 | + const res = await fetch(url, { |
| 21 | + method: 'POST', |
| 22 | + body: JSON.stringify(dehydrateWorkflowArguments(args, [], globalThis)), |
| 23 | + }); |
| 24 | + if (!res.ok) { |
| 25 | + throw new Error( |
| 26 | + `Failed to trigger workflow: ${res.url} ${res.status}: ${await res.text()}` |
| 27 | + ); |
| 28 | + } |
| 29 | + const run = await res.json(); |
| 30 | + return run; |
| 31 | +} |
| 32 | + |
| 33 | +async function getWorkflowReturnValue(runId: string) { |
| 34 | + // Poll the GET endpoint until the workflow run is completed |
| 35 | + while (true) { |
| 36 | + const url = new URL('/api/trigger', deploymentUrl); |
| 37 | + url.searchParams.set('runId', runId); |
| 38 | + |
| 39 | + const res = await fetch(url); |
| 40 | + |
| 41 | + if (res.status === 202) { |
| 42 | + // Workflow run is still running, wait and poll again |
| 43 | + await new Promise((resolve) => setTimeout(resolve, 5_000)); |
| 44 | + continue; |
| 45 | + } |
| 46 | + const contentType = res.headers.get('Content-Type'); |
| 47 | + |
| 48 | + if (contentType?.includes('application/json')) { |
| 49 | + return await res.json(); |
| 50 | + } |
| 51 | + |
| 52 | + if (contentType?.includes('application/octet-stream')) { |
| 53 | + return res.body; |
| 54 | + } |
| 55 | + |
| 56 | + throw new Error(`Unexpected content type: ${contentType}`); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +describe('bun workbench e2e', () => { |
| 61 | + test('calc workflow (0_demo.ts)', { timeout: 60_000 }, async () => { |
| 62 | + const run = await triggerWorkflow( |
| 63 | + { workflowFile: 'workflows/0_calc.ts', workflowFn: 'calc' }, |
| 64 | + [2] |
| 65 | + ); |
| 66 | + const returnValue = await getWorkflowReturnValue(run.runId); |
| 67 | + expect(returnValue).toBe(4); |
| 68 | + }); |
| 69 | + |
| 70 | + test('addTenWorkflow', { timeout: 60_000 }, async () => { |
| 71 | + const run = await triggerWorkflow( |
| 72 | + { workflowFile: 'workflows/99_e2e.ts', workflowFn: 'addTenWorkflow' }, |
| 73 | + [123] |
| 74 | + ); |
| 75 | + const returnValue = await getWorkflowReturnValue(run.runId); |
| 76 | + expect(returnValue).toBe(133); |
| 77 | + }); |
| 78 | + |
| 79 | + test('promiseAllWorkflow', { timeout: 60_000 }, async () => { |
| 80 | + const run = await triggerWorkflow('promiseAllWorkflow', []); |
| 81 | + const returnValue = await getWorkflowReturnValue(run.runId); |
| 82 | + expect(returnValue).toBe('ABC'); |
| 83 | + }); |
| 84 | + |
| 85 | + test('promiseRaceWorkflow', { timeout: 60_000 }, async () => { |
| 86 | + const run = await triggerWorkflow('promiseRaceWorkflow', []); |
| 87 | + const returnValue = await getWorkflowReturnValue(run.runId); |
| 88 | + expect(returnValue).toBe('B'); |
| 89 | + }); |
| 90 | + |
| 91 | + test('hookWorkflow', { timeout: 60_000 }, async () => { |
| 92 | + const token = Math.random().toString(36).slice(2); |
| 93 | + const customData = Math.random().toString(36).slice(2); |
| 94 | + |
| 95 | + const run = await triggerWorkflow('hookWorkflow', [token, customData]); |
| 96 | + |
| 97 | + // Wait for webhook to be registered |
| 98 | + await new Promise((resolve) => setTimeout(resolve, 5_000)); |
| 99 | + |
| 100 | + const hookUrl = new URL('/api/hook', deploymentUrl); |
| 101 | + |
| 102 | + let res = await fetch(hookUrl, { |
| 103 | + method: 'POST', |
| 104 | + body: JSON.stringify({ token, data: { message: 'one' } }), |
| 105 | + }); |
| 106 | + expect(res.status).toBe(200); |
| 107 | + let body = await res.json(); |
| 108 | + expect(body.runId).toBe(run.runId); |
| 109 | + |
| 110 | + // Invalid token test |
| 111 | + res = await fetch(hookUrl, { |
| 112 | + method: 'POST', |
| 113 | + body: JSON.stringify({ token: 'invalid' }), |
| 114 | + }); |
| 115 | + expect(res.status).toBe(404); |
| 116 | + body = await res.json(); |
| 117 | + expect(body).toBeNull(); |
| 118 | + |
| 119 | + res = await fetch(hookUrl, { |
| 120 | + method: 'POST', |
| 121 | + body: JSON.stringify({ token, data: { message: 'two' } }), |
| 122 | + }); |
| 123 | + expect(res.status).toBe(200); |
| 124 | + |
| 125 | + res = await fetch(hookUrl, { |
| 126 | + method: 'POST', |
| 127 | + body: JSON.stringify({ token, data: { message: 'three', done: true } }), |
| 128 | + }); |
| 129 | + expect(res.status).toBe(200); |
| 130 | + |
| 131 | + const returnValue = await getWorkflowReturnValue(run.runId); |
| 132 | + expect(returnValue).toBeInstanceOf(Array); |
| 133 | + expect(returnValue.length).toBe(3); |
| 134 | + expect(returnValue[0].message).toBe('one'); |
| 135 | + expect(returnValue[0].customData).toBe(customData); |
| 136 | + expect(returnValue[1].message).toBe('two'); |
| 137 | + expect(returnValue[2].message).toBe('three'); |
| 138 | + expect(returnValue[2].done).toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + test('webhookWorkflow', { timeout: 60_000 }, async () => { |
| 142 | + const token = Math.random().toString(36).slice(2); |
| 143 | + const token2 = Math.random().toString(36).slice(2); |
| 144 | + const token3 = Math.random().toString(36).slice(2); |
| 145 | + |
| 146 | + const run = await triggerWorkflow('webhookWorkflow', [ |
| 147 | + token, |
| 148 | + token2, |
| 149 | + token3, |
| 150 | + ]); |
| 151 | + |
| 152 | + // Wait for webhooks to be registered |
| 153 | + await new Promise((resolve) => setTimeout(resolve, 5_000)); |
| 154 | + |
| 155 | + // Webhook with default response |
| 156 | + const res = await fetch( |
| 157 | + new URL( |
| 158 | + `/.well-known/workflow/v1/webhook/${encodeURIComponent(token)}`, |
| 159 | + deploymentUrl |
| 160 | + ), |
| 161 | + { |
| 162 | + method: 'POST', |
| 163 | + body: JSON.stringify({ message: 'one' }), |
| 164 | + } |
| 165 | + ); |
| 166 | + expect(res.status).toBe(202); |
| 167 | + |
| 168 | + // Webhook with static response |
| 169 | + const res2 = await fetch( |
| 170 | + new URL( |
| 171 | + `/.well-known/workflow/v1/webhook/${encodeURIComponent(token2)}`, |
| 172 | + deploymentUrl |
| 173 | + ), |
| 174 | + { |
| 175 | + method: 'POST', |
| 176 | + body: JSON.stringify({ message: 'two' }), |
| 177 | + } |
| 178 | + ); |
| 179 | + expect(res2.status).toBe(402); |
| 180 | + |
| 181 | + // Webhook with manual response |
| 182 | + const res3 = await fetch( |
| 183 | + new URL( |
| 184 | + `/.well-known/workflow/v1/webhook/${encodeURIComponent(token3)}`, |
| 185 | + deploymentUrl |
| 186 | + ), |
| 187 | + { |
| 188 | + method: 'POST', |
| 189 | + body: JSON.stringify({ message: 'three' }), |
| 190 | + } |
| 191 | + ); |
| 192 | + expect(res3.status).toBe(200); |
| 193 | + |
| 194 | + const returnValue = await getWorkflowReturnValue(run.runId); |
| 195 | + expect(returnValue).toHaveLength(3); |
| 196 | + expect(returnValue[0].method).toBe('POST'); |
| 197 | + expect(returnValue[1].method).toBe('POST'); |
| 198 | + expect(returnValue[2].method).toBe('POST'); |
| 199 | + }); |
| 200 | + |
| 201 | + test('webhook route with invalid token', { timeout: 60_000 }, async () => { |
| 202 | + const invalidWebhookUrl = new URL( |
| 203 | + `/.well-known/workflow/v1/webhook/${encodeURIComponent('invalid')}`, |
| 204 | + deploymentUrl |
| 205 | + ); |
| 206 | + const res = await fetch(invalidWebhookUrl, { |
| 207 | + method: 'POST', |
| 208 | + body: JSON.stringify({}), |
| 209 | + }); |
| 210 | + expect(res.status).toBe(404); |
| 211 | + }); |
| 212 | + |
| 213 | + test('sleepingWorkflow', { timeout: 60_000 }, async () => { |
| 214 | + const run = await triggerWorkflow('sleepingWorkflow', []); |
| 215 | + const returnValue = await getWorkflowReturnValue(run.runId); |
| 216 | + expect(returnValue.startTime).toBeLessThan(returnValue.endTime); |
| 217 | + expect(returnValue.endTime - returnValue.startTime).toBeGreaterThan(9999); |
| 218 | + }); |
| 219 | +}); |
0 commit comments