Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

instanceEnvironment.test.ts

111 lines4.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import { SecretProviderId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Path from "effect/Path";
6import * as HostProcess from "@t3tools/shared/HostProcess";
7​
8import { mergeProviderInstanceEnvironment } from "./instanceEnvironment.ts";
9​
10describe("mergeProviderInstanceEnvironment", () => {
11 it.effect.each([
12 { value: "~/.account", tail: ".account" },
13 { value: "~\\.account\\work", tail: ".account\\work" },
14 ])("expands configured provider homes set to $value", ({ value, tail }) =>
15 Effect.gen(function* () {
16 const path = yield* Path.Path;
17 const baseEnv = {
18 CODEX_HOME: "~/.inherited-codex",
19 CLAUDE_CONFIG_DIR: "~/.inherited-claude",
20 };
21 const environment = yield* mergeProviderInstanceEnvironment(
22 [
23 { name: "CODEX_HOME", value, sensitive: false },
24 { name: "CLAUDE_CONFIG_DIR", value, sensitive: false },
25 { name: "CUSTOM_VALUE", value, sensitive: false },
26 ],
27 baseEnv,
28 );
29​
30 expect(environment).toEqual({
31 CODEX_HOME: path.join("/home/ada", tail),
32 CLAUDE_CONFIG_DIR: path.join("/home/ada", tail),
33 CUSTOM_VALUE: value,
34 });
35 expect(baseEnv).toEqual({
36 CODEX_HOME: "~/.inherited-codex",
37 CLAUDE_CONFIG_DIR: "~/.inherited-claude",
38 });
39 }).pipe(
40 Effect.provideService(HostProcess.HomeDirectory, "/home/ada"),
41 Effect.provide(NodeServices.layer),
42 ),
43 );
44​
45 it.effect("leaves inherited provider homes unchanged", () =>
46 Effect.gen(function* () {
47 const baseEnv = { CODEX_HOME: "~/.codex", CLAUDE_CONFIG_DIR: "~\\.claude" };
48​
49 expect(
50 yield* mergeProviderInstanceEnvironment(
51 [{ name: "CUSTOM_VALUE", value: "~/.custom", sensitive: false }],
52 baseEnv,
53 ),
54 ).toEqual({ ...baseEnv, CUSTOM_VALUE: "~/.custom" });
55 }),
56 );
57​
58 it.effect("overrides inherited environment values and preserves empty strings", () =>
59 Effect.gen(function* () {
60 expect(
61 yield* mergeProviderInstanceEnvironment(
62 [
63 { name: "OPENROUTER_API_KEY", value: "sk-or-test", sensitive: true },
64 { name: "ANTHROPIC_API_KEY", value: "", sensitive: false },
65 {
66 name: "OPENAI_API_KEY",
67 value: {
68 provider: SecretProviderId.make("personal"),
69 reference: "op://Private/OpenAI/credential",
70 },
71 sensitive: true,
72 },
73 ],
74 { ANTHROPIC_API_KEY: "inherited", OPENAI_API_KEY: "inherited", PATH: "/bin" },
75 ),
76 ).toMatchObject({
77 OPENROUTER_API_KEY: "sk-or-test",
78 ANTHROPIC_API_KEY: "",
79 OPENAI_API_KEY: "inherited",
80 PATH: "/bin",
81 });
82 }),
83 );
84​
85 it.effect("replaces every inherited spelling of a name on Windows, and only there", () =>
86 Effect.gen(function* () {
87 const merge = (platform: NodeJS.Platform) =>
88 mergeProviderInstanceEnvironment(
89 [{ name: "Https_Proxy", value: "http://gateway", sensitive: false }],
90 { HTTPS_PROXY: "http://mine", PATH: "/bin" },
91 ).pipe(Effect.provideService(HostProcess.Platform, platform));
92 // A home variable in another spelling is still expanded.
93 expect(
94 yield* mergeProviderInstanceEnvironment(
95 [{ name: "Codex_Home", value: "~/corp", sensitive: false }],
96 {},
97 ).pipe(
98 Effect.provideService(HostProcess.Platform, "win32"),
99 Effect.provideService(HostProcess.HomeDirectory, "/home/ada"),
100 ),
101 ).toEqual({ CODEX_HOME: "/home/ada/corp" });
102 // Upper case sorts first, so it also wins where a spawn merges the parent environment back.
103 expect(yield* merge("win32")).toEqual({ HTTPS_PROXY: "http://gateway", PATH: "/bin" });
104 expect(yield* merge("linux")).toEqual({
105 HTTPS_PROXY: "http://mine",
106 Https_Proxy: "http://gateway",
107 PATH: "/bin",
108 });
109 }),
110 );
111});