Skip to content

Commit f876f7d

Browse files
committed
test: util — proxy detection and lazy error recovery
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) <[email protected]> https://claude.ai/code/session_01EDCRjjHdb1dWvxyAfrLuhw
1 parent 9ba2114 commit f876f7d

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎packages/opencode/test/util/lazy.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,44 @@ describe("util.lazy", () => {
4747
expect(lazyNull()).toBe(null)
4848
expect(lazyUndefined()).toBe(undefined)
4949
})
50+
51+
// altimate_change start — error recovery and reset tests
52+
test("retries initialization after a throw (does not cache failures)", () => {
53+
let attempt = 0
54+
const lazyValue = lazy(() => {
55+
attempt++
56+
if (attempt === 1) throw new Error("init failed")
57+
return "recovered"
58+
})
59+
60+
expect(() => lazyValue()).toThrow("init failed")
61+
expect(attempt).toBe(1)
62+
63+
// Second call should retry, not return cached error
64+
const result = lazyValue()
65+
expect(result).toBe("recovered")
66+
expect(attempt).toBe(2)
67+
68+
// Third call should use cached value
69+
const result2 = lazyValue()
70+
expect(result2).toBe("recovered")
71+
expect(attempt).toBe(2)
72+
})
73+
74+
test("reset() allows re-initialization", () => {
75+
let callCount = 0
76+
const lazyValue = lazy(() => {
77+
callCount++
78+
return callCount
79+
})
80+
81+
expect(lazyValue()).toBe(1)
82+
expect(lazyValue()).toBe(1)
83+
84+
lazyValue.reset()
85+
86+
expect(lazyValue()).toBe(2)
87+
expect(lazyValue()).toBe(2)
88+
})
89+
// altimate_change end
5090
})
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// altimate_change start — tests for proxied() corporate proxy detection
2+
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
3+
import { proxied } from "../../src/util/proxied"
4+
5+
describe("proxied(): corporate proxy detection", () => {
6+
const PROXY_VARS = ["HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy"]
7+
const saved: Record<string, string | undefined> = {}
8+
9+
beforeEach(() => {
10+
for (const v of PROXY_VARS) {
11+
saved[v] = process.env[v]
12+
delete process.env[v]
13+
}
14+
})
15+
16+
afterEach(() => {
17+
for (const v of PROXY_VARS) {
18+
if (saved[v] !== undefined) process.env[v] = saved[v]
19+
else delete process.env[v]
20+
}
21+
})
22+
23+
test("returns false when no proxy env vars are set", () => {
24+
expect(proxied()).toBe(false)
25+
})
26+
27+
test("returns true when HTTP_PROXY is set", () => {
28+
process.env.HTTP_PROXY = "http://proxy.corp.com:8080"
29+
expect(proxied()).toBe(true)
30+
})
31+
32+
test("returns true when HTTPS_PROXY is set", () => {
33+
process.env.HTTPS_PROXY = "http://proxy.corp.com:8443"
34+
expect(proxied()).toBe(true)
35+
})
36+
37+
test("returns true when lowercase http_proxy is set", () => {
38+
process.env.http_proxy = "http://proxy.corp.com:8080"
39+
expect(proxied()).toBe(true)
40+
})
41+
42+
test("returns true when lowercase https_proxy is set", () => {
43+
process.env.https_proxy = "http://proxy.corp.com:8443"
44+
expect(proxied()).toBe(true)
45+
})
46+
47+
test("returns false when env var is set to empty string", () => {
48+
process.env.HTTP_PROXY = ""
49+
expect(proxied()).toBe(false)
50+
})
51+
})
52+
// altimate_change end

0 commit comments

Comments
 (0)