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 { activeReplicaRegions, serviceRegionPlacement } from "@alchemy.run/railway/ServiceRegion"; |
| 3 | import { withEnvironmentConfigLock } from "@alchemy.run/railway/transient"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import { Query } from "@distilled.cloud/core/query"; |
| 7 | import { Railway as RailwayApi } from "@distilled.cloud/railway"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Result from "effect/Result"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import { suitePartition } from "./suiteProject.ts"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: Railway.providers() }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | const readServiceDeletedAt = Query.fn((id: string) => ({ |
| 21 | deletedAt: RailwayApi.service({ id }).deletedAt, |
| 22 | })); |
| 23 | |
| 24 | const readService = Query.fn((id: string) => { |
| 25 | const service = RailwayApi.service({ id }); |
| 26 | return { |
| 27 | id: service.id, |
| 28 | name: service.name, |
| 29 | projectId: service.projectId, |
| 30 | deletedAt: service.deletedAt, |
| 31 | }; |
| 32 | }); |
| 33 | |
| 34 | const readServiceInstance = Query.fn((environmentId: string, serviceId: string) => { |
| 35 | const instance = RailwayApi.serviceInstance({ environmentId, serviceId }); |
| 36 | return { |
| 37 | serviceId: instance.serviceId, |
| 38 | environmentId: instance.environmentId, |
| 39 | source: instance.source.pipe( |
| 40 | Query.map((source) => ({ image: source.image, repo: source.repo })), |
| 41 | ), |
| 42 | healthcheckPath: instance.healthcheckPath, |
| 43 | preDeployCommand: instance.preDeployCommand, |
| 44 | numReplicas: instance.numReplicas, |
| 45 | dockerfilePath: instance.dockerfilePath, |
| 46 | }; |
| 47 | }); |
| 48 | |
| 49 | const readServiceDomains = Query.fn( |
| 50 | (input: { projectId: string; environmentId: string; serviceId: string }) => ({ |
| 51 | serviceDomains: RailwayApi.domains(input).serviceDomains.pipe( |
| 52 | Query.map((domain) => ({ |
| 53 | id: domain.id, |
| 54 | domain: domain.domain, |
| 55 | targetPort: domain.targetPort, |
| 56 | deletedAt: domain.deletedAt, |
| 57 | syncStatus: domain.syncStatus, |
| 58 | })), |
| 59 | ), |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | const environmentPatchCommit = Query.fn( |
| 64 | (args: { environmentId: string; commitMessage: string; patch: unknown }) => |
| 65 | RailwayApi.environmentPatchCommit(args), |
| 66 | ); |
| 67 | |
| 68 | const readGithubRepos = Query.fn(() => |
| 69 | RailwayApi.githubRepos().pipe( |
| 70 | Query.map((repo) => ({ fullName: repo.fullName, defaultBranch: repo.defaultBranch })), |
| 71 | ), |
| 72 | ); |
| 73 | |
| 74 | const readEnvironmentConfig = Query.fn((id: string, projectId: string) => ({ |
| 75 | config: RailwayApi.environment({ id, projectId }).config, |
| 76 | })); |
| 77 | |
| 78 | const waitUntilGone = (serviceId: string) => |
| 79 | readServiceDeletedAt(serviceId).pipe( |
| 80 | Effect.map((service) => (service.deletedAt != null ? ("gone" as const) : ("found" as const))), |
| 81 | Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)), |
| 82 | Effect.repeat({ |
| 83 | schedule: Schedule.spaced("1 second"), |
| 84 | until: (status) => status === "gone", |
| 85 | times: 10, |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | const waitUntilNoServiceDomains = (input: { |
| 90 | projectId: string; |
| 91 | environmentId: string; |
| 92 | serviceId: string; |
| 93 | }) => |
| 94 | readServiceDomains(input).pipe( |
| 95 | Effect.map((domains) => |
| 96 | domains.serviceDomains.some( |
| 97 | (domain) => |
| 98 | domain.deletedAt == null && |
| 99 | domain.syncStatus !== "DELETED" && |
| 100 | domain.syncStatus !== "DELETING", |
| 101 | ) |
| 102 | ? ("found" as const) |
| 103 | : ("gone" as const), |
| 104 | ), |
| 105 | Effect.repeat({ |
| 106 | schedule: Schedule.spaced("1 second"), |
| 107 | until: (status) => status === "gone", |
| 108 | times: 10, |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | test.provider( |
| 113 | "create, serve, list, update, and delete an image service", |
| 114 | (stack) => |
| 115 | Effect.gen(function* () { |
| 116 | yield* stack.destroy(); |
| 117 | |
| 118 | const created = yield* stack.deploy( |
| 119 | Effect.gen(function* () { |
| 120 | const { project, environment } = yield* suitePartition; |
| 121 | const api = yield* Railway.Service("Api", { |
| 122 | project, |
| 123 | environment, |
| 124 | image: "nginx:alpine", |
| 125 | port: 80, |
| 126 | healthcheckPath: "/", |
| 127 | preDeploy: { command: "echo predeploy" }, |
| 128 | }); |
| 129 | return { project, environment, api }; |
| 130 | }), |
| 131 | ); |
| 132 | |
| 133 | expect(created.api.serviceId).toEqual(expect.any(String)); |
| 134 | expect(created.api.serviceId.length).toBeGreaterThan(0); |
| 135 | expect(created.api.projectId).toEqual(created.project.projectId); |
| 136 | expect(created.api.environmentId).toEqual(created.environment.environmentId); |
| 137 | expect(created.api.name).toEqual(expect.any(String)); |
| 138 | expect(created.api.name.length).toBeGreaterThan(0); |
| 139 | expect(created.api.name.length).toBeLessThanOrEqual(32); |
| 140 | expect(created.api.name).toMatch(/^[a-z][a-z0-9-]*$/); |
| 141 | expect(created.api.port).toEqual(80); |
| 142 | expect(created.api.domain).toEqual(expect.any(String)); |
| 143 | expect(created.api.domain).toContain("up.railway.app"); |
| 144 | expect(created.api.url).toEqual(`https://${created.api.domain}`); |
| 145 | expect(created.api.domainId).toEqual(expect.any(String)); |
| 146 | expect(created.api.domainId!.length).toBeGreaterThan(0); |
| 147 | |
| 148 | const fetched = yield* readService(created.api.serviceId); |
| 149 | expect(fetched.id).toEqual(created.api.serviceId); |
| 150 | expect(fetched.name).toEqual(created.api.name); |
| 151 | expect(fetched.projectId).toEqual(created.api.projectId); |
| 152 | expect(fetched.deletedAt).toBeNull(); |
| 153 | |
| 154 | const instance = yield* readServiceInstance(created.api.environmentId, created.api.serviceId); |
| 155 | expect(instance.serviceId).toEqual(created.api.serviceId); |
| 156 | expect(instance.environmentId).toEqual(created.api.environmentId); |
| 157 | expect(instance.source?.image).toEqual(expect.stringContaining("nginx:alpine")); |
| 158 | expect(instance.healthcheckPath).toEqual("/"); |
| 159 | expect( |
| 160 | instance.preDeployCommand === "echo predeploy" || |
| 161 | (Array.isArray(instance.preDeployCommand) && |
| 162 | instance.preDeployCommand.length === 1 && |
| 163 | instance.preDeployCommand[0] === "echo predeploy"), |
| 164 | ).toEqual(true); |
| 165 | // Railway omits numReplicas until you scale; default is one replica. |
| 166 | expect(instance.numReplicas === null || instance.numReplicas === 1).toEqual(true); |
| 167 | expect(created.api.healthcheckPath).toEqual("/"); |
| 168 | expect(created.api.replicas === undefined || created.api.replicas === 1).toEqual(true); |
| 169 | |
| 170 | const domains = yield* readServiceDomains({ |
| 171 | environmentId: created.api.environmentId, |
| 172 | projectId: created.api.projectId, |
| 173 | serviceId: created.api.serviceId, |
| 174 | }); |
| 175 | const liveDomain = domains.serviceDomains.find( |
| 176 | (domain) => domain.deletedAt == null && domain.syncStatus !== "DELETED", |
| 177 | ); |
| 178 | expect(liveDomain).toBeDefined(); |
| 179 | expect(liveDomain?.domain).toEqual(created.api.domain); |
| 180 | expect(liveDomain?.targetPort).toEqual(80); |
| 181 | |
| 182 | const provider = yield* Provider.findProvider(Railway.Service); |
| 183 | const listed = yield* provider.list(); |
| 184 | const found = listed.find((service) => service.serviceId === created.api.serviceId); |
| 185 | expect(found).toBeDefined(); |
| 186 | expect(found?.name).toEqual(created.api.name); |
| 187 | expect(found?.projectId).toEqual(created.api.projectId); |
| 188 | |
| 189 | const client = yield* HttpClient.HttpClient; |
| 190 | const body = yield* client.get(created.api.url!).pipe( |
| 191 | Effect.flatMap((res) => |
| 192 | res.status === 200 ? res.text : Effect.fail(new Error(`api returned ${res.status}`)), |
| 193 | ), |
| 194 | Effect.retry({ schedule: Schedule.spaced("4 seconds"), times: 10 }), |
| 195 | ); |
| 196 | expect(typeof body).toEqual("string"); |
| 197 | expect(body.length).toBeGreaterThan(0); |
| 198 | |
| 199 | const nextName = created.api.name.slice(0, -1) + (created.api.name.endsWith("z") ? "y" : "z"); |
| 200 | |
| 201 | const updated = yield* stack.deploy( |
| 202 | Effect.gen(function* () { |
| 203 | const { project, environment } = yield* suitePartition; |
| 204 | const api = yield* Railway.Service("Api", { |
| 205 | project, |
| 206 | environment, |
| 207 | image: "nginx:alpine", |
| 208 | port: 80, |
| 209 | name: nextName, |
| 210 | healthcheckPath: "/", |
| 211 | preDeploy: { command: null }, |
| 212 | }); |
| 213 | return { project, environment, api }; |
| 214 | }), |
| 215 | ); |
| 216 | |
| 217 | expect(updated.api.serviceId).toEqual(created.api.serviceId); |
| 218 | expect(updated.api.name).toEqual(nextName); |
| 219 | expect(updated.api.projectId).toEqual(created.api.projectId); |
| 220 | expect(updated.api.url).toEqual(created.api.url); |
| 221 | |
| 222 | const updatedInstance = yield* readServiceInstance( |
| 223 | updated.api.environmentId, |
| 224 | updated.api.serviceId, |
| 225 | ); |
| 226 | expect(updatedInstance.healthcheckPath).toEqual("/"); |
| 227 | expect( |
| 228 | updatedInstance.preDeployCommand == null || |
| 229 | (Array.isArray(updatedInstance.preDeployCommand) && |
| 230 | updatedInstance.preDeployCommand.length === 0), |
| 231 | ).toEqual(true); |
| 232 | |
| 233 | const fetchedUpdate = yield* readService(updated.api.serviceId); |
| 234 | expect(fetchedUpdate.id).toEqual(updated.api.serviceId); |
| 235 | expect(fetchedUpdate.name).toEqual(nextName); |
| 236 | |
| 237 | const privateUpdate = yield* stack.deploy( |
| 238 | Effect.gen(function* () { |
| 239 | const { project, environment } = yield* suitePartition; |
| 240 | const api = yield* Railway.Service("Api", { |
| 241 | project, |
| 242 | environment, |
| 243 | image: "hashicorp/http-echo", |
| 244 | port: 5678, |
| 245 | name: nextName, |
| 246 | healthcheckPath: "/health", |
| 247 | publicDomain: false, |
| 248 | }); |
| 249 | return { api }; |
| 250 | }), |
| 251 | ); |
| 252 | |
| 253 | expect(privateUpdate.api.serviceId).toEqual(created.api.serviceId); |
| 254 | expect(privateUpdate.api.dnsName).toEqual(`${nextName}.railway.internal`); |
| 255 | expect(privateUpdate.api.url).toBeUndefined(); |
| 256 | expect(privateUpdate.api.domain).toBeUndefined(); |
| 257 | expect(privateUpdate.api.domainId).toBeUndefined(); |
| 258 | expect( |
| 259 | yield* waitUntilNoServiceDomains({ |
| 260 | projectId: privateUpdate.api.projectId, |
| 261 | environmentId: privateUpdate.api.environmentId, |
| 262 | serviceId: privateUpdate.api.serviceId, |
| 263 | }), |
| 264 | ).toEqual("gone"); |
| 265 | |
| 266 | const publicUpdate = yield* stack.deploy( |
| 267 | Effect.gen(function* () { |
| 268 | const { project, environment } = yield* suitePartition; |
| 269 | const api = yield* Railway.Service("Api", { |
| 270 | project, |
| 271 | environment, |
| 272 | image: "hashicorp/http-echo", |
| 273 | port: 5678, |
| 274 | name: nextName, |
| 275 | healthcheckPath: "/health", |
| 276 | publicDomain: true, |
| 277 | }); |
| 278 | return { api }; |
| 279 | }), |
| 280 | ); |
| 281 | |
| 282 | expect(publicUpdate.api.serviceId).toEqual(created.api.serviceId); |
| 283 | expect(publicUpdate.api.domainId).toEqual(expect.any(String)); |
| 284 | expect(publicUpdate.api.url).toEqual(`https://${publicUpdate.api.domain}`); |
| 285 | expect(publicUpdate.api.dnsName).toEqual(`${nextName}.railway.internal`); |
| 286 | |
| 287 | // Add a foreign generated domain. The config map key may not equal |
| 288 | // the GraphQL id, and list order is not ownership — wait for a new |
| 289 | // live row rather than assuming `[0] === patch UUID`. |
| 290 | const liveDomainIds = (domains: { |
| 291 | serviceDomains: ReadonlyArray<{ id: string; deletedAt: string | null; syncStatus: string }>; |
| 292 | }) => |
| 293 | domains.serviceDomains |
| 294 | .filter( |
| 295 | (domain) => |
| 296 | domain.deletedAt == null && |
| 297 | domain.syncStatus !== "DELETED" && |
| 298 | domain.syncStatus !== "DELETING", |
| 299 | ) |
| 300 | .map((domain) => domain.id); |
| 301 | const beforeForeign = new Set( |
| 302 | liveDomainIds( |
| 303 | yield* readServiceDomains({ |
| 304 | projectId: publicUpdate.api.projectId, |
| 305 | environmentId: publicUpdate.api.environmentId, |
| 306 | serviceId: publicUpdate.api.serviceId, |
| 307 | }), |
| 308 | ), |
| 309 | ); |
| 310 | const foreignPatchId = yield* Effect.sync(() => crypto.randomUUID()); |
| 311 | yield* withEnvironmentConfigLock( |
| 312 | publicUpdate.api.environmentId, |
| 313 | environmentPatchCommit({ |
| 314 | environmentId: publicUpdate.api.environmentId, |
| 315 | commitMessage: "Arrange generated domains for ownership test", |
| 316 | patch: { |
| 317 | services: { |
| 318 | [publicUpdate.api.serviceId]: { |
| 319 | networking: { |
| 320 | serviceDomains: { [foreignPatchId]: {}, [publicUpdate.api.domainId!]: {} }, |
| 321 | }, |
| 322 | }, |
| 323 | }, |
| 324 | }, |
| 325 | }), |
| 326 | ); |
| 327 | const orderedIds = yield* readServiceDomains({ |
| 328 | projectId: publicUpdate.api.projectId, |
| 329 | environmentId: publicUpdate.api.environmentId, |
| 330 | serviceId: publicUpdate.api.serviceId, |
| 331 | }).pipe( |
| 332 | Effect.map(liveDomainIds), |
| 333 | Effect.repeat({ |
| 334 | schedule: Schedule.spaced("1 second"), |
| 335 | until: (ids) => |
| 336 | ids.includes(publicUpdate.api.domainId!) && |
| 337 | (ids.includes(foreignPatchId) || ids.some((id) => !beforeForeign.has(id))), |
| 338 | times: 10, |
| 339 | }), |
| 340 | ); |
| 341 | const foreignDomainId = |
| 342 | orderedIds.find((id) => id === foreignPatchId) ?? |
| 343 | orderedIds.find((id) => !beforeForeign.has(id)); |
| 344 | expect(foreignDomainId).toBeDefined(); |
| 345 | if (foreignDomainId === undefined) { |
| 346 | return; |
| 347 | } |
| 348 | expect(foreignDomainId).not.toEqual(publicUpdate.api.domainId); |
| 349 | expect(orderedIds).toContain(publicUpdate.api.domainId); |
| 350 | |
| 351 | const publicWithForeignDomain = yield* stack.deploy( |
| 352 | Effect.gen(function* () { |
| 353 | const { project, environment } = yield* suitePartition; |
| 354 | const api = yield* Railway.Service("Api", { |
| 355 | project, |
| 356 | environment, |
| 357 | image: "hashicorp/http-echo", |
| 358 | port: 5678, |
| 359 | name: nextName, |
| 360 | healthcheckPath: "/health", |
| 361 | publicDomain: true, |
| 362 | }); |
| 363 | return { api }; |
| 364 | }), |
| 365 | ); |
| 366 | expect(publicWithForeignDomain.api.domainId).toEqual(publicUpdate.api.domainId); |
| 367 | |
| 368 | const privateWithForeignDomain = yield* stack.deploy( |
| 369 | Effect.gen(function* () { |
| 370 | const { project, environment } = yield* suitePartition; |
| 371 | const api = yield* Railway.Service("Api", { |
| 372 | project, |
| 373 | environment, |
| 374 | image: "hashicorp/http-echo", |
| 375 | port: 5678, |
| 376 | name: nextName, |
| 377 | healthcheckPath: "/health", |
| 378 | publicDomain: false, |
| 379 | }); |
| 380 | return { api }; |
| 381 | }), |
| 382 | ); |
| 383 | expect(privateWithForeignDomain.api.domainId).toBeUndefined(); |
| 384 | const remainingDomains = yield* readServiceDomains({ |
| 385 | projectId: publicUpdate.api.projectId, |
| 386 | environmentId: publicUpdate.api.environmentId, |
| 387 | serviceId: publicUpdate.api.serviceId, |
| 388 | }); |
| 389 | const remainingIds = remainingDomains.serviceDomains |
| 390 | .filter((domain) => domain.deletedAt == null && domain.syncStatus !== "DELETED") |
| 391 | .map((domain) => domain.id); |
| 392 | expect(remainingIds).toContain(foreignDomainId); |
| 393 | expect(remainingIds).not.toContain(publicUpdate.api.domainId); |
| 394 | |
| 395 | yield* stack.destroy(); |
| 396 | |
| 397 | const gone = yield* waitUntilGone(created.api.serviceId); |
| 398 | expect(gone).toEqual("gone"); |
| 399 | }).pipe(logLevel), |
| 400 | { |
| 401 | tags: [ |
| 402 | "provider:railway", |
| 403 | "provider:railway:project", |
| 404 | "provider:railway:projectenvironment", |
| 405 | "provider:railway:service", |
| 406 | "live", |
| 407 | ], |
| 408 | // Multiple deployment cycles plus deletion need their own lifecycle budget. |
| 409 | // Leave 30 seconds before the 240-second process cap for failure cleanup. |
| 410 | timeout: 210_000, |
| 411 | }, |
| 412 | ); |
| 413 | |
| 414 | test.provider( |
| 415 | "create, read, and delete a private image service", |
| 416 | (stack) => |
| 417 | Effect.gen(function* () { |
| 418 | yield* stack.destroy(); |
| 419 | |
| 420 | const created = yield* stack.deploy( |
| 421 | Effect.gen(function* () { |
| 422 | const { project, environment } = yield* suitePartition; |
| 423 | const worker = yield* Railway.Service("Worker", { |
| 424 | project, |
| 425 | environment, |
| 426 | image: "hashicorp/http-echo", |
| 427 | port: 5678, |
| 428 | publicDomain: false, |
| 429 | }); |
| 430 | return { worker }; |
| 431 | }), |
| 432 | ); |
| 433 | |
| 434 | expect(created.worker.serviceId).toEqual(expect.any(String)); |
| 435 | expect(created.worker.dnsName).toEqual(`${created.worker.name}.railway.internal`); |
| 436 | expect(created.worker.url).toBeUndefined(); |
| 437 | expect(created.worker.domain).toBeUndefined(); |
| 438 | expect(created.worker.domainId).toBeUndefined(); |
| 439 | expect( |
| 440 | yield* waitUntilNoServiceDomains({ |
| 441 | projectId: created.worker.projectId, |
| 442 | environmentId: created.worker.environmentId, |
| 443 | serviceId: created.worker.serviceId, |
| 444 | }), |
| 445 | ).toEqual("gone"); |
| 446 | |
| 447 | // A second deploy exercises refresh/read without claiming or creating a |
| 448 | // generated domain. |
| 449 | const refreshed = yield* stack.deploy( |
| 450 | Effect.gen(function* () { |
| 451 | const { project, environment } = yield* suitePartition; |
| 452 | const worker = yield* Railway.Service("Worker", { |
| 453 | project, |
| 454 | environment, |
| 455 | image: "hashicorp/http-echo", |
| 456 | port: 5678, |
| 457 | publicDomain: false, |
| 458 | }); |
| 459 | return { worker }; |
| 460 | }), |
| 461 | ); |
| 462 | expect(refreshed.worker.serviceId).toEqual(created.worker.serviceId); |
| 463 | expect(refreshed.worker.dnsName).toEqual(created.worker.dnsName); |
| 464 | expect(refreshed.worker.url).toBeUndefined(); |
| 465 | expect(refreshed.worker.domain).toBeUndefined(); |
| 466 | expect(refreshed.worker.domainId).toBeUndefined(); |
| 467 | |
| 468 | yield* stack.destroy(); |
| 469 | expect(yield* waitUntilGone(created.worker.serviceId)).toEqual("gone"); |
| 470 | }).pipe(logLevel), |
| 471 | { |
| 472 | tags: [ |
| 473 | "provider:railway", |
| 474 | "provider:railway:project", |
| 475 | "provider:railway:projectenvironment", |
| 476 | "provider:railway:service", |
| 477 | "live", |
| 478 | ], |
| 479 | timeout: 120_000, |
| 480 | }, |
| 481 | ); |
| 482 | |
| 483 | test.provider( |
| 484 | "deleting one environment's service keeps its instances in other environments", |
| 485 | (stack) => |
| 486 | Effect.gen(function* () { |
| 487 | yield* stack.destroy(); |
| 488 | |
| 489 | // Railway keeps one service per project with an instance per |
| 490 | // environment. A second Service with the same name attaches to it. |
| 491 | const created = yield* stack.deploy( |
| 492 | Effect.gen(function* () { |
| 493 | const { project, environment } = yield* suitePartition; |
| 494 | const staging = yield* Railway.Environment("Staging", { project }); |
| 495 | const prod = yield* Railway.Service("WebProd", { |
| 496 | project, |
| 497 | environment, |
| 498 | image: "hashicorp/http-echo", |
| 499 | port: 5678, |
| 500 | publicDomain: false, |
| 501 | }); |
| 502 | const stage = yield* Railway.Service("WebStaging", { |
| 503 | project, |
| 504 | environment: staging, |
| 505 | name: prod.name, |
| 506 | image: "hashicorp/http-echo", |
| 507 | port: 5678, |
| 508 | publicDomain: false, |
| 509 | }); |
| 510 | return { prod, stage }; |
| 511 | }), |
| 512 | ); |
| 513 | expect(created.stage.serviceId).toEqual(created.prod.serviceId); |
| 514 | expect(created.stage.environmentId).not.toEqual(created.prod.environmentId); |
| 515 | |
| 516 | // Dropping the staging Service must only delete its instance. |
| 517 | yield* stack.deploy( |
| 518 | Effect.gen(function* () { |
| 519 | const { project, environment } = yield* suitePartition; |
| 520 | yield* Railway.Environment("Staging", { project }); |
| 521 | const prod = yield* Railway.Service("WebProd", { |
| 522 | project, |
| 523 | environment, |
| 524 | image: "hashicorp/http-echo", |
| 525 | port: 5678, |
| 526 | publicDomain: false, |
| 527 | }); |
| 528 | return { prod }; |
| 529 | }), |
| 530 | ); |
| 531 | |
| 532 | const service = yield* readService(created.prod.serviceId); |
| 533 | expect(service.deletedAt).toBeNull(); |
| 534 | const prodInstance = yield* readServiceInstance( |
| 535 | created.prod.environmentId, |
| 536 | created.prod.serviceId, |
| 537 | ); |
| 538 | expect(prodInstance.serviceId).toEqual(created.prod.serviceId); |
| 539 | |
| 540 | yield* stack.destroy(); |
| 541 | expect(yield* waitUntilGone(created.prod.serviceId)).toEqual("gone"); |
| 542 | }).pipe(logLevel), |
| 543 | { |
| 544 | tags: [ |
| 545 | "provider:railway", |
| 546 | "provider:railway:project", |
| 547 | "provider:railway:projectenvironment", |
| 548 | "provider:railway:service", |
| 549 | "live", |
| 550 | ], |
| 551 | timeout: 240_000, |
| 552 | }, |
| 553 | ); |
| 554 | |
| 555 | const localContextDir = `${import.meta.dirname}/fixtures/local-context`; |
| 556 | |
| 557 | test.provider( |
| 558 | "create, serve, switch to image, and delete a local Docker context service", |
| 559 | (stack) => |
| 560 | Effect.gen(function* () { |
| 561 | yield* stack.destroy(); |
| 562 | |
| 563 | const created = yield* stack.deploy( |
| 564 | Effect.gen(function* () { |
| 565 | const { project, environment } = yield* suitePartition; |
| 566 | const api = yield* Railway.Service("Ctx", { |
| 567 | project, |
| 568 | environment, |
| 569 | context: localContextDir, |
| 570 | port: 80, |
| 571 | healthcheckPath: "/", |
| 572 | }); |
| 573 | return { api }; |
| 574 | }), |
| 575 | ); |
| 576 | |
| 577 | expect(created.api.serviceId).toEqual(expect.any(String)); |
| 578 | expect(created.api.domain).toContain("up.railway.app"); |
| 579 | expect(created.api.url).toEqual(`https://${created.api.domain}`); |
| 580 | |
| 581 | const fromContext = yield* readServiceInstance( |
| 582 | created.api.environmentId, |
| 583 | created.api.serviceId, |
| 584 | ); |
| 585 | expect(fromContext.dockerfilePath).toEqual("Dockerfile"); |
| 586 | |
| 587 | const client = yield* HttpClient.HttpClient; |
| 588 | const body = yield* client.get(created.api.url!).pipe( |
| 589 | Effect.flatMap((res) => |
| 590 | res.status === 200 ? res.text : Effect.fail(new Error(`api returned ${res.status}`)), |
| 591 | ), |
| 592 | Effect.retry({ schedule: Schedule.spaced("4 seconds"), times: 10 }), |
| 593 | ); |
| 594 | expect(typeof body).toEqual("string"); |
| 595 | expect(body.length).toBeGreaterThan(0); |
| 596 | |
| 597 | const updated = yield* stack.deploy( |
| 598 | Effect.gen(function* () { |
| 599 | const { project, environment } = yield* suitePartition; |
| 600 | const api = yield* Railway.Service("Ctx", { |
| 601 | project, |
| 602 | environment, |
| 603 | image: "nginx:alpine", |
| 604 | port: 80, |
| 605 | healthcheckPath: "/", |
| 606 | }); |
| 607 | return { api }; |
| 608 | }), |
| 609 | ); |
| 610 | expect(updated.api.serviceId).toEqual(created.api.serviceId); |
| 611 | |
| 612 | const fromImage = yield* readServiceInstance( |
| 613 | updated.api.environmentId, |
| 614 | updated.api.serviceId, |
| 615 | ); |
| 616 | expect(fromImage.source?.image).toEqual(expect.stringContaining("nginx:alpine")); |
| 617 | |
| 618 | const afterImage = yield* client.get(updated.api.url!).pipe( |
| 619 | Effect.flatMap((res) => |
| 620 | res.status === 200 ? res.text : Effect.fail(new Error(`api returned ${res.status}`)), |
| 621 | ), |
| 622 | Effect.retry({ schedule: Schedule.spaced("4 seconds"), times: 10 }), |
| 623 | ); |
| 624 | expect(typeof afterImage).toEqual("string"); |
| 625 | expect(afterImage.length).toBeGreaterThan(0); |
| 626 | |
| 627 | yield* stack.destroy(); |
| 628 | expect(yield* waitUntilGone(created.api.serviceId)).toEqual("gone"); |
| 629 | }).pipe(logLevel), |
| 630 | { |
| 631 | tags: [ |
| 632 | "provider:railway", |
| 633 | "provider:railway:project", |
| 634 | "provider:railway:projectenvironment", |
| 635 | "provider:railway:service", |
| 636 | "live", |
| 637 | ], |
| 638 | timeout: 120_000, |
| 639 | }, |
| 640 | ); |
| 641 | |
| 642 | // GitHub repo source requires a GitHub App connection on the Railway |
| 643 | // account. The probe always runs and pins the typed gate tag. The |
| 644 | // lifecycle is opt-in via RAILWAY_TEST_GITHUB=1. |
| 645 | const githubEntitled = !!process.env.RAILWAY_TEST_GITHUB; |
| 646 | |
| 647 | test.provider( |
| 648 | "unconnected GitHub surfaces a typed entitlement error", |
| 649 | (stack) => |
| 650 | Effect.gen(function* () { |
| 651 | yield* stack.destroy(); |
| 652 | |
| 653 | const result = yield* Effect.result(readGithubRepos()); |
| 654 | if (Result.isSuccess(result)) { |
| 655 | yield* Effect.logInfo( |
| 656 | `GitHub is connected (${result.success.length} repos); probe is a no-op`, |
| 657 | ); |
| 658 | yield* stack.destroy(); |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | // GitHub App is not connected for this token: GraphQL `Not Authorized` |
| 663 | // is already the typed `RailwayForbidden` tag (never UnknownRailwayError). |
| 664 | expect(result.failure._tag === "RailwayForbidden").toBe(true); |
| 665 | |
| 666 | yield* stack.destroy(); |
| 667 | }).pipe(logLevel), |
| 668 | { tags: ["provider:railway", "provider:railway:service", "live"], timeout: 120_000 }, |
| 669 | ); |
| 670 | |
| 671 | test.provider.skipIf(!githubEntitled)( |
| 672 | "create a github repo service", |
| 673 | (stack) => |
| 674 | Effect.gen(function* () { |
| 675 | yield* stack.destroy(); |
| 676 | |
| 677 | const repos = yield* readGithubRepos(); |
| 678 | const repo = repos[0]; |
| 679 | expect(repo).toBeDefined(); |
| 680 | expect(repo!.fullName.length).toBeGreaterThan(0); |
| 681 | |
| 682 | const created = yield* stack.deploy( |
| 683 | Effect.gen(function* () { |
| 684 | const { project, environment } = yield* suitePartition; |
| 685 | const api = yield* Railway.Service("Api", { |
| 686 | project, |
| 687 | environment, |
| 688 | repo: repo!.fullName, |
| 689 | branch: repo!.defaultBranch, |
| 690 | }); |
| 691 | return { project, environment, api }; |
| 692 | }), |
| 693 | ); |
| 694 | |
| 695 | expect(created.api.serviceId).toEqual(expect.any(String)); |
| 696 | expect(created.api.repo).toEqual(repo!.fullName); |
| 697 | |
| 698 | const instance = yield* readServiceInstance(created.api.environmentId, created.api.serviceId); |
| 699 | expect(instance.source?.repo).toEqual(repo!.fullName); |
| 700 | |
| 701 | yield* stack.destroy(); |
| 702 | |
| 703 | const gone = yield* waitUntilGone(created.api.serviceId); |
| 704 | expect(gone).toEqual("gone"); |
| 705 | }).pipe(logLevel), |
| 706 | { |
| 707 | tags: [ |
| 708 | "provider:railway", |
| 709 | "provider:railway:project", |
| 710 | "provider:railway:projectenvironment", |
| 711 | "provider:railway:service", |
| 712 | "live", |
| 713 | ], |
| 714 | timeout: 120_000, |
| 715 | }, |
| 716 | ); |
| 717 | |
| 718 | const placedRegions = (config: unknown, serviceId: string) => |
| 719 | activeReplicaRegions(serviceRegionPlacement(config, serviceId)).map((row) => row.region); |
| 720 | |
| 721 | test.provider( |
| 722 | "pin a service to a region and move it", |
| 723 | (stack) => |
| 724 | Effect.gen(function* () { |
| 725 | yield* stack.destroy(); |
| 726 | |
| 727 | const region = "europe-west4-drams3a"; |
| 728 | const created = yield* stack.deploy( |
| 729 | Effect.gen(function* () { |
| 730 | const { project, environment } = yield* suitePartition; |
| 731 | const api = yield* Railway.Service("Region", { |
| 732 | project, |
| 733 | environment, |
| 734 | image: "hashicorp/http-echo", |
| 735 | port: 5678, |
| 736 | publicDomain: false, |
| 737 | region, |
| 738 | }); |
| 739 | return { api }; |
| 740 | }), |
| 741 | ); |
| 742 | |
| 743 | expect(created.api.region).toEqual(region); |
| 744 | const createdConfig = yield* readEnvironmentConfig( |
| 745 | created.api.environmentId, |
| 746 | created.api.projectId, |
| 747 | ); |
| 748 | expect(placedRegions(createdConfig.config, created.api.serviceId)).toEqual([region]); |
| 749 | |
| 750 | const moved = "us-west2"; |
| 751 | const updated = yield* stack.deploy( |
| 752 | Effect.gen(function* () { |
| 753 | const { project, environment } = yield* suitePartition; |
| 754 | const api = yield* Railway.Service("Region", { |
| 755 | project, |
| 756 | environment, |
| 757 | image: "hashicorp/http-echo", |
| 758 | port: 5678, |
| 759 | publicDomain: false, |
| 760 | region: moved, |
| 761 | }); |
| 762 | return { api }; |
| 763 | }), |
| 764 | ); |
| 765 | |
| 766 | expect(updated.api.serviceId).toEqual(created.api.serviceId); |
| 767 | expect(updated.api.region).toEqual(moved); |
| 768 | const updatedConfig = yield* readEnvironmentConfig( |
| 769 | updated.api.environmentId, |
| 770 | updated.api.projectId, |
| 771 | ); |
| 772 | expect(placedRegions(updatedConfig.config, updated.api.serviceId)).toEqual([moved]); |
| 773 | |
| 774 | yield* stack.destroy(); |
| 775 | |
| 776 | const gone = yield* waitUntilGone(created.api.serviceId); |
| 777 | expect(gone).toEqual("gone"); |
| 778 | }).pipe(logLevel), |
| 779 | { |
| 780 | tags: [ |
| 781 | "provider:railway", |
| 782 | "provider:railway:project", |
| 783 | "provider:railway:projectenvironment", |
| 784 | "provider:railway:service", |
| 785 | "live", |
| 786 | ], |
| 787 | timeout: 180_000, |
| 788 | }, |
| 789 | ); |