The best way to control Claude Code, Codex and more from any computer or phone

main
ts 99 lines 3.44 KB
Raw
1 import { PrismaApiError, type PrismaManagementClient } from "@alchemy.run/prisma/Client";
2 import { describe, expect, it } from "@alchemy.run/test";
3 import * as Effect from "effect/Effect";
4 import {
5 startDeploymentIdempotent,
6 stopDeploymentIdempotent,
7 } from "../../../prisma/src/internal/DeploymentActions.ts";
8 import {
9 type Captured,
10 dispatchTo,
11 makeFakeManagementApi,
12 unhandled,
13 } from "./fixtures/FakeManagementApi.ts";
14
15 const conflict = (action: "start" | "stop") =>
16 new PrismaApiError({
17 method: "POST",
18 path: `/v1/deployments/deployment-1/${action}`,
19 status: 409,
20 message: "state conflict",
21 });
22
23 const version = (status: string) => ({
24 id: "deployment-1",
25 type: "deployment" as const,
26 serviceId: "service-1",
27 url: "https://api.prisma.test/v1/deployments/deployment-1",
28 foundryVersionId: "foundry-1",
29 status,
30 previewDomain: null,
31 createdAt: "2026-01-01T00:00:00Z",
32 });
33
34 /**
35 * Serve the Management API from the same hermetic client-shaped handlers
36 * these tests declare, for the deployment routes the actions now reach
37 * through distilled operations. An injected `PrismaApiError` becomes its
38 * real status, `undefined` becomes a 404, everything else a `{ data }`
39 * envelope.
40 */
41 const clientBackedApi = (client: any) =>
42 makeFakeManagementApi((request: Captured) => {
43 // segments[0] is the "v1" prefix.
44 const [head, id, tail] = request.pathname
45 .split("/")
46 .filter((segment) => segment.length > 0)
47 .slice(1);
48 const { call, callVoid } = dispatchTo(request);
49
50 if (head === "deployments" && id !== undefined) {
51 if (tail === "start") return call(client.startDeployment, [id]);
52 if (tail === "stop") return callVoid(client.stopDeployment, [id]);
53 if (request.method === "GET") return call(client.getDeployment, [id]);
54 }
55 return unhandled(request);
56 });
57
58 describe(
59 "Prisma deployment actions",
60 { tags: ["unit", "provider:prisma", "provider:prisma:deployment", "local"] },
61 () => {
62 it.effect("does not hide a start conflict for an unuploaded version", () => {
63 const error = conflict("start");
64 const client = {
65 startDeployment: () => Effect.fail(error),
66 getDeployment: () => Effect.succeed(version("new")),
67 } as unknown as PrismaManagementClient;
68
69 return Effect.gen(function* () {
70 const observed = yield* startDeploymentIdempotent("deployment-1").pipe(Effect.flip);
71 // Over the wire the injected conflict decodes into the typed error.
72 expect(observed._tag).toBe("Conflict");
73 expect(observed.message).toBe("state conflict");
74 }).pipe(Effect.provide(clientBackedApi(client).layer));
75 });
76
77 it.effect("accepts a start conflict only after observing progress", () => {
78 const client = {
79 startDeployment: () => Effect.fail(conflict("start")),
80 getDeployment: () => Effect.succeed(version("provisioning")),
81 } as unknown as PrismaManagementClient;
82
83 return startDeploymentIdempotent("deployment-1").pipe(
84 Effect.provide(clientBackedApi(client).layer),
85 );
86 });
87
88 it.effect("accepts a stop conflict while teardown is already progressing", () => {
89 const client = {
90 stopDeployment: () => Effect.fail(conflict("stop")),
91 getDeployment: () => Effect.succeed(version("stopping")),
92 } as unknown as PrismaManagementClient;
93
94 return stopDeploymentIdempotent("deployment-1").pipe(
95 Effect.provide(clientBackedApi(client).layer),
96 );
97 });
98 },
99 );