The best way to control Claude Code, Codex and more from any computer or phone
| 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 { |
| 6 | Railway as RailwayApi, |
| 7 | type SandboxCheckpoint as RailwaySandboxCheckpoint, |
| 8 | } from "@distilled.cloud/railway"; |
| 9 | import { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy"; |
| 10 | import * as Provider from "alchemy/Provider"; |
| 11 | import { State, type UpdatingReourceState } from "alchemy/State"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Result from "effect/Result"; |
| 14 | import { suitePartition } from "./suiteProject.ts"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: Railway.providers() }); |
| 17 | |
| 18 | const Base = Effect.gen(function* () { |
| 19 | const { environment } = yield* suitePartition; |
| 20 | const box = yield* Railway.Sandbox("Source", { environment, idleTimeoutMinutes: 5 }); |
| 21 | return { environment, box }; |
| 22 | }); |
| 23 | |
| 24 | const listLive = Query.fn((environmentId: string) => |
| 25 | RailwayApi.sandboxCheckpoints({ environmentId }).pipe( |
| 26 | Query.map((checkpoint) => ({ |
| 27 | id: checkpoint.id, |
| 28 | key: checkpoint.key, |
| 29 | createdAt: checkpoint.createdAt, |
| 30 | environmentId: checkpoint.environmentId, |
| 31 | })), |
| 32 | ), |
| 33 | ); |
| 34 | |
| 35 | const checkpointFields = <E>(checkpoint: Query<RailwaySandboxCheckpoint, E>) => ({ |
| 36 | id: checkpoint.id, |
| 37 | key: checkpoint.key, |
| 38 | createdAt: checkpoint.createdAt, |
| 39 | }); |
| 40 | |
| 41 | const renameSandboxCheckpoint = Query.fn( |
| 42 | (input: { environmentId: string; id: string; name: string }) => |
| 43 | checkpointFields(RailwayApi.sandboxCheckpointRename(input)), |
| 44 | ); |
| 45 | |
| 46 | const createSandboxCheckpoint = Query.fn( |
| 47 | (input: { environmentId: string; sandboxId: string; name: string }) => |
| 48 | checkpointFields(RailwayApi.sandboxCheckpointCreate(input)), |
| 49 | ); |
| 50 | |
| 51 | const deleteSandboxCheckpoint = Query.fn((input: { environmentId: string; id: string }) => |
| 52 | RailwayApi.sandboxCheckpointDelete(input), |
| 53 | ); |
| 54 | |
| 55 | const Snapshot = (name?: string, restore = false) => |
| 56 | Effect.gen(function* () { |
| 57 | const { environment, box } = yield* Base; |
| 58 | const checkpoint = yield* Railway.SandboxCheckpoint("Snapshot", { sandbox: box, name }); |
| 59 | const restored = restore |
| 60 | ? yield* Railway.Sandbox("Restored", { |
| 61 | environment, |
| 62 | idleTimeoutMinutes: 5, |
| 63 | template: { name: checkpoint.key }, |
| 64 | }) |
| 65 | : undefined; |
| 66 | return { environment, box, checkpoint, restored }; |
| 67 | }); |
| 68 | |
| 69 | test.provider( |
| 70 | "capture, list, rename, restore, and delete a sandbox checkpoint", |
| 71 | (stack) => |
| 72 | Effect.gen(function* () { |
| 73 | yield* stack.destroy(); |
| 74 | const base = yield* stack.deploy(Base); |
| 75 | const written = yield* Railway.execSandbox({ |
| 76 | sandboxId: base.box.sandboxId, |
| 77 | environmentId: base.box.environmentId, |
| 78 | command: "echo checkpoint-content > /tmp/alchemy-checkpoint.txt", |
| 79 | timeoutSec: 10, |
| 80 | }); |
| 81 | expect(written.exitCode).toBe(0); |
| 82 | |
| 83 | const created = yield* stack.deploy(Snapshot()); |
| 84 | const original = created.checkpoint; |
| 85 | expect(original.sandboxId).toBe(base.box.sandboxId); |
| 86 | expect(original.key).toBe(original.name); |
| 87 | expect(original.name.length).toBeGreaterThan(0); |
| 88 | const live = yield* listLive(base.box.environmentId); |
| 89 | expect(live.find((item) => item.id === original.sandboxCheckpointId)).toEqual({ |
| 90 | id: original.sandboxCheckpointId, |
| 91 | key: original.key, |
| 92 | createdAt: original.createdAt, |
| 93 | environmentId: original.environmentId, |
| 94 | }); |
| 95 | const provider = yield* Provider.findProvider(Railway.SandboxCheckpoint); |
| 96 | expect( |
| 97 | (yield* provider.list()).some( |
| 98 | (item) => |
| 99 | item.sandboxCheckpointId === original.sandboxCheckpointId && |
| 100 | item.environmentId === original.environmentId, |
| 101 | ), |
| 102 | ).toBe(true); |
| 103 | |
| 104 | const unchanged = yield* stack.deploy(Snapshot()); |
| 105 | expect(unchanged.checkpoint).toEqual(original); |
| 106 | const renamed = yield* stack.deploy(Snapshot("restorable-checkpoint", true)); |
| 107 | expect(renamed.checkpoint.name).toBe("restorable-checkpoint"); |
| 108 | expect(renamed.checkpoint.createdAt).toBe(original.createdAt); |
| 109 | const renamedLive = yield* listLive(base.box.environmentId); |
| 110 | expect(renamedLive.some((item) => item.key === original.key)).toBe(false); |
| 111 | expect(renamedLive.find((item) => item.key === renamed.checkpoint.key)?.id).toBe( |
| 112 | renamed.checkpoint.sandboxCheckpointId, |
| 113 | ); |
| 114 | expect(renamed.restored).toBeDefined(); |
| 115 | const restored = yield* Railway.execSandbox({ |
| 116 | sandboxId: renamed.restored!.sandboxId, |
| 117 | environmentId: base.box.environmentId, |
| 118 | command: "cat /tmp/alchemy-checkpoint.txt", |
| 119 | timeoutSec: 10, |
| 120 | }); |
| 121 | expect(restored.exitCode).toBe(0); |
| 122 | expect(restored.stdout.trim()).toBe("checkpoint-content"); |
| 123 | |
| 124 | const resetName = yield* stack.deploy(Snapshot()); |
| 125 | expect(resetName.checkpoint.key).toBe(original.key); |
| 126 | expect(resetName.checkpoint.createdAt).toBe(original.createdAt); |
| 127 | |
| 128 | yield* stack.deploy(Base); |
| 129 | expect(yield* listLive(base.box.environmentId)).toEqual([]); |
| 130 | yield* stack.destroy(); |
| 131 | }), |
| 132 | { |
| 133 | tags: [ |
| 134 | "provider:railway", |
| 135 | "provider:railway:project", |
| 136 | "provider:railway:projectenvironment", |
| 137 | "provider:railway:sandbox", |
| 138 | "provider:railway:sandboxcheckpoint", |
| 139 | "live", |
| 140 | ], |
| 141 | timeout: 120_000, |
| 142 | }, |
| 143 | ); |
| 144 | |
| 145 | test.provider( |
| 146 | "replace a checkpoint when its source changes without removing either source", |
| 147 | (stack) => |
| 148 | Effect.gen(function* () { |
| 149 | yield* stack.destroy(); |
| 150 | const program = (second: boolean) => |
| 151 | Effect.gen(function* () { |
| 152 | const { environment, box } = yield* Base; |
| 153 | const other = yield* Railway.Sandbox("Other", { environment, idleTimeoutMinutes: 5 }); |
| 154 | const checkpoint = yield* Railway.SandboxCheckpoint("Snapshot", { |
| 155 | sandbox: second ? other : box, |
| 156 | name: "replace-source-checkpoint", |
| 157 | }); |
| 158 | return { box, other, checkpoint }; |
| 159 | }); |
| 160 | const before = yield* stack.deploy(program(false)); |
| 161 | const after = yield* stack.deploy(program(true)); |
| 162 | expect(after.box.sandboxId).toBe(before.box.sandboxId); |
| 163 | expect(after.other.sandboxId).toBe(before.other.sandboxId); |
| 164 | expect(after.checkpoint.sandboxId).toBe(after.other.sandboxId); |
| 165 | expect(after.checkpoint.createdAt).not.toBe(before.checkpoint.createdAt); |
| 166 | const live = yield* listLive(after.box.environmentId); |
| 167 | expect(live).toHaveLength(1); |
| 168 | expect(live[0]?.createdAt).toBe(after.checkpoint.createdAt); |
| 169 | yield* stack.deploy(Base); |
| 170 | expect(yield* listLive(after.box.environmentId)).toEqual([]); |
| 171 | yield* stack.destroy(); |
| 172 | }), |
| 173 | { |
| 174 | tags: [ |
| 175 | "provider:railway", |
| 176 | "provider:railway:project", |
| 177 | "provider:railway:projectenvironment", |
| 178 | "provider:railway:sandbox", |
| 179 | "provider:railway:sandboxcheckpoint", |
| 180 | "live", |
| 181 | ], |
| 182 | timeout: 120_000, |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | test.provider( |
| 187 | "recover interrupted renames without adopting or deleting a foreign capture", |
| 188 | (stack) => |
| 189 | Effect.gen(function* () { |
| 190 | yield* stack.destroy(); |
| 191 | const state = yield* yield* State; |
| 192 | const provider = yield* Provider.findProvider(Railway.SandboxCheckpoint); |
| 193 | const key = { stack: stack.name, stage: stack.stage, fqn: "Snapshot" }; |
| 194 | for (const recapture of [false, true]) { |
| 195 | const created = yield* stack.deploy(Snapshot("before-rename")); |
| 196 | const persisted = yield* state.get(key); |
| 197 | if (persisted?.status !== "created" && persisted?.status !== "updated") { |
| 198 | return yield* Effect.fail(new Error("Expected a stable checkpoint row")); |
| 199 | } |
| 200 | const attempted: Railway.SandboxCheckpointProps = { |
| 201 | sandbox: { sandboxId: created.box.sandboxId, environmentId: created.box.environmentId }, |
| 202 | name: "attempted-rename", |
| 203 | }; |
| 204 | // Apply commits attempted props before invoking the rename mutation. |
| 205 | yield* state.set({ |
| 206 | ...key, |
| 207 | value: { |
| 208 | ...persisted, |
| 209 | status: "updating", |
| 210 | props: attempted, |
| 211 | old: persisted, |
| 212 | } satisfies UpdatingReourceState, |
| 213 | }); |
| 214 | const renamed = yield* renameSandboxCheckpoint({ |
| 215 | environmentId: created.box.environmentId, |
| 216 | id: created.checkpoint.sandboxCheckpointId, |
| 217 | name: attempted.name!, |
| 218 | }); |
| 219 | expect(renamed.createdAt).toBe(created.checkpoint.createdAt); |
| 220 | let live = renamed; |
| 221 | if (recapture) { |
| 222 | yield* deleteSandboxCheckpoint({ |
| 223 | environmentId: created.box.environmentId, |
| 224 | id: renamed.id, |
| 225 | }); |
| 226 | live = yield* createSandboxCheckpoint({ |
| 227 | environmentId: created.box.environmentId, |
| 228 | sandboxId: created.box.sandboxId, |
| 229 | name: attempted.name!, |
| 230 | }); |
| 231 | expect(live.createdAt).not.toBe(created.checkpoint.createdAt); |
| 232 | } |
| 233 | // The successful rename's returned attributes were never persisted. |
| 234 | const read = yield* Effect.result( |
| 235 | provider.read!({ |
| 236 | id: "Snapshot", |
| 237 | fqn: "Snapshot", |
| 238 | instanceId: persisted.instanceId, |
| 239 | olds: attempted, |
| 240 | output: created.checkpoint, |
| 241 | }), |
| 242 | ); |
| 243 | if (recapture) { |
| 244 | expect(Result.isFailure(read)).toBe(true); |
| 245 | if (Result.isFailure(read)) { |
| 246 | expect(read.failure).toBeInstanceOf(OwnedBySomeoneElse); |
| 247 | } |
| 248 | } else { |
| 249 | expect(Result.isSuccess(read)).toBe(true); |
| 250 | if (Result.isSuccess(read)) { |
| 251 | expect(read.success).toMatchObject({ |
| 252 | sandboxCheckpointId: renamed.id, |
| 253 | key: renamed.key, |
| 254 | createdAt: created.checkpoint.createdAt, |
| 255 | }); |
| 256 | } |
| 257 | } |
| 258 | // Deletion must recover directly from attempted props and stale attrs. |
| 259 | yield* stack.deploy(Base); |
| 260 | const remaining = yield* listLive(created.box.environmentId); |
| 261 | if (recapture) { |
| 262 | expect(remaining).toHaveLength(1); |
| 263 | expect(remaining[0]?.id).toBe(live.id); |
| 264 | expect(remaining[0]?.createdAt).toBe(live.createdAt); |
| 265 | yield* deleteSandboxCheckpoint({ environmentId: created.box.environmentId, id: live.id }); |
| 266 | } else { |
| 267 | expect(remaining).toEqual([]); |
| 268 | } |
| 269 | expect(yield* listLive(created.box.environmentId)).toEqual([]); |
| 270 | } |
| 271 | yield* stack.destroy(); |
| 272 | }), |
| 273 | { |
| 274 | tags: [ |
| 275 | "provider:railway", |
| 276 | "provider:railway:project", |
| 277 | "provider:railway:projectenvironment", |
| 278 | "provider:railway:sandbox", |
| 279 | "provider:railway:sandboxcheckpoint", |
| 280 | "live", |
| 281 | ], |
| 282 | timeout: 120_000, |
| 283 | }, |
| 284 | ); |
| 285 | |
| 286 | test.provider( |
| 287 | "require adoption and preserve a foreign recapture with the same checkpoint name", |
| 288 | (stack) => |
| 289 | Effect.gen(function* () { |
| 290 | yield* stack.destroy(); |
| 291 | const base = yield* stack.deploy(Base); |
| 292 | const foreign = yield* createSandboxCheckpoint({ |
| 293 | environmentId: base.box.environmentId, |
| 294 | sandboxId: base.box.sandboxId, |
| 295 | name: "foreign-checkpoint", |
| 296 | }); |
| 297 | const program = (allowAdoption: boolean, name = "foreign-checkpoint") => |
| 298 | Effect.gen(function* () { |
| 299 | const { box } = yield* Base; |
| 300 | return yield* Railway.SandboxCheckpoint("Snapshot", { sandbox: box, name }).pipe( |
| 301 | adopt(allowAdoption), |
| 302 | ); |
| 303 | }); |
| 304 | const denied = yield* Effect.result(stack.deploy(program(false))); |
| 305 | expect(Result.isFailure(denied)).toBe(true); |
| 306 | if (Result.isFailure(denied)) { |
| 307 | expect(denied.failure).toBeInstanceOf(OwnedBySomeoneElse); |
| 308 | } |
| 309 | expect((yield* listLive(base.box.environmentId))[0]?.createdAt).toBe(foreign.createdAt); |
| 310 | |
| 311 | const adopted = yield* stack.deploy(program(true)); |
| 312 | expect(adopted.sandboxCheckpointId).toBe(foreign.id); |
| 313 | expect(adopted.createdAt).toBe(foreign.createdAt); |
| 314 | const occupied = yield* createSandboxCheckpoint({ |
| 315 | environmentId: base.box.environmentId, |
| 316 | sandboxId: base.box.sandboxId, |
| 317 | name: "occupied-checkpoint", |
| 318 | }); |
| 319 | const collision = yield* Effect.result(stack.deploy(program(false, "occupied-checkpoint"))); |
| 320 | expect(Result.isFailure(collision)).toBe(true); |
| 321 | const afterCollision = yield* listLive(base.box.environmentId); |
| 322 | expect(afterCollision).toHaveLength(2); |
| 323 | expect(afterCollision.find((item) => item.id === occupied.id)?.createdAt).toBe( |
| 324 | occupied.createdAt, |
| 325 | ); |
| 326 | expect(afterCollision.find((item) => item.id === foreign.id)?.createdAt).toBe( |
| 327 | foreign.createdAt, |
| 328 | ); |
| 329 | yield* deleteSandboxCheckpoint({ environmentId: base.box.environmentId, id: occupied.id }); |
| 330 | yield* stack.deploy(program(false)); |
| 331 | yield* deleteSandboxCheckpoint({ environmentId: base.box.environmentId, id: foreign.id }); |
| 332 | const replacement = yield* createSandboxCheckpoint({ |
| 333 | environmentId: base.box.environmentId, |
| 334 | sandboxId: base.box.sandboxId, |
| 335 | name: "foreign-checkpoint", |
| 336 | }); |
| 337 | expect(replacement.createdAt).not.toBe(adopted.createdAt); |
| 338 | const provider = yield* Provider.findProvider(Railway.SandboxCheckpoint); |
| 339 | const read = yield* Effect.result( |
| 340 | provider.read!({ |
| 341 | id: "Snapshot", |
| 342 | fqn: "Snapshot", |
| 343 | instanceId: "unused", |
| 344 | olds: { |
| 345 | sandbox: { sandboxId: base.box.sandboxId, environmentId: base.box.environmentId }, |
| 346 | name: "foreign-checkpoint", |
| 347 | }, |
| 348 | output: adopted, |
| 349 | }), |
| 350 | ); |
| 351 | expect(Result.isFailure(read)).toBe(true); |
| 352 | if (Result.isFailure(read)) { |
| 353 | expect(read.failure).toBeInstanceOf(OwnedBySomeoneElse); |
| 354 | } |
| 355 | |
| 356 | yield* stack.deploy(Base); |
| 357 | const preserved = yield* listLive(base.box.environmentId); |
| 358 | expect(preserved).toHaveLength(1); |
| 359 | expect(preserved[0]?.id).toBe(replacement.id); |
| 360 | expect(preserved[0]?.createdAt).toBe(replacement.createdAt); |
| 361 | yield* deleteSandboxCheckpoint({ environmentId: base.box.environmentId, id: replacement.id }); |
| 362 | expect(yield* listLive(base.box.environmentId)).toEqual([]); |
| 363 | yield* stack.destroy(); |
| 364 | }), |
| 365 | { |
| 366 | tags: [ |
| 367 | "provider:railway", |
| 368 | "provider:railway:project", |
| 369 | "provider:railway:projectenvironment", |
| 370 | "provider:railway:sandbox", |
| 371 | "provider:railway:sandboxcheckpoint", |
| 372 | "live", |
| 373 | ], |
| 374 | timeout: 120_000, |
| 375 | }, |
| 376 | ); |