package web
import (
"strings"
"testing"
"time"
"craftdesign.group/quarry/internal/obs"
)
// The distributed crawler's page lists what the sweep kept, says when
// nothing has been kept yet, and when the forge keeps no history.
func TestTheSwarmPageShowsWhatTheSweepKept(t *testing.T) {
render := func(v fleetSwarmView) string {
t.Helper()
var b strings.Builder
if err := noHooks.template(fleetSwarmPage).ExecuteTemplate(&b, "content", pageData{FleetSwarm: v}); err != nil {
t.Fatal(err)
}
return b.String()
}
day := time.Date(2026, 11, 7, 0, 0, 0, 0, time.UTC)
sw := &obs.Swarm{Requests: 3,
Days: []obs.SwarmDay{{At: day, Requests: 3, Prefixes: 2, Networks: 2}},
Hours: []obs.SwarmDay{{At: day.Add(13 * time.Hour), Requests: 3}},
Prefixes: []obs.SwarmPrefix{{Prefix: "203.0.113.0/24", Network: "Orange", Country: "FR", Requests: 2, Days: 1, First: day, Last: day}, {Prefix: "198.51.100.0/24", Requests: 1}},
Networks: []obs.SwarmNetwork{{ASN: 3215, Network: "Orange", Requests: 2, Prefixes: 1}},
Hints: []obs.SwarmCount{{Key: "", Count: 3}},
Shapes: []obs.SwarmCount{{Key: "/r/tcl/checkin/{hash}/diff", Count: 3}},
}
got := render(swarmView(sw, 7))
if n := strings.Count(got, `data-hook="swarm-range"`); n != 2 {
t.Errorf("FAIL: %d ranges listed, want 2", n)
}
if !strings.Contains(got, "203.0.113.0/24") || !strings.Contains(got, `data-hook="swarm-total"`) {
t.Errorf("FAIL: the page lacks the ranges or the total")
}
if empty := render(swarmView(&obs.Swarm{}, 7)); !strings.Contains(empty, `data-hook="swarm-empty"`) {
t.Errorf("FAIL: an empty window does not say so")
}
if none := render(swarmView(nil, 0)); !strings.Contains(none, `data-hook="swarm-none"`) {
t.Errorf("FAIL: a forge with no history does not say so")
}
}