-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathterminal.test.ts
More file actions
230 lines (180 loc) · 7.58 KB
/
terminal.test.ts
File metadata and controls
230 lines (180 loc) · 7.58 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
220
221
222
223
224
225
226
227
228
229
230
/**
* Terminal Service tests.
* Tests for TerminalService using sinon stubs for VS Code terminal API.
*/
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as vscode from 'vscode';
import { TerminalService } from '../services/terminal';
suite('TerminalService', () => {
let sandbox: sinon.SinonSandbox;
setup(() => {
sandbox = sinon.createSandbox();
});
teardown(() => {
sandbox.restore();
});
suite('constructor', () => {
test('should use default config values when no config provided', () => {
const service = new TerminalService();
assert.ok(service);
});
test('should accept custom config', () => {
const service = new TerminalService({
createNewTerminal: false,
showTerminal: false,
closeTerminalOnStop: false,
});
assert.ok(service);
});
test('should accept partial config', () => {
const service = new TerminalService({
createNewTerminal: false,
});
assert.ok(service);
});
});
suite('fromConfiguration', () => {
test('should create service from VS Code configuration', () => {
const service = TerminalService.fromConfiguration();
assert.ok(service instanceof TerminalService);
});
});
suite('getOrCreateTerminal', () => {
test('should create new terminal when createNewTerminal is true', () => {
const mockTerminal = {
show: sandbox.stub(),
hide: sandbox.stub(),
name: 'Dev Proxy',
} as unknown as vscode.Terminal;
sandbox.stub(vscode.window, 'createTerminal').returns(mockTerminal);
const service = new TerminalService({ createNewTerminal: true, showTerminal: true });
const terminal = service.getOrCreateTerminal();
assert.strictEqual(terminal, mockTerminal);
assert.ok((mockTerminal.show as sinon.SinonStub).calledOnce);
});
test('should reuse active terminal when createNewTerminal is false', () => {
const activeTerminal = {
name: 'Existing Terminal',
} as unknown as vscode.Terminal;
sandbox.stub(vscode.window, 'activeTerminal').value(activeTerminal);
const createStub = sandbox.stub(vscode.window, 'createTerminal');
const service = new TerminalService({ createNewTerminal: false });
const terminal = service.getOrCreateTerminal();
assert.strictEqual(terminal, activeTerminal);
assert.ok(createStub.notCalled);
});
test('should create new terminal when createNewTerminal is false but no active terminal', () => {
const mockTerminal = {
show: sandbox.stub(),
hide: sandbox.stub(),
name: 'Dev Proxy',
} as unknown as vscode.Terminal;
sandbox.stub(vscode.window, 'activeTerminal').value(undefined);
sandbox.stub(vscode.window, 'createTerminal').returns(mockTerminal);
const service = new TerminalService({ createNewTerminal: false, showTerminal: true });
const terminal = service.getOrCreateTerminal();
assert.strictEqual(terminal, mockTerminal);
});
test('should hide terminal when showTerminal is false', () => {
const mockTerminal = {
show: sandbox.stub(),
hide: sandbox.stub(),
name: 'Dev Proxy',
} as unknown as vscode.Terminal;
sandbox.stub(vscode.window, 'createTerminal').returns(mockTerminal);
const service = new TerminalService({ createNewTerminal: true, showTerminal: false });
service.getOrCreateTerminal();
assert.ok((mockTerminal.hide as sinon.SinonStub).calledOnce);
assert.ok((mockTerminal.show as sinon.SinonStub).notCalled);
});
test('should pass env to terminal when env is provided', () => {
const mockTerminal = {
show: sandbox.stub(),
hide: sandbox.stub(),
name: 'Dev Proxy',
} as unknown as vscode.Terminal;
const createStub = sandbox.stub(vscode.window, 'createTerminal').returns(mockTerminal);
const env = { 'NODE_ENV': 'test', 'DEBUG': 'true' };
const service = new TerminalService({ createNewTerminal: true, showTerminal: true, env });
service.getOrCreateTerminal();
assert.ok(createStub.calledOnce);
const options = createStub.firstCall.args[0] as vscode.TerminalOptions;
assert.deepStrictEqual(options.env, env, 'Should pass env to terminal options');
});
test('should not include env in terminal options when env is undefined', () => {
const mockTerminal = {
show: sandbox.stub(),
hide: sandbox.stub(),
name: 'Dev Proxy',
} as unknown as vscode.Terminal;
const createStub = sandbox.stub(vscode.window, 'createTerminal').returns(mockTerminal);
const service = new TerminalService({ createNewTerminal: true, showTerminal: true });
service.getOrCreateTerminal();
assert.ok(createStub.calledOnce);
const options = createStub.firstCall.args[0] as vscode.TerminalOptions;
assert.strictEqual(options.env, undefined, 'Should not include env in terminal options');
});
});
suite('disposeDevProxyTerminals', () => {
test('should dispose terminals named "Dev Proxy"', () => {
const devProxyTerminal = {
name: 'Dev Proxy',
dispose: sandbox.stub(),
} as unknown as vscode.Terminal;
const otherTerminal = {
name: 'Other Terminal',
dispose: sandbox.stub(),
} as unknown as vscode.Terminal;
sandbox.stub(vscode.window, 'terminals').value([devProxyTerminal, otherTerminal]);
const service = new TerminalService({ closeTerminalOnStop: true });
service.disposeDevProxyTerminals();
assert.ok((devProxyTerminal.dispose as sinon.SinonStub).calledOnce);
assert.ok((otherTerminal.dispose as sinon.SinonStub).notCalled);
});
test('should not dispose any terminals when closeTerminalOnStop is false', () => {
const devProxyTerminal = {
name: 'Dev Proxy',
dispose: sandbox.stub(),
} as unknown as vscode.Terminal;
sandbox.stub(vscode.window, 'terminals').value([devProxyTerminal]);
const service = new TerminalService({ closeTerminalOnStop: false });
service.disposeDevProxyTerminals();
assert.ok((devProxyTerminal.dispose as sinon.SinonStub).notCalled);
});
test('should handle empty terminals array', () => {
sandbox.stub(vscode.window, 'terminals').value([]);
const service = new TerminalService({ closeTerminalOnStop: true });
// Should not throw
service.disposeDevProxyTerminals();
});
test('should dispose multiple Dev Proxy terminals', () => {
const terminal1 = {
name: 'Dev Proxy',
dispose: sandbox.stub(),
} as unknown as vscode.Terminal;
const terminal2 = {
name: 'Dev Proxy',
dispose: sandbox.stub(),
} as unknown as vscode.Terminal;
sandbox.stub(vscode.window, 'terminals').value([terminal1, terminal2]);
const service = new TerminalService({ closeTerminalOnStop: true });
service.disposeDevProxyTerminals();
assert.ok((terminal1.dispose as sinon.SinonStub).calledOnce);
assert.ok((terminal2.dispose as sinon.SinonStub).calledOnce);
});
});
suite('sendCommand', () => {
test('should send command text to terminal', () => {
const mockTerminal = {
sendText: sandbox.stub(),
} as unknown as vscode.Terminal;
const service = new TerminalService();
service.sendCommand(mockTerminal, 'devproxy --config-file config.json');
assert.ok((mockTerminal.sendText as sinon.SinonStub).calledOnce);
assert.ok(
(mockTerminal.sendText as sinon.SinonStub).calledWith('devproxy --config-file config.json')
);
});
});
});