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

main
ts 143 lines 5.22 KB
Raw
1 import * as Railway from "@alchemy.run/railway";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import { Query } from "@distilled.cloud/core/query";
5 import { Railway as RailwayApi } from "@distilled.cloud/railway";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Result from "effect/Result";
10 import * as Schedule from "effect/Schedule";
11 import { suitePartition } from "./suiteProject.ts";
12
13 const { test } = Test.make({ providers: Railway.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // Cloud agents are Priority Boarding. Unentitled workspaces reject
18 // `cloudAgentCreate` with a typed plan/auth tag. The probe always runs
19 // and pins that tag. When the token is entitled the same test continues
20 // through the resource create+list+delete lifecycle.
21
22 // `cloudAgents` declares no not-found error; a missing environment
23 // surfaces as an empty list.
24 const listLive = Query.fn((environmentId: string) =>
25 RailwayApi.cloudAgents({ environmentId, mine: true }).pipe(
26 Query.map((agent) => ({
27 id: agent.id,
28 name: agent.name,
29 status: agent.status,
30 environmentId: agent.environmentId,
31 projectId: agent.projectId,
32 })),
33 ),
34 );
35
36 const waitUntilAgentGone = (environmentId: string, cloudAgentId: string) =>
37 listLive(environmentId).pipe(
38 Effect.map((items) =>
39 items.some((agent) => agent.id === cloudAgentId && agent.status !== "DELETING")
40 ? ("found" as const)
41 : ("gone" as const),
42 ),
43 Effect.repeat({
44 schedule: Schedule.spaced("1 second"),
45 until: (status) => status === "gone",
46 times: 10,
47 }),
48 );
49
50 const deleteAgent = Query.fn((id: string) => RailwayApi.cloudAgentDelete({ id }));
51
52 const createCloudAgent = Query.fn((input: { environmentId: string; name: string }) => {
53 const agent = RailwayApi.cloudAgentCreate({ input });
54 return { id: agent.id };
55 });
56
57 test.provider(
58 "create, list, and delete a cloud agent",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const projectOnly = yield* stack.deploy(
64 Effect.gen(function* () {
65 const { project, environment } = yield* suitePartition;
66 return { project, environment };
67 }),
68 );
69
70 const probe = yield* Effect.result(
71 createCloudAgent({
72 environmentId: projectOnly.environment.environmentId,
73 name: projectOnly.project.name,
74 }),
75 );
76 if (Result.isFailure(probe)) {
77 expect(
78 ["RailwayForbidden", "RailwayPlanLimitExceeded"].includes(probe.failure._tag),
79 ).toEqual(true);
80 yield* stack.destroy();
81 return;
82 }
83
84 yield* deleteAgent(probe.success.id);
85 yield* waitUntilAgentGone(projectOnly.environment.environmentId, probe.success.id);
86
87 const created = yield* stack.deploy(
88 Effect.gen(function* () {
89 const { project, environment } = yield* suitePartition;
90 const agent = yield* Railway.CloudAgent("Coder", { environment });
91 return { project, environment, agent };
92 }),
93 );
94
95 expect(created.agent.cloudAgentId).toEqual(expect.any(String));
96 expect(created.agent.cloudAgentId.length).toBeGreaterThan(0);
97 expect(created.agent.environmentId).toEqual(created.environment.environmentId);
98 expect(created.agent.projectId).toEqual(created.project.projectId);
99 expect(created.agent.name).toEqual(expect.any(String));
100 expect(created.agent.name.length).toBeGreaterThan(0);
101 expect(created.agent.name.length).toBeLessThanOrEqual(32);
102 expect(created.agent.name).toMatch(/^[a-z][a-z0-9-]*$/);
103 expect(created.agent.createdAt).toEqual(expect.any(String));
104 expect(created.agent.status).toEqual(expect.any(String));
105 expect(
106 ["CRASHED", "DELETING", "FAILED", "RUNNING", "SLEEPING", "STARTING"].includes(
107 created.agent.status,
108 ),
109 ).toEqual(true);
110
111 const listed = yield* listLive(created.environment.environmentId);
112 const fetched = listed.find((agent) => agent.id === created.agent.cloudAgentId);
113 expect(fetched).toBeDefined();
114 expect(fetched?.name).toEqual(created.agent.name);
115 expect(fetched?.environmentId).toEqual(created.environment.environmentId);
116 expect(fetched?.projectId).toEqual(created.project.projectId);
117
118 const provider = yield* Provider.findProvider(Railway.CloudAgent);
119 const fromProvider = yield* provider.list();
120 const found = fromProvider.find((agent) => agent.cloudAgentId === created.agent.cloudAgentId);
121 expect(found).toBeDefined();
122 expect(found?.name).toEqual(created.agent.name);
123 expect(found?.environmentId).toEqual(created.environment.environmentId);
124
125 yield* stack.destroy();
126
127 const agentGone = yield* waitUntilAgentGone(
128 created.environment.environmentId,
129 created.agent.cloudAgentId,
130 );
131 expect(agentGone).toEqual("gone");
132 }).pipe(logLevel),
133 {
134 tags: [
135 "provider:railway",
136 "provider:railway:cloudagent",
137 "provider:railway:project",
138 "provider:railway:projectenvironment",
139 "live",
140 ],
141 timeout: 120_000,
142 },
143 );