Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/test/node/protocol/handshake.integrationTest.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { URI } from '../../../../../base/common/uri.js';
8
import { PROTOCOL_VERSION } from '../../../common/state/sessionCapabilities.js';
9
import {
10
JSON_RPC_PARSE_ERROR,
11
type InitializeResult,
12
type JsonRpcErrorResponse,
13
} from '../../../common/state/sessionProtocol.js';
14
import { IServerHandle, nextSessionUri, startServer, TestProtocolClient } from './testHelpers.js';
15
16
suite('Protocol WebSocket — Handshake & Errors', function () {
17
18
let server: IServerHandle;
19
let client: TestProtocolClient;
20
21
suiteSetup(async function () {
22
this.timeout(15_000);
23
server = await startServer();
24
});
25
26
suiteTeardown(function () {
27
server.process.kill();
28
});
29
30
setup(async function () {
31
this.timeout(10_000);
32
client = new TestProtocolClient(server.port);
33
await client.connect();
34
});
35
36
teardown(function () {
37
client.close();
38
});
39
40
test('handshake returns initialize response with protocol version', async function () {
41
this.timeout(5_000);
42
43
const result = await client.call<InitializeResult>('initialize', {
44
protocolVersion: PROTOCOL_VERSION,
45
clientId: 'test-handshake',
46
initialSubscriptions: [URI.from({ scheme: 'agenthost', path: '/root' }).toString()],
47
});
48
49
assert.strictEqual(result.protocolVersion, PROTOCOL_VERSION);
50
assert.ok(result.serverSeq >= 0);
51
assert.ok(result.snapshots.length >= 1, 'should have root state snapshot');
52
});
53
54
test('malformed JSON message returns parse error', async function () {
55
this.timeout(10_000);
56
57
const raw = new TestProtocolClient(server.port);
58
await raw.connect();
59
60
const responsePromise = raw.waitForRawMessage();
61
raw.sendRaw('this is not valid json{{{');
62
63
const response = await responsePromise as JsonRpcErrorResponse;
64
assert.strictEqual(response.jsonrpc, '2.0');
65
assert.strictEqual(response.id, null);
66
assert.strictEqual(response.error.code, JSON_RPC_PARSE_ERROR);
67
68
raw.close();
69
});
70
71
test('createSession with invalid provider does not crash server', async function () {
72
this.timeout(10_000);
73
74
await client.call('initialize', { protocolVersion: PROTOCOL_VERSION, clientId: 'test-invalid-create' });
75
76
let gotError = false;
77
try {
78
await client.call('createSession', { session: nextSessionUri(), provider: 'nonexistent' });
79
} catch {
80
gotError = true;
81
}
82
assert.ok(gotError, 'should have received an error for invalid provider');
83
84
// Server should still be functional
85
await client.call('createSession', { session: nextSessionUri(), provider: 'mock' });
86
const notif = await client.waitForNotification(n =>
87
n.method === 'notification' && (n.params as { notification: { type: string } }).notification.type === 'notify/sessionAdded'
88
);
89
assert.ok(notif);
90
});
91
});
92
93