forked from khalilgharbaoui/opencode-claude-code-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-cwd-resolution.ts
More file actions
117 lines (107 loc) · 4.22 KB
/
test-cwd-resolution.ts
File metadata and controls
117 lines (107 loc) · 4.22 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
import assert from "node:assert/strict"
import { test } from "node:test"
import {
getOpencodeProjectDirectory,
isUsableDirectory,
resolveSpawnCwd,
resolveSpawnCwdFrom,
setOpencodeProjectDirectory,
} from "./src/runtime-status.js"
function withCapturedDirectory<T>(value: string | undefined, fn: () => T): T {
const previous = getOpencodeProjectDirectory()
try {
setOpencodeProjectDirectory(value)
return fn()
} finally {
setOpencodeProjectDirectory(previous)
}
}
test("isUsableDirectory rejects /, empty, single chars, and non-strings", () => {
assert.equal(isUsableDirectory("/"), false)
assert.equal(isUsableDirectory(""), false)
assert.equal(isUsableDirectory("x"), false)
assert.equal(isUsableDirectory(undefined), false)
assert.equal(isUsableDirectory(null), false)
assert.equal(isUsableDirectory(42), false)
assert.equal(isUsableDirectory("/x"), true)
assert.equal(isUsableDirectory("/Users/jessie/projects/foo"), true)
})
test("explicit configured value wins over live and captured", () => {
assert.equal(
resolveSpawnCwdFrom("/explicit", "/Users/me/proj", "/Users/me/other"),
"/explicit",
)
// User override remains absolute even when it's "/". They asked for it.
assert.equal(resolveSpawnCwdFrom("/", "/Users/me/proj", "/Users/me/other"), "/")
})
test("live process.cwd() preferred when it's a usable directory", () => {
// Terminal launch: process.cwd() is the project dir, no captured needed.
assert.equal(
resolveSpawnCwdFrom(undefined, "/Users/me/proj", undefined),
"/Users/me/proj",
)
// Live wins over a captured value too — lazy resolution honors opencode
// workspace switches via chdir, even when we have a stale captured init dir.
assert.equal(
resolveSpawnCwdFrom(undefined, "/Users/me/now", "/Users/me/then"),
"/Users/me/now",
)
})
test("captured directory rescues macOS GUI launches at /", () => {
assert.equal(
resolveSpawnCwdFrom(undefined, "/", "/Users/jessie/projects/svelte-monorepo"),
"/Users/jessie/projects/svelte-monorepo",
)
})
test("falls through to live when neither configured nor captured is usable", () => {
// Both unavailable: degrade gracefully to live, even if that's "/".
// Caller sees the same value process.cwd() would have returned, so nothing
// worse than pre-fix behavior.
assert.equal(resolveSpawnCwdFrom(undefined, "/", undefined), "/")
assert.equal(resolveSpawnCwdFrom(undefined, "", undefined), "")
})
test("empty configured string falls through to the rest of the chain", () => {
// Defensive: a corrupt or empty options.cwd shouldn't pin Claude to ""
// when a real live cwd is available.
assert.equal(
resolveSpawnCwdFrom("", "/Users/me/proj", "/Users/me/captured"),
"/Users/me/proj",
)
assert.equal(
resolveSpawnCwdFrom("", "/", "/Users/me/captured"),
"/Users/me/captured",
)
})
test("resolveSpawnCwd reads module-level captured state via the setter", () => {
withCapturedDirectory("/Users/jessie/projects/svelte-monorepo", () => {
// Stub process.cwd() temporarily to simulate the GUI-launch case.
const originalCwd = process.cwd
process.cwd = () => "/"
try {
assert.equal(
resolveSpawnCwd(undefined),
"/Users/jessie/projects/svelte-monorepo",
)
// Explicit config still wins.
assert.equal(resolveSpawnCwd("/explicit/override"), "/explicit/override")
} finally {
process.cwd = originalCwd
}
})
})
test("resolveSpawnCwd returns live cwd when usable, regardless of captured", () => {
withCapturedDirectory("/Users/jessie/projects/captured-at-init", () => {
// Terminal-launched opencode: process.cwd() is the active project.
// Captured value must not override the live one (workspace switching
// depends on this; baking captured into config is what broke #4).
const live = process.cwd()
if (!isUsableDirectory(live)) return // skip if test runner started at /
assert.equal(resolveSpawnCwd(undefined), live)
})
})
test("setter accepts undefined to clear the captured directory", () => {
setOpencodeProjectDirectory("/Users/me/captured")
assert.equal(getOpencodeProjectDirectory(), "/Users/me/captured")
setOpencodeProjectDirectory(undefined)
assert.equal(getOpencodeProjectDirectory(), undefined)
})