2026-03-27 14:00:26 +00:00
|
|
|
import { spyOn } from "bun:test"
|
|
|
|
|
import path from "path"
|
2026-07-11 23:51:19 +00:00
|
|
|
import { resolve, type Info, type Resolved } from "@opencode-ai/tui/config/v1"
|
2026-06-07 03:27:28 +00:00
|
|
|
import { TuiConfig } from "../../src/config/tui"
|
2026-07-11 23:51:19 +00:00
|
|
|
import { TuiKeybind } from "@opencode-ai/tui/config/v1/keybind"
|
2026-03-27 14:00:26 +00:00
|
|
|
|
|
|
|
|
type PluginSpec = string | [string, Record<string, unknown>]
|
2026-06-07 03:27:28 +00:00
|
|
|
type PluginOrigin = {
|
|
|
|
|
spec: PluginSpec
|
|
|
|
|
scope: "global" | "local"
|
|
|
|
|
source: string
|
|
|
|
|
}
|
|
|
|
|
type HostResolved = Resolved & { plugin_origins?: PluginOrigin[] }
|
|
|
|
|
type ResolvedInput = Omit<Info, "attention" | "keybinds" | "leader_timeout"> & {
|
|
|
|
|
attention?: Partial<Resolved["attention"]>
|
2026-05-08 23:29:13 +00:00
|
|
|
keybinds?: Partial<TuiKeybind.Keybinds>
|
|
|
|
|
leader_timeout?: number
|
2026-06-07 03:27:28 +00:00
|
|
|
plugin_origins?: PluginOrigin[]
|
2026-05-07 18:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-07 03:27:28 +00:00
|
|
|
export function createTuiResolvedKeybinds(input: Partial<TuiKeybind.Keybinds> = {}): Resolved["keybinds"] {
|
|
|
|
|
return resolve({ keybinds: input }, { terminalSuspend: process.platform !== "win32" }).keybinds
|
2026-05-07 18:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-07 03:27:28 +00:00
|
|
|
export function createTuiResolvedConfig(input: ResolvedInput = {}): HostResolved {
|
2026-05-07 18:35:31 +00:00
|
|
|
return {
|
2026-06-07 03:27:28 +00:00
|
|
|
...resolve(input, { terminalSuspend: process.platform !== "win32" }),
|
|
|
|
|
plugin_origins: input.plugin_origins,
|
2026-05-07 18:35:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-27 14:00:26 +00:00
|
|
|
|
2026-04-16 06:03:03 +00:00
|
|
|
export function mockTuiRuntime(dir: string, plugin: PluginSpec[], opts?: { plugin_enabled?: Record<string, boolean> }) {
|
2026-03-27 14:00:26 +00:00
|
|
|
process.env.OPENCODE_PLUGIN_META_FILE = path.join(dir, "plugin-meta.json")
|
2026-04-01 23:50:22 +00:00
|
|
|
const plugin_origins = plugin.map((spec) => ({
|
|
|
|
|
spec,
|
2026-03-30 18:36:21 +00:00
|
|
|
scope: "local" as const,
|
|
|
|
|
source: path.join(dir, "tui.json"),
|
|
|
|
|
}))
|
2026-03-27 14:00:26 +00:00
|
|
|
const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
|
|
|
|
|
const cwd = spyOn(process, "cwd").mockImplementation(() => dir)
|
|
|
|
|
|
2026-05-07 18:35:31 +00:00
|
|
|
const config = createTuiResolvedConfig({
|
2026-04-16 06:03:03 +00:00
|
|
|
plugin,
|
|
|
|
|
plugin_origins,
|
|
|
|
|
...(opts?.plugin_enabled && { plugin_enabled: opts.plugin_enabled }),
|
2026-05-07 18:35:31 +00:00
|
|
|
})
|
2026-04-16 06:03:03 +00:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
config,
|
|
|
|
|
restore: () => {
|
|
|
|
|
cwd.mockRestore()
|
|
|
|
|
wait.mockRestore()
|
|
|
|
|
delete process.env.OPENCODE_PLUGIN_META_FILE
|
|
|
|
|
},
|
2026-03-27 14:00:26 +00:00
|
|
|
}
|
|
|
|
|
}
|