Skip to content
File

Blob: src/cloudflare/internal/test/workflows/workflows-api-test.js

javascript82 lines
1// Copyright (c) 2024 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 
5import * as assert from 'node:assert';
6 
7async function getLastRestartBody(env, id) {
8 const res = await env.mock.fetch('http://placeholder/last-restart', {
9 method: 'POST',
10 body: JSON.stringify({ id }),
11 });
12 return (await res.json()).result;
13}
14 
15export const tests = {
16 async test(_, env) {
17 {
18 // Test create instance
19 const instance = await env.workflow.create({
20 id: 'foo',
21 payload: { bar: 'baz' },
22 });
23 assert.deepStrictEqual(instance.id, 'foo');
24 }
25 
26 {
27 // Test get instance
28 const instance = await env.workflow.get('bar');
29 assert.deepStrictEqual(instance.id, 'bar');
30 }
31 
32 {
33 // Test createBatch
34 const instances = await env.workflow.createBatch([
35 {
36 id: 'foo',
37 payload: { bar: 'baz' },
38 },
39 {
40 id: 'bar',
41 payload: { bar: 'baz' },
42 },
43 ]);
44 assert.deepStrictEqual(instances[0].id, 'foo');
45 assert.deepStrictEqual(instances[1].id, 'bar');
46 }
47 },
48 
49 async testRestartNoOptions(_, env) {
50 const instance = await env.workflow.get('restart-basic');
51 await instance.restart();
52 
53 const body = await getLastRestartBody(env, 'restart-basic');
54 assert.deepStrictEqual(body.id, 'restart-basic');
55 assert.strictEqual(body.from, undefined);
56 },
57 
58 async testRestartFromStepNameOnly(_, env) {
59 const instance = await env.workflow.get('restart-step');
60 await instance.restart({ from: { name: 'fetch data' } });
61 
62 const body = await getLastRestartBody(env, 'restart-step');
63 assert.deepStrictEqual(body.id, 'restart-step');
64 assert.deepStrictEqual(body.from, { name: 'fetch data' });
65 },
66 
67 async testRestartFromStepAllOptions(_, env) {
68 const instance = await env.workflow.get('restart-full');
69 await instance.restart({
70 from: { name: 'process item', count: 3, type: 'do' },
71 });
72 
73 const body = await getLastRestartBody(env, 'restart-full');
74 assert.deepStrictEqual(body.id, 'restart-full');
75 assert.deepStrictEqual(body.from, {
76 name: 'process item',
77 count: 3,
78 type: 'do',
79 });
80 },
81};