From f876f7db64ee52228d14438b5e2ef742740fbd56 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Mar 2026 23:12:07 +0000 Subject: [PATCH] =?UTF-8?q?test:=20util=20=E2=80=94=20proxy=20detection=20?= =?UTF-8?q?and=20lazy=20error=20recovery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add tests for proxied() corporate proxy detection (6 tests) and lazy() error recovery + reset behavior (2 tests) to cover untested code paths that affect package installation and initialization. Co-Authored-By: Claude Opus 4.6 (1M context) https://claude.ai/code/session_01EDCRjjHdb1dWvxyAfrLuhw --- packages/opencode/test/util/lazy.test.ts | 40 ++++++++++++++++ packages/opencode/test/util/proxied.test.ts | 52 +++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 packages/opencode/test/util/proxied.test.ts diff --git a/packages/opencode/test/util/lazy.test.ts b/packages/opencode/test/util/lazy.test.ts index 66a08b7831..cb2c847ff9 100644 --- a/packages/opencode/test/util/lazy.test.ts +++ b/packages/opencode/test/util/lazy.test.ts @@ -47,4 +47,44 @@ describe("util.lazy", () => { expect(lazyNull()).toBe(null) expect(lazyUndefined()).toBe(undefined) }) + + // altimate_change start — error recovery and reset tests + test("retries initialization after a throw (does not cache failures)", () => { + let attempt = 0 + const lazyValue = lazy(() => { + attempt++ + if (attempt === 1) throw new Error("init failed") + return "recovered" + }) + + expect(() => lazyValue()).toThrow("init failed") + expect(attempt).toBe(1) + + // Second call should retry, not return cached error + const result = lazyValue() + expect(result).toBe("recovered") + expect(attempt).toBe(2) + + // Third call should use cached value + const result2 = lazyValue() + expect(result2).toBe("recovered") + expect(attempt).toBe(2) + }) + + test("reset() allows re-initialization", () => { + let callCount = 0 + const lazyValue = lazy(() => { + callCount++ + return callCount + }) + + expect(lazyValue()).toBe(1) + expect(lazyValue()).toBe(1) + + lazyValue.reset() + + expect(lazyValue()).toBe(2) + expect(lazyValue()).toBe(2) + }) + // altimate_change end }) diff --git a/packages/opencode/test/util/proxied.test.ts b/packages/opencode/test/util/proxied.test.ts new file mode 100644 index 0000000000..a268bb6204 --- /dev/null +++ b/packages/opencode/test/util/proxied.test.ts @@ -0,0 +1,52 @@ +// altimate_change start — tests for proxied() corporate proxy detection +import { describe, test, expect, beforeEach, afterEach } from "bun:test" +import { proxied } from "../../src/util/proxied" + +describe("proxied(): corporate proxy detection", () => { + const PROXY_VARS = ["HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy"] + const saved: Record = {} + + beforeEach(() => { + for (const v of PROXY_VARS) { + saved[v] = process.env[v] + delete process.env[v] + } + }) + + afterEach(() => { + for (const v of PROXY_VARS) { + if (saved[v] !== undefined) process.env[v] = saved[v] + else delete process.env[v] + } + }) + + test("returns false when no proxy env vars are set", () => { + expect(proxied()).toBe(false) + }) + + test("returns true when HTTP_PROXY is set", () => { + process.env.HTTP_PROXY = "http://proxy.corp.com:8080" + expect(proxied()).toBe(true) + }) + + test("returns true when HTTPS_PROXY is set", () => { + process.env.HTTPS_PROXY = "http://proxy.corp.com:8443" + expect(proxied()).toBe(true) + }) + + test("returns true when lowercase http_proxy is set", () => { + process.env.http_proxy = "http://proxy.corp.com:8080" + expect(proxied()).toBe(true) + }) + + test("returns true when lowercase https_proxy is set", () => { + process.env.https_proxy = "http://proxy.corp.com:8443" + expect(proxied()).toBe(true) + }) + + test("returns false when env var is set to empty string", () => { + process.env.HTTP_PROXY = "" + expect(proxied()).toBe(false) + }) +}) +// altimate_change end