2025-05-23 10:25:17 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-06-03 10:12:47 -07:00
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
2025-10-25 14:41:53 -07:00
|
|
|
import { act } from 'react';
|
2025-10-30 11:50:26 -07:00
|
|
|
import { render } from '../../test-utils/render.js';
|
2025-05-23 10:25:17 -07:00
|
|
|
import { useLoadingIndicator } from './useLoadingIndicator.js';
|
|
|
|
|
import { StreamingState } from '../types.js';
|
2025-11-21 12:19:34 -05:00
|
|
|
import {
|
|
|
|
|
PHRASE_CHANGE_INTERVAL_MS,
|
|
|
|
|
INTERACTIVE_SHELL_WAITING_PHRASE,
|
|
|
|
|
} from './usePhraseCycler.js';
|
2025-11-18 12:32:31 +05:30
|
|
|
import { WITTY_LOADING_PHRASES } from '../constants/wittyPhrases.js';
|
2025-11-21 12:19:34 -05:00
|
|
|
import { INFORMATIVE_TIPS } from '../constants/tips.js';
|
2026-01-13 23:03:19 -05:00
|
|
|
import type { RetryAttemptPayload } from '@google/gemini-cli-core';
|
2025-05-23 10:25:17 -07:00
|
|
|
|
|
|
|
|
describe('useLoadingIndicator', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.useFakeTimers();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
2025-05-24 00:44:17 -07:00
|
|
|
vi.useRealTimers(); // Restore real timers after each test
|
2025-12-05 16:12:49 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2025-06-11 13:01:04 -07:00
|
|
|
act(() => vi.runOnlyPendingTimers);
|
2025-10-16 22:38:43 +05:30
|
|
|
vi.restoreAllMocks();
|
2025-05-23 10:25:17 -07:00
|
|
|
});
|
|
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
const renderLoadingIndicatorHook = async (
|
2025-10-25 14:41:53 -07:00
|
|
|
initialStreamingState: StreamingState,
|
2026-01-21 14:31:24 -08:00
|
|
|
initialShouldShowFocusHint: boolean = false,
|
2026-01-13 23:03:19 -05:00
|
|
|
initialRetryStatus: RetryAttemptPayload | null = null,
|
2026-03-23 19:30:48 -07:00
|
|
|
initialShowTips: boolean = true,
|
|
|
|
|
initialShowWit: boolean = true,
|
2026-02-27 14:15:10 -05:00
|
|
|
initialErrorVerbosity: 'low' | 'full' = 'full',
|
2025-10-25 14:41:53 -07:00
|
|
|
) => {
|
|
|
|
|
let hookResult: ReturnType<typeof useLoadingIndicator>;
|
|
|
|
|
function TestComponent({
|
|
|
|
|
streamingState,
|
2026-01-21 14:31:24 -08:00
|
|
|
shouldShowFocusHint,
|
2026-01-13 23:03:19 -05:00
|
|
|
retryStatus,
|
2026-03-23 19:30:48 -07:00
|
|
|
showTips,
|
|
|
|
|
showWit,
|
2026-02-27 14:15:10 -05:00
|
|
|
errorVerbosity,
|
2025-10-25 14:41:53 -07:00
|
|
|
}: {
|
|
|
|
|
streamingState: StreamingState;
|
2026-01-21 14:31:24 -08:00
|
|
|
shouldShowFocusHint?: boolean;
|
2026-01-13 23:03:19 -05:00
|
|
|
retryStatus?: RetryAttemptPayload | null;
|
2026-03-23 19:30:48 -07:00
|
|
|
showTips?: boolean;
|
|
|
|
|
showWit?: boolean;
|
|
|
|
|
errorVerbosity?: 'low' | 'full';
|
2025-10-25 14:41:53 -07:00
|
|
|
}) {
|
2026-01-21 14:31:24 -08:00
|
|
|
hookResult = useLoadingIndicator({
|
2025-11-21 12:19:34 -05:00
|
|
|
streamingState,
|
2026-01-21 14:31:24 -08:00
|
|
|
shouldShowFocusHint: !!shouldShowFocusHint,
|
|
|
|
|
retryStatus: retryStatus || null,
|
2026-03-23 19:30:48 -07:00
|
|
|
showTips,
|
|
|
|
|
showWit,
|
2026-02-27 14:15:10 -05:00
|
|
|
errorVerbosity,
|
2026-01-21 14:31:24 -08:00
|
|
|
});
|
2025-10-25 14:41:53 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
2026-03-23 19:30:48 -07:00
|
|
|
|
|
|
|
|
const { rerender, waitUntilReady } = await render(
|
2025-11-21 12:19:34 -05:00
|
|
|
<TestComponent
|
|
|
|
|
streamingState={initialStreamingState}
|
2026-01-21 14:31:24 -08:00
|
|
|
shouldShowFocusHint={initialShouldShowFocusHint}
|
2026-01-13 23:03:19 -05:00
|
|
|
retryStatus={initialRetryStatus}
|
2026-03-23 19:30:48 -07:00
|
|
|
showTips={initialShowTips}
|
|
|
|
|
showWit={initialShowWit}
|
2026-02-27 14:15:10 -05:00
|
|
|
errorVerbosity={initialErrorVerbosity}
|
2025-11-21 12:19:34 -05:00
|
|
|
/>,
|
2025-10-25 14:41:53 -07:00
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
result: {
|
|
|
|
|
get current() {
|
|
|
|
|
return hookResult;
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-23 19:30:48 -07:00
|
|
|
rerender: async (newProps: {
|
2025-11-21 12:19:34 -05:00
|
|
|
streamingState: StreamingState;
|
2026-01-21 14:31:24 -08:00
|
|
|
shouldShowFocusHint?: boolean;
|
2026-01-13 23:03:19 -05:00
|
|
|
retryStatus?: RetryAttemptPayload | null;
|
2026-03-23 19:30:48 -07:00
|
|
|
showTips?: boolean;
|
|
|
|
|
showWit?: boolean;
|
2026-02-27 14:15:10 -05:00
|
|
|
errorVerbosity?: 'low' | 'full';
|
2026-03-23 19:30:48 -07:00
|
|
|
}) => {
|
2026-02-27 14:15:10 -05:00
|
|
|
rerender(
|
|
|
|
|
<TestComponent
|
2026-03-23 19:30:48 -07:00
|
|
|
showTips={initialShowTips}
|
|
|
|
|
showWit={initialShowWit}
|
2026-02-27 14:15:10 -05:00
|
|
|
errorVerbosity={initialErrorVerbosity}
|
|
|
|
|
{...newProps}
|
|
|
|
|
/>,
|
2026-03-23 19:30:48 -07:00
|
|
|
);
|
|
|
|
|
await waitUntilReady();
|
|
|
|
|
},
|
|
|
|
|
waitUntilReady,
|
2025-10-25 14:41:53 -07:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
it('should initialize with default values when Idle', async () => {
|
2025-10-16 22:38:43 +05:30
|
|
|
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result } = await renderLoadingIndicatorHook(StreamingState.Idle);
|
2025-05-23 10:25:17 -07:00
|
|
|
expect(result.current.elapsedTime).toBe(0);
|
2026-02-10 09:36:20 -08:00
|
|
|
expect(result.current.currentLoadingPhrase).toBeUndefined();
|
2025-05-23 10:25:17 -07:00
|
|
|
});
|
|
|
|
|
|
2026-01-21 14:31:24 -08:00
|
|
|
it('should show interactive shell waiting phrase when shouldShowFocusHint is true', async () => {
|
2025-11-21 12:19:34 -05:00
|
|
|
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result, rerender } = await renderLoadingIndicatorHook(
|
2025-11-21 12:19:34 -05:00
|
|
|
StreamingState.Responding,
|
2026-01-21 14:31:24 -08:00
|
|
|
false,
|
2025-11-21 12:19:34 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
2026-03-23 19:30:48 -07:00
|
|
|
await rerender({
|
2026-01-21 14:31:24 -08:00
|
|
|
streamingState: StreamingState.Responding,
|
|
|
|
|
shouldShowFocusHint: true,
|
|
|
|
|
});
|
2025-11-21 12:19:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.current.currentLoadingPhrase).toBe(
|
|
|
|
|
INTERACTIVE_SHELL_WAITING_PHRASE,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-09 12:19:42 -07:00
|
|
|
it('should reflect values when Responding', async () => {
|
2025-11-18 12:32:31 +05:30
|
|
|
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty for subsequent phrases
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result } = await renderLoadingIndicatorHook(
|
|
|
|
|
StreamingState.Responding,
|
|
|
|
|
);
|
2025-05-23 10:25:17 -07:00
|
|
|
|
2025-05-24 00:44:17 -07:00
|
|
|
expect(result.current.elapsedTime).toBe(0);
|
2025-05-23 10:25:17 -07:00
|
|
|
|
2025-06-09 12:19:42 -07:00
|
|
|
await act(async () => {
|
2025-07-04 17:04:05 -05:00
|
|
|
await vi.advanceTimersByTimeAsync(PHRASE_CHANGE_INTERVAL_MS + 1);
|
2025-05-23 10:25:17 -07:00
|
|
|
});
|
2025-06-09 12:19:42 -07:00
|
|
|
|
2026-03-23 19:30:48 -07:00
|
|
|
// Both tip and witty phrase are available in the currentLoadingPhrase because it defaults to tip if present
|
|
|
|
|
expect([...WITTY_LOADING_PHRASES, ...INFORMATIVE_TIPS]).toContain(
|
2025-06-03 10:12:47 -07:00
|
|
|
result.current.currentLoadingPhrase,
|
|
|
|
|
);
|
2025-05-23 10:25:17 -07:00
|
|
|
});
|
|
|
|
|
|
2025-06-09 12:19:42 -07:00
|
|
|
it('should show waiting phrase and retain elapsedTime when WaitingForConfirmation', async () => {
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result, rerender } = await renderLoadingIndicatorHook(
|
2025-10-25 14:41:53 -07:00
|
|
|
StreamingState.Responding,
|
2025-05-23 10:25:17 -07:00
|
|
|
);
|
2025-05-24 00:44:17 -07:00
|
|
|
|
2025-06-09 12:19:42 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await vi.advanceTimersByTimeAsync(60000);
|
2025-05-24 00:44:17 -07:00
|
|
|
});
|
|
|
|
|
expect(result.current.elapsedTime).toBe(60);
|
|
|
|
|
|
2026-03-23 19:30:48 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await rerender({ streamingState: StreamingState.WaitingForConfirmation });
|
2025-06-09 12:19:42 -07:00
|
|
|
});
|
2025-05-24 00:44:17 -07:00
|
|
|
|
|
|
|
|
expect(result.current.currentLoadingPhrase).toBe(
|
|
|
|
|
'Waiting for user confirmation...',
|
|
|
|
|
);
|
|
|
|
|
expect(result.current.elapsedTime).toBe(60); // Elapsed time should be retained
|
|
|
|
|
|
|
|
|
|
// Timer should not advance further
|
2025-06-09 12:19:42 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await vi.advanceTimersByTimeAsync(2000);
|
2025-05-24 00:44:17 -07:00
|
|
|
});
|
|
|
|
|
expect(result.current.elapsedTime).toBe(60);
|
2025-05-23 10:25:17 -07:00
|
|
|
});
|
|
|
|
|
|
2026-03-23 19:30:48 -07:00
|
|
|
it('should reset elapsedTime and cycle phrases when transitioning from WaitingForConfirmation to Responding', async () => {
|
2025-10-16 22:38:43 +05:30
|
|
|
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result, rerender } = await renderLoadingIndicatorHook(
|
2025-10-25 14:41:53 -07:00
|
|
|
StreamingState.Responding,
|
2025-05-23 10:25:17 -07:00
|
|
|
);
|
|
|
|
|
|
2025-06-09 12:19:42 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await vi.advanceTimersByTimeAsync(5000); // 5s
|
2025-05-23 10:25:17 -07:00
|
|
|
});
|
2025-05-24 00:44:17 -07:00
|
|
|
expect(result.current.elapsedTime).toBe(5);
|
2025-05-23 10:25:17 -07:00
|
|
|
|
2026-03-23 19:30:48 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await rerender({ streamingState: StreamingState.WaitingForConfirmation });
|
2025-06-09 12:19:42 -07:00
|
|
|
});
|
2025-05-24 00:44:17 -07:00
|
|
|
expect(result.current.elapsedTime).toBe(5);
|
2025-05-23 10:25:17 -07:00
|
|
|
expect(result.current.currentLoadingPhrase).toBe(
|
|
|
|
|
'Waiting for user confirmation...',
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-23 19:30:48 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await rerender({ streamingState: StreamingState.Responding });
|
2025-06-09 12:19:42 -07:00
|
|
|
});
|
2025-05-24 00:44:17 -07:00
|
|
|
expect(result.current.elapsedTime).toBe(0); // Should reset
|
2026-03-23 19:30:48 -07:00
|
|
|
expect([...WITTY_LOADING_PHRASES, ...INFORMATIVE_TIPS]).toContain(
|
2025-06-03 10:12:47 -07:00
|
|
|
result.current.currentLoadingPhrase,
|
|
|
|
|
);
|
2025-05-24 00:44:17 -07:00
|
|
|
|
2025-06-09 12:19:42 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await vi.advanceTimersByTimeAsync(1000);
|
2025-05-24 00:44:17 -07:00
|
|
|
});
|
|
|
|
|
expect(result.current.elapsedTime).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-09 12:19:42 -07:00
|
|
|
it('should reset timer and phrase when streamingState changes from Responding to Idle', async () => {
|
2025-10-16 22:38:43 +05:30
|
|
|
vi.spyOn(Math, 'random').mockImplementation(() => 0.5); // Always witty
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result, rerender } = await renderLoadingIndicatorHook(
|
2025-10-25 14:41:53 -07:00
|
|
|
StreamingState.Responding,
|
2025-05-24 00:44:17 -07:00
|
|
|
);
|
|
|
|
|
|
2025-06-09 12:19:42 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await vi.advanceTimersByTimeAsync(10000); // 10s
|
2025-05-24 00:44:17 -07:00
|
|
|
});
|
|
|
|
|
expect(result.current.elapsedTime).toBe(10);
|
|
|
|
|
|
2026-03-23 19:30:48 -07:00
|
|
|
await act(async () => {
|
|
|
|
|
await rerender({ streamingState: StreamingState.Idle });
|
2025-06-09 12:19:42 -07:00
|
|
|
});
|
2025-05-24 00:44:17 -07:00
|
|
|
|
|
|
|
|
expect(result.current.elapsedTime).toBe(0);
|
2026-02-10 09:36:20 -08:00
|
|
|
expect(result.current.currentLoadingPhrase).toBeUndefined();
|
2025-05-23 10:25:17 -07:00
|
|
|
});
|
2026-01-13 23:03:19 -05:00
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
it('should reflect retry status in currentLoadingPhrase when provided', async () => {
|
2026-01-13 23:03:19 -05:00
|
|
|
const retryStatus = {
|
|
|
|
|
model: 'gemini-pro',
|
|
|
|
|
attempt: 2,
|
|
|
|
|
maxAttempts: 3,
|
|
|
|
|
delayMs: 1000,
|
|
|
|
|
};
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result } = await renderLoadingIndicatorHook(
|
2026-01-13 23:03:19 -05:00
|
|
|
StreamingState.Responding,
|
|
|
|
|
false,
|
|
|
|
|
retryStatus,
|
|
|
|
|
);
|
|
|
|
|
|
2026-01-21 14:31:24 -08:00
|
|
|
expect(result.current.currentLoadingPhrase).toContain('Trying to reach');
|
|
|
|
|
expect(result.current.currentLoadingPhrase).toContain('Attempt 3/3');
|
2026-01-13 23:03:19 -05:00
|
|
|
});
|
2026-02-19 13:43:12 -05:00
|
|
|
|
2026-04-02 12:44:39 -07:00
|
|
|
it('should not show retry status phrase when idle', async () => {
|
|
|
|
|
const retryStatus = {
|
|
|
|
|
model: 'gemini-pro',
|
|
|
|
|
attempt: 2,
|
|
|
|
|
maxAttempts: 3,
|
|
|
|
|
delayMs: 1000,
|
|
|
|
|
};
|
|
|
|
|
const { result } = await renderLoadingIndicatorHook(
|
|
|
|
|
StreamingState.Idle,
|
|
|
|
|
false,
|
|
|
|
|
retryStatus,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.current.currentLoadingPhrase).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
it('should hide low-verbosity retry status for early retry attempts', async () => {
|
2026-02-27 14:15:10 -05:00
|
|
|
const retryStatus = {
|
|
|
|
|
model: 'gemini-pro',
|
|
|
|
|
attempt: 1,
|
|
|
|
|
maxAttempts: 5,
|
|
|
|
|
delayMs: 1000,
|
|
|
|
|
};
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result } = await renderLoadingIndicatorHook(
|
2026-02-27 14:15:10 -05:00
|
|
|
StreamingState.Responding,
|
|
|
|
|
false,
|
|
|
|
|
retryStatus,
|
2026-03-23 19:30:48 -07:00
|
|
|
true,
|
|
|
|
|
true,
|
2026-02-27 14:15:10 -05:00
|
|
|
'low',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.current.currentLoadingPhrase).not.toBe(
|
|
|
|
|
"This is taking a bit longer, we're still on it.",
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
it('should show a generic retry phrase in low error verbosity mode for later retries', async () => {
|
2026-02-27 14:15:10 -05:00
|
|
|
const retryStatus = {
|
|
|
|
|
model: 'gemini-pro',
|
|
|
|
|
attempt: 2,
|
|
|
|
|
maxAttempts: 5,
|
|
|
|
|
delayMs: 1000,
|
|
|
|
|
};
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result } = await renderLoadingIndicatorHook(
|
2026-02-27 14:15:10 -05:00
|
|
|
StreamingState.Responding,
|
|
|
|
|
false,
|
|
|
|
|
retryStatus,
|
2026-03-23 19:30:48 -07:00
|
|
|
true,
|
|
|
|
|
true,
|
2026-02-27 14:15:10 -05:00
|
|
|
'low',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.current.currentLoadingPhrase).toBe(
|
|
|
|
|
"This is taking a bit longer, we're still on it.",
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-23 19:30:48 -07:00
|
|
|
it('should show no phrases when showTips and showWit are false', async () => {
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result } = await renderLoadingIndicatorHook(
|
2026-02-19 13:43:12 -05:00
|
|
|
StreamingState.Responding,
|
|
|
|
|
false,
|
|
|
|
|
null,
|
2026-03-23 19:30:48 -07:00
|
|
|
false,
|
|
|
|
|
false,
|
2026-02-19 13:43:12 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.current.currentLoadingPhrase).toBeUndefined();
|
|
|
|
|
});
|
2025-05-23 10:25:17 -07:00
|
|
|
});
|