@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 155 lines 5.65 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, type UsageLimitSetInput } from "@distilled.cloud/railway";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Result from "effect/Result";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: Railway.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const SOFT_V1 = 100_000;
17 const SOFT_V2 = 100_001;
18
19 const readCustomerId = Query.fn((workspaceId: string) => {
20 const workspace = RailwayApi.workspace({ workspaceId });
21 return { customer: { id: workspace.customer.id } };
22 });
23
24 const readUsageLimit = Query.fn((workspaceId: string) => {
25 const customer = RailwayApi.workspace({ workspaceId }).customer;
26 return {
27 customer: {
28 id: customer.id,
29 usageLimit: customer.usageLimit.pipe(
30 Query.map((limit) => ({ id: limit.id, softLimit: limit.softLimit })),
31 ),
32 },
33 };
34 });
35
36 const setUsageLimit = Query.fn((input: UsageLimitSetInput) => RailwayApi.usageLimitSet({ input }));
37
38 const removeUsageLimit = Query.fn((customerId: string) =>
39 RailwayApi.usageLimitRemove({ input: { customerId } }),
40 );
41
42 const waitUntilLimitGone = (workspaceId: string, usageLimitId: string) =>
43 readUsageLimit(workspaceId).pipe(
44 Effect.map((workspace) => {
45 const limit = workspace.customer.usageLimit;
46 if (limit == null) return "gone" as const;
47 return limit.id === usageLimitId ? ("found" as const) : ("gone" as const);
48 }),
49 Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)),
50 Effect.repeat({
51 schedule: Schedule.spaced("1 second"),
52 until: (status) => status === "gone",
53 times: 10,
54 }),
55 );
56
57 test.provider(
58 "usage() returns rows for the current workspace",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const rows = yield* Railway.usage({ measurements: ["CPU_USAGE", "MEMORY_USAGE_GB"] });
64 expect(Array.isArray(rows)).toEqual(true);
65 for (const row of rows) {
66 expect(row.measurement).toEqual(expect.any(String));
67 expect(row.value).toEqual(expect.any(Number));
68 }
69
70 const estimated = yield* Railway.estimatedUsage({ measurements: ["CPU_USAGE"] });
71 expect(Array.isArray(estimated)).toEqual(true);
72 for (const row of estimated) {
73 expect(row.measurement).toEqual(expect.any(String));
74 expect(row.estimatedValue).toEqual(expect.any(Number));
75 }
76
77 yield* stack.destroy();
78 }).pipe(logLevel),
79 { tags: ["provider:railway", "provider:railway:usage", "live"], timeout: 120_000 },
80 );
81
82 test.provider(
83 "create, update, list, and delete a usage limit",
84 (stack) =>
85 Effect.gen(function* () {
86 yield* stack.destroy();
87
88 const workspace = yield* Railway.currentWorkspace();
89 const live = yield* readCustomerId(workspace.id);
90 const customerId = live.customer.id;
91 expect(customerId.length).toBeGreaterThan(0);
92
93 const probe = yield* Effect.result(setUsageLimit({ customerId, softLimitDollars: SOFT_V1 }));
94 if (Result.isFailure(probe)) {
95 expect(
96 ["RailwayForbidden", "RailwayPlanLimitExceeded"].includes(probe.failure._tag),
97 ).toEqual(true);
98 yield* stack.destroy();
99 return;
100 }
101
102 // `usageLimitRemove` is idempotent; it declares no not-found error.
103 yield* removeUsageLimit(customerId);
104
105 const created = yield* stack.deploy(
106 Effect.gen(function* () {
107 return yield* Railway.UsageLimit("SpendCap", {
108 workspace: { workspaceId: workspace.id },
109 limit: SOFT_V1,
110 });
111 }),
112 );
113
114 expect(created.usageLimitId).toEqual(expect.any(String));
115 expect(created.usageLimitId.length).toBeGreaterThan(0);
116 expect(created.customerId).toEqual(customerId);
117 expect(created.workspaceId).toEqual(workspace.id);
118 expect(created.softLimitDollars).toEqual(SOFT_V1);
119 expect(created.isOverLimit).toEqual(expect.any(Boolean));
120
121 const fetched = yield* readUsageLimit(created.workspaceId);
122 expect(fetched.customer.id).toEqual(created.customerId);
123 expect(fetched.customer.usageLimit?.id).toEqual(created.usageLimitId);
124 expect(fetched.customer.usageLimit?.softLimit).toEqual(SOFT_V1);
125
126 const provider = yield* Provider.findProvider(Railway.UsageLimit);
127 const listed = yield* provider.list();
128 const found = listed.find((row) => row.usageLimitId === created.usageLimitId);
129 expect(found).toBeDefined();
130 expect(found?.customerId).toEqual(created.customerId);
131 expect(found?.softLimitDollars).toEqual(SOFT_V1);
132
133 const updated = yield* stack.deploy(
134 Effect.gen(function* () {
135 return yield* Railway.UsageLimit("SpendCap", {
136 workspace: { workspaceId: workspace.id },
137 limit: SOFT_V2,
138 });
139 }),
140 );
141
142 expect(updated.customerId).toEqual(created.customerId);
143 expect(updated.workspaceId).toEqual(created.workspaceId);
144 expect(updated.softLimitDollars).toEqual(SOFT_V2);
145
146 const fetchedUpdate = yield* readUsageLimit(updated.workspaceId);
147 expect(fetchedUpdate.customer.usageLimit?.softLimit).toEqual(SOFT_V2);
148
149 yield* stack.destroy();
150
151 const gone = yield* waitUntilLimitGone(created.workspaceId, created.usageLimitId);
152 expect(gone).toEqual("gone");
153 }).pipe(logLevel),
154 { tags: ["provider:railway", "provider:railway:usage", "live"], timeout: 120_000 },
155 );