The best way to control Claude Code, Codex and more from any computer or phone

main
ts 95 lines 3.24 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as stream from "@distilled.cloud/cloudflare/stream";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import * as Redacted from "effect/Redacted";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 /**
17 * True when the key with the given id is listed on the account. The list
18 * endpoint returns `result: null` (not `[]`) when there are no keys.
19 * Rides out 403 blips (`Forbidden`) from token propagation.
20 */
21 const keyListed = (accountId: string, keyId: string) =>
22 stream.getKey({ accountId }).pipe(
23 Effect.map((res) => (res.result ?? []).some((k) => k.id === keyId)),
24 Effect.retry({
25 while: (e) => e._tag === "Forbidden",
26 schedule: Schedule.exponential("500 millis"),
27 times: 8,
28 }),
29 );
30
31 const expectGone = (accountId: string, keyId: string) =>
32 keyListed(accountId, keyId).pipe(
33 Effect.flatMap((listed) =>
34 listed ? Effect.fail({ _tag: "SigningKeyNotDeleted" } as const) : Effect.void,
35 ),
36 Effect.retry({
37 while: (e) => e._tag === "SigningKeyNotDeleted",
38 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
39 }),
40 );
41
42 test.provider(
43 "create and delete a signing key",
44 (stack) =>
45 Effect.gen(function* () {
46 const { accountId } = yield* yield* CloudflareEnvironment;
47
48 yield* stack.destroy();
49
50 const key = yield* stack.deploy(Cloudflare.Stream.SigningKey("PlaybackKey", {}));
51
52 expect(key.keyId).toBeTruthy();
53 expect(key.accountId).toEqual(accountId);
54 // The key material is only returned at creation time — it must be
55 // captured in the attributes.
56 expect(Redacted.value(key.pem)).toBeTruthy();
57 expect(Redacted.value(key.jwk)).toBeTruthy();
58
59 expect(yield* keyListed(accountId, key.keyId)).toBe(true);
60
61 // Redeploying is a no-op — same key, same material preserved.
62 const noop = yield* stack.deploy(Cloudflare.Stream.SigningKey("PlaybackKey", {}));
63 expect(noop.keyId).toEqual(key.keyId);
64 expect(Redacted.value(noop.pem)).toEqual(Redacted.value(key.pem));
65
66 yield* stack.destroy();
67
68 yield* expectGone(accountId, key.keyId);
69 }).pipe(logLevel),
70 {
71 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
72 timeout: 120_000,
73 },
74 );
75
76 test.provider(
77 "list enumerates the deployed signing key",
78 (stack) =>
79 Effect.gen(function* () {
80 yield* stack.destroy();
81
82 const key = yield* stack.deploy(Cloudflare.Stream.SigningKey("ListKey", {}));
83
84 const provider = yield* Provider.findProvider(Cloudflare.Stream.SigningKey);
85 const all = yield* provider.list();
86
87 expect(all.some((k) => k.keyId === key.keyId)).toBe(true);
88
89 yield* stack.destroy();
90 }).pipe(logLevel),
91 {
92 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
93 timeout: 120_000,
94 },
95 );