@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 193 lines 7.33 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as compute from "@distilled.cloud/gcp/compute_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const zone = "us-central1-a";
15
16 const waitUntilGone = (autoscaler: string) =>
17 GcpEnvironment.current.pipe(
18 Effect.flatMap(({ project }) =>
19 compute.getAutoscalers({ project, zone, autoscaler }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 ),
28 ),
29 );
30
31 const templateProps: GCP.Compute.InstanceTemplateProps = {
32 machineType: "e2-micro",
33 disks: [
34 {
35 boot: true,
36 autoDelete: true,
37 sourceImage: "projects/debian-cloud/global/images/family/debian-12",
38 diskSizeGb: 10,
39 },
40 ],
41 networkInterfaces: [{ network: "global/networks/default" }],
42 };
43
44 test.provider.skipIf(!!process.env.FAST)(
45 "create, update, replace, and delete an autoscaler",
46 (stack) =>
47 Effect.gen(function* () {
48 const { project } = yield* GcpEnvironment.current;
49 yield* stack.destroy();
50
51 const created = yield* stack.deploy(
52 Effect.gen(function* () {
53 const template = yield* GCP.Compute.InstanceTemplate("Tpl", templateProps);
54 const mig = yield* GCP.Compute.InstanceGroupManager("Mig", {
55 zone,
56 instanceTemplate: template.templateName.as<string>(),
57 targetSize: 0,
58 });
59 const scaler = yield* GCP.Compute.Autoscaler("Scale", {
60 zone,
61 target: mig.selfLink.as<string>(),
62 description: "test scaler",
63 labels: { env: "test" },
64 autoscalingPolicy: {
65 minNumReplicas: 0,
66 maxNumReplicas: 1,
67 coolDownPeriodSec: 60,
68 mode: "OFF",
69 cpuUtilization: { utilizationTarget: 0.6 },
70 },
71 });
72 return { template, mig, scaler };
73 }),
74 );
75
76 expect(created.scaler.autoscalerName).toEqual(expect.any(String));
77 expect(created.scaler.zone).toEqual(zone);
78 expect(created.scaler.description).toEqual("test scaler");
79 expect(created.scaler.labels).toMatchObject({ env: "test" });
80 expect(created.scaler.autoscalingPolicy?.maxNumReplicas).toEqual(1);
81 expect(created.scaler.autoscalingPolicy?.minNumReplicas).toEqual(0);
82 expect(created.scaler.autoscalingPolicy?.mode).toEqual("OFF");
83
84 const fetched = yield* compute.getAutoscalers({
85 project,
86 zone,
87 autoscaler: created.scaler.autoscalerName,
88 });
89 expect(fetched.name).toEqual(created.scaler.autoscalerName);
90 expect(fetched.description).toContain("[alchemy ");
91 expect(fetched.description).toContain("test scaler");
92 expect(fetched.description).toContain("env=test");
93 expect(fetched.autoscalingPolicy?.maxNumReplicas).toEqual(1);
94 expect(fetched.autoscalingPolicy?.coolDownPeriodSec).toEqual(60);
95 expect(fetched.autoscalingPolicy?.cpuUtilization?.utilizationTarget).toEqual(0.6);
96 expect(fetched.target).toEqual(expect.any(String));
97
98 const updated = yield* stack.deploy(
99 Effect.gen(function* () {
100 const template = yield* GCP.Compute.InstanceTemplate("Tpl", templateProps);
101 const mig = yield* GCP.Compute.InstanceGroupManager("Mig", {
102 zone,
103 instanceTemplate: template.templateName.as<string>(),
104 targetSize: 0,
105 });
106 const scaler = yield* GCP.Compute.Autoscaler("Scale", {
107 autoscalerName: created.scaler.autoscalerName,
108 zone,
109 target: mig.selfLink.as<string>(),
110 description: "updated scaler",
111 labels: { env: "prod", role: "scale" },
112 autoscalingPolicy: {
113 minNumReplicas: 0,
114 maxNumReplicas: 2,
115 coolDownPeriodSec: 90,
116 mode: "OFF",
117 cpuUtilization: { utilizationTarget: 0.5 },
118 },
119 });
120 return { template, mig, scaler };
121 }),
122 );
123
124 expect(updated.scaler.autoscalerName).toEqual(created.scaler.autoscalerName);
125 expect(updated.scaler.autoscalerId).toEqual(created.scaler.autoscalerId);
126 expect(updated.scaler.description).toEqual("updated scaler");
127 expect(updated.scaler.labels).toMatchObject({
128 env: "prod",
129 role: "scale",
130 });
131 expect(updated.scaler.autoscalingPolicy?.maxNumReplicas).toEqual(2);
132 expect(updated.scaler.autoscalingPolicy?.coolDownPeriodSec).toEqual(90);
133
134 const refetched = yield* compute.getAutoscalers({
135 project,
136 zone,
137 autoscaler: updated.scaler.autoscalerName,
138 });
139 expect(refetched.id).toEqual(created.scaler.autoscalerId);
140 expect(refetched.description).toContain("updated scaler");
141 expect(refetched.description).toContain("env=prod");
142 expect(refetched.autoscalingPolicy?.maxNumReplicas).toEqual(2);
143 expect(refetched.autoscalingPolicy?.cpuUtilization?.utilizationTarget).toEqual(0.5);
144
145 const replacedName = `${created.scaler.autoscalerName.replace(/-+$/, "").slice(0, 61)}-r`;
146 const replaced = yield* stack.deploy(
147 Effect.gen(function* () {
148 const template = yield* GCP.Compute.InstanceTemplate("Tpl", templateProps);
149 const mig = yield* GCP.Compute.InstanceGroupManager("Mig", {
150 zone,
151 instanceTemplate: template.templateName.as<string>(),
152 targetSize: 0,
153 });
154 const scaler = yield* GCP.Compute.Autoscaler("Scale", {
155 autoscalerName: replacedName,
156 zone,
157 target: mig.selfLink.as<string>(),
158 description: "replaced scaler",
159 labels: { env: "prod", role: "scale" },
160 autoscalingPolicy: {
161 minNumReplicas: 0,
162 maxNumReplicas: 2,
163 coolDownPeriodSec: 90,
164 mode: "OFF",
165 cpuUtilization: { utilizationTarget: 0.5 },
166 },
167 });
168 return { template, mig, scaler };
169 }),
170 );
171
172 expect(replaced.scaler.autoscalerName).toEqual(replacedName);
173 expect(replaced.scaler.description).toEqual("replaced scaler");
174 expect(replaced.scaler.autoscalerId).not.toEqual(created.scaler.autoscalerId);
175
176 const replacedFetched = yield* compute.getAutoscalers({
177 project,
178 zone,
179 autoscaler: replaced.scaler.autoscalerName,
180 });
181 expect(replacedFetched.name).toEqual(replacedName);
182 expect(replacedFetched.description).toContain("replaced scaler");
183
184 const oldGone = yield* waitUntilGone(created.scaler.autoscalerName);
185 expect(oldGone).toEqual("gone");
186
187 yield* stack.destroy();
188
189 const gone = yield* waitUntilGone(replaced.scaler.autoscalerName);
190 expect(gone).toEqual("gone");
191 }).pipe(logLevel),
192 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 },
193 );