| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as essentialcontacts from "@distilled.cloud/gcp/essentialcontacts_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | essentialcontacts.getProjectsContacts({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsContacts on a missing contact fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const error = yield* Effect.flip( |
| 36 | essentialcontacts.getProjectsContacts({ |
| 37 | name: `projects/${project}/contacts/0`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toEqual("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { |
| 45 | tags: ["provider:gcp", "provider:gcp:essentialcontacts", "live"], |
| 46 | timeout: 90_000, |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "create, update, replace, and delete a project essential contact", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | const { project } = yield* GcpEnvironment.current; |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | const created = yield* stack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | return yield* GCP.EssentialContacts.Contact("Ops", { |
| 60 | email: "ops@example.com", |
| 61 | languageTag: "en-US", |
| 62 | notificationCategorySubscriptions: ["ALL"], |
| 63 | }); |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | expect(created.contactId.length).toBeGreaterThan(0); |
| 68 | expect(created.name).toContain("/contacts/"); |
| 69 | expect(created.email).toEqual("ops@example.com"); |
| 70 | expect(created.languageTag).toEqual("en-US"); |
| 71 | expect(created.notificationCategorySubscriptions).toEqual(["ALL"]); |
| 72 | expect(created.project).toEqual(project); |
| 73 | |
| 74 | const fetched = yield* essentialcontacts.getProjectsContacts({ |
| 75 | name: created.name, |
| 76 | }); |
| 77 | expect(fetched.name).toEqual(created.name); |
| 78 | expect(fetched.email).toContain("+alc."); |
| 79 | expect(fetched.email).toContain("ops@"); |
| 80 | expect(fetched.languageTag).toEqual("en-US"); |
| 81 | |
| 82 | const updated = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | return yield* GCP.EssentialContacts.Contact("Ops", { |
| 85 | email: "ops@example.com", |
| 86 | languageTag: "en-GB", |
| 87 | notificationCategorySubscriptions: ["SECURITY", "TECHNICAL"], |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(updated.name).toEqual(created.name); |
| 93 | expect(updated.email).toEqual("ops@example.com"); |
| 94 | expect(updated.languageTag).toEqual("en-GB"); |
| 95 | expect(updated.notificationCategorySubscriptions).toEqual([ |
| 96 | "SECURITY", |
| 97 | "TECHNICAL", |
| 98 | ]); |
| 99 | |
| 100 | const replaced = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | return yield* GCP.EssentialContacts.Contact("Ops", { |
| 103 | email: "security@example.com", |
| 104 | languageTag: "en-GB", |
| 105 | notificationCategorySubscriptions: ["SECURITY"], |
| 106 | }); |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | expect(replaced.email).toEqual("security@example.com"); |
| 111 | expect(replaced.contactId.length).toBeGreaterThan(0); |
| 112 | expect(replaced.languageTag).toEqual("en-GB"); |
| 113 | expect(replaced.notificationCategorySubscriptions).toEqual(["SECURITY"]); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | |
| 117 | const gone = yield* waitUntilGone(replaced.name); |
| 118 | expect(gone).toEqual("gone"); |
| 119 | }).pipe(logLevel), |
| 120 | { |
| 121 | tags: ["provider:gcp", "provider:gcp:essentialcontacts", "live"], |
| 122 | timeout: 90_000, |
| 123 | }, |
| 124 | ); |