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 RailwayApi } 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 Result from "effect/Result"; |
| 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 | // Volume backups are Pro-plan gated. Railway rejects Hobby/unentitled |
| 18 | // workspaces with GraphQL `Not Authorized`, already typed as |
| 19 | // RailwayForbidden. The probe always runs and pins that tag. The |
| 20 | // create+list+delete lifecycle is opt-in via RAILWAY_TEST_VOLUME_BACKUP=1. |
| 21 | const backupEntitled = !!process.env.RAILWAY_TEST_VOLUME_BACKUP; |
| 22 | |
| 23 | const VolumeStack = Effect.gen(function* () { |
| 24 | const { project, environment } = yield* suitePartition; |
| 25 | const api = yield* Railway.Service("Api", { project, environment, image: "hashicorp/http-echo" }); |
| 26 | const volume = yield* Railway.Volume("Data", { |
| 27 | project, |
| 28 | environment, |
| 29 | mountPath: "/data", |
| 30 | service: api, |
| 31 | }); |
| 32 | return { project, environment, api, volume }; |
| 33 | }); |
| 34 | |
| 35 | const listVolumeInstanceBackups = Query.fn((volumeInstanceId: string) => |
| 36 | RailwayApi.volumeInstanceBackupList({ volumeInstanceId }).pipe( |
| 37 | Query.map((backup) => ({ id: backup.id, name: backup.name, createdAt: backup.createdAt })), |
| 38 | ), |
| 39 | ); |
| 40 | |
| 41 | const readVolumeInstanceState = Query.fn((id: string) => { |
| 42 | const instance = RailwayApi.volumeInstance({ id }); |
| 43 | return { deletedAt: instance.deletedAt, state: instance.state }; |
| 44 | }); |
| 45 | |
| 46 | const createVolumeInstanceBackup = Query.fn((volumeInstanceId: string) => { |
| 47 | const created = RailwayApi.volumeInstanceBackupCreate({ volumeInstanceId }); |
| 48 | return { workflowId: created.workflowId }; |
| 49 | }); |
| 50 | |
| 51 | const readWorkflowStatus = Query.fn((workflowId: string) => { |
| 52 | const workflow = RailwayApi.workflowStatus({ workflowId }); |
| 53 | return { status: workflow.status }; |
| 54 | }); |
| 55 | |
| 56 | const deleteVolumeInstanceBackup = Query.fn( |
| 57 | (volumeInstanceBackupId: string, volumeInstanceId: string) => { |
| 58 | const deleted = RailwayApi.volumeInstanceBackupDelete({ |
| 59 | volumeInstanceBackupId, |
| 60 | volumeInstanceId, |
| 61 | }); |
| 62 | return { workflowId: deleted.workflowId }; |
| 63 | }, |
| 64 | ); |
| 65 | |
| 66 | const listLive = (volumeInstanceId: string) => |
| 67 | listVolumeInstanceBackups(volumeInstanceId).pipe( |
| 68 | Effect.catchTag(["RailwayNotFound", "RailwayForbidden"], () => Effect.succeed([])), |
| 69 | ); |
| 70 | |
| 71 | const waitUntilReady = (volumeInstanceId: string) => |
| 72 | readVolumeInstanceState(volumeInstanceId).pipe( |
| 73 | Effect.map((instance) => |
| 74 | instance.deletedAt == null && |
| 75 | instance.state !== "DELETED" && |
| 76 | instance.state !== "DELETING" && |
| 77 | instance.state !== "UPDATING" && |
| 78 | instance.state !== "MIGRATING" && |
| 79 | instance.state !== "MIGRATION_PENDING" && |
| 80 | instance.state !== "RESTORING" && |
| 81 | instance.state !== "ERROR" |
| 82 | ? ("ready" as const) |
| 83 | : ("pending" as const), |
| 84 | ), |
| 85 | Effect.catchTag("RailwayNotFound", () => Effect.succeed("pending" as const)), |
| 86 | Effect.repeat({ |
| 87 | schedule: Schedule.spaced("2 seconds"), |
| 88 | until: (status) => status === "ready", |
| 89 | times: 10, |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | const waitUntilBackupGone = (volumeInstanceId: string, volumeInstanceBackupId: string) => |
| 94 | listLive(volumeInstanceId).pipe( |
| 95 | Effect.map((items) => |
| 96 | items.some((backup) => backup.id === volumeInstanceBackupId) |
| 97 | ? ("found" as const) |
| 98 | : ("gone" as const), |
| 99 | ), |
| 100 | Effect.repeat({ |
| 101 | schedule: Schedule.spaced("1 second"), |
| 102 | until: (status) => status === "gone", |
| 103 | times: 10, |
| 104 | }), |
| 105 | ); |
| 106 | |
| 107 | test.provider( |
| 108 | "volume backup create surfaces a typed entitlement error", |
| 109 | (stack) => |
| 110 | Effect.gen(function* () { |
| 111 | yield* stack.destroy(); |
| 112 | |
| 113 | const created = yield* stack.deploy(VolumeStack); |
| 114 | yield* waitUntilReady(created.volume.volumeInstanceId); |
| 115 | |
| 116 | const result = yield* Effect.result( |
| 117 | createVolumeInstanceBackup(created.volume.volumeInstanceId), |
| 118 | ); |
| 119 | if (Result.isSuccess(result)) { |
| 120 | yield* Effect.logInfo("volume backups are entitled on this token; probe is a no-op"); |
| 121 | if (result.success.workflowId != null && result.success.workflowId.length > 0) { |
| 122 | yield* readWorkflowStatus(result.success.workflowId).pipe( |
| 123 | Effect.catchTag("RailwayForbidden", () => Effect.void), |
| 124 | ); |
| 125 | } |
| 126 | const extras = yield* listLive(created.volume.volumeInstanceId); |
| 127 | for (const extra of extras) { |
| 128 | yield* deleteVolumeInstanceBackup(extra.id, created.volume.volumeInstanceId).pipe( |
| 129 | Effect.catchTag(["RailwayNotFound", "RailwayForbidden"], () => Effect.void), |
| 130 | ); |
| 131 | } |
| 132 | yield* stack.destroy(); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | expect(result.failure._tag === "RailwayForbidden").toBe(true); |
| 137 | |
| 138 | yield* stack.destroy(); |
| 139 | }).pipe(logLevel), |
| 140 | { |
| 141 | tags: [ |
| 142 | "provider:railway", |
| 143 | "provider:railway:project", |
| 144 | "provider:railway:projectenvironment", |
| 145 | "provider:railway:service", |
| 146 | "provider:railway:volume", |
| 147 | "provider:railway:volumebackup", |
| 148 | "live", |
| 149 | ], |
| 150 | timeout: 120_000, |
| 151 | }, |
| 152 | ); |
| 153 | |
| 154 | test.provider.skipIf(!backupEntitled)( |
| 155 | "create, list, and delete a volume backup", |
| 156 | (stack) => |
| 157 | Effect.gen(function* () { |
| 158 | yield* stack.destroy(); |
| 159 | |
| 160 | const base = yield* stack.deploy(VolumeStack); |
| 161 | yield* waitUntilReady(base.volume.volumeInstanceId); |
| 162 | |
| 163 | const created = yield* stack.deploy( |
| 164 | Effect.gen(function* () { |
| 165 | const { project, environment } = yield* suitePartition; |
| 166 | const api = yield* Railway.Service("Api", { |
| 167 | project, |
| 168 | environment, |
| 169 | image: "hashicorp/http-echo", |
| 170 | }); |
| 171 | const volume = yield* Railway.Volume("Data", { |
| 172 | project, |
| 173 | environment, |
| 174 | mountPath: "/data", |
| 175 | service: api, |
| 176 | }); |
| 177 | const backup = yield* Railway.VolumeBackup("Snapshot", { volume, environment }); |
| 178 | return { project, environment, api, volume, backup }; |
| 179 | }), |
| 180 | ); |
| 181 | |
| 182 | expect(created.backup.volumeInstanceBackupId).toEqual(expect.any(String)); |
| 183 | expect(created.backup.volumeInstanceBackupId.length).toBeGreaterThan(0); |
| 184 | expect(created.backup.volumeInstanceId).toEqual(created.volume.volumeInstanceId); |
| 185 | expect(created.backup.volumeId).toEqual(created.volume.volumeId); |
| 186 | expect(created.backup.projectId).toEqual(created.project.projectId); |
| 187 | expect(created.backup.environmentId).toEqual(created.environment.environmentId); |
| 188 | expect(created.backup.name).toEqual(expect.any(String)); |
| 189 | expect(created.backup.name.length).toBeGreaterThan(0); |
| 190 | expect(created.backup.createdAt).toEqual(expect.any(String)); |
| 191 | |
| 192 | const listed = yield* listLive(created.volume.volumeInstanceId); |
| 193 | const fetched = listed.find((backup) => backup.id === created.backup.volumeInstanceBackupId); |
| 194 | expect(fetched).toBeDefined(); |
| 195 | expect(fetched?.name).toEqual(created.backup.name); |
| 196 | expect(fetched?.createdAt).toEqual(created.backup.createdAt); |
| 197 | |
| 198 | const provider = yield* Provider.findProvider(Railway.VolumeBackup); |
| 199 | const fromProvider = yield* provider.list(); |
| 200 | const found = fromProvider.find( |
| 201 | (backup) => backup.volumeInstanceBackupId === created.backup.volumeInstanceBackupId, |
| 202 | ); |
| 203 | expect(found).toBeDefined(); |
| 204 | expect(found?.volumeInstanceId).toEqual(created.volume.volumeInstanceId); |
| 205 | expect(found?.name).toEqual(created.backup.name); |
| 206 | |
| 207 | yield* stack.destroy(); |
| 208 | |
| 209 | const backupGone = yield* waitUntilBackupGone( |
| 210 | created.volume.volumeInstanceId, |
| 211 | created.backup.volumeInstanceBackupId, |
| 212 | ); |
| 213 | expect(backupGone).toEqual("gone"); |
| 214 | }).pipe(logLevel), |
| 215 | { |
| 216 | tags: [ |
| 217 | "provider:railway", |
| 218 | "provider:railway:project", |
| 219 | "provider:railway:projectenvironment", |
| 220 | "provider:railway:service", |
| 221 | "provider:railway:volume", |
| 222 | "provider:railway:volumebackup", |
| 223 | "live", |
| 224 | ], |
| 225 | timeout: 120_000, |
| 226 | }, |
| 227 | ); |