Skip to content
File

Blob: tests/worker/dispatch/workflows/runtime.test.ts

typescript74 lines
1import { introspectWorkflowInstance } from "cloudflare:test";
2import { env } from "cloudflare:workers";
3import { describe, expect, it } from "vitest";
4 
5import { DEFAULT_EXECUTION_RUNTIME } from "@/contracts";
6import { AcceptedRunSnapshot } from "@/worker/contracts";
7 
8import { acceptManualRunWithoutAlarm } from "../../../helpers/project-do";
9import { readProjectDoRows, seedProject, seedUser } from "../../../helpers/runtime";
10import { registerWorkerRuntimeHooks } from "../../../helpers/worker-hooks";
11 
12describe("run workflows runtime", () => {
13 registerWorkerRuntimeHooks();
14 
15 it("finalizes the run when the execute step times out at the workflow layer", async () => {
16 const user = await seedUser({
17 email: "workflow-runtime-timeout@example.com",
18 slug: "workflow-runtime-timeout-user",
19 });
20 const project = await seedProject(user, {
21 projectSlug: "workflow-runtime-timeout-project",
22 dispatchMode: "workflows",
23 });
24 const projectStub = env.PROJECT_DO.getByName(project.id);
25 const accepted = await acceptManualRunWithoutAlarm(projectStub, {
26 projectId: project.id,
27 triggeredByUserId: user.id,
28 branch: project.defaultBranch,
29 });
30 const snapshot = AcceptedRunSnapshot.assertDecode({
31 runId: accepted.runId,
32 projectId: project.id,
33 triggerType: "manual",
34 triggeredByUserId: user.id,
35 branch: project.defaultBranch,
36 commitSha: null,
37 repoUrl: project.repoUrl,
38 configPath: project.configPath,
39 dispatchMode: "workflows",
40 executionRuntime: DEFAULT_EXECUTION_RUNTIME,
41 queuedAt: accepted.queuedAt,
42 });
43 
44 const instance = await introspectWorkflowInstance(env.RUN_WORKFLOWS, accepted.runId);
45 try {
46 await instance.modify(async (modifier) => {
47 await modifier.forceStepTimeout({ name: "execute run" });
48 });
49 
50 await env.RUN_WORKFLOWS.create({
51 id: accepted.runId,
52 params: snapshot,
53 });
54 
55 await expect(instance.waitForStatus("complete")).resolves.not.toThrow();
56 await expect(instance.getOutput()).resolves.toEqual({
57 kind: "executed",
58 terminalStatus: "failed",
59 });
60 
61 const rows = await readProjectDoRows(project.id);
62 expect(rows.state?.activeRunId).toBeNull();
63 expect(rows.runs[0]?.status).toBe("failed");
64 expect(rows.runs[0]?.dispatchStatus).toBe("terminal");
65 
66 const runMeta = await env.RUN_DO.getByName(accepted.runId).getRunSummary(accepted.runId);
67 expect(runMeta?.status).toBe("failed");
68 expect(runMeta?.finishedAt).not.toBeNull();
69 } finally {
70 await instance.dispose();
71 }
72 }, 15_000);
73});