@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 132 lines 5.05 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 waitUntilGone = (forwardingRuleName: string) =>
15 GcpEnvironment.current.pipe(
16 Effect.flatMap(({ project }) =>
17 compute
18 .getGlobalForwardingRules({
19 project,
20 forwardingRule: forwardingRuleName,
21 })
22 .pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("1 second"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 ),
31 ),
32 );
33
34 const resourceTail = (value: string | undefined): string => {
35 if (value === undefined || value.length === 0) return "";
36 const parts = value.split("/").filter((part) => part.length > 0);
37 return (parts[parts.length - 1] ?? "").toLowerCase();
38 };
39
40 const httpsRedirect = (host: string) => ({
41 httpsRedirect: true,
42 hostRedirect: host,
43 stripQuery: false,
44 });
45
46 test.provider(
47 "create, update, and delete a global forwarding rule",
48 (stack) =>
49 Effect.gen(function* () {
50 yield* stack.destroy();
51
52 const created = yield* stack.deploy(
53 Effect.gen(function* () {
54 const map = yield* GCP.Compute.UrlMap("Web", {
55 description: "https redirect",
56 defaultUrlRedirect: httpsRedirect("example.com"),
57 });
58 const proxy = yield* GCP.Compute.TargetHttpProxy("Proxy", {
59 description: "http frontend",
60 urlMap: map.urlMapName,
61 });
62 return yield* GCP.Compute.GlobalForwardingRule("Http", {
63 description: "alchemy test global forwarding rule",
64 target: proxy.selfLink.as<string>(),
65 portRange: "80",
66 loadBalancingScheme: "EXTERNAL",
67 labels: { env: "test" },
68 });
69 }),
70 );
71
72 expect(created.forwardingRuleName).toEqual(expect.any(String));
73 expect(created.ipAddress).toEqual(expect.any(String));
74 expect(created.ipProtocol).toEqual("TCP");
75 expect(created.loadBalancingScheme).toEqual("EXTERNAL");
76 expect(created.labels).toMatchObject({ env: "test" });
77 expect(created.description).toEqual("alchemy test global forwarding rule");
78 expect(resourceTail(created.target).length).toBeGreaterThan(0);
79
80 const fetched = yield* compute.getGlobalForwardingRules({
81 project: created.project,
82 forwardingRule: created.forwardingRuleName,
83 });
84 expect(fetched.name).toEqual(created.forwardingRuleName);
85 expect(fetched.IPAddress).toEqual(created.ipAddress);
86 expect(fetched.description).toContain("alchemy-id=");
87 expect(fetched.description).toContain("alchemy test global forwarding rule");
88 expect(fetched.labels?.env).toEqual("test");
89 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
90 expect(resourceTail(fetched.target)).toEqual(resourceTail(created.target));
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 const map = yield* GCP.Compute.UrlMap("Web", {
95 description: "https redirect",
96 defaultUrlRedirect: httpsRedirect("example.com"),
97 });
98 const proxy = yield* GCP.Compute.TargetHttpProxy("Proxy", {
99 description: "http frontend",
100 urlMap: map.urlMapName,
101 });
102 return yield* GCP.Compute.GlobalForwardingRule("Http", {
103 forwardingRuleName: created.forwardingRuleName,
104 description: "alchemy test global forwarding rule",
105 target: proxy.selfLink.as<string>(),
106 portRange: "80",
107 loadBalancingScheme: "EXTERNAL",
108 labels: { env: "prod", role: "edge" },
109 });
110 }),
111 );
112
113 expect(updated.forwardingRuleName).toEqual(created.forwardingRuleName);
114 expect(updated.ipAddress).toEqual(created.ipAddress);
115 expect(updated.labels).toMatchObject({ env: "prod", role: "edge" });
116 expect(resourceTail(updated.target)).toEqual(resourceTail(created.target));
117
118 const refetched = yield* compute.getGlobalForwardingRules({
119 project: updated.project,
120 forwardingRule: updated.forwardingRuleName,
121 });
122 expect(refetched.labels?.env).toEqual("prod");
123 expect(refetched.labels?.role).toEqual("edge");
124 expect(resourceTail(refetched.target)).toEqual(resourceTail(updated.target));
125
126 yield* stack.destroy();
127
128 const gone = yield* waitUntilGone(created.forwardingRuleName);
129 expect(gone).toEqual("gone");
130 }).pipe(logLevel),
131 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 },
132 );