-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathExecaTerminalProcess.spec.ts
More file actions
219 lines (192 loc) · 7.21 KB
/
ExecaTerminalProcess.spec.ts
File metadata and controls
219 lines (192 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// npx vitest run integrations/terminal/__tests__/ExecaTerminalProcess.spec.ts
const mockPid = 12345
vitest.mock("execa", () => {
const mockKill = vitest.fn()
const execa = vitest.fn((options: any) => {
return (_template: TemplateStringsArray, ...args: any[]) => ({
pid: mockPid,
iterable: (_opts: any) =>
(async function* () {
yield "test output\n"
})(),
kill: mockKill,
})
})
return { execa, ExecaError: class extends Error {} }
})
vitest.mock("ps-tree", () => ({
default: vitest.fn((_: number, cb: any) => cb(null, [])),
}))
import { execa } from "execa"
import { ExecaTerminalProcess } from "../ExecaTerminalProcess"
import { BaseTerminal } from "../BaseTerminal"
import type { RooTerminal } from "../types"
describe("ExecaTerminalProcess", () => {
let mockTerminal: RooTerminal
let terminalProcess: ExecaTerminalProcess
let originalEnv: NodeJS.ProcessEnv
beforeEach(() => {
originalEnv = { ...process.env }
BaseTerminal.setExecaShellPath(undefined)
mockTerminal = {
provider: "execa",
id: 1,
busy: false,
running: false,
getCurrentWorkingDirectory: vitest.fn().mockReturnValue("/test/cwd"),
isClosed: vitest.fn().mockReturnValue(false),
runCommand: vitest.fn(),
setActiveStream: vitest.fn(),
shellExecutionComplete: vitest.fn(),
getProcessesWithOutput: vitest.fn().mockReturnValue([]),
getUnretrievedOutput: vitest.fn().mockReturnValue(""),
getLastCommand: vitest.fn().mockReturnValue(""),
cleanCompletedProcessQueue: vitest.fn(),
} as unknown as RooTerminal
terminalProcess = new ExecaTerminalProcess(mockTerminal)
})
afterEach(() => {
process.env = originalEnv
vitest.clearAllMocks()
})
describe("UTF-8 encoding fix", () => {
it("should set LANG and LC_ALL to en_US.UTF-8", async () => {
await terminalProcess.run("echo test")
const execaMock = vitest.mocked(execa)
expect(execaMock).toHaveBeenCalledWith(
expect.objectContaining({
shell: true,
cwd: "/test/cwd",
all: true,
env: expect.objectContaining({
LANG: "en_US.UTF-8",
LC_ALL: "en_US.UTF-8",
}),
}),
)
})
it("should preserve existing environment variables", async () => {
process.env.EXISTING_VAR = "existing"
terminalProcess = new ExecaTerminalProcess(mockTerminal)
await terminalProcess.run("echo test")
const execaMock = vitest.mocked(execa)
const calledOptions = execaMock.mock.calls[0][0] as any
expect(calledOptions.env.EXISTING_VAR).toBe("existing")
})
it("should override existing LANG and LC_ALL values", async () => {
process.env.LANG = "C"
process.env.LC_ALL = "POSIX"
terminalProcess = new ExecaTerminalProcess(mockTerminal)
await terminalProcess.run("echo test")
const execaMock = vitest.mocked(execa)
const calledOptions = execaMock.mock.calls[0][0] as any
expect(calledOptions.env.LANG).toBe("en_US.UTF-8")
expect(calledOptions.env.LC_ALL).toBe("en_US.UTF-8")
})
it("should set Windows-specific UTF-8 environment variables", async () => {
await terminalProcess.run("echo test")
const execaMock = vitest.mocked(execa)
expect(execaMock).toHaveBeenCalledWith(
expect.objectContaining({
env: expect.objectContaining({
// Python UTF-8 encoding
PYTHONIOENCODING: "utf-8",
PYTHONUTF8: "1",
// Ruby UTF-8 encoding
RUBYOPT: "-EUTF-8",
}),
}),
)
})
it("should override existing Python and Ruby encoding environment variables", async () => {
process.env.PYTHONIOENCODING = "latin-1"
process.env.PYTHONUTF8 = "0"
process.env.RUBYOPT = "-ELATIN-1"
terminalProcess = new ExecaTerminalProcess(mockTerminal)
await terminalProcess.run("echo test")
const execaMock = vitest.mocked(execa)
const calledOptions = execaMock.mock.calls[0][0] as any
expect(calledOptions.env.PYTHONIOENCODING).toBe("utf-8")
expect(calledOptions.env.PYTHONUTF8).toBe("1")
expect(calledOptions.env.RUBYOPT).toBe("-EUTF-8")
})
it("should use execaShellPath when set", async () => {
BaseTerminal.setExecaShellPath("/bin/bash")
await terminalProcess.run("echo test")
const execaMock = vitest.mocked(execa)
expect(execaMock).toHaveBeenCalledWith(
expect.objectContaining({
shell: "/bin/bash",
}),
)
})
it("should fall back to shell=true when execaShellPath is undefined", async () => {
BaseTerminal.setExecaShellPath(undefined)
await terminalProcess.run("echo test")
const execaMock = vitest.mocked(execa)
expect(execaMock).toHaveBeenCalledWith(
expect.objectContaining({
shell: true,
}),
)
})
})
describe("basic functionality", () => {
it("should create instance with terminal reference", () => {
expect(terminalProcess).toBeInstanceOf(ExecaTerminalProcess)
expect(terminalProcess.terminal).toBe(mockTerminal)
})
it("should emit shell_execution_complete with exitCode 0", async () => {
const spy = vitest.fn()
terminalProcess.on("shell_execution_complete", spy)
await terminalProcess.run("echo test")
expect(spy).toHaveBeenCalledWith({ exitCode: 0 })
})
it("should emit completed event with full output", async () => {
const spy = vitest.fn()
terminalProcess.on("completed", spy)
await terminalProcess.run("echo test")
expect(spy).toHaveBeenCalledWith("test output\n")
})
it("should set and clear active stream", async () => {
await terminalProcess.run("echo test")
expect(mockTerminal.setActiveStream).toHaveBeenCalledWith(expect.any(Object), mockPid)
expect(mockTerminal.setActiveStream).toHaveBeenLastCalledWith(undefined)
})
})
describe("trimRetrievedOutput", () => {
it("clears buffer when all output has been retrieved", () => {
// Set up a scenario where all output has been retrieved
terminalProcess["fullOutput"] = "test output data"
terminalProcess["lastRetrievedIndex"] = 16 // Same as fullOutput.length
// Access the protected method through type casting
;(terminalProcess as any).trimRetrievedOutput()
expect(terminalProcess["fullOutput"]).toBe("")
expect(terminalProcess["lastRetrievedIndex"]).toBe(0)
})
it("does not clear buffer when there is unretrieved output", () => {
// Set up a scenario where not all output has been retrieved
terminalProcess["fullOutput"] = "test output data"
terminalProcess["lastRetrievedIndex"] = 5 // Less than fullOutput.length
;(terminalProcess as any).trimRetrievedOutput()
// Buffer should NOT be cleared - there's still unretrieved content
expect(terminalProcess["fullOutput"]).toBe("test output data")
expect(terminalProcess["lastRetrievedIndex"]).toBe(5)
})
it("does nothing when buffer is already empty", () => {
terminalProcess["fullOutput"] = ""
terminalProcess["lastRetrievedIndex"] = 0
;(terminalProcess as any).trimRetrievedOutput()
expect(terminalProcess["fullOutput"]).toBe("")
expect(terminalProcess["lastRetrievedIndex"]).toBe(0)
})
it("clears buffer when lastRetrievedIndex exceeds fullOutput length", () => {
// Edge case: index is greater than current length (could happen if output was modified)
terminalProcess["fullOutput"] = "short"
terminalProcess["lastRetrievedIndex"] = 100
;(terminalProcess as any).trimRetrievedOutput()
expect(terminalProcess["fullOutput"]).toBe("")
expect(terminalProcess["lastRetrievedIndex"]).toBe(0)
})
})
})