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

main
ts 209 lines 7.59 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as workers from "@distilled.cloud/cloudflare/workers";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import { expectUrlContains } from "../Utils/Http.ts";
9 import Stack from "./fixtures/env/stack.ts";
10
11 // Populate process.env before deploy so the worker fixture's
12 // `Config.xxx(...)` reads resolve at deploy time (default provider).
13 const CONFIG_STR_VALUE = (process.env.CONFIG_STR = "config-string-value");
14 const CONFIG_NUM_VALUE = (process.env.CONFIG_NUM = "1234");
15 const CONFIG_REDACTED_VALUE = (process.env.CONFIG_REDACTED = "config-redacted-value");
16 const CONFIG_REDACTED_INIT_VALUE = (process.env.CONFIG_REDACTED_INIT =
17 "config-redacted-init-value");
18 // Read via `Config.String("HOST").pipe(Config.nested("CONFIG_NESTED"))` —
19 // binds under the flattened `CONFIG_NESTED_HOST` key.
20 const CONFIG_NESTED_HOST_VALUE = (process.env.CONFIG_NESTED_HOST = "config-nested-host-value");
21
22 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
23 providers: Cloudflare.providers(),
24 });
25
26 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
27
28 const stack = beforeAll(deploy(Stack));
29 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
30
31 describe.concurrent(
32 "Cloudflare.Worker env bindings",
33 { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] },
34 () => {
35 test(
36 "async worker round-trips every supported binding shape",
37 Effect.gen(function* () {
38 const { asyncUrl } = yield* stack;
39 expect(asyncUrl).toBeTypeOf("string");
40 yield* Effect.log(asyncUrl);
41
42 const body = yield* expectUrlContains(asyncUrl, '"STR":"hello"', {
43 timeout: "60 seconds",
44 label: "async env-worker response",
45 });
46 expect(JSON.parse(body)).toEqual({
47 STR: "hello",
48 NUM: 42,
49 BOOL: true,
50 NULL: null,
51 OBJ: { nested: { value: "ok" }, count: 7 },
52 ARR: [1, 2, 3],
53 OUTPUT_STR: "output-str",
54 RANDOM_IS_HEX: true,
55 SECRET_STR: "shh",
56 SECRET_JSON: { token: "abc", scopes: ["read", "write"] },
57 CONFIG_STR: CONFIG_STR_VALUE,
58 CONFIG_NUM: Number(CONFIG_NUM_VALUE),
59 CONFIG_REDACTED: CONFIG_REDACTED_VALUE,
60 VERSION_METADATA: {
61 id: expect.any(String),
62 tag: expect.any(String),
63 timestamp: expect.any(String),
64 },
65 });
66 }).pipe(logLevel),
67 );
68
69 test.provider("Output env values classify by resolved value on the wire", () =>
70 Effect.gen(function* () {
71 const { asyncWorkerName } = yield* stack;
72 const { accountId } = yield* yield* CloudflareEnvironment;
73
74 const settings = yield* workers.getScriptScriptAndVersionSetting({
75 accountId,
76 scriptName: asyncWorkerName,
77 });
78 // Output<Redacted<string>> (Alchemy.Random) must deploy encrypted,
79 // never as a plaintext json binding.
80 expect(settings.bindings).toContainEqual(
81 expect.objectContaining({ type: "secret_text", name: "RANDOM" }),
82 );
83 // Output<string> resolves to a string, so it lands as plain_text.
84 expect(settings.bindings).toContainEqual(
85 expect.objectContaining({ type: "plain_text", name: "OUTPUT_STR" }),
86 );
87 }).pipe(logLevel),
88 );
89
90 test(
91 "effect worker round-trips env: literals and Redacted via WorkerEnvironment",
92 Effect.gen(function* () {
93 const { effectUrl } = yield* stack;
94
95 const body = yield* expectUrlContains(`${effectUrl}/env`, '"STR":"hello"', {
96 timeout: "60 seconds",
97 label: "effect env-worker /env",
98 });
99 expect(JSON.parse(body)).toEqual({
100 STR: "hello",
101 NUM: 42,
102 BOOL: true,
103 NULL: null,
104 OBJ: { nested: { value: "ok" }, count: 7 },
105 ARR: [1, 2, 3],
106 OUTPUT_STR: "output-str",
107 SECRET_STR: "shh",
108 SECRET_JSON: { token: "abc", scopes: ["read", "write"] },
109 });
110 }).pipe(logLevel),
111 );
112
113 test(
114 "effect worker reads env strings that parse as JSON as strings (#2847)",
115 Effect.gen(function* () {
116 const { effectUrl } = yield* stack;
117
118 const body = yield* expectUrlContains(`${effectUrl}/json-strings`, '"PLAIN"', {
119 timeout: "60 seconds",
120 label: "effect env-worker /json-strings",
121 });
122 const strings = {
123 JSON_FALSE: "false",
124 JSON_NUM: "42",
125 JSON_NULL: "null",
126 JSON_OBJ: '{"a":1}',
127 PLAIN: "my-queue",
128 };
129 expect(JSON.parse(body)).toEqual({
130 init: { ...strings, JSON_FALSE_BOOL: false },
131 runtime: { ...strings, JSON_FALSE_BOOL: false },
132 env: strings,
133 });
134 }).pipe(logLevel),
135 );
136
137 test(
138 "effect worker resolves the yielded VersionMetadata binding",
139 Effect.gen(function* () {
140 const { effectUrl } = yield* stack;
141
142 const body = yield* expectUrlContains(`${effectUrl}/version`, '"id"', {
143 timeout: "60 seconds",
144 label: "effect env-worker /version",
145 });
146 expect(JSON.parse(body)).toEqual({
147 id: expect.any(String),
148 tag: expect.any(String),
149 timestamp: expect.any(String),
150 });
151 }).pipe(logLevel),
152 );
153
154 test(
155 "effect worker re-resolves Config.xxx at request time inside a nested effect",
156 Effect.gen(function* () {
157 const { effectUrl } = yield* stack;
158
159 const body = yield* expectUrlContains(`${effectUrl}/config-runtime`, '"CONFIG_STR"', {
160 timeout: "60 seconds",
161 label: "effect env-worker /config-runtime",
162 });
163 expect(JSON.parse(body)).toEqual({
164 CONFIG_STR: CONFIG_STR_VALUE,
165 CONFIG_NUM: Number(CONFIG_NUM_VALUE),
166 CONFIG_NUM_WITH_DEFAULT: Number(CONFIG_NUM_VALUE),
167 CONFIG_UNSET_WITH_DEFAULT: 3000,
168 CONFIG_REDACTED_INIT: CONFIG_REDACTED_INIT_VALUE,
169 CONFIG_REDACTED_INIT_IS_REDACTED: true,
170 CONFIG_ALL_OBJ: {
171 str: CONFIG_STR_VALUE,
172 num: Number(CONFIG_NUM_VALUE),
173 redacted: CONFIG_REDACTED_INIT_VALUE,
174 redactedIsRedacted: true,
175 },
176 CONFIG_ALL_TUPLE: [CONFIG_STR_VALUE, Number(CONFIG_NUM_VALUE)],
177 CONFIG_NESTED_HOST: CONFIG_NESTED_HOST_VALUE,
178 });
179 }).pipe(logLevel),
180 );
181
182 test(
183 "effect worker round-trips Config.xxx bindings captured in Init",
184 Effect.gen(function* () {
185 const { effectUrl } = yield* stack;
186
187 const body = yield* expectUrlContains(`${effectUrl}/config`, '"CONFIG_STR"', {
188 timeout: "60 seconds",
189 label: "effect env-worker /config",
190 });
191 expect(JSON.parse(body)).toEqual({
192 CONFIG_STR: CONFIG_STR_VALUE,
193 CONFIG_NUM: Number(CONFIG_NUM_VALUE),
194 CONFIG_REDACTED: CONFIG_REDACTED_VALUE,
195 CONFIG_REDACTED_INIT: CONFIG_REDACTED_INIT_VALUE,
196 CONFIG_REDACTED_INIT_IS_REDACTED: true,
197 CONFIG_ALL_OBJ: {
198 str: CONFIG_STR_VALUE,
199 num: Number(CONFIG_NUM_VALUE),
200 redacted: CONFIG_REDACTED_INIT_VALUE,
201 redactedIsRedacted: true,
202 },
203 CONFIG_ALL_TUPLE: [CONFIG_STR_VALUE, Number(CONFIG_NUM_VALUE)],
204 CONFIG_NESTED_HOST: CONFIG_NESTED_HOST_VALUE,
205 });
206 }).pipe(logLevel),
207 );
208 },
209 );