The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Result from "effect/Result"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import VolumeApi, { Data, MARKER, MARKER_FILE, VOLUME_PATH } from "./fixtures/volume-api.ts"; |
| 14 | import { suitePartition } from "./suiteProject.ts"; |
| 15 | import { waitUntilVolumeGone } from "./waitUntilVolumeGone.ts"; |
| 16 | |
| 17 | const { test } = Test.make({ providers: Railway.providers() }); |
| 18 | |
| 19 | const readVolumeInstance = Query.fn((id: string) => { |
| 20 | const instance = RailwayApi.volumeInstance({ id }); |
| 21 | return { |
| 22 | id: instance.id, |
| 23 | volumeId: instance.volumeId, |
| 24 | mountPath: instance.mountPath, |
| 25 | environmentId: instance.environmentId, |
| 26 | volume: { name: instance.volume.name, projectId: instance.volume.projectId }, |
| 27 | serviceId: instance.serviceId, |
| 28 | }; |
| 29 | }); |
| 30 | |
| 31 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 32 | |
| 33 | test.provider( |
| 34 | "create, update, list, and delete a volume", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const created = yield* stack.deploy( |
| 40 | Effect.gen(function* () { |
| 41 | const { project, environment } = yield* suitePartition; |
| 42 | const volume = yield* Railway.Volume("Data", { |
| 43 | project, |
| 44 | environment, |
| 45 | mountPath: "/data", |
| 46 | }); |
| 47 | return { project, environment, volume }; |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | expect(created.volume.volumeId).toEqual(expect.any(String)); |
| 52 | expect(created.volume.volumeId.length).toBeGreaterThan(0); |
| 53 | expect(created.volume.volumeInstanceId).toEqual(expect.any(String)); |
| 54 | expect(created.volume.volumeInstanceId.length).toBeGreaterThan(0); |
| 55 | expect(created.volume.projectId).toEqual(created.project.projectId); |
| 56 | expect(created.volume.environmentId).toEqual(created.environment.environmentId); |
| 57 | expect(created.volume.mountPath).toEqual("/data"); |
| 58 | expect(created.volume.serviceId).toBeUndefined(); |
| 59 | expect(created.volume.name).toEqual(expect.any(String)); |
| 60 | expect(created.volume.name.length).toBeGreaterThan(0); |
| 61 | expect(created.volume.name.length).toBeLessThanOrEqual(32); |
| 62 | expect(created.volume.name).toMatch(/^[a-z][a-z0-9-]*$/); |
| 63 | expect(created.volume.sizeMB).toEqual(expect.any(Number)); |
| 64 | expect(created.volume.createdAt).toEqual(expect.any(String)); |
| 65 | |
| 66 | const fetched = yield* readVolumeInstance(created.volume.volumeInstanceId); |
| 67 | expect(fetched.id).toEqual(created.volume.volumeInstanceId); |
| 68 | expect(fetched.volumeId).toEqual(created.volume.volumeId); |
| 69 | expect(fetched.mountPath).toEqual("/data"); |
| 70 | expect(fetched.environmentId).toEqual(created.volume.environmentId); |
| 71 | expect(fetched.volume.name).toEqual(created.volume.name); |
| 72 | expect(fetched.volume.projectId).toEqual(created.volume.projectId); |
| 73 | expect(fetched.serviceId).toBeNull(); |
| 74 | |
| 75 | const provider = yield* Provider.findProvider(Railway.Volume); |
| 76 | const listed = yield* provider.list(); |
| 77 | const found = listed.find((volume) => volume.volumeId === created.volume.volumeId); |
| 78 | expect(found).toBeDefined(); |
| 79 | expect(found?.volumeInstanceId).toEqual(created.volume.volumeInstanceId); |
| 80 | expect(found?.mountPath).toEqual("/data"); |
| 81 | expect(found?.name).toEqual(created.volume.name); |
| 82 | |
| 83 | const updated = yield* stack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | const { project, environment } = yield* suitePartition; |
| 86 | const volume = yield* Railway.Volume("Data", { |
| 87 | project, |
| 88 | environment, |
| 89 | mountPath: "/app/data", |
| 90 | }); |
| 91 | return { project, environment, volume }; |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | expect(updated.volume.volumeId).toEqual(created.volume.volumeId); |
| 96 | expect(updated.volume.volumeInstanceId).toEqual(created.volume.volumeInstanceId); |
| 97 | expect(updated.volume.projectId).toEqual(created.volume.projectId); |
| 98 | expect(updated.volume.environmentId).toEqual(created.volume.environmentId); |
| 99 | expect(updated.volume.mountPath).toEqual("/app/data"); |
| 100 | expect(updated.volume.name).toEqual(created.volume.name); |
| 101 | |
| 102 | const fetchedUpdate = yield* readVolumeInstance(updated.volume.volumeInstanceId); |
| 103 | expect(fetchedUpdate.id).toEqual(updated.volume.volumeInstanceId); |
| 104 | expect(fetchedUpdate.mountPath).toEqual("/app/data"); |
| 105 | expect(fetchedUpdate.volume.name).toEqual(updated.volume.name); |
| 106 | |
| 107 | yield* stack.destroy(); |
| 108 | |
| 109 | const gone = yield* waitUntilVolumeGone(created.volume.volumeInstanceId); |
| 110 | expect(gone).toEqual("gone"); |
| 111 | }).pipe(logLevel), |
| 112 | { |
| 113 | tags: [ |
| 114 | "provider:railway", |
| 115 | "provider:railway:project", |
| 116 | "provider:railway:projectenvironment", |
| 117 | "provider:railway:volume", |
| 118 | "live", |
| 119 | ], |
| 120 | timeout: 120_000, |
| 121 | }, |
| 122 | ); |
| 123 | |
| 124 | test.provider( |
| 125 | "refuse a second volume on a service", |
| 126 | (stack) => |
| 127 | Effect.gen(function* () { |
| 128 | yield* stack.destroy(); |
| 129 | |
| 130 | const created = yield* stack.deploy( |
| 131 | Effect.gen(function* () { |
| 132 | const { project, environment } = yield* suitePartition; |
| 133 | const api = yield* Railway.Service("Api", { |
| 134 | project, |
| 135 | environment, |
| 136 | image: "hashicorp/http-echo", |
| 137 | port: 5678, |
| 138 | }); |
| 139 | const data = yield* Railway.Volume("Data", { |
| 140 | project, |
| 141 | environment, |
| 142 | mountPath: "/data", |
| 143 | service: api, |
| 144 | }); |
| 145 | return { project, environment, api, data }; |
| 146 | }), |
| 147 | ); |
| 148 | |
| 149 | const result = yield* Effect.result( |
| 150 | stack.deploy( |
| 151 | Effect.gen(function* () { |
| 152 | const { project, environment } = yield* suitePartition; |
| 153 | const api = yield* Railway.Service("Api", { |
| 154 | project, |
| 155 | environment, |
| 156 | image: "hashicorp/http-echo", |
| 157 | port: 5678, |
| 158 | }); |
| 159 | const data = yield* Railway.Volume("Data", { |
| 160 | project, |
| 161 | environment, |
| 162 | mountPath: "/data", |
| 163 | service: api, |
| 164 | }); |
| 165 | const cache = yield* Railway.Volume("Cache", { |
| 166 | project, |
| 167 | environment, |
| 168 | mountPath: "/cache", |
| 169 | service: api, |
| 170 | }); |
| 171 | return { project, environment, api, data, cache }; |
| 172 | }), |
| 173 | ), |
| 174 | ); |
| 175 | |
| 176 | expect(created.data.serviceId).toEqual(created.api.serviceId); |
| 177 | expect(Result.isFailure(result)).toEqual(true); |
| 178 | if (Result.isFailure(result)) { |
| 179 | expect(result.failure._tag).toEqual("Railway.MultipleVolumes"); |
| 180 | } |
| 181 | |
| 182 | yield* stack.destroy(); |
| 183 | }).pipe(logLevel), |
| 184 | { |
| 185 | tags: [ |
| 186 | "provider:railway", |
| 187 | "provider:railway:project", |
| 188 | "provider:railway:projectenvironment", |
| 189 | "provider:railway:service", |
| 190 | "provider:railway:volume", |
| 191 | "live", |
| 192 | ], |
| 193 | timeout: 120_000, |
| 194 | }, |
| 195 | ); |
| 196 | |
| 197 | test.provider( |
| 198 | "MountVolume attaches a disk to a hosted Service", |
| 199 | (stack) => |
| 200 | Effect.gen(function* () { |
| 201 | yield* stack.destroy(); |
| 202 | |
| 203 | const created = yield* stack.deploy( |
| 204 | Effect.gen(function* () { |
| 205 | const data = yield* Data; |
| 206 | const api = yield* VolumeApi; |
| 207 | return { data, api }; |
| 208 | }), |
| 209 | ); |
| 210 | |
| 211 | expect(created.data.volumeId.length).toBeGreaterThan(0); |
| 212 | // Volume is created disconnected; MountVolume attaches it from |
| 213 | // the Service. Resource attrs are not refreshed after that bind. |
| 214 | expect(created.data.serviceId).toBeUndefined(); |
| 215 | expect(created.data.mountPath).toEqual(VOLUME_PATH); |
| 216 | expect(created.api.url).toEqual(expect.any(String)); |
| 217 | expect(created.api.url).toContain("up.railway.app"); |
| 218 | |
| 219 | const fetched = yield* readVolumeInstance(created.data.volumeInstanceId); |
| 220 | expect(fetched.serviceId).toEqual(created.api.serviceId); |
| 221 | expect(fetched.mountPath).toEqual(VOLUME_PATH); |
| 222 | expect(fetched.volumeId).toEqual(created.data.volumeId); |
| 223 | |
| 224 | const client = yield* HttpClient.HttpClient; |
| 225 | const health = yield* client.get(`${created.api.url}/health`).pipe( |
| 226 | Effect.flatMap((res) => |
| 227 | res.status === 200 |
| 228 | ? Effect.succeed(res) |
| 229 | : Effect.fail(new Error(`health returned ${res.status}`)), |
| 230 | ), |
| 231 | Effect.retry({ schedule: Schedule.spaced("4 seconds"), times: 10 }), |
| 232 | ); |
| 233 | expect(health.status).toEqual(200); |
| 234 | |
| 235 | const put = yield* client.execute( |
| 236 | HttpClientRequest.put(`${created.api.url}/${MARKER_FILE}`).pipe( |
| 237 | HttpClientRequest.bodyText(MARKER), |
| 238 | ), |
| 239 | ); |
| 240 | expect(put.status).toEqual(204); |
| 241 | |
| 242 | const got = yield* client.get(`${created.api.url}/${MARKER_FILE}`); |
| 243 | expect(got.status).toEqual(200); |
| 244 | expect(yield* got.text).toEqual(MARKER); |
| 245 | |
| 246 | yield* stack.destroy(); |
| 247 | |
| 248 | const gone = yield* waitUntilVolumeGone(created.data.volumeInstanceId); |
| 249 | expect(gone).toEqual("gone"); |
| 250 | }).pipe(logLevel), |
| 251 | { |
| 252 | tags: [ |
| 253 | "provider:railway", |
| 254 | "provider:railway:mountvolume", |
| 255 | "provider:railway:project", |
| 256 | "provider:railway:projectenvironment", |
| 257 | "provider:railway:service", |
| 258 | "provider:railway:volume", |
| 259 | "live", |
| 260 | ], |
| 261 | timeout: 120_000, |
| 262 | }, |
| 263 | ); |