Show ages on the AS and prefix pages in words (closes #163)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The Age column printed Go's duration format for every plural age, such as "5m0s ago" or "360h0m0s ago". The templates now register humanize.Time from go-humanize, which the server already uses, as the timeSince function, and the hand-written function is gone. Ages read "5 minutes ago", "3 hours ago" or "2 weeks ago". An age under a minute reads "now" or in seconds instead of "just now". An age over a month reads in months or years instead of a date. Model: opus-5-5
This commit was merged in pull request #171.
This commit is contained in:
@@ -512,6 +512,71 @@ func TestPagesSayOriginZeroIsNoKnownOrigin(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPagesShowAgesInWords stores routes last updated about 5 minutes, 3 hours
|
||||
// and 15 days ago, and checks that the AS page and the prefix pages show each
|
||||
// age in words. Before the fix the ages read "5m0s ago", "3h0m0s ago" and
|
||||
// "360h0m0s ago" (https://git.eeqj.de/sneak/routewatch/issues/163).
|
||||
func TestPagesShowAgesInWords(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db, err := database.New(&config.Config{StateDir: t.TempDir()}, logger.New())
|
||||
if err != nil {
|
||||
t.Fatalf("database.New: %v", err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
// Each age is well past a whole number of its unit, so the time the test
|
||||
// takes to run does not change the words.
|
||||
ages := []struct {
|
||||
prefix string
|
||||
age time.Duration
|
||||
want string
|
||||
}{
|
||||
{testPrefixV4, 5*time.Minute + 30*time.Second, "5 minutes ago"},
|
||||
{"198.51.100.0/24", 3*time.Hour + 30*time.Minute, "3 hours ago"},
|
||||
{"203.0.113.0/24", 15 * 24 * time.Hour, "2 weeks ago"},
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
routes := make([]*database.LiveRoute, 0, len(ages))
|
||||
|
||||
for _, a := range ages {
|
||||
routes = append(routes, &database.LiveRoute{
|
||||
Prefix: a.prefix, MaskLength: 24, IPVersion: 4,
|
||||
OriginASN: 64500, PeerIP: testPeerV4, ASPath: []int{64500},
|
||||
NextHop: testPeerV4, LastUpdated: now.Add(-a.age),
|
||||
})
|
||||
}
|
||||
|
||||
err = db.UpsertLiveRouteBatch(routes)
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertLiveRouteBatch: %v", err)
|
||||
}
|
||||
|
||||
s := New(db, streamer.New(logger.New(), metrics.New()), logger.New(), &config.Config{})
|
||||
|
||||
for _, a := range ages {
|
||||
cell := `<td class="age">` + a.want + `</td>`
|
||||
|
||||
for _, path := range []string{asPagePath, "/prefix/" + a.prefix} {
|
||||
rec := httptest.NewRecorder()
|
||||
s.router.ServeHTTP(rec, httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodGet, path, nil))
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("%s: status %d, want %d; body %s",
|
||||
path, rec.Code, http.StatusOK, rec.Body)
|
||||
}
|
||||
|
||||
if !strings.Contains(rec.Body.String(), cell) {
|
||||
t.Errorf("%s does not show the age of the route for %s as %q",
|
||||
path, a.prefix, a.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// shortRequestDeadline is the deadline the tests below give a request. The
|
||||
// server's 30-second limit and the stats handlers' 4-second limit inherit it,
|
||||
// so the tests do not wait for either.
|
||||
|
||||
Reference in New Issue
Block a user