File size: 6,238 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { act, useCallback } from 'react';
import { vi } from 'vitest';
import { render } from '../../test-utils/render.js';
import {
  useConsoleMessages,
  useErrorCount,
  initializeConsoleStore,
} from './useConsoleMessages.js';
import { coreEvents } from '@google/gemini-cli-core';

vi.mock('@google/gemini-cli-core', async (importOriginal) => {
  const actual = await importOriginal();
  const handlers = new Map<string, (payload: unknown) => void>();

  return {
    ...(actual as Record<string, unknown>),
    coreEvents: {
      ...((actual as Record<string, unknown>)['coreEvents'] as Record<
        string,
        unknown
      >),
      on: vi.fn((event: string, handler: (payload: unknown) => void) => {
        handlers.set(event, handler);
      }),
      off: vi.fn((event: string) => {
        handlers.delete(event);
      }),
      // Helper for testing to trigger the handlers
      _trigger: (event: string, payload: unknown) => {
        handlers.get(event)?.(payload);
      },
    },
  };
});

describe('useConsoleMessages', () => {
  let unmounts: Array<() => void> = [];

  beforeEach(() => {
    vi.useFakeTimers();
    initializeConsoleStore();
  });

  afterEach(() => {
    for (const unmount of unmounts) {
      try {
        unmount();
      } catch {
        // Ignore unmount errors
      }
    }
    unmounts = [];
    vi.runOnlyPendingTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
  });

  const useTestableConsoleMessages = () => {
    const consoleMessages = useConsoleMessages();
    const log = useCallback((content: string) => {
      // @ts-expect-error - internal testing helper
      coreEvents._trigger('console-log', { type: 'log', content });
    }, []);
    const error = useCallback((content: string) => {
      // @ts-expect-error - internal testing helper
      coreEvents._trigger('console-log', { type: 'error', content });
    }, []);
    const clearConsoleMessages = useCallback(() => {
      initializeConsoleStore();
    }, []);
    return {
      consoleMessages,
      log,
      error,
      clearConsoleMessages,
    };
  };

  const renderConsoleMessagesHook = async () => {
    let hookResult: ReturnType<typeof useTestableConsoleMessages> | undefined;
    function TestComponent() {
      hookResult = useTestableConsoleMessages();
      return null;
    }
    const { unmount } = await render(<TestComponent />);
    unmounts.push(unmount);
    return {
      result: {
        get current() {
          return hookResult!;
        },
      },
      unmount,
    };
  };

  it('should initialize with an empty array of console messages', async () => {
    const { result } = await renderConsoleMessagesHook();
    expect(result.current.consoleMessages).toEqual([]);
  });

  it('should add a new message when log is called', async () => {
    const { result } = await renderConsoleMessagesHook();

    act(() => {
      result.current.log('Test message');
      vi.runAllTimers();
    });

    expect(result.current.consoleMessages).toEqual([
      { type: 'log', content: 'Test message', count: 1 },
    ]);
  });

  it('should batch and count identical consecutive messages', async () => {
    const { result } = await renderConsoleMessagesHook();

    act(() => {
      result.current.log('Test message');
      result.current.log('Test message');
      result.current.log('Test message');
      vi.runAllTimers();
    });

    expect(result.current.consoleMessages).toEqual([
      { type: 'log', content: 'Test message', count: 3 },
    ]);
  });

  it('should not batch different messages', async () => {
    const { result } = await renderConsoleMessagesHook();

    act(() => {
      result.current.log('First message');
      result.current.error('Second message');
      vi.runAllTimers();
    });

    expect(result.current.consoleMessages).toEqual([
      { type: 'log', content: 'First message', count: 1 },
      { type: 'error', content: 'Second message', count: 1 },
    ]);
  });
});

describe('useErrorCount', () => {
  let unmounts: Array<() => void> = [];

  beforeEach(() => {
    vi.useFakeTimers();
    initializeConsoleStore();
  });

  afterEach(() => {
    for (const unmount of unmounts) {
      try {
        unmount();
      } catch {
        // Ignore unmount errors
      }
    }
    unmounts = [];
    vi.runOnlyPendingTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
  });

  const renderErrorCountHook = async () => {
    let hookResult: ReturnType<typeof useErrorCount>;
    function TestComponent() {
      hookResult = useErrorCount();
      return null;
    }
    const { unmount } = await render(<TestComponent />);
    unmounts.push(unmount);
    return {
      result: {
        get current() {
          return hookResult;
        },
      },
      unmount,
    };
  };

  it('should initialize with an error count of 0', async () => {
    const { result } = await renderErrorCountHook();
    expect(result.current.errorCount).toBe(0);
  });

  it('should increment error count when an error is logged', async () => {
    const { result } = await renderErrorCountHook();
    act(() => {
      // @ts-expect-error - internal testing helper
      coreEvents._trigger('console-log', { type: 'error', content: 'error' });
      vi.runAllTimers();
    });
    expect(result.current.errorCount).toBe(1);
  });

  it('should not increment error count for non-error logs', async () => {
    const { result } = await renderErrorCountHook();
    act(() => {
      // @ts-expect-error - internal testing helper
      coreEvents._trigger('console-log', { type: 'log', content: 'log' });
      vi.runAllTimers();
    });
    expect(result.current.errorCount).toBe(0);
  });

  it('should clear the error count', async () => {
    const { result } = await renderErrorCountHook();
    act(() => {
      // @ts-expect-error - internal testing helper
      coreEvents._trigger('console-log', { type: 'error', content: 'error' });
      vi.runAllTimers();
    });
    expect(result.current.errorCount).toBe(1);

    act(() => {
      result.current.clearErrorCount();
    });
    expect(result.current.errorCount).toBe(0);
  });
});