UNPKG

chrome-devtools-frontend

Version:
177 lines (162 loc) 5.52 kB
// Copyright 2022 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import * as ProtocolMonitor from './protocol_monitor.js'; describe('ProtocolMonitor', () => { describe('parseCommandInput', () => { it('parses various JSON formats', async () => { const input = { command: 'Input.dispatchMouseEvent', parameters: {parameter1: 'value1'}, }; // "command" variations. assert.deepEqual( ProtocolMonitor.ProtocolMonitor.parseCommandInput(JSON.stringify({ command: input.command, parameters: input.parameters, })), input); assert.deepEqual( ProtocolMonitor.ProtocolMonitor.parseCommandInput(JSON.stringify({ cmd: input.command, parameters: input.parameters, })), input); assert.deepEqual( ProtocolMonitor.ProtocolMonitor.parseCommandInput(JSON.stringify({ method: input.command, parameters: input.parameters, })), input); // "parameters" variations. assert.deepEqual( ProtocolMonitor.ProtocolMonitor.parseCommandInput(JSON.stringify({ command: input.command, params: input.parameters, })), input); assert.deepEqual( ProtocolMonitor.ProtocolMonitor.parseCommandInput(JSON.stringify({ cmd: input.command, args: input.parameters, })), input); assert.deepEqual( ProtocolMonitor.ProtocolMonitor.parseCommandInput(JSON.stringify({ method: input.command, arguments: input.parameters, })), input); }); it('parses non-JSON data as a command name', async () => { assert.deepEqual(ProtocolMonitor.ProtocolMonitor.parseCommandInput('Input.dispatchMouseEvent'), { command: 'Input.dispatchMouseEvent', parameters: {}, }); }); it('should correctly creates a map of CDP commands with their corresponding metadata', async () => { const domains = [ { domain: 'Test', metadata: { 'Test.test': { parameters: [{ name: 'test', type: 'test', optional: true, }], description: 'Description1', replyArgs: ['Test1'], }, }, }, { domain: 'Test2', metadata: { 'Test2.test2': { parameters: [{ name: 'test2', type: 'test2', optional: true, }], description: 'Description2', replyArgs: ['Test2'], }, 'Test2.test3': { parameters: [{ name: 'test3', type: 'test3', optional: true, }], description: 'Description3', replyArgs: ['Test3'], }, }, }, ] as Iterable<ProtocolMonitor.ProtocolMonitor.ProtocolDomain>; const expectedCommands = new Map(); expectedCommands.set('Test.test', { parameters: [{ name: 'test', type: 'test', optional: true, }], description: 'Description1', replyArgs: ['Test1'], }); expectedCommands.set('Test2.test2', { parameters: [{ name: 'test2', type: 'test2', optional: true, }], description: 'Description2', replyArgs: ['Test2'], }); expectedCommands.set('Test2.test3', { parameters: [{ name: 'test3', type: 'test3', optional: true, }], description: 'Description3', replyArgs: ['Test3'], }); const metadataByCommand = ProtocolMonitor.ProtocolMonitor.buildProtocolMetadata(domains); assert.deepEqual(metadataByCommand, expectedCommands); }); }); describe('HistoryAutocompleteDataProvider', () => { it('should create completions with no history', async () => { const provider = new ProtocolMonitor.ProtocolMonitor.CommandAutocompleteSuggestionProvider(); assert.deepEqual(await provider.buildTextPromptCompletions('test', 'test'), []); }); it('should build completions in the reverse insertion order', async () => { const provider = new ProtocolMonitor.ProtocolMonitor.CommandAutocompleteSuggestionProvider(); provider.addEntry('test1'); provider.addEntry('test2'); provider.addEntry('test3'); assert.deepEqual(await provider.buildTextPromptCompletions('test', 'test'), [ {text: 'test3'}, {text: 'test2'}, {text: 'test1'}, ]); provider.addEntry('test1'); assert.deepEqual(await provider.buildTextPromptCompletions('test', 'test'), [ {text: 'test1'}, {text: 'test3'}, {text: 'test2'}, ]); }); it('should limit the number of completions', async () => { const provider = new ProtocolMonitor.ProtocolMonitor.CommandAutocompleteSuggestionProvider(2); provider.addEntry('test1'); provider.addEntry('test2'); provider.addEntry('test3'); assert.deepEqual(await provider.buildTextPromptCompletions('test', 'test'), [ {text: 'test3'}, {text: 'test2'}, ]); }); }); });