|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +type RequestedVersion = Exclude<SlimPackument['requestedVersion'], null> |
| 4 | + |
| 5 | +function mockPackage(repository: RequestedVersion['repository']): RequestedVersion { |
| 6 | + return { |
| 7 | + _id: 'foo', |
| 8 | + name: 'foo', |
| 9 | + dist: { shasum: 'foo', signatures: [], tarball: '' }, |
| 10 | + _npmVersion: '', |
| 11 | + version: '0.1.0', |
| 12 | + repository, |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +describe('useRepositoryUrl', () => { |
| 17 | + it('should strip .git from repository URL', () => { |
| 18 | + const { repositoryUrl } = useRepositoryUrl( |
| 19 | + mockPackage({ |
| 20 | + type: 'git', |
| 21 | + url: 'git+https://github.com/agentmarkup/agentmarkup.git', |
| 22 | + }), |
| 23 | + ) |
| 24 | + |
| 25 | + expect(repositoryUrl.value).toBe('https://github.com/agentmarkup/agentmarkup') |
| 26 | + }) |
| 27 | + |
| 28 | + it('should append /tree/HEAD/{directory} for monorepo packages without .git', () => { |
| 29 | + const { repositoryUrl } = useRepositoryUrl( |
| 30 | + mockPackage({ |
| 31 | + type: 'git', |
| 32 | + url: 'git+https://github.com/agentmarkup/agentmarkup.git', |
| 33 | + directory: 'packages/vite', |
| 34 | + }), |
| 35 | + ) |
| 36 | + |
| 37 | + expect(repositoryUrl.value).toBe( |
| 38 | + 'https://github.com/agentmarkup/agentmarkup/tree/HEAD/packages/vite', |
| 39 | + ) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should return null when repository has no url', () => { |
| 43 | + // @ts-expect-error tests |
| 44 | + const { repositoryUrl } = useRepositoryUrl(mockPackage({})) |
| 45 | + expect(repositoryUrl.value).toBeNull() |
| 46 | + }) |
| 47 | + |
| 48 | + it('should return null when no repository field', () => { |
| 49 | + // @ts-expect-error tests |
| 50 | + const { repositoryUrl } = useRepositoryUrl(mockPackage()) |
| 51 | + expect(repositoryUrl.value).toBeNull() |
| 52 | + }) |
| 53 | + |
| 54 | + it('should handle plain HTTPS URLs without .git suffix', () => { |
| 55 | + const { repositoryUrl } = useRepositoryUrl(mockPackage({ url: 'https://github.com/nuxt/ui' })) |
| 56 | + expect(repositoryUrl.value).toBe('https://github.com/nuxt/ui') |
| 57 | + }) |
| 58 | + |
| 59 | + it('should handle directory with trailing slash', () => { |
| 60 | + const { repositoryUrl } = useRepositoryUrl( |
| 61 | + mockPackage({ |
| 62 | + url: 'git+https://github.com/org/repo.git', |
| 63 | + directory: 'packages/core/', |
| 64 | + }), |
| 65 | + ) |
| 66 | + |
| 67 | + expect(repositoryUrl.value).toBe('https://github.com/org/repo/tree/HEAD/packages/core/') |
| 68 | + }) |
| 69 | +}) |
0 commit comments