package oltp
import (
"errors"
"testing"
"time"
)
// Each attempt is kept ā the last twenty ā with its duration and
// outcome, and RecordMirrorRun answers the streak so the caller can
// decide whether this attempt is the one worth telling somebody about.
func TestAMirrorRemembersItsLastPullsAndItsStreak(t *testing.T) {
d := openTest(t)
if err := d.SetMirror("w", "https://example.invalid/w", time.Hour); err != nil {
t.Fatal(err)
}
at := time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC)
var streaks []int
for i := 0; i < 25; i++ {
var err error
if i%5 != 4 {
err = errors.New("cannot connect")
}
s, rerr := d.RecordMirrorRun("w", at.Add(time.Duration(i)*time.Minute), time.Duration(100+i)*time.Millisecond, i%5 == 4, err)
if rerr != nil {
t.Fatal(rerr)
}
streaks = append(streaks, s)
}
if streaks[2] != 3 || streaks[4] != 0 || streaks[23] != 4 || streaks[24] != 0 {
t.Fatalf("FAIL: streaks %v", streaks)
}
pulls, err := d.MirrorPulls("w", 0)
if err != nil {
t.Fatal(err)
}
if len(pulls) != MirrorPullKeep {
t.Fatalf("FAIL: kept %d pulls, want %d", len(pulls), MirrorPullKeep)
}
if pulls[0].Ms != 124 || !pulls[0].Changed || pulls[0].Err != "" || pulls[1].Err == "" {
t.Fatalf("FAIL: newest-first order or fields wrong: %+v %+v", pulls[0], pulls[1])
}
t.Logf("ok: 25 attempts, %d kept, newest first, streaks %vā¦", len(pulls), streaks[:6])
}