| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as androidmanagement from "@distilled.cloud/gcp/androidmanagement_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import { enterpriseName, logLevel, runLifecycle } from "./common.ts"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const waitUntilGone = (name: string) => |
| 12 | androidmanagement.getEnterprisesWebApps({ name }).pipe( |
| 13 | Effect.as("found" as const), |
| 14 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 15 | Effect.repeat({ |
| 16 | schedule: Schedule.spaced("1 second"), |
| 17 | until: (status) => status === "gone", |
| 18 | times: 10, |
| 19 | }), |
| 20 | ); |
| 21 | |
| 22 | test.provider.skipIf(runLifecycle)( |
| 23 | "getEnterprisesWebApps without the Android Management scope fails with InsufficientScopes", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* stack.destroy(); |
| 27 | |
| 28 | const error = yield* Effect.flip( |
| 29 | androidmanagement.getEnterprisesWebApps({ |
| 30 | name: "enterprises/alchemy-missing-enterprise/webApps/com.alchemy.missing", |
| 31 | }), |
| 32 | ); |
| 33 | expect(error._tag).toEqual("InsufficientScopes"); |
| 34 | |
| 35 | yield* stack.destroy(); |
| 36 | }).pipe(logLevel), |
| 37 | { |
| 38 | tags: ["provider:gcp", "provider:gcp:androidmanagement", "live"], |
| 39 | timeout: 90_000, |
| 40 | }, |
| 41 | ); |
| 42 | |
| 43 | test.provider.skipIf(runLifecycle)( |
| 44 | "createEnterprisesWebApps without Android Management access fails with a typed entitlement error", |
| 45 | (stack) => |
| 46 | Effect.gen(function* () { |
| 47 | yield* stack.destroy(); |
| 48 | |
| 49 | const error = yield* Effect.flip( |
| 50 | androidmanagement.createEnterprisesWebApps({ |
| 51 | parent: "enterprises/alchemy-missing-enterprise", |
| 52 | body: { |
| 53 | title: "Alchemy Probe", |
| 54 | startUrl: "https://example.com/", |
| 55 | displayMode: "STANDALONE", |
| 56 | }, |
| 57 | }), |
| 58 | ); |
| 59 | expect(error._tag).toEqual("InsufficientScopes"); |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | }).pipe(logLevel), |
| 63 | { |
| 64 | tags: ["provider:gcp", "provider:gcp:androidmanagement", "live"], |
| 65 | timeout: 90_000, |
| 66 | }, |
| 67 | ); |
| 68 | |
| 69 | test.provider.skipIf(!runLifecycle)( |
| 70 | "create, update, and delete a web app", |
| 71 | (stack) => |
| 72 | Effect.gen(function* () { |
| 73 | yield* stack.destroy(); |
| 74 | |
| 75 | const created = yield* stack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | const enterprise = enterpriseName |
| 78 | ? { name: enterpriseName } |
| 79 | : yield* GCP.AndroidManagement.Enterprise("WebHost", { |
| 80 | enterpriseDisplayName: "Web Host", |
| 81 | }); |
| 82 | const app = yield* GCP.AndroidManagement.EnterprisesWebApp("Docs", { |
| 83 | parent: enterprise.name, |
| 84 | startUrl: "https://example.com/alchemy-docs", |
| 85 | title: "Docs", |
| 86 | }); |
| 87 | return { enterprise, app }; |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | expect(created.app.name).toContain("/webApps/"); |
| 92 | expect(created.app.parent).toEqual(created.enterprise.name); |
| 93 | expect(created.app.title).toEqual("Docs"); |
| 94 | expect(created.app.startUrl).toEqual("https://example.com/alchemy-docs"); |
| 95 | |
| 96 | const fetched = yield* androidmanagement.getEnterprisesWebApps({ |
| 97 | name: created.app.name, |
| 98 | }); |
| 99 | expect(fetched.name).toEqual(created.app.name); |
| 100 | expect(fetched.title).toEqual("Docs"); |
| 101 | |
| 102 | const updated = yield* stack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | const enterprise = enterpriseName |
| 105 | ? { name: enterpriseName } |
| 106 | : yield* GCP.AndroidManagement.Enterprise("WebHost", { |
| 107 | enterpriseDisplayName: "Web Host", |
| 108 | }); |
| 109 | return yield* GCP.AndroidManagement.EnterprisesWebApp("Docs", { |
| 110 | parent: enterprise.name, |
| 111 | startUrl: "https://example.com/alchemy-docs", |
| 112 | title: "Internal docs", |
| 113 | displayMode: "MINIMAL_UI", |
| 114 | }); |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | expect(updated.name).toEqual(created.app.name); |
| 119 | expect(updated.title).toEqual("Internal docs"); |
| 120 | expect(updated.displayMode).toEqual("MINIMAL_UI"); |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | |
| 124 | const gone = yield* waitUntilGone(created.app.name); |
| 125 | expect(gone).toEqual("gone"); |
| 126 | }).pipe(logLevel), |
| 127 | { |
| 128 | tags: ["provider:gcp", "provider:gcp:androidmanagement", "live"], |
| 129 | timeout: 90_000, |
| 130 | }, |
| 131 | ); |