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