@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 152 lines 5.62 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 RailwaySdk } from "@distilled.cloud/railway";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import * as Redacted from "effect/Redacted";
9 import { MinimumLogLevel } from "effect/References";
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 const VALUE_A = Redacted.make("alchemy-railway-var-a");
18 const VALUE_B = Redacted.make("alchemy-railway-var-b");
19
20 const asVariableMap = (value: unknown): Record<string, string> => {
21 if (value === null || typeof value !== "object" || Array.isArray(value)) {
22 return {};
23 }
24 const out: Record<string, string> = {};
25 for (const [key, item] of Object.entries(value)) {
26 if (typeof item === "string") {
27 out[key] = item;
28 }
29 }
30 return out;
31 };
32
33 const queryVariables = Query.fn((projectId: string, environmentId: string, serviceId?: string) =>
34 RailwaySdk.variables({
35 projectId,
36 environmentId,
37 ...(serviceId !== undefined ? { serviceId } : {}),
38 unrendered: true,
39 }),
40 );
41
42 const readVariables = (projectId: string, environmentId: string, serviceId?: string) =>
43 queryVariables(projectId, environmentId, serviceId).pipe(
44 Effect.map(asVariableMap),
45 Effect.catchTag("RailwayNotFound", () => Effect.succeed({} as Record<string, string>)),
46 );
47
48 const waitUntilVariableGone = (projectId: string, environmentId: string, name: string) =>
49 readVariables(projectId, environmentId).pipe(
50 Effect.map((vars) => (Object.hasOwn(vars, name) ? ("found" as const) : ("gone" as const))),
51 Effect.repeat({
52 schedule: Schedule.spaced("1 second"),
53 until: (status) => status === "gone",
54 times: 10,
55 }),
56 );
57
58 const matchesPlain = (observed: string | undefined, expected: string) => observed === expected;
59
60 test.provider(
61 "create, update, list, and delete a variable",
62 (stack) =>
63 Effect.gen(function* () {
64 yield* stack.destroy();
65
66 const created = yield* stack.deploy(
67 Effect.gen(function* () {
68 const { project, environment } = yield* suitePartition;
69 const variable = yield* Railway.Variable("DbUrl", {
70 project,
71 environment,
72 value: VALUE_A,
73 });
74 return { project, environment, variable };
75 }),
76 );
77
78 expect(created.variable.projectId).toEqual(created.project.projectId);
79 expect(created.variable.environmentId).toEqual(created.environment.environmentId);
80 expect(created.variable.serviceId).toBeUndefined();
81 expect(created.variable.name).toEqual(expect.any(String));
82 expect(created.variable.name.length).toBeGreaterThan(0);
83 expect(created.variable.digest).toEqual(expect.any(String));
84 expect(created.variable.digest.length).toBeGreaterThan(0);
85 expect(Object.hasOwn(created.variable, "value")).toBe(false);
86
87 const fetched = yield* readVariables(
88 created.variable.projectId,
89 created.variable.environmentId,
90 );
91 expect(Object.hasOwn(fetched, created.variable.name)).toBe(true);
92 expect(matchesPlain(fetched[created.variable.name], Redacted.value(VALUE_A))).toBe(true);
93
94 const provider = yield* Provider.findProvider(Railway.Variable);
95 const listed = yield* provider.list();
96 const found = listed.find(
97 (variable) =>
98 variable.projectId === created.variable.projectId &&
99 variable.environmentId === created.variable.environmentId &&
100 variable.name === created.variable.name,
101 );
102 expect(found).toBeDefined();
103 expect(found?.digest).toEqual(created.variable.digest);
104 expect(found !== undefined && Object.hasOwn(found, "value")).toBe(false);
105
106 const updated = yield* stack.deploy(
107 Effect.gen(function* () {
108 const { project, environment } = yield* suitePartition;
109 const variable = yield* Railway.Variable("DbUrl", {
110 project,
111 environment,
112 value: VALUE_B,
113 });
114 return { project, environment, variable };
115 }),
116 );
117
118 expect(updated.variable.projectId).toEqual(created.variable.projectId);
119 expect(updated.variable.environmentId).toEqual(created.variable.environmentId);
120 expect(updated.variable.name).toEqual(created.variable.name);
121 expect(updated.variable.digest).toEqual(expect.any(String));
122 expect(updated.variable.digest).not.toEqual(created.variable.digest);
123 expect(Object.hasOwn(updated.variable, "value")).toBe(false);
124
125 const refetched = yield* readVariables(
126 updated.variable.projectId,
127 updated.variable.environmentId,
128 );
129 expect(Object.hasOwn(refetched, updated.variable.name)).toBe(true);
130 expect(matchesPlain(refetched[updated.variable.name], Redacted.value(VALUE_B))).toBe(true);
131 expect(matchesPlain(refetched[updated.variable.name], Redacted.value(VALUE_A))).toBe(false);
132
133 yield* stack.destroy();
134
135 const variableGone = yield* waitUntilVariableGone(
136 created.variable.projectId,
137 created.variable.environmentId,
138 created.variable.name,
139 );
140 expect(variableGone).toEqual("gone");
141 }).pipe(logLevel),
142 {
143 tags: [
144 "provider:railway",
145 "provider:railway:project",
146 "provider:railway:projectenvironment",
147 "provider:railway:variable",
148 "live",
149 ],
150 timeout: 120_000,
151 },
152 );