@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 242 lines 8.77 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as secretsStore from "@distilled.cloud/cloudflare/secrets-store";
5 import * as Effect from "effect/Effect";
6 import * as Redacted from "effect/Redacted";
7 import { MinimumLogLevel } from "effect/References";
8 import { testName } from "../../names.ts";
9
10 const { test } = Test.make({ providers: Cloudflare.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const GATEWAY_ID = testName("alchemy-test-aigw-providerkey");
15 const PROVIDER_SLUG = "openai";
16
17 test.provider(
18 "provisions a Secret + GatewayProvider with the correct naming contract",
19 (stack) =>
20 Effect.gen(function* () {
21 yield* stack.destroy();
22
23 const deployed = yield* stack.deploy(
24 Effect.gen(function* () {
25 const store = yield* Cloudflare.SecretsStore.Store("PkStore");
26 const gateway = yield* Cloudflare.AI.Gateway("PkGateway", {
27 id: GATEWAY_ID,
28 storeId: store.storeId,
29 });
30 // ProviderKey composes Secret + GatewayProvider, deriving the secret
31 // name from `{gatewayId}_{providerSlug}_{alias}`.
32 return yield* Cloudflare.AI.ProviderKey("PkKey", {
33 store,
34 gatewayId: gateway.gatewayId,
35 providerSlug: PROVIDER_SLUG,
36 value: Redacted.make("alchemy-test-not-a-real-key"),
37 });
38 }),
39 );
40
41 // The secret must be named exactly `{gatewayId}_{providerSlug}_{alias}`.
42 const expectedName = `${GATEWAY_ID}_${PROVIDER_SLUG}_default`;
43 expect(deployed.secret.secretName).toEqual(expectedName);
44 // The secret must be scoped to `ai_gateway` for BYOK resolution.
45 expect(deployed.secret.scopes).toContain("ai_gateway");
46
47 // The gateway provider must reference the backing secret's id.
48 expect(deployed.gatewayProvider.secretId).toEqual(deployed.secret.secretId);
49 // The alias carried through is `"default"` (the omitted-alias default).
50 expect(deployed.gatewayProvider.alias).toEqual("default");
51 expect(deployed.gatewayProvider.providerSlug).toEqual(PROVIDER_SLUG);
52 expect(deployed.gatewayProvider.gatewayId).toEqual(GATEWAY_ID);
53
54 // Redeploying identical props is a no-op — same resource IDs.
55 const redeployed = yield* stack.deploy(
56 Effect.gen(function* () {
57 const store = yield* Cloudflare.SecretsStore.Store("PkStore");
58 const gateway = yield* Cloudflare.AI.Gateway("PkGateway", {
59 id: GATEWAY_ID,
60 storeId: store.storeId,
61 });
62 return yield* Cloudflare.AI.ProviderKey("PkKey", {
63 store,
64 gatewayId: gateway.gatewayId,
65 providerSlug: PROVIDER_SLUG,
66 value: Redacted.make("alchemy-test-not-a-real-key"),
67 });
68 }),
69 );
70 expect(redeployed.secret.secretId).toEqual(deployed.secret.secretId);
71 expect(redeployed.gatewayProvider.providerConfigId).toEqual(
72 deployed.gatewayProvider.providerConfigId,
73 );
74
75 yield* stack.destroy();
76 }).pipe(logLevel),
77 {
78 tags: [
79 "provider:cloudflare",
80 "provider:cloudflare:ai",
81 "provider:cloudflare:secretsstore",
82 "live",
83 ],
84 },
85 );
86
87 test.provider(
88 "explicit alias threads through the naming contract",
89 (stack) =>
90 Effect.gen(function* () {
91 yield* stack.destroy();
92
93 const deployed = yield* stack.deploy(
94 Effect.gen(function* () {
95 const store = yield* Cloudflare.SecretsStore.Store("PkAliasStore");
96 const gateway = yield* Cloudflare.AI.Gateway("PkAliasGateway", {
97 id: GATEWAY_ID + "-alias",
98 storeId: store.storeId,
99 });
100 return yield* Cloudflare.AI.ProviderKey("PkAliasKey", {
101 store,
102 gatewayId: gateway.gatewayId,
103 providerSlug: "anthropic",
104 alias: "evals",
105 value: Redacted.make("alchemy-test-not-a-real-key"),
106 });
107 }),
108 );
109
110 expect(deployed.secret.secretName).toEqual(`${GATEWAY_ID}-alias_anthropic_evals`);
111 expect(deployed.gatewayProvider.alias).toEqual("evals");
112
113 yield* stack.destroy();
114 }).pipe(logLevel),
115 {
116 tags: [
117 "provider:cloudflare",
118 "provider:cloudflare:ai",
119 "provider:cloudflare:secretsstore",
120 "live",
121 ],
122 },
123 );
124
125 test.provider(
126 "value rotation updates in place; alias change cascades a replacement",
127 (stack) =>
128 Effect.gen(function* () {
129 yield* stack.destroy();
130
131 const program = (opts: { value: string; alias?: string }) =>
132 Effect.gen(function* () {
133 const store = yield* Cloudflare.SecretsStore.Store("PkRotateStore");
134 const gateway = yield* Cloudflare.AI.Gateway("PkRotateGateway", {
135 id: GATEWAY_ID + "-rotate",
136 storeId: store.storeId,
137 });
138 return yield* Cloudflare.AI.ProviderKey("PkRotateKey", {
139 store,
140 gatewayId: gateway.gatewayId,
141 providerSlug: PROVIDER_SLUG,
142 alias: opts.alias,
143 value: Redacted.make(opts.value),
144 });
145 });
146
147 const initial = yield* stack.deploy(program({ value: "alchemy-test-key-v1" }));
148
149 // Rotating the key value is an in-place update: the secret keeps its
150 // id, so the provider config it feeds is untouched.
151 const rotated = yield* stack.deploy(program({ value: "alchemy-test-key-v2" }));
152 expect(rotated.secret.secretId).toEqual(initial.secret.secretId);
153 expect(rotated.gatewayProvider.providerConfigId).toEqual(
154 initial.gatewayProvider.providerConfigId,
155 );
156
157 // Changing the alias renames the secret — a replacement (new id) —
158 // and must cascade: the provider config is replaced (delete-first)
159 // and re-pointed at the new secret.
160 const renamed = yield* stack.deploy(
161 program({ value: "alchemy-test-key-v2", alias: "rotated" }),
162 );
163 expect(renamed.secret.secretName).toEqual(`${GATEWAY_ID}-rotate_${PROVIDER_SLUG}_rotated`);
164 expect(renamed.secret.secretId).not.toEqual(initial.secret.secretId);
165 expect(renamed.gatewayProvider.providerConfigId).not.toEqual(
166 initial.gatewayProvider.providerConfigId,
167 );
168 expect(renamed.gatewayProvider.alias).toEqual("rotated");
169 expect(renamed.gatewayProvider.secretId).toEqual(renamed.secret.secretId);
170
171 // The replaced secret is reclaimed — no orphan left in the store.
172 const oldSecret = yield* secretsStore
173 .getStoreSecret({
174 accountId: initial.secret.accountId,
175 storeId: initial.secret.storeId,
176 secretId: initial.secret.secretId,
177 })
178 .pipe(Effect.catchTag("SecretNotFound", () => Effect.succeed(undefined)));
179 expect(oldSecret).toBeUndefined();
180
181 yield* stack.destroy();
182 }).pipe(logLevel),
183 {
184 tags: [
185 "provider:cloudflare",
186 "provider:cloudflare:ai",
187 "provider:cloudflare:secretsstore",
188 "live",
189 ],
190 },
191 );
192
193 test.provider(
194 "destroying the provider key removes the secret from the surviving store",
195 (stack) =>
196 Effect.gen(function* () {
197 yield* stack.destroy();
198
199 const deployed = yield* stack.deploy(
200 Effect.gen(function* () {
201 const store = yield* Cloudflare.SecretsStore.Store("PkDeleteStore");
202 const gateway = yield* Cloudflare.AI.Gateway("PkDeleteGateway", {
203 id: GATEWAY_ID + "-delete",
204 storeId: store.storeId,
205 });
206 return yield* Cloudflare.AI.ProviderKey("PkDeleteKey", {
207 store,
208 gatewayId: gateway.gatewayId,
209 providerSlug: PROVIDER_SLUG,
210 value: Redacted.make("alchemy-test-not-a-real-key"),
211 });
212 }),
213 );
214
215 expect(deployed.secret.secretName).toEqual(`${GATEWAY_ID}-delete_${PROVIDER_SLUG}_default`);
216
217 yield* stack.destroy();
218
219 // The account-level Secrets Store is adopted, not created, so it
220 // survives `destroy` — the helper's Secret resource is the only thing
221 // that reclaims the BYOK secret. Verify the secret is actually gone
222 // (an orphaned secret is the one real leak this composition could
223 // introduce). A surviving store means a missing secret surfaces as
224 // `SecretNotFound`, not `StoreNotFound`.
225 const secretAfter = yield* secretsStore
226 .getStoreSecret({
227 accountId: deployed.secret.accountId,
228 storeId: deployed.secret.storeId,
229 secretId: deployed.secret.secretId,
230 })
231 .pipe(Effect.catchTag("SecretNotFound", () => Effect.succeed(undefined)));
232 expect(secretAfter).toBeUndefined();
233 }).pipe(logLevel),
234 {
235 tags: [
236 "provider:cloudflare",
237 "provider:cloudflare:ai",
238 "provider:cloudflare:secretsstore",
239 "live",
240 ],
241 },
242 );