|
3 | 3 | globalResolverRegistry, |
4 | 4 | registerDefaultResolver, |
5 | 5 | resolveDefault, |
6 | | - getGitConfig |
| 6 | + getGitConfig, |
| 7 | + getNpmWhoami |
7 | 8 | } from '../src/resolvers'; |
8 | 9 |
|
9 | 10 | // Mock child_process.execSync for git config tests |
@@ -245,6 +246,75 @@ describe('Git Resolvers', () => { |
245 | 246 | }); |
246 | 247 | }); |
247 | 248 |
|
| 249 | +describe('NPM Resolvers', () => { |
| 250 | + beforeEach(() => { |
| 251 | + jest.clearAllMocks(); |
| 252 | + }); |
| 253 | + |
| 254 | + describe('getNpmWhoami', () => { |
| 255 | + it('should return npm username when logged in', () => { |
| 256 | + mockedExecSync.mockReturnValue('johndoe\n' as any); |
| 257 | + |
| 258 | + const result = getNpmWhoami(); |
| 259 | + |
| 260 | + expect(result).toBe('johndoe'); |
| 261 | + expect(mockedExecSync).toHaveBeenCalledWith( |
| 262 | + 'npm whoami', |
| 263 | + expect.objectContaining({ |
| 264 | + encoding: 'utf8', |
| 265 | + stdio: ['pipe', 'pipe', 'ignore'] |
| 266 | + }) |
| 267 | + ); |
| 268 | + }); |
| 269 | + |
| 270 | + it('should trim whitespace from npm username', () => { |
| 271 | + mockedExecSync.mockReturnValue(' janedoe \n' as any); |
| 272 | + |
| 273 | + const result = getNpmWhoami(); |
| 274 | + |
| 275 | + expect(result).toBe('janedoe'); |
| 276 | + }); |
| 277 | + |
| 278 | + it('should return undefined when not logged in to npm', () => { |
| 279 | + mockedExecSync.mockImplementation(() => { |
| 280 | + throw new Error('Not logged in'); |
| 281 | + }); |
| 282 | + |
| 283 | + const result = getNpmWhoami(); |
| 284 | + |
| 285 | + expect(result).toBeUndefined(); |
| 286 | + }); |
| 287 | + |
| 288 | + it('should return undefined when npm whoami returns empty string', () => { |
| 289 | + mockedExecSync.mockReturnValue('' as any); |
| 290 | + |
| 291 | + const result = getNpmWhoami(); |
| 292 | + |
| 293 | + expect(result).toBeUndefined(); |
| 294 | + }); |
| 295 | + }); |
| 296 | + |
| 297 | + describe('npm.whoami resolver', () => { |
| 298 | + it('should resolve npm username', async () => { |
| 299 | + mockedExecSync.mockReturnValue('npmuser\n' as any); |
| 300 | + |
| 301 | + const result = await globalResolverRegistry.resolve('npm.whoami'); |
| 302 | + |
| 303 | + expect(result).toBe('npmuser'); |
| 304 | + }); |
| 305 | + |
| 306 | + it('should return undefined when npm not logged in', async () => { |
| 307 | + mockedExecSync.mockImplementation(() => { |
| 308 | + throw new Error('Not logged in'); |
| 309 | + }); |
| 310 | + |
| 311 | + const result = await globalResolverRegistry.resolve('npm.whoami'); |
| 312 | + |
| 313 | + expect(result).toBeUndefined(); |
| 314 | + }); |
| 315 | + }); |
| 316 | +}); |
| 317 | + |
248 | 318 | describe('Date Resolvers', () => { |
249 | 319 | beforeEach(() => { |
250 | 320 | // Mock Date to return consistent values |
@@ -328,6 +398,10 @@ describe('Global Registry', () => { |
328 | 398 | expect(globalResolverRegistry.has('git.user.email')).toBe(true); |
329 | 399 | }); |
330 | 400 |
|
| 401 | + it('should have npm resolvers registered by default', () => { |
| 402 | + expect(globalResolverRegistry.has('npm.whoami')).toBe(true); |
| 403 | + }); |
| 404 | + |
331 | 405 | it('should have date resolvers registered by default', () => { |
332 | 406 | expect(globalResolverRegistry.has('date.year')).toBe(true); |
333 | 407 | expect(globalResolverRegistry.has('date.month')).toBe(true); |
|
0 commit comments