From 7daf98dc070a62a9b9c3cd2683bb0166101812b3 Mon Sep 17 00:00:00 2001 From: sec-check Date: Fri, 18 Sep 2026 01:05:36 -0400 Subject: [PATCH] [quality] cover ETagCacheStats and MergeVerdictKey (0% -> 100%) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit go tool cover -func on main showed 0.0% for two exported contract functions in pkg/github: - ETagCacheStats (etagcache.go:109): exercised through etagCacheWrap and the existing etagServer helper so the test proves the stats come from the SHARED cache the transport chain installs — one charged fetch, one free 304 revalidation, hits/misses/entries deltas 1/1/1, with cleanup dropping the test's entry from the process-wide cache. - MergeVerdictKey (merge_verdict.go:50): table test pinning the Repo"#"Number spelling for bare and owner/name repos — the shared key contract between the governor's verdict map and the dashboard lookup (#7478). Closes #7552 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: sec-check --- src/pkg/github/etagcache_stats_test.go | 53 ++++++++++++++++++++++++++ src/pkg/github/merge_verdict_test.go | 26 +++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/pkg/github/etagcache_stats_test.go create mode 100644 src/pkg/github/merge_verdict_test.go diff --git a/src/pkg/github/etagcache_stats_test.go b/src/pkg/github/etagcache_stats_test.go new file mode 100644 index 0000000000..ec1a727c4b --- /dev/null +++ b/src/pkg/github/etagcache_stats_test.go @@ -0,0 +1,53 @@ +package github + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +// ETagCacheStats is the operator's window into #7430: it must report the +// SHARED cache — the one etagCacheWrap installs in every client's transport +// chain — not a private store. Exercise the shared cache through the wrapper +// and assert the deltas show up. +func TestETagCacheStats_ReportsSharedCache(t *testing.T) { + srv := &etagServer{body: `[{"number":7}]`, etag: `W/"stats-probe"`, remaining: 100} + ts := httptest.NewServer(http.HandlerFunc(srv.handler)) + defer ts.Close() + + url := ts.URL + "/repos/hivecommons/hive/issues?stats-probe=1" + // Drop this test's entry from the process-wide cache afterwards. + t.Cleanup(func() { + req, _ := http.NewRequest(http.MethodGet, url, nil) + req.Header.Set("Accept", "application/vnd.github+json") + req.Header.Set("Authorization", "Bearer tok-stats") + sharedETagCache.drop(etagCacheKey(req)) + }) + + hits0, misses0, entries0 := ETagCacheStats() + + tr := etagCacheWrap(http.DefaultTransport) + + // Charged fetch: one miss, one stored body. + resp, _ := etagGet(t, tr, url, "tok-stats") + if resp.StatusCode != http.StatusOK { + t.Fatalf("first fetch = %d, want 200", resp.StatusCode) + } + // Free revalidation: one hit. + resp, _ = etagGet(t, tr, url, "tok-stats") + if resp.StatusCode != http.StatusOK || resp.Header.Get(etagCacheHeader) != "revalidated" { + t.Fatalf("second fetch = %d (%s=%q), want a replayed 200", + resp.StatusCode, etagCacheHeader, resp.Header.Get(etagCacheHeader)) + } + + hits1, misses1, entries1 := ETagCacheStats() + if hits1-hits0 != 1 { + t.Errorf("hits delta = %d, want 1", hits1-hits0) + } + if misses1-misses0 != 1 { + t.Errorf("misses delta = %d, want 1", misses1-misses0) + } + if entries1-entries0 != 1 { + t.Errorf("entries delta = %d, want 1", entries1-entries0) + } +} diff --git a/src/pkg/github/merge_verdict_test.go b/src/pkg/github/merge_verdict_test.go new file mode 100644 index 0000000000..e890bbec4b --- /dev/null +++ b/src/pkg/github/merge_verdict_test.go @@ -0,0 +1,26 @@ +package github + +import "testing" + +// MergeVerdictKey is the contract between the governor (which records +// verdicts) and the dashboard (which looks them up): both must derive the +// same key from the same PullRequest, spelled exactly as the enumeration +// spelled Repo (#7478). Pin the format for both spellings. +func TestMergeVerdictKey(t *testing.T) { + cases := []struct { + name string + pr PullRequest + want string + }{ + {"bare repo", PullRequest{Repo: "hive", Number: 42}, "hive#42"}, + {"owner/name repo", PullRequest{Repo: "hivecommons/hive", Number: 7478}, "hivecommons/hive#7478"}, + {"zero value", PullRequest{}, "#0"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := MergeVerdictKey(tc.pr); got != tc.want { + t.Errorf("MergeVerdictKey(%q, %d) = %q, want %q", tc.pr.Repo, tc.pr.Number, got, tc.want) + } + }) + } +}