| 
 | 1 | +import * as assert from 'assert';  | 
 | 2 | +import { suite, test } from 'mocha';  | 
 | 3 | +//import { getGitHubPullRequestIdentityFromMaybeUrl } from '../github/models';  | 
 | 4 | +import { getGitHubPullRequestIdentityFromMaybeUrl, isMaybeGitHubPullRequestUrl } from '../github.utils';  | 
 | 5 | + | 
 | 6 | +suite('Test GitHub PR URL parsing to identity: getPullRequestIdentityFromMaybeUrl()', () => {  | 
 | 7 | +	function t(message: string, query: string, prNumber: string | undefined, ownerAndRepo?: string) {  | 
 | 8 | +		assert.deepStrictEqual(  | 
 | 9 | +			getGitHubPullRequestIdentityFromMaybeUrl(query),  | 
 | 10 | +			prNumber == null  | 
 | 11 | +				? undefined  | 
 | 12 | +				: {  | 
 | 13 | +						ownerAndRepo: ownerAndRepo,  | 
 | 14 | +						prNumber: prNumber,  | 
 | 15 | +						provider: 'github',  | 
 | 16 | +				  },  | 
 | 17 | +			`Parse: ${message} (${JSON.stringify(query)})`,  | 
 | 18 | +		);  | 
 | 19 | +		assert.equal(  | 
 | 20 | +			isMaybeGitHubPullRequestUrl(query),  | 
 | 21 | +			prNumber != null,  | 
 | 22 | +			`Check: ${message} (${JSON.stringify(query)})`,  | 
 | 23 | +		);  | 
 | 24 | +	}  | 
 | 25 | + | 
 | 26 | +	test('full URL or without protocol but with domain, should parse to ownerAndRepo and prNumber', () => {  | 
 | 27 | +		t('full URL', 'https://github.com/eamodio/vscode-gitlens/pull/1', '1', 'eamodio/vscode-gitlens');  | 
 | 28 | +		t(  | 
 | 29 | +			'with suffix',  | 
 | 30 | +			'https://github.com/eamodio/vscode-gitlens/pull/1/files?diff=unified#hello',  | 
 | 31 | +			'1',  | 
 | 32 | +			'eamodio/vscode-gitlens',  | 
 | 33 | +		);  | 
 | 34 | +		t(  | 
 | 35 | +			'with query',  | 
 | 36 | +			'https://github.com/eamodio/vscode-gitlens/pull/1?diff=unified#hello',  | 
 | 37 | +			'1',  | 
 | 38 | +			'eamodio/vscode-gitlens',  | 
 | 39 | +		);  | 
 | 40 | + | 
 | 41 | +		t('with anchor', 'https://github.com/eamodio/vscode-gitlens/pull/1#hello', '1', 'eamodio/vscode-gitlens');  | 
 | 42 | +		t('a weird suffix', 'https://github.com/eamodio/vscode-gitlens/pull/1-files', '1', 'eamodio/vscode-gitlens');  | 
 | 43 | +		t('numeric repo name', 'https://github.com/sergeibbb/1/pull/16', '16', 'sergeibbb/1');  | 
 | 44 | + | 
 | 45 | +		t('no protocol with leading slash', '/github.com/sergeibbb/1/pull/16?diff=unified', '16', 'sergeibbb/1');  | 
 | 46 | +		t('no protocol without leading slash', 'github.com/sergeibbb/1/pull/16/files', '16', 'sergeibbb/1');  | 
 | 47 | +	});  | 
 | 48 | + | 
 | 49 | +	test('no domain, should parse to ownerAndRepo and prNumber', () => {  | 
 | 50 | +		t('with leading slash', '/sergeibbb/1/pull/16#hello', '16', 'sergeibbb/1');  | 
 | 51 | +		t('words in repo name', 'eamodio/vscode-gitlens/pull/1?diff=unified#hello', '1', 'eamodio/vscode-gitlens');  | 
 | 52 | +		t('numeric repo name', 'sergeibbb/1/pull/16/files', '16', 'sergeibbb/1');  | 
 | 53 | +	});  | 
 | 54 | + | 
 | 55 | +	test('domain vs. no domain', () => {  | 
 | 56 | +		t(  | 
 | 57 | +			'with anchor',  | 
 | 58 | +			'https://github.com/eamodio/vscode-gitlens/pull/1#hello/sergeibbb/1/pull/16',  | 
 | 59 | +			'1',  | 
 | 60 | +			'eamodio/vscode-gitlens',  | 
 | 61 | +		);  | 
 | 62 | +	});  | 
 | 63 | + | 
 | 64 | +	test('numbers', () => {  | 
 | 65 | +		t('has "pull/" fragment', '/pull/16/files#hello', '16');  | 
 | 66 | +		t('has "pull/" fragment with double slash', '1//pull/16?diff=unified#hello', '16');  | 
 | 67 | +		t('with leading slash', '/16/files#hello', undefined);  | 
 | 68 | +		t('just a number', '16', undefined);  | 
 | 69 | +		t('with a hash', '#16', undefined);  | 
 | 70 | +	});  | 
 | 71 | + | 
 | 72 | +	test('does not match', () => {  | 
 | 73 | +		t('without leading slash', '16?diff=unified#hello', undefined);  | 
 | 74 | +		t('with leading hash', '/#16/files#hello', undefined);  | 
 | 75 | +		t('number is a part of a word', 'hello16', undefined);  | 
 | 76 | +		t('number is a part of a word', '16hello', undefined);  | 
 | 77 | + | 
 | 78 | +		t('GitLab', 'https://gitlab.com/eamodio/vscode-gitlens/-/merge_requests/16', undefined);  | 
 | 79 | + | 
 | 80 | +		t('with a number', '1/16?diff=unified#hello', undefined);  | 
 | 81 | +		t('with a number and slash', '/1/16?diff=unified#hello', undefined);  | 
 | 82 | +		t('with a word', 'anything/16?diff=unified#hello', undefined);  | 
 | 83 | + | 
 | 84 | +		t('with a wrong character leading to pull', 'sergeibbb/1/-pull/16?diff=unified#hello', undefined);  | 
 | 85 | +		t('with a wrong character leading to pull', 'sergeibbb/1-pull/16?diff=unified#hello', undefined);  | 
 | 86 | +	});  | 
 | 87 | +});  | 
0 commit comments