Skip to content

Commit 95df1ea

Browse files
committed
[DebugAdapter] Add some unit tests
1 parent 62a0bc6 commit 95df1ea

File tree

1 file changed

+265
-0
lines changed

1 file changed

+265
-0
lines changed
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
import * as assert from 'assert';
2+
import { DebugProtocol } from '@vscode/debugprotocol';
3+
4+
import { DebugSession, BinaryNotFoundError } from '../../debugAdapter';
5+
6+
7+
suite('DebugAdapter Test Suite', () => {
8+
let debugSession: DebugSession | undefined;
9+
let originalPlatform: string;
10+
let originalArch: string;
11+
12+
setup(() => {
13+
originalPlatform = process.platform;
14+
originalArch = process.arch;
15+
});
16+
17+
teardown(() => {
18+
Object.defineProperty(process, 'platform', {
19+
value: originalPlatform,
20+
configurable: true
21+
});
22+
Object.defineProperty(process, 'arch', {
23+
value: originalArch,
24+
configurable: true
25+
});
26+
27+
if (debugSession) {
28+
try {
29+
(debugSession as any).dispose?.();
30+
} catch (e) {
31+
// Ignore
32+
}
33+
debugSession = undefined;
34+
}
35+
});
36+
37+
suite('Constructor Tests', () => {
38+
test('Should create debug session with correct binary path for darwin-arm64', () => {
39+
Object.defineProperty(process, 'platform', {
40+
value: 'darwin',
41+
configurable: true
42+
});
43+
Object.defineProperty(process, 'arch', {
44+
value: 'arm64',
45+
configurable: true
46+
});
47+
48+
debugSession = new DebugSession();
49+
50+
assert.ok(debugSession, 'Debug session should be created');
51+
});
52+
53+
test('Should create debug session with correct binary path for linux-x64', () => {
54+
Object.defineProperty(process, 'platform', {
55+
value: 'linux',
56+
configurable: true
57+
});
58+
Object.defineProperty(process, 'arch', {
59+
value: 'x64',
60+
configurable: true
61+
});
62+
63+
debugSession = new DebugSession();
64+
65+
assert.ok(debugSession, 'Debug session should be created');
66+
});
67+
68+
test('Should create debug session with correct binary path for win32-x64', () => {
69+
Object.defineProperty(process, 'platform', {
70+
value: 'win32',
71+
configurable: true
72+
});
73+
Object.defineProperty(process, 'arch', {
74+
value: 'x64',
75+
configurable: true
76+
});
77+
78+
debugSession = new DebugSession();
79+
80+
assert.ok(debugSession, 'Debug session should be created');
81+
});
82+
83+
test('Should throw BinaryNotFoundError for unsupported platform', () => {
84+
Object.defineProperty(process, 'platform', {
85+
value: 'unsupported',
86+
configurable: true
87+
});
88+
Object.defineProperty(process, 'arch', {
89+
value: 'unsupported',
90+
configurable: true
91+
});
92+
93+
assert.throws(() => {
94+
new DebugSession();
95+
}, BinaryNotFoundError, 'Should throw BinaryNotFoundError for unsupported platform');
96+
});
97+
});
98+
99+
suite('Initialize Request Tests', () => {
100+
setup(() => {
101+
Object.defineProperty(process, 'platform', {
102+
value: 'darwin',
103+
configurable: true
104+
});
105+
Object.defineProperty(process, 'arch', {
106+
value: 'arm64',
107+
configurable: true
108+
});
109+
debugSession = new DebugSession();
110+
});
111+
112+
test('Should handle initialize request correctly', () => {
113+
const args: DebugProtocol.InitializeRequestArguments = {
114+
clientID: 'test-client',
115+
clientName: 'Test Client',
116+
adapterID: 'log2src',
117+
pathFormat: 'path'
118+
};
119+
120+
const response: DebugProtocol.InitializeResponse = {
121+
request_seq: 1,
122+
success: true,
123+
command: 'initialize',
124+
seq: 1,
125+
type: 'response',
126+
body: {}
127+
};
128+
129+
let capturedResponse: DebugProtocol.InitializeResponse | undefined;
130+
const originalSendResponse = (debugSession as any).sendResponse.bind(debugSession);
131+
(debugSession as any).sendResponse = (resp: any) => {
132+
capturedResponse = resp;
133+
};
134+
135+
let eventSent = false;
136+
const originalSendEvent = (debugSession as any).sendEvent.bind(debugSession);
137+
(debugSession as any).sendEvent = () => {
138+
eventSent = true;
139+
};
140+
141+
try {
142+
(debugSession as any).initializeRequest(response, args);
143+
144+
assert.ok(capturedResponse, 'Response should be sent');
145+
assert.ok(eventSent, 'Event should be sent');
146+
147+
assert.ok(capturedResponse!.body, 'Response should have body');
148+
assert.strictEqual(capturedResponse!.body.supportsStepBack, true);
149+
assert.strictEqual(capturedResponse!.body.supportTerminateDebuggee, true);
150+
} finally {
151+
(debugSession as any).sendResponse = originalSendResponse;
152+
(debugSession as any).sendEvent = originalSendEvent;
153+
}
154+
});
155+
});
156+
157+
suite('Breakpoint Tests', () => {
158+
setup(() => {
159+
Object.defineProperty(process, 'platform', {
160+
value: 'darwin',
161+
configurable: true
162+
});
163+
Object.defineProperty(process, 'arch', {
164+
value: 'arm64',
165+
configurable: true
166+
});
167+
debugSession = new DebugSession();
168+
});
169+
170+
test('Should set breakpoints correctly', () => {
171+
const sourcePath = '/test/source/file.log';
172+
const args: DebugProtocol.SetBreakpointsArguments = {
173+
source: { path: sourcePath },
174+
breakpoints: [
175+
{ line: 10 },
176+
{ line: 20 },
177+
{ line: 30 }
178+
]
179+
};
180+
181+
const response: DebugProtocol.SetBreakpointsResponse = {
182+
request_seq: 1,
183+
success: true,
184+
command: 'setBreakpoints',
185+
seq: 1,
186+
type: 'response',
187+
body: { breakpoints: [] }
188+
};
189+
190+
let capturedResponse: DebugProtocol.SetBreakpointsResponse | undefined;
191+
const originalSendResponse = (debugSession as any).sendResponse.bind(debugSession);
192+
(debugSession as any).sendResponse = (resp: any) => {
193+
capturedResponse = resp;
194+
};
195+
196+
let eventSent = false;
197+
const originalSendEvent = (debugSession as any).sendEvent.bind(debugSession);
198+
(debugSession as any).sendEvent = () => {
199+
eventSent = true;
200+
};
201+
202+
try {
203+
(debugSession as any).setBreakPointsRequest(response, args);
204+
205+
assert.ok(capturedResponse, 'Response should be sent');
206+
207+
assert.ok(capturedResponse!.body, 'Response should have body');
208+
assert.ok(capturedResponse!.body.breakpoints, 'Response should have breakpoints');
209+
assert.strictEqual(capturedResponse!.body.breakpoints.length, 3, 'Should have 3 breakpoints');
210+
211+
capturedResponse!.body.breakpoints.forEach((bp, index) => {
212+
assert.strictEqual(bp.line, args.breakpoints![index].line, `Breakpoint ${index} should have correct line`);
213+
});
214+
215+
assert.ok(eventSent, 'Should send stopped event');
216+
} finally {
217+
(debugSession as any).sendResponse = originalSendResponse;
218+
(debugSession as any).sendEvent = originalSendEvent;
219+
}
220+
});
221+
222+
test('Should handle empty breakpoints array', () => {
223+
const sourcePath = '/test/source/file.log';
224+
const args: DebugProtocol.SetBreakpointsArguments = {
225+
source: { path: sourcePath },
226+
breakpoints: []
227+
};
228+
229+
const response: DebugProtocol.SetBreakpointsResponse = {
230+
request_seq: 1,
231+
success: true,
232+
command: 'setBreakpoints',
233+
seq: 1,
234+
type: 'response',
235+
body: { breakpoints: [] }
236+
};
237+
238+
let capturedResponse: DebugProtocol.SetBreakpointsResponse | undefined;
239+
let eventSent = false;
240+
241+
const originalSendResponse = (debugSession as any).sendResponse.bind(debugSession);
242+
(debugSession as any).sendResponse = (resp: any) => {
243+
capturedResponse = resp;
244+
};
245+
246+
const originalSendEvent = (debugSession as any).sendEvent.bind(debugSession);
247+
(debugSession as any).sendEvent = () => {
248+
eventSent = true;
249+
};
250+
251+
try {
252+
(debugSession as any).setBreakPointsRequest(response, args);
253+
254+
assert.ok(capturedResponse, 'Response should be sent');
255+
assert.ok(capturedResponse!.body, 'Response should have body');
256+
assert.ok(capturedResponse!.body.breakpoints, 'Response should have breakpoints array');
257+
assert.strictEqual(capturedResponse!.body.breakpoints.length, 0, 'Should have no breakpoints');
258+
assert.strictEqual(eventSent, false, 'Should not send stopped event for empty breakpoints');
259+
} finally {
260+
(debugSession as any).sendResponse = originalSendResponse;
261+
(debugSession as any).sendEvent = originalSendEvent;
262+
}
263+
});
264+
});
265+
});

0 commit comments

Comments
 (0)