forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui-runtime.ts
More file actions
64 lines (58 loc) · 2.08 KB
/
tui-runtime.ts
File metadata and controls
64 lines (58 loc) · 2.08 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
import { spyOn } from "bun:test"
import path from "path"
import { createBindingLookup } from "@opentui/keymap/extras"
import { TuiConfig } from "../../src/cli/cmd/tui/config/tui"
import { TuiKeybind } from "../../src/cli/cmd/tui/config/keybind"
type PluginSpec = string | [string, Record<string, unknown>]
type ResolvedInput = Omit<TuiConfig.Resolved, "attention" | "keybinds" | "leader_timeout"> & {
attention?: Partial<TuiConfig.Resolved["attention"]>
keybinds?: Partial<TuiKeybind.Keybinds>
leader_timeout?: number
}
export function createTuiResolvedKeybinds(input: Partial<TuiKeybind.Keybinds> = {}): TuiConfig.Resolved["keybinds"] {
const keybinds = TuiKeybind.Keybinds.parse(input)
return createBindingLookup(TuiKeybind.toBindingConfig(keybinds), {
commandMap: TuiKeybind.CommandMap,
bindingDefaults: TuiKeybind.bindingDefaults(),
})
}
export function createTuiResolvedConfig(input: ResolvedInput = {}): TuiConfig.Resolved {
const keybinds = TuiKeybind.Keybinds.parse(input.keybinds ?? {})
return {
...input,
attention: {
enabled: false,
notifications: true,
sound: true,
volume: 0.4,
sound_pack: "opencode.default",
sounds: {},
...input.attention,
},
keybinds: createTuiResolvedKeybinds(keybinds),
leader_timeout: input.leader_timeout ?? 2000,
}
}
export function mockTuiRuntime(dir: string, plugin: PluginSpec[], opts?: { plugin_enabled?: Record<string, boolean> }) {
process.env.OPENCODE_PLUGIN_META_FILE = path.join(dir, "plugin-meta.json")
const plugin_origins = plugin.map((spec) => ({
spec,
scope: "local" as const,
source: path.join(dir, "tui.json"),
}))
const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
const cwd = spyOn(process, "cwd").mockImplementation(() => dir)
const config = createTuiResolvedConfig({
plugin,
plugin_origins,
...(opts?.plugin_enabled && { plugin_enabled: opts.plugin_enabled }),
})
return {
config,
restore: () => {
cwd.mockRestore()
wait.mockRestore()
delete process.env.OPENCODE_PLUGIN_META_FILE
},
}
}