File size: 7,995 Bytes
7421850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import { parseSlashCommand } from './commands.js';
import { CommandKind, type SlashCommand } from '../ui/commands/types.js';

// Mock command structure for testing
const mockCommands: readonly SlashCommand[] = [
  {
    name: 'help',
    description: 'Show help',
    action: async () => {},
    kind: CommandKind.BUILT_IN,
  },
  {
    name: 'commit',
    description: 'Commit changes',
    action: async () => {},
    kind: CommandKind.USER_FILE,
  },
  {
    name: 'memory',
    description: 'Manage memory',
    altNames: ['mem'],
    subCommands: [
      {
        name: 'list',
        description: 'List memory files',
        action: async () => {},
        kind: CommandKind.BUILT_IN,
      },
      {
        name: 'clear',
        description: 'Clear memory',
        altNames: ['c'],
        action: async () => {},
        kind: CommandKind.BUILT_IN,
      },
    ],
    kind: CommandKind.BUILT_IN,
  },
];

describe('parseSlashCommand', () => {
  it('should parse a simple command without arguments', () => {
    const result = parseSlashCommand('/help', mockCommands);
    expect(result.commandToExecute?.name).toBe('help');
    expect(result.args).toBe('');
    expect(result.canonicalPath).toEqual(['help']);
  });

  it('should parse a simple command with arguments', () => {
    const result = parseSlashCommand(
      '/commit -m "Initial commit"',
      mockCommands,
    );
    expect(result.commandToExecute?.name).toBe('commit');
    expect(result.args).toBe('-m "Initial commit"');
    expect(result.canonicalPath).toEqual(['commit']);
  });

  it('should parse a subcommand', () => {
    const result = parseSlashCommand('/memory list', mockCommands);
    expect(result.commandToExecute?.name).toBe('list');
    expect(result.args).toBe('');
    expect(result.canonicalPath).toEqual(['memory', 'list']);
  });

  it('should parse a subcommand with arguments', () => {
    const result = parseSlashCommand(
      '/memory list some important data',
      mockCommands,
    );
    expect(result.commandToExecute?.name).toBe('list');
    expect(result.args).toBe('some important data');
    expect(result.canonicalPath).toEqual(['memory', 'list']);
  });

  it('should handle a command alias', () => {
    const result = parseSlashCommand('/mem list some data', mockCommands);
    expect(result.commandToExecute?.name).toBe('list');
    expect(result.args).toBe('some data');
    expect(result.canonicalPath).toEqual(['memory', 'list']);
  });

  it('should handle a subcommand alias', () => {
    const result = parseSlashCommand('/memory c', mockCommands);
    expect(result.commandToExecute?.name).toBe('clear');
    expect(result.args).toBe('');
    expect(result.canonicalPath).toEqual(['memory', 'clear']);
  });

  it('should return undefined for an unknown command', () => {
    const result = parseSlashCommand('/unknown', mockCommands);
    expect(result.commandToExecute).toBeUndefined();
    expect(result.args).toBe('unknown');
    expect(result.canonicalPath).toEqual([]);
  });

  it('should return the parent command if subcommand is unknown', () => {
    const result = parseSlashCommand(
      '/memory unknownsub some args',
      mockCommands,
    );
    expect(result.commandToExecute?.name).toBe('memory');
    expect(result.args).toBe('unknownsub some args');
    expect(result.canonicalPath).toEqual(['memory']);
  });

  it('should handle extra whitespace', () => {
    const result = parseSlashCommand(
      '  /memory   list  some data  ',
      mockCommands,
    );
    expect(result.commandToExecute?.name).toBe('list');
    expect(result.args).toBe('some data');
    expect(result.canonicalPath).toEqual(['memory', 'list']);
  });

  it('should return undefined if query does not start with a slash', () => {
    const result = parseSlashCommand('help', mockCommands);
    expect(result.commandToExecute).toBeUndefined();
  });

  it('should handle an empty query', () => {
    const result = parseSlashCommand('', mockCommands);
    expect(result.commandToExecute).toBeUndefined();
  });

  it('should handle a query with only a slash', () => {
    const result = parseSlashCommand('/', mockCommands);
    expect(result.commandToExecute).toBeUndefined();
    expect(result.args).toBe('');
    expect(result.canonicalPath).toEqual([]);
  });

  describe('backtracking', () => {
    const backtrackingCommands: readonly SlashCommand[] = [
      {
        name: 'parent',
        description: 'Parent command',
        kind: CommandKind.BUILT_IN,
        action: async () => {},
        subCommands: [
          {
            name: 'notakes',
            description: 'Subcommand that does not take arguments',
            kind: CommandKind.BUILT_IN,
            takesArgs: false,
            action: async () => {},
          },
          {
            name: 'takes',
            description: 'Subcommand that takes arguments',
            kind: CommandKind.BUILT_IN,
            takesArgs: true,
            action: async () => {},
          },
        ],
      },
    ];

    it('should backtrack to parent if subcommand has takesArgs: false and args are provided', () => {
      const result = parseSlashCommand(
        '/parent notakes some prompt',
        backtrackingCommands,
      );
      expect(result.commandToExecute?.name).toBe('parent');
      expect(result.args).toBe('notakes some prompt');
      expect(result.canonicalPath).toEqual(['parent']);
    });

    it('should NOT backtrack if subcommand has takesArgs: false but NO args are provided', () => {
      const result = parseSlashCommand('/parent notakes', backtrackingCommands);
      expect(result.commandToExecute?.name).toBe('notakes');
      expect(result.args).toBe('');
      expect(result.canonicalPath).toEqual(['parent', 'notakes']);
    });

    it('should NOT backtrack if subcommand has takesArgs: true and args are provided', () => {
      const result = parseSlashCommand(
        '/parent takes some args',
        backtrackingCommands,
      );
      expect(result.commandToExecute?.name).toBe('takes');
      expect(result.args).toBe('some args');
      expect(result.canonicalPath).toEqual(['parent', 'takes']);
    });

    it('should NOT backtrack if parent has NO action', () => {
      const noActionCommands: readonly SlashCommand[] = [
        {
          name: 'parent',
          description: 'Parent without action',
          kind: CommandKind.BUILT_IN,
          subCommands: [
            {
              name: 'notakes',
              description: 'Subcommand without args',
              kind: CommandKind.BUILT_IN,
              takesArgs: false,
              action: async () => {},
            },
          ],
        },
      ];
      const result = parseSlashCommand(
        '/parent notakes some args',
        noActionCommands,
      );
      // It stays with the subcommand because parent can't handle it
      expect(result.commandToExecute?.name).toBe('notakes');
      expect(result.args).toBe('some args');
      expect(result.canonicalPath).toEqual(['parent', 'notakes']);
    });

    it('should NOT backtrack if subcommand is NOT marked with takesArgs: false', () => {
      const result = parseSlashCommand(
        '/parent takes some args',
        backtrackingCommands,
      );
      expect(result.commandToExecute?.name).toBe('takes');
      expect(result.args).toBe('some args');
      expect(result.canonicalPath).toEqual(['parent', 'takes']);
    });

    it('should backtrack if subcommand has takesArgs: false and args are provided (like /plan copy foo)', () => {
      const result = parseSlashCommand(
        '/parent notakes some prompt',
        backtrackingCommands,
      );
      expect(result.commandToExecute?.name).toBe('parent');
      expect(result.args).toBe('notakes some prompt');
      expect(result.canonicalPath).toEqual(['parent']);
    });
  });
});