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 { 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 deploymentmanager from "@distilled.cloud/gcp/deploymentmanager_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 = (deployment: string) => |
| 15 | GcpEnvironment.current.pipe( |
| 16 | Effect.flatMap(({ project }) => deploymentmanager.getDeployments({ project, deployment })), |
| 17 | Effect.as("found" as const), |
| 18 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 19 | Effect.repeat({ |
| 20 | schedule: Schedule.spaced("2 seconds"), |
| 21 | until: (status) => status === "gone", |
| 22 | times: 10, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | const target = { |
| 27 | config: { |
| 28 | content: `imports: |
| 29 | - path: topic.jinja |
| 30 | resources: |
| 31 | - name: topic |
| 32 | type: topic.jinja |
| 33 | `, |
| 34 | }, |
| 35 | imports: [ |
| 36 | { |
| 37 | name: "topic.jinja", |
| 38 | content: `resources: |
| 39 | - name: topic |
| 40 | type: pubsub.v1.topic |
| 41 | properties: |
| 42 | topic: {{ env["deployment"] }} |
| 43 | `, |
| 44 | }, |
| 45 | ], |
| 46 | }; |
| 47 | |
| 48 | test.provider( |
| 49 | "getDeployments on a missing deployment fails with a typed tag", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | const { project } = yield* GcpEnvironment.current; |
| 53 | |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const error = yield* Effect.flip( |
| 57 | deploymentmanager.getDeployments({ |
| 58 | project, |
| 59 | deployment: "alchemy-missing-deployment", |
| 60 | }), |
| 61 | ); |
| 62 | expect(error._tag).toEqual("NotFound"); |
| 63 | |
| 64 | yield* stack.destroy(); |
| 65 | }).pipe(logLevel), |
| 66 | { |
| 67 | tags: ["provider:gcp", "provider:gcp:deploymentmanager", "live"], |
| 68 | timeout: 90_000, |
| 69 | }, |
| 70 | ); |
| 71 | |
| 72 | test.provider( |
| 73 | "create, update, and delete a deployment", |
| 74 | (stack) => |
| 75 | Effect.gen(function* () { |
| 76 | const { project } = yield* GcpEnvironment.current; |
| 77 | |
| 78 | yield* stack.destroy(); |
| 79 | |
| 80 | const created = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | return yield* GCP.DeploymentManager.Deployment("App", { |
| 83 | description: "alchemy dm test", |
| 84 | labels: { env: "test" }, |
| 85 | target, |
| 86 | }); |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | expect(created.name).toEqual(expect.any(String)); |
| 91 | expect(created.deploymentId).toEqual(created.name); |
| 92 | expect(created.project).toEqual(project); |
| 93 | expect(created.description).toEqual("alchemy dm test"); |
| 94 | expect(created.labels).toMatchObject({ env: "test" }); |
| 95 | expect(created.preview).toEqual(false); |
| 96 | |
| 97 | const fetched = yield* deploymentmanager.getDeployments({ |
| 98 | project, |
| 99 | deployment: created.name, |
| 100 | }); |
| 101 | expect(fetched.name).toEqual(created.name); |
| 102 | const fetchedLabels = Object.fromEntries( |
| 103 | (fetched.labels ?? []) |
| 104 | .filter( |
| 105 | (entry): entry is { key: string; value: string } => |
| 106 | typeof entry.key === "string" && typeof entry.value === "string", |
| 107 | ) |
| 108 | .map((entry) => [entry.key, entry.value]), |
| 109 | ); |
| 110 | expect(fetchedLabels.env).toEqual("test"); |
| 111 | expect(fetchedLabels["alchemy-id"]).toEqual(expect.any(String)); |
| 112 | |
| 113 | const updated = yield* stack.deploy( |
| 114 | Effect.gen(function* () { |
| 115 | return yield* GCP.DeploymentManager.Deployment("App", { |
| 116 | deploymentId: created.deploymentId, |
| 117 | description: "alchemy dm test updated", |
| 118 | labels: { env: "prod", role: "events" }, |
| 119 | target, |
| 120 | }); |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | expect(updated.name).toEqual(created.name); |
| 125 | expect(updated.deploymentId).toEqual(created.deploymentId); |
| 126 | expect(updated.description).toEqual("alchemy dm test updated"); |
| 127 | expect(updated.labels).toMatchObject({ env: "prod", role: "events" }); |
| 128 | |
| 129 | const fetchedUpdate = yield* deploymentmanager.getDeployments({ |
| 130 | project, |
| 131 | deployment: created.name, |
| 132 | }); |
| 133 | expect(fetchedUpdate.description).toEqual("alchemy dm test updated"); |
| 134 | |
| 135 | yield* stack.destroy(); |
| 136 | |
| 137 | const gone = yield* waitUntilGone(created.name); |
| 138 | expect(gone).toEqual("gone"); |
| 139 | }).pipe(logLevel), |
| 140 | { |
| 141 | tags: ["provider:gcp", "provider:gcp:deploymentmanager", "live"], |
| 142 | timeout: 90_000, |
| 143 | }, |
| 144 | ); |