test: unit coverage for conductor-env-shim

Refactor lib/conductor-env-shim.ts to export promoteConductorEnv()
so unit tests can manipulate env and call it directly (a bare side-
effect IIFE on import isn't reachable from bun:test once cached).
The on-import IIFE still runs — existing four-entry-point imports
keep working unchanged.

test/conductor-env-shim.test.ts covers all three branches:
GSTACK_FOO present + FOO empty → promotion; FOO already set →
no-overwrite; nothing in env → no-op.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-05-15 17:50:37 -07:00
parent 6d7dad245b
commit 93c8a8a729
2 changed files with 53 additions and 3 deletions
+7 -3
View File
@@ -7,8 +7,12 @@
*
* Import this for its side effect: `import "../lib/conductor-env-shim";`
*/
for (const key of ["ANTHROPIC_API_KEY", "OPENAI_API_KEY"] as const) {
if (!process.env[key] && process.env[`GSTACK_${key}`]) {
process.env[key] = process.env[`GSTACK_${key}`];
export function promoteConductorEnv(): void {
for (const key of ["ANTHROPIC_API_KEY", "OPENAI_API_KEY"] as const) {
if (!process.env[key] && process.env[`GSTACK_${key}`]) {
process.env[key] = process.env[`GSTACK_${key}`];
}
}
}
promoteConductorEnv();
+46
View File
@@ -0,0 +1,46 @@
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import { promoteConductorEnv } from '../lib/conductor-env-shim';
describe('conductor-env-shim', () => {
const KEYS = ['ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'GSTACK_ANTHROPIC_API_KEY', 'GSTACK_OPENAI_API_KEY'] as const;
const saved: Record<string, string | undefined> = {};
beforeEach(() => {
for (const k of KEYS) {
saved[k] = process.env[k];
delete process.env[k];
}
});
afterEach(() => {
for (const k of KEYS) {
if (saved[k] === undefined) delete process.env[k];
else process.env[k] = saved[k];
}
});
test('promotes GSTACK_ANTHROPIC_API_KEY to ANTHROPIC_API_KEY when canonical is empty', () => {
process.env.GSTACK_ANTHROPIC_API_KEY = 'sk-ant-test-123';
promoteConductorEnv();
expect(process.env.ANTHROPIC_API_KEY).toBe('sk-ant-test-123');
});
test('promotes GSTACK_OPENAI_API_KEY to OPENAI_API_KEY when canonical is empty', () => {
process.env.GSTACK_OPENAI_API_KEY = 'sk-oai-test-456';
promoteConductorEnv();
expect(process.env.OPENAI_API_KEY).toBe('sk-oai-test-456');
});
test('does not overwrite canonical when both canonical and GSTACK_-prefixed are set', () => {
process.env.ANTHROPIC_API_KEY = 'sk-ant-original';
process.env.GSTACK_ANTHROPIC_API_KEY = 'sk-ant-prefixed';
promoteConductorEnv();
expect(process.env.ANTHROPIC_API_KEY).toBe('sk-ant-original');
});
test('no-op when neither canonical nor GSTACK_-prefixed are set', () => {
promoteConductorEnv();
expect(process.env.ANTHROPIC_API_KEY).toBeUndefined();
expect(process.env.OPENAI_API_KEY).toBeUndefined();
});
});