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 { RailwayEnvironment } from "@alchemy.run/railway/Environment"; |
| 3 | import { FunctionProvider } from "@alchemy.run/railway/Function"; |
| 4 | import { |
| 5 | createRailwayFunctionSupport, |
| 6 | createRailwayHostedSupport, |
| 7 | } from "@alchemy.run/railway/hosted"; |
| 8 | import { ServiceProvider } from "@alchemy.run/railway/ServiceProvider"; |
| 9 | import { expect } from "@alchemy.run/test"; |
| 10 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 11 | import { Credentials, GqlTransport } from "@distilled.cloud/railway"; |
| 12 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 13 | import * as Bundle from "alchemy/Bundle/Bundle"; |
| 14 | import * as Provider from "alchemy/Provider"; |
| 15 | import { Stack } from "alchemy/Stack"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as FileSystem from "effect/FileSystem"; |
| 18 | import * as HttpClient from "effect/http/HttpClient"; |
| 19 | import * as Layer from "effect/Layer"; |
| 20 | import * as Path from "effect/Path"; |
| 21 | |
| 22 | const { test } = Test.make({ |
| 23 | providers: Layer.mergeAll(ServiceProvider(), FunctionProvider()).pipe( |
| 24 | Layer.provide( |
| 25 | Layer.mergeAll( |
| 26 | Layer.succeed(Credentials, Effect.die("Offline diff must not resolve credentials")), |
| 27 | Layer.succeed(GqlTransport, { |
| 28 | execute: () => Effect.die("Offline diff must not issue GraphQL requests"), |
| 29 | }), |
| 30 | Layer.succeed(RailwayEnvironment, Effect.die("Offline diff must not resolve environment")), |
| 31 | Layer.succeed( |
| 32 | HttpClient.HttpClient, |
| 33 | HttpClient.make(() => Effect.die("Offline diff must not make HTTP requests")), |
| 34 | ), |
| 35 | ), |
| 36 | ), |
| 37 | ), |
| 38 | }); |
| 39 | |
| 40 | for (const kind of ["Service", "Function"] as const) { |
| 41 | test.provider( |
| 42 | `${kind} detects code edits with runtime exports`, |
| 43 | () => |
| 44 | Effect.gen(function* () { |
| 45 | const fs = yield* FileSystem.FileSystem; |
| 46 | const path = yield* Path.Path; |
| 47 | const stack = yield* Stack; |
| 48 | const { dotAlchemy } = yield* AlchemyContext; |
| 49 | yield* fs.makeDirectory(path.resolve(".tmp"), { recursive: true }); |
| 50 | const directory = yield* fs.makeTempDirectoryScoped({ |
| 51 | directory: path.resolve(".tmp"), |
| 52 | prefix: "railway-code-diff-", |
| 53 | }); |
| 54 | const main = path.join(directory, "main.ts"); |
| 55 | const source = (value: string) => |
| 56 | `export default { fetch: () => new Response(${JSON.stringify(value)}) };`; |
| 57 | yield* fs.writeFileString(main, source("before")); |
| 58 | const options = { |
| 59 | stackName: stack.name, |
| 60 | stage: stack.stage, |
| 61 | dotAlchemy, |
| 62 | virtualEntryPlugin: yield* Bundle.virtualEntryPlugin, |
| 63 | }; |
| 64 | const hosted = |
| 65 | kind === "Service" |
| 66 | ? createRailwayHostedSupport(options) |
| 67 | : createRailwayFunctionSupport(options); |
| 68 | const props = { |
| 69 | // Diff receives plain persisted attributes, although its public Props |
| 70 | // type still describes the Resource with unresolved Output fields. |
| 71 | project: { |
| 72 | projectId: "project", |
| 73 | name: "project", |
| 74 | workspaceId: "workspace", |
| 75 | environmentId: "environment", |
| 76 | url: "https://railway.com/project/project", |
| 77 | } satisfies Railway.Project["Attributes"] as unknown as Railway.Project, |
| 78 | environment: { environmentId: "environment" }, |
| 79 | main, |
| 80 | isExternal: true, |
| 81 | port: 3000, |
| 82 | // Platform supplies runtime exports as Effects during planning. |
| 83 | exports: { fetch: Effect.succeed("runtime handler") }, |
| 84 | }; |
| 85 | const originalHash = yield* hosted.hash(props); |
| 86 | const provider = |
| 87 | kind === "Service" |
| 88 | ? yield* Provider.findProvider(Railway.Service) |
| 89 | : yield* Provider.findProvider(Railway.Function); |
| 90 | const diff = (hash: string) => |
| 91 | provider.diff!({ |
| 92 | id: "Api", |
| 93 | fqn: "Api", |
| 94 | instanceId: "code-diff", |
| 95 | olds: props, |
| 96 | news: props, |
| 97 | oldBindings: [], |
| 98 | newBindings: [], |
| 99 | // These are the persisted attributes consulted by both diff methods. |
| 100 | output: { |
| 101 | projectId: "project", |
| 102 | environmentId: "environment", |
| 103 | code: { hash }, |
| 104 | } as Railway.Service["Attributes"] & Railway.Function["Attributes"], |
| 105 | }); |
| 106 | expect(yield* diff(originalHash)).toBeUndefined(); |
| 107 | yield* fs.writeFileString(main, source("after")); |
| 108 | expect(yield* diff(originalHash)).toEqual({ action: "update" }); |
| 109 | const updatedHash = yield* hosted.hash(props); |
| 110 | expect(updatedHash).not.toBe(originalHash); |
| 111 | expect(yield* diff(updatedHash)).toBeUndefined(); |
| 112 | }), |
| 113 | { |
| 114 | tags: [ |
| 115 | "unit", |
| 116 | "provider:railway", |
| 117 | "provider:railway:function", |
| 118 | "provider:railway:service", |
| 119 | "local", |
| 120 | ], |
| 121 | timeout: 30_000, |
| 122 | }, |
| 123 | ); |
| 124 | } |