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

packages/forkhorse-mcp/__tests__/judge-fixture.ts

Snapshot at 34e3175

Containing directory

7,558 bytes · typescriptView raw
1import { createCoreClient } from "../src/core";
2import { createMcpHandler } from "../src/mcp";
3
4const origin = "https://judge.fixture.idiot.tax";
5const at = "2026-01-01T00:00:00.000Z";
6const sha = "a".repeat(40);
7export const fixtureDiffPath = "src/changed file.ts";
8const entrants = ["builder", "second"].map((agent) => ({
9  agent,
10  repoName: `demo--${agent}-1`,
11  claimedAt: at,
12  firstPushAt: at,
13  reviewRequestedAt: at,
14  receiptPassedAt: at,
15  approvedAt: null,
16  promotedAt: null,
17  place: null,
18  closedAt: null,
19  closedBy: null,
20  promoteRejected: null,
21  usage: {
22    reported:
23      agent === "builder" ? { inputTokens: 120, outputTokens: 40, source: "reported", at } : null,
24    metered: null,
25  },
26}));
27export const judgeState = {
28  fixture: "FH-063 fixed-contract fixture; no live core, authorization or provider acceptance",
29  claims: [],
30  packets: [
31    { id: "FH-judge", mode: "race" },
32    { id: "FH-other", mode: "race" },
33  ],
34  races: ["FH-judge", "FH-other"].map((packet) => ({
35    packet,
36    status: "open",
37    finishedAt: null,
38    winner: null,
39    ledgerEntryId: null,
40    places: [],
41    entrants,
42    judge: packet === "FH-judge" ? "judge" : "other-judge",
43    verdict: null as null | { ranking: string[]; rationale: string; at: string },
44  })),
45};
46export const judgeDetail = {
47  review: {
48    id: "review-builder",
49    packet: "FH-judge",
50    agent: "builder",
51    repoName: "demo--builder-1",
52    sha,
53    base: "b".repeat(40),
54    createdAt: at,
55    stale: false,
56    promoted: false,
57    returned: null,
58    approval: null,
59    receipt: { status: "passed", digest: "fixture-digest", durationMs: 40, checks: [] },
60    usageAtReview: {
61      reported: { inputTokens: 120, outputTokens: 40, source: "reported", at },
62      metered: null,
63    },
64  },
65  packet: {
66    id: "FH-judge",
67    title: "Judge fixture",
68    goal: "Offline contract validation",
69    paths: ["src/"],
70    acceptance: ["Workerd fixture"],
71  },
72  changes: {
73    base: "b".repeat(40),
74    head: sha,
75    fileCount: 1,
76    truncated: false,
77    files: [{ path: fixtureDiffPath, status: "modified" }],
78  },
79  commits: [],
80  commitsTruncated: false,
81  promotion: null,
82  blockers: [],
83};
84export const judgeDiff = {
85  path: fixtureDiffPath,
86  status: "modified",
87  binary: false,
88  truncated: false,
89  patch:
90    "--- a/src/changed file.ts\n+++ b/src/changed file.ts\n@@ -1 +1 @@\n-fixture before\n+fixture after\n",
91};
92
93/** Fake core policy only; core must enforce the confirmed full retained roster independently. */
94export function judgeFixture() {
95  const state = structuredClone(judgeState);
96  const tokens = new Map(
97    ["judge", "builder", "second", "other-judge", "outsider"].map((agent) => [
98      agent,
99      crypto.randomUUID(),
100    ]),
101  );
102  const calls: {
103    path: string;
104    query: string;
105    method: string;
106    agent: string | undefined;
107    body: unknown;
108    redirect: RequestRedirect;
109  }[] = [];
110  const controls = {
111    status: 0,
112    throws: false,
113    oversized: false,
114    detail: structuredClone(judgeDetail) as unknown,
115    diff: structuredClone(judgeDiff) as unknown,
116    adviceAt: "2026-01-01T00:01:00.000Z",
117  };
118  const core = createCoreClient({
119    authenticate: async (token) => {
120      const agent = [...tokens].find(([, value]) => value === token)?.[0];
121      return agent ? { project: "demo", agent } : null;
122    },
123    enrollAgent: async () => {
124      throw new Error("No fixture enrollment.");
125    },
126    fetch: async (request) => {
127      const url = new URL(request.url);
128      const agent = [...tokens].find(
129        ([, value]) => request.headers.get("authorization") === `Bearer ${value}`,
130      )?.[0];
131      const data = request.method === "POST" ? await request.json() : null;
132      calls.push({
133        path: url.pathname,
134        query: url.search,
135        method: request.method,
136        agent,
137        body: data,
138        redirect: request.redirect,
139      });
140      if (controls.throws) throw new Error("fixture-private-provider-detail");
141      if (controls.status)
142        return Response.json(
143          { error: "fixture-private-provider-detail" },
144          { status: controls.status },
145        );
146      if (controls.oversized) return Response.json({ data: "x".repeat(1024 * 1024) });
147      if (url.pathname.endsWith("/state")) return Response.json(state);
148      if (url.pathname.endsWith("/me")) return Response.json({ work: [] });
149      const detail =
150        /^\/projects\/demo\/reviews\/(review-builder|review-second|review-foreign)(\/diff)?$/.exec(
151          url.pathname,
152        );
153      if (detail) {
154        const owner = detail[1] === "review-second" ? "second" : "builder";
155        const packet = detail[1] === "review-foreign" ? "FH-other" : "FH-judge";
156        const race = state.races.find((candidate) => candidate.packet === packet)!;
157        if (agent !== owner && race.judge !== agent) return Response.json({}, { status: 403 });
158        if (detail[2]) {
159          if (url.searchParams.get("path") !== fixtureDiffPath)
160            return Response.json({}, { status: 404 });
161          return Response.json(controls.diff);
162        }
163        return Response.json(controls.detail);
164      }
165      const advice = /^\/projects\/demo\/races\/([^/]+)\/verdict$/.exec(url.pathname);
166      if (advice) {
167        const race = state.races.find((candidate) => candidate.packet === advice[1]);
168        if (!race) return Response.json({}, { status: 404 });
169        if (race.judge !== agent) return Response.json({}, { status: 403 });
170        if (race.status === "finished") return Response.json({}, { status: 409 });
171        const input = data as { ranking: string[]; rationale: string };
172        const names = race.entrants.map((entrant) => entrant.agent);
173        if (
174          input.ranking.length !== names.length ||
175          new Set(input.ranking).size !== names.length ||
176          input.ranking.some((name) => !names.includes(name))
177        )
178          return Response.json({}, { status: 400 });
179        race.verdict = { ...input, at: controls.adviceAt };
180        return Response.json({ verdict: race.verdict });
181      }
182      if (url.pathname.endsWith("/claim")) return Response.json({}, { status: 409 });
183      return Response.json({}, { status: 404 });
184    },
185  });
186  const handler = createMcpHandler({ core, publicOrigin: origin });
187  async function rpc(method: string, params: unknown, actor = "judge") {
188    const response = await handler(
189      new Request(`${origin}/mcp`, {
190        method: "POST",
191        headers: {
192          authorization: `Bearer ${tokens.get(actor) ?? "invalid-fixture"}`,
193          "content-type": "application/json",
194          accept: "application/json, text/event-stream",
195        },
196        body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
197      }),
198    );
199    return {
200      status: response.status,
201      cache: response.headers.get("cache-control"),
202      body:
203        response.status === 200
204          ? ((await response.json()) as {
205              result: {
206                isError?: boolean;
207                content: { text: string }[];
208                tools?: {
209                  name: string;
210                  annotations: {
211                    readOnlyHint: boolean;
212                    idempotentHint: boolean;
213                    destructiveHint: boolean;
214                  };
215                }[];
216              };
217            })
218          : null,
219    };
220  }
221  const call = (name: string, args: unknown, actor = "judge") =>
222    rpc("tools/call", { name, arguments: args }, actor);
223  return { state, calls, controls, rpc, call };
224}
225