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 billingbudgets from "@distilled.cloud/gcp/billingbudgets_v1"; |
| 6 | import * as cloudbilling from "@distilled.cloud/gcp/cloudbilling_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const waitUntilGone = (name: string) => |
| 16 | billingbudgets.getBillingAccountsBudgets({ name }).pipe( |
| 17 | Effect.as("found" as const), |
| 18 | Effect.catchTag(["NotFound", "BudgetNotFound"], () => Effect.succeed("gone" as const)), |
| 19 | Effect.repeat({ |
| 20 | schedule: Schedule.spaced("1 second"), |
| 21 | until: (status) => status === "gone", |
| 22 | times: 10, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | const billingAccountId = () => { |
| 27 | const fromEnv = process.env.GOOGLE_BILLING_ACCOUNT; |
| 28 | if (fromEnv && fromEnv.length > 0) { |
| 29 | return Effect.succeed( |
| 30 | fromEnv.startsWith("billingAccounts/") ? fromEnv.slice("billingAccounts/".length) : fromEnv, |
| 31 | ); |
| 32 | } |
| 33 | return GcpEnvironment.current.pipe( |
| 34 | Effect.flatMap(({ project }) => |
| 35 | cloudbilling.getBillingInfoProjects({ name: `projects/${project}` }), |
| 36 | ), |
| 37 | Effect.map((info) => (info.billingAccountName ?? "").split("/").pop() ?? ""), |
| 38 | ); |
| 39 | }; |
| 40 | |
| 41 | test.provider( |
| 42 | "getBillingAccountsBudgets on a missing budget fails with a typed tag", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | const account = yield* billingAccountId(); |
| 48 | const error = yield* Effect.flip( |
| 49 | billingbudgets.getBillingAccountsBudgets({ |
| 50 | name: `billingAccounts/${account}/budgets/00000000-0000-0000-0000-000000000000`, |
| 51 | }), |
| 52 | ); |
| 53 | expect(error._tag).toEqual("BudgetNotFound"); |
| 54 | |
| 55 | yield* stack.destroy(); |
| 56 | }).pipe(logLevel), |
| 57 | { |
| 58 | tags: ["provider:gcp", "provider:gcp:billingbudgets", "live"], |
| 59 | timeout: 90_000, |
| 60 | }, |
| 61 | ); |
| 62 | |
| 63 | test.provider( |
| 64 | "create, update, and delete a billing budget", |
| 65 | (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | const { project } = yield* GcpEnvironment.current; |
| 68 | |
| 69 | yield* stack.destroy(); |
| 70 | |
| 71 | const account = yield* billingAccountId(); |
| 72 | expect(account.length).toBeGreaterThan(0); |
| 73 | |
| 74 | const created = yield* stack.deploy( |
| 75 | Effect.gen(function* () { |
| 76 | return yield* GCP.BillingBudgets.BillingBudget("Spend", { |
| 77 | billingAccountId: account, |
| 78 | displayName: "cap", |
| 79 | amount: { |
| 80 | specifiedAmount: { currencyCode: "USD", units: "10" }, |
| 81 | }, |
| 82 | budgetFilter: { |
| 83 | projects: [`projects/${project}`], |
| 84 | calendarPeriod: "MONTH", |
| 85 | }, |
| 86 | thresholdRules: [{ thresholdPercent: 0.5, spendBasis: "CURRENT_SPEND" }], |
| 87 | }); |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | expect(created.name).toContain(`/budgets/`); |
| 92 | expect(created.budgetId).toEqual(expect.any(String)); |
| 93 | expect(created.billingAccountId).toEqual(account); |
| 94 | expect(created.displayName).toEqual("cap"); |
| 95 | expect(created.amount?.specifiedAmount?.units).toEqual("10"); |
| 96 | expect(created.thresholdRules[0]?.thresholdPercent).toEqual(0.5); |
| 97 | |
| 98 | const fetched = yield* billingbudgets.getBillingAccountsBudgets({ |
| 99 | name: created.name, |
| 100 | }); |
| 101 | expect(fetched.name).toEqual(created.name); |
| 102 | expect(fetched.displayName).toEqual("cap"); |
| 103 | expect(fetched.amount?.specifiedAmount?.units).toEqual("10"); |
| 104 | |
| 105 | const updated = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.BillingBudgets.BillingBudget("Spend", { |
| 108 | billingAccountId: account, |
| 109 | displayName: "cap", |
| 110 | amount: { |
| 111 | specifiedAmount: { currencyCode: "USD", units: "25" }, |
| 112 | }, |
| 113 | budgetFilter: { |
| 114 | projects: [`projects/${project}`], |
| 115 | calendarPeriod: "MONTH", |
| 116 | }, |
| 117 | thresholdRules: [ |
| 118 | { thresholdPercent: 0.5, spendBasis: "CURRENT_SPEND" }, |
| 119 | { thresholdPercent: 0.9, spendBasis: "CURRENT_SPEND" }, |
| 120 | ], |
| 121 | }); |
| 122 | }), |
| 123 | ); |
| 124 | |
| 125 | expect(updated.name).toEqual(created.name); |
| 126 | expect(updated.budgetId).toEqual(created.budgetId); |
| 127 | expect(updated.amount?.specifiedAmount?.units).toEqual("25"); |
| 128 | expect(updated.thresholdRules.length).toEqual(2); |
| 129 | |
| 130 | const fetchedUpdate = yield* billingbudgets.getBillingAccountsBudgets({ |
| 131 | name: created.name, |
| 132 | }); |
| 133 | expect(fetchedUpdate.amount?.specifiedAmount?.units).toEqual("25"); |
| 134 | expect(fetchedUpdate.thresholdRules?.length).toEqual(2); |
| 135 | |
| 136 | yield* stack.destroy(); |
| 137 | |
| 138 | const gone = yield* waitUntilGone(created.name); |
| 139 | expect(gone).toEqual("gone"); |
| 140 | }).pipe(logLevel), |
| 141 | { |
| 142 | tags: ["provider:gcp", "provider:gcp:billingbudgets", "live"], |
| 143 | timeout: 90_000, |
| 144 | }, |
| 145 | ); |