The best way to control Claude Code, Codex and more from any computer or phone

main
ts 121 lines 4.34 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 logging from "@distilled.cloud/gcp/logging_v2";
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 = (name: string) =>
15 logging.getFoldersLocationsBuckets({ name }).pipe(
16 Effect.map((bucket) =>
17 bucket.lifecycleState === "DELETE_REQUESTED" ? ("gone" as const) : ("found" as const),
18 ),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27 test.provider(
28 "create, update, replace, and delete a folder logging bucket",
29 (stack) =>
30 Effect.gen(function* () {
31 const { project } = yield* GcpEnvironment.current;
32
33 yield* stack.destroy();
34
35 const created = yield* stack.deploy(
36 Effect.gen(function* () {
37 return yield* GCP.Logging.FolderBucket("AppLogs", {
38 description: "application logs",
39 retentionDays: 31,
40 });
41 }),
42 );
43
44 expect(created.bucketId).toEqual(expect.any(String));
45 expect(created.location).toEqual("global");
46 expect(created.parent).toEqual(`projects/${project}`);
47 expect(created.name).toEqual(
48 `projects/${project}/locations/global/buckets/${created.bucketId}`,
49 );
50 expect(created.description).toEqual("application logs");
51 expect(created.retentionDays).toEqual(31);
52 expect(created.locked).toEqual(false);
53 expect(created.lifecycleState).toEqual("ACTIVE");
54
55 const fetched = yield* logging.getFoldersLocationsBuckets({
56 name: created.name,
57 });
58 expect(fetched.name).toEqual(created.name);
59 expect(fetched.retentionDays).toEqual(31);
60 expect(fetched.description).toContain("alchemy-id=");
61 expect(fetched.description).toContain("application logs");
62 expect(fetched.lifecycleState).toEqual("ACTIVE");
63
64 const updated = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.Logging.FolderBucket("AppLogs", {
67 bucketId: created.bucketId,
68 location: created.location,
69 description: "retained application logs",
70 retentionDays: 60,
71 });
72 }),
73 );
74
75 expect(updated.name).toEqual(created.name);
76 expect(updated.bucketId).toEqual(created.bucketId);
77 expect(updated.description).toEqual("retained application logs");
78 expect(updated.retentionDays).toEqual(60);
79 expect(updated.createTime).toEqual(created.createTime);
80
81 const fetchedUpdate = yield* logging.getFoldersLocationsBuckets({
82 name: created.name,
83 });
84 expect(fetchedUpdate.retentionDays).toEqual(60);
85 expect(fetchedUpdate.description).toContain("retained application logs");
86
87 const last = created.bucketId.at(-1) ?? "a";
88 const nextBucketId = `${created.bucketId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
89
90 const replaced = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* GCP.Logging.FolderBucket("AppLogs", {
93 bucketId: nextBucketId,
94 location: "global",
95 description: "replaced bucket",
96 retentionDays: 31,
97 });
98 }),
99 );
100
101 expect(replaced.bucketId).not.toEqual(created.bucketId);
102 expect(replaced.name).toEqual(
103 `projects/${project}/locations/global/buckets/${replaced.bucketId}`,
104 );
105 expect(replaced.description).toEqual("replaced bucket");
106
107 const fetchedReplacement = yield* logging.getFoldersLocationsBuckets({
108 name: replaced.name,
109 });
110 expect(fetchedReplacement.name).toEqual(replaced.name);
111
112 const previousGone = yield* waitUntilGone(created.name);
113 expect(previousGone).toEqual("gone");
114
115 yield* stack.destroy();
116
117 const gone = yield* waitUntilGone(replaced.name);
118 expect(gone).toEqual("gone");
119 }).pipe(logLevel),
120 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
121 );