Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions desktop/src/main/__tests__/pty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { homedir, platform } from "node:os"
import type { IPty } from "node-pty"
import { describe, expect, it, vi } from "vitest"
import { PtyManager } from "../pty.js"

describe("PtyManager", () => {
it("starts local shells in the user's home directory", () => {
const proc = {
onData: vi.fn(),
onExit: vi.fn(),
} as unknown as IPty
const spawnPty = vi.fn(() => proc)
const manager = new PtyManager({
binaryPath: "/devsy",
getMainWindow: () => null,
spawnPty,
})

manager.createSession(80, 24)

expect(spawnPty).toHaveBeenCalledWith(
platform() === "win32"
? "powershell.exe"
: process.env.SHELL || "/bin/sh",
[],
expect.objectContaining({ cwd: homedir(), cols: 80, rows: 24 }),
)
})
})
8 changes: 5 additions & 3 deletions desktop/src/main/pty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { platform } from "node:os"
import { homedir, platform } from "node:os"
import { dirname } from "node:path"
import type { BrowserWindow } from "electron"
import type { IPty } from "node-pty"
Expand All @@ -25,6 +25,7 @@ function requirePty(): typeof import("node-pty") {
interface PtyDeps {
binaryPath: string
getMainWindow: () => BrowserWindow | null
spawnPty?: typeof import("node-pty").spawn
}

export class PtyManager {
Expand Down Expand Up @@ -60,15 +61,16 @@ export class PtyManager {
}

createSession(cols: number, rows: number): string {
const pty = requirePty()
const spawn = this.deps.spawnPty ?? requirePty().spawn
const shell =
platform() === "win32" ? "powershell.exe" : process.env.SHELL || "/bin/sh"

const sessionId = crypto.randomUUID()
const proc = pty.spawn(shell, [], {
const proc = spawn(shell, [], {
name: "xterm-256color",
cols,
rows,
cwd: homedir(),
env: this.env,
})

Expand Down
Loading