Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/sharp-workflows-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@cloudflare/vitest-pool-workers": minor
---

Make Workflow introspector `get()` async

The `introspectWorkflow(...).get()` method now returns a promise, so callers must await it:

```ts
const introspector = await introspectWorkflow(env.MY_WORKFLOW);

// Before
const instances = introspector.get();

// After
const instances = await introspector.get();
```

This aligns Workflow introspection with the shared implementation used by `createTestHarness()`.
25 changes: 25 additions & 0 deletions .changeset/soft-workflows-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"miniflare": patch
"wrangler": minor
---

Add Workflow introspection to `createTestHarness()`

Worker handles can now introspect Workflow bindings by name, allowing tests to disable sleeps, mock step results, and wait for Workflow outcomes. Tests can introspect a known Workflow instance by ID or track instances created after introspection starts.

```ts
const harness = createTestHarness({
workers: [{ configPath: "./wrangler.json" }],
});

const worker = harness.getWorker();
await using workflow = await worker.introspectWorkflow("MY_WORKFLOW");

await workflow.modifyAll((modifier) =>
modifier.disableSleeps([{ name: "wait-for-approval" }])
);

const response = await worker.fetch("/start-workflow");
const [instance] = await workflow.get();
await instance.waitForStatus("complete");
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ it("workflow should be able to reach the end and be successful", async ({

await exports.default.fetch(`https://mock-worker.local/moderate`);

const instances = introspector.get();
const instances = await introspector.get();
expect(instances.length).toBe(1);

// ASSERTIONS:
Expand Down Expand Up @@ -49,7 +49,7 @@ it("workflow batch should be able to reach the end and be successful", async ({

await exports.default.fetch(`https://mock-worker.local/moderate-batch`);

const instances = introspector.get();
const instances = await introspector.get();
expect(instances.length).toBe(3);

// ASSERTIONS:
Expand Down Expand Up @@ -86,7 +86,7 @@ describe("workflow instance lifecycle methods", () => {
);
const data = (await res.json()) as { id: string; details: unknown };

const instances = introspector.get();
const instances = await introspector.get();
expect(instances.length).toBe(1);
const instance = instances[0];

Expand Down Expand Up @@ -120,7 +120,7 @@ describe("workflow instance lifecycle methods", () => {
);
const data = (await res.json()) as { id: string; details: unknown };

const instances = introspector.get();
const instances = await introspector.get();
expect(instances.length).toBe(1);
const instance = instances[0];

Expand Down Expand Up @@ -155,7 +155,7 @@ describe("workflow instance lifecycle methods", () => {
);
const data = (await res.json()) as { id: string; details: unknown };

const instances = introspector.get();
const instances = await introspector.get();
expect(instances.length).toBe(1);
const instance = instances[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
WorkflowBinding,
WorkflowInstanceRestartOptions,
} from "@cloudflare/workflows-shared/src/binding";
import type { WorkflowIntrospectionOperation } from "@cloudflare/workflows-shared/src/types";

class WorkflowImpl implements Workflow {
constructor(private binding: WorkflowBinding) {}
Expand Down Expand Up @@ -36,6 +37,25 @@ class WorkflowImpl implements Workflow {
return this.binding.unsafeGetBindingName();
}

async unsafeStartIntrospection(): Promise<string> {
return this.binding.unsafeStartIntrospection();
}

async unsafeStopIntrospection(sessionId: string): Promise<void> {
return this.binding.unsafeStopIntrospection(sessionId);
}

async unsafeSetIntrospectionOperations(
sessionId: string,
operations: WorkflowIntrospectionOperation[]
): Promise<void> {
return this.binding.unsafeSetIntrospectionOperations(sessionId, operations);
}

async unsafeGetIntrospectionInstances(sessionId: string): Promise<string[]> {
return this.binding.unsafeGetIntrospectionInstances(sessionId);
}

async unsafeAbort(instanceId: string, reason?: string): Promise<void> {
return this.binding.unsafeAbort(instanceId, reason);
}
Expand Down
Loading
Loading