diff --git a/src/auto-reply/envelope.test.ts b/src/auto-reply/envelope.test.ts index 179bd69abbe..69571636282 100644 --- a/src/auto-reply/envelope.test.ts +++ b/src/auto-reply/envelope.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { withEnv } from "../test-utils/env.js"; import { formatAgentEnvelope, formatInboundEnvelope, @@ -7,56 +8,47 @@ import { describe("formatAgentEnvelope", () => { it("includes channel, from, ip, host, and timestamp", () => { - const originalTz = process.env.TZ; - process.env.TZ = "UTC"; + withEnv({ TZ: "UTC" }, () => { + const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z + const body = formatAgentEnvelope({ + channel: "WebChat", + from: "user1", + host: "mac-mini", + ip: "10.0.0.5", + timestamp: ts, + envelope: { timezone: "utc" }, + body: "hello", + }); - const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z - const body = formatAgentEnvelope({ - channel: "WebChat", - from: "user1", - host: "mac-mini", - ip: "10.0.0.5", - timestamp: ts, - envelope: { timezone: "utc" }, - body: "hello", + expect(body).toBe("[WebChat user1 mac-mini 10.0.0.5 Thu 2025-01-02T03:04Z] hello"); }); - - process.env.TZ = originalTz; - - expect(body).toBe("[WebChat user1 mac-mini 10.0.0.5 Thu 2025-01-02T03:04Z] hello"); }); it("formats timestamps in local timezone by default", () => { - const originalTz = process.env.TZ; - process.env.TZ = "America/Los_Angeles"; + withEnv({ TZ: "America/Los_Angeles" }, () => { + const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z + const body = formatAgentEnvelope({ + channel: "WebChat", + timestamp: ts, + body: "hello", + }); - const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z - const body = formatAgentEnvelope({ - channel: "WebChat", - timestamp: ts, - body: "hello", + expect(body).toMatch(/\[WebChat Wed 2025-01-01 19:04 [^\]]+\] hello/); }); - - process.env.TZ = originalTz; - - expect(body).toMatch(/\[WebChat Wed 2025-01-01 19:04 [^\]]+\] hello/); }); it("formats timestamps in UTC when configured", () => { - const originalTz = process.env.TZ; - process.env.TZ = "America/Los_Angeles"; + withEnv({ TZ: "America/Los_Angeles" }, () => { + const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z (19:04 PST) + const body = formatAgentEnvelope({ + channel: "WebChat", + timestamp: ts, + envelope: { timezone: "utc" }, + body: "hello", + }); - const ts = Date.UTC(2025, 0, 2, 3, 4); // 2025-01-02T03:04:00Z (19:04 PST) - const body = formatAgentEnvelope({ - channel: "WebChat", - timestamp: ts, - envelope: { timezone: "utc" }, - body: "hello", + expect(body).toBe("[WebChat Thu 2025-01-02T03:04Z] hello"); }); - - process.env.TZ = originalTz; - - expect(body).toBe("[WebChat Thu 2025-01-02T03:04Z] hello"); }); it("formats timestamps in user timezone when configured", () => { diff --git a/src/config/config.pruning-defaults.test.ts b/src/config/config.pruning-defaults.test.ts index b6a0c4563d3..c37b9ba8f45 100644 --- a/src/config/config.pruning-defaults.test.ts +++ b/src/config/config.pruning-defaults.test.ts @@ -1,6 +1,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import { describe, expect, it } from "vitest"; +import { withEnvAsync } from "../test-utils/env.js"; import { loadConfig } from "./config.js"; import { withTempHome } from "./test-helpers.js"; @@ -16,27 +17,15 @@ async function writeConfigForTest(home: string, config: unknown): Promise describe("config pruning defaults", () => { it("does not enable contextPruning by default", async () => { - const prevApiKey = process.env.ANTHROPIC_API_KEY; - const prevOauthToken = process.env.ANTHROPIC_OAUTH_TOKEN; - process.env.ANTHROPIC_API_KEY = ""; - process.env.ANTHROPIC_OAUTH_TOKEN = ""; - await withTempHome(async (home) => { - await writeConfigForTest(home, { agents: { defaults: {} } }); + await withEnvAsync({ ANTHROPIC_API_KEY: "", ANTHROPIC_OAUTH_TOKEN: "" }, async () => { + await withTempHome(async (home) => { + await writeConfigForTest(home, { agents: { defaults: {} } }); - const cfg = loadConfig(); + const cfg = loadConfig(); - expect(cfg.agents?.defaults?.contextPruning?.mode).toBeUndefined(); + expect(cfg.agents?.defaults?.contextPruning?.mode).toBeUndefined(); + }); }); - if (prevApiKey === undefined) { - delete process.env.ANTHROPIC_API_KEY; - } else { - process.env.ANTHROPIC_API_KEY = prevApiKey; - } - if (prevOauthToken === undefined) { - delete process.env.ANTHROPIC_OAUTH_TOKEN; - } else { - process.env.ANTHROPIC_OAUTH_TOKEN = prevOauthToken; - } }); it("enables cache-ttl pruning + 1h heartbeat for Anthropic OAuth", async () => { diff --git a/src/infra/env.test.ts b/src/infra/env.test.ts index ce968a6e47f..42eb0b921cf 100644 --- a/src/infra/env.test.ts +++ b/src/infra/env.test.ts @@ -1,52 +1,31 @@ import { describe, expect, it } from "vitest"; +import { withEnv } from "../test-utils/env.js"; import { isTruthyEnvValue, normalizeZaiEnv } from "./env.js"; describe("normalizeZaiEnv", () => { - function withZaiEnv(env: { zaiApiKey?: string; legacyZaiApiKey?: string }, run: () => void) { - const prevZai = process.env.ZAI_API_KEY; - const prevLegacy = process.env.Z_AI_API_KEY; - if (env.zaiApiKey === undefined) { - delete process.env.ZAI_API_KEY; - } else { - process.env.ZAI_API_KEY = env.zaiApiKey; - } - if (env.legacyZaiApiKey === undefined) { - delete process.env.Z_AI_API_KEY; - } else { - process.env.Z_AI_API_KEY = env.legacyZaiApiKey; - } - try { - run(); - } finally { - if (prevZai === undefined) { - delete process.env.ZAI_API_KEY; - } else { - process.env.ZAI_API_KEY = prevZai; - } - if (prevLegacy === undefined) { - delete process.env.Z_AI_API_KEY; - } else { - process.env.Z_AI_API_KEY = prevLegacy; - } - } - } - it("copies Z_AI_API_KEY to ZAI_API_KEY when missing", () => { - withZaiEnv({ zaiApiKey: "", legacyZaiApiKey: "zai-legacy" }, () => { + withEnv({ ZAI_API_KEY: "", Z_AI_API_KEY: "zai-legacy" }, () => { normalizeZaiEnv(); expect(process.env.ZAI_API_KEY).toBe("zai-legacy"); }); }); it("does not override existing ZAI_API_KEY", () => { - withZaiEnv({ zaiApiKey: "zai-current", legacyZaiApiKey: "zai-legacy" }, () => { + withEnv({ ZAI_API_KEY: "zai-current", Z_AI_API_KEY: "zai-legacy" }, () => { normalizeZaiEnv(); expect(process.env.ZAI_API_KEY).toBe("zai-current"); }); }); it("ignores blank legacy Z_AI_API_KEY values", () => { - withZaiEnv({ zaiApiKey: "", legacyZaiApiKey: " " }, () => { + withEnv({ ZAI_API_KEY: "", Z_AI_API_KEY: " " }, () => { + normalizeZaiEnv(); + expect(process.env.ZAI_API_KEY).toBe(""); + }); + }); + + it("does not copy when legacy Z_AI_API_KEY is unset", () => { + withEnv({ ZAI_API_KEY: "", Z_AI_API_KEY: undefined }, () => { normalizeZaiEnv(); expect(process.env.ZAI_API_KEY).toBe(""); });