refactor(tmux): migrate layout commands to typed runner
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { beforeEach, describe, expect, it, mock } from "bun:test"
|
||||
|
||||
import type { TmuxCommandResult } from "../runner"
|
||||
|
||||
const layoutSpecifier = import.meta.resolve("./layout")
|
||||
const loggerSpecifier = import.meta.resolve("../../logger")
|
||||
const runnerSpecifier = import.meta.resolve("../runner")
|
||||
const tmuxPathResolverSpecifier = import.meta.resolve("../../../tools/interactive-bash/tmux-path-resolver")
|
||||
|
||||
const runTmuxCommandMock = mock(async (): Promise<TmuxCommandResult> => ({
|
||||
success: true,
|
||||
output: "",
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
}))
|
||||
const getTmuxPathMock = mock(async (): Promise<string | undefined> => "sh")
|
||||
const logMock = mock(() => undefined)
|
||||
|
||||
async function loadEnforceMainPaneWidth(): Promise<typeof import("./layout").enforceMainPaneWidth> {
|
||||
const module = await import(`${layoutSpecifier}?test=${crypto.randomUUID()}`)
|
||||
return module.enforceMainPaneWidth
|
||||
}
|
||||
|
||||
function registerModuleMocks(): void {
|
||||
mock.module(loggerSpecifier, () => ({ log: logMock }))
|
||||
mock.module(runnerSpecifier, () => ({ runTmuxCommand: runTmuxCommandMock }))
|
||||
mock.module(tmuxPathResolverSpecifier, () => ({ getTmuxPath: getTmuxPathMock }))
|
||||
}
|
||||
|
||||
describe("enforceMainPaneWidth runner integration", () => {
|
||||
beforeEach(() => {
|
||||
registerModuleMocks()
|
||||
runTmuxCommandMock.mockClear()
|
||||
getTmuxPathMock.mockClear()
|
||||
logMock.mockClear()
|
||||
|
||||
runTmuxCommandMock.mockResolvedValue({ success: true, output: "", stdout: "", stderr: "", exitCode: 0 })
|
||||
getTmuxPathMock.mockResolvedValue("sh")
|
||||
})
|
||||
|
||||
it("#given pane width inputs #when enforceMainPaneWidth called #then delegates resize-pane to shared runner", async () => {
|
||||
// given
|
||||
const enforceMainPaneWidth = await loadEnforceMainPaneWidth()
|
||||
|
||||
// when
|
||||
await enforceMainPaneWidth("%42", 200, 60)
|
||||
|
||||
// then
|
||||
expect(runTmuxCommandMock.mock.calls).toEqual([
|
||||
[[expect.any(String), ["resize-pane", "-t", "%42", "-x", "119"]]][0],
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,3 @@
|
||||
import { spawn } from "../../bun-spawn-shim"
|
||||
import type { TmuxLayout } from "../../../config/schema"
|
||||
import { getTmuxPath } from "../../../tools/interactive-bash/tmux-path-resolver"
|
||||
|
||||
@@ -46,7 +45,12 @@ export async function applyLayout(
|
||||
mainPaneSize: number,
|
||||
deps?: LayoutDeps,
|
||||
): Promise<void> {
|
||||
const spawnCommand: TmuxSpawnCommand = deps?.spawnCommand ?? spawn
|
||||
const spawnCommand: TmuxSpawnCommand = deps?.spawnCommand ?? ((args) => ({
|
||||
exited: (async () => {
|
||||
const { runTmuxCommand } = await import("../runner")
|
||||
return (await runTmuxCommand(args[0] ?? "", args.slice(1))).exitCode
|
||||
})(),
|
||||
}))
|
||||
const layoutProc = spawnCommand([tmux, "select-layout", layout], {
|
||||
stdout: "ignore",
|
||||
stderr: "ignore",
|
||||
@@ -78,12 +82,9 @@ export async function enforceMainPaneWidth(
|
||||
? { mainPaneSize: mainPaneSizeOrOptions }
|
||||
: mainPaneSizeOrOptions ?? {}
|
||||
const mainWidth = calculateMainPaneWidth(windowWidth, options)
|
||||
const { runTmuxCommand } = await import("../runner")
|
||||
|
||||
const proc = spawn([tmux, "resize-pane", "-t", mainPaneId, "-x", String(mainWidth)], {
|
||||
stdout: "ignore",
|
||||
stderr: "ignore",
|
||||
})
|
||||
await proc.exited
|
||||
await runTmuxCommand(tmux, ["resize-pane", "-t", mainPaneId, "-x", String(mainWidth)])
|
||||
|
||||
log("[enforceMainPaneWidth] main pane resized", {
|
||||
mainPaneId,
|
||||
|
||||
Reference in New Issue
Block a user