|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + describe, |
| 9 | + it, |
| 10 | + expect, |
| 11 | + vi, |
| 12 | + beforeEach, |
| 13 | + type MockedFunction, |
| 14 | +} from 'vitest'; |
| 15 | +import { renderHook } from '../../test-utils/render.js'; |
| 16 | +import { useBanner } from './useBanner.js'; |
| 17 | +import { persistentState } from '../../utils/persistentState.js'; |
| 18 | +import type { Config } from '@google/gemini-cli-core'; |
| 19 | +import crypto from 'node:crypto'; |
| 20 | + |
| 21 | +vi.mock('../../utils/persistentState.js', () => ({ |
| 22 | + persistentState: { |
| 23 | + get: vi.fn(), |
| 24 | + set: vi.fn(), |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock('../semantic-colors.js', () => ({ |
| 29 | + theme: { |
| 30 | + status: { |
| 31 | + warning: 'mock-warning-color', |
| 32 | + }, |
| 33 | + }, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock('../colors.js', () => ({ |
| 37 | + Colors: { |
| 38 | + AccentBlue: 'mock-accent-blue', |
| 39 | + }, |
| 40 | +})); |
| 41 | + |
| 42 | +// Define the shape of the config methods used by this hook |
| 43 | +interface MockConfigShape { |
| 44 | + getPreviewFeatures: MockedFunction<() => boolean>; |
| 45 | +} |
| 46 | + |
| 47 | +describe('useBanner', () => { |
| 48 | + let mockConfig: MockConfigShape; |
| 49 | + const mockedPersistentStateGet = persistentState.get as MockedFunction< |
| 50 | + typeof persistentState.get |
| 51 | + >; |
| 52 | + const mockedPersistentStateSet = persistentState.set as MockedFunction< |
| 53 | + typeof persistentState.set |
| 54 | + >; |
| 55 | + |
| 56 | + const defaultBannerData = { |
| 57 | + defaultText: 'Standard Banner', |
| 58 | + warningText: '', |
| 59 | + }; |
| 60 | + |
| 61 | + beforeEach(() => { |
| 62 | + vi.resetAllMocks(); |
| 63 | + |
| 64 | + // Initialize the mock config with default behavior |
| 65 | + mockConfig = { |
| 66 | + getPreviewFeatures: vi.fn().mockReturnValue(false), |
| 67 | + }; |
| 68 | + |
| 69 | + // Default persistentState behavior: return empty object (no counts) |
| 70 | + mockedPersistentStateGet.mockReturnValue({}); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return warning text and warning color if warningText is present', () => { |
| 74 | + const data = { defaultText: 'Standard', warningText: 'Critical Error' }; |
| 75 | + |
| 76 | + const { result } = renderHook(() => |
| 77 | + useBanner(data, mockConfig as unknown as Config), |
| 78 | + ); |
| 79 | + |
| 80 | + expect(result.current.bannerText).toBe('Critical Error'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should NOT show default banner if preview features are enabled in config', () => { |
| 84 | + // Simulate Preview Features Enabled |
| 85 | + mockConfig.getPreviewFeatures.mockReturnValue(true); |
| 86 | + |
| 87 | + const { result } = renderHook(() => |
| 88 | + useBanner(defaultBannerData, mockConfig as unknown as Config), |
| 89 | + ); |
| 90 | + |
| 91 | + // Should fall back to warningText (which is empty) |
| 92 | + expect(result.current.bannerText).toBe(''); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should hide banner if show count exceeds max limit (Legacy format)', () => { |
| 96 | + mockedPersistentStateGet.mockReturnValue({ |
| 97 | + [crypto |
| 98 | + .createHash('sha256') |
| 99 | + .update(defaultBannerData.defaultText) |
| 100 | + .digest('hex')]: 5, |
| 101 | + }); |
| 102 | + |
| 103 | + const { result } = renderHook(() => |
| 104 | + useBanner(defaultBannerData, mockConfig as unknown as Config), |
| 105 | + ); |
| 106 | + |
| 107 | + expect(result.current.bannerText).toBe(''); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should increment the persistent count when banner is shown', () => { |
| 111 | + const data = { defaultText: 'Tracker', warningText: '' }; |
| 112 | + |
| 113 | + // Current count is 1 |
| 114 | + mockedPersistentStateGet.mockReturnValue({ |
| 115 | + [crypto.createHash('sha256').update(data.defaultText).digest('hex')]: 1, |
| 116 | + }); |
| 117 | + |
| 118 | + renderHook(() => useBanner(data, mockConfig as unknown as Config)); |
| 119 | + |
| 120 | + // Expect set to be called with incremented count |
| 121 | + expect(mockedPersistentStateSet).toHaveBeenCalledWith( |
| 122 | + 'defaultBannerShownCount', |
| 123 | + { |
| 124 | + [crypto.createHash('sha256').update(data.defaultText).digest('hex')]: 2, |
| 125 | + }, |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should NOT increment count if warning text is shown instead', () => { |
| 130 | + const data = { defaultText: 'Standard', warningText: 'Warning' }; |
| 131 | + |
| 132 | + renderHook(() => useBanner(data, mockConfig as unknown as Config)); |
| 133 | + |
| 134 | + // Since warning text takes precedence, default banner logic (and increment) is skipped |
| 135 | + expect(mockedPersistentStateSet).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should handle newline replacements', () => { |
| 139 | + const data = { defaultText: 'Line1\\nLine2', warningText: '' }; |
| 140 | + |
| 141 | + const { result } = renderHook(() => |
| 142 | + useBanner(data, mockConfig as unknown as Config), |
| 143 | + ); |
| 144 | + |
| 145 | + expect(result.current.bannerText).toBe('Line1\nLine2'); |
| 146 | + }); |
| 147 | +}); |
0 commit comments