File
Blob: src/workerd/server/tests/python/workflow-entrypoint/worker.js
| 1 | // Copyright (c) 2025 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import { RpcTarget } from 'cloudflare:workers'; |
| 5 | import * as assert from 'node:assert'; |
| 6 | |
| 7 | class Context extends RpcTarget { |
| 8 | constructor(shouldSendCtx) { |
| 9 | super(); |
| 10 | this.shouldSendCtx = shouldSendCtx; |
| 11 | } |
| 12 | |
| 13 | async do(name, fn) { |
| 14 | try { |
| 15 | const ctx = { attempt: '1', metadata: 'expected_return_metadata' }; |
| 16 | const result = this.shouldSendCtx ? await fn(ctx) : await fn(); |
| 17 | return result; |
| 18 | } catch (e) { |
| 19 | console.log(`Error received: ${e.name} Message: ${e.message}`); |
| 20 | // let's rethrow here since the engine does the same |
| 21 | throw e; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | export default { |
| 27 | async test(ctrl, env, ctx) { |
| 28 | let stubStep = new Context(true); |
| 29 | |
| 30 | // Tests forward compat - i.e.: python workflows should be compatible with steps that pass a ctx argument |
| 31 | // this param is optional and searched by name. Meaning it's not positional |
| 32 | let resp = await env.PythonWorkflow.run( |
| 33 | { |
| 34 | foo: 'bar', |
| 35 | }, |
| 36 | stubStep |
| 37 | ); |
| 38 | assert.deepStrictEqual(resp, 'foobar'); |
| 39 | |
| 40 | // Tests backwards compat for dependency resolution - previously deps were not following a name based |
| 41 | // approach. Instead, they were passed in the same order as they were declared in the depends param |
| 42 | // This test also doesn't pass any ctx to steps |
| 43 | stubStep = new Context(false); |
| 44 | resp = await env.PythonWorkflowDepends.run( |
| 45 | { |
| 46 | foo: 'bar', |
| 47 | }, |
| 48 | stubStep |
| 49 | ); |
| 50 | assert.deepStrictEqual(resp, 'foobar'); |
| 51 | |
| 52 | // Tests forwards compat for dependency resolution - pass ctx onto workflow that runs with the legacy |
| 53 | // code path |
| 54 | stubStep = new Context(true); |
| 55 | resp = await env.PythonWorkflowDepends.run( |
| 56 | { |
| 57 | foo: 'bar', |
| 58 | }, |
| 59 | stubStep |
| 60 | ); |
| 61 | assert.deepStrictEqual(resp, 'foobar'); |
| 62 | }, |
| 63 | }; |