@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 83 lines 3.49 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 { Action } from "alchemy/Action";
5 import { poll } from "alchemy/Util/poll";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { testName } from "../../names.ts";
10
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Reading Flagship feature flags inside an Action via `ReadFlagsLocal` — the
16 // local (current-credentials) implementation of the `ReadFlags` binding. It
17 // evaluates flags over the HTTP `evaluate` endpoint instead of the native
18 // Worker binding. Exercises the value + details client methods and the accessor
19 // mechanism (`yield* app.appId` resolved at apply time).
20 test.provider(
21 "ReadFlagsLocal: evaluate a flag from an Action",
22 (stack) =>
23 Effect.gen(function* () {
24 yield* stack.destroy();
25
26 const key = testName("alchemy-test-flag-read-local");
27
28 const out = yield* stack.deploy(
29 Effect.gen(function* () {
30 const app = yield* Cloudflare.Flagship.App("ReadFlagsLocalApp", {});
31 // defaultVariation "on" -> with no rules the flag always evaluates
32 // to `true`, which is distinct from the client-side default `false`,
33 // so a successful read is observable.
34 yield* Cloudflare.Flagship.Flag("ReadFlagsLocalFlag", {
35 appId: app.appId,
36 key,
37 defaultVariation: "on",
38 variations: { off: false, on: true },
39 });
40
41 const Read = Action(
42 "Read",
43 Effect.gen(function* () {
44 const flags = yield* Cloudflare.Flagship.ReadFlags(app);
45
46 return Effect.fn(function* () {
47 // The edge `evaluate` endpoint can lag a freshly-created flag,
48 // and each read can land on a different edge; poll until
49 // every read returns the flag's true value (bounded).
50 const { enabled, details, untyped } = yield* poll({
51 description: "evaluate returns the flag value",
52 effect: Effect.all({
53 enabled: flags.getBooleanValue(key, false),
54 details: flags.getBooleanDetails(key, false),
55 untyped: flags.get(key),
56 }),
57 predicate: (read) =>
58 read.enabled === true && read.details.value === true && read.untyped === true,
59 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]),
60 });
61 // A boolean flag read as a string falls back to the default.
62 const asString = yield* flags.getStringValue(key, "fallback");
63
64 return { enabled, details, asString, untyped };
65 });
66 }).pipe(Effect.provide(Cloudflare.Flagship.ReadFlagsLocal)),
67 );
68
69 return yield* Read({});
70 }),
71 );
72
73 expect(out.enabled).toBe(true);
74 expect(out.details.flagKey).toBe(key);
75 expect(out.details.value).toBe(true);
76 expect(out.details.variant).toBe("on");
77 expect(out.asString).toBe("fallback");
78 expect(out.untyped).toBe(true);
79
80 yield* stack.destroy();
81 }).pipe(logLevel),
82 { tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"], timeout: 120_000 },
83 );