Path: blob/main/src/vs/platform/agentHost/test/node/protocol/handshake.integrationTest.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { URI } from '../../../../../base/common/uri.js';7import { PROTOCOL_VERSION } from '../../../common/state/sessionCapabilities.js';8import {9JSON_RPC_PARSE_ERROR,10type InitializeResult,11type JsonRpcErrorResponse,12} from '../../../common/state/sessionProtocol.js';13import { IServerHandle, nextSessionUri, startServer, TestProtocolClient } from './testHelpers.js';1415suite('Protocol WebSocket — Handshake & Errors', function () {1617let server: IServerHandle;18let client: TestProtocolClient;1920suiteSetup(async function () {21this.timeout(15_000);22server = await startServer();23});2425suiteTeardown(function () {26server.process.kill();27});2829setup(async function () {30this.timeout(10_000);31client = new TestProtocolClient(server.port);32await client.connect();33});3435teardown(function () {36client.close();37});3839test('handshake returns initialize response with protocol version', async function () {40this.timeout(5_000);4142const result = await client.call<InitializeResult>('initialize', {43protocolVersion: PROTOCOL_VERSION,44clientId: 'test-handshake',45initialSubscriptions: [URI.from({ scheme: 'agenthost', path: '/root' }).toString()],46});4748assert.strictEqual(result.protocolVersion, PROTOCOL_VERSION);49assert.ok(result.serverSeq >= 0);50assert.ok(result.snapshots.length >= 1, 'should have root state snapshot');51});5253test('malformed JSON message returns parse error', async function () {54this.timeout(10_000);5556const raw = new TestProtocolClient(server.port);57await raw.connect();5859const responsePromise = raw.waitForRawMessage();60raw.sendRaw('this is not valid json{{{');6162const response = await responsePromise as JsonRpcErrorResponse;63assert.strictEqual(response.jsonrpc, '2.0');64assert.strictEqual(response.id, null);65assert.strictEqual(response.error.code, JSON_RPC_PARSE_ERROR);6667raw.close();68});6970test('createSession with invalid provider does not crash server', async function () {71this.timeout(10_000);7273await client.call('initialize', { protocolVersion: PROTOCOL_VERSION, clientId: 'test-invalid-create' });7475let gotError = false;76try {77await client.call('createSession', { session: nextSessionUri(), provider: 'nonexistent' });78} catch {79gotError = true;80}81assert.ok(gotError, 'should have received an error for invalid provider');8283// Server should still be functional84await client.call('createSession', { session: nextSessionUri(), provider: 'mock' });85const notif = await client.waitForNotification(n =>86n.method === 'notification' && (n.params as { notification: { type: string } }).notification.type === 'notify/sessionAdded'87);88assert.ok(notif);89});90});919293