|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import { validateVariablePath, parseVariablePath } from './parse_variable_path'; |
| 11 | + |
| 12 | +describe('validateVariablePath', () => { |
| 13 | + it('should validate a simple path', () => { |
| 14 | + expect(validateVariablePath('foo')).toBe(true); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should fail if any segment starts with a number', () => { |
| 18 | + expect(validateVariablePath('1foo')).toBe(false); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should fail if any segment contains a space', () => { |
| 22 | + expect(validateVariablePath('foo bar')).toBe(false); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should fail if any segment contains a special character', () => { |
| 26 | + expect(validateVariablePath('foo@bar')).toBe(false); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should validate a path with a dot', () => { |
| 30 | + expect(validateVariablePath('steps.snake_case')).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should fail on kebab-case if accessed with dot', () => { |
| 34 | + expect(validateVariablePath('steps.first-step')).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should validate a path with a brackets', () => { |
| 38 | + expect(validateVariablePath('foo["4-bar-a"]')).toBe(true); |
| 39 | + expect(validateVariablePath("foo['4-bar-a']")).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should fail if quotes are not closed or mixed', () => { |
| 43 | + expect(validateVariablePath(`steps["first-step']`)).toBe(false); |
| 44 | + expect(validateVariablePath('steps["first-step]')).toBe(false); |
| 45 | + expect(validateVariablePath(`steps['first-step]`)).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should validate a path with a dot and a bracket', () => { |
| 49 | + expect(validateVariablePath('foo.bar["baz"]')).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should validate a numeric index', () => { |
| 53 | + expect(validateVariablePath('steps[0]')).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should validate a path with filters', () => { |
| 57 | + expect(validateVariablePath('foo | title')).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should validate a path with multiple filters', () => { |
| 61 | + expect(validateVariablePath('foo | replace("foo", "bar") | capitalize')).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should validate a complex path with filters', () => { |
| 65 | + expect(validateVariablePath('steps.data["key"] | join(",") | upper')).toBe(true); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('parseVariablePath', () => { |
| 70 | + it('should parse a simple path without filters', () => { |
| 71 | + const result = parseVariablePath('foo'); |
| 72 | + expect(result).toEqual({ |
| 73 | + propertyPath: 'foo', |
| 74 | + filters: [], |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should parse a complex path without filters', () => { |
| 79 | + const result = parseVariablePath('steps.data["key"][0]'); |
| 80 | + expect(result).toEqual({ |
| 81 | + propertyPath: 'steps.data["key"][0]', |
| 82 | + filters: [], |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return errors for invalid paths', () => { |
| 87 | + const result = parseVariablePath('steps.data.kebab-case[0] | capitalize'); |
| 88 | + expect(result).toEqual({ |
| 89 | + errors: ['Invalid property path: steps.data.kebab-case[0]'], |
| 90 | + propertyPath: null, |
| 91 | + filters: [], |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should parse a path with a single filter', () => { |
| 96 | + const result = parseVariablePath('foo | title'); |
| 97 | + expect(result).toEqual({ |
| 98 | + propertyPath: 'foo', |
| 99 | + filters: ['title'], |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should parse a path with a filter that has arguments', () => { |
| 104 | + const result = parseVariablePath('foo | join(",")'); |
| 105 | + expect(result).toEqual({ |
| 106 | + propertyPath: 'foo', |
| 107 | + filters: ['join(",")'], |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should parse a path with multiple filters', () => { |
| 112 | + const result = parseVariablePath('foo | replace("foo", "bar") | capitalize'); |
| 113 | + expect(result).toEqual({ |
| 114 | + propertyPath: 'foo', |
| 115 | + filters: ['replace("foo", "bar")', 'capitalize'], |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should parse a complex path with multiple filters', () => { |
| 120 | + const result = parseVariablePath('steps.data["key"] | join(",") | upper | trim'); |
| 121 | + expect(result).toEqual({ |
| 122 | + propertyPath: 'steps.data["key"]', |
| 123 | + filters: ['join(",")', 'upper', 'trim'], |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should handle filters with nested parentheses', () => { |
| 128 | + const result = parseVariablePath('foo | replace("(test)", "bar") | title'); |
| 129 | + expect(result).toEqual({ |
| 130 | + propertyPath: 'foo', |
| 131 | + filters: ['replace("(test)", "bar")', 'title'], |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should handle filters with single quotes', () => { |
| 136 | + const result = parseVariablePath("foo | replace('old', 'new')"); |
| 137 | + expect(result).toEqual({ |
| 138 | + propertyPath: 'foo', |
| 139 | + filters: ["replace('old', 'new')"], |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle filters with named parameters', () => { |
| 144 | + const result = parseVariablePath('foo | replace(old="old", new="new")'); |
| 145 | + expect(result).toEqual({ |
| 146 | + propertyPath: 'foo', |
| 147 | + filters: ['replace(old="old", new="new")'], |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should handle spaces around pipes and filter names', () => { |
| 152 | + const result = parseVariablePath(' foo | title | upper '); |
| 153 | + expect(result).toEqual({ |
| 154 | + propertyPath: 'foo', |
| 155 | + filters: ['title', 'upper'], |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should return null for invalid variable paths', () => { |
| 160 | + expect(parseVariablePath('1foo | title')).toEqual({ |
| 161 | + errors: ['Invalid property path: 1foo'], |
| 162 | + propertyPath: null, |
| 163 | + filters: [], |
| 164 | + }); |
| 165 | + expect(parseVariablePath('foo@bar | title')).toEqual({ |
| 166 | + errors: ['Invalid property path: foo@bar'], |
| 167 | + propertyPath: null, |
| 168 | + filters: [], |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should return null for invalid filters', () => { |
| 173 | + expect(parseVariablePath('foo | 1invalid')).toEqual({ |
| 174 | + errors: ['Invalid filter name: 1invalid'], |
| 175 | + propertyPath: null, |
| 176 | + filters: [], |
| 177 | + }); |
| 178 | + |
| 179 | + expect(parseVariablePath('foo | title | $invalid')).toEqual({ |
| 180 | + errors: ['Invalid filter name: $invalid'], |
| 181 | + propertyPath: null, |
| 182 | + filters: [], |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments