forked from khalilgharbaoui/opencode-claude-code-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-startup-diagnostics.ts
More file actions
185 lines (172 loc) · 6.19 KB
/
Copy pathtest-startup-diagnostics.ts
File metadata and controls
185 lines (172 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import assert from "node:assert/strict"
import * as fs from "node:fs"
import * as os from "node:os"
import * as path from "node:path"
import { test } from "node:test"
import { claudeCodeProviders } from "./src/index.js"
import { resolveSpawnCwdFrom } from "./src/runtime-status.js"
import {
collectStartupDiagnostics,
describeSpawnCwd,
detectOpencodeVersion,
pickOpencodeVersion,
pluginVersion,
resetOpencodeVersionProbe,
} from "./src/startup-diagnostics.js"
test("pluginVersion reads the real package manifest", () => {
const version = pluginVersion()
assert.match(version, /^\d+\.\d+\.\d+/)
})
test("describeSpawnCwd reports which branch resolveSpawnCwd would take", () => {
assert.deepEqual(describeSpawnCwd("/pinned", "/live", "/captured"), {
resolved: "/pinned",
source: "configured",
})
assert.deepEqual(describeSpawnCwd(undefined, "/live/dir", "/captured"), {
resolved: "/live/dir",
source: "process",
})
// The macOS GUI-launch fingerprint from issue #4: process.cwd() is "/".
assert.deepEqual(describeSpawnCwd(undefined, "/", "/captured/dir"), {
resolved: "/captured/dir",
source: "captured",
})
assert.deepEqual(describeSpawnCwd(undefined, "/", undefined), {
resolved: "/",
source: "unresolved",
})
})
test("describeSpawnCwd never disagrees with resolveSpawnCwd", () => {
const cases: Array<[string | undefined, string, string | undefined]> = [
["/pinned", "/live", "/captured"],
[undefined, "/live/dir", "/captured"],
[undefined, "/", "/captured/dir"],
[undefined, "/", undefined],
]
for (const [configured, live, captured] of cases) {
assert.equal(
describeSpawnCwd(configured, live, captured).resolved,
resolveSpawnCwdFrom(configured, live, captured),
)
}
})
test("pickOpencodeVersion probes known shapes and degrades to undefined", () => {
assert.equal(pickOpencodeVersion({ app: { version: "1.17.0" } }), "1.17.0")
assert.equal(pickOpencodeVersion({ version: "1.17.0" }), "1.17.0")
assert.equal(pickOpencodeVersion({ app: {} }), undefined)
assert.equal(pickOpencodeVersion({ app: { version: "" } }), undefined)
assert.equal(pickOpencodeVersion(undefined), undefined)
assert.equal(pickOpencodeVersion("nope"), undefined)
})
test("claudeCodeProviders keeps only this plugin's providers", () => {
const providers = claudeCodeProviders({
"claude-code": { options: { cliPath: "claude" } },
"claude-code-work": { options: { account: "work" } },
anthropic: { options: { cliPath: "not-ours" } },
"github-copilot": {},
})
assert.deepEqual(Object.keys(providers).sort(), [
"claude-code",
"claude-code-work",
])
})
test("collectStartupDiagnostics summarizes account providers", () => {
const diagnostics = collectStartupDiagnostics(
{
"claude-code-work": {
options: {
account: "work",
cliPath: "/tmp/claude-work",
cwd: "/pinned/dir",
proxyTools: ["Bash", "Task"],
},
},
"claude-code-personal": {
options: { account: "personal", cliPath: "/tmp/claude-personal" },
},
},
"1.17.0",
)
assert.equal(diagnostics.opencode, "1.17.0")
assert.equal(diagnostics.claudeCliPath, "/tmp/claude-work")
assert.deepEqual(diagnostics.accounts, ["work", "personal"])
assert.deepEqual(diagnostics.proxyTools, ["Bash", "Task"])
assert.deepEqual(diagnostics.cwd, {
resolved: "/pinned/dir",
source: "configured",
})
assert.deepEqual(diagnostics.providers, [
"claude-code-work",
"claude-code-personal",
])
assert.ok(Array.isArray(diagnostics.mcpServers))
})
test("collectStartupDiagnostics falls back when options are absent", () => {
const diagnostics = collectStartupDiagnostics({ "claude-code": {} })
assert.equal(diagnostics.claudeCliPath, "claude")
assert.deepEqual(diagnostics.accounts, [])
assert.deepEqual(diagnostics.proxyTools, [])
assert.equal(diagnostics.cwd.source, "process")
// No opencode version handed in and none in the env → explicit "unknown",
// never a fabricated number.
if (!process.env.OPENCODE_VERSION) {
assert.equal(diagnostics.opencode, "unknown")
}
})
test("collectStartupDiagnostics reports interactive transport from env", () => {
const previous = process.env.CLAUDE_CODE_INTERACTIVE_TRANSPORT
try {
delete process.env.CLAUDE_CODE_INTERACTIVE_TRANSPORT
assert.equal(
collectStartupDiagnostics({ "claude-code": {} }).interactiveTransport,
false,
)
assert.equal(
collectStartupDiagnostics({
"claude-code": { options: { interactive: true } },
}).interactiveTransport,
true,
)
process.env.CLAUDE_CODE_INTERACTIVE_TRANSPORT = "1"
assert.equal(
collectStartupDiagnostics({ "claude-code": {} }).interactiveTransport,
true,
)
} finally {
if (previous === undefined) delete process.env.CLAUDE_CODE_INTERACTIVE_TRANSPORT
else process.env.CLAUDE_CODE_INTERACTIVE_TRANSPORT = previous
}
})
test("detectOpencodeVersion reads the version from the opencode binary", async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "oc-version-probe-"))
const fake = path.join(dir, "opencode")
fs.writeFileSync(fake, '#!/bin/sh\necho "1.18.5"\n')
fs.chmodSync(fake, 0o755)
try {
resetOpencodeVersionProbe()
assert.equal(await detectOpencodeVersion(fake), "1.18.5")
// Cached: a second call with a different path reuses the first probe.
assert.equal(await detectOpencodeVersion("/nonexistent/opencode"), "1.18.5")
} finally {
resetOpencodeVersionProbe()
fs.rmSync(dir, { recursive: true, force: true })
}
})
test("detectOpencodeVersion refuses to report a non-opencode execPath", async () => {
try {
// Running from source means execPath is Bun; reporting Bun's version as
// opencode's would be actively misleading, so the probe declines.
resetOpencodeVersionProbe()
assert.equal(await detectOpencodeVersion("/opt/homebrew/bin/bun"), undefined)
} finally {
resetOpencodeVersionProbe()
}
})
test("detectOpencodeVersion returns undefined when the binary fails", async () => {
try {
resetOpencodeVersionProbe()
assert.equal(await detectOpencodeVersion("/nonexistent/dir/opencode"), undefined)
} finally {
resetOpencodeVersionProbe()
}
})