11import { isSHA , repoUrl , revisionUrl } from './urls' ;
22
33describe ( 'isSHA' , ( ) => {
4- it ( 'identifies SHA hashes' , ( ) => {
4+ it ( 'identifies short and full hex SHAs' , ( ) => {
5+ expect ( isSHA ( 'abcde' ) ) . toMatchInlineSnapshot ( `true` ) ;
56 expect ( isSHA ( 'abc123def' ) ) . toMatchInlineSnapshot ( `true` ) ;
67 expect ( isSHA ( 'abc123def456789012345678901234567890abcd' ) ) . toMatchInlineSnapshot ( `true` ) ;
8+ expect ( isSHA ( '1234567890123456789012345678901234567890' ) ) . toMatchInlineSnapshot ( `true` ) ;
9+ } ) ;
10+
11+ it ( 'identifies sha256-prefixed hashes' , ( ) => {
12+ expect ( isSHA ( 'sha256:abc123de' ) ) . toMatchInlineSnapshot ( `true` ) ;
713 expect ( isSHA ( 'sha256:abc123def456789012345678901234567890abcd' ) ) . toMatchInlineSnapshot ( `true` ) ;
14+ } ) ;
15+
16+ it ( 'rejects empty strings, branches, tags, and missing SHAs' , ( ) => {
17+ expect ( isSHA ( '' ) ) . toMatchInlineSnapshot ( `false` ) ;
18+ expect ( isSHA ( 'HEAD' ) ) . toMatchInlineSnapshot ( `false` ) ;
819 expect ( isSHA ( 'main' ) ) . toMatchInlineSnapshot ( `false` ) ;
20+ expect ( isSHA ( 'develop' ) ) . toMatchInlineSnapshot ( `false` ) ;
921 expect ( isSHA ( 'v1.0.0' ) ) . toMatchInlineSnapshot ( `false` ) ;
22+ expect ( isSHA ( 'v1.2.3' ) ) . toMatchInlineSnapshot ( `false` ) ;
23+ } ) ;
24+
25+ it ( 'rejects values outside the supported hex length and charset' , ( ) => {
26+ //too short for plain sha (needs 5–40 hex chars)
1027 expect ( isSHA ( 'abc' ) ) . toMatchInlineSnapshot ( `false` ) ;
28+ expect ( isSHA ( 'abcd' ) ) . toMatchInlineSnapshot ( `false` ) ;
29+ //too long for plain sha (>40)
30+ expect ( isSHA ( '12345678901234567890123456789012345678901' ) ) . toMatchInlineSnapshot ( `false` ) ;
31+ //uppercase is not matched by the lowercase-only regex
32+ expect ( isSHA ( 'ABCDEF' ) ) . toMatchInlineSnapshot ( `false` ) ;
33+ expect ( isSHA ( 'abc123DEF' ) ) . toMatchInlineSnapshot ( `false` ) ;
34+ //sha256 prefix with empty or too-short hash
35+ expect ( isSHA ( 'sha256:' ) ) . toMatchInlineSnapshot ( `false` ) ;
36+ expect ( isSHA ( 'sha256:abc' ) ) . toMatchInlineSnapshot ( `false` ) ;
1137 } ) ;
1238} ) ;
1339
1440describe ( 'repoUrl' , ( ) => {
15- it ( 'extracts repo URLs from various formats' , ( ) => {
41+ it ( 'extracts canonical https repo paths from common formats' , ( ) => {
1642 expect ( repoUrl ( 'https://github.com/argoproj/argo-cd.git' ) ) . toMatchInlineSnapshot (
1743 `"https://github.com/argoproj/argo-cd"` ,
1844 ) ;
@@ -22,24 +48,30 @@ describe('repoUrl', () => {
2248 expect ( repoUrl ( 'git@github.com:argoproj/argo-cd.git' ) ) . toMatchInlineSnapshot (
2349 `"https://github.com/argoproj/argo-cd"` ,
2450 ) ;
51+ expect ( repoUrl ( 'ssh://git@github.com/foo/bar.git' ) ) . toMatchInlineSnapshot (
52+ `"https://github.com/foo/bar"` ,
53+ ) ;
2554 expect ( repoUrl ( 'https://gitlab.com/group/project.git' ) ) . toMatchInlineSnapshot (
2655 `"https://gitlab.com/group/project"` ,
2756 ) ;
2857 expect ( repoUrl ( 'https://bitbucket.org/team/repo.git' ) ) . toMatchInlineSnapshot (
2958 `"https://bitbucket.org/team/repo"` ,
3059 ) ;
60+ } ) ;
61+
62+ it ( 'returns null for empty, malformed, or unsupported providers' , ( ) => {
63+ expect ( repoUrl ( '' ) ) . toMatchInlineSnapshot ( `null` ) ;
64+ expect ( repoUrl ( 'not a url' ) ) . toMatchInlineSnapshot ( `null` ) ;
3165 expect ( repoUrl ( 'https://internal.example.com/repo.git' ) ) . toMatchInlineSnapshot ( `null` ) ;
66+ expect ( repoUrl ( 'https://gitea.io/foo/bar' ) ) . toMatchInlineSnapshot ( `null` ) ;
3267 } ) ;
3368} ) ;
3469
3570describe ( 'revisionUrl' , ( ) => {
36- it ( 'builds revision URLs for different providers ' , ( ) => {
71+ it ( 'builds commit URLs for SHA revisions ' , ( ) => {
3772 expect ( revisionUrl ( 'https://github.com/foo/bar.git' , 'abc123def' , false ) ) . toMatchInlineSnapshot (
3873 `"https://github.com/foo/bar/commit/abc123def"` ,
3974 ) ;
40- expect ( revisionUrl ( 'https://github.com/foo/bar.git' , 'main' , false ) ) . toMatchInlineSnapshot (
41- `"https://github.com/foo/bar/tree/main"` ,
42- ) ;
4375 expect ( revisionUrl ( 'https://gitlab.com/foo/bar.git' , 'abc123def' , false ) ) . toMatchInlineSnapshot (
4476 `"https://gitlab.com/foo/bar/-/commit/abc123def"` ,
4577 ) ;
@@ -49,8 +81,37 @@ describe('revisionUrl', () => {
4981 expect (
5082 revisionUrl ( 'https://bitbucket.org/foo/bar.git' , 'abc123def' , true ) ,
5183 ) . toMatchInlineSnapshot ( `"https://bitbucket.org/foo/bar/src/abc123def"` ) ;
84+ } ) ;
85+
86+ it ( 'builds tree/src URLs for branch names' , ( ) => {
87+ expect ( revisionUrl ( 'https://github.com/foo/bar.git' , 'main' , false ) ) . toMatchInlineSnapshot (
88+ `"https://github.com/foo/bar/tree/main"` ,
89+ ) ;
90+ expect ( revisionUrl ( 'https://gitlab.com/foo/bar.git' , 'main' , false ) ) . toMatchInlineSnapshot (
91+ `"https://gitlab.com/foo/bar/-/tree/main"` ,
92+ ) ;
93+ expect ( revisionUrl ( 'https://bitbucket.org/foo/bar.git' , 'main' , false ) ) . toMatchInlineSnapshot (
94+ `"https://bitbucket.org/foo/bar/src/main"` ,
95+ ) ;
96+ expect ( revisionUrl ( 'https://bitbucket.org/foo/bar.git' , 'main' , true ) ) . toMatchInlineSnapshot (
97+ `"https://bitbucket.org/foo/bar/src/main"` ,
98+ ) ;
99+ } ) ;
100+
101+ it ( 'defaults missing revision to HEAD' , ( ) => {
52102 expect ( revisionUrl ( 'https://github.com/foo/bar.git' , '' , false ) ) . toMatchInlineSnapshot (
53103 `"https://github.com/foo/bar/tree/HEAD"` ,
54104 ) ;
105+ expect ( revisionUrl ( 'https://github.com/foo/bar.git' , null as any , false ) ) . toMatchInlineSnapshot (
106+ `"https://github.com/foo/bar/tree/HEAD"` ,
107+ ) ;
108+ } ) ;
109+
110+ it ( 'returns null for empty, malformed, or unsupported repo URLs' , ( ) => {
111+ expect ( revisionUrl ( '' , 'abc123' , false ) ) . toMatchInlineSnapshot ( `null` ) ;
112+ expect ( revisionUrl ( 'not a url' , 'abc123' , false ) ) . toMatchInlineSnapshot ( `null` ) ;
113+ expect ( revisionUrl ( 'https://gitea.io/foo/bar.git' , 'abc123' , false ) ) . toMatchInlineSnapshot (
114+ `null` ,
115+ ) ;
55116 } ) ;
56117} ) ;
0 commit comments