import * as NodeServices from "@effect/platform-node/NodeServices";
import { describe, expect, it } from "@effect/vitest";
import { SecretProviderId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Path from "effect/Path";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { mergeProviderInstanceEnvironment } from "./instanceEnvironment.ts";
describe("mergeProviderInstanceEnvironment", () => {
it.effect.each([
{ value: "~/.account", tail: ".account" },
{ value: "~\\.account\\work", tail: ".account\\work" },
])("expands configured provider homes set to $value", ({ value, tail }) =>
Effect.gen(function* () {
const path = yield* Path.Path;
const baseEnv = {
CODEX_HOME: "~/.inherited-codex",
CLAUDE_CONFIG_DIR: "~/.inherited-claude",
};
const environment = yield* mergeProviderInstanceEnvironment(
[
{ name: "CODEX_HOME", value, sensitive: false },
{ name: "CLAUDE_CONFIG_DIR", value, sensitive: false },
{ name: "CUSTOM_VALUE", value, sensitive: false },
],
baseEnv,
);
expect(environment).toEqual({
CODEX_HOME: path.join("/home/ada", tail),
CLAUDE_CONFIG_DIR: path.join("/home/ada", tail),
CUSTOM_VALUE: value,
});
expect(baseEnv).toEqual({
CODEX_HOME: "~/.inherited-codex",
CLAUDE_CONFIG_DIR: "~/.inherited-claude",
});
}).pipe(
Effect.provideService(HostProcess.HomeDirectory, "/home/ada"),
Effect.provide(NodeServices.layer),
),
);
it.effect("leaves inherited provider homes unchanged", () =>
Effect.gen(function* () {
const baseEnv = { CODEX_HOME: "~/.codex", CLAUDE_CONFIG_DIR: "~\\.claude" };
expect(
yield* mergeProviderInstanceEnvironment(
[{ name: "CUSTOM_VALUE", value: "~/.custom", sensitive: false }],
baseEnv,
),
).toEqual({ ...baseEnv, CUSTOM_VALUE: "~/.custom" });
}),
);
it.effect("overrides inherited environment values and preserves empty strings", () =>
Effect.gen(function* () {
expect(
yield* mergeProviderInstanceEnvironment(
[
{ name: "OPENROUTER_API_KEY", value: "sk-or-test", sensitive: true },
{ name: "ANTHROPIC_API_KEY", value: "", sensitive: false },
{
name: "OPENAI_API_KEY",
value: {
provider: SecretProviderId.make("personal"),
reference: "op://Private/OpenAI/credential",
},
sensitive: true,
},
],
{ ANTHROPIC_API_KEY: "inherited", OPENAI_API_KEY: "inherited", PATH: "/bin" },
),
).toMatchObject({
OPENROUTER_API_KEY: "sk-or-test",
ANTHROPIC_API_KEY: "",
OPENAI_API_KEY: "inherited",
PATH: "/bin",
});
}),
);
it.effect("replaces every inherited spelling of a name on Windows, and only there", () =>
Effect.gen(function* () {
const merge = (platform: NodeJS.Platform) =>
mergeProviderInstanceEnvironment(
[{ name: "Https_Proxy", value: "http://gateway", sensitive: false }],
{ HTTPS_PROXY: "http://mine", PATH: "/bin" },
).pipe(Effect.provideService(HostProcess.Platform, platform));
// A home variable in another spelling is still expanded.
expect(
yield* mergeProviderInstanceEnvironment(
[{ name: "Codex_Home", value: "~/corp", sensitive: false }],
{},
).pipe(
Effect.provideService(HostProcess.Platform, "win32"),
Effect.provideService(HostProcess.HomeDirectory, "/home/ada"),
),
).toEqual({ CODEX_HOME: "/home/ada/corp" });
// Upper case sorts first, so it also wins where a spawn merges the parent environment back.
expect(yield* merge("win32")).toEqual({ HTTPS_PROXY: "http://gateway", PATH: "/bin" });
expect(yield* merge("linux")).toEqual({
HTTPS_PROXY: "http://mine",
Https_Proxy: "http://gateway",
PATH: "/bin",
});
}),
);
});