import * as Cloudflare from "@alchemy.run/cloudflare";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import { Action } from "alchemy/Action";
import { poll } from "alchemy/Util/poll";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
import { testName } from "../../names.ts";
const { test } = Test.make({ providers: Cloudflare.providers() });
const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
// Reading Flagship feature flags inside an Action via `ReadFlagsLocal` — the
// local (current-credentials) implementation of the `ReadFlags` binding. It
// evaluates flags over the HTTP `evaluate` endpoint instead of the native
// Worker binding. Exercises the value + details client methods and the accessor
// mechanism (`yield* app.appId` resolved at apply time).
test.provider(
"ReadFlagsLocal: evaluate a flag from an Action",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
const key = testName("alchemy-test-flag-read-local");
const out = yield* stack.deploy(
Effect.gen(function* () {
const app = yield* Cloudflare.Flagship.App("ReadFlagsLocalApp", {});
// defaultVariation "on" -> with no rules the flag always evaluates
// to `true`, which is distinct from the client-side default `false`,
// so a successful read is observable.
yield* Cloudflare.Flagship.Flag("ReadFlagsLocalFlag", {
appId: app.appId,
key,
defaultVariation: "on",
variations: { off: false, on: true },
});
const Read = Action(
"Read",
Effect.gen(function* () {
const flags = yield* Cloudflare.Flagship.ReadFlags(app);
return Effect.fn(function* () {
// The edge `evaluate` endpoint can lag a freshly-created flag,
// and each read can land on a different edge; poll until
// every read returns the flag's true value (bounded).
const { enabled, details, untyped } = yield* poll({
description: "evaluate returns the flag value",
effect: Effect.all({
enabled: flags.getBooleanValue(key, false),
details: flags.getBooleanDetails(key, false),
untyped: flags.get(key),
}),
predicate: (read) =>
read.enabled === true && read.details.value === true && read.untyped === true,
schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]),
});
// A boolean flag read as a string falls back to the default.
const asString = yield* flags.getStringValue(key, "fallback");
return { enabled, details, asString, untyped };
});
}).pipe(Effect.provide(Cloudflare.Flagship.ReadFlagsLocal)),
);
return yield* Read({});
}),
);
expect(out.enabled).toBe(true);
expect(out.details.flagKey).toBe(key);
expect(out.details.value).toBe(true);
expect(out.details.variant).toBe("on");
expect(out.asString).toBe("fallback");
expect(out.untyped).toBe(true);
yield* stack.destroy();
}).pipe(logLevel),
{ tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"], timeout: 120_000 },
);