@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 135 lines 5.3 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 images from "@distilled.cloud/cloudflare/images";
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 import { testName } from "../../names.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // The Images signing-keys endpoints are gated behind a higher Images
18 // entitlement than variants: on the testing account, `GET
19 // /accounts/{id}/images/v1/keys` fails with code 5403 — "The given account
20 // is not valid or is not authorized to access this service" — surfaced as
21 // the typed `ImagesAccessNotEnabled` error, while the variants endpoints on
22 // the very same account succeed. The full lifecycle test below is gated
23 // behind an entitled account supplied via env.
24 const keysEntitled = !!process.env.CLOUDFLARE_TEST_IMAGES_KEYS;
25
26 const listKeyNames = (accountId: string) =>
27 images.listV1Keys({ accountId }).pipe(Effect.map((r) => (r.keys ?? []).map((k) => k.name)));
28
29 // Poll the key list until the named key disappears — list reads are
30 // eventually consistent after a DELETE.
31 const expectGone = (accountId: string, name: string) =>
32 listKeyNames(accountId).pipe(
33 Effect.flatMap((names) =>
34 names.includes(name) ? Effect.fail({ _tag: "KeyNotDeleted" } as const) : Effect.void,
35 ),
36 Effect.retry({
37 while: (e) => e._tag === "KeyNotDeleted",
38 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
39 }),
40 );
41
42 // Read-only enumeration is always exercised (not entitlement-gated):
43 // `list()` swallows the `ImagesAccessNotEnabled` 5403 and returns `[]` on
44 // unentitled accounts, so the result is a well-typed Attributes[] in either
45 // case. On an entitled account it additionally contains the deployed key.
46 test.provider(
47 "list enumerates the account's signing keys",
48 (stack) =>
49 Effect.gen(function* () {
50 const { accountId } = yield* yield* CloudflareEnvironment;
51
52 yield* stack.destroy();
53
54 const provider = yield* Provider.findProvider(Cloudflare.Images.SigningKey);
55 const all = yield* provider.list();
56
57 expect(Array.isArray(all)).toBe(true);
58 for (const key of all) {
59 expect(typeof key.keyName).toBe("string");
60 expect(key.accountId).toEqual(accountId);
61 expect(typeof Redacted.value(key.value)).toBe("string");
62 }
63
64 yield* stack.destroy();
65 }).pipe(logLevel),
66 { tags: ["provider:cloudflare", "provider:cloudflare:images", "live"] },
67 );
68
69 test.provider.skipIf(keysEntitled)(
70 "surfaces the typed ImagesAccessNotEnabled error on unentitled accounts",
71 (stack) =>
72 Effect.gen(function* () {
73 const { accountId } = yield* yield* CloudflareEnvironment;
74
75 yield* stack.destroy();
76
77 // The testing account's Images subscription does not include the
78 // signing-keys API — the distilled call must fail with the typed
79 // entitlement tag (Cloudflare error code 5403).
80 const error = yield* images.listV1Keys({ accountId }).pipe(Effect.flip);
81 expect(error._tag).toEqual("ImagesAccessNotEnabled");
82
83 yield* stack.destroy();
84 }).pipe(logLevel),
85 { tags: ["provider:cloudflare", "provider:cloudflare:images", "live"] },
86 );
87
88 // NOTE: Cloudflare caps signing keys at 2 per account (the account's
89 // default key counts as #1) and refuses to delete the last remaining key.
90 // This suite creates exactly one extra key with a deterministic name and
91 // always cleans it up, so repeated runs stay under the cap.
92 test.provider.skipIf(!keysEntitled)(
93 "create a signing key, no-op redeploy keeps the value, delete",
94 (stack) =>
95 Effect.gen(function* () {
96 const { accountId } = yield* yield* CloudflareEnvironment;
97 const keyName = testName("alchemytestsigningkey");
98
99 yield* stack.destroy();
100
101 const created = yield* stack.deploy(
102 Cloudflare.Images.SigningKey("TestSigningKey", {
103 name: keyName,
104 }),
105 );
106
107 expect(created.keyName).toEqual(keyName);
108 expect(created.accountId).toEqual(accountId);
109 const createdValue = Redacted.value(created.value);
110 expect(createdValue).toBeTruthy();
111
112 // Out-of-band verification — the key shows up in the account list
113 // with the same value.
114 const names = yield* listKeyNames(accountId);
115 expect(names).toContain(keyName);
116
117 // Redeploying identical props must NOT re-PUT (a re-PUT would rotate
118 // the key material) — the value stays byte-identical.
119 const noop = yield* stack.deploy(
120 Cloudflare.Images.SigningKey("TestSigningKey", {
121 name: keyName,
122 }),
123 );
124 expect(noop.keyName).toEqual(keyName);
125 expect(Redacted.value(noop.value)).toEqual(createdValue);
126
127 yield* stack.destroy();
128
129 yield* expectGone(accountId, keyName);
130
131 // Destroy again — delete is idempotent.
132 yield* stack.destroy();
133 }).pipe(logLevel),
134 { tags: ["provider:cloudflare", "provider:cloudflare:images", "live"] },
135 );