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 { projectServices } 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 Result from "effect/Result"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { suitePartition } from "./suiteProject.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Railway.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const PUBLIC_TEMPLATE_CODE = "postgres"; |
| 19 | |
| 20 | const readServiceDeletedAt = Query.fn((id: string) => ({ |
| 21 | deletedAt: RailwayApi.service({ id }).deletedAt, |
| 22 | })); |
| 23 | |
| 24 | const readServiceTemplateId = Query.fn((id: string) => ({ |
| 25 | templateId: RailwayApi.service({ id }).templateId, |
| 26 | })); |
| 27 | |
| 28 | const readTemplateByCode = Query.fn((code: string) => { |
| 29 | const template = RailwayApi.template({ code }); |
| 30 | return { |
| 31 | id: template.id, |
| 32 | code: template.code, |
| 33 | name: template.name, |
| 34 | serializedConfig: template.serializedConfig, |
| 35 | }; |
| 36 | }); |
| 37 | |
| 38 | const readTemplateById = Query.fn((id: string) => { |
| 39 | const template = RailwayApi.template({ id }); |
| 40 | return { id: template.id, code: template.code }; |
| 41 | }); |
| 42 | |
| 43 | const readTemplateSourceForProject = Query.fn((projectId: string) => |
| 44 | RailwayApi.templateSourceForProject({ projectId }).pipe( |
| 45 | Query.map((template) => ({ id: template.id })), |
| 46 | ), |
| 47 | ); |
| 48 | |
| 49 | const waitUntilServiceGone = (serviceId: string) => |
| 50 | readServiceDeletedAt(serviceId).pipe( |
| 51 | Effect.map((service) => (service.deletedAt != null ? ("gone" as const) : ("found" as const))), |
| 52 | Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)), |
| 53 | Effect.repeat({ |
| 54 | schedule: Schedule.spaced("1 second"), |
| 55 | until: (status) => status === "gone", |
| 56 | times: 10, |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | test.provider( |
| 61 | "lookup a well-known public template", |
| 62 | (stack) => |
| 63 | Effect.gen(function* () { |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | const fetched = yield* readTemplateByCode(PUBLIC_TEMPLATE_CODE); |
| 67 | expect(fetched.id).toEqual(expect.any(String)); |
| 68 | expect(fetched.id.length).toBeGreaterThan(0); |
| 69 | expect(fetched.code).toEqual(PUBLIC_TEMPLATE_CODE); |
| 70 | expect(fetched.name).toEqual(expect.any(String)); |
| 71 | expect(fetched.serializedConfig).toBeDefined(); |
| 72 | |
| 73 | yield* stack.destroy(); |
| 74 | }).pipe(logLevel), |
| 75 | { tags: ["provider:railway", "provider:railway:template", "live"], timeout: 120_000 }, |
| 76 | ); |
| 77 | |
| 78 | test.provider( |
| 79 | "deploy, list, and delete a marketplace template", |
| 80 | (stack) => |
| 81 | Effect.gen(function* () { |
| 82 | yield* stack.destroy(); |
| 83 | |
| 84 | const created = yield* stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | const { project, environment } = yield* suitePartition; |
| 87 | const deployed = yield* Railway.Template("Postgres", { |
| 88 | templateId: PUBLIC_TEMPLATE_CODE, |
| 89 | project, |
| 90 | environment, |
| 91 | }); |
| 92 | return { project, environment, deployed }; |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | expect(created.deployed.templateId).toEqual(expect.any(String)); |
| 97 | expect(created.deployed.templateId.length).toBeGreaterThan(0); |
| 98 | expect(created.deployed.code).toEqual(PUBLIC_TEMPLATE_CODE); |
| 99 | expect(created.deployed.name).toEqual(expect.any(String)); |
| 100 | expect(created.deployed.projectId).toEqual(created.project.projectId); |
| 101 | expect(created.deployed.environmentId).toEqual(created.environment.environmentId); |
| 102 | expect(created.deployed.workspaceId).toEqual(created.project.workspaceId); |
| 103 | expect(created.deployed.ownsProject).toEqual(false); |
| 104 | expect(created.deployed.serviceIds.length).toBeGreaterThan(0); |
| 105 | expect(created.deployed.url).toEqual( |
| 106 | `https://railway.com/project/${created.project.projectId}`, |
| 107 | ); |
| 108 | |
| 109 | const live = yield* projectServices(created.project.projectId, (service) => ({ |
| 110 | id: service.id, |
| 111 | deletedAt: service.deletedAt, |
| 112 | })); |
| 113 | const liveIds = live.filter((node) => node.deletedAt == null).map((node) => node.id); |
| 114 | for (const serviceId of created.deployed.serviceIds) { |
| 115 | expect(liveIds).toContain(serviceId); |
| 116 | } |
| 117 | |
| 118 | const source = yield* readTemplateSourceForProject(created.project.projectId).pipe( |
| 119 | Effect.catchTag("RailwayForbidden", () => Effect.succeed(undefined)), |
| 120 | ); |
| 121 | if (source != null) { |
| 122 | expect(source.id).toEqual(created.deployed.templateId); |
| 123 | } |
| 124 | |
| 125 | const stamped = yield* readServiceTemplateId(created.deployed.serviceIds[0]!); |
| 126 | if (stamped.templateId != null) { |
| 127 | expect(stamped.templateId).toEqual(created.deployed.templateId); |
| 128 | } |
| 129 | |
| 130 | const provider = yield* Provider.findProvider(Railway.Template); |
| 131 | const listed = yield* provider.list(); |
| 132 | const found = listed.find( |
| 133 | (row) => |
| 134 | row.projectId === created.deployed.projectId && |
| 135 | row.templateId === created.deployed.templateId, |
| 136 | ); |
| 137 | expect(found).toBeDefined(); |
| 138 | if (found === undefined) { |
| 139 | return yield* Effect.fail(new Error("Deployed marketplace template was not listed")); |
| 140 | } |
| 141 | expect(found.templateId).toEqual(created.deployed.templateId); |
| 142 | expect(found.serviceIds.length).toBeGreaterThan(0); |
| 143 | if (found.code !== undefined) { |
| 144 | expect(found.code).toEqual(PUBLIC_TEMPLATE_CODE); |
| 145 | } else { |
| 146 | // Listing preserves service ownership even when Railway refuses |
| 147 | // marketplace metadata by ID. Confirm that omission against the API. |
| 148 | const metadata = yield* Effect.result(readTemplateById(found.templateId)); |
| 149 | expect(Result.isFailure(metadata)).toBe(true); |
| 150 | if (Result.isFailure(metadata)) { |
| 151 | expect(["RailwayForbidden", "RailwayNotFound"].includes(metadata.failure._tag)).toBe( |
| 152 | true, |
| 153 | ); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | yield* stack.destroy(); |
| 158 | |
| 159 | for (const serviceId of created.deployed.serviceIds) { |
| 160 | const gone = yield* waitUntilServiceGone(serviceId); |
| 161 | expect(gone).toEqual("gone"); |
| 162 | } |
| 163 | }).pipe(logLevel), |
| 164 | { |
| 165 | tags: [ |
| 166 | "provider:railway", |
| 167 | "provider:railway:project", |
| 168 | "provider:railway:projectenvironment", |
| 169 | "provider:railway:service", |
| 170 | "provider:railway:template", |
| 171 | "live", |
| 172 | ], |
| 173 | timeout: 120_000, |
| 174 | }, |
| 175 | ); |