107 lines
3.4 KiB
TypeScript
107 lines
3.4 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
buildClaudeAuthEnv,
|
|
loadConfig,
|
|
resolveAnthropicToken,
|
|
resolveOpenAiApiKey,
|
|
} from "../src/config.js";
|
|
|
|
test("loads defaults and freezes config", () => {
|
|
const config = loadConfig({});
|
|
|
|
assert.equal(config.agentManager.maxConcurrentAgents, 4);
|
|
assert.equal(config.orchestration.maxDepth, 4);
|
|
assert.equal(config.provisioning.portRange.basePort, 36000);
|
|
assert.equal(config.discovery.fileRelativePath, ".agent-context/resources.json");
|
|
assert.equal(config.security.violationHandling, "hard_abort");
|
|
assert.equal(config.security.commandTimeoutMs, 120000);
|
|
assert.equal(config.runtimeEvents.logPath, ".ai_ops/events/runtime-events.ndjson");
|
|
assert.equal(config.runtimeEvents.discordMinSeverity, "critical");
|
|
assert.deepEqual(config.runtimeEvents.discordAlwaysNotifyTypes, [
|
|
"session.started",
|
|
"session.completed",
|
|
"session.failed",
|
|
]);
|
|
assert.equal(config.provider.openAiAuthMode, "auto");
|
|
assert.equal(Object.isFrozen(config), true);
|
|
assert.equal(Object.isFrozen(config.orchestration), true);
|
|
});
|
|
|
|
test("validates OPENAI_AUTH_MODE values", () => {
|
|
assert.throws(
|
|
() => loadConfig({ OPENAI_AUTH_MODE: "oauth" }),
|
|
/OPENAI_AUTH_MODE must be one of/,
|
|
);
|
|
});
|
|
|
|
test("validates boolean env values", () => {
|
|
assert.throws(
|
|
() => loadConfig({ CODEX_SKIP_GIT_CHECK: "maybe" }),
|
|
/must be "true" or "false"/,
|
|
);
|
|
});
|
|
|
|
test("validates security violation mode", () => {
|
|
assert.throws(
|
|
() => loadConfig({ AGENT_SECURITY_VIOLATION_MODE: "retry_forever" }),
|
|
/invalid_union|Invalid input/i,
|
|
);
|
|
});
|
|
|
|
test("validates runtime discord severity mode", () => {
|
|
assert.throws(
|
|
() => loadConfig({ AGENT_RUNTIME_DISCORD_MIN_SEVERITY: "verbose" }),
|
|
/Runtime event severity/,
|
|
);
|
|
});
|
|
|
|
test("prefers CLAUDE_CODE_OAUTH_TOKEN over ANTHROPIC_API_KEY", () => {
|
|
const config = loadConfig({
|
|
CLAUDE_CODE_OAUTH_TOKEN: "oauth-token",
|
|
ANTHROPIC_API_KEY: "api-key",
|
|
});
|
|
|
|
assert.equal(config.provider.anthropicOauthToken, "oauth-token");
|
|
assert.equal(config.provider.anthropicApiKey, "api-key");
|
|
assert.equal(resolveAnthropicToken(config.provider), "oauth-token");
|
|
|
|
const authEnv = buildClaudeAuthEnv(config.provider);
|
|
assert.equal(authEnv.CLAUDE_CODE_OAUTH_TOKEN, "oauth-token");
|
|
assert.equal(authEnv.ANTHROPIC_API_KEY, undefined);
|
|
});
|
|
|
|
test("falls back to ANTHROPIC_API_KEY when oauth token is absent", () => {
|
|
const config = loadConfig({
|
|
ANTHROPIC_API_KEY: "api-key",
|
|
});
|
|
|
|
assert.equal(config.provider.anthropicOauthToken, undefined);
|
|
assert.equal(config.provider.anthropicApiKey, "api-key");
|
|
assert.equal(resolveAnthropicToken(config.provider), "api-key");
|
|
|
|
const authEnv = buildClaudeAuthEnv(config.provider);
|
|
assert.equal(authEnv.CLAUDE_CODE_OAUTH_TOKEN, undefined);
|
|
assert.equal(authEnv.ANTHROPIC_API_KEY, "api-key");
|
|
});
|
|
|
|
test("resolveOpenAiApiKey respects chatgpt auth mode", () => {
|
|
const config = loadConfig({
|
|
OPENAI_AUTH_MODE: "chatgpt",
|
|
CODEX_API_KEY: "codex-key",
|
|
OPENAI_API_KEY: "openai-key",
|
|
});
|
|
|
|
assert.equal(resolveOpenAiApiKey(config.provider), undefined);
|
|
});
|
|
|
|
test("resolveOpenAiApiKey prefers CODEX_API_KEY in auto mode", () => {
|
|
const config = loadConfig({
|
|
OPENAI_AUTH_MODE: "auto",
|
|
CODEX_API_KEY: "codex-key",
|
|
OPENAI_API_KEY: "openai-key",
|
|
});
|
|
|
|
assert.equal(resolveOpenAiApiKey(config.provider), "codex-key");
|
|
});
|