File size: 5,323 Bytes
84aa3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { SlashCommandConflictHandler } from './SlashCommandConflictHandler.js';
import {
  coreEvents,
  CoreEvent,
  type SlashCommandConflictsPayload,
  type SlashCommandConflict,
} from '@google/gemini-cli-core';
import { CommandKind } from '../ui/commands/types.js';

vi.mock('@google/gemini-cli-core', async (importOriginal) => {
  const actual =
    await importOriginal<typeof import('@google/gemini-cli-core')>();
  return {
    ...actual,
    coreEvents: {
      on: vi.fn(),
      off: vi.fn(),
      emitFeedback: vi.fn(),
    },
  };
});

describe('SlashCommandConflictHandler', () => {
  let handler: SlashCommandConflictHandler;

  /**
   * Helper to find and invoke the registered conflict event listener.
   */
  const simulateEvent = (conflicts: SlashCommandConflict[]) => {
    const callback = vi
      .mocked(coreEvents.on)
      .mock.calls.find(
        (call) => call[0] === CoreEvent.SlashCommandConflicts,
      )![1] as (payload: SlashCommandConflictsPayload) => void;
    callback({ conflicts });
  };

  beforeEach(() => {
    vi.useFakeTimers();
    handler = new SlashCommandConflictHandler();
    handler.start();
  });

  afterEach(() => {
    handler.stop();
    vi.clearAllMocks();
    vi.useRealTimers();
  });

  it('should listen for conflict events on start', () => {
    expect(coreEvents.on).toHaveBeenCalledWith(
      CoreEvent.SlashCommandConflicts,
      expect.any(Function),
    );
  });

  it('should display a descriptive message for a single extension conflict', () => {
    simulateEvent([
      {
        name: 'deploy',
        renamedTo: 'firebase.deploy',
        loserExtensionName: 'firebase',
        loserKind: CommandKind.EXTENSION_FILE,
        winnerKind: CommandKind.BUILT_IN,
      },
    ]);

    vi.advanceTimersByTime(600);

    expect(coreEvents.emitFeedback).toHaveBeenCalledWith(
      'info',
      "Extension 'firebase' command '/deploy' was renamed to '/firebase.deploy' because it conflicts with built-in command.",
    );
  });

  it('should display a descriptive message for a single MCP conflict', () => {
    simulateEvent([
      {
        name: 'pickle',
        renamedTo: 'test-server.pickle',
        loserMcpServerName: 'test-server',
        loserKind: CommandKind.MCP_PROMPT,
        winnerExtensionName: 'pickle-rick',
        winnerKind: CommandKind.EXTENSION_FILE,
      },
    ]);

    vi.advanceTimersByTime(600);

    expect(coreEvents.emitFeedback).toHaveBeenCalledWith(
      'info',
      "MCP server 'test-server' command '/pickle' was renamed to '/test-server.pickle' because it conflicts with extension 'pickle-rick' command.",
    );
  });

  it('should group multiple conflicts for the same command name', () => {
    simulateEvent([
      {
        name: 'launch',
        renamedTo: 'user.launch',
        loserKind: CommandKind.USER_FILE,
        winnerKind: CommandKind.WORKSPACE_FILE,
      },
      {
        name: 'launch',
        renamedTo: 'workspace.launch',
        loserKind: CommandKind.WORKSPACE_FILE,
        winnerKind: CommandKind.USER_FILE,
      },
    ]);

    vi.advanceTimersByTime(600);

    expect(coreEvents.emitFeedback).toHaveBeenCalledWith(
      'info',
      `Conflicts detected for command '/launch':
- User command '/launch' was renamed to '/user.launch'
- Workspace command '/launch' was renamed to '/workspace.launch'`,
    );
  });

  it('should debounce multiple events within the flush window', () => {
    simulateEvent([
      {
        name: 'a',
        renamedTo: 'user.a',
        loserKind: CommandKind.USER_FILE,
        winnerKind: CommandKind.BUILT_IN,
      },
    ]);

    vi.advanceTimersByTime(200);

    simulateEvent([
      {
        name: 'b',
        renamedTo: 'user.b',
        loserKind: CommandKind.USER_FILE,
        winnerKind: CommandKind.BUILT_IN,
      },
    ]);

    vi.advanceTimersByTime(600);

    // Should emit two feedbacks (one for each unique command name)
    expect(coreEvents.emitFeedback).toHaveBeenCalledTimes(2);
  });

  it('should deduplicate already notified conflicts', () => {
    const conflict = {
      name: 'deploy',
      renamedTo: 'firebase.deploy',
      loserExtensionName: 'firebase',
      loserKind: CommandKind.EXTENSION_FILE,
      winnerKind: CommandKind.BUILT_IN,
    };

    simulateEvent([conflict]);
    vi.advanceTimersByTime(600);
    expect(coreEvents.emitFeedback).toHaveBeenCalledTimes(1);

    vi.mocked(coreEvents.emitFeedback).mockClear();

    simulateEvent([conflict]);
    vi.advanceTimersByTime(600);
    expect(coreEvents.emitFeedback).not.toHaveBeenCalled();
  });

  it('should display a descriptive message for a skill conflict', () => {
    simulateEvent([
      {
        name: 'chat',
        renamedTo: 'google-workspace.chat',
        loserExtensionName: 'google-workspace',
        loserKind: CommandKind.SKILL,
        winnerKind: CommandKind.BUILT_IN,
      },
    ]);

    vi.advanceTimersByTime(600);

    expect(coreEvents.emitFeedback).toHaveBeenCalledWith(
      'info',
      "Extension 'google-workspace' skill '/chat' was renamed to '/google-workspace.chat' because it conflicts with built-in command.",
    );
  });
});