@@ -197,7 +197,17 @@ func setupTest(t *testing.T) {
197197 // Initialize semaphore for tests
198198 semaphore = make (chan struct {}, 100 )
199199
200- t .Cleanup (resetSaveTimer )
200+ // Initialize caches for tests
201+ repoCacheMaxCostBytes = 1024 * 1024 // 1MB for test
202+ invalidRepoCacheMaxEntries = 100
203+ InitRepoCache ()
204+ InitInvalidRepoCache ()
205+
206+ t .Cleanup (func () {
207+ resetSaveTimer ()
208+ CloseRepoCache ()
209+ CloseInvalidRepoCache ()
210+ })
201211}
202212
203213func TestGitHandler_Integration (t * testing.T ) {
@@ -400,3 +410,74 @@ func TestAffectedCommitsHandler(t *testing.T) {
400410 })
401411 }
402412}
413+
414+ func TestTagsHandler (t * testing.T ) {
415+ setupTest (t )
416+
417+ tests := []struct {
418+ name string
419+ url string
420+ expectedCode int
421+ expectedTags map [string ]string
422+ }{
423+ {
424+ name : "Valid repo with tags" ,
425+ url : "https://github.com/oliverchang/osv-test.git" ,
426+ expectedCode : http .StatusOK ,
427+ expectedTags : map [string ]string {
428+ "v0.2" : "8d8242f545e9cec3e6d0d2e3f5bde8be1c659735" ,
429+ "branch-v0.1.1" : "4c155795426727ea05575bd5904321def23c03f4" ,
430+ "branch-v0.1.1-with-fix" : "b9b3fd4732695b83c3068b7b6a14bb372ec31f98" ,
431+ "branch_1_cherrypick_regress" : "febfac1940086bc1f6d3dc33fda0a1d1ba336209" ,
432+ "v0.1" : "a2ba949290915d445d34d0e8e9de2e7ce38198fc" ,
433+ "v0.1.1" : "b1c95a196f22d06fcf80df8c6691cd113d8fefff" ,
434+ },
435+ },
436+ {
437+ name : "Repo exist but no tags" ,
438+ // This repo hasn't gotten a commit in 8 years so should be fairly stable for our testing.
439+ url : "https://github.com/torvalds/test-tlb.git" ,
440+ expectedCode : http .StatusNoContent ,
441+ expectedTags : nil ,
442+ },
443+ {
444+ name : "Non-existent repo" ,
445+ url : "https://github.com/google/this-repo-does-not-exist-12345.git" ,
446+ expectedCode : http .StatusNotFound ,
447+ expectedTags : nil ,
448+ },
449+ }
450+
451+ for _ , tt := range tests {
452+ t .Run (tt .name , func (t * testing.T ) {
453+ req , err := http .NewRequest (http .MethodGet , "/tags?url=" + tt .url , nil )
454+ req .Header .Set ("Content-Type" , "application/json" )
455+ if err != nil {
456+ t .Fatal (err )
457+ }
458+ rr := httptest .NewRecorder ()
459+ tagsHandler (rr , req )
460+
461+ if status := rr .Code ; status != tt .expectedCode {
462+ t .Errorf ("handler returned wrong status code: got %v want %v" ,
463+ status , tt .expectedCode )
464+ }
465+
466+ if tt .expectedTags != nil {
467+ respBody := & pb.TagsResponse {}
468+ if err := protojson .Unmarshal (rr .Body .Bytes (), respBody ); err != nil {
469+ t .Fatalf ("Failed to unmarshal JSON response: %v" , err )
470+ }
471+
472+ gotTags := make (map [string ]string )
473+ for _ , ref := range respBody .GetTags () {
474+ gotTags [ref .GetLabel ()] = hex .EncodeToString (ref .GetHash ())
475+ }
476+
477+ if diff := cmp .Diff (tt .expectedTags , gotTags ); diff != "" {
478+ t .Errorf ("handler returned wrong tags (-want +got):\n %s" , diff )
479+ }
480+ }
481+ })
482+ }
483+ }
0 commit comments