1import * as ACME from "@/ACME";
2import * as Cloudflare from "@/Cloudflare";
3import * as Test from "@/Test/Alchemy";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as Schedule from "effect/Schedule";
8import * as HttpClient from "effect/http/HttpClient";
9import Stack from "./fixtures/issue-stack.ts";
10import { ZONE_NAME } from "./fixtures/shared.ts";
11
12/**
13 * The same runtime issuance as `IssueCertificate.test.ts`, but the Worker
14 * runs in local workerd (`alchemy dev`): the bound account and DNS token
15 * are real, the CA is Let's Encrypt staging, only the Worker is local.
16 */
17const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
18 providers: Layer.mergeAll(Cloudflare.providers(), ACME.providers()),
19 dev: true,
20});
21
22const stack = beforeAll(deploy(Stack));
23afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
24
25const NAME = `alchemy-acme-worker-local.${ZONE_NAME}`;
26
27test(
28 "a local Worker issues a certificate at runtime through the bound account",
29 Effect.gen(function* () {
30 const { url } = yield* stack;
31 expect(url).toContain("localhost");
32 const client = yield* HttpClient.HttpClient;
33 const ok = yield* client.get(`${url}/`).pipe(
34 Effect.filterOrFail(
35 (r) => r.status === 200,
36 (r) => new Error(`not ready: ${r.status}`),
37 ),
38 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 8 }),
39 );
40 expect(ok.status).toBe(200);
41
42 const response = yield* client.get(`${url}/issue?name=${NAME}`);
43 const text = yield* response.text;
44 yield* Effect.log("ISSUE RESPONSE", { status: response.status, text });
45 const body = JSON.parse(text) as {
46 issuer?: string;
47 dnsNames?: string[];
48 hasKey?: boolean;
49 error?: string;
50 };
51 expect(body.error).toBeUndefined();
52 expect(body.issuer).toContain("STAGING");
53 expect(body.dnsNames).toEqual([NAME]);
54 expect(body.hasKey).toBe(true);
55 }),
56 {
57 tags: [
58 "provider:acme",
59 "provider:acme:account",
60 "provider:cloudflare",
61 "provider:cloudflare:dns",
62 "provider:cloudflare:worker",
63 "provider:cloudflare:zone",
64 "provider:zerossl",
65 "live",
66 ],
67 timeout: 240_000,
68 },
69);