Guest User

workflow-durable-object.test.ts

a guest
Sep 17th, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Example: using introspectWorkflow with a Durable Object in a plain Todo app
  2. // Replace placeholders (UserDurableObject, app, triggerCompleteTaskWorkflow, WORKFLOW binding) with your own.
  3.  
  4. import { env, runInDurableObject, introspectWorkflow } from "cloudflare:test";
  5. import { expect, it, vi } from "vitest";
  6. // import { UserDurableObject } from "../src"; // <-- your DO class
  7. // import { app } from "../src";               // <-- your worker entry
  8.  
  9. it("introspectWorkflow with DO (Todo: complete task)", async () => {
  10.   vi.clearAllMocks();
  11.  
  12.   const taskId = "task-introspect-DO-123";
  13.   const idemKey = crypto.randomUUID();
  14.  
  15.   // What each workflow step should yield for this test run
  16.   const mockUserSession = {
  17.     userId: "user-" + Date.now(),
  18.     sessionToken: "mock-session",
  19.     expiresAt: new Date(Date.now() + 60 * 60 * 1000),
  20.   };
  21.  
  22.   const mockCompleteResult = {
  23.     ok: true,
  24.     taskId,
  25.     status: "completed",
  26.   };
  27.  
  28.   // Intercept the workflow BEFORE hitting the worker
  29.   await using introspector = await introspectWorkflow(env.TODO_COMPLETE_WORKFLOW);
  30.   await introspector.modifyAll(async (m) => {
  31.     await m.disableSleeps();
  32.     await m.mockStepResult({ name: "init" }, "complete");
  33.     await m.mockStepResult({ name: "load user" }, mockUserSession);
  34.     await m.mockStepResult({ name: "refresh-session" }, mockUserSession);
  35.     await m.mockStepResult({ name: "complete task" }, mockCompleteResult);
  36.   });
  37.  
  38.   // public-safe headers
  39.   const headers = {
  40.     "x-app-org-id": "demo-org",
  41.     "x-app-project-id": "demo-project",
  42.     "x-app-api-key": "demo__token",
  43.     "x-app-user-id": `user-${Math.random().toString(36).slice(2)}`,
  44.   };
  45.  
  46.   // Seed DO with a user + an open task
  47.   const userRecord = {
  48.     id: headers["x-app-user-id"],
  49.     projectId: headers["x-app-project-id"],
  50.     displayName: "Test User",
  51.   };
  52.  
  53.   const openTask = {
  54.     id: taskId,
  55.     userId: headers["x-app-user-id"],
  56.     title: "Write docs",
  57.     notes: "Make sure to include examples",
  58.     status: "open" as const,
  59.     createdAt: new Date(),
  60.     expiresAt: new Date(Date.now() + 60_000), // still valid
  61.   };
  62.  
  63.   const response = await runInDurableObject(orgDb, async (instance: any /* UserDurableObject */) => {
  64.     expect(instance).toBeTruthy();
  65.  
  66.     // Replace with your DO helpers
  67.     await instance.insertUser?.(userRecord);
  68.     await instance.insertTask?.(openTask);
  69.  
  70.     return app.request(
  71.       `/v0/tasks/${taskId}/complete?idem=${idemKey}`,
  72.       {
  73.         method: "PUT",
  74.         headers: { ...headers, "Content-Type": "application/json" },
  75.         body: JSON.stringify({ note: "completed via test", source: "e2e" }),
  76.       },
  77.       {
  78.         ...env,
  79.         USER_DO: {
  80.           get: () => instance,
  81.           idFromName: (name: string) => name,
  82.         },
  83.       }
  84.     );
  85.   });
  86.  
  87.   // Basic HTTP assertions
  88.   const json = await response.json<any>();
  89.   expect(response.status).toBe(200);
  90.   expect(typeof json.workflowRunId).toBe("string");
  91.  
  92.   // Exactly one workflow instance should be intercepted
  93.   const instances = introspector.get();
  94.   expect(instances.length).toBe(1);
  95.  
  96.   const wf = instances[0];
  97.  
  98.   // Step order and results
  99.   expect(await wf.waitForStepResult({ name: "init" })).toBe("complete");
  100.   expect(await wf.waitForStepResult({ name: "load user" })).toEqual(mockUserSession);
  101.   expect(await wf.waitForStepResult({ name: "refresh-session" })).toEqual(mockUserSession);
  102.   expect(await wf.waitForStepResult({ name: "complete task" })).toEqual(mockCompleteResult);
  103.  
  104.   // Workflow completes successfully
  105.   await expect(wf.waitForStatus("complete")).resolves.not.toThrow();
  106. });
Advertisement
Add Comment
Please, Sign In to add comment