|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | +import { ShellSession } from "../shell/session.js" |
| 3 | +import { spawn, ChildProcess } from "child_process" |
| 4 | +import { EventEmitter } from "events" |
| 5 | + |
| 6 | +// Define mock types for testing |
| 7 | +interface MockStdin { |
| 8 | + write: ReturnType<typeof vi.fn> |
| 9 | + end: ReturnType<typeof vi.fn> |
| 10 | +} |
| 11 | + |
| 12 | +interface MockChildProcess extends EventEmitter { |
| 13 | + stdout: EventEmitter |
| 14 | + stderr: EventEmitter |
| 15 | + stdin: MockStdin |
| 16 | + kill: ReturnType<typeof vi.fn> |
| 17 | +} |
| 18 | + |
| 19 | +// Mock the default-shell module |
| 20 | +vi.mock("default-shell", () => ({ |
| 21 | + detectDefaultShell: vi.fn(() => "/bin/bash"), |
| 22 | +})) |
| 23 | + |
| 24 | +// Mock child_process.spawn |
| 25 | +vi.mock("child_process", () => ({ |
| 26 | + spawn: vi.fn(), |
| 27 | +})) |
| 28 | + |
| 29 | +describe("ShellSession", () => { |
| 30 | + let mockChildProcess: MockChildProcess |
| 31 | + let stdoutEmitter: EventEmitter |
| 32 | + let stderrEmitter: EventEmitter |
| 33 | + let stdin: MockStdin |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks() |
| 37 | + |
| 38 | + // Use EventEmitters for stdout/stderr to easily simulate data events |
| 39 | + stdoutEmitter = new EventEmitter() |
| 40 | + stderrEmitter = new EventEmitter() |
| 41 | + |
| 42 | + stdin = { |
| 43 | + write: vi.fn(), |
| 44 | + end: vi.fn(), |
| 45 | + } |
| 46 | + |
| 47 | + mockChildProcess = new EventEmitter() |
| 48 | + mockChildProcess.stdout = stdoutEmitter |
| 49 | + mockChildProcess.stderr = stderrEmitter |
| 50 | + mockChildProcess.stdin = stdin |
| 51 | + mockChildProcess.kill = vi.fn() |
| 52 | + |
| 53 | + vi.mocked(spawn).mockReturnValue(mockChildProcess as unknown as ChildProcess) |
| 54 | + }) |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + vi.restoreAllMocks() |
| 58 | + }) |
| 59 | + |
| 60 | + describe("initialization", () => { |
| 61 | + it("should create a new shell session with default shell", async () => { |
| 62 | + const session = new ShellSession() |
| 63 | + |
| 64 | + await session.ensureSession("/tmp/test") |
| 65 | + |
| 66 | + expect(spawn).toHaveBeenCalledWith("/bin/bash", [], { |
| 67 | + cwd: "/tmp/test", |
| 68 | + stdio: ["pipe", "pipe", "pipe"], |
| 69 | + env: expect.objectContaining({ |
| 70 | + PS1: "", |
| 71 | + // PROMPT_COMMAND is no longer set in env |
| 72 | + }), |
| 73 | + detached: false, |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + it("should handle shell spawn errors", async () => { |
| 78 | + const session = new ShellSession() |
| 79 | + |
| 80 | + // ensureSession doesn't reject on spawn error because spawn error is async |
| 81 | + // and ensureSession returns immediately. |
| 82 | + // We should verify that the error event is emitted. |
| 83 | + |
| 84 | + const errorPromise = new Promise<void>((resolve, reject) => { |
| 85 | + session.on("error", (err) => { |
| 86 | + try { |
| 87 | + expect(err.message).toBe("Spawn failed") |
| 88 | + resolve() |
| 89 | + } catch (e) { |
| 90 | + reject(e) |
| 91 | + } |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + await session.ensureSession("/tmp/test") |
| 96 | + |
| 97 | + // Simulate spawn error |
| 98 | + mockChildProcess.emit("error", new Error("Spawn failed")) |
| 99 | + |
| 100 | + await errorPromise |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + describe("command execution", () => { |
| 105 | + let session: ShellSession |
| 106 | + |
| 107 | + beforeEach(async () => { |
| 108 | + session = new ShellSession() |
| 109 | + await session.ensureSession("/tmp/test") |
| 110 | + }) |
| 111 | + |
| 112 | + it("should execute commands and parse sentinel output", async () => { |
| 113 | + const runPromise = session.run("echo hello") |
| 114 | + |
| 115 | + // Simulate output |
| 116 | + stdoutEmitter.emit("data", "__KILO_DONE__:0:/tmp/test\n") |
| 117 | + |
| 118 | + const result = await runPromise |
| 119 | + |
| 120 | + expect(result).toEqual({ |
| 121 | + stdout: "", |
| 122 | + stderr: "", |
| 123 | + exitCode: 0, |
| 124 | + cwd: "/tmp/test", |
| 125 | + }) |
| 126 | + |
| 127 | + expect(stdin.write).toHaveBeenCalledWith( |
| 128 | + expect.stringContaining("echo hello"), |
| 129 | + "utf8", |
| 130 | + expect.any(Function), |
| 131 | + ) |
| 132 | + }) |
| 133 | + |
| 134 | + it("should handle command output", async () => { |
| 135 | + const runPromise = session.run("echo hello") |
| 136 | + |
| 137 | + // Simulate output |
| 138 | + stdoutEmitter.emit("data", "hello world\n__KILO_DONE__:0:/tmp/test\n") |
| 139 | + |
| 140 | + const result = await runPromise |
| 141 | + |
| 142 | + expect(result).toEqual({ |
| 143 | + stdout: "hello world", |
| 144 | + stderr: "", |
| 145 | + exitCode: 0, |
| 146 | + cwd: "/tmp/test", |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + it("should handle stderr output", async () => { |
| 151 | + const runPromise = session.run("failing command") |
| 152 | + |
| 153 | + // Simulate output |
| 154 | + stderrEmitter.emit("data", "error message\n") |
| 155 | + stdoutEmitter.emit("data", "__KILO_DONE__:1:/tmp/test\n") |
| 156 | + |
| 157 | + const result = await runPromise |
| 158 | + |
| 159 | + expect(result).toEqual({ |
| 160 | + stdout: "", |
| 161 | + stderr: "error message", |
| 162 | + exitCode: 1, |
| 163 | + cwd: "/tmp/test", |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + it("should timeout long-running commands", async () => { |
| 168 | + const session = new ShellSession({ timeout: 50 }) |
| 169 | + // We need to initialize the session first (which was done in beforeEach but for the other 'session' instance) |
| 170 | + // Actually beforeEach initializes 'session'. But here we create a new one. |
| 171 | + |
| 172 | + // Mock spawn again for this new session if needed, but the global mock persists. |
| 173 | + await session.ensureSession("/tmp/test") |
| 174 | + |
| 175 | + await expect(session.run("sleep 1")).rejects.toThrow("Command timeout after 50ms") |
| 176 | + }) |
| 177 | + |
| 178 | + it("should reject concurrent commands", async () => { |
| 179 | + const promise1 = session.run("cmd1") |
| 180 | + |
| 181 | + await expect(session.run("cmd2")).rejects.toThrow("Command already in progress") |
| 182 | + |
| 183 | + // Complete the first command |
| 184 | + stdoutEmitter.emit("data", "__KILO_DONE__:0:/tmp/test\n") |
| 185 | + |
| 186 | + await promise1 |
| 187 | + }) |
| 188 | + }) |
| 189 | + |
| 190 | + describe("directory tracking", () => { |
| 191 | + it("should track current directory from sentinel", async () => { |
| 192 | + const session = new ShellSession() |
| 193 | + await session.ensureSession("/tmp/test") |
| 194 | + |
| 195 | + const runPromise = session.run("cd /home/user") |
| 196 | + |
| 197 | + stdoutEmitter.emit("data", "__KILO_DONE__:0:/home/user\n") |
| 198 | + |
| 199 | + await runPromise |
| 200 | + |
| 201 | + expect(session.getCurrentDirectory()).toBe("/home/user") |
| 202 | + }) |
| 203 | + }) |
| 204 | + |
| 205 | + describe("lifecycle", () => { |
| 206 | + it("should dispose properly", async () => { |
| 207 | + const session = new ShellSession() |
| 208 | + await session.ensureSession("/tmp/test") |
| 209 | + |
| 210 | + session.dispose() |
| 211 | + |
| 212 | + expect(mockChildProcess.kill).toHaveBeenCalled() |
| 213 | + expect(session.isSessionReady()).toBe(false) |
| 214 | + }) |
| 215 | + |
| 216 | + it("should handle process exit", async () => { |
| 217 | + const session = new ShellSession() |
| 218 | + await session.ensureSession("/tmp/test") |
| 219 | + |
| 220 | + const runPromise = session.run("test") |
| 221 | + |
| 222 | + mockChildProcess.emit("close", 1, "SIGTERM") |
| 223 | + |
| 224 | + await expect(runPromise).rejects.toThrow("Shell process exited unexpectedly with code 1") |
| 225 | + |
| 226 | + expect(session.isSessionReady()).toBe(false) |
| 227 | + }) |
| 228 | + }) |
| 229 | +}) |
0 commit comments