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 { Railway as RailwaySdk } 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 Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Railway.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const readProject = Query.fn((id: string) => { |
| 16 | const project = RailwaySdk.project({ id }); |
| 17 | return { |
| 18 | id: project.id, |
| 19 | name: project.name, |
| 20 | description: project.description, |
| 21 | workspaceId: project.workspaceId, |
| 22 | }; |
| 23 | }); |
| 24 | |
| 25 | const readProjectDeletedAt = Query.fn((id: string) => ({ |
| 26 | deletedAt: RailwaySdk.project({ id }).deletedAt, |
| 27 | })); |
| 28 | |
| 29 | const waitUntilGone = (projectId: string) => |
| 30 | readProjectDeletedAt(projectId).pipe( |
| 31 | Effect.map((project) => (project.deletedAt != null ? ("gone" as const) : ("found" as const))), |
| 32 | Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)), |
| 33 | Effect.repeat({ |
| 34 | schedule: Schedule.spaced("1 second"), |
| 35 | until: (status) => status === "gone", |
| 36 | times: 10, |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | test.provider( |
| 41 | "create, update, list, and delete a project", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const created = yield* stack.deploy( |
| 47 | Effect.gen(function* () { |
| 48 | return yield* Railway.Project("Site", { description: "v1" }); |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | expect(created.projectId).toEqual(expect.any(String)); |
| 53 | expect(created.projectId.length).toBeGreaterThan(0); |
| 54 | expect(created.name).toEqual(expect.any(String)); |
| 55 | expect(created.name.length).toBeGreaterThan(0); |
| 56 | expect(created.name.length).toBeLessThanOrEqual(32); |
| 57 | expect(created.name).toMatch(/^[a-z][a-z0-9-]*$/); |
| 58 | expect(created.workspaceId).toEqual(expect.any(String)); |
| 59 | expect(created.workspaceId.length).toBeGreaterThan(0); |
| 60 | expect(created.environmentId).toEqual(expect.any(String)); |
| 61 | expect(created.environmentId.length).toBeGreaterThan(0); |
| 62 | expect(created.url).toEqual(`https://railway.com/project/${created.projectId}`); |
| 63 | |
| 64 | const fetched = yield* readProject(created.projectId); |
| 65 | expect(fetched.id).toEqual(created.projectId); |
| 66 | expect(fetched.name).toEqual(created.name); |
| 67 | expect(fetched.description).toEqual("v1"); |
| 68 | expect(fetched.workspaceId).toEqual(created.workspaceId); |
| 69 | |
| 70 | const provider = yield* Provider.findProvider(Railway.Project); |
| 71 | const listed = yield* provider.list(); |
| 72 | const found = listed.find((project) => project.projectId === created.projectId); |
| 73 | expect(found).toBeDefined(); |
| 74 | expect(found?.name).toEqual(created.name); |
| 75 | expect(found?.url).toEqual(created.url); |
| 76 | expect(found?.environmentId).toEqual(created.environmentId); |
| 77 | |
| 78 | const nextName = created.name.slice(0, -1) + (created.name.endsWith("z") ? "y" : "z"); |
| 79 | |
| 80 | const updated = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | return yield* Railway.Project("Site", { name: nextName, description: "v2" }); |
| 83 | }), |
| 84 | ); |
| 85 | |
| 86 | expect(updated.projectId).toEqual(created.projectId); |
| 87 | expect(updated.name).toEqual(nextName); |
| 88 | expect(updated.workspaceId).toEqual(created.workspaceId); |
| 89 | expect(updated.environmentId).toEqual(created.environmentId); |
| 90 | expect(updated.url).toEqual(created.url); |
| 91 | |
| 92 | const fetchedUpdate = yield* readProject(updated.projectId); |
| 93 | expect(fetchedUpdate.id).toEqual(updated.projectId); |
| 94 | expect(fetchedUpdate.name).toEqual(nextName); |
| 95 | expect(fetchedUpdate.description).toEqual("v2"); |
| 96 | |
| 97 | yield* stack.destroy(); |
| 98 | |
| 99 | const gone = yield* waitUntilGone(created.projectId); |
| 100 | expect(gone).toEqual("gone"); |
| 101 | }).pipe(logLevel), |
| 102 | { tags: ["provider:railway", "provider:railway:project", "live"], timeout: 120_000 }, |
| 103 | ); |