@@ -688,3 +688,230 @@ func TestGetStarredReposPagination(t *testing.T) {
688688 t .Errorf ("expected 2 requests, got %d" , requestCount )
689689 }
690690}
691+
692+ func TestGetRepository (t * testing.T ) {
693+ repo := Repository {
694+ ID : 123 ,
695+ Name : "test-repo" ,
696+ FullName : "owner/test-repo" ,
697+ Description : "A test repository" ,
698+ Language : "Go" ,
699+ StarCount : 42 ,
700+ Owner : User {
701+ Login : "owner" ,
702+ ID : 1 ,
703+ },
704+ }
705+
706+ requestCount := 0
707+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
708+ requestCount ++
709+ if r .URL .Path != "/repos/owner/test-repo" {
710+ t .Errorf ("unexpected path: %s" , r .URL .Path )
711+ }
712+ w .Header ().Set ("Content-Type" , "application/json" )
713+ if err := json .NewEncoder (w ).Encode (repo ); err != nil {
714+ t .Fatalf ("encoding response: %v" , err )
715+ }
716+ }))
717+ defer server .Close ()
718+
719+ c := NewClient ("test-token" , WithBaseURL (server .URL ))
720+ result , err := c .GetRepository (context .Background (), "owner" , "test-repo" )
721+ if err != nil {
722+ t .Fatalf ("GetRepository() error: %v" , err )
723+ }
724+
725+ if result .Name != "test-repo" {
726+ t .Errorf ("expected name 'test-repo', got %q" , result .Name )
727+ }
728+ if result .Description != "A test repository" {
729+ t .Errorf ("expected description 'A test repository', got %q" , result .Description )
730+ }
731+ if result .StarCount != 42 {
732+ t .Errorf ("expected star count 42, got %d" , result .StarCount )
733+ }
734+ if requestCount != 1 {
735+ t .Errorf ("expected 1 request, got %d" , requestCount )
736+ }
737+ }
738+
739+ func TestGetRepositoryCaching (t * testing.T ) {
740+ repo := Repository {
741+ ID : 123 ,
742+ Name : "cached-repo" ,
743+ FullName : "owner/cached-repo" ,
744+ Description : "Cached repository" ,
745+ Owner : User {
746+ Login : "owner" ,
747+ ID : 1 ,
748+ },
749+ }
750+
751+ requestCount := 0
752+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
753+ requestCount ++
754+ w .Header ().Set ("Content-Type" , "application/json" )
755+ if err := json .NewEncoder (w ).Encode (repo ); err != nil {
756+ t .Fatalf ("encoding response: %v" , err )
757+ }
758+ }))
759+ defer server .Close ()
760+
761+ c := NewClient ("test-token" , WithBaseURL (server .URL ))
762+
763+ // First request - should hit API
764+ result1 , err := c .GetRepository (context .Background (), "owner" , "cached-repo" )
765+ if err != nil {
766+ t .Fatalf ("first GetRepository() error: %v" , err )
767+ }
768+ if result1 .Description != "Cached repository" {
769+ t .Errorf ("expected description 'Cached repository', got %q" , result1 .Description )
770+ }
771+ if requestCount != 1 {
772+ t .Errorf ("expected 1 request after first call, got %d" , requestCount )
773+ }
774+
775+ // Second request - should use cache
776+ result2 , err := c .GetRepository (context .Background (), "owner" , "cached-repo" )
777+ if err != nil {
778+ t .Fatalf ("second GetRepository() error: %v" , err )
779+ }
780+ if result2 .Description != "Cached repository" {
781+ t .Errorf ("expected cached description 'Cached repository', got %q" , result2 .Description )
782+ }
783+ if requestCount != 1 {
784+ t .Errorf ("expected still 1 request after second call (cache hit), got %d" , requestCount )
785+ }
786+ }
787+
788+ func TestCacheRepository (t * testing.T ) {
789+ repo := & Repository {
790+ ID : 456 ,
791+ Name : "pre-cached-repo" ,
792+ FullName : "owner/pre-cached-repo" ,
793+ Description : "Pre-cached repository" ,
794+ Owner : User {
795+ Login : "owner" ,
796+ ID : 1 ,
797+ },
798+ }
799+
800+ requestCount := 0
801+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
802+ requestCount ++
803+ t .Error ("should not hit API for pre-cached repo" )
804+ }))
805+ defer server .Close ()
806+
807+ c := NewClient ("test-token" , WithBaseURL (server .URL ))
808+
809+ // Pre-cache the repository
810+ c .CacheRepository (repo )
811+
812+ // Request should use cache and not hit API
813+ result , err := c .GetRepository (context .Background (), "owner" , "pre-cached-repo" )
814+ if err != nil {
815+ t .Fatalf ("GetRepository() error: %v" , err )
816+ }
817+ if result .Description != "Pre-cached repository" {
818+ t .Errorf ("expected cached description 'Pre-cached repository', got %q" , result .Description )
819+ }
820+ if requestCount != 0 {
821+ t .Errorf ("expected 0 requests (cache hit), got %d" , requestCount )
822+ }
823+ }
824+
825+ func TestClearRepoCache (t * testing.T ) {
826+ repo := Repository {
827+ ID : 789 ,
828+ Name : "clear-test-repo" ,
829+ FullName : "owner/clear-test-repo" ,
830+ Description : "Test clear cache" ,
831+ Owner : User {
832+ Login : "owner" ,
833+ ID : 1 ,
834+ },
835+ }
836+
837+ requestCount := 0
838+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
839+ requestCount ++
840+ w .Header ().Set ("Content-Type" , "application/json" )
841+ if err := json .NewEncoder (w ).Encode (repo ); err != nil {
842+ t .Fatalf ("encoding response: %v" , err )
843+ }
844+ }))
845+ defer server .Close ()
846+
847+ c := NewClient ("test-token" , WithBaseURL (server .URL ))
848+
849+ // First request
850+ _ , _ = c .GetRepository (context .Background (), "owner" , "clear-test-repo" )
851+ // Second request (should use cache)
852+ _ , _ = c .GetRepository (context .Background (), "owner" , "clear-test-repo" )
853+
854+ if requestCount != 1 {
855+ t .Errorf ("expected 1 request before clear, got %d" , requestCount )
856+ }
857+
858+ // Clear repo cache
859+ c .ClearRepoCache ()
860+
861+ // Third request (should not use cache)
862+ _ , _ = c .GetRepository (context .Background (), "owner" , "clear-test-repo" )
863+
864+ if requestCount != 2 {
865+ t .Errorf ("expected 2 requests after clear, got %d" , requestCount )
866+ }
867+ }
868+
869+ func TestGetStarredReposPopulatesCache (t * testing.T ) {
870+ repos := []Repository {
871+ {ID : 1 , Name : "repo1" , FullName : "owner/repo1" , Description : "First repo" },
872+ {ID : 2 , Name : "repo2" , FullName : "owner/repo2" , Description : "Second repo" },
873+ }
874+
875+ starredRequestCount := 0
876+ repoRequestCount := 0
877+
878+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
879+ if r .URL .Path == "/user/starred" {
880+ starredRequestCount ++
881+ w .Header ().Set ("Content-Type" , "application/json" )
882+ if err := json .NewEncoder (w ).Encode (repos ); err != nil {
883+ t .Fatalf ("encoding response: %v" , err )
884+ }
885+ } else if strings .HasPrefix (r .URL .Path , "/repos/" ) {
886+ repoRequestCount ++
887+ t .Error ("should not request individual repo when already cached from starred" )
888+ }
889+ }))
890+ defer server .Close ()
891+
892+ c := NewClient ("test-token" , WithBaseURL (server .URL ))
893+
894+ // Fetch starred repos (should populate cache)
895+ _ , err := c .GetStarredRepos (context .Background ())
896+ if err != nil {
897+ t .Fatalf ("GetStarredRepos() error: %v" , err )
898+ }
899+
900+ if starredRequestCount != 1 {
901+ t .Errorf ("expected 1 starred request, got %d" , starredRequestCount )
902+ }
903+
904+ // Now try to get one of those repos individually - should use cache
905+ result , err := c .GetRepository (context .Background (), "owner" , "repo1" )
906+ if err != nil {
907+ t .Fatalf ("GetRepository() error: %v" , err )
908+ }
909+
910+ if result .Description != "First repo" {
911+ t .Errorf ("expected description 'First repo', got %q" , result .Description )
912+ }
913+
914+ if repoRequestCount != 0 {
915+ t .Errorf ("expected 0 individual repo requests (should use cache), got %d" , repoRequestCount )
916+ }
917+ }
0 commit comments