TestStartupMockCutoff pins the startup-mock exemption boundary used by RemoveUnusedMocks: it must be the (models.StartupMockTestCaseWindow+1)-th test case by request time once a set has more tests than the window, fall back to keepAll when the whole set is startup, and disable cleanly with no usable
(t *testing.T)
| 21 | // keepAll when the whole set is startup, and disable cleanly with no usable |
| 22 | // timestamps. |
| 23 | func TestStartupMockCutoff(t *testing.T) { |
| 24 | t.Parallel() |
| 25 | |
| 26 | base := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) |
| 27 | keepAll := base.Add(-time.Hour) // stand-in for pruneBefore (replay start) |
| 28 | |
| 29 | // at(i) is the i-th second after base; ordering is intentionally shuffled |
| 30 | // in the slices below so we also exercise the internal sort. |
| 31 | at := func(i int) time.Time { return base.Add(time.Duration(i) * time.Second) } |
| 32 | |
| 33 | t.Run("more tests than window picks the (window+1)-th by time", func(t *testing.T) { |
| 34 | // Window is 5, so with 8 tests the cutoff is the 6th-earliest (index 5), |
| 35 | // i.e. at(5). Feed them out of order to prove sorting decides, not arrival. |
| 36 | tcs := []*models.TestCase{ |
| 37 | tcAt(at(3)), tcAt(at(0)), tcAt(at(6)), tcAt(at(1)), |
| 38 | tcAt(at(7)), tcAt(at(2)), tcAt(at(5)), tcAt(at(4)), |
| 39 | } |
| 40 | got := startupMockCutoff(tcs, keepAll) |
| 41 | if !got.Equal(at(models.StartupMockTestCaseWindow)) { |
| 42 | t.Fatalf("cutoff = %v, want %v (the (window+1)-th test case)", got, at(models.StartupMockTestCaseWindow)) |
| 43 | } |
| 44 | }) |
| 45 | |
| 46 | t.Run("exactly window tests keeps everything", func(t *testing.T) { |
| 47 | tcs := make([]*models.TestCase, 0, models.StartupMockTestCaseWindow) |
| 48 | for i := range models.StartupMockTestCaseWindow { |
| 49 | tcs = append(tcs, tcAt(at(i))) |
| 50 | } |
| 51 | got := startupMockCutoff(tcs, keepAll) |
| 52 | if !got.Equal(keepAll) { |
| 53 | t.Fatalf("cutoff = %v, want keepAll %v when the whole set is startup", got, keepAll) |
| 54 | } |
| 55 | }) |
| 56 | |
| 57 | t.Run("fewer than window tests keeps everything", func(t *testing.T) { |
| 58 | got := startupMockCutoff([]*models.TestCase{tcAt(at(0)), tcAt(at(1))}, keepAll) |
| 59 | if !got.Equal(keepAll) { |
| 60 | t.Fatalf("cutoff = %v, want keepAll %v", got, keepAll) |
| 61 | } |
| 62 | }) |
| 63 | |
| 64 | t.Run("no test cases disables the exemption", func(t *testing.T) { |
| 65 | got := startupMockCutoff(nil, keepAll) |
| 66 | if !got.IsZero() { |
| 67 | t.Fatalf("cutoff = %v, want zero time", got) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | t.Run("timestamp-less tests are ignored and disable the exemption", func(t *testing.T) { |
| 72 | // Created==0 and no req timestamps → no usable candidate → zero time. |
| 73 | tcs := []*models.TestCase{{Kind: models.HTTP}, {Kind: models.HTTP}} |
| 74 | got := startupMockCutoff(tcs, keepAll) |
| 75 | if !got.IsZero() { |
| 76 | t.Fatalf("cutoff = %v, want zero time for timestamp-less set", got) |
| 77 | } |
| 78 | }) |
| 79 | |
| 80 | t.Run("falls back to grpc and Created timestamps", func(t *testing.T) { |
nothing calls this directly
no test coverage detected