52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package localapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type fixedLine struct {
|
|
mbps float64
|
|
ok bool
|
|
}
|
|
|
|
func (f fixedLine) Value() (float64, time.Time, bool) {
|
|
return f.mbps, time.Date(2026, 10, 24, 6, 31, 0, 0, time.UTC), f.ok
|
|
}
|
|
|
|
func lineTestBody(t *testing.T, s *Server) LineTestResponse {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
s.handleLineTest(rec, httptest.NewRequest("GET", "/host/line-test", nil), 9201)
|
|
var env struct {
|
|
OK bool `json:"ok"`
|
|
Data LineTestResponse `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
|
|
t.Fatalf("decode %q: %v", rec.Body.String(), err)
|
|
}
|
|
return env.Data
|
|
}
|
|
|
|
// R-1247: the controller sets qBittorrent's limit only from a PRESENT value; never measured and not wired must both
|
|
// read present:false, never a zero line.
|
|
func TestLineTestRoute(t *testing.T) {
|
|
s := &Server{logger: slog.Default()}
|
|
if got := lineTestBody(t, s); got.Present {
|
|
t.Fatalf("not wired: %+v, want present:false", got)
|
|
}
|
|
s.SetLineTest(fixedLine{ok: false})
|
|
if got := lineTestBody(t, s); got.Present || got.Mbps != 0 {
|
|
t.Fatalf("never measured: %+v, want present:false", got)
|
|
}
|
|
s.SetLineTest(fixedLine{mbps: 90.7, ok: true})
|
|
got := lineTestBody(t, s)
|
|
if !got.Present || got.Mbps != 90.7 || got.Method != "iperf3-tunnel" || !strings.HasPrefix(got.MeasuredAt, "2026-10-24T06:31") {
|
|
t.Fatalf("measured: %+v", got)
|
|
}
|
|
}
|