The best way to control Claude Code, Codex and more from any computer or phone

main
ts 160 lines 5.64 KB
Raw
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 Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { suitePartition } from "../suiteProject.ts";
10
11 const { test } = Test.make({ providers: Railway.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const origin = Effect.gen(function* () {
16 const { project, environment } = yield* suitePartition;
17 // A public domain must exist before Railway can enable CDN caching.
18 const service = yield* Railway.Service("Origin", {
19 project,
20 environment,
21 image: "nginx:alpine",
22 port: 80,
23 healthcheckPath: "/",
24 });
25 return { project, environment, service };
26 });
27
28 const readConfig = Query.fn((serviceId: string, environmentId: string) => ({
29 edgeConfig: RailwayApi.serviceInstance({
30 serviceId,
31 environmentId,
32 }).edgeConfig.pipe(
33 Query.map((config) => ({
34 id: config.id,
35 enabled: config.enabled,
36 caching: config.caching.pipe(
37 Query.map((caching) => ({
38 mode: caching.mode,
39 htmlCaching: caching.htmlCaching,
40 purgeOnDeploy: caching.purgeOnDeploy,
41 defaultTtlSeconds: caching.defaultTtlSeconds,
42 })),
43 ),
44 })),
45 ),
46 }));
47
48 const readCachingState = Query.fn((serviceId: string, environmentId: string) => {
49 const instance = RailwayApi.serviceInstance({ serviceId, environmentId });
50 return {
51 serviceId: instance.serviceId,
52 edgeConfig: instance.edgeConfig.pipe(
53 Query.map((config) => ({
54 enabled: config.enabled,
55 caching: config.caching.pipe(Query.map((caching) => ({ mode: caching.mode }))),
56 })),
57 ),
58 };
59 });
60
61 test.provider(
62 "enable, update, and disable CDN while retaining its origin",
63 (stack) =>
64 Effect.gen(function* () {
65 yield* stack.destroy();
66
67 const created = yield* stack.deploy(
68 Effect.gen(function* () {
69 const resources = yield* origin;
70 const cdn = yield* Railway.Website.Cdn("Cache", {
71 service: resources.service,
72 environment: resources.environment,
73 htmlCaching: "AUTO",
74 purgeOnDeploy: "HTML",
75 defaultTtlSeconds: 60,
76 });
77 return { ...resources, cdn };
78 }),
79 );
80
81 expect(created.service.domain).toEqual(expect.any(String));
82 expect(created.cdn.enabled).toEqual(true);
83 const initial = yield* readConfig(
84 created.service.serviceId,
85 created.environment.environmentId,
86 );
87 expect(initial.edgeConfig?.id).toEqual(created.cdn.edgeConfigId);
88 expect(initial.edgeConfig?.enabled).toEqual(true);
89 expect(initial.edgeConfig?.caching?.mode.toLowerCase()).not.toEqual("off");
90 expect(initial.edgeConfig?.caching?.htmlCaching).toEqual("auto");
91 expect(initial.edgeConfig?.caching?.purgeOnDeploy).toEqual("HTML");
92 expect(initial.edgeConfig?.caching?.defaultTtlSeconds).toEqual(60);
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 const resources = yield* origin;
97 const cdn = yield* Railway.Website.Cdn("Cache", {
98 service: resources.service,
99 environment: resources.environment,
100 htmlCaching: "FORCE",
101 purgeOnDeploy: "ALL",
102 defaultTtlSeconds: 120,
103 });
104 return { ...resources, cdn };
105 }),
106 );
107
108 expect(updated.service.serviceId).toEqual(created.service.serviceId);
109 expect(updated.cdn.edgeConfigId).toEqual(created.cdn.edgeConfigId);
110 expect(updated.cdn.enabled).toEqual(true);
111 const changed = yield* readConfig(
112 updated.service.serviceId,
113 updated.environment.environmentId,
114 );
115 expect(changed.edgeConfig?.enabled).toEqual(true);
116 expect(changed.edgeConfig?.caching?.mode.toLowerCase()).not.toEqual("off");
117 expect(changed.edgeConfig?.caching?.htmlCaching).toEqual("force");
118 expect(changed.edgeConfig?.caching?.purgeOnDeploy).toEqual("ALL");
119 expect(changed.edgeConfig?.caching?.defaultTtlSeconds).toEqual(120);
120
121 // Keep both dependencies deployed so this step tests CDN deletion alone.
122 const retained = yield* stack.deploy(origin);
123 expect(retained.service.serviceId).toEqual(created.service.serviceId);
124 expect(retained.environment.environmentId).toEqual(created.environment.environmentId);
125
126 const disabled = yield* readCachingState(
127 retained.service.serviceId,
128 retained.environment.environmentId,
129 ).pipe(
130 Effect.map((instance) => ({
131 serviceId: instance.serviceId,
132 // Edge routing can stay enabled after CDN caching is switched off.
133 cachingEnabled:
134 instance.edgeConfig?.enabled === true &&
135 instance.edgeConfig.caching != null &&
136 instance.edgeConfig.caching.mode.toLowerCase() !== "off",
137 })),
138 Effect.repeat({
139 schedule: Schedule.spaced("1 second"),
140 until: (observed) => !observed.cachingEnabled,
141 times: 10,
142 }),
143 );
144 expect(disabled.serviceId).toEqual(retained.service.serviceId);
145 expect(disabled.cachingEnabled).toEqual(false);
146
147 yield* stack.destroy();
148 }).pipe(logLevel),
149 {
150 tags: [
151 "provider:railway",
152 "provider:railway:project",
153 "provider:railway:projectenvironment",
154 "provider:railway:service",
155 "provider:railway:website",
156 "live",
157 ],
158 timeout: 120_000,
159 },
160 );