refactor: testing from the beginning. a journey in 150k lines of tests to none

This commit is contained in:
sam committed 2026-10-03 21:16:25 +10:00
1 parent ba37e37e53
commit ff424b70ac
651 files changed
-146443

No files matched your search

-61
View File
@@ -1,61 +0,0 @@
package sampling
import (
"context"
"testing"
"time"
)
func TestAllSampler(t *testing.T) {
t.Parallel()
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
candidates := []Candidate{
{ID: "a", At: base},
{ID: "b", At: base.Add(time.Hour)},
{ID: "c", At: base.Add(2 * time.Hour)},
}
tests := []struct {
name string
cap int
wantIDs []string
}{
{name: "no cap returns everything in input order", cap: 0, wantIDs: []string{"a", "b", "c"}},
{name: "cap truncates from the front", cap: 2, wantIDs: []string{"a", "b"}},
{name: "cap larger than candidate count returns everything", cap: 100, wantIDs: []string{"a", "b", "c"}},
{name: "negative cap means unlimited", cap: -1, wantIDs: []string{"a", "b", "c"}},
}
s := NewAll()
if s.Name() != "all" {
t.Fatalf("Name() = %q, want %q", s.Name(), "all")
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := s.Sample(context.Background(), candidates, Request{Cap: tt.cap})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
assertIDs(t, got, tt.wantIDs)
})
}
}
func TestAllSamplerEmptyInput(t *testing.T) {
t.Parallel()
got, err := NewAll().Sample(context.Background(), nil, Request{})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
if len(got) != 0 {
t.Fatalf("Sample() = %v, want empty", got)
}
}
func TestAllSamplerDoesNotMutateInput(t *testing.T) {
t.Parallel()
assertInputNotMutated(t, NewAll())
}
-140
View File
@@ -1,140 +0,0 @@
package sampling
import (
"context"
"testing"
"time"
)
func candidatesFor(n int) []Candidate {
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
out := make([]Candidate, n)
for i := range out {
out[i] = Candidate{ID: string(rune('a' + i)), At: base.Add(time.Duration(i) * time.Hour)}
}
return out
}
func TestRandomSamplerName(t *testing.T) {
t.Parallel()
if got := NewRandom().Name(); got != "random" {
t.Fatalf("Name() = %q, want %q", got, "random")
}
}
func TestRandomSamplerCap(t *testing.T) {
t.Parallel()
candidates := candidatesFor(10)
tests := []struct {
name string
cap int
wantLen int
}{
{name: "no cap returns every candidate", cap: 0, wantLen: 10},
{name: "cap smaller than count", cap: 3, wantLen: 3},
{name: "cap larger than count returns every candidate", cap: 100, wantLen: 10},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := NewRandom().Sample(context.Background(), candidates, Request{Cap: tt.cap, Seed: 1})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
if len(got) != tt.wantLen {
t.Fatalf("len(Sample()) = %d, want %d", len(got), tt.wantLen)
}
})
}
}
func TestRandomSamplerSelectionIsASubsetOfInput(t *testing.T) {
t.Parallel()
candidates := candidatesFor(10)
got, err := NewRandom().Sample(context.Background(), candidates, Request{Cap: 5, Seed: 42})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
valid := make(map[string]bool, len(candidates))
for _, c := range candidates {
valid[c.ID] = true
}
seen := make(map[string]bool, len(got))
for _, c := range got {
if !valid[c.ID] {
t.Fatalf("Sample() returned candidate %q not present in input", c.ID)
}
if seen[c.ID] {
t.Fatalf("Sample() returned candidate %q more than once", c.ID)
}
seen[c.ID] = true
}
}
func TestRandomSamplerDeterministicForSameSeed(t *testing.T) {
t.Parallel()
candidates := candidatesFor(20)
s := NewRandom()
first, err := s.Sample(context.Background(), candidates, Request{Cap: 8, Seed: 99})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
second, err := s.Sample(context.Background(), candidates, Request{Cap: 8, Seed: 99})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
wantIDs := make([]string, len(first))
for i, c := range first {
wantIDs[i] = c.ID
}
assertIDs(t, second, wantIDs)
}
func TestRandomSamplerDifferentSeedsCanDiffer(t *testing.T) {
t.Parallel()
candidates := candidatesFor(20)
s := NewRandom()
a, err := s.Sample(context.Background(), candidates, Request{Cap: 8, Seed: 1})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
b, err := s.Sample(context.Background(), candidates, Request{Cap: 8, Seed: 2})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
same := len(a) == len(b)
if same {
for i := range a {
if a[i].ID != b[i].ID {
same = false
break
}
}
}
if same {
t.Fatalf("Sample() with different seeds produced identical output: %v", a)
}
}
func TestRandomSamplerEmptyInput(t *testing.T) {
t.Parallel()
got, err := NewRandom().Sample(context.Background(), nil, Request{Seed: 1})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
if len(got) != 0 {
t.Fatalf("Sample() = %v, want empty", got)
}
}
func TestRandomSamplerDoesNotMutateInput(t *testing.T) {
t.Parallel()
assertInputNotMutated(t, NewRandom())
}
-64
View File
@@ -1,64 +0,0 @@
package sampling
import (
"context"
"testing"
"time"
)
func TestRecencySampler(t *testing.T) {
t.Parallel()
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
candidates := []Candidate{
{ID: "oldest", At: base},
{ID: "middle", At: base.Add(time.Hour)},
{ID: "newest", At: base.Add(2 * time.Hour)},
}
tests := []struct {
name string
cap int
wantIDs []string
}{
{name: "no cap orders newest first", cap: 0, wantIDs: []string{"newest", "middle", "oldest"}},
{name: "cap keeps only the most recent", cap: 2, wantIDs: []string{"newest", "middle"}},
{name: "cap larger than count returns everything", cap: 100, wantIDs: []string{"newest", "middle", "oldest"}},
}
s := NewRecency()
if s.Name() != "recency" {
t.Fatalf("Name() = %q, want %q", s.Name(), "recency")
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := s.Sample(context.Background(), candidates, Request{Cap: tt.cap})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
assertIDs(t, got, tt.wantIDs)
})
}
}
func TestRecencySamplerTiesKeepInputOrder(t *testing.T) {
t.Parallel()
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
candidates := []Candidate{
{ID: "first", At: at},
{ID: "second", At: at},
{ID: "third", At: at},
}
got, err := NewRecency().Sample(context.Background(), candidates, Request{})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
assertIDs(t, got, []string{"first", "second", "third"})
}
func TestRecencySamplerDoesNotMutateInput(t *testing.T) {
t.Parallel()
assertInputNotMutated(t, NewRecency())
}
-48
View File
@@ -1,48 +0,0 @@
package sampling
import (
"context"
"testing"
"time"
)
// assertIDs checks got's IDs match want, in order.
func assertIDs(t *testing.T, got []Candidate, want []string) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("got %d candidates, want %d (%v)", len(got), len(want), want)
}
for i, c := range got {
if c.ID != want[i] {
t.Fatalf("candidate[%d].ID = %q, want %q", i, c.ID, want[i])
}
}
}
// assertInputNotMutated builds a candidate slice, samples it through s,
// then verifies the original slice's order and values are untouched --
// the non-mutation guarantee every Sampler implementation must uphold.
func assertInputNotMutated(t *testing.T, s Sampler) {
t.Helper()
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
candidates := []Candidate{
{ID: "a", At: base.Add(3 * time.Hour)},
{ID: "b", At: base.Add(time.Hour)},
{ID: "c", At: base.Add(2 * time.Hour)},
{ID: "d", At: base},
}
original := append([]Candidate(nil), candidates...)
if _, err := s.Sample(context.Background(), candidates, Request{Cap: 2, Seed: 7}); err != nil {
t.Fatalf("Sample() error = %v", err)
}
if len(candidates) != len(original) {
t.Fatalf("input slice length changed: got %d, want %d", len(candidates), len(original))
}
for i := range candidates {
if candidates[i].ID != original[i].ID || !candidates[i].At.Equal(original[i].At) {
t.Fatalf("input slice mutated at index %d: got %+v, want %+v", i, candidates[i], original[i])
}
}
}
@@ -1,64 +0,0 @@
package sampling
import (
"context"
"testing"
"time"
)
func TestStalenessSampler(t *testing.T) {
t.Parallel()
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
candidates := []Candidate{
{ID: "newest", At: base.Add(2 * time.Hour)},
{ID: "middle", At: base.Add(time.Hour)},
{ID: "oldest", At: base},
}
tests := []struct {
name string
cap int
wantIDs []string
}{
{name: "no cap orders oldest first", cap: 0, wantIDs: []string{"oldest", "middle", "newest"}},
{name: "cap keeps only the stalest", cap: 2, wantIDs: []string{"oldest", "middle"}},
{name: "cap larger than count returns everything", cap: 100, wantIDs: []string{"oldest", "middle", "newest"}},
}
s := NewStaleness()
if s.Name() != "staleness" {
t.Fatalf("Name() = %q, want %q", s.Name(), "staleness")
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := s.Sample(context.Background(), candidates, Request{Cap: tt.cap})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
assertIDs(t, got, tt.wantIDs)
})
}
}
func TestStalenessSamplerTiesKeepInputOrder(t *testing.T) {
t.Parallel()
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
candidates := []Candidate{
{ID: "first", At: at},
{ID: "second", At: at},
{ID: "third", At: at},
}
got, err := NewStaleness().Sample(context.Background(), candidates, Request{})
if err != nil {
t.Fatalf("Sample() error = %v", err)
}
assertIDs(t, got, []string{"first", "second", "third"})
}
func TestStalenessSamplerDoesNotMutateInput(t *testing.T) {
t.Parallel()
assertInputNotMutated(t, NewStaleness())
}