-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathintegration.test.ts
More file actions
165 lines (132 loc) · 4.98 KB
/
integration.test.ts
File metadata and controls
165 lines (132 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { env, introspectWorkflow, SELF } from "cloudflare:test";
import { describe, it } from "vitest";
const STATUS_COMPLETE = "complete";
const STEP_NAME = "AI content scan";
const mockResult = { violationScore: 0 };
// This example implicitly disposes the Workflow instance
it("workflow should be able to reach the end and be successful", async ({
expect,
}) => {
// CONFIG with `await using` to ensure Workflow instances cleanup:
await using introspector = await introspectWorkflow(env.MODERATOR);
await introspector.modifyAll(async (m) => {
await m.disableSleeps();
await m.mockStepResult({ name: STEP_NAME }, mockResult);
});
await SELF.fetch(`https://mock-worker.local/moderate`);
const instances = introspector.get();
expect(instances.length).toBe(1);
// ASSERTIONS:
const instance = instances[0];
expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual(
mockResult
);
await expect(instance.waitForStatus(STATUS_COMPLETE)).resolves.not.toThrow();
const output = await instance.getOutput();
expect(output).toEqual({ status: "auto_approved" });
// DISPOSE: ensured by `await using`
});
// This example explicitly disposes the Workflow instances
it("workflow batch should be able to reach the end and be successful", async ({
expect,
}) => {
// CONFIG:
let introspector = await introspectWorkflow(env.MODERATOR);
try {
await introspector.modifyAll(async (m) => {
await m.disableSleeps();
await m.mockStepResult({ name: STEP_NAME }, mockResult);
});
await SELF.fetch(`https://mock-worker.local/moderate-batch`);
const instances = introspector.get();
expect(instances.length).toBe(3);
// ASSERTIONS:
for (const instance of instances) {
expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual(
mockResult
);
await expect(
instance.waitForStatus(STATUS_COMPLETE)
).resolves.not.toThrow();
const output = await instance.getOutput();
expect(output).toEqual({ status: "auto_approved" });
}
} finally {
// DISPOSE:
// Workflow introspector should be disposed the end of each test, if no `await using` dyntax is used
// Also disposes all intercepted instances
await introspector.dispose();
}
});
describe("workflow instance lifecycle methods", () => {
it("should terminate a workflow instance", async ({ expect }) => {
// CONFIG:
await using introspector = await introspectWorkflow(env.MODERATOR);
await introspector.modifyAll(async (m) => {
await m.disableSleeps();
await m.mockStepResult({ name: STEP_NAME }, { violationScore: 50 });
});
const res = await SELF.fetch("https://mock-worker.local/moderate");
const data = (await res.json()) as { id: string; details: unknown };
const instances = introspector.get();
expect(instances.length).toBe(1);
const instance = instances[0];
expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual({
violationScore: 50,
});
const handle = await env.MODERATOR.get(data.id);
await handle.terminate();
// ASSERTIONS:
await expect(instance.waitForStatus("terminated")).resolves.not.toThrow();
// DISPOSE: ensured by `await using`
});
it("should restart a workflow instance", async ({ expect }) => {
// CONFIG:
await using introspector = await introspectWorkflow(env.MODERATOR);
await introspector.modifyAll(async (m) => {
await m.disableSleeps();
await m.mockStepResult({ name: STEP_NAME }, { violationScore: 50 });
await m.mockEvent({
type: "moderation-decision",
payload: { moderatorAction: "approve" },
});
});
const res = await SELF.fetch("https://mock-worker.local/moderate");
const data = (await res.json()) as { id: string; details: unknown };
const instances = introspector.get();
expect(instances.length).toBe(1);
const instance = instances[0];
expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual({
violationScore: 50,
});
const handle = await env.MODERATOR.get(data.id);
await handle.restart();
// Mocks survive instace restart, so the restarted workflow re-runs
// with the same config
await expect(
instance.waitForStatus(STATUS_COMPLETE)
).resolves.not.toThrow();
// DISPOSE: ensured by `await using`
});
it("should pause a workflow instance", async ({ expect }) => {
// CONFIG:
await using introspector = await introspectWorkflow(env.MODERATOR);
await introspector.modifyAll(async (m) => {
await m.disableSleeps();
await m.mockStepResult({ name: STEP_NAME }, { violationScore: 50 });
});
const res = await SELF.fetch("https://mock-worker.local/moderate");
const data = (await res.json()) as { id: string; details: unknown };
const instances = introspector.get();
expect(instances.length).toBe(1);
const instance = instances[0];
expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual({
violationScore: 50,
});
const handle = await env.MODERATOR.get(data.id);
await handle.pause();
// ASSERTIONS:
await expect(instance.waitForStatus("paused")).resolves.not.toThrow();
// DISPOSE: ensured by `await using`
});
});