Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example: using introspectWorkflow with a Durable Object in a plain Todo app
- // Replace placeholders (UserDurableObject, app, triggerCompleteTaskWorkflow, WORKFLOW binding) with your own.
- import { env, runInDurableObject, introspectWorkflow } from "cloudflare:test";
- import { expect, it, vi } from "vitest";
- // import { UserDurableObject } from "../src"; // <-- your DO class
- // import { app } from "../src"; // <-- your worker entry
- it("introspectWorkflow with DO (Todo: complete task)", async () => {
- vi.clearAllMocks();
- const taskId = "task-introspect-DO-123";
- const idemKey = crypto.randomUUID();
- // What each workflow step should yield for this test run
- const mockUserSession = {
- userId: "user-" + Date.now(),
- sessionToken: "mock-session",
- expiresAt: new Date(Date.now() + 60 * 60 * 1000),
- };
- const mockCompleteResult = {
- ok: true,
- taskId,
- status: "completed",
- };
- // Intercept the workflow BEFORE hitting the worker
- await using introspector = await introspectWorkflow(env.TODO_COMPLETE_WORKFLOW);
- await introspector.modifyAll(async (m) => {
- await m.disableSleeps();
- await m.mockStepResult({ name: "init" }, "complete");
- await m.mockStepResult({ name: "load user" }, mockUserSession);
- await m.mockStepResult({ name: "refresh-session" }, mockUserSession);
- await m.mockStepResult({ name: "complete task" }, mockCompleteResult);
- });
- // public-safe headers
- const headers = {
- "x-app-org-id": "demo-org",
- "x-app-project-id": "demo-project",
- "x-app-api-key": "demo__token",
- "x-app-user-id": `user-${Math.random().toString(36).slice(2)}`,
- };
- // Seed DO with a user + an open task
- const userRecord = {
- id: headers["x-app-user-id"],
- projectId: headers["x-app-project-id"],
- displayName: "Test User",
- };
- const openTask = {
- id: taskId,
- userId: headers["x-app-user-id"],
- title: "Write docs",
- notes: "Make sure to include examples",
- status: "open" as const,
- createdAt: new Date(),
- expiresAt: new Date(Date.now() + 60_000), // still valid
- };
- const response = await runInDurableObject(orgDb, async (instance: any /* UserDurableObject */) => {
- expect(instance).toBeTruthy();
- // Replace with your DO helpers
- await instance.insertUser?.(userRecord);
- await instance.insertTask?.(openTask);
- return app.request(
- `/v0/tasks/${taskId}/complete?idem=${idemKey}`,
- {
- method: "PUT",
- headers: { ...headers, "Content-Type": "application/json" },
- body: JSON.stringify({ note: "completed via test", source: "e2e" }),
- },
- {
- ...env,
- USER_DO: {
- get: () => instance,
- idFromName: (name: string) => name,
- },
- }
- );
- });
- // Basic HTTP assertions
- const json = await response.json<any>();
- expect(response.status).toBe(200);
- expect(typeof json.workflowRunId).toBe("string");
- // Exactly one workflow instance should be intercepted
- const instances = introspector.get();
- expect(instances.length).toBe(1);
- const wf = instances[0];
- // Step order and results
- expect(await wf.waitForStepResult({ name: "init" })).toBe("complete");
- expect(await wf.waitForStepResult({ name: "load user" })).toEqual(mockUserSession);
- expect(await wf.waitForStepResult({ name: "refresh-session" })).toEqual(mockUserSession);
- expect(await wf.waitForStepResult({ name: "complete task" })).toEqual(mockCompleteResult);
- // Workflow completes successfully
- await expect(wf.waitForStatus("complete")).resolves.not.toThrow();
- });
Advertisement
Add Comment
Please, Sign In to add comment