//go:build !js && !wasip1
package obsduck
import (
"testing"
"time"
"craftdesign.group/quarry/internal/obs"
)
// History carries each row's surface, so an API call read back from the
// store is told apart from a page. Rows written before the column
// existed were all pages, and read back as such.
func TestHistoryCarriesTheSurface(t *testing.T) {
s := openSink(t)
now := time.Now()
s.Render(obs.Render{At: now, Path: "GET /api/v1/r/{repo}/ticket", Surface: obs.SurfaceAPI,
Elapsed: 300 * time.Millisecond, Status: 200})
s.Render(obs.Render{At: now.Add(-time.Second), Path: "GET /r/{repo}/timeline", Surface: obs.SurfaceWeb,
Elapsed: 20 * time.Millisecond, Status: 200})
s.Flush()
// A row from before the surface column: every one of those was a page.
if _, err := s.db.Exec(`INSERT INTO render (ts, path, repo, elapsed_ms, ops_ms, zit_ms, status, url)
VALUES (?, 'GET /old', '', 1, 0, 0, 200, '/old')`, now.Add(-2*time.Second)); err != nil {
t.Fatal(err)
}
got, err := s.History(obs.HistoryQuery{})
if err != nil {
t.Fatal(err)
}
want := map[string]string{
"GET /api/v1/r/{repo}/ticket": obs.SurfaceAPI,
"GET /r/{repo}/timeline": obs.SurfaceWeb,
"GET /old": obs.SurfaceWeb,
}
if len(got) != len(want) {
t.Fatalf("got %d rows, want %d: %+v", len(got), len(want), got)
}
for _, r := range got {
if r.Surface != want[r.Path] {
t.Errorf("%s: surface %q, want %q", r.Path, r.Surface, want[r.Path])
}
}
}