|
| 1 | +package dashboard |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "sync" |
| 11 | + "sync/atomic" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + ghpkg "github.com/hivecommons/hive/pkg/github" |
| 16 | +) |
| 17 | + |
| 18 | +// repoMetaServer serves GET /repos/{owner}/{repo} for a fixed set of repos and |
| 19 | +// counts how often each is read. When fail is set every read returns 500. |
| 20 | +type repoMetaServer struct { |
| 21 | + mu sync.Mutex |
| 22 | + reads map[string]int |
| 23 | + meta map[string]map[string]any |
| 24 | + fail atomic.Bool |
| 25 | +} |
| 26 | + |
| 27 | +func newRepoMetaServer(t *testing.T) (*repoMetaServer, *httptest.Server) { |
| 28 | + t.Helper() |
| 29 | + s := &repoMetaServer{ |
| 30 | + reads: map[string]int{}, |
| 31 | + meta: map[string]map[string]any{ |
| 32 | + "/repos/myorg/open": {"has_issues": true, "fork": false, "stargazers_count": 32, "forks_count": 4}, |
| 33 | + "/repos/myorg/fork-off": {"has_issues": false, "fork": true}, |
| 34 | + "/repos/other/off": {"has_issues": false, "fork": false}, |
| 35 | + }, |
| 36 | + } |
| 37 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 38 | + meta, ok := s.meta[r.URL.Path] |
| 39 | + if !ok { |
| 40 | + w.WriteHeader(http.StatusNotFound) |
| 41 | + return |
| 42 | + } |
| 43 | + s.mu.Lock() |
| 44 | + s.reads[r.URL.Path]++ |
| 45 | + s.mu.Unlock() |
| 46 | + if s.fail.Load() { |
| 47 | + w.WriteHeader(http.StatusInternalServerError) |
| 48 | + return |
| 49 | + } |
| 50 | + _ = json.NewEncoder(w).Encode(meta) |
| 51 | + })) |
| 52 | + t.Cleanup(srv.Close) |
| 53 | + return s, srv |
| 54 | +} |
| 55 | + |
| 56 | +func (s *repoMetaServer) count(path string) int { |
| 57 | + s.mu.Lock() |
| 58 | + defer s.mu.Unlock() |
| 59 | + return s.reads[path] |
| 60 | +} |
| 61 | + |
| 62 | +// The live projectbluefin hive (2026-11-08) sent GET /repos/{owner}/{repo} |
| 63 | +// 189 times an hour from the metrics collector, 177 of them charged: the |
| 64 | +// primary repo for stars/forks plus every watched repo for has_issues, on |
| 65 | +// every five-minute collect. Stars, forks and the Issues-disabled banner do |
| 66 | +// not need that: each repo's metadata is read at most once an hour, and the |
| 67 | +// primary repo's read serves both the outreach card and the banner. |
| 68 | +func TestMetricsCollector_RepoMetadataReadHourly(t *testing.T) { |
| 69 | + s, srv := newRepoMetaServer(t) |
| 70 | + gh := ghpkg.NewClientForTest(srv.URL, "myorg", []string{"open", "fork-off", "other/off"}, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 71 | + mc := issuesDisabledTestCollector(gh) |
| 72 | + now := time.Date(2026, 11, 8, 2, 0, 0, 0, time.UTC) |
| 73 | + mc.now = func() time.Time { return now } |
| 74 | + |
| 75 | + collect := func() map[string]any { |
| 76 | + out := mc.collectOutreach(context.Background()) |
| 77 | + mc.collectIssuesDisabled(context.Background()) |
| 78 | + return out |
| 79 | + } |
| 80 | + |
| 81 | + // Twelve collects, five minutes apart: one hour of the old cadence. |
| 82 | + var outreach map[string]any |
| 83 | + for i := 0; i < 12; i++ { |
| 84 | + outreach = collect() |
| 85 | + if i < 11 { |
| 86 | + now = now.Add(metricsCollectInterval) |
| 87 | + } |
| 88 | + } |
| 89 | + for _, path := range []string{"/repos/myorg/open", "/repos/myorg/fork-off", "/repos/other/off"} { |
| 90 | + if got := s.count(path); got != 1 { |
| 91 | + t.Errorf("GET %s = %d reads in 55 minutes, want 1", path, got) |
| 92 | + } |
| 93 | + } |
| 94 | + if outreach["stars"] != 32 || outreach["forks"] != 4 { |
| 95 | + t.Errorf("outreach = %v, want stars 32 and forks 4 from the held metadata", outreach) |
| 96 | + } |
| 97 | + if got := mc.GetIssuesDisabledRepos(); len(got) != 2 || got[0].Repo != "myorg/fork-off" || got[1].Repo != "other/off" { |
| 98 | + t.Errorf("issues-disabled repos = %+v, want myorg/fork-off and other/off from the held metadata", got) |
| 99 | + } |
| 100 | + |
| 101 | + // Past the hour, each repo is read once more and a change is seen. |
| 102 | + s.meta["/repos/myorg/fork-off"] = map[string]any{"has_issues": true, "fork": true} |
| 103 | + now = now.Add(repoMetadataRefreshInterval) |
| 104 | + collect() |
| 105 | + for _, path := range []string{"/repos/myorg/open", "/repos/myorg/fork-off", "/repos/other/off"} { |
| 106 | + if got := s.count(path); got != 2 { |
| 107 | + t.Errorf("GET %s = %d reads after the hour, want 2", path, got) |
| 108 | + } |
| 109 | + } |
| 110 | + if got := mc.GetIssuesDisabledRepos(); len(got) != 1 || got[0].Repo != "other/off" { |
| 111 | + t.Errorf("issues-disabled repos after refresh = %+v, want only other/off", got) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// A failed read is not held: the next collect asks again instead of waiting |
| 116 | +// an hour, and the banner keeps its previous verdict meanwhile. |
| 117 | +func TestMetricsCollector_RepoMetadataFailureRetriesNextCollect(t *testing.T) { |
| 118 | + s, srv := newRepoMetaServer(t) |
| 119 | + gh := ghpkg.NewClientForTest(srv.URL, "myorg", []string{"fork-off"}, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 120 | + mc := issuesDisabledTestCollector(gh) |
| 121 | + now := time.Date(2026, 11, 8, 2, 0, 0, 0, time.UTC) |
| 122 | + mc.now = func() time.Time { return now } |
| 123 | + |
| 124 | + mc.collectIssuesDisabled(context.Background()) |
| 125 | + if got := mc.GetIssuesDisabledRepos(); len(got) != 1 { |
| 126 | + t.Fatalf("initial verdict = %+v, want myorg/fork-off", got) |
| 127 | + } |
| 128 | + |
| 129 | + now = now.Add(repoMetadataRefreshInterval + time.Minute) |
| 130 | + s.fail.Store(true) |
| 131 | + mc.collectIssuesDisabled(context.Background()) |
| 132 | + if got := mc.GetIssuesDisabledRepos(); len(got) != 1 || got[0].Repo != "myorg/fork-off" { |
| 133 | + t.Fatalf("after a failed read = %+v, want the previous verdict kept", got) |
| 134 | + } |
| 135 | + s.fail.Store(false) |
| 136 | + now = now.Add(metricsCollectInterval) |
| 137 | + mc.collectIssuesDisabled(context.Background()) |
| 138 | + if got := s.count("/repos/myorg/fork-off"); got != 3 { |
| 139 | + t.Fatalf("reads = %d, want 3 (initial, failed, retried on the next collect)", got) |
| 140 | + } |
| 141 | +} |
0 commit comments