t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/Cloudflare/Flagship/ReadFlagsLocal.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

95 lines
3.6 KiB
TypeScript

import { Action } from "@/Action";
import * as Cloudflare from "@/Cloudflare";
import * as Test from "@/Test/Alchemy";
import { poll } from "@/Util/poll.ts";
import { expect } from "alchemy-test";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
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 = "alchemy-test-flag-read-local";
const out = yield* stack.deploy(
Effect.gen(function* () {
const app = yield* Cloudflare.Flagship.App("ReadFlagsLocalApp", {
name: "alchemy-test-flagship-read-local",
});
// 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,
},
);