Skip to content

Commit e17651b

Browse files
committed
[Test] Add utility method for capturing the response/events sent during testing
1 parent 74698b3 commit e17651b

1 file changed

Lines changed: 66 additions & 101 deletions

File tree

editors/code/src/test/suite/debugAdapter.test.ts

Lines changed: 66 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@ type PatchedSession = DebugSession & {
1010
dispose?: () => void;
1111
};
1212

13+
interface RequestCapture<R extends DebugProtocol.Response> {
14+
response: R | undefined;
15+
eventCount: number;
16+
}
17+
18+
/**
19+
* Patches sendResponse and sendEvent on a session, invokes fn, then restores
20+
* the originals. Returns the captured response and number of events sent.
21+
*/
22+
function captureRequest<R extends DebugProtocol.Response>(
23+
session: PatchedSession,
24+
fn: () => void
25+
): RequestCapture<R> {
26+
let response: R | undefined;
27+
let eventCount = 0;
28+
29+
const originalSendResponse = session.sendResponse.bind(session);
30+
const originalSendEvent = session.sendEvent.bind(session);
31+
session.sendResponse = (resp: DebugProtocol.Response) => { response = resp as R; };
32+
session.sendEvent = () => { eventCount++; };
33+
try {
34+
fn();
35+
} finally {
36+
session.sendResponse = originalSendResponse;
37+
session.sendEvent = originalSendEvent;
38+
}
39+
return { response, eventCount };
40+
}
41+
1342
function setPlatformArch(platform: NodeJS.Platform | string, arch: string): void {
1443
Object.defineProperty(process, 'platform', {
1544
value: platform,
@@ -98,7 +127,6 @@ suite('DebugAdapter Test Suite', () => {
98127
adapterID: 'log2src',
99128
pathFormat: 'path'
100129
};
101-
102130
const response: DebugProtocol.InitializeResponse = {
103131
request_seq: 1,
104132
success: true,
@@ -108,32 +136,17 @@ suite('DebugAdapter Test Suite', () => {
108136
body: {}
109137
};
110138

111-
let capturedResponse: DebugProtocol.InitializeResponse | undefined;
112139
const session = debugSession as PatchedSession;
113-
const originalSendResponse = session.sendResponse.bind(session);
114-
session.sendResponse = (resp: DebugProtocol.Response) => {
115-
capturedResponse = resp as DebugProtocol.InitializeResponse;
116-
};
117-
118-
let eventSent = false;
119-
const originalSendEvent = session.sendEvent.bind(session);
120-
session.sendEvent = () => {
121-
eventSent = true;
122-
};
123-
124-
try {
125-
(session as any).initializeRequest(response, args);
126-
127-
assert.ok(capturedResponse, 'Response should be sent');
128-
assert.ok(eventSent, 'Event should be sent');
129-
130-
assert.ok(capturedResponse!.body, 'Response should have body');
131-
assert.strictEqual(capturedResponse!.body.supportsStepBack, true);
132-
assert.strictEqual(capturedResponse!.body.supportTerminateDebuggee, true);
133-
} finally {
134-
session.sendResponse = originalSendResponse;
135-
session.sendEvent = originalSendEvent;
136-
}
140+
const { response: captured, eventCount } = captureRequest<DebugProtocol.InitializeResponse>(
141+
session,
142+
() => (session as any).initializeRequest(response, args)
143+
);
144+
145+
assert.ok(captured, 'Response should be sent');
146+
assert.ok(captured!.body, 'Response should have body');
147+
assert.strictEqual(captured!.body.supportsStepBack, true);
148+
assert.strictEqual(captured!.body.supportTerminateDebuggee, true);
149+
assert.strictEqual(eventCount, 1, 'InitializedEvent should be sent');
137150
});
138151
});
139152

@@ -146,53 +159,26 @@ suite('DebugAdapter Test Suite', () => {
146159
const sourcePath = '/test/source/file.log';
147160
const args: DebugProtocol.SetBreakpointsArguments = {
148161
source: { path: sourcePath },
149-
breakpoints: [
150-
{ line: 10 },
151-
{ line: 20 },
152-
{ line: 30 }
153-
]
162+
breakpoints: [{ line: 10 }, { line: 20 }, { line: 30 }]
154163
};
155-
156164
const response: DebugProtocol.SetBreakpointsResponse = {
157-
request_seq: 1,
158-
success: true,
159-
command: 'setBreakpoints',
160-
seq: 1,
161-
type: 'response',
162-
body: { breakpoints: [] }
165+
request_seq: 1, success: true, command: 'setBreakpoints',
166+
seq: 1, type: 'response', body: { breakpoints: [] }
163167
};
164168

165-
let capturedResponse: DebugProtocol.SetBreakpointsResponse | undefined;
166169
const session = debugSession as PatchedSession;
167-
const originalSendResponse = session.sendResponse.bind(session);
168-
session.sendResponse = (resp: DebugProtocol.Response) => {
169-
capturedResponse = resp as DebugProtocol.SetBreakpointsResponse;
170-
};
171-
172-
let eventSent = false;
173-
const originalSendEvent = session.sendEvent.bind(session);
174-
session.sendEvent = () => {
175-
eventSent = true;
176-
};
177-
178-
try {
179-
(session as any).setBreakPointsRequest(response, args);
180-
181-
assert.ok(capturedResponse, 'Response should be sent');
182-
183-
assert.ok(capturedResponse!.body, 'Response should have body');
184-
assert.ok(capturedResponse!.body.breakpoints, 'Response should have breakpoints');
185-
assert.strictEqual(capturedResponse!.body.breakpoints.length, 3, 'Should have 3 breakpoints');
186-
187-
capturedResponse!.body.breakpoints.forEach((bp, index) => {
188-
assert.strictEqual(bp.line, args.breakpoints![index].line, `Breakpoint ${index} should have correct line`);
189-
});
190-
191-
assert.ok(eventSent, 'Should send stopped event');
192-
} finally {
193-
session.sendResponse = originalSendResponse;
194-
session.sendEvent = originalSendEvent;
195-
}
170+
const { response: captured, eventCount } = captureRequest<DebugProtocol.SetBreakpointsResponse>(
171+
session,
172+
() => (session as any).setBreakPointsRequest(response, args)
173+
);
174+
175+
assert.ok(captured, 'Response should be sent');
176+
assert.ok(captured!.body.breakpoints, 'Response should have breakpoints');
177+
assert.strictEqual(captured!.body.breakpoints.length, 3, 'Should have 3 breakpoints');
178+
captured!.body.breakpoints.forEach((bp, index) => {
179+
assert.strictEqual(bp.line, args.breakpoints![index].line, `Breakpoint ${index} should have correct line`);
180+
});
181+
assert.strictEqual(eventCount, 1, 'Should send stopped event');
196182
});
197183

198184
test('Should handle empty breakpoints array', () => {
@@ -201,42 +187,21 @@ suite('DebugAdapter Test Suite', () => {
201187
source: { path: sourcePath },
202188
breakpoints: []
203189
};
204-
205190
const response: DebugProtocol.SetBreakpointsResponse = {
206-
request_seq: 1,
207-
success: true,
208-
command: 'setBreakpoints',
209-
seq: 1,
210-
type: 'response',
211-
body: { breakpoints: [] }
191+
request_seq: 1, success: true, command: 'setBreakpoints',
192+
seq: 1, type: 'response', body: { breakpoints: [] }
212193
};
213194

214-
let capturedResponse: DebugProtocol.SetBreakpointsResponse | undefined;
215-
let eventSent = false;
216-
217195
const session = debugSession as PatchedSession;
218-
const originalSendResponse = session.sendResponse.bind(session);
219-
session.sendResponse = (resp: DebugProtocol.Response) => {
220-
capturedResponse = resp as DebugProtocol.SetBreakpointsResponse;
221-
};
222-
223-
const originalSendEvent = session.sendEvent.bind(session);
224-
session.sendEvent = () => {
225-
eventSent = true;
226-
};
227-
228-
try {
229-
(session as any).setBreakPointsRequest(response, args);
230-
231-
assert.ok(capturedResponse, 'Response should be sent');
232-
assert.ok(capturedResponse!.body, 'Response should have body');
233-
assert.ok(capturedResponse!.body.breakpoints, 'Response should have breakpoints array');
234-
assert.strictEqual(capturedResponse!.body.breakpoints.length, 0, 'Should have no breakpoints');
235-
assert.strictEqual(eventSent, false, 'Should not send stopped event for empty breakpoints');
236-
} finally {
237-
session.sendResponse = originalSendResponse;
238-
session.sendEvent = originalSendEvent;
239-
}
196+
const { response: captured, eventCount } = captureRequest<DebugProtocol.SetBreakpointsResponse>(
197+
session,
198+
() => (session as any).setBreakPointsRequest(response, args)
199+
);
200+
201+
assert.ok(captured, 'Response should be sent');
202+
assert.strictEqual(captured!.body.breakpoints.length, 0, 'Should have no breakpoints');
203+
assert.strictEqual(eventCount, 0, 'Should not send stopped event for empty breakpoints');
204+
assert.strictEqual(logDebugger.hasBreakpoints(), false, 'LogDebugger should report no breakpoints');
240205
});
241206
});
242-
});
207+
});

0 commit comments

Comments
 (0)