feat(tmux): add pane-dimensions utility with tests
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
import { beforeEach, describe, expect, it, mock } from "bun:test"
|
||||||
|
|
||||||
|
import type { TmuxCommandResult } from "../runner"
|
||||||
|
|
||||||
|
const paneDimensionsSpecifier = import.meta.resolve("./pane-dimensions")
|
||||||
|
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: "80,160",
|
||||||
|
stdout: "80,160",
|
||||||
|
stderr: "",
|
||||||
|
exitCode: 0,
|
||||||
|
}))
|
||||||
|
const getTmuxPathMock = mock(async (): Promise<string | undefined> => "sh")
|
||||||
|
|
||||||
|
async function loadGetPaneDimensions(): Promise<typeof import("./pane-dimensions").getPaneDimensions> {
|
||||||
|
const module = await import(`${paneDimensionsSpecifier}?test=${crypto.randomUUID()}`)
|
||||||
|
return module.getPaneDimensions
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerModuleMocks(): void {
|
||||||
|
mock.module(runnerSpecifier, () => ({ runTmuxCommand: runTmuxCommandMock }))
|
||||||
|
mock.module(tmuxPathResolverSpecifier, () => ({ getTmuxPath: getTmuxPathMock }))
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("getPaneDimensions runner integration", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
registerModuleMocks()
|
||||||
|
runTmuxCommandMock.mockClear()
|
||||||
|
getTmuxPathMock.mockClear()
|
||||||
|
|
||||||
|
runTmuxCommandMock.mockResolvedValue({ success: true, output: "80,160", stdout: "80,160", stderr: "", exitCode: 0 })
|
||||||
|
getTmuxPathMock.mockResolvedValue("sh")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("#given pane id #when getPaneDimensions called #then delegates display to shared runner", async () => {
|
||||||
|
// given
|
||||||
|
const getPaneDimensions = await loadGetPaneDimensions()
|
||||||
|
|
||||||
|
// when
|
||||||
|
const result = await getPaneDimensions("%42")
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(result).toEqual({ paneWidth: 80, windowWidth: 160 })
|
||||||
|
expect(runTmuxCommandMock.mock.calls).toEqual([
|
||||||
|
[[expect.any(String), ["display", "-p", "-t", "%42", "#{pane_width},#{window_width}"]]][0],
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import { spawn } from "../../bun-spawn-shim"
|
|
||||||
import { getTmuxPath } from "../../../tools/interactive-bash/tmux-path-resolver"
|
import { getTmuxPath } from "../../../tools/interactive-bash/tmux-path-resolver"
|
||||||
|
|
||||||
export interface PaneDimensions {
|
export interface PaneDimensions {
|
||||||
@@ -11,17 +10,13 @@ export async function getPaneDimensions(
|
|||||||
): Promise<PaneDimensions | null> {
|
): Promise<PaneDimensions | null> {
|
||||||
const tmux = await getTmuxPath()
|
const tmux = await getTmuxPath()
|
||||||
if (!tmux) return null
|
if (!tmux) return null
|
||||||
|
const { runTmuxCommand } = await import("../runner")
|
||||||
|
|
||||||
const proc = spawn(
|
const result = await runTmuxCommand(tmux, ["display", "-p", "-t", paneId, "#{pane_width},#{window_width}"])
|
||||||
[tmux, "display", "-p", "-t", paneId, "#{pane_width},#{window_width}"],
|
|
||||||
{ stdout: "pipe", stderr: "pipe" },
|
|
||||||
)
|
|
||||||
const exitCode = await proc.exited
|
|
||||||
const stdout = await new Response(proc.stdout).text()
|
|
||||||
|
|
||||||
if (exitCode !== 0) return null
|
if (result.exitCode !== 0) return null
|
||||||
|
|
||||||
const [paneWidth, windowWidth] = stdout.trim().split(",").map(Number)
|
const [paneWidth, windowWidth] = result.output.trim().split(",").map(Number)
|
||||||
if (Number.isNaN(paneWidth) || Number.isNaN(windowWidth)) return null
|
if (Number.isNaN(paneWidth) || Number.isNaN(windowWidth)) return null
|
||||||
|
|
||||||
return { paneWidth, windowWidth }
|
return { paneWidth, windowWidth }
|
||||||
|
|||||||
Reference in New Issue
Block a user