|
| 1 | +package rubygems |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/matzehuels/stacktower/pkg/integrations" |
| 13 | +) |
| 14 | + |
| 15 | +func TestClient_FetchGem(t *testing.T) { |
| 16 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | + if r.URL.Path == "/api/v1/gems/rails.json" { |
| 18 | + resp := gemResponse{ |
| 19 | + Name: "rails", |
| 20 | + Version: "7.1.0", |
| 21 | + Info: "Ruby on Rails is a full-stack web framework", |
| 22 | + Licenses: []string{"MIT"}, |
| 23 | + SourceCodeURI: "https://github.com/rails/rails", |
| 24 | + HomepageURI: "https://rubyonrails.org", |
| 25 | + Authors: "David Heinemeier Hansson", |
| 26 | + Downloads: 500000000, |
| 27 | + Dependencies: dependenciesResponse{ |
| 28 | + Runtime: []dependencyInfo{ |
| 29 | + {Name: "activesupport", Requirements: "= 7.1.0"}, |
| 30 | + {Name: "actionpack", Requirements: "= 7.1.0"}, |
| 31 | + }, |
| 32 | + Development: []dependencyInfo{ |
| 33 | + {Name: "rake", Requirements: ">= 0"}, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + json.NewEncoder(w).Encode(resp) |
| 38 | + } else { |
| 39 | + http.NotFound(w, r) |
| 40 | + } |
| 41 | + })) |
| 42 | + defer server.Close() |
| 43 | + |
| 44 | + c, _ := NewClient(time.Hour) |
| 45 | + c.HTTP = server.Client() |
| 46 | + c.baseURL = server.URL + "/api/v1" |
| 47 | + |
| 48 | + info, err := c.FetchGem(context.Background(), "rails", false) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("FetchGem failed: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + if info.Name != "rails" { |
| 54 | + t.Errorf("expected name rails, got %s", info.Name) |
| 55 | + } |
| 56 | + if info.Version != "7.1.0" { |
| 57 | + t.Errorf("expected version 7.1.0, got %s", info.Version) |
| 58 | + } |
| 59 | + if len(info.Dependencies) != 2 { |
| 60 | + t.Errorf("expected 2 runtime dependencies, got %d", len(info.Dependencies)) |
| 61 | + } |
| 62 | + if info.License != "MIT" { |
| 63 | + t.Errorf("expected license MIT, got %s", info.License) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestClient_FetchGem_NotFound(t *testing.T) { |
| 68 | + server := httptest.NewServer(http.NotFoundHandler()) |
| 69 | + defer server.Close() |
| 70 | + |
| 71 | + c, _ := NewClient(time.Hour) |
| 72 | + c.HTTP = server.Client() |
| 73 | + c.baseURL = server.URL |
| 74 | + |
| 75 | + _, err := c.FetchGem(context.Background(), "missing-gem", false) |
| 76 | + if err == nil { |
| 77 | + t.Fatal("expected error for missing gem") |
| 78 | + } |
| 79 | + if !errors.Is(err, integrations.ErrNotFound) { |
| 80 | + t.Errorf("expected ErrNotFound, got %v", err) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestExtractDeps_RuntimeOnly(t *testing.T) { |
| 85 | + deps := dependenciesResponse{ |
| 86 | + Runtime: []dependencyInfo{ |
| 87 | + {Name: "activesupport", Requirements: ">= 0"}, |
| 88 | + {Name: "actionpack", Requirements: ">= 0"}, |
| 89 | + }, |
| 90 | + Development: []dependencyInfo{ |
| 91 | + {Name: "rake", Requirements: ">= 0"}, |
| 92 | + {Name: "rspec", Requirements: ">= 0"}, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + result := extractDeps(deps) |
| 97 | + if len(result) != 2 { |
| 98 | + t.Errorf("expected 2 runtime deps, got %d", len(result)) |
| 99 | + } |
| 100 | + |
| 101 | + // Verify only runtime deps are included |
| 102 | + hasRake := false |
| 103 | + for _, d := range result { |
| 104 | + if d == "rake" || d == "rspec" { |
| 105 | + hasRake = true |
| 106 | + } |
| 107 | + } |
| 108 | + if hasRake { |
| 109 | + t.Error("expected development dependencies to be excluded") |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestNormalizeName(t *testing.T) { |
| 114 | + tests := []struct { |
| 115 | + input string |
| 116 | + expected string |
| 117 | + }{ |
| 118 | + {"Rails", "rails"}, |
| 119 | + {" ActiveRecord ", "activerecord"}, |
| 120 | + {"UPPERCASE", "uppercase"}, |
| 121 | + {"some_gem", "some_gem"}, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.input, func(t *testing.T) { |
| 126 | + result := normalizeName(tt.input) |
| 127 | + if result != tt.expected { |
| 128 | + t.Errorf("expected %s, got %s", tt.expected, result) |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func TestJoinLicenses(t *testing.T) { |
| 135 | + tests := []struct { |
| 136 | + input []string |
| 137 | + expected string |
| 138 | + }{ |
| 139 | + {nil, ""}, |
| 140 | + {[]string{}, ""}, |
| 141 | + {[]string{"MIT"}, "MIT"}, |
| 142 | + {[]string{"MIT", "Apache-2.0"}, "MIT, Apache-2.0"}, |
| 143 | + } |
| 144 | + |
| 145 | + for _, tt := range tests { |
| 146 | + result := joinLicenses(tt.input) |
| 147 | + if result != tt.expected { |
| 148 | + t.Errorf("joinLicenses(%v): expected %s, got %s", tt.input, tt.expected, result) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
0 commit comments