The best way to control Claude Code, Codex and more from any computer or phone

main
ts 2,243 lines 75.7 KB
Raw
1 // Alchemy modifications are licensed under Apache-2.0.
2 // This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
3 // Alchemy modifications: expects Effect Schema validation messages and cloudflare-runtime Workflow storage paths.
4 import {
5 createExecutionContext,
6 runInDurableObject,
7 waitOnExecutionContext,
8 } from "cloudflare:test";
9 import { env } from "cloudflare:workers";
10 import type { WorkflowEvent } from "cloudflare:workers";
11 import { describe, it, vi } from "vitest";
12 import { deleteWorkflowStorage, WorkflowBinding } from "../binding.ts";
13 import type { WorkflowBatchCreateOptions, WorkflowHandle } from "../binding.ts";
14 import type { Engine, EngineLogs } from "../engine.ts";
15 import { InstanceEvent, InstanceStatus } from "../index.ts";
16 import { WorkflowSubscriptionTarget } from "../subscription.ts";
17 import type { WorkflowSubscription, WorkflowSubscriptionEvent } from "../subscription.ts";
18 import { setTestWorkflowCallback } from "./test-entry.ts";
19
20 let instanceCounter = 0;
21 function uniqueId(prefix = "instance"): string {
22 return `${prefix}-${++instanceCounter}`;
23 }
24
25 function createBinding(): WorkflowBinding {
26 const ctx = createExecutionContext();
27 return new WorkflowBinding(ctx, {
28 ENGINE: env.ENGINE,
29 BINDING_NAME: "TEST_WORKFLOW",
30 WORKFLOW_NAME: "test-workflow",
31 });
32 }
33
34 async function waitUntilLogEvent(
35 engineStub: DurableObjectStub<Engine>,
36 event: InstanceEvent,
37 timeout = 5000,
38 ): Promise<void> {
39 await vi.waitUntil(
40 async () => {
41 const logs = (await engineStub.readLogs()) as EngineLogs;
42 const hasEvent = logs.logs.some((log) => log.event === event);
43 return hasEvent;
44 },
45 { timeout },
46 );
47 }
48
49 describe("WorkflowBinding", () => {
50 describe("create()", () => {
51 it("should create an instance with provided id and params", async ({ expect }) => {
52 const id = uniqueId();
53 const binding = createBinding();
54 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
55
56 setTestWorkflowCallback(async (event) => {
57 return (event as WorkflowEvent<{ key: string }>).payload;
58 });
59
60 const params = { key: "test-value" };
61 const result = await binding.create({ id, params });
62
63 expect(result.id).toBe(id);
64
65 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
66
67 const instance = await binding.get(id);
68 const status = await instance.status();
69 expect(status.output).toEqual(params);
70 });
71
72 it("should pass the workflow name in the event", async ({ expect }) => {
73 const id = uniqueId();
74 const binding = createBinding();
75 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
76
77 setTestWorkflowCallback(async (event) => {
78 return (event as WorkflowEvent<unknown>).workflowName;
79 });
80
81 await binding.create({ id });
82
83 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
84
85 const instance = await binding.get(id);
86 const status = await instance.status();
87 expect(status.output).toBe("test-workflow");
88 });
89
90 it("should auto-generate id when not provided", async ({ expect }) => {
91 const binding = createBinding();
92 setTestWorkflowCallback(async () => "done");
93 const result = await binding.create();
94
95 expect(result.id).toBeDefined();
96 expect(result.id.length).toBeGreaterThan(0);
97
98 // Wait for the workflow to complete before the test ends so
99 // the fire-and-forget init() RPC settles before teardown.
100 const instance = await binding.get(result.id);
101 await vi.waitUntil(
102 async () => {
103 const s = await instance.status();
104 return s.status === "complete";
105 },
106 { timeout: 5000 },
107 );
108 });
109
110 it("should throw WorkflowError for invalid instance id", async ({ expect }) => {
111 const binding = createBinding();
112 await expect(binding.create({ id: "#invalid!" })).rejects.toThrow(
113 "Workflow instance has invalid id",
114 );
115 });
116
117 it("should reject a null id instead of generating one", async ({ expect }) => {
118 const binding = createBinding();
119 await expect(binding.create({ id: null as unknown as string })).rejects.toThrow(
120 "Workflow instance has invalid id",
121 );
122 });
123
124 for (const method of ["create", "createBatch"] as const) {
125 it(`${method} should retry failed cleanup before reusing an instance ID`, async ({
126 expect,
127 }) => {
128 const id = uniqueId("cleanup-failed");
129 const ctx = createExecutionContext();
130 const abort = vi.fn(async () => {});
131 const diskFetch = vi.fn((_url: string, _init?: RequestInit) =>
132 Promise.resolve(
133 new Response(null, {
134 headers: { "Content-Type": "application/json" },
135 }),
136 ),
137 );
138 const storageEnv = {
139 ENGINE: env.ENGINE,
140 BINDING_NAME: "TEST_WORKFLOW",
141 WORKFLOW_NAME: "test-workflow",
142 WORKFLOW_STORAGE: { fetch: diskFetch } as unknown as Fetcher,
143 };
144 const binding = new WorkflowBinding(ctx, storageEnv);
145 const deletionError = "Refusing to delete a Workflow storage directory";
146 await expect(
147 deleteWorkflowStorage({
148 disk: storageEnv.WORKFLOW_STORAGE,
149 workflowName: storageEnv.WORKFLOW_NAME,
150 ctx,
151 hexId: env.ENGINE.idFromName(id).toString(),
152 abort,
153 }),
154 ).rejects.toThrow(deletionError);
155
156 await expect(
157 deleteWorkflowStorage({
158 disk: storageEnv.WORKFLOW_STORAGE,
159 workflowName: storageEnv.WORKFLOW_NAME,
160 ctx,
161 hexId: env.ENGINE.idFromName(id).toString(),
162 abort: async () => {
163 throw new Error("instance.not_found");
164 },
165 }),
166 ).rejects.toThrow("instance.not_found");
167
168 diskFetch.mockClear();
169 await expect(
170 method === "create" ? binding.create({ id }) : binding.createBatch([{ id }]),
171 ).rejects.toThrow(deletionError);
172 expect(diskFetch.mock.calls.filter(([url]) => url.endsWith(".sqlite"))).toHaveLength(1);
173 await expect(waitOnExecutionContext(ctx)).resolves.toBeUndefined();
174
175 diskFetch.mockImplementation((_url, init) =>
176 Promise.resolve(
177 new Response(null, {
178 status: init?.method === "DELETE" ? 204 : 200,
179 headers: { "Content-Type": "application/octet-stream" },
180 }),
181 ),
182 );
183 setTestWorkflowCallback(async () => "done");
184 const freshBinding = new WorkflowBinding(ctx, storageEnv);
185 await expect(
186 method === "create" ? freshBinding.create({ id }) : freshBinding.createBatch([{ id }]),
187 ).resolves.toEqual(method === "create" ? { id } : [{ id }]);
188 expect(diskFetch).toHaveBeenCalledWith(expect.stringMatching(/\.sqlite$/), {
189 method: "DELETE",
190 });
191 expect(abort).toHaveBeenCalledOnce();
192 await waitUntilLogEvent(
193 env.ENGINE.get(env.ENGINE.idFromName(id)),
194 InstanceEvent.WORKFLOW_SUCCESS,
195 );
196 await waitOnExecutionContext(ctx);
197 });
198 }
199 });
200
201 describe("get()", () => {
202 it("should return a WorkflowHandle for an existing instance", async ({ expect }) => {
203 const id = uniqueId();
204 const binding = createBinding();
205 env.ENGINE.get(env.ENGINE.idFromName(id));
206
207 setTestWorkflowCallback(async () => "done");
208 await binding.create({ id });
209
210 const instance = await binding.get(id);
211
212 expect(instance).toMatchObject({
213 id,
214 status: expect.any(Function),
215 pause: expect.any(Function),
216 resume: expect.any(Function),
217 terminate: expect.any(Function),
218 restart: expect.any(Function),
219 delete: expect.any(Function),
220 });
221
222 // Wait for the workflow to complete before the test ends so
223 // the fire-and-forget init() RPC settles before teardown.
224 await vi.waitUntil(
225 async () => {
226 const s = await instance.status();
227 return s.status === "complete";
228 },
229 { timeout: 5000 },
230 );
231 });
232 });
233
234 describe("instance deletion", () => {
235 it("deleteInstance should delete an instance and wipe its stored state", async ({ expect }) => {
236 const id = uniqueId();
237 const binding = createBinding();
238
239 setTestWorkflowCallback(async () => "done");
240 await binding.create({ id });
241
242 const instance = await binding.get(id);
243 await vi.waitUntil(
244 async () => {
245 const status = await instance.status();
246 return status.status === "complete";
247 },
248 { timeout: 5000 },
249 );
250
251 await expect(binding.deleteInstance(id)).resolves.toBeUndefined();
252 await expect(binding.get(id)).rejects.toThrow("instance.not_found");
253 });
254
255 it("should reject an invalid instance ID", async ({ expect }) => {
256 await expect(createBinding().deleteInstance("")).rejects.toThrow(
257 "(instance.invalid_id) Instance ID is invalid",
258 );
259 });
260
261 it("should accept a cron-generated instance ID", async ({ expect }) => {
262 await expect(createBinding().deleteInstance("*/30 * * * *-1786001400000")).rejects.toThrow(
263 "instance.not_found",
264 );
265 });
266
267 it("should let a running instance delete itself and stop execution", async ({ expect }) => {
268 const id = uniqueId();
269 const binding = createBinding();
270 let deleteStarted = false;
271 let continuedAfterDelete = false;
272
273 setTestWorkflowCallback(async () => {
274 deleteStarted = true;
275 const instance = await binding.get(id);
276 await (instance as unknown as { delete(): Promise<void> }).delete();
277 continuedAfterDelete = true;
278 });
279 await binding.create({ id });
280 await vi.waitUntil(() => deleteStarted, { timeout: 5000 });
281
282 await vi.waitUntil(
283 async () => {
284 try {
285 await binding.get(id);
286 return false;
287 } catch {
288 return true;
289 }
290 },
291 { timeout: 5000 },
292 );
293
294 await scheduler.wait(50);
295 expect(continuedAfterDelete).toBe(false);
296 await expect(binding.get(id)).rejects.toThrow("instance.not_found");
297 });
298 });
299
300 describe("deleteBatch()", () => {
301 it("should delete instances and wipe their stored state", async ({ expect }) => {
302 const ids = [uniqueId(), uniqueId()];
303 const binding = createBinding();
304
305 setTestWorkflowCallback(async () => "done");
306 await binding.createBatch(ids.map((id) => ({ id })));
307
308 for (const id of ids) {
309 const instance = await binding.get(id);
310 await vi.waitUntil(
311 async () => {
312 const status = await instance.status();
313 return status.status === "complete";
314 },
315 { timeout: 5000 },
316 );
317 }
318
319 await expect(binding.deleteBatch({ instances: ids })).resolves.toEqual({
320 deleted: ids.map((id) => ({ id })),
321 errors: [],
322 });
323 for (const id of ids) {
324 await expect(binding.get(id)).rejects.toThrow("instance.not_found");
325 }
326 });
327
328 it("should report each duplicate missing cron-generated ID", async ({ expect }) => {
329 const binding = createBinding();
330 const cronId = "*/30 * * * *-1786001400000";
331 await expect(
332 binding.deleteBatch({
333 instances: [cronId, cronId],
334 }),
335 ).resolves.toEqual({
336 deleted: [],
337 errors: [
338 {
339 id: cronId,
340 code: 10400,
341 message: "workflows.api.error.instance.not_found",
342 },
343 {
344 id: cronId,
345 code: 10400,
346 message: "workflows.api.error.instance.not_found",
347 },
348 ],
349 });
350 });
351
352 it("should normalize unexpected deletion errors", async ({ expect }) => {
353 const deleteInstance = vi.fn().mockRejectedValue(new Error("sensitive failure"));
354 const binding = new WorkflowBinding(createExecutionContext(), {
355 ENGINE: {
356 idFromName: (id: string) => env.ENGINE.idFromName(id),
357 get: (id: DurableObjectId) => ({ id, deleteInstance }),
358 } as unknown as DurableObjectNamespace<Engine>,
359 BINDING_NAME: "TEST_WORKFLOW",
360 WORKFLOW_NAME: "test-workflow",
361 });
362
363 await expect(binding.deleteBatch({ instances: ["broken-instance"] })).resolves.toEqual({
364 deleted: [],
365 errors: [
366 {
367 id: "broken-instance",
368 code: 10001,
369 message: "workflows.api.error.internal_server",
370 },
371 ],
372 });
373 expect(deleteInstance).toHaveBeenCalledOnce();
374 });
375
376 it("should report persistence cleanup failures per instance", async ({ expect }) => {
377 const abort = vi.fn(() => Promise.reject(new Error("Durable Object aborted")));
378 const cleanupFailedId = env.ENGINE.idFromName("cleanup-failed").toString();
379 const missingId = env.ENGINE.idFromName("missing").toString();
380 const ctx = createExecutionContext();
381 const diskFetch = vi.fn((url: string, init?: RequestInit) =>
382 Promise.resolve(
383 new Response(null, {
384 status:
385 url.includes(cleanupFailedId) && url.endsWith(".sqlite")
386 ? init?.method === "HEAD"
387 ? 200
388 : 500
389 : 404,
390 headers: { "Content-Type": "application/octet-stream" },
391 }),
392 ),
393 );
394 const binding = new WorkflowBinding(ctx, {
395 ENGINE: {
396 idFromName: (id: string) => env.ENGINE.idFromName(id),
397 get: (id: DurableObjectId) => ({
398 id,
399 deleteInstance: () => {
400 if (id.toString() === missingId) {
401 return Promise.reject(new Error("(instance.not_found) Instance does not exist"));
402 }
403 return Promise.resolve();
404 },
405 unsafeAbort: abort,
406 }),
407 } as unknown as DurableObjectNamespace<Engine>,
408 BINDING_NAME: "TEST_WORKFLOW",
409 WORKFLOW_NAME: "test-workflow",
410 WORKFLOW_STORAGE: { fetch: diskFetch } as unknown as Fetcher,
411 });
412
413 await expect(
414 binding.deleteBatch({
415 instances: ["cleanup-failed", "missing", "deleted", "cleanup-failed"],
416 }),
417 ).resolves.toEqual({
418 deleted: [{ id: "deleted" }],
419 errors: [
420 {
421 id: "cleanup-failed",
422 code: 10001,
423 message: "workflows.api.error.internal_server",
424 },
425 {
426 id: "missing",
427 code: 10400,
428 message: "workflows.api.error.instance.not_found",
429 },
430 {
431 id: "cleanup-failed",
432 code: 10001,
433 message: "workflows.api.error.internal_server",
434 },
435 ],
436 });
437 expect(abort).toHaveBeenCalledOnce();
438 expect(diskFetch).toHaveBeenCalledWith(`http://disk/test-workflow/${missingId}.sqlite`, {
439 method: "HEAD",
440 });
441 await expect(waitOnExecutionContext(ctx)).resolves.toBeUndefined();
442 });
443
444 it("should reject invalid batches", async ({ expect }) => {
445 const binding = createBinding();
446 await expect(binding.deleteBatch({ instances: [] })).rejects.toThrow(
447 "(body) batchDeleteInstances should have at least 1 instance",
448 );
449 await expect(
450 binding.deleteBatch({
451 instances: Array.from({ length: 101 }, (_, i) => `instance-${i}`),
452 }),
453 ).rejects.toThrow("(body) batchDeleteInstances only supports 100 instances at a time");
454 await expect(binding.deleteBatch({ instances: [""] })).rejects.toThrow(
455 "(instance.invalid_id) Instance ID is invalid",
456 );
457 });
458 });
459
460 describe("createBatch()", () => {
461 it("should create multiple instances in a batch", async ({ expect }) => {
462 const binding = createBinding();
463 // Unique per attempt: createBatch() skips existing IDs, so fixed IDs would
464 // make a vitest retry return [] after a failed first attempt.
465 const ids = [1, 2, 3].map((n) => uniqueId(`batch-${n}`));
466 setTestWorkflowCallback(async () => "done");
467
468 const results = await binding.createBatch(ids.map((id) => ({ id })));
469
470 expect(results).toHaveLength(3);
471 expect(results.map((r) => r.id)).toEqual(ids);
472
473 for (const id of ids) {
474 const instance = await binding.get(id);
475 expect(instance.id).toBe(id);
476 }
477
478 // Wait for all batch workflows to complete before the test ends
479 // so the fire-and-forget init() RPCs settle before teardown.
480 for (const id of ids) {
481 const instance = await binding.get(id);
482 await vi.waitUntil(
483 async () => {
484 const s = await instance.status();
485 return s.status === "complete";
486 },
487 { timeout: 5000 },
488 );
489 }
490 });
491
492 it("should create instances by count with shared options", async ({ expect }) => {
493 const binding = createBinding();
494 const params = { source: "count" };
495 setTestWorkflowCallback(async (event) => (event as WorkflowEvent<typeof params>).payload);
496
497 const result = await binding.createBatch({
498 count: 2,
499 params,
500 retention: { successRetention: "1 day" },
501 locationHint: "weur",
502 });
503
504 expect(result.errors).toEqual([]);
505 expect(result.created).toHaveLength(2);
506 expect(result.created[0].id).not.toBe(result.created[1].id);
507
508 for (const { id } of result.created) {
509 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
510 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
511 const instance = await binding.get(id);
512 expect((await instance.status()).output).toEqual(params);
513 }
514 });
515
516 it("should report per-instance errors for duplicate ids", async ({ expect }) => {
517 const binding = createBinding();
518 const existing = uniqueId("batch-existing");
519 const fresh = uniqueId("batch-fresh");
520 setTestWorkflowCallback(async () => "done");
521
522 await binding.create({ id: existing });
523 await waitUntilLogEvent(
524 env.ENGINE.get(env.ENGINE.idFromName(existing)),
525 InstanceEvent.WORKFLOW_SUCCESS,
526 );
527
528 const result = await binding.createBatch({
529 instances: [{ id: fresh }, { id: existing }, { id: fresh }],
530 });
531
532 expect(result.created).toEqual([{ id: fresh }]);
533 expect(result.errors).toEqual([
534 {
535 index: 1,
536 id: existing,
537 code: 10405,
538 message: "workflows.api.error.instance.already_exists",
539 },
540 {
541 index: 2,
542 id: fresh,
543 code: 10415,
544 message: "workflows.api.error.instance.duplicate_in_batch",
545 },
546 ]);
547
548 await waitUntilLogEvent(
549 env.ENGINE.get(env.ENGINE.idFromName(fresh)),
550 InstanceEvent.WORKFLOW_SUCCESS,
551 );
552 });
553
554 it("should throw error for empty batch", async ({ expect }) => {
555 const binding = createBinding();
556
557 await expect(binding.createBatch([])).rejects.toThrow(
558 "WorkflowError: batchCreate should have at least 1 instance",
559 );
560 await expect(binding.createBatch({ instances: [] })).rejects.toThrow(
561 "(body) Batch size exceeds maximum allowed",
562 );
563 await expect(binding.createBatch({ count: 0 })).rejects.toThrow(
564 "(body) count must be a positive integer",
565 );
566 });
567
568 it("should reject invalid batch options", async ({ expect }) => {
569 const binding = createBinding();
570 const tooMany = Array.from({ length: 101 }, (_, i) => ({
571 id: `too-many-${i}`,
572 }));
573
574 await expect(binding.createBatch(tooMany)).rejects.toThrow(
575 "WorkflowError: batchCreate only supports 100 instances at a time",
576 );
577 await expect(binding.createBatch({ instances: tooMany })).rejects.toThrow(
578 "(body) Batch size exceeds maximum allowed",
579 );
580 await expect(binding.createBatch({ count: 101 })).rejects.toThrow(
581 "(body) batchCreate only supports 100 instances at a time",
582 );
583 await expect(binding.createBatch({ count: Number.MAX_SAFE_INTEGER })).rejects.toThrow(
584 "(body) batchCreate only supports 100 instances at a time",
585 );
586 await expect(binding.createBatch({ count: -1 })).rejects.toThrow(
587 "(body) count must be a positive integer",
588 );
589 await expect(binding.createBatch({ count: 1.5 })).rejects.toThrow(
590 "(body) count must be a positive integer",
591 );
592 await expect(binding.createBatch({} as WorkflowBatchCreateOptions)).rejects.toThrow(
593 "(body) Provided argument is invalid",
594 );
595 await expect(
596 binding.createBatch({
597 instances: [[]],
598 } as unknown as WorkflowBatchCreateOptions),
599 ).rejects.toThrow("(body) Expected object");
600 await expect(
601 binding.createBatch({
602 count: 1,
603 retention: { successRetention: "invalid" },
604 } as unknown as WorkflowBatchCreateOptions),
605 ).rejects.toThrow(
606 "(body) Duration must be a number or a string in format '{{number}} {{unit}}' where unit is second(s), minute(s), etc.",
607 );
608 await expect(
609 binding.createBatch({
610 instances: [{ locationHint: "invalid" }],
611 } as unknown as WorkflowBatchCreateOptions),
612 ).rejects.toThrow('at ["locationHint"]');
613 });
614
615 it("should only accept positive retention durations", async ({ expect }) => {
616 const binding = createBinding();
617 setTestWorkflowCallback(async () => "done");
618
619 for (const successRetention of [-1, 0, 0.5, "0 days", "-1 day"]) {
620 await expect(
621 binding.createBatch({
622 count: 1,
623 retention: { successRetention },
624 } as unknown as WorkflowBatchCreateOptions),
625 ).rejects.toThrow(
626 "(body) Duration must be a number or a string in format '{{number}} {{unit}}' where unit is second(s), minute(s), etc.",
627 );
628 }
629
630 const result = await binding.createBatch({
631 count: 1,
632 retention: { successRetention: 1000, errorRetention: "1 day" },
633 });
634 expect(result.created).toHaveLength(1);
635 await waitUntilLogEvent(
636 env.ENGINE.get(env.ENGINE.idFromName(result.created[0].id)),
637 InstanceEvent.WORKFLOW_SUCCESS,
638 );
639 });
640
641 it("should prefer count when instances are also provided", async ({ expect }) => {
642 const binding = createBinding();
643 setTestWorkflowCallback(async () => "done");
644
645 const result = await binding.createBatch({
646 count: 1,
647 instances: [],
648 } as unknown as WorkflowBatchCreateOptions);
649
650 expect(result.created).toHaveLength(1);
651 await waitUntilLogEvent(
652 env.ENGINE.get(env.ENGINE.idFromName(result.created[0].id)),
653 InstanceEvent.WORKFLOW_SUCCESS,
654 );
655 });
656
657 it("should skip and exclude ids that already exist", async ({ expect }) => {
658 const existing = uniqueId("dedup-existing");
659 const fresh = uniqueId("dedup-fresh");
660 const binding = createBinding();
661 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(existing));
662 setTestWorkflowCallback(async () => "done");
663
664 const first = await binding.createBatch([{ id: existing }]);
665 expect(first.map((r) => r.id)).toEqual([existing]);
666 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
667
668 const second = await binding.createBatch([{ id: existing }]);
669 expect(second).toEqual([]);
670
671 const mixed = await binding.createBatch([{ id: existing }, { id: fresh }]);
672 expect(mixed.map((r) => r.id)).toEqual([fresh]);
673
674 const freshStub = env.ENGINE.get(env.ENGINE.idFromName(fresh));
675 await waitUntilLogEvent(freshStub, InstanceEvent.WORKFLOW_SUCCESS);
676 });
677
678 it("should collapse duplicate ids within a single batch", async ({ expect }) => {
679 const id = uniqueId("dedup-in-batch");
680 const binding = createBinding();
681 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
682 setTestWorkflowCallback(async () => "done");
683
684 const results = await binding.createBatch([{ id }, { id }, { id }]);
685 expect(results.map((r) => r.id)).toEqual([id]);
686
687 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
688 });
689
690 it("should reject the whole batch before creating anything when an id is invalid", async ({
691 expect,
692 }) => {
693 const good = uniqueId("batch-invalid-good");
694 const binding = createBinding();
695 setTestWorkflowCallback(async () => "done");
696
697 await expect(binding.createBatch([{ id: good }, { id: "#invalid!" }])).rejects.toThrow(
698 "Workflow instance has invalid id",
699 );
700
701 // The valid entry listed before the invalid one must not have been
702 // created.
703 await expect(binding.get(good)).rejects.toThrow("instance.not_found");
704 });
705
706 it("should reject reserved instance ids", async ({ expect }) => {
707 const binding = createBinding();
708 const reservedIds = ["batch", "terminate", "terminateAll", `cf_${"0".repeat(64)}`];
709
710 for (const id of reservedIds) {
711 await expect(binding.createBatch({ instances: [{ id }] })).rejects.toThrow(
712 "(instance.invalid_id) Instance ID is invalid",
713 );
714 }
715 });
716
717 it("should keep accepting reserved ids in the array form", async ({ expect }) => {
718 const binding = createBinding();
719 const ids = ["batch", "terminate", "terminateAll", `cf_${"1".repeat(64)}`];
720 setTestWorkflowCallback(async () => "done");
721
722 const results = await binding.createBatch(ids.map((id) => ({ id })));
723
724 expect(results.map((r) => r.id)).toEqual(ids);
725 for (const id of ids) {
726 await waitUntilLogEvent(
727 env.ENGINE.get(env.ENGINE.idFromName(id)),
728 InstanceEvent.WORKFLOW_SUCCESS,
729 );
730 }
731 });
732
733 it("should keep accepting unvalidated options in the array form", async ({ expect }) => {
734 const binding = createBinding();
735 const id = uniqueId("legacy-batch-options");
736 setTestWorkflowCallback(async () => "done");
737
738 const results = await binding.createBatch([
739 {
740 id,
741 retention: { successRetention: "invalid" },
742 locationHint: "invalid",
743 },
744 ] as unknown as WorkflowInstanceCreateOptions<unknown>[]);
745
746 expect(results).toEqual([{ id }]);
747 await waitUntilLogEvent(
748 env.ENGINE.get(env.ENGINE.idFromName(id)),
749 InstanceEvent.WORKFLOW_SUCCESS,
750 );
751 });
752
753 it("should not create anything when a pending deletion cannot complete", async ({ expect }) => {
754 const id = uniqueId("batch-cleanup-failed");
755 const ctx = createExecutionContext();
756 const diskFetch = vi.fn(() =>
757 Promise.resolve(
758 new Response(null, {
759 headers: { "Content-Type": "application/json" },
760 }),
761 ),
762 );
763 const storageEnv = {
764 ENGINE: env.ENGINE,
765 BINDING_NAME: "TEST_WORKFLOW",
766 WORKFLOW_NAME: "test-workflow",
767 WORKFLOW_STORAGE: { fetch: diskFetch } as unknown as Fetcher,
768 };
769 const binding = new WorkflowBinding(ctx, storageEnv);
770 const hexId = env.ENGINE.idFromName(id).toString();
771 const deletionError = "Refusing to delete a Workflow storage directory";
772 await expect(
773 deleteWorkflowStorage({
774 disk: storageEnv.WORKFLOW_STORAGE,
775 workflowName: storageEnv.WORKFLOW_NAME,
776 ctx,
777 hexId,
778 abort: async () => {},
779 }),
780 ).rejects.toThrow(deletionError);
781
782 await expect(binding.createBatch([{ id }])).rejects.toThrow(deletionError);
783 await expect(binding.createBatch({ instances: [{ id }] })).rejects.toThrow(deletionError);
784 await expect(createBinding().get(id)).rejects.toThrow("instance.not_found");
785
786 diskFetch.mockImplementation(() => Promise.resolve(new Response(null, { status: 404 })));
787 setTestWorkflowCallback(async () => "done");
788 await expect(binding.createBatch({ instances: [{ id }] })).resolves.toEqual({
789 created: [{ id }],
790 errors: [],
791 });
792 await waitUntilLogEvent(
793 env.ENGINE.get(env.ENGINE.idFromName(id)),
794 InstanceEvent.WORKFLOW_SUCCESS,
795 );
796 await expect(waitOnExecutionContext(ctx)).resolves.toBeUndefined();
797 });
798
799 for (const kind of ["explicit", "generated"] as const) {
800 it(`should wait for each ${kind} batch id's pending deletion`, async ({ expect }) => {
801 const first = crypto.randomUUID();
802 const second = crypto.randomUUID();
803 if (kind === "generated") {
804 vi.spyOn(crypto, "randomUUID").mockReturnValueOnce(first).mockReturnValueOnce(second);
805 }
806 const ctx = createExecutionContext();
807 const firstCleanup = Promise.withResolvers<void>();
808 const secondCleanup = Promise.withResolvers<void>();
809 const firstHexId = env.ENGINE.idFromName(first).toString();
810 const secondHexId = env.ENGINE.idFromName(second).toString();
811 const firstUrl = `http://disk/test-workflow/${firstHexId}.sqlite`;
812 const secondUrl = `http://disk/test-workflow/${secondHexId}.sqlite`;
813 const cleanupGates = new Map([
814 [firstUrl, firstCleanup.promise],
815 [secondUrl, secondCleanup.promise],
816 ]);
817 const engineGet = vi.fn((id: DurableObjectId) => env.ENGINE.get(id));
818 const diskFetch = vi.fn(async (url: string) => {
819 await cleanupGates.get(url);
820 return new Response(null, { status: 404 });
821 });
822 const storageEnv = {
823 ENGINE: {
824 idFromName: (id: string) => env.ENGINE.idFromName(id),
825 get: engineGet,
826 } as unknown as DurableObjectNamespace<Engine>,
827 BINDING_NAME: "TEST_WORKFLOW",
828 WORKFLOW_NAME: "test-workflow",
829 WORKFLOW_STORAGE: {
830 fetch: diskFetch,
831 } as unknown as Fetcher,
832 };
833 const binding = new WorkflowBinding(ctx, storageEnv);
834 setTestWorkflowCallback(async () => "done");
835
836 const deletions = [first, second].map((id) =>
837 deleteWorkflowStorage({
838 disk: storageEnv.WORKFLOW_STORAGE,
839 workflowName: storageEnv.WORKFLOW_NAME,
840 ctx,
841 hexId: env.ENGINE.idFromName(id).toString(),
842 abort: async () => {},
843 }),
844 );
845 const creation = binding.createBatch(
846 kind === "explicit"
847 ? { instances: [{ id: first }, { id: second }, { id: first }] }
848 : { count: 2 },
849 );
850 await vi.waitFor(() => {
851 for (const url of [firstUrl, secondUrl]) {
852 expect(diskFetch).toHaveBeenCalledWith(url, { method: "HEAD" });
853 }
854 });
855 expect(engineGet).not.toHaveBeenCalled();
856 firstCleanup.resolve();
857 await deletions[0];
858 await vi.waitFor(() => {
859 expect(engineGet.mock.calls.map(([id]) => id.toString())).toContain(firstHexId);
860 });
861 expect(engineGet.mock.calls.map(([id]) => id.toString())).not.toContain(secondHexId);
862 secondCleanup.resolve();
863 await Promise.all(deletions);
864 const result = await creation;
865 expect(result.created).toEqual([{ id: first }, { id: second }]);
866 for (const { id } of result.created) {
867 await waitUntilLogEvent(
868 env.ENGINE.get(env.ENGINE.idFromName(id)),
869 InstanceEvent.WORKFLOW_SUCCESS,
870 );
871 }
872 });
873 }
874
875 it("should create batch entries without ids under generated ids", async ({ expect }) => {
876 const binding = createBinding();
877 setTestWorkflowCallback(async () => "done");
878
879 const results = await binding.createBatch([{}, {}]);
880 expect(results).toHaveLength(2);
881 expect(results[0].id).not.toBe(results[1].id);
882
883 // Wait for both workflows to complete so the fire-and-forget
884 // init() RPCs settle before teardown.
885 for (const { id } of results) {
886 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
887 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
888 }
889 });
890 });
891
892 describe("deterministic id uniqueness", () => {
893 it("should throw when creating an instance with an existing id", async ({ expect }) => {
894 const id = uniqueId("dup-create");
895 const binding = createBinding();
896 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
897 setTestWorkflowCallback(async () => "done");
898
899 await binding.create({ id });
900 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
901
902 await expect(binding.create({ id })).rejects.toThrow(
903 `(instance.already_exists) Workflow instance with id "${id}" already exists`,
904 );
905 });
906
907 it("should not throw for auto-generated ids", async ({ expect }) => {
908 const binding = createBinding();
909 setTestWorkflowCallback(async () => "done");
910
911 const first = await binding.create();
912 const second = await binding.create();
913 expect(first.id).not.toBe(second.id);
914
915 // Wait for both workflows to complete so the fire-and-forget
916 // init() RPCs settle before teardown.
917 for (const { id } of [first, second]) {
918 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
919 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
920 }
921 });
922 });
923 });
924
925 describe("WorkflowBinding", () => {
926 it("should not call dispose when sending an event to an instance", async ({
927 expect,
928 onTestFinished,
929 }) => {
930 const id = uniqueId();
931 const binding = createBinding();
932 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
933
934 setTestWorkflowCallback(async (_event, step) => {
935 const receivedEvent = await step.waitForEvent("wait-for-test-event", {
936 type: "test-event",
937 timeout: "10 seconds",
938 });
939 return receivedEvent;
940 });
941
942 const createdInstance = await binding.create({ id });
943 expect(createdInstance.id).toBe(id);
944
945 const instance = await binding.get(id);
946 expect(instance.id).toBe(id);
947
948 const disposeSpy = vi.fn();
949
950 await runInDurableObject<Engine, void>(engineStub, (engine) => {
951 // RPC methods must remain on the prototype to be callable by workerd.
952 const enginePrototype = Object.getPrototypeOf(engine) as Engine;
953 const originalReceiveEvent = enginePrototype.receiveEvent;
954 const receiveEventSpy = vi.spyOn(enginePrototype, "receiveEvent");
955 onTestFinished(() => receiveEventSpy.mockRestore());
956 receiveEventSpy.mockImplementation(function (this: Engine, event) {
957 const result = originalReceiveEvent.call(this, event);
958 return Object.assign(result, {
959 [Symbol.dispose]: disposeSpy,
960 });
961 });
962 });
963
964 using _ = (await instance.sendEvent({
965 type: "test-event",
966 payload: { test: "data" },
967 })) as unknown as Disposable;
968
969 await vi.waitUntil(
970 async () => {
971 const status = await instance.status();
972 return status.status === "complete";
973 },
974 { timeout: 5000 },
975 );
976
977 expect(disposeSpy).not.toHaveBeenCalled();
978 });
979 });
980
981 describe("WorkflowHandle", () => {
982 describe("status()", () => {
983 it("should return running status for a workflow waiting for an event", async ({ expect }) => {
984 const id = uniqueId();
985 const binding = createBinding();
986 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
987
988 setTestWorkflowCallback(async (_event, step) => {
989 await step.waitForEvent("wait-for-event", {
990 type: "some-event",
991 timeout: "2 seconds",
992 });
993 return "completed";
994 });
995
996 await binding.create({ id });
997 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
998
999 const instance = await binding.get(id);
1000 const status = await instance.status();
1001
1002 expect(status.status).toBe("running");
1003 expect(status.output).toBeNull();
1004 expect(status.error).toBeUndefined();
1005
1006 // Terminate the waiting workflow so the init() RPC settles
1007 // before teardown (the 2-second waitForEvent timeout would
1008 // otherwise leave the workflow running past test end).
1009 await instance.terminate();
1010 });
1011
1012 it("should return complete status and output for a successful workflow", async ({ expect }) => {
1013 const id = uniqueId();
1014 const binding = createBinding();
1015 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1016 const expectedOutput = { result: "success", value: 42 };
1017
1018 setTestWorkflowCallback(async () => expectedOutput);
1019 await binding.create({ id });
1020 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1021
1022 const instance = await binding.get(id);
1023 const status = await instance.status();
1024
1025 expect(status.status).toBe("complete");
1026 expect(status.output).toEqual(expectedOutput);
1027 expect(status.error).toBeUndefined();
1028 });
1029
1030 it("should return errored status and error for a failed workflow", async ({ expect }) => {
1031 const id = uniqueId();
1032 const binding = createBinding();
1033 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1034
1035 setTestWorkflowCallback(async () => {
1036 throw new Error("Workflow failed intentionally");
1037 });
1038
1039 await binding.create({ id });
1040 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_FAILURE);
1041
1042 const instance = await binding.get(id);
1043 const status = await instance.status();
1044
1045 expect(status.status).toBe("errored");
1046 expect(status.error).toBeDefined();
1047 expect(status.error?.message).toBe("Workflow failed intentionally");
1048 expect(status.output).toBeNull();
1049 });
1050
1051 it("should return step outputs in __LOCAL_DEV_STEP_OUTPUTS", async ({ expect }) => {
1052 const id = uniqueId();
1053 const binding = createBinding();
1054 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1055
1056 setTestWorkflowCallback(async (_event, step) => {
1057 const step1Result = await step.do("step-1", async () => "result-from-step-1");
1058 const step2Result = await step.do("step-2", async () => ({
1059 data: "result-from-step-2",
1060 }));
1061 const step3Result = await step.do("step-3", async () => 123);
1062 return { step1Result, step2Result, step3Result };
1063 });
1064
1065 await binding.create({ id });
1066 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1067
1068 const instance = (await binding.get(id)) as WorkflowHandle;
1069 const status = await instance.status();
1070
1071 expect(status.status).toBe("complete");
1072 expect(status.__LOCAL_DEV_STEP_OUTPUTS).toHaveLength(3);
1073 expect(status.__LOCAL_DEV_STEP_OUTPUTS[0]).toBe("result-from-step-1");
1074 expect(status.__LOCAL_DEV_STEP_OUTPUTS[1]).toEqual({
1075 data: "result-from-step-2",
1076 });
1077 expect(status.__LOCAL_DEV_STEP_OUTPUTS[2]).toBe(123);
1078 });
1079
1080 it("should return terminated status for a terminated instance", async ({ expect }) => {
1081 const id = uniqueId();
1082 const binding = createBinding();
1083 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1084
1085 setTestWorkflowCallback(async (_event, step) => {
1086 await step.waitForEvent("wait-for-event", {
1087 type: "some-event",
1088 timeout: "1 second",
1089 });
1090 return "completed";
1091 });
1092
1093 await binding.create({ id });
1094 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
1095
1096 const instance = await binding.get(id);
1097 await instance.terminate();
1098
1099 const newInstance = await binding.get(id);
1100 const status = await newInstance.status();
1101
1102 expect(status.status).toBe("terminated");
1103 });
1104 });
1105
1106 describe("subscribe()", () => {
1107 it("finishes a pending next request when disposed", async ({ expect }) => {
1108 const started = Promise.withResolvers<void>();
1109 const pendingEvent =
1110 Promise.withResolvers<IteratorResult<WorkflowSubscriptionEvent, undefined>>();
1111 const onClose = vi.fn();
1112 const subscription = new WorkflowSubscriptionTarget(async () => {
1113 started.resolve();
1114 return pendingEvent.promise;
1115 }, onClose);
1116
1117 const result = subscription.next();
1118 await started.promise;
1119 subscription[Symbol.dispose]();
1120 pendingEvent.resolve({
1121 done: false,
1122 value: {
1123 type: "workflow_queued",
1124 instanceId: "disposed-subscription",
1125 eventId: 0,
1126 timestamp: Date.now(),
1127 },
1128 });
1129
1130 expect(await result).toEqual({ done: true, value: undefined });
1131 expect(onClose).toHaveBeenCalledOnce();
1132 });
1133
1134 it("closes after returning a terminal event", async ({ expect }) => {
1135 const onClose = vi.fn();
1136 const terminalEvent = {
1137 type: "workflow_completed",
1138 instanceId: "completed-subscription",
1139 eventId: 1,
1140 timestamp: Date.now(),
1141 output: "done",
1142 } as const satisfies WorkflowSubscriptionEvent;
1143 const subscription = new WorkflowSubscriptionTarget(
1144 () =>
1145 Promise.resolve({
1146 done: false as const,
1147 value: terminalEvent,
1148 }),
1149 onClose,
1150 );
1151
1152 expect(await subscription.next()).toEqual({
1153 done: false,
1154 value: terminalEvent,
1155 });
1156 expect(onClose).toHaveBeenCalledOnce();
1157 expect(await subscription.next()).toEqual({
1158 done: true,
1159 value: undefined,
1160 });
1161 });
1162
1163 it("streams historical events in log order through a disposable RPC target", async ({
1164 expect,
1165 }) => {
1166 const id = uniqueId();
1167 const binding = createBinding();
1168 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1169
1170 setTestWorkflowCallback(async (_event, step) => {
1171 await step.do(
1172 "subscription step",
1173 {
1174 retries: {
1175 limit: 3,
1176 delay: () => "2 seconds" as const,
1177 backoff: "linear",
1178 },
1179 timeout: "30 seconds",
1180 },
1181 async () => "step output",
1182 );
1183 return "workflow output";
1184 });
1185
1186 await binding.create({ id, params: { input: true } });
1187 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1188
1189 const instance = (await binding.get(id)) as WorkflowHandle;
1190 using subscription = await instance.subscribe();
1191 const events: Array<{
1192 eventId: number;
1193 type: string;
1194 [key: string]: unknown;
1195 }> = [];
1196 while (true) {
1197 const result = await subscription.next();
1198 if (result.done) {
1199 break;
1200 }
1201 events.push(result.value);
1202 }
1203
1204 expect(events.map(({ type }) => type)).toEqual([
1205 "workflow_queued",
1206 "workflow_started",
1207 "workflow_running",
1208 "step_started",
1209 "attempt_started",
1210 "attempt_completed",
1211 "step_completed",
1212 "workflow_completed",
1213 ]);
1214 expect(events[2]).toMatchObject({
1215 type: "workflow_running",
1216 });
1217 const firstEvent = events[0];
1218 if (firstEvent === undefined) {
1219 throw new Error("Expected subscription events");
1220 }
1221 expect(events.map(({ eventId }) => eventId)).toEqual(
1222 Array.from({ length: events.length }, (_, index) => firstEvent.eventId + index),
1223 );
1224 expect(events).toContainEqual(
1225 expect.objectContaining({
1226 type: "workflow_started",
1227 params: { input: true },
1228 }),
1229 );
1230 expect(events).toContainEqual(
1231 expect.objectContaining({
1232 type: "step_started",
1233 stepName: "subscription step-1",
1234 config: {
1235 retries: {
1236 limit: 3,
1237 delay: "[dynamic]",
1238 backoff: "linear",
1239 },
1240 timeout: "30 seconds",
1241 },
1242 }),
1243 );
1244 expect(events).toContainEqual(
1245 expect.objectContaining({
1246 type: "step_completed",
1247 stepName: "subscription step-1",
1248 output: "step output",
1249 }),
1250 );
1251 expect(events.at(-1)).toMatchObject({
1252 type: "workflow_completed",
1253 output: "workflow output",
1254 });
1255 expect(await subscription.next()).toEqual({
1256 done: true,
1257 value: undefined,
1258 });
1259 });
1260
1261 it("uses errored event names for Workflow and step failures", async ({ expect }) => {
1262 const id = uniqueId();
1263 const binding = createBinding();
1264 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1265
1266 setTestWorkflowCallback(async (_event, step) => {
1267 await step.do(
1268 "failing subscription step",
1269 { retries: { limit: 0, delay: 0 } },
1270 async () => {
1271 throw new Error("subscription failure");
1272 },
1273 );
1274 });
1275
1276 await binding.create({ id });
1277 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_FAILURE);
1278
1279 const instance = (await binding.get(id)) as WorkflowHandle;
1280 using subscription = await instance.subscribe();
1281 const eventTypes: Array<string> = [];
1282 while (true) {
1283 const result = await subscription.next();
1284 if (result.done) {
1285 break;
1286 }
1287 eventTypes.push(result.value.type);
1288 }
1289
1290 expect(eventTypes).toContain("attempt_errored");
1291 expect(eventTypes).toContain("step_errored");
1292 expect(eventTypes.at(-1)).toBe("workflow_errored");
1293 });
1294
1295 it("includes the resolved rollback config in rollback step events", async ({ expect }) => {
1296 const id = uniqueId();
1297 const binding = createBinding();
1298 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1299
1300 setTestWorkflowCallback(async (_event, step) => {
1301 await step.do("rollback subscription step", async () => "step output", {
1302 rollback: async () => {},
1303 rollbackConfig: {
1304 retries: {
1305 limit: 0,
1306 delay: "1 second",
1307 backoff: "constant",
1308 },
1309 timeout: "30 seconds",
1310 },
1311 });
1312 throw new Error("trigger rollback");
1313 });
1314
1315 await binding.create({ id });
1316 await waitUntilLogEvent(engineStub, InstanceEvent.ROLLBACK_COMPLETE);
1317
1318 const instance = (await binding.get(id)) as WorkflowHandle;
1319 using subscription = await instance.subscribe({
1320 filter: ["rollback_step_started", "workflow_errored"],
1321 });
1322 expect(await subscription.next()).toMatchObject({
1323 done: false,
1324 value: {
1325 type: "rollback_step_started",
1326 stepName: "rollback subscription step-1",
1327 config: {
1328 retries: {
1329 limit: 0,
1330 delay: "1 second",
1331 backoff: "constant",
1332 },
1333 timeout: "30 seconds",
1334 },
1335 },
1336 });
1337 });
1338
1339 it("returns stored structured and streamed step outputs", async ({ expect }) => {
1340 const id = uniqueId();
1341 const binding = createBinding();
1342 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1343
1344 setTestWorkflowCallback(async (_event, step) => {
1345 await step.do("structured output", async () => ({ total: 1n }));
1346 const stream = await step.do("stream output", async () => {
1347 return new ReadableStream<Uint8Array>({
1348 start(controller) {
1349 controller.enqueue(new TextEncoder().encode("streamed step output"));
1350 controller.close();
1351 },
1352 });
1353 });
1354 await new Response(stream as ReadableStream<Uint8Array>).arrayBuffer();
1355 await step.do("sensitive output", { sensitive: "output" }, async () => "secret");
1356 await step.do("undefined output", async () => undefined);
1357 await step.do("null output", async () => null);
1358 return "done";
1359 });
1360
1361 await binding.create({ id });
1362 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1363
1364 const instance = (await binding.get(id)) as WorkflowHandle;
1365 using subscription = await instance.subscribe({
1366 filter: ["step_completed", "workflow_completed"],
1367 });
1368
1369 const structuredResult = await subscription.next();
1370 if (structuredResult.done || structuredResult.value.type !== "step_completed") {
1371 throw new Error("Expected a structured step output event");
1372 }
1373 const structuredEvent = structuredResult.value;
1374 expect(structuredEvent).toMatchObject({
1375 type: "step_completed",
1376 stepName: "structured output-1",
1377 output: { total: 1n },
1378 });
1379
1380 const streamResult = await subscription.next();
1381 if (streamResult.done || streamResult.value.type !== "step_completed") {
1382 throw new Error("Expected a streamed step output event");
1383 }
1384 const streamEvent = streamResult.value;
1385 expect(streamEvent).toMatchObject({
1386 type: "step_completed",
1387 stepName: "stream output-1",
1388 });
1389 expect(streamEvent.output).toBeInstanceOf(ReadableStream);
1390 expect(await new Response(streamEvent.output as ReadableStream<Uint8Array>).text()).toBe(
1391 "streamed step output",
1392 );
1393
1394 expect(await subscription.next()).toMatchObject({
1395 done: false,
1396 value: {
1397 type: "step_completed",
1398 stepName: "sensitive output-1",
1399 output: "[REDACTED]",
1400 },
1401 });
1402 const undefinedResult = await subscription.next();
1403 expect(undefinedResult).toMatchObject({
1404 done: false,
1405 value: {
1406 type: "step_completed",
1407 stepName: "undefined output-1",
1408 },
1409 });
1410 if (!undefinedResult.done) {
1411 expect("output" in undefinedResult.value).toBe(false);
1412 }
1413 expect(await subscription.next()).toMatchObject({
1414 done: false,
1415 value: {
1416 type: "step_completed",
1417 stepName: "null output-1",
1418 output: null,
1419 },
1420 });
1421 expect(await subscription.next()).toMatchObject({
1422 done: false,
1423 value: { type: "workflow_completed", output: "done" },
1424 });
1425 });
1426
1427 it("returns full structured workflow outputs while keeping logs JSON-safe", async ({
1428 expect,
1429 }) => {
1430 const id = uniqueId();
1431 const binding = createBinding();
1432 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1433 const createWorkflowOutput = () => ({
1434 count: 42n,
1435 typedArray: new Uint16Array([1, 2]),
1436 arrayBuffer: new Uint8Array([3, 4, 5]).buffer,
1437 completedAt: new Date("2026-09-09T12:00:00.000Z"),
1438 values: new Map([["nested", new Set([6n, 7n])]]),
1439 });
1440
1441 setTestWorkflowCallback(async () => createWorkflowOutput());
1442 await binding.create({ id });
1443 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1444
1445 const instance = (await binding.get(id)) as WorkflowHandle;
1446 using subscription = await instance.subscribe({
1447 filter: ["workflow_completed"],
1448 });
1449 const result = await subscription.next();
1450 if (result.done || result.value.type !== "workflow_completed") {
1451 throw new Error("Expected a completed Workflow event");
1452 }
1453 expect(result.value.output).toEqual(createWorkflowOutput());
1454
1455 const logs = (await engineStub.readLogs()) as EngineLogs;
1456 const completedLog = logs.logs.find((log) => log.event === InstanceEvent.WORKFLOW_SUCCESS);
1457 expect(completedLog?.metadata.result).toEqual({
1458 count: "[BigInt(42)]",
1459 typedArray: "[Uint16Array(4 bytes)]",
1460 arrayBuffer: "[ArrayBuffer(3 bytes)]",
1461 completedAt: "2026-09-09T12:00:00.000Z",
1462 values: {},
1463 });
1464 });
1465
1466 it("rejects when a stored streamed step output is corrupt", async ({ expect }) => {
1467 const id = uniqueId();
1468 const binding = createBinding();
1469 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1470
1471 setTestWorkflowCallback(async (_event, step) => {
1472 const stream = await step.do("stream output", async () => {
1473 return new ReadableStream<Uint8Array>({
1474 start(controller) {
1475 controller.enqueue(new TextEncoder().encode("streamed output"));
1476 controller.close();
1477 },
1478 });
1479 });
1480 await new Response(stream as ReadableStream<Uint8Array>).arrayBuffer();
1481 });
1482
1483 await binding.create({ id });
1484 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1485 await runInDurableObject(engineStub, (_engine, state) => {
1486 const step = state.storage.sql
1487 .exec<{ groupKey: string }>(
1488 "SELECT groupKey FROM states WHERE event = ? LIMIT 1",
1489 InstanceEvent.STEP_SUCCESS,
1490 )
1491 .one();
1492 if (step === null) {
1493 throw new Error("Expected a completed step");
1494 }
1495 state.storage.sql.exec(
1496 "DELETE FROM streaming_step_chunks WHERE cache_key = ?",
1497 step.groupKey,
1498 );
1499 });
1500
1501 await runInDurableObject(engineStub, async (engine) => {
1502 const subscription = await engine.subscribe({
1503 filter: ["step_completed"],
1504 });
1505 await expect(subscription.next()).rejects.toThrow(
1506 "Step has completed but its stored stream output is corrupt or incomplete",
1507 );
1508 expect(await subscription.next()).toEqual({
1509 done: true,
1510 value: undefined,
1511 });
1512 });
1513 });
1514
1515 it("returns done when the cursor is at or after a terminal event", async ({ expect }) => {
1516 const id = uniqueId();
1517 const binding = createBinding();
1518 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1519
1520 setTestWorkflowCallback(async () => "done");
1521 await binding.create({ id });
1522 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1523
1524 const getEventId = async (event: InstanceEvent) => {
1525 const logs = (await engineStub.readDetailedLogs()) as Array<{
1526 id: number;
1527 event: InstanceEvent;
1528 }>;
1529 const eventId = logs.find((log) => log.event === event)?.id;
1530 if (eventId === undefined) {
1531 throw new Error(`Expected Workflow event ${event}`);
1532 }
1533 return eventId;
1534 };
1535 const expectDoneAtCursor = async (cursor: number) => {
1536 const instance = (await binding.get(id)) as WorkflowHandle;
1537 using subscription = await instance.subscribe({ cursor });
1538 const result = Promise.resolve(subscription.next());
1539 await expect(
1540 Promise.race([result, scheduler.wait(1000).then(() => "timed out" as const)]),
1541 ).resolves.toEqual({ done: true, value: undefined });
1542 };
1543
1544 const terminalEventId = await getEventId(InstanceEvent.WORKFLOW_SUCCESS);
1545 await expectDoneAtCursor(terminalEventId);
1546
1547 await runInDurableObject(engineStub, (engine) => {
1548 engine.writeLog(InstanceEvent.__INTERNAL_PROD, null, null, {});
1549 });
1550 const laterInternalEventId = await getEventId(InstanceEvent.__INTERNAL_PROD);
1551 await expectDoneAtCursor(laterInternalEventId);
1552 });
1553
1554 it("waits for live events and applies cursor and filter options", async ({ expect }) => {
1555 const id = uniqueId();
1556 const binding = createBinding();
1557 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1558
1559 setTestWorkflowCallback(async (_event, step) => {
1560 await step.waitForEvent("subscription wait", {
1561 type: "continue",
1562 timeout: "10 seconds",
1563 });
1564 return "done";
1565 });
1566
1567 await binding.create({ id });
1568 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
1569
1570 const logs = (await engineStub.readDetailedLogs()) as Array<{
1571 id: number;
1572 event: InstanceEvent;
1573 }>;
1574 const startEventId = logs.find(({ event }) => event === InstanceEvent.WORKFLOW_START)?.id;
1575 if (startEventId === undefined) {
1576 throw new Error("Expected a workflow start event");
1577 }
1578
1579 const instance = (await binding.get(id)) as WorkflowHandle;
1580 using subscription = await instance.subscribe({
1581 cursor: startEventId,
1582 filter: ["workflow_queued", "wait_started", "workflow_completed"],
1583 });
1584 expect(await subscription.next()).toMatchObject({
1585 done: false,
1586 value: {
1587 type: "wait_started",
1588 stepName: "subscription wait-1",
1589 eventType: "continue",
1590 },
1591 });
1592
1593 const terminalEvent = subscription.next();
1594 await instance.sendEvent({ type: "continue", payload: null });
1595 expect(await terminalEvent).toMatchObject({
1596 done: false,
1597 value: { type: "workflow_completed", output: "done" },
1598 });
1599 expect(await subscription.next()).toEqual({
1600 done: true,
1601 value: undefined,
1602 });
1603 });
1604
1605 it("streams explicit Workflow status events", async ({ expect }) => {
1606 const id = uniqueId();
1607 const binding = createBinding();
1608 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1609
1610 setTestWorkflowCallback(async (_event, step) => {
1611 await step.waitForEvent("status subscription wait", {
1612 type: "continue",
1613 timeout: "10 seconds",
1614 });
1615 return "done";
1616 });
1617
1618 await binding.create({ id });
1619 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
1620
1621 const instance = (await binding.get(id)) as WorkflowHandle;
1622 using subscription = await instance.subscribe({
1623 filter: [
1624 "workflow_queued",
1625 "workflow_running",
1626 "workflow_waiting",
1627 "workflow_waiting_for_pause",
1628 "workflow_paused",
1629 "workflow_completed",
1630 ],
1631 });
1632
1633 expect(await subscription.next()).toMatchObject({
1634 done: false,
1635 value: { type: "workflow_queued" },
1636 });
1637 expect(await subscription.next()).toMatchObject({
1638 done: false,
1639 value: { type: "workflow_running" },
1640 });
1641
1642 await engineStub.setStatus(0, id, InstanceStatus.Waiting);
1643 expect(await subscription.next()).toMatchObject({
1644 done: false,
1645 value: { type: "workflow_waiting" },
1646 });
1647
1648 await engineStub.setStatus(0, id, InstanceStatus.Queued);
1649 expect(await subscription.next()).toMatchObject({
1650 done: false,
1651 value: { type: "workflow_queued" },
1652 });
1653 await engineStub.setStatus(0, id, InstanceStatus.Running);
1654 expect(await subscription.next()).toMatchObject({
1655 done: false,
1656 value: { type: "workflow_running" },
1657 });
1658
1659 await engineStub.setStatus(0, id, InstanceStatus.WaitingForPause);
1660 expect(await subscription.next()).toMatchObject({
1661 done: false,
1662 value: { type: "workflow_waiting_for_pause" },
1663 });
1664 await engineStub.setStatus(0, id, InstanceStatus.Paused);
1665 expect(await subscription.next()).toMatchObject({
1666 done: false,
1667 value: { type: "workflow_paused" },
1668 });
1669
1670 await engineStub.setStatus(0, id, InstanceStatus.Running);
1671 expect(await subscription.next()).toMatchObject({
1672 done: false,
1673 value: { type: "workflow_running" },
1674 });
1675 const terminalEvent = subscription.next();
1676 await instance.sendEvent({ type: "continue", payload: null });
1677 expect(await terminalEvent).toMatchObject({
1678 done: false,
1679 value: { type: "workflow_completed", output: "done" },
1680 });
1681 expect(await subscription.next()).toEqual({
1682 done: true,
1683 value: undefined,
1684 });
1685
1686 const explicitStatusEventTypes = new Set<InstanceEvent>([
1687 InstanceEvent.WORKFLOW_QUEUED,
1688 InstanceEvent.WORKFLOW_RUNNING,
1689 InstanceEvent.WORKFLOW_PAUSED,
1690 InstanceEvent.WORKFLOW_WAITING_FOR_PAUSE,
1691 InstanceEvent.WORKFLOW_WAITING,
1692 ]);
1693 const statusEvents = (
1694 (await engineStub.readDetailedLogs()) as Array<{
1695 event: InstanceEvent;
1696 }>
1697 )
1698 .filter(({ event }) => explicitStatusEventTypes.has(event))
1699 .map(({ event }) => event);
1700 expect(statusEvents).toEqual([
1701 InstanceEvent.WORKFLOW_QUEUED,
1702 InstanceEvent.WORKFLOW_RUNNING,
1703 InstanceEvent.WORKFLOW_WAITING,
1704 InstanceEvent.WORKFLOW_QUEUED,
1705 InstanceEvent.WORKFLOW_RUNNING,
1706 InstanceEvent.WORKFLOW_WAITING_FOR_PAUSE,
1707 InstanceEvent.WORKFLOW_PAUSED,
1708 InstanceEvent.WORKFLOW_RUNNING,
1709 ]);
1710 });
1711
1712 it("rejects invalid subscription options", async ({ expect }) => {
1713 const id = uniqueId();
1714 const binding = createBinding();
1715 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1716
1717 setTestWorkflowCallback(async () => undefined);
1718 await binding.create({ id });
1719 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1720
1721 const instance = await binding.get(id);
1722 const unsafeInstance = instance as unknown as {
1723 subscribe(options: unknown): Promise<WorkflowSubscription>;
1724 };
1725 const invalidOptions: Array<[description: string, options: unknown]> = [
1726 ["null", null],
1727 ["an array", []],
1728 ["a negative cursor", { cursor: -1 }],
1729 ["a fractional cursor", { cursor: 1.5 }],
1730 ["an unsafe cursor", { cursor: Number.MAX_SAFE_INTEGER + 1 }],
1731 ["a non-array filter", { filter: "workflow_started" }],
1732 ["a non-string event filter", { filter: [42] }],
1733 ["an unknown event filter", { filter: ["does_not_exist"] }],
1734 ["the internal event filter", { filter: ["internal"] }],
1735 ["an unknown option", { unexpected: true }],
1736 ];
1737
1738 for (const [description, options] of invalidOptions) {
1739 await expect(unsafeInstance.subscribe(options), description).rejects.toThrow(
1740 "Invalid Workflow subscription options",
1741 );
1742 }
1743 });
1744
1745 it("streams a persisted termination event after the Engine aborts", async ({ expect }) => {
1746 const id = uniqueId();
1747 const binding = createBinding();
1748 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1749
1750 setTestWorkflowCallback(async (_event, step) => {
1751 await step.waitForEvent("termination wait", {
1752 type: "never",
1753 timeout: "10 seconds",
1754 });
1755 });
1756
1757 await binding.create({ id });
1758 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
1759 const instance = await binding.get(id);
1760 await instance.terminate();
1761
1762 const terminatedInstance = (await binding.get(id)) as WorkflowHandle;
1763 using subscription = await terminatedInstance.subscribe({
1764 filter: ["workflow_terminated"],
1765 });
1766
1767 expect(await subscription.next()).toMatchObject({
1768 done: false,
1769 value: { type: "workflow_terminated" },
1770 });
1771 });
1772 });
1773
1774 describe("sendEvent()", () => {
1775 it("should deliver event payload to a waiting workflow", async ({ expect }) => {
1776 const id = uniqueId();
1777 const binding = createBinding();
1778 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1779
1780 setTestWorkflowCallback(async (_event, step) => {
1781 const receivedEvent = await step.waitForEvent("wait-for-event", {
1782 type: "my-event-type",
1783 timeout: "2 seconds",
1784 });
1785 return receivedEvent;
1786 });
1787
1788 await binding.create({ id });
1789 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
1790
1791 const instance = await binding.get(id);
1792 const eventPayload = { message: "hello", count: 42 };
1793
1794 await instance.sendEvent({
1795 type: "my-event-type",
1796 payload: eventPayload,
1797 });
1798
1799 await vi.waitUntil(
1800 async () => {
1801 const status = await instance.status();
1802 return status.status === "complete";
1803 },
1804 { timeout: 5000 },
1805 );
1806
1807 const status = await instance.status();
1808 expect(status.output).toMatchObject({
1809 payload: eventPayload,
1810 type: "my-event-type",
1811 });
1812 });
1813
1814 it("should handle multiple sequential events", async ({ expect }) => {
1815 const id = uniqueId();
1816 const binding = createBinding();
1817 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1818
1819 setTestWorkflowCallback(async (_event, step) => {
1820 const event1 = await step.waitForEvent("wait-1", {
1821 type: "event-type-1",
1822 timeout: "10 seconds",
1823 });
1824 const event2 = await step.waitForEvent("wait-2", {
1825 type: "event-type-2",
1826 timeout: "10 seconds",
1827 });
1828 return { first: event1.payload, second: event2.payload };
1829 });
1830
1831 await binding.create({ id });
1832 const instance = await binding.get(id);
1833
1834 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
1835 await instance.sendEvent({
1836 type: "event-type-1",
1837 payload: { value: "first" },
1838 });
1839
1840 // Wait for the second waitForEvent
1841 await vi.waitUntil(
1842 async () => {
1843 const logs = (await engineStub.readLogs()) as EngineLogs;
1844 const waitStarts = logs.logs.filter((log) => log.event === InstanceEvent.WAIT_START);
1845 return waitStarts.length === 2;
1846 },
1847 { timeout: 5000 },
1848 );
1849
1850 await instance.sendEvent({
1851 type: "event-type-2",
1852 payload: { value: "second" },
1853 });
1854
1855 await vi.waitUntil(
1856 async () => {
1857 const status = await instance.status();
1858 return status.status === "complete";
1859 },
1860 { timeout: 5000 },
1861 );
1862
1863 const status = await instance.status();
1864 expect(status.output).toEqual({
1865 first: { value: "first" },
1866 second: { value: "second" },
1867 });
1868 });
1869 });
1870
1871 describe("terminate()", () => {
1872 it("should terminate a running workflow instance", async ({ expect }) => {
1873 const id = uniqueId();
1874 const binding = createBinding();
1875 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1876
1877 setTestWorkflowCallback(async (_event, step) => {
1878 await step.waitForEvent("wait-for-event", {
1879 type: "some-event",
1880 timeout: "1 second",
1881 });
1882 await step.do("should not be called", async () => {
1883 return "should not be called";
1884 });
1885 return "should never complete";
1886 });
1887
1888 await binding.create({ id });
1889 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
1890
1891 const instance = await binding.get(id);
1892 await instance.terminate();
1893
1894 // Get a new stub since the engine was aborted
1895 const newEngineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1896
1897 const status = await runInDurableObject(newEngineStub, (engine) => {
1898 return engine.getStatus();
1899 });
1900 expect(status).toBe(InstanceStatus.Terminated);
1901
1902 const logs = (await newEngineStub.readLogs()) as EngineLogs;
1903 const hasTerminatedEvent = logs.logs.some(
1904 (log) => log.event === InstanceEvent.WORKFLOW_TERMINATED,
1905 );
1906 expect(hasTerminatedEvent).toBe(true);
1907
1908 // assert that step.do never started
1909 const hasStepStart = logs.logs.some((log) => log.event === InstanceEvent.STEP_START);
1910 expect(hasStepStart).toBe(false);
1911 });
1912 });
1913
1914 describe("restart()", () => {
1915 it("should restart a workflow instance", async ({ expect }) => {
1916 const id = uniqueId();
1917 const binding = createBinding();
1918 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1919
1920 setTestWorkflowCallback(async (_event, step) => {
1921 await step.sleep("sleep", 250);
1922 return "complete";
1923 });
1924
1925 await binding.create({ id });
1926 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
1927
1928 let instance = await binding.get(id);
1929 let status = await instance.status();
1930 expect(status.status).toBe("complete");
1931
1932 // restart() aborts the old DO, gets a fresh stub, and calls attemptRestart()
1933 // The service binding (USER_WORKFLOW) survives the abort, so no re-setup needed
1934 await instance.restart();
1935
1936 const statusAfterRestart = await instance.status();
1937 expect(statusAfterRestart.status).toBe("running");
1938
1939 // Wait for the restarted workflow to complete via status polling
1940 await vi.waitUntil(
1941 async () => {
1942 const s = await instance.status();
1943 return s.status === "complete";
1944 },
1945 { timeout: 5000 },
1946 );
1947
1948 // Verify second run completed
1949 instance = await binding.get(id);
1950 status = await instance.status();
1951 expect(status.status).toBe("complete");
1952 });
1953 });
1954
1955 describe("pause()", () => {
1956 it("should pause a running workflow", async ({ expect }) => {
1957 const id = uniqueId();
1958 const binding = createBinding();
1959 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1960
1961 setTestWorkflowCallback(async (_event, step) => {
1962 await step.do("long-step", async () => {
1963 await scheduler.wait(500);
1964 return "result-1";
1965 });
1966 // step-2 should never run because pause takes effect after long-step
1967 await step.do("step-2", async () => "result-2");
1968 return "done";
1969 });
1970
1971 await binding.create({ id });
1972 await waitUntilLogEvent(engineStub, InstanceEvent.STEP_START);
1973
1974 const instance = await binding.get(id);
1975
1976 // Pause while long-step is in flight
1977 await instance.pause();
1978
1979 await vi.waitUntil(
1980 async () => {
1981 const s = await instance.status();
1982 return s.status === "paused";
1983 },
1984 { timeout: 5000 },
1985 );
1986
1987 const finalStatus = await instance.status();
1988 expect(finalStatus.status).toBe("paused");
1989 });
1990 });
1991
1992 describe("resume()", () => {
1993 it("should resume a paused workflow and complete it", async ({ expect }) => {
1994 const id = uniqueId();
1995 const binding = createBinding();
1996 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
1997
1998 setTestWorkflowCallback(async (_event, step) => {
1999 await step.do("long-step", async () => {
2000 await scheduler.wait(500);
2001 return "result-1";
2002 });
2003 await step.do("step-2", async () => "result-2");
2004 return "all-done";
2005 });
2006
2007 await binding.create({ id });
2008 await waitUntilLogEvent(engineStub, InstanceEvent.STEP_START);
2009
2010 const instance = await binding.get(id);
2011
2012 // Pause while long-step is in flight
2013 await instance.pause();
2014
2015 await vi.waitUntil(
2016 async () => {
2017 const s = await instance.status();
2018 return s.status === "paused";
2019 },
2020 { timeout: 5000 },
2021 );
2022
2023 await instance.resume();
2024
2025 await vi.waitUntil(
2026 async () => {
2027 const s = await instance.status();
2028 return s.status === "complete";
2029 },
2030 { timeout: 5000 },
2031 );
2032
2033 const finalStatus = await instance.status();
2034 expect(finalStatus.status).toBe("complete");
2035 expect(finalStatus.output).toBe("all-done");
2036 });
2037
2038 it("should cancel a pending pause when resume is called before step finishes", async ({
2039 expect,
2040 }) => {
2041 const id = uniqueId();
2042 const binding = createBinding();
2043 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
2044
2045 setTestWorkflowCallback(async (_event, step) => {
2046 await step.do("long-step", async () => {
2047 await scheduler.wait(1000);
2048 return "long-result";
2049 });
2050 await step.do("step-after", async () => "final-result");
2051 return "completed";
2052 });
2053
2054 await binding.create({ id });
2055 await waitUntilLogEvent(engineStub, InstanceEvent.STEP_START);
2056
2057 const instance = await binding.get(id);
2058
2059 // Pause while long-step is in flight — sets WaitingForPause
2060 await instance.pause();
2061
2062 const statusAfterPause = await instance.status();
2063 expect(statusAfterPause.status).toBe("waitingForPause");
2064
2065 // resume before the step finishes — this should cancel the pending pause
2066 await instance.resume();
2067
2068 // status should go back to Running
2069 const statusAfterResume = await instance.status();
2070 expect(statusAfterResume.status).toBe("running");
2071
2072 await vi.waitUntil(
2073 async () => {
2074 const s = await instance.status();
2075 return s.status === "complete";
2076 },
2077 { timeout: 5000 },
2078 );
2079
2080 const finalStatus = await instance.status();
2081 expect(finalStatus.status).toBe("complete");
2082 expect(finalStatus.output).toBe("completed");
2083 });
2084 });
2085 });
2086
2087 describe("WorkflowBinding instance methods by id", () => {
2088 it("getInstance() returns the id of an existing instance", async ({ expect }) => {
2089 const id = uniqueId();
2090 const binding = createBinding();
2091 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
2092
2093 setTestWorkflowCallback(async () => "done");
2094
2095 await binding.create({ id });
2096 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
2097
2098 expect(await binding.getInstance(id)).toEqual({ id });
2099 });
2100
2101 it.for([
2102 ["getInstance"],
2103 ["pause"],
2104 ["resume"],
2105 ["terminate"],
2106 ["restart"],
2107 ["status"],
2108 ["sendEvent"],
2109 ["subscribe"],
2110 ] as const)(
2111 "%s() rejects with instance.not_found for an unknown id",
2112 async ([method], { expect }) => {
2113 const binding = createBinding();
2114
2115 await expect(
2116 binding[method](uniqueId("missing"), {
2117 type: "event",
2118 payload: null,
2119 }),
2120 ).rejects.toThrow("instance.not_found");
2121 },
2122 );
2123
2124 it("status() and sendEvent() address an instance by id", async ({ expect }) => {
2125 const id = uniqueId();
2126 const binding = createBinding();
2127 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
2128
2129 setTestWorkflowCallback(async (_event, step) => {
2130 const received = await step.waitForEvent("wait-for-event", {
2131 type: "some-event",
2132 timeout: "10 seconds",
2133 });
2134 return received.payload;
2135 });
2136
2137 await binding.create({ id });
2138 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
2139 expect((await binding.status(id)).status).toBe("running");
2140
2141 await binding.sendEvent(id, { type: "some-event", payload: "hello" });
2142
2143 await vi.waitUntil(async () => (await binding.status(id)).status === "complete", {
2144 timeout: 5000,
2145 });
2146 expect((await binding.status(id)).output).toBe("hello");
2147 });
2148
2149 it("pause() and resume() address an instance by id", async ({ expect }) => {
2150 const id = uniqueId();
2151 const binding = createBinding();
2152 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
2153
2154 setTestWorkflowCallback(async (_event, step) => {
2155 await step.do("long-step", async () => {
2156 await scheduler.wait(500);
2157 return "result-1";
2158 });
2159 await step.do("step-2", async () => "result-2");
2160 return "all-done";
2161 });
2162
2163 await binding.create({ id });
2164 await waitUntilLogEvent(engineStub, InstanceEvent.STEP_START);
2165
2166 await binding.pause(id);
2167 await vi.waitUntil(async () => (await binding.status(id)).status === "paused", {
2168 timeout: 5000,
2169 });
2170
2171 await binding.resume(id);
2172 await vi.waitUntil(async () => (await binding.status(id)).status === "complete", {
2173 timeout: 5000,
2174 });
2175 expect((await binding.status(id)).output).toBe("all-done");
2176 });
2177
2178 it("terminate() addresses an instance by id", async ({ expect }) => {
2179 const id = uniqueId();
2180 const binding = createBinding();
2181 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
2182
2183 setTestWorkflowCallback(async (_event, step) => {
2184 await step.waitForEvent("wait-for-event", {
2185 type: "some-event",
2186 timeout: "10 seconds",
2187 });
2188 return "should never complete";
2189 });
2190
2191 await binding.create({ id });
2192 await waitUntilLogEvent(engineStub, InstanceEvent.WAIT_START);
2193
2194 await binding.terminate(id);
2195
2196 expect((await binding.status(id)).status).toBe("terminated");
2197 });
2198
2199 it("restart() addresses an instance by id", async ({ expect }) => {
2200 const id = uniqueId();
2201 const binding = createBinding();
2202 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
2203 let runs = 0;
2204
2205 setTestWorkflowCallback(async () => {
2206 runs++;
2207 return "complete";
2208 });
2209
2210 await binding.create({ id });
2211 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
2212
2213 await binding.restart(id);
2214
2215 await vi.waitUntil(async () => runs === 2 && (await binding.status(id)).status === "complete", {
2216 timeout: 5000,
2217 });
2218 expect(runs).toBe(2);
2219 });
2220
2221 it("subscribe() addresses an instance by id", async ({ expect }) => {
2222 const id = uniqueId();
2223 const binding = createBinding();
2224 const engineStub = env.ENGINE.get(env.ENGINE.idFromName(id));
2225
2226 setTestWorkflowCallback(async () => "done");
2227
2228 await binding.create({ id });
2229 await waitUntilLogEvent(engineStub, InstanceEvent.WORKFLOW_SUCCESS);
2230
2231 using subscription = (await binding.subscribe(id)) as unknown as WorkflowSubscription;
2232 const types: string[] = [];
2233 while (true) {
2234 const result = await subscription.next();
2235 if (result.done) {
2236 break;
2237 }
2238 types.push(result.value.type);
2239 }
2240
2241 expect(types.at(-1)).toBe("workflow_completed");
2242 });
2243 });