t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/GCP/CertificateManager/CertificateMapEntry.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

213 lines
8.5 KiB
TypeScript

import * as GCP from "@/GCP";
import * as Test from "@/Test/Alchemy";
import * as certificatemanager from "@distilled.cloud/gcp/certificatemanager_v1";
import { expect } from "alchemy-test";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
const { test } = Test.make({ providers: GCP.providers() });
const logLevel = Effect.provideService(
MinimumLogLevel,
process.env.DEBUG ? "Debug" : "Info",
);
// Self-signed RSA-2048 fixtures generated once with openssl (not at test time).
const CERT_A = `-----BEGIN CERTIFICATE-----
MIIDOjCCAiKgAwIBAgIUY07uz+vKYeBK3NYMhk03eWyTPKkwDQYJKoZIhvcNAQEL
BQAwHTEbMBkGA1UEAwwSYWxjaGVteS1zc2wtYS50ZXN0MB4XDTI2MDgyNDE1NDM0
NFoXDTM2MDgyMTE1NDM0NFowHTEbMBkGA1UEAwwSYWxjaGVteS1zc2wtYS50ZXN0
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzUAYVVO5iOluteYkxOqA
bBF+EnWrtfsUMoPJPXBEIrmT66G+vx+rArDsRxJqnWr9YstEQKsHtkErGF37avZr
12jTIiKWk9auxyhN8O6WD2jND13m+IfsdeWKSIFx3j/RJ0McJQYu4YuN8ViZm3t9
o3f5N+4DczgqzloV7U6/DFeMZ23nwBD9IgrUf6KyGoC/sJ4twTjpxSECekiOHgyn
/M7SwrBTJYJTsMXzqYBPVjk7f17+7LAwNqBNzQx6HsGV8sZX4kDlcMAL53obLV1z
kkJHhP3suaWCUl5uarZ9r5vc7DC2j6vvtghFsDvRWqqram3fCEOJOQmoAVlChbtq
SwIDAQABo3Iwc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Wgy2TvR4gfcfI9QKVXSzDBauq1DiW86j6edI+2
fygch3+OZKV+N0l93MQpSWv4
-----END PRIVATE KEY-----`;
const certificateIdOf = (name: string) => name.split("/").pop() ?? name;
const hasCertificate = (
names: readonly string[] | undefined,
certName: string,
) =>
(names ?? []).some(
(name) => certificateIdOf(name) === certificateIdOf(certName),
);
const waitUntilGone = (name: string) =>
certificatemanager
.getProjectsLocationsCertificateMapsCertificateMapEntries({ name })
.pipe(
Effect.as("found" as const),
Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
Effect.repeat({
schedule: Schedule.spaced("1 second"),
until: (status) => status === "gone",
times: 10,
}),
);
test.provider(
"create, update, and delete a certificate map entry",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
const created = yield* stack.deploy(
Effect.gen(function* () {
const map = yield* GCP.CertificateManager.CertificateMap(
"FrontendMap",
{
location: "global",
description: "frontend map for entries",
},
);
const certA = yield* GCP.CertificateManager.Certificate("TlsA", {
location: "global",
pemCertificate: CERT_A,
pemPrivateKey: KEY_A,
});
const entry = yield* GCP.CertificateManager.CertificateMapEntry(
"Www",
{
certificateMap: map.name,
description: "entry a",
labels: { env: "test" },
certificates: [certA.name],
hostname: "alchemy-ssl-a.test",
},
);
return { map, certA, entry };
}),
);
expect(created.entry.name).toContain("/certificateMapEntries/");
expect(created.entry.certificateMapEntryId).toEqual(expect.any(String));
expect(created.entry.certificateMap).toEqual(created.map.name);
expect(created.entry.location).toEqual("global");
expect(created.entry.description).toEqual("entry a");
expect(created.entry.labels).toMatchObject({ env: "test" });
expect(created.entry.hostname).toEqual("alchemy-ssl-a.test");
expect(created.entry.certificates).toContain(created.certA.name);
expect(created.entry.createTime).toEqual(expect.any(String));
const fetched =
yield* certificatemanager.getProjectsLocationsCertificateMapsCertificateMapEntries(
{
name: created.entry.name,
},
);
expect(fetched.name).toEqual(created.entry.name);
expect(fetched.hostname).toEqual("alchemy-ssl-a.test");
expect(fetched.description).toEqual("entry a");
expect(fetched.labels?.env).toEqual("test");
expect(hasCertificate(fetched.certificates, created.certA.name)).toEqual(
true,
);
expect(
Object.keys(fetched.labels ?? {}).some((key) =>
key.startsWith("alchemy-"),
),
).toEqual(true);
const updated = yield* stack.deploy(
Effect.gen(function* () {
const map = yield* GCP.CertificateManager.CertificateMap(
"FrontendMap",
{
certificateMapId: created.map.certificateMapId,
location: "global",
description: "frontend map for entries",
},
);
const certA = yield* GCP.CertificateManager.Certificate("TlsA", {
certificateId: created.certA.certificateId,
location: "global",
pemCertificate: CERT_A,
pemPrivateKey: KEY_A,
});
const entry = yield* GCP.CertificateManager.CertificateMapEntry(
"Www",
{
certificateMap: map.name,
certificateMapEntryId: created.entry.certificateMapEntryId,
description: "entry b",
labels: { env: "prod", role: "sni" },
certificates: [certA.name],
hostname: "alchemy-ssl-a.test",
},
);
return { map, certA, entry };
}),
);
expect(updated.entry.name).toEqual(created.entry.name);
expect(updated.entry.description).toEqual("entry b");
expect(updated.entry.labels).toMatchObject({ env: "prod", role: "sni" });
expect(updated.entry.hostname).toEqual("alchemy-ssl-a.test");
expect(updated.entry.certificates).toContain(created.certA.name);
const refetched =
yield* certificatemanager.getProjectsLocationsCertificateMapsCertificateMapEntries(
{
name: created.entry.name,
},
);
expect(refetched.description).toEqual("entry b");
expect(refetched.labels?.env).toEqual("prod");
expect(refetched.labels?.role).toEqual("sni");
expect(refetched.hostname).toEqual("alchemy-ssl-a.test");
expect(
hasCertificate(refetched.certificates, created.certA.name),
).toEqual(true);
yield* stack.destroy();
const gone = yield* waitUntilGone(created.entry.name);
expect(gone).toEqual("gone");
}).pipe(logLevel),
{
tags: ["provider:gcp", "provider:gcp:certificatemanager", "live"],
timeout: 120_000,
},
);