@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 132 lines 4.14 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 * as Schedule from "effect/Schedule";
8 import Stack from "./fixtures/stack.ts";
9
10 // The fixture stack provisions its own Flagship app via the FlagshipApp
11 // resource and binds it to both workers. Each fixture worker evaluates a
12 // flag and returns the result; with no matching flag configured, Flagship
13 // falls back to the provided defaults, so the assertions below hold whether
14 // or not the app has flags defined.
15 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
16 providers: Cloudflare.providers(),
17 });
18
19 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
20 status: number;
21 body: string;
22 }> {}
23
24 // A fresh workers.dev URL needs a few seconds of edge propagation before it
25 // serves real traffic. During that window the request can fail in several
26 // ways: the DNS name doesn't resolve yet (transport `RequestError`), the
27 // edge returns Cloudflare's placeholder page (4xx/5xx), or a 200 briefly
28 // carries a non-JSON body (parse error). Retry through ALL of these — the
29 // same defensiveness the Workers/*.test.ts suites use — until the worker
30 // answers with a real 200 JSON body. `WorkerNotReady` keeps the status/body
31 // on the final surfaced error so a genuine failure isn't masked.
32 const getJson = (url: string) =>
33 HttpClient.get(url).pipe(
34 Effect.flatMap((res) =>
35 res.status === 200
36 ? res.json
37 : res.text.pipe(
38 Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))),
39 ),
40 ),
41 Effect.retry({
42 schedule: Schedule.max([
43 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("5 seconds")]),
44 Schedule.recurs(30),
45 ]),
46 }),
47 );
48
49 const stack = beforeAll(deploy(Stack));
50 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
51
52 test(
53 "async worker evaluates a boolean flag via env Flagship binding",
54 Effect.gen(function* () {
55 const { asyncWorkerUrl } = yield* stack;
56 const result = yield* getJson(`${asyncWorkerUrl}/bool`);
57 expect(result).toMatchObject({ mode: "async", enabled: false });
58 }),
59 {
60 tags: [
61 "provider:cloudflare",
62 "provider:cloudflare:flagship",
63 "provider:cloudflare:worker",
64 "live",
65 ],
66 timeout: 240_000,
67 },
68 );
69
70 test(
71 "async worker returns fallback details for a nonexistent flag",
72 Effect.gen(function* () {
73 const { asyncWorkerUrl } = yield* stack;
74 const result = (yield* getJson(`${asyncWorkerUrl}/details`)) as {
75 mode: string;
76 details: { flagKey: string; value: string };
77 };
78 expect(result.mode).toBe("async");
79 expect(result.details.flagKey).toBe("nonexistent-flag");
80 expect(result.details.value).toBe("fallback");
81 }),
82 {
83 tags: [
84 "provider:cloudflare",
85 "provider:cloudflare:flagship",
86 "provider:cloudflare:worker",
87 "live",
88 ],
89 timeout: 240_000,
90 },
91 );
92
93 test(
94 "effect worker evaluates a boolean flag via FlagshipApp.bind",
95 Effect.gen(function* () {
96 const { effectWorkerUrl } = yield* stack;
97 const result = yield* getJson(`${effectWorkerUrl}/bool`);
98 expect(result).toMatchObject({ mode: "effect", enabled: false });
99 }),
100 {
101 tags: [
102 "provider:cloudflare",
103 "provider:cloudflare:flagship",
104 "provider:cloudflare:worker",
105 "live",
106 ],
107 timeout: 240_000,
108 },
109 );
110
111 test(
112 "effect worker returns fallback details for a nonexistent flag",
113 Effect.gen(function* () {
114 const { effectWorkerUrl } = yield* stack;
115 const result = (yield* getJson(`${effectWorkerUrl}/details`)) as {
116 mode: string;
117 details: { flagKey: string; value: string };
118 };
119 expect(result.mode).toBe("effect");
120 expect(result.details.flagKey).toBe("nonexistent-flag");
121 expect(result.details.value).toBe("fallback");
122 }),
123 {
124 tags: [
125 "provider:cloudflare",
126 "provider:cloudflare:flagship",
127 "provider:cloudflare:worker",
128 "live",
129 ],
130 timeout: 240_000,
131 },
132 );