The best way to control Claude Code, Codex and more from any computer or phone
| 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 monitoring from "@distilled.cloud/gcp/monitoring_v3"; |
| 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 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 12 | |
| 13 | const waitUntilGone = (name: string) => |
| 14 | monitoring.getProjectsNotificationChannels({ name }).pipe( |
| 15 | Effect.as("found" as const), |
| 16 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 17 | Effect.repeat({ |
| 18 | schedule: Schedule.spaced("1 second"), |
| 19 | until: (status) => status === "gone", |
| 20 | times: 10, |
| 21 | }), |
| 22 | ); |
| 23 | |
| 24 | test.provider( |
| 25 | "create, update, replace, and delete a notification channel", |
| 26 | (stack) => |
| 27 | Effect.gen(function* () { |
| 28 | yield* stack.destroy(); |
| 29 | |
| 30 | const created = yield* stack.deploy( |
| 31 | Effect.gen(function* () { |
| 32 | return yield* GCP.Monitoring.NotificationChannel("Alerts", { |
| 33 | type: "email", |
| 34 | description: "oncall", |
| 35 | labels: { email_address: "alchemy-test@example.com" }, |
| 36 | userLabels: { env: "test" }, |
| 37 | }); |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | expect(created.name).toContain("/notificationChannels/"); |
| 42 | expect(created.notificationChannelId).toEqual(expect.any(String)); |
| 43 | expect(created.type).toEqual("email"); |
| 44 | expect(created.description).toEqual("oncall"); |
| 45 | expect(created.labels.email_address).toEqual("alchemy-test@example.com"); |
| 46 | expect(created.userLabels).toMatchObject({ env: "test" }); |
| 47 | expect(created.enabled).toEqual(true); |
| 48 | |
| 49 | const fetched = yield* monitoring.getProjectsNotificationChannels({ |
| 50 | name: created.name, |
| 51 | }); |
| 52 | expect(fetched.name).toEqual(created.name); |
| 53 | expect(fetched.type).toEqual("email"); |
| 54 | expect(fetched.labels?.email_address).toEqual("alchemy-test@example.com"); |
| 55 | expect(fetched.userLabels?.env).toEqual("test"); |
| 56 | expect( |
| 57 | Object.keys(fetched.userLabels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 58 | ).toEqual(true); |
| 59 | |
| 60 | const updated = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | return yield* GCP.Monitoring.NotificationChannel("Alerts", { |
| 63 | type: "email", |
| 64 | displayName: created.displayName, |
| 65 | description: "oncall v2", |
| 66 | labels: { email_address: "alchemy-alerts@example.com" }, |
| 67 | userLabels: { env: "prod", role: "alerts" }, |
| 68 | enabled: false, |
| 69 | }); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(updated.name).toEqual(created.name); |
| 74 | expect(updated.description).toEqual("oncall v2"); |
| 75 | expect(updated.labels.email_address).toEqual("alchemy-alerts@example.com"); |
| 76 | expect(updated.userLabels).toMatchObject({ |
| 77 | env: "prod", |
| 78 | role: "alerts", |
| 79 | }); |
| 80 | expect(updated.enabled).toEqual(false); |
| 81 | |
| 82 | const fetchedUpdate = yield* monitoring.getProjectsNotificationChannels({ |
| 83 | name: updated.name, |
| 84 | }); |
| 85 | expect(fetchedUpdate.description).toEqual("oncall v2"); |
| 86 | expect(fetchedUpdate.enabled).toEqual(false); |
| 87 | expect(fetchedUpdate.labels?.email_address).toEqual("alchemy-alerts@example.com"); |
| 88 | |
| 89 | const replaced = yield* stack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | return yield* GCP.Monitoring.NotificationChannel("Alerts", { |
| 92 | type: "webhook_tokenauth", |
| 93 | displayName: created.displayName, |
| 94 | labels: { url: "https://example.com/hooks/alerts" }, |
| 95 | userLabels: { env: "prod" }, |
| 96 | }); |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | expect(replaced.type).toEqual("webhook_tokenauth"); |
| 101 | expect(replaced.labels.url).toEqual("https://example.com/hooks/alerts"); |
| 102 | expect(replaced.name).not.toEqual(created.name); |
| 103 | |
| 104 | const oldGone = yield* waitUntilGone(created.name); |
| 105 | expect(oldGone).toEqual("gone"); |
| 106 | |
| 107 | const fetchedReplace = yield* monitoring.getProjectsNotificationChannels({ |
| 108 | name: replaced.name, |
| 109 | }); |
| 110 | expect(fetchedReplace.type).toEqual("webhook_tokenauth"); |
| 111 | expect(fetchedReplace.labels?.url).toEqual("https://example.com/hooks/alerts"); |
| 112 | |
| 113 | yield* stack.destroy(); |
| 114 | |
| 115 | const gone = yield* waitUntilGone(replaced.name); |
| 116 | expect(gone).toEqual("gone"); |
| 117 | }).pipe(logLevel), |
| 118 | { |
| 119 | tags: ["provider:gcp", "provider:gcp:monitoring", "live"], |
| 120 | timeout: 90_000, |
| 121 | }, |
| 122 | ); |