|
| 1 | +import { describe, it, expect } from 'bun:test'; |
| 2 | + |
| 3 | +import { splitGithubUrl } from '../src/github'; |
| 4 | + |
| 5 | +describe('splitGithubUrl', () => { |
| 6 | + describe('valid GitHub URLs', () => { |
| 7 | + it('should split a basic GitHub URL', () => { |
| 8 | + const url = 'https://github.com/gitbookio/gitbook/blob/master/README.md'; |
| 9 | + const result = splitGithubUrl(url); |
| 10 | + expect(result).toEqual({ |
| 11 | + orgName: 'gitbookio', |
| 12 | + repoName: 'gitbook', |
| 13 | + fileName: 'README.md', |
| 14 | + ref: 'master', |
| 15 | + lines: [], |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should split GitHub URL with line range', () => { |
| 20 | + const url = 'https://github.com/gitbookio/gitbook/blob/master/README.md#L1-L2'; |
| 21 | + const result = splitGithubUrl(url); |
| 22 | + expect(result).toEqual({ |
| 23 | + orgName: 'gitbookio', |
| 24 | + repoName: 'gitbook', |
| 25 | + fileName: 'README.md', |
| 26 | + ref: 'master', |
| 27 | + lines: [1, 2], |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should split GitHub URL with single line', () => { |
| 32 | + const url = 'https://github.com/gitbookio/gitbook/blob/master/README.md#L1'; |
| 33 | + const result = splitGithubUrl(url); |
| 34 | + expect(result).toEqual({ |
| 35 | + orgName: 'gitbookio', |
| 36 | + repoName: 'gitbook', |
| 37 | + fileName: 'README.md', |
| 38 | + ref: 'master', |
| 39 | + lines: [1, 1], |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle repo names with dots', () => { |
| 44 | + const url = 'https://github.com/vercel/next.js/blob/canary/package.json'; |
| 45 | + const result = splitGithubUrl(url); |
| 46 | + expect(result).toEqual({ |
| 47 | + orgName: 'vercel', |
| 48 | + repoName: 'next.js', |
| 49 | + fileName: 'package.json', |
| 50 | + ref: 'canary', |
| 51 | + lines: [], |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle repo names with hyphens and underscores', () => { |
| 56 | + const url = 'https://github.com/vercel/next-learn/blob/main/package.json'; |
| 57 | + const result = splitGithubUrl(url); |
| 58 | + expect(result).toEqual({ |
| 59 | + orgName: 'vercel', |
| 60 | + repoName: 'next-learn', |
| 61 | + fileName: 'package.json', |
| 62 | + ref: 'main', |
| 63 | + lines: [], |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle nested file paths', () => { |
| 68 | + const url = 'https://github.com/facebook/react/blob/main/src/index.js'; |
| 69 | + const result = splitGithubUrl(url); |
| 70 | + expect(result).toEqual({ |
| 71 | + orgName: 'facebook', |
| 72 | + repoName: 'react', |
| 73 | + fileName: 'src/index.js', |
| 74 | + ref: 'main', |
| 75 | + lines: [], |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should handle deeply nested file paths', () => { |
| 80 | + const url = |
| 81 | + 'https://github.com/microsoft/vscode/blob/main/src/vs/workbench/contrib/terminal/browser/terminal.ts'; |
| 82 | + const result = splitGithubUrl(url); |
| 83 | + expect(result).toEqual({ |
| 84 | + orgName: 'microsoft', |
| 85 | + repoName: 'vscode', |
| 86 | + fileName: 'src/vs/workbench/contrib/terminal/browser/terminal.ts', |
| 87 | + ref: 'main', |
| 88 | + lines: [], |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle URLs with query parameters', () => { |
| 93 | + const url = 'https://github.com/gitbookio/gitbook/blob/master/README.md?query=test'; |
| 94 | + const result = splitGithubUrl(url); |
| 95 | + expect(result).toEqual({ |
| 96 | + orgName: 'gitbookio', |
| 97 | + repoName: 'gitbook', |
| 98 | + fileName: 'README.md', |
| 99 | + ref: 'master', |
| 100 | + lines: [], |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should handle URLs with both query parameters and line numbers', () => { |
| 105 | + const url = |
| 106 | + 'https://github.com/gitbookio/gitbook/blob/master/README.md?query=test#L5-L10'; |
| 107 | + const result = splitGithubUrl(url); |
| 108 | + expect(result).toEqual({ |
| 109 | + orgName: 'gitbookio', |
| 110 | + repoName: 'gitbook', |
| 111 | + fileName: 'README.md', |
| 112 | + ref: 'master', |
| 113 | + lines: [5, 10], |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should handle HTTP URLs', () => { |
| 118 | + const url = 'http://github.com/gitbookio/gitbook/blob/master/README.md'; |
| 119 | + const result = splitGithubUrl(url); |
| 120 | + expect(result).toEqual({ |
| 121 | + orgName: 'gitbookio', |
| 122 | + repoName: 'gitbook', |
| 123 | + fileName: 'README.md', |
| 124 | + ref: 'master', |
| 125 | + lines: [], |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should handle organization names with hyphens', () => { |
| 130 | + const url = 'https://github.com/microsoft-docs/azure-docs/blob/main/README.md'; |
| 131 | + const result = splitGithubUrl(url); |
| 132 | + expect(result).toEqual({ |
| 133 | + orgName: 'microsoft-docs', |
| 134 | + repoName: 'azure-docs', |
| 135 | + fileName: 'README.md', |
| 136 | + ref: 'main', |
| 137 | + lines: [], |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe('invalid GitHub URLs', () => { |
| 143 | + it('should return undefined for non-GitHub URLs', () => { |
| 144 | + const url = 'https://gitlab.com/gitbookio/gitbook/blob/master/README.md'; |
| 145 | + const result = splitGithubUrl(url); |
| 146 | + expect(result).toBeUndefined(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should return undefined for malformed GitHub URLs', () => { |
| 150 | + const url = 'https://github.com/gitbookio/gitbook/README.md'; |
| 151 | + const result = splitGithubUrl(url); |
| 152 | + expect(result).toBeUndefined(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should return undefined for GitHub URLs without blob', () => { |
| 156 | + const url = 'https://github.com/gitbookio/gitbook/tree/master/README.md'; |
| 157 | + const result = splitGithubUrl(url); |
| 158 | + expect(result).toBeUndefined(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should return undefined for empty string', () => { |
| 162 | + const result = splitGithubUrl(''); |
| 163 | + expect(result).toBeUndefined(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should return undefined for malformed URLs', () => { |
| 167 | + const testCases = [ |
| 168 | + 'not-a-url', |
| 169 | + 'http://', |
| 170 | + 'https://', |
| 171 | + 'ftp://example.com', |
| 172 | + |
| 173 | + 'javascript:alert("test")', |
| 174 | + '://github.com/test/repo', |
| 175 | + 'github.com/test/repo', // missing protocol |
| 176 | + ]; |
| 177 | + |
| 178 | + for (const url of testCases) { |
| 179 | + const result = splitGithubUrl(url); |
| 180 | + expect(result).toBeUndefined(); |
| 181 | + } |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments