@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 149 lines 5.52 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 memcache from "@distilled.cloud/gcp/memcache_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 // Memcached instances need Private Service Access on the project's default
15 // network — without it create fails with BadRequest "Google private service
16 // access is not enabled." — and take ~10 minutes to provision. Set
17 // GCP_TEST_PRIVATE_SERVICE_ACCESS=1 on a project with PSA configured.
18 const privateServiceAccess = !!process.env.GCP_TEST_PRIVATE_SERVICE_ACCESS;
19 const runLifecycle = privateServiceAccess && !!process.env.GCP_TEST_SLOW && !process.env.FAST;
20
21 const waitUntilGone = (name: string) =>
22 memcache.getProjectsLocationsInstances({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32 test.provider(
33 "getProjectsLocationsInstances on a missing instance fails with NotFound",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 memcache.getProjectsLocationsInstances({
41 name: `projects/${project}/locations/us-central1/instances/alchemy-memcache-missing`,
42 }),
43 );
44 expect(error._tag).toBe("NotFound");
45
46 const page = yield* memcache.listProjectsLocationsInstances({
47 parent: `projects/${project}/locations/-`,
48 pageSize: 10,
49 });
50 expect((page.instances ?? []).map((instance) => instance.name)).not.toContain(
51 `projects/${project}/locations/us-central1/instances/alchemy-memcache-missing`,
52 );
53
54 yield* stack.destroy();
55 }).pipe(logLevel),
56 { tags: ["provider:gcp", "provider:gcp:memcache", "live"], timeout: 90_000 },
57 );
58
59 test.provider.skipIf(privateServiceAccess)(
60 "createProjectsLocationsInstances without private service access fails with PrivateServiceAccessNotEnabled",
61 (stack) =>
62 Effect.gen(function* () {
63 const { project } = yield* GcpEnvironment.current;
64 yield* stack.destroy();
65 const error = yield* Effect.flip(
66 memcache.createProjectsLocationsInstances({
67 parent: `projects/${project}/locations/us-central1`,
68 instanceId: "alchemy-memcache-psa-probe",
69 body: {
70 nodeCount: 1,
71 nodeConfig: { cpuCount: 1, memorySizeMb: 1024 },
72 },
73 }),
74 );
75 expect(error._tag).toEqual("PrivateServiceAccessNotEnabled");
76 }).pipe(logLevel),
77 { tags: ["provider:gcp", "provider:gcp:memcache", "live"], timeout: 90_000 },
78 );
79
80 test.provider.skipIf(!runLifecycle)(
81 "create, update, and delete a memcache instance",
82 (stack) =>
83 Effect.gen(function* () {
84 yield* stack.destroy();
85
86 const created = yield* stack.deploy(
87 Effect.gen(function* () {
88 return yield* GCP.Memcache.Instance("Cache", {
89 location: "us-central1",
90 nodeCount: 1,
91 nodeConfig: { cpuCount: 1, memorySizeMb: 1024 },
92 displayName: "alchemy-test-memcache",
93 labels: { env: "test" },
94 });
95 }),
96 );
97
98 expect(created.name).toContain("/instances/");
99 expect(created.instanceId).toEqual(expect.any(String));
100 expect(created.location).toEqual("us-central1");
101 expect(created.nodeCount).toEqual(1);
102 expect(created.nodeConfig.cpuCount).toEqual(1);
103 expect(created.nodeConfig.memorySizeMb).toEqual(1024);
104 expect(created.displayName).toEqual("alchemy-test-memcache");
105 expect(created.labels).toMatchObject({ env: "test" });
106 expect(created.state).toEqual("READY");
107
108 const fetched = yield* memcache.getProjectsLocationsInstances({
109 name: created.name,
110 });
111 expect(fetched.name).toEqual(created.name);
112 expect(fetched.nodeCount).toEqual(1);
113 expect(fetched.labels?.env).toEqual("test");
114 expect(fetched.displayName).toEqual("alchemy-test-memcache");
115
116 const updated = yield* stack.deploy(
117 Effect.gen(function* () {
118 return yield* GCP.Memcache.Instance("Cache", {
119 instanceId: created.instanceId,
120 location: "us-central1",
121 nodeCount: 1,
122 nodeConfig: { cpuCount: 1, memorySizeMb: 1024 },
123 displayName: "alchemy-prod-memcache",
124 labels: { env: "prod", role: "cache" },
125 });
126 }),
127 );
128
129 expect(updated.name).toEqual(created.name);
130 expect(updated.displayName).toEqual("alchemy-prod-memcache");
131 expect(updated.labels).toMatchObject({ env: "prod", role: "cache" });
132
133 const refetched = yield* memcache.getProjectsLocationsInstances({
134 name: created.name,
135 });
136 expect(refetched.displayName).toEqual("alchemy-prod-memcache");
137 expect(refetched.labels?.env).toEqual("prod");
138 expect(refetched.labels?.role).toEqual("cache");
139
140 yield* stack.destroy();
141
142 const gone = yield* waitUntilGone(created.name);
143 expect(gone).toEqual("gone");
144 }).pipe(logLevel),
145 {
146 tags: ["provider:gcp", "provider:gcp:memcache", "live"],
147 timeout: 1_800_000,
148 },
149 );