Fork Horse repository, listed by its owner. Publisher snapshot.

packages/forkhorse-gatekeeper/__tests__/workflow-native.test.ts

Snapshot at 34e3175

Containing directory

2,161 bytes · typescriptView raw
1import { env, RpcTarget } from "cloudflare:workers";
2import { expect, it } from "vitest";
3import type { ApprovalQueue } from "@gadgets/workshop-shared/gatekeeper";
4import type { FixtureWorkspace } from "./worker.ts";
5
6declare global {
7  namespace Cloudflare { interface Env { WORKSPACES: DurableObjectNamespace<FixtureWorkspace> } }
8}
9class Queue extends RpcTarget {
10  ids: number[] = [];
11  observations = 0;
12  async authorizeObservation() { this.observations++; }
13  async submitAction(id: number, description: { awaitDecision?: boolean; autoApprovable?: boolean }) {
14    expect(description.awaitDecision).toBe(true);
15    expect(description.autoApprovable).toBe(true);
16    this.ids.push(id);
17  }
18}
19
20it("runs each named workflow call over native RPC with real durable approval state and an offline transport", async () => {
21  const workspace = env.WORKSPACES.getByName(crypto.randomUUID());
22  const queue = new Queue();
23  using session = await workspace.workflowSession(queue as unknown as ApprovalQueue);
24  expect(await session.context()).toMatchObject({ project: { id: "fixture" }, myWork: [] });
25  expect(queue.observations).toBe(1);
26  const calls = [
27    () => session.join(),
28    () => session.claim("fixture-packet", "fixture-fork"),
29    () => session.requestReview("fixture-packet", "fixture-fork", "a".repeat(40)),
30    () => session.release("fixture-packet"),
31  ];
32  for (const [index, call] of calls.entries()) {
33    expect(await call()).toBeUndefined();
34    expect(await workspace.workflowCalls()).toHaveLength(index);
35    await workspace.applyWorkflow(queue.ids[index]!);
36    await workspace.applyWorkflow(queue.ids[index]!);
37    expect(await workspace.workflowCalls()).toHaveLength(index + 1);
38  }
39  expect(await workspace.workflowCalls()).toEqual([
40    { path: "/join", method: "POST", body: { agent: "fixture-agent", ttlSeconds: 60 } },
41    { path: "/packets/fixture-packet/claim", method: "POST", body: { repoName: "fixture-fork" } },
42    { path: "/reviews", method: "POST", body: { packet: "fixture-packet", repoName: "fixture-fork", sha: "a".repeat(40) } },
43    { path: "/packets/fixture-packet/claim", method: "DELETE", body: null },
44  ]);
45});
46