Connection.test.ts (1356B)
1 /** 2 * @license 3 * Copyright 2022 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 import {describe, it} from 'node:test'; 8 9 import expect from 'expect'; 10 11 import type {ConnectionTransport} from '../common/ConnectionTransport.js'; 12 13 import {BidiConnection} from './Connection.js'; 14 15 describe('WebDriver BiDi Connection', () => { 16 class TestConnectionTransport implements ConnectionTransport { 17 sent: string[] = []; 18 closed = false; 19 20 send(message: string) { 21 this.sent.push(message); 22 } 23 24 close(): void { 25 this.closed = true; 26 } 27 } 28 29 it('should work', async () => { 30 const transport = new TestConnectionTransport(); 31 const connection = new BidiConnection('ws://127.0.0.1', transport); 32 const responsePromise = connection.send('session.new', { 33 capabilities: {}, 34 }); 35 expect(transport.sent).toEqual([ 36 `{"id":1,"method":"session.new","params":{"capabilities":{}}}`, 37 ]); 38 const id = JSON.parse(transport.sent[0]!).id; 39 const rawResponse = { 40 id, 41 type: 'success', 42 result: {ready: false, message: 'already connected'}, 43 }; 44 (transport as ConnectionTransport).onmessage?.(JSON.stringify(rawResponse)); 45 const response = await responsePromise; 46 expect(response).toEqual(rawResponse); 47 connection.dispose(); 48 expect(transport.closed).toBeTruthy(); 49 }); 50 });