-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Expand file tree
/
Copy pathuseBanner.test.ts
More file actions
217 lines (171 loc) · 6.94 KB
/
Copy pathuseBanner.test.ts
File metadata and controls
217 lines (171 loc) · 6.94 KB
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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
describe,
it,
expect,
vi,
beforeEach,
afterEach,
type MockedFunction,
} from 'vitest';
import { renderHook } from '../../test-utils/render.js';
import { useBanner, _clearSessionBannersForTest } from './useBanner.js';
import { persistentState } from '../../utils/persistentState.js';
import crypto from 'node:crypto';
import chalk from 'chalk';
vi.mock('../../utils/persistentState.js', () => ({
persistentState: {
get: vi.fn(),
set: vi.fn(),
},
}));
vi.mock('../semantic-colors.js', () => ({
theme: {
status: {
warning: 'mock-warning-color',
},
ui: {
focus: 'mock-focus-color',
},
},
}));
vi.mock('../colors.js', () => ({
Colors: {
AccentBlue: 'mock-accent-blue',
},
}));
describe('useBanner', () => {
const mockedPersistentStateGet = persistentState.get as MockedFunction<
typeof persistentState.get
>;
const mockedPersistentStateSet = persistentState.set as MockedFunction<
typeof persistentState.set
>;
const defaultBannerData = {
defaultText: 'Standard Banner',
warningText: '',
};
beforeEach(() => {
vi.resetAllMocks();
_clearSessionBannersForTest();
// Default persistentState behavior: return empty object (no counts)
mockedPersistentStateGet.mockReturnValue({});
});
it('should return warning text and warning color if warningText is present', async () => {
const data = { defaultText: 'Standard', warningText: 'Critical Error' };
const { result } = await renderHook(() => useBanner(data));
expect(result.current.bannerText).toBe('Critical Error');
});
it('should hide banner if show count exceeds max limit (Legacy format)', async () => {
mockedPersistentStateGet.mockReturnValue({
[crypto
.createHash('sha256')
.update(defaultBannerData.defaultText)
.digest('hex')]: 5,
});
});
it('should not hide banner if show count exceeds max limit (Legacy format) if it contains an Antigravity announcement', async () => {
const antigravityBannerData = {
defaultText: 'Antigravity is coming to town!',
warningText: '',
};
mockedPersistentStateGet.mockReturnValue({
[crypto
.createHash('sha256')
.update(antigravityBannerData.defaultText)
.digest('hex')]: 5,
});
const { result } = await renderHook(() => useBanner(antigravityBannerData));
expect(result.current.bannerText).toContain(
'Antigravity is coming to town!',
);
});
it('should increment the persistent count when banner is shown', async () => {
const data = { defaultText: 'Tracker', warningText: '' };
// Current count is 1
mockedPersistentStateGet.mockReturnValue({
[crypto.createHash('sha256').update(data.defaultText).digest('hex')]: 1,
});
await renderHook(() => useBanner(data));
// Expect set to be called with incremented count
expect(mockedPersistentStateSet).toHaveBeenCalledWith(
'defaultBannerShownCount',
{
[crypto.createHash('sha256').update(data.defaultText).digest('hex')]: 2,
},
);
});
it('should increment count if warning text is shown instead', async () => {
const data = { defaultText: 'Standard', warningText: 'Warning' };
await renderHook(() => useBanner(data));
// Warning text now also gets counted
expect(mockedPersistentStateSet).toHaveBeenCalledWith(
'defaultBannerShownCount',
{
[crypto.createHash('sha256').update(data.warningText).digest('hex')]: 1,
},
);
});
it('should handle newline replacements', async () => {
const data = { defaultText: 'Line1\\nLine2', warningText: '' };
const { result } = await renderHook(() => useBanner(data));
expect(result.current.bannerText).toBe('Line1\nLine2');
});
describe('Antigravity installation commands', () => {
const originalPlatform = process.platform;
afterEach(() => {
Object.defineProperty(process, 'platform', { value: originalPlatform });
vi.unstubAllEnvs();
});
it('should append macOS & Linux install command when on darwin', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });
const data = { defaultText: 'Welcome to Antigravity!', warningText: '' };
const { result } = await renderHook(() => useBanner(data));
expect(result.current.bannerText).toBe(
`Welcome to Antigravity!\n \nTo install run "${chalk.bold('curl -fsSL https://antigravity.google/cli/install.sh | bash')}"`,
);
});
it('should append macOS & Linux install command when on linux', async () => {
Object.defineProperty(process, 'platform', { value: 'linux' });
const data = { defaultText: 'Welcome to Antigravity!', warningText: '' };
const { result } = await renderHook(() => useBanner(data));
expect(result.current.bannerText).toBe(
`Welcome to Antigravity!\n \nTo install run "${chalk.bold('curl -fsSL https://antigravity.google/cli/install.sh | bash')}"`,
);
});
it('should append Windows PowerShell install command when on win32 and PSModulePath is set', async () => {
Object.defineProperty(process, 'platform', { value: 'win32' });
vi.stubEnv('PSModulePath', 'C:\\some\\path');
const data = { defaultText: 'Welcome to Antigravity!', warningText: '' };
const { result } = await renderHook(() => useBanner(data));
expect(result.current.bannerText).toBe(
`Welcome to Antigravity!\n \nTo install run "${chalk.bold('irm https://antigravity.google/cli/install.ps1 | iex')}"`,
);
});
it('should append Windows CMD install command when on win32 and PSModulePath is not set', async () => {
Object.defineProperty(process, 'platform', { value: 'win32' });
vi.stubEnv('PSModulePath', '');
const data = { defaultText: 'Welcome to Antigravity!', warningText: '' };
const { result } = await renderHook(() => useBanner(data));
expect(result.current.bannerText).toBe(
`Welcome to Antigravity!\n \nTo install run "${chalk.bold('curl -fsSL https://antigravity.google/cli/install.cmd -o install.cmd && install.cmd && del install.cmd')}"`,
);
});
it('should not append install command if banner text does not contain Antigravity', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });
const data = { defaultText: 'Regular Banner', warningText: '' };
const { result } = await renderHook(() => useBanner(data));
expect(result.current.bannerText).toBe('Regular Banner');
});
it('should not append install command if process.platform is an unsupported platform', async () => {
Object.defineProperty(process, 'platform', { value: 'freebsd' });
const data = { defaultText: 'Welcome to Antigravity!', warningText: '' };
const { result } = await renderHook(() => useBanner(data));
expect(result.current.bannerText).toBe('Welcome to Antigravity!');
});
});
});