1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as appengine from "@distilled.cloud/gcp/appengine_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { CERT_A, CERT_B, KEY_A, KEY_B } from "./fixtures/cert.ts";
9import { GcpEnvironment } from "@/GCP/Environment";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18// Needs an App Engine application in the project, which is permanent once
19// created (it can never be deleted). The testing project has none and the
20// App Engine Admin API is off (ServiceDisabled: "App Engine Admin API has not been
21// used in project ... or it is disabled."). Set GCP_TEST_APPENGINE_APP=1 on a
22// project with an app to run the lifecycle.
23const runLifecycle = !!process.env.GCP_TEST_APPENGINE_APP;
24
25const waitUntilGone = (appsId: string, certificateId: string) =>
26 appengine
27 .getAppsAuthorizedCertificates({
28 appsId,
29 authorizedCertificatesId: certificateId,
30 })
31 .pipe(
32 Effect.as("found" as const),
33 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
34 Effect.repeat({
35 schedule: Schedule.spaced("1 second"),
36 until: (status) => status === "gone",
37 times: 10,
38 }),
39 );
40
41test.provider(
42 "getAppsAuthorizedCertificates on a missing certificate fails with a typed tag",
43 (stack) =>
44 Effect.gen(function* () {
45 const { project } = yield* GcpEnvironment.current;
46 yield* stack.destroy();
47
48 const error = yield* Effect.flip(
49 appengine.getAppsAuthorizedCertificates({
50 appsId: project,
51 authorizedCertificatesId: "missing",
52 }),
53 );
54 expect(error._tag).toEqual("ServiceDisabled");
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
59);
60
61test.provider.skipIf(runLifecycle)(
62 "createAppsAuthorizedCertificates without an App Engine app fails with a typed tag",
63 (stack) =>
64 Effect.gen(function* () {
65 const { project } = yield* GcpEnvironment.current;
66 yield* stack.destroy();
67
68 const error = yield* Effect.flip(
69 appengine.createAppsAuthorizedCertificates({
70 appsId: project,
71 body: {
72 displayName: "Alchemy Appengine Probe",
73 certificateRawData: {
74 publicCertificate: CERT_A,
75 privateKey: KEY_A,
76 },
77 },
78 }),
79 );
80 expect(error._tag).toEqual("ServiceDisabled");
81
82 yield* stack.destroy();
83 }).pipe(logLevel),
84 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
85);
86
87test.provider.skipIf(!runLifecycle)(
88 "create, update, and delete an authorized certificate",
89 (stack) =>
90 Effect.gen(function* () {
91 const { project } = yield* GcpEnvironment.current;
92 yield* stack.destroy();
93
94 const created = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.AppEngine.AppsAuthorizedCertificate("FrontendTls", {
97 displayName: "frontend",
98 publicCertificate: CERT_A,
99 privateKey: KEY_A,
100 });
101 }),
102 );
103
104 expect(created.certificateId.length).toBeGreaterThan(0);
105 expect(created.displayName).toEqual("frontend");
106 expect(created.appsId).toEqual(project);
107
108 const fetched = yield* appengine.getAppsAuthorizedCertificates({
109 appsId: created.appsId,
110 authorizedCertificatesId: created.certificateId,
111 });
112 expect(fetched.id).toEqual(created.certificateId);
113 expect(fetched.displayName).toContain("[alchemy ");
114
115 const updated = yield* stack.deploy(
116 Effect.gen(function* () {
117 return yield* GCP.AppEngine.AppsAuthorizedCertificate("FrontendTls", {
118 certificateId: created.certificateId,
119 displayName: "frontend-prod",
120 publicCertificate: CERT_B,
121 privateKey: KEY_B,
122 });
123 }),
124 );
125
126 expect(updated.certificateId).toEqual(created.certificateId);
127 expect(updated.displayName).toEqual("frontend-prod");
128
129 yield* stack.destroy();
130
131 const gone = yield* waitUntilGone(created.appsId, created.certificateId);
132 expect(gone).toEqual("gone");
133 }).pipe(logLevel),
134 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
135);