From cda1e3de1420861168df6e95f5008498a5e81e40 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov <53621505+mibragimov@users.noreply.github.com> Date: Wed, 7 Sep 2022 14:49:24 +0500 Subject: [PATCH] [Console] Fix bug in which selecting requests with characters ending with '{}' was not possible Co-authored-by: Muhammad Ibragimov --- .../console/public/lib/row_parser.test.ts | 91 +++++++++++++++++++ src/plugins/console/public/lib/row_parser.ts | 8 +- 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/plugins/console/public/lib/row_parser.test.ts diff --git a/src/plugins/console/public/lib/row_parser.test.ts b/src/plugins/console/public/lib/row_parser.test.ts new file mode 100644 index 0000000000000..d595e9841131d --- /dev/null +++ b/src/plugins/console/public/lib/row_parser.test.ts @@ -0,0 +1,91 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import '../application/models/legacy_core_editor/legacy_core_editor.test.mocks'; + +import RowParser from './row_parser'; +import { create, MODE } from '../application/models'; +import type { SenseEditor } from '../application/models'; +import type { CoreEditor } from '../types'; + +describe('RowParser', () => { + let editor: SenseEditor | null; + let parser: RowParser | null; + + beforeEach(function () { + // Set up our document body + document.body.innerHTML = `
+
+
+
+
`; + editor = create(document.getElementById('ConAppEditor')!); + parser = new RowParser(editor.getCoreEditor() as CoreEditor); + }); + + afterEach(function () { + editor?.getCoreEditor().destroy(); + editor = null; + parser = null; + }); + + describe('getRowParseMode', () => { + const forceRetokenize = false; + + it('should return MODE.BETWEEN_REQUESTS if line is empty', () => { + editor?.getCoreEditor().setValue('', forceRetokenize); + expect(parser?.getRowParseMode()).toBe(MODE.BETWEEN_REQUESTS); + }); + + it('should return MODE.BETWEEN_REQUESTS if line is a comment', () => { + editor?.getCoreEditor().setValue('// comment', forceRetokenize); + expect(parser?.getRowParseMode()).toBe(MODE.BETWEEN_REQUESTS); + }); + + it('should return MODE.REQUEST_START | MODE.REQUEST_END if line is a single line request', () => { + editor?.getCoreEditor().setValue('GET _search', forceRetokenize); + // eslint-disable-next-line no-bitwise + expect(parser?.getRowParseMode()).toBe(MODE.REQUEST_START | MODE.REQUEST_END); + }); + + it('should return MODE.IN_REQUEST if line is a request with an opening curly brace', () => { + editor?.getCoreEditor().setValue('{', forceRetokenize); + expect(parser?.getRowParseMode()).toBe(MODE.IN_REQUEST); + }); + + it('should return MODE.MULTI_DOC_CUR_DOC_END | MODE.IN_REQUEST if line is a multi doc request with an opening curly brace', () => { + editor?.getCoreEditor().setValue('GET _msearch\n{}\n{', forceRetokenize); + const lineNumber = editor?.getCoreEditor().getLineCount()! - 1; + expect(parser?.getRowParseMode(lineNumber)).toBe( + // eslint-disable-next-line no-bitwise + MODE.MULTI_DOC_CUR_DOC_END | MODE.IN_REQUEST + ); + }); + + it('should return MODE.MULTI_DOC_CUR_DOC_END | MODE.REQUEST_END if line is a multi doc request with a closing curly brace', () => { + editor?.getCoreEditor().setValue('GET _msearch\n{}\n{"foo": 1}\n', forceRetokenize); + const lineNumber = editor?.getCoreEditor().getLineCount()! - 1; + expect(parser?.getRowParseMode(lineNumber)).toBe( + // eslint-disable-next-line no-bitwise + MODE.MULTI_DOC_CUR_DOC_END | MODE.REQUEST_END + ); + }); + + it('should return MODE.REQUEST_START | MODE.REQUEST_END if line is a request with variables', () => { + editor?.getCoreEditor().setValue('GET /${exampleVariable}', forceRetokenize); + // eslint-disable-next-line no-bitwise + expect(parser?.getRowParseMode()).toBe(MODE.REQUEST_START | MODE.REQUEST_END); + }); + + it('should return MODE.REQUEST_END | MODE.MULTI_DOC_CUR_DOC_END for a request that ends with a curly closing brace', () => { + editor?.getCoreEditor().setValue('DELETE /_bar/_baz%{test}', forceRetokenize); + // eslint-disable-next-line no-bitwise + expect(parser?.getRowParseMode()).toBe(MODE.REQUEST_END | MODE.MULTI_DOC_CUR_DOC_END); + }); + }); +}); diff --git a/src/plugins/console/public/lib/row_parser.ts b/src/plugins/console/public/lib/row_parser.ts index 55014345ae3cc..2ecfbe61c952f 100644 --- a/src/plugins/console/public/lib/row_parser.ts +++ b/src/plugins/console/public/lib/row_parser.ts @@ -29,6 +29,8 @@ export default class RowParser { return MODE.BETWEEN_REQUESTS; } const mode = this.editor.getLineState(lineNumber); + const pos = this.editor.getCurrentPosition(); + const token = this.editor.getTokenAt(pos); if (!mode) { return MODE.BETWEEN_REQUESTS; @@ -57,8 +59,10 @@ export default class RowParser { return MODE.BETWEEN_REQUESTS; } // empty line or a comment waiting for a new req to start - if (line.indexOf('}', line.length - 1) >= 0) { - // check for a multi doc request (must start a new json doc immediately after this one end. + // If the line ends with a closing curly brace, it's the end of a request, + // and we should also check if the current token is not an url token + if (line.indexOf('}', line.length - 1) >= 0 && token?.type !== 'url.part') { + // check for a multi doc request must start a new json doc immediately after this one end. lineNumber++; if (lineNumber < linesCount + 1) { line = (this.editor.getLineValue(lineNumber) || '').trim();