t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/Auth/Env.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

76 lines
2.2 KiB
TypeScript

import {
getEnv,
getEnvRedacted,
getEnvRedactedRequired,
getEnvRequired,
mapPromptCancellation,
} from "@/Auth/Env.ts";
import { AuthError } from "@/Auth/AuthProvider.ts";
import { TerminalCancelled } from "@/Cli/CliKit/index.ts";
import { expect, it } from "alchemy-test";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import * as Redacted from "effect/Redacted";
const provideConfig = (values: Record<string, unknown>) =>
Effect.provideService(
ConfigProvider.ConfigProvider,
ConfigProvider.fromUnknown(values),
);
it.effect(
"reads optional environment configuration",
() =>
Effect.gen(function* () {
expect(yield* getEnv("PRESENT")).toBe("value");
expect(yield* getEnv("MISSING")).toBeUndefined();
}).pipe(provideConfig({ PRESENT: "value" })),
{ tags: ["unit", "local"] },
);
it.effect(
"preserves the configuration error as the cause of AuthError",
() =>
getEnvRequired("MISSING").pipe(
Effect.flip,
Effect.tap((error) =>
Effect.sync(() => {
expect(error).toBeInstanceOf(AuthError);
expect(error.message).toBe("Missing required env: MISSING");
expect(error.cause).toBeDefined();
}),
),
provideConfig({}),
),
{ tags: ["unit", "local"] },
);
it.effect(
"handles optional and required redacted configuration",
() =>
Effect.gen(function* () {
const present = yield* getEnvRedactedRequired("SECRET");
expect(Redacted.value(present)).toBe("secret");
expect(yield* getEnvRedacted("MISSING")).toBeUndefined();
}).pipe(provideConfig({ SECRET: "secret" })),
{ tags: ["unit", "local"] },
);
it.effect(
"maps prompt cancellation without retrying the prompt",
() =>
Effect.gen(function* () {
let attempts = 0;
const error = yield* mapPromptCancellation(
Effect.suspend(() => {
attempts++;
return Effect.fail(new TerminalCancelled());
}),
).pipe(Effect.flip);
expect(attempts).toBe(1);
expect(error).toBeInstanceOf(AuthError);
expect(error.cause).toBeInstanceOf(TerminalCancelled);
}),
{ tags: ["unit", "local"] },
);