|
| 1 | +// Copyright (c) 2025 coze-dev |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 5 | +import type { RushConfigurationProject } from '@rushstack/rush-sdk'; |
| 6 | + |
| 7 | +import { resolveRegistry } from '../../src/utils/resolve-registry'; |
| 8 | + |
| 9 | +// Mock logger |
| 10 | +vi.mock('@coze-arch/logger', () => ({ |
| 11 | + logger: { |
| 12 | + info: vi.fn(), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +describe('resolveRegistry', () => { |
| 17 | + let mockProject: RushConfigurationProject; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + // 创建一个基础的 mock project |
| 21 | + const baseMockProject: Partial<RushConfigurationProject> = { |
| 22 | + packageName: '@test/package', |
| 23 | + packageJson: {}, |
| 24 | + }; |
| 25 | + mockProject = baseMockProject as RushConfigurationProject; |
| 26 | + }); |
| 27 | + |
| 28 | + it('should use CLI registry with highest priority', () => { |
| 29 | + mockProject.packageJson = { |
| 30 | + publishConfig: { registry: 'https://package.json.registry.com' }, |
| 31 | + }; |
| 32 | + |
| 33 | + const result = resolveRegistry(mockProject, 'https://cli.registry.com'); |
| 34 | + |
| 35 | + expect(result).toBe('https://cli.registry.com'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should use package.json publishConfig.registry when CLI is not provided', () => { |
| 39 | + mockProject.packageJson = { |
| 40 | + publishConfig: { registry: 'https://package.json.registry.com' }, |
| 41 | + }; |
| 42 | + |
| 43 | + const result = resolveRegistry(mockProject); |
| 44 | + |
| 45 | + expect(result).toBe('https://package.json.registry.com'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return undefined when no config is provided (use npm config)', () => { |
| 49 | + mockProject.packageJson = {}; |
| 50 | + |
| 51 | + const result = resolveRegistry(mockProject); |
| 52 | + |
| 53 | + expect(result).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle package.json without publishConfig', () => { |
| 57 | + mockProject.packageJson = { |
| 58 | + name: '@test/package', |
| 59 | + version: '1.0.0', |
| 60 | + }; |
| 61 | + |
| 62 | + const result = resolveRegistry(mockProject); |
| 63 | + |
| 64 | + expect(result).toBeUndefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle empty publishConfig in package.json', () => { |
| 68 | + mockProject.packageJson = { |
| 69 | + publishConfig: {}, |
| 70 | + }; |
| 71 | + |
| 72 | + const result = resolveRegistry(mockProject); |
| 73 | + |
| 74 | + expect(result).toBeUndefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle undefined CLI parameter explicitly', () => { |
| 78 | + mockProject.packageJson = {}; |
| 79 | + |
| 80 | + const result = resolveRegistry(mockProject, undefined); |
| 81 | + |
| 82 | + expect(result).toBeUndefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should prioritize CLI over package.json even with empty string', () => { |
| 86 | + mockProject.packageJson = { |
| 87 | + publishConfig: { registry: 'https://package.json.registry.com' }, |
| 88 | + }; |
| 89 | + |
| 90 | + const result = resolveRegistry(mockProject, ''); |
| 91 | + |
| 92 | + // Empty string is falsy, so should fall back to package.json |
| 93 | + expect(result).toBe('https://package.json.registry.com'); |
| 94 | + }); |
| 95 | +}); |
0 commit comments