@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 231 lines 7.23 KB
Raw
1 import { RailwayEnvironment } from "@alchemy.run/railway/Environment";
2 import { Sandbox, SandboxProvider, type SandboxProps } from "@alchemy.run/railway/Sandbox";
3 import { describe, expect, it } from "@alchemy.run/test";
4 import { CredentialsFromToken, GraphQLLive, toConfig } from "@distilled.cloud/railway";
5 import * as Effect from "effect/Effect";
6 import * as Fiber from "effect/Fiber";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientResponse from "effect/http/HttpClientResponse";
9 import * as Layer from "effect/Layer";
10 import * as Redacted from "effect/Redacted";
11 import * as TestClock from "effect/testing/TestClock";
12
13 const testLayer = (client: HttpClient.HttpClient) =>
14 SandboxProvider().pipe(
15 Layer.provideMerge(
16 Layer.mergeAll(
17 Layer.succeed(
18 RailwayEnvironment,
19 Effect.succeed({
20 ...toConfig({ token: Redacted.make("fixture") }),
21 workspaceId: "workspace",
22 }),
23 ),
24 CredentialsFromToken({ token: Redacted.make("fixture") }),
25 GraphQLLive,
26 Layer.succeed(HttpClient.HttpClient, client),
27 ),
28 ),
29 );
30
31 const props: SandboxProps = { environment: { environmentId: "env" } };
32 const attrs: Sandbox["Attributes"] = {
33 sandboxId: "box",
34 environmentId: "env",
35 projectId: undefined,
36 region: "us-west2",
37 status: "RUNNING",
38 idleTimeoutMinutes: 5,
39 networkIsolation: "ISOLATED",
40 createdAt: "2026-09-16T00:00:00Z",
41 domains: [],
42 };
43 const context = {
44 id: "Box",
45 fqn: "Box",
46 instanceId: "instance",
47 bindings: [],
48 session: undefined as never,
49 };
50 const cloud = (status: Sandbox["Attributes"]["status"]) => ({
51 id: attrs.sandboxId,
52 environmentId: attrs.environmentId,
53 region: attrs.region,
54 status,
55 idleTimeoutMinutes: attrs.idleTimeoutMinutes,
56 networkIsolation: attrs.networkIsolation,
57 createdAt: attrs.createdAt,
58 domains: [],
59 });
60 const http = (respond: (query: string) => unknown) =>
61 HttpClient.make((request) =>
62 Effect.sync(() => {
63 if (request.body._tag !== "Uint8Array") throw new Error("Expected JSON request");
64 const { query } = JSON.parse(new TextDecoder().decode(request.body.body));
65 return HttpClientResponse.fromWeb(request, Response.json({ data: respond(query) }));
66 }),
67 );
68
69 const immutable: Partial<SandboxProps>[] = [
70 { region: "us-east4-eqdc4a" },
71 { idleTimeoutMinutes: 0 },
72 { networkIsolation: "PRIVATE" },
73 { template: { name: "base" } },
74 { variables: { TOKEN: "fixture" } },
75 { publicDomains: [{ port: 3000 }] },
76 { resources: { cpu: 0.5, memoryGB: 1 } },
77 { sourceSandboxId: "source" },
78 ];
79
80 describe(
81 "Railway Sandbox provider",
82 { tags: ["unit", "provider:railway", "provider:railway:sandbox", "local"] },
83 () => {
84 for (const option of immutable) {
85 const name = Object.keys(option)[0];
86 it.effect(`replaces when ${name} is changed or removed`, () =>
87 Effect.gen(function* () {
88 const provider = yield* Sandbox.Provider;
89 for (const [olds, news] of [
90 [props, { ...props, ...option }],
91 [{ ...props, ...option }, props],
92 ]) {
93 expect(
94 yield* provider.diff!({
95 ...context,
96 olds: olds!,
97 news: news!,
98 output: attrs,
99 oldBindings: [],
100 newBindings: [],
101 }),
102 ).toEqual({ action: "replace" });
103 }
104 }).pipe(
105 Effect.provide(
106 testLayer(
107 http(() => {
108 throw new Error("Diff must not perform I/O");
109 }),
110 ),
111 ),
112 ),
113 );
114 }
115
116 it.effect("ignores variable and route ordering", () =>
117 Effect.gen(function* () {
118 const provider = yield* Sandbox.Provider;
119 const olds = {
120 ...props,
121 variables: { A: "1", B: "2" },
122 template: { variables: { A: "1", B: "2" } },
123 publicDomains: [{ port: 3000 }, { port: 8080, prefix: "api" }],
124 };
125 const news = {
126 ...props,
127 variables: { B: "2", A: "1" },
128 template: { variables: { B: "2", A: "1" } },
129 publicDomains: [{ prefix: "api", port: 8080 }, { port: 3000 }],
130 };
131 expect(
132 yield* provider.diff!({
133 ...context,
134 olds,
135 news,
136 output: attrs,
137 oldBindings: [],
138 newBindings: [],
139 }),
140 ).toBeUndefined();
141 }).pipe(
142 Effect.provide(
143 testLayer(
144 http(() => {
145 throw new Error("Diff must not perform I/O");
146 }),
147 ),
148 ),
149 ),
150 );
151
152 it.effect("fails rather than returning a sandbox that never becomes ready", () =>
153 Effect.gen(function* () {
154 const provider = yield* Sandbox.Provider;
155 const fiber = yield* provider
156 .reconcile({ ...context, news: props, olds: undefined, output: undefined })
157 .pipe(Effect.flip, Effect.forkChild({ startImmediately: true }));
158 yield* TestClock.adjust("30 seconds");
159 expect(yield* Fiber.join(fiber)).toMatchObject({
160 _tag: "Railway.SandboxPending",
161 sandboxId: "box",
162 status: "CREATING",
163 });
164 }).pipe(
165 Effect.provide(
166 testLayer(
167 http((query) =>
168 query.includes("sandboxCreate")
169 ? { sandboxCreate: cloud("CREATING") }
170 : { sandbox: cloud("CREATING") },
171 ),
172 ),
173 ),
174 ),
175 );
176
177 it.effect("recreates a missing cached sandbox and preserves an existing adopted one", () => {
178 let creates = 0;
179 let exists = false;
180 return Effect.gen(function* () {
181 const provider = yield* Sandbox.Provider;
182 const recreated = yield* provider.reconcile({
183 ...context,
184 news: props,
185 olds: props,
186 output: attrs,
187 });
188 expect(recreated.status).toBe("RUNNING");
189 expect(creates).toBe(1);
190 yield* provider.reconcile({ ...context, news: props, olds: undefined, output: recreated });
191 expect(creates).toBe(1);
192 }).pipe(
193 Effect.provide(
194 testLayer(
195 http((query) => {
196 if (query.includes("sandboxCreate")) {
197 creates++;
198 exists = true;
199 return { sandboxCreate: cloud("RUNNING") };
200 }
201 return { sandbox: exists ? cloud("RUNNING") : null };
202 }),
203 ),
204 ),
205 );
206 });
207
208 it.effect("waits for DESTROYED instead of treating DESTROYING as absent", () => {
209 let reads = 0;
210 return Effect.gen(function* () {
211 const provider = yield* Sandbox.Provider;
212 const fiber = yield* provider
213 .delete({ ...context, olds: props, output: attrs })
214 .pipe(Effect.forkChild({ startImmediately: true }));
215 yield* TestClock.adjust("2 seconds");
216 yield* Fiber.join(fiber);
217 expect(reads).toBe(3);
218 }).pipe(
219 Effect.provide(
220 testLayer(
221 http((query) =>
222 query.includes("sandboxDestroy")
223 ? { sandboxDestroy: cloud("DESTROYING") }
224 : { sandbox: cloud(++reads >= 3 ? "DESTROYED" : "DESTROYING") },
225 ),
226 ),
227 ),
228 );
229 });
230 },
231 );