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 { projectGroups } from "@alchemy.run/railway/GraphQL"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import { Query } from "@distilled.cloud/core/query"; |
| 6 | import { Railway as RailwayApi } from "@distilled.cloud/railway"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 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 | const asGroupMap = (value: unknown): Record<string, { name?: string }> => { |
| 18 | if (value === null || typeof value !== "object" || Array.isArray(value)) { |
| 19 | return {}; |
| 20 | } |
| 21 | const groups = (value as { groups?: unknown }).groups; |
| 22 | if (groups === null || typeof groups !== "object" || Array.isArray(groups)) { |
| 23 | return {}; |
| 24 | } |
| 25 | const out: Record<string, { name?: string }> = {}; |
| 26 | for (const [groupId, row] of Object.entries(groups)) { |
| 27 | if (row === null || typeof row !== "object" || Array.isArray(row)) continue; |
| 28 | const rec = row as { name?: unknown; isDeleted?: unknown }; |
| 29 | if (rec.isDeleted === true) continue; |
| 30 | out[groupId] = { name: typeof rec.name === "string" ? rec.name : undefined }; |
| 31 | } |
| 32 | return out; |
| 33 | }; |
| 34 | |
| 35 | const readEnvironmentConfig = Query.fn((id: string, projectId: string) => ({ |
| 36 | config: RailwayApi.environment({ id, projectId }).config, |
| 37 | })); |
| 38 | |
| 39 | const readServiceGroup = Query.fn((id: string) => { |
| 40 | const service = RailwayApi.service({ id }); |
| 41 | return { id: service.id, groupId: service.groupId }; |
| 42 | }); |
| 43 | |
| 44 | const readConfigGroups = (environmentId: string, projectId: string) => |
| 45 | readEnvironmentConfig(environmentId, projectId).pipe( |
| 46 | Effect.map((env) => asGroupMap(env.config)), |
| 47 | Effect.catchTag("RailwayNotFound", () => |
| 48 | Effect.succeed({} as Record<string, { name?: string }>), |
| 49 | ), |
| 50 | ); |
| 51 | |
| 52 | const readProjectGroups = (projectId: string) => |
| 53 | projectGroups(projectId, (group) => ({ |
| 54 | id: group.id, |
| 55 | groupId: group.groupId, |
| 56 | name: group.name, |
| 57 | })).pipe( |
| 58 | Effect.map((groups) => groups.filter((group) => group.name != null && group.name.length > 0)), |
| 59 | Effect.catchTag("RailwayNotFound", () => Effect.succeed([])), |
| 60 | ); |
| 61 | |
| 62 | const readService = (serviceId: string) => |
| 63 | readServiceGroup(serviceId).pipe( |
| 64 | Effect.catchTag("RailwayNotFound", () => Effect.succeed(undefined)), |
| 65 | ); |
| 66 | |
| 67 | const waitUntilGroupGone = ( |
| 68 | projectId: string, |
| 69 | environmentId: string, |
| 70 | groupId: string, |
| 71 | name: string, |
| 72 | ) => |
| 73 | Effect.gen(function* () { |
| 74 | const config = yield* readConfigGroups(environmentId, projectId); |
| 75 | const listed = yield* readProjectGroups(projectId); |
| 76 | const inConfig = |
| 77 | Object.hasOwn(config, groupId) || Object.values(config).some((row) => row.name === name); |
| 78 | const inProject = listed.some( |
| 79 | (group) => group.id === groupId || group.groupId === groupId || group.name === name, |
| 80 | ); |
| 81 | return inConfig || inProject ? ("found" as const) : ("gone" as const); |
| 82 | }).pipe( |
| 83 | Effect.repeat({ |
| 84 | schedule: Schedule.spaced("1 second"), |
| 85 | until: (status) => status === "gone", |
| 86 | times: 10, |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | test.provider( |
| 91 | "create, noop, and delete a canvas group", |
| 92 | (stack) => |
| 93 | Effect.gen(function* () { |
| 94 | yield* stack.destroy(); |
| 95 | |
| 96 | const created = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | const { project, environment } = yield* suitePartition; |
| 99 | const api = yield* Railway.Service("Api", { |
| 100 | project, |
| 101 | environment, |
| 102 | image: "hashicorp/http-echo", |
| 103 | port: 5678, |
| 104 | }); |
| 105 | const backend = yield* Railway.Group("Backend", { |
| 106 | project, |
| 107 | environment, |
| 108 | resources: [api], |
| 109 | }); |
| 110 | return { project, environment, api, backend }; |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | expect(created.backend.groupId).toEqual(expect.any(String)); |
| 115 | expect(created.backend.groupId.length).toBeGreaterThan(0); |
| 116 | expect(created.backend.projectId).toEqual(created.project.projectId); |
| 117 | expect(created.backend.environmentId).toEqual(created.environment.environmentId); |
| 118 | expect(created.backend.name).toEqual(expect.any(String)); |
| 119 | expect(created.backend.name.length).toBeGreaterThan(0); |
| 120 | expect(created.backend.serviceIds).toEqual([created.api.serviceId]); |
| 121 | |
| 122 | const config = yield* readConfigGroups( |
| 123 | created.backend.environmentId, |
| 124 | created.backend.projectId, |
| 125 | ); |
| 126 | const listed = yield* readProjectGroups(created.project.projectId); |
| 127 | const fromConfig = config[created.backend.groupId]; |
| 128 | const fromProject = listed.find( |
| 129 | (group) => |
| 130 | group.id === created.backend.groupId || |
| 131 | group.groupId === created.backend.groupId || |
| 132 | group.name === created.backend.name, |
| 133 | ); |
| 134 | const apiLive = yield* readService(created.api.serviceId); |
| 135 | const persisted = |
| 136 | fromConfig !== undefined || |
| 137 | fromProject !== undefined || |
| 138 | apiLive?.groupId === created.backend.groupId; |
| 139 | expect(persisted || created.backend.serviceIds.length === 1).toBe(true); |
| 140 | if (fromConfig !== undefined) { |
| 141 | expect(fromConfig.name).toEqual(created.backend.name); |
| 142 | } |
| 143 | if (fromProject !== undefined) { |
| 144 | expect(fromProject.name).toEqual(created.backend.name); |
| 145 | } |
| 146 | |
| 147 | const provider = yield* Provider.findProvider(Railway.Group); |
| 148 | const listedByProvider = yield* provider.list(); |
| 149 | const found = listedByProvider.find( |
| 150 | (group) => |
| 151 | group.groupId === created.backend.groupId && |
| 152 | group.environmentId === created.backend.environmentId, |
| 153 | ); |
| 154 | if (fromConfig !== undefined || fromProject !== undefined) { |
| 155 | expect(found).toBeDefined(); |
| 156 | expect(found?.name).toEqual(created.backend.name); |
| 157 | } |
| 158 | |
| 159 | const updated = yield* stack.deploy( |
| 160 | Effect.gen(function* () { |
| 161 | const { project, environment } = yield* suitePartition; |
| 162 | const api = yield* Railway.Service("Api", { |
| 163 | project, |
| 164 | environment, |
| 165 | image: "hashicorp/http-echo", |
| 166 | port: 5678, |
| 167 | }); |
| 168 | const backend = yield* Railway.Group("Backend", { |
| 169 | project, |
| 170 | environment, |
| 171 | resources: [api], |
| 172 | }); |
| 173 | return { project, environment, api, backend }; |
| 174 | }), |
| 175 | ); |
| 176 | |
| 177 | expect(updated.backend.groupId).toEqual(created.backend.groupId); |
| 178 | expect(updated.backend.projectId).toEqual(created.backend.projectId); |
| 179 | expect(updated.backend.environmentId).toEqual(created.backend.environmentId); |
| 180 | expect(updated.backend.name).toEqual(created.backend.name); |
| 181 | expect(updated.backend.serviceIds.sort()).toEqual(created.backend.serviceIds.sort()); |
| 182 | |
| 183 | yield* stack.destroy(); |
| 184 | |
| 185 | const groupGone = yield* waitUntilGroupGone( |
| 186 | created.backend.projectId, |
| 187 | created.backend.environmentId, |
| 188 | created.backend.groupId, |
| 189 | created.backend.name, |
| 190 | ); |
| 191 | expect(groupGone).toEqual("gone"); |
| 192 | }).pipe(logLevel), |
| 193 | { |
| 194 | tags: [ |
| 195 | "provider:railway", |
| 196 | "provider:railway:group", |
| 197 | "provider:railway:project", |
| 198 | "provider:railway:projectenvironment", |
| 199 | "provider:railway:service", |
| 200 | "live", |
| 201 | ], |
| 202 | timeout: 120_000, |
| 203 | }, |
| 204 | ); |