File size: 5,548 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { resolveInput } from '../variable-resolver';

describe('resolveInput', () => {
  const context = {
    user: {
      name: 'John Doe',
      age: 30,
    },
    settings: {
      theme: 'dark',
      notifications: true,
    },
    specialValues: {
      nullValue: null,
      undefinedValue: undefined,
      emptyString: '',
      zero: 0,
      booleanFalse: false,
      booleanTrue: true,
    },
  };

  it('should return null for null input', () => {
    expect(resolveInput(null, context)).toBeNull();
  });

  it('should support special values', () => {
    expect(resolveInput('{{specialValues.nullValue}}', context)).toBeNull();
    expect(
      resolveInput('{{specialValues.undefinedValue}}', context),
    ).toBeUndefined();
    expect(resolveInput('{{specialValues.emptyString}}', context)).toBe('');
    expect(resolveInput('{{specialValues.zero}}', context)).toBe(0);
    expect(resolveInput('{{specialValues.booleanFalse}}', context)).toBe(false);
    expect(resolveInput('{{specialValues.booleanTrue}}', context)).toBe(true);
  });

  it('should return undefined for undefined input', () => {
    expect(resolveInput(undefined, context)).toBeUndefined();
  });

  it('should resolve a simple string variable', () => {
    expect(resolveInput('{{user.name}}', context)).toBe('John Doe');
  });

  it('should resolve multiple variables in a string', () => {
    expect(
      resolveInput('Name: {{user.name}}, Age: {{user.age}}', context),
    ).toBe('Name: John Doe, Age: 30');
  });

  it('should handle non-existent variables', () => {
    expect(resolveInput('{{user.email}}', context)).toBe(undefined);
  });

  it('should resolve variables in an array', () => {
    const input = ['{{user.name}}', '{{settings.theme}}', 'static'];
    const expected = ['John Doe', 'dark', 'static'];

    expect(resolveInput(input, context)).toEqual(expected);
  });

  it('should resolve variables in an object', () => {
    const input = {
      name: '{{user.name}}',
      theme: '{{settings.theme}}',
      static: 'value',
    };
    const expected = {
      name: 'John Doe',
      theme: 'dark',
      static: 'value',
    };

    expect(resolveInput(input, context)).toEqual(expected);
  });

  it('should handle nested objects and arrays', () => {
    const input = {
      user: {
        displayName: '{{user.name}}',
        preferences: ['{{settings.theme}}', '{{settings.notifications}}'],
      },
      staticData: [1, 2, 3],
    };
    const expected = {
      user: {
        displayName: 'John Doe',
        preferences: ['dark', true],
      },
      staticData: [1, 2, 3],
    };

    expect(resolveInput(input, context)).toEqual(expected);
  });

  it('should serialize an object variable embedded in a string', () => {
    expect(resolveInput('Log this message: {{user}}', context)).toBe(
      'Log this message: {"name":"John Doe","age":30}',
    );
  });

  it('should serialize an array variable embedded in a string', () => {
    expect(
      resolveInput('Themes: {{preferences}}', {
        preferences: ['dark', 'light'],
      }),
    ).toBe('Themes: ["dark","light"]');
  });

  it('should return the raw object when the whole string is a single variable', () => {
    expect(resolveInput('{{user}}', context)).toEqual({
      name: 'John Doe',
      age: 30,
    });
  });

  it('does not wrap string variables with double quotes', () => {
    expect(
      resolveInput('{ {{test}}: 2 }', {
        test: 'prop',
      }),
    ).toBe('{ prop: 2 }');
  });

  it('does not modify static JSON', () => {
    expect(resolveInput('{ "a": 2 }', {})).toBe('{ "a": 2 }');
  });

  it('supports variable as JSON object property name', () => {
    expect(
      resolveInput('{ "{{test}}": 2 }', {
        test: 'prop',
      }),
    ).toBe('{ "prop": 2 }');
  });

  it('supports variable as JSON number value', () => {
    expect(
      resolveInput('{ "a": {{test}} }', {
        test: 2,
      }),
    ).toBe('{ "a": 2 }');
  });

  it('supports variable as JSON string value', () => {
    expect(
      resolveInput('{ "a": "{{test}}" }', {
        test: 'str',
      }),
    ).toBe('{ "a": "str" }');
  });

  describe('bracket notation for keys with special characters', () => {
    it('should resolve variables with keys containing spaces', () => {
      const contextWithSpaces = {
        step: {
          'key with space': 'value from space key',
        },
      };
      expect(resolveInput('{{step.[key with space]}}', contextWithSpaces)).toBe(
        'value from space key',
      );
    });

    it('should resolve nested variables with keys containing spaces', () => {
      const contextWithSpaces = {
        step: {
          'first key': {
            'nested key': 'nested value',
          },
        },
      };
      expect(
        resolveInput('{{step.[first key].[nested key]}}', contextWithSpaces),
      ).toBe('nested value');
    });

    it('should resolve mixed normal and bracket notation paths', () => {
      const contextWithMixed = {
        step: {
          normal: {
            'key with space': 42,
          },
        },
      };
      expect(
        resolveInput('{{step.normal.[key with space]}}', contextWithMixed),
      ).toBe(42);
    });

    it('should resolve variables with keys containing dots', () => {
      const contextWithDots = {
        step: {
          'key.with.dots': 'dotted value',
        },
      };
      expect(resolveInput('{{step.[key.with.dots]}}', contextWithDots)).toBe(
        'dotted value',
      );
    });
  });
});