@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 122 lines 4.17 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as logging from "@distilled.cloud/gcp/logging_v2";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 // Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials
12 // administer the organization (the testing service account does not).
13 const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(/^organizations\//, "");
14 const organization = `organizations/${organizationId}`;
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const waitUntilGone = (name: string) =>
19 logging.getOrganizationsSinks({ sinkName: name }).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 test.provider.skipIf(!organizationId)(
30 "getOrganizationsSinks on a missing sink fails with NotFound",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 logging.getOrganizationsSinks({
37 sinkName: `${organization}/sinks/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
45 );
46
47 test.provider.skipIf(!organizationId)(
48 "create, update, replace, and delete an organization logging sink",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const destination = `logging.googleapis.com/${organization}/locations/global/buckets/_Default`;
54
55 const created = yield* stack.deploy(
56 Effect.gen(function* () {
57 return yield* GCP.Logging.OrganizationSink("Errors", {
58 organization,
59 destination,
60 filter: "severity>=ERROR",
61 description: "application errors",
62 });
63 }),
64 );
65
66 expect(created.sinkId).toEqual(expect.any(String));
67 expect(created.organization).toEqual(organization);
68 expect(created.name).toEqual(`${organization}/sinks/${created.sinkId}`);
69 expect(created.destination).toEqual(destination);
70 expect(created.filter).toEqual("severity>=ERROR");
71 expect(created.description).toEqual("application errors");
72
73 const fetched = yield* logging.getOrganizationsSinks({
74 sinkName: created.name,
75 });
76 expect(fetched.destination).toEqual(destination);
77 expect(fetched.description).toContain("alchemy-id=");
78
79 const updated = yield* stack.deploy(
80 Effect.gen(function* () {
81 return yield* GCP.Logging.OrganizationSink("Errors", {
82 organization,
83 sinkId: created.sinkId,
84 destination,
85 filter: "severity>=WARNING",
86 description: "warnings and errors",
87 disabled: true,
88 });
89 }),
90 );
91
92 expect(updated.name).toEqual(created.name);
93 expect(updated.filter).toEqual("severity>=WARNING");
94 expect(updated.disabled).toEqual(true);
95
96 const last = created.sinkId.at(-1) ?? "a";
97 const nextSinkId = `${created.sinkId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
98
99 const replaced = yield* stack.deploy(
100 Effect.gen(function* () {
101 return yield* GCP.Logging.OrganizationSink("Errors", {
102 organization,
103 sinkId: nextSinkId,
104 destination,
105 filter: "severity>=WARNING",
106 description: "replaced sink",
107 });
108 }),
109 );
110
111 expect(replaced.sinkId).not.toEqual(created.sinkId);
112
113 const previousGone = yield* waitUntilGone(created.name);
114 expect(previousGone).toEqual("gone");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(replaced.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
122 );