File size: 7,925 Bytes
d810ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import { extractInjections } from './injectionParser.js';

describe('extractInjections', () => {
  const SHELL_TRIGGER = '!{';
  const AT_FILE_TRIGGER = '@{';

  describe('Basic Functionality', () => {
    it('should return an empty array if no trigger is present', () => {
      const prompt = 'This is a simple prompt without injections.';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toEqual([]);
    });

    it('should extract a single, simple injection', () => {
      const prompt = 'Run this command: !{ls -la}';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toEqual([
        {
          content: 'ls -la',
          startIndex: 18,
          endIndex: 27,
        },
      ]);
    });

    it('should extract multiple injections', () => {
      const prompt = 'First: !{cmd1}, Second: !{cmd2}';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toHaveLength(2);
      expect(result[0]).toEqual({
        content: 'cmd1',
        startIndex: 7,
        endIndex: 14,
      });
      expect(result[1]).toEqual({
        content: 'cmd2',
        startIndex: 24,
        endIndex: 31,
      });
    });

    it('should handle different triggers (e.g., @{)', () => {
      const prompt = 'Read this file: @{path/to/file.txt}';
      const result = extractInjections(prompt, AT_FILE_TRIGGER);
      expect(result).toEqual([
        {
          content: 'path/to/file.txt',
          startIndex: 16,
          endIndex: 35,
        },
      ]);
    });
  });

  describe('Positioning and Edge Cases', () => {
    it('should handle injections at the start and end of the prompt', () => {
      const prompt = '!{start} middle text !{end}';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toHaveLength(2);
      expect(result[0]).toEqual({
        content: 'start',
        startIndex: 0,
        endIndex: 8,
      });
      expect(result[1]).toEqual({
        content: 'end',
        startIndex: 21,
        endIndex: 27,
      });
    });

    it('should handle adjacent injections', () => {
      const prompt = '!{A}!{B}';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toHaveLength(2);
      expect(result[0]).toEqual({ content: 'A', startIndex: 0, endIndex: 4 });
      expect(result[1]).toEqual({ content: 'B', startIndex: 4, endIndex: 8 });
    });

    it('should handle empty injections', () => {
      const prompt = 'Empty: !{}';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toEqual([
        {
          content: '',
          startIndex: 7,
          endIndex: 10,
        },
      ]);
    });

    it('should trim whitespace within the content', () => {
      const prompt = '!{  \n command with space  \t }';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toEqual([
        {
          content: 'command with space',
          startIndex: 0,
          endIndex: 29,
        },
      ]);
    });

    it('should ignore similar patterns that are not the exact trigger', () => {
      const prompt = 'Not a trigger: !(cmd) or {cmd} or ! {cmd}';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toEqual([]);
    });

    it('should ignore extra closing braces before the trigger', () => {
      const prompt = 'Ignore this } then !{run}';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toEqual([
        {
          content: 'run',
          startIndex: 19,
          endIndex: 25,
        },
      ]);
    });

    it('should stop parsing at the first balanced closing brace (non-greedy)', () => {
      // This tests that the parser doesn't greedily consume extra closing braces
      const prompt = 'Run !{ls -l}} extra braces';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toEqual([
        {
          content: 'ls -l',
          startIndex: 4,
          endIndex: 12,
        },
      ]);
    });
  });

  describe('Nested Braces (Balanced)', () => {
    it('should correctly parse content with simple nested braces (e.g., JSON)', () => {
      const prompt = `Send JSON: !{curl -d '{"key": "value"}'}`;
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toHaveLength(1);
      expect(result[0].content).toBe(`curl -d '{"key": "value"}'`);
    });

    it('should correctly parse content with shell constructs (e.g., awk)', () => {
      const prompt = `Process text: !{awk '{print $1}' file.txt}`;
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toHaveLength(1);
      expect(result[0].content).toBe(`awk '{print $1}' file.txt`);
    });

    it('should correctly parse multiple levels of nesting', () => {
      const prompt = `!{level1 {level2 {level3}} suffix}`;
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toHaveLength(1);
      expect(result[0].content).toBe(`level1 {level2 {level3}} suffix`);
      expect(result[0].endIndex).toBe(prompt.length);
    });

    it('should correctly parse paths containing balanced braces', () => {
      const prompt = 'Analyze @{path/with/{braces}/file.txt}';
      const result = extractInjections(prompt, AT_FILE_TRIGGER);
      expect(result).toHaveLength(1);
      expect(result[0].content).toBe('path/with/{braces}/file.txt');
    });

    it('should correctly handle an injection containing the trigger itself', () => {
      // This works because the parser counts braces, it doesn't look for the trigger again until the current one is closed.
      const prompt = '!{echo "The trigger is !{ confusing }"}';
      const expectedContent = 'echo "The trigger is !{ confusing }"';
      const result = extractInjections(prompt, SHELL_TRIGGER);
      expect(result).toHaveLength(1);
      expect(result[0].content).toBe(expectedContent);
    });
  });

  describe('Error Handling (Unbalanced/Unclosed)', () => {
    it('should throw an error for a simple unclosed injection', () => {
      const prompt = 'This prompt has !{an unclosed trigger';
      expect(() => extractInjections(prompt, SHELL_TRIGGER)).toThrow(
        /Invalid syntax: Unclosed injection starting at index 16 \('!{'\)/,
      );
    });

    it('should throw an error if the prompt ends inside a nested block', () => {
      const prompt = 'This fails: !{outer {inner';
      expect(() => extractInjections(prompt, SHELL_TRIGGER)).toThrow(
        /Invalid syntax: Unclosed injection starting at index 12 \('!{'\)/,
      );
    });

    it('should include the context name in the error message if provided', () => {
      const prompt = 'Failing !{command';
      const contextName = 'test-command';
      expect(() =>
        extractInjections(prompt, SHELL_TRIGGER, contextName),
      ).toThrow(
        /Invalid syntax in command 'test-command': Unclosed injection starting at index 8/,
      );
    });

    it('should throw if content contains unbalanced braces (e.g., missing closing)', () => {
      // This is functionally the same as an unclosed injection from the parser's perspective.
      const prompt = 'Analyze @{path/with/braces{example.txt}';
      expect(() => extractInjections(prompt, AT_FILE_TRIGGER)).toThrow(
        /Invalid syntax: Unclosed injection starting at index 8 \('@{'\)/,
      );
    });

    it('should clearly state that unbalanced braces in content are not supported in the error', () => {
      const prompt = 'Analyze @{path/with/braces{example.txt}';
      expect(() => extractInjections(prompt, AT_FILE_TRIGGER)).toThrow(
        /Paths or commands with unbalanced braces are not supported directly/,
      );
    });
  });
});