package obs
import (
"testing"
"time"
)
// API calls are recorded beside the pages now, and a runner or a crawler
// makes far more of them. Sharing one window, they pushed the pages out
// of it; each surface keeps its own window, read back as one list.
func TestAPICallsDoNotPushPagesOutOfTheWindow(t *testing.T) {
r := NewRecorder(4, nil)
t0 := time.Now()
r.RecordRender(Render{ID: "w1", At: t0, Path: "GET /a", Surface: SurfaceWeb})
r.RecordRender(Render{ID: "w2", At: t0.Add(time.Millisecond), Path: "GET /b", Surface: SurfaceWeb})
for i := 0; i < 20; i++ {
r.RecordRender(Render{ID: "api", At: t0.Add(time.Duration(2+i) * time.Millisecond),
Path: "GET /api/v1/x", Surface: SurfaceAPI})
}
got := r.Snapshot().Renders
web := 0
for i, v := range got {
if v.Surface == SurfaceWeb {
web++
}
if i > 0 && v.At.After(got[i-1].At) {
t.Fatalf("renders are not newest first at %d", i)
}
}
if web != 2 {
t.Errorf("%d web renders left in the window after an API burst, want 2", web)
}
if d := r.Route("GET /a", 10); len(d.Renders) != 1 {
t.Errorf("the route view lost the page: %d renders", len(d.Renders))
}
}