@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 89 lines 3.01 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 Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import SecretKeyWorker from "./fixtures/secret-key/worker.ts";
10
11 // `dev: true` runs local providers behind the RPC sidecar proxy by default,
12 // matching the process topology of the real `alchemy dev` command.
13 const { test } = Test.make({
14 providers: Cloudflare.providers(),
15 dev: true,
16 });
17
18 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
19
20 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
21 status: number;
22 }> {}
23
24 /**
25 * `secret_key` bindings are lowered to workerd's native
26 * `Worker_Binding.cryptoKey` config under the local runtime, so the dev
27 * worker sees real non-extractable `CryptoKey`s in `env`. The fixture
28 * carries the same HMAC material as a `raw` binding and a `jwk` binding
29 * and cross-verifies a signature between them.
30 */
31 test.provider(
32 "secret_key bindings become native CryptoKeys under the local runtime",
33 (stack) =>
34 Effect.gen(function* () {
35 yield* stack.destroy();
36
37 const deployed = yield* stack.deploy(
38 Effect.gen(function* () {
39 const worker = yield* SecretKeyWorker;
40 return { worker };
41 }),
42 );
43
44 // The local provider serves from the dev proxy — proof no cloud call
45 // ran.
46 expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/);
47
48 const url = deployed.worker.url;
49 const client = yield* HttpClient.HttpClient;
50
51 const res = yield* Effect.gen(function* () {
52 const res = yield* client.get(`${url}/`);
53 if (res.status !== 200) {
54 return yield* Effect.fail(new WorkerNotReady({ status: res.status }));
55 }
56 return res;
57 }).pipe(
58 Effect.retry({
59 schedule: Schedule.exponential("500 millis"),
60 times: 10,
61 }),
62 );
63
64 const body = (yield* res.json) as {
65 crossVerified: boolean;
66 rawIsCryptoKey: boolean;
67 jwkIsCryptoKey: boolean;
68 algorithm: string;
69 usages: string[];
70 extractable: boolean;
71 };
72 // A signature from the raw-format key verifies with the jwk-format
73 // key — both formats imported the same material correctly.
74 expect(body.crossVerified).toBe(true);
75 expect(body.rawIsCryptoKey).toBe(true);
76 expect(body.jwkIsCryptoKey).toBe(true);
77 expect(body.algorithm).toBe("HMAC");
78 expect(body.usages).toEqual(["sign", "verify"]);
79 // The Cloudflare API never marks bound keys extractable; the local
80 // lowering matches.
81 expect(body.extractable).toBe(false);
82
83 yield* stack.destroy();
84 }).pipe(logLevel),
85 {
86 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
87 timeout: 120_000,
88 },
89 );