chore: import upstream snapshot with attribution
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { env } from "cloudflare:test";
|
||||
import { setTestWorkflowCallback } from "./test-entry";
|
||||
import type {
|
||||
DatabaseInstance,
|
||||
DatabaseVersion,
|
||||
DatabaseWorkflow,
|
||||
Engine,
|
||||
} from "../src/engine";
|
||||
import type { WorkflowStep } from "cloudflare:workers";
|
||||
|
||||
// Track fire-and-forget init() RPC promises so they can be settled
|
||||
// in afterAll hooks before vitest tears down miniflare
|
||||
const pendingInits: Promise<unknown>[] = [];
|
||||
|
||||
/**
|
||||
* Await all tracked init promises (with a timeout safety net).
|
||||
* Call this in afterAll() for every test file that uses runWorkflow().
|
||||
*/
|
||||
export async function settlePendingWorkflows(): Promise<void> {
|
||||
await Promise.allSettled(pendingInits);
|
||||
pendingInits.length = 0;
|
||||
}
|
||||
|
||||
export async function runWorkflow(
|
||||
instanceId: string,
|
||||
callback: (event: unknown, step: WorkflowStep) => Promise<unknown>
|
||||
): Promise<DurableObjectStub<Engine>> {
|
||||
const engineId = env.ENGINE.idFromName(instanceId);
|
||||
const engineStub = env.ENGINE.get(engineId);
|
||||
|
||||
setTestWorkflowCallback(callback);
|
||||
|
||||
// Fire-and-forget: suppress all rejections to prevent unhandled
|
||||
// rejections inside workerd (which crash the HTTP server on Windows).
|
||||
// Abort errors (pause, terminate, restart) are expected; workflow
|
||||
// errors (NonRetryableError, step failures) are also expected for
|
||||
// tests that intentionally trigger them. Tests validate correctness
|
||||
// through separate log/status assertions, not through init()'s result.
|
||||
const initPromise = engineStub
|
||||
.init(
|
||||
12346,
|
||||
{} as DatabaseWorkflow,
|
||||
{} as DatabaseVersion,
|
||||
{ id: instanceId } as DatabaseInstance,
|
||||
{ payload: {}, timestamp: new Date(), instanceId, workflowName: "" }
|
||||
)
|
||||
.catch(() => {});
|
||||
|
||||
pendingInits.push(initPromise);
|
||||
|
||||
return engineStub;
|
||||
}
|
||||
|
||||
export async function runWorkflowAndAwait(
|
||||
instanceId: string,
|
||||
callback: (event: unknown, step: WorkflowStep) => Promise<unknown>
|
||||
): Promise<DurableObjectStub<Engine>> {
|
||||
const engineId = env.ENGINE.idFromName(instanceId);
|
||||
const engineStub = env.ENGINE.get(engineId);
|
||||
|
||||
setTestWorkflowCallback(callback);
|
||||
|
||||
await engineStub
|
||||
.init(
|
||||
12346,
|
||||
{} as DatabaseWorkflow,
|
||||
{} as DatabaseVersion,
|
||||
{ id: instanceId } as DatabaseInstance,
|
||||
{ payload: {}, timestamp: new Date(), instanceId, workflowName: "" }
|
||||
)
|
||||
.catch(() => {});
|
||||
|
||||
return engineStub;
|
||||
}
|
||||
Reference in New Issue
Block a user