[READ-ONLY] a fast, modern browser for the npm registry
at main 99 lines 3.3 kB view raw
1import { describe, expect, it } from 'vitest' 2import { areUrlsEquivalent, normalizeUrlForComparison } from '#shared/utils/url' 3 4describe('normalizeUrlForComparison', () => { 5 it('removes http protocol', () => { 6 expect(normalizeUrlForComparison('http://github.com/foo')).toBe('github.com/foo') 7 }) 8 9 it('removes https protocol', () => { 10 expect(normalizeUrlForComparison('https://github.com/foo')).toBe('github.com/foo') 11 }) 12 13 it('removes www prefix', () => { 14 expect(normalizeUrlForComparison('https://www.example.com/foo')).toBe('example.com/foo') 15 }) 16 17 it('removes trailing slashes', () => { 18 expect(normalizeUrlForComparison('https://github.com/foo/')).toBe('github.com/foo') 19 }) 20 21 it('removes hash fragments', () => { 22 expect(normalizeUrlForComparison('https://github.com/foo#readme')).toBe('github.com/foo') 23 }) 24 25 it('removes /tree/head path', () => { 26 expect(normalizeUrlForComparison('https://github.com/foo/bar/tree/head')).toBe( 27 'github.com/foo/bar', 28 ) 29 }) 30 31 it('removes /tree/main path', () => { 32 expect(normalizeUrlForComparison('https://github.com/foo/bar/tree/main')).toBe( 33 'github.com/foo/bar', 34 ) 35 }) 36 37 it('removes /tree/master path', () => { 38 expect(normalizeUrlForComparison('https://github.com/foo/bar/tree/master')).toBe( 39 'github.com/foo/bar', 40 ) 41 }) 42 43 it('removes /tree/HEAD/ with trailing content', () => { 44 expect(normalizeUrlForComparison('https://github.com/foo/bar/tree/HEAD/packages/core')).toBe( 45 'github.com/foo/bar/packages/core', 46 ) 47 }) 48 49 it('lowercases the URL', () => { 50 expect(normalizeUrlForComparison('https://GitHub.com/Foo/Bar')).toBe('github.com/foo/bar') 51 }) 52}) 53 54describe('areUrlsEquivalent', () => { 55 it('returns true for identical URLs', () => { 56 expect(areUrlsEquivalent('https://github.com/foo', 'https://github.com/foo')).toBe(true) 57 }) 58 59 it('returns true for URLs with different protocols', () => { 60 expect(areUrlsEquivalent('http://github.com/foo', 'https://github.com/foo')).toBe(true) 61 }) 62 63 it('returns true for URLs with/without www', () => { 64 expect(areUrlsEquivalent('https://www.example.com', 'https://example.com')).toBe(true) 65 }) 66 67 it('returns true for URLs with/without trailing slash', () => { 68 expect(areUrlsEquivalent('https://github.com/foo/', 'https://github.com/foo')).toBe(true) 69 }) 70 71 it('returns true for repo URL vs homepage with tree/HEAD', () => { 72 expect( 73 areUrlsEquivalent( 74 'https://github.com/nuxt/nuxt/tree/HEAD/packages/nuxt', 75 'https://github.com/nuxt/nuxt/packages/nuxt', 76 ), 77 ).toBe(true) 78 }) 79 80 it('returns true for repo URL vs homepage with tree/main', () => { 81 expect( 82 areUrlsEquivalent('https://github.com/foo/bar', 'https://github.com/foo/bar/tree/main'), 83 ).toBe(true) 84 }) 85 86 it('returns false for different repos', () => { 87 expect(areUrlsEquivalent('https://github.com/foo/bar', 'https://github.com/foo/baz')).toBe( 88 false, 89 ) 90 }) 91 92 it('returns false for different domains', () => { 93 expect(areUrlsEquivalent('https://github.com/foo', 'https://gitlab.com/foo')).toBe(false) 94 }) 95 96 it('returns false for repo URL vs separate homepage', () => { 97 expect(areUrlsEquivalent('https://github.com/nuxt/nuxt', 'https://nuxt.com')).toBe(false) 98 }) 99})