package web
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"craftdesign.group/quarry/internal/ops"
)
type dashFeedIn struct {
Kind string `json:"kind"`
Find string `json:"find"`
Repo string `json:"repo"`
Offset int `json:"offset"`
}
type dashAcrossIn struct {
Awaiting bool `json:"awaiting"`
Limit int `json:"limit"`
}
// The waiting-on-you band reaches into every tracker the reader can see,
// so it is asked for only under ticket changes, the one chip that draws
// it: computed under every chip, it cost the home page four seconds
// whenever those trackers were cold.
func TestTheHomeAsksForTheWaitingBandOnlyWhereItIsDrawn(t *testing.T) {
r := ops.NewRegistry()
across := 0
type empty struct{}
must := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
must(ops.Register(r, ops.Op[dashFeedIn, struct {
Total int `json:"total"`
}]{Name: "me.feed", Doc: "test double", Kind: ops.Query, Auth: ops.Token, Route: "GET /api/v1/me/feed",
Run: func(context.Context, dashFeedIn) (struct {
Total int `json:"total"`
}, error) {
return struct {
Total int `json:"total"`
}{}, nil
}}))
must(ops.Register(r, ops.Op[dashAcrossIn, struct {
Tickets []struct{} `json:"tickets"`
}]{Name: "ticket.across", Doc: "test double", Kind: ops.Query, Auth: ops.Token, Route: "GET /api/v1/me/tickets",
Run: func(context.Context, dashAcrossIn) (struct {
Tickets []struct{} `json:"tickets"`
}, error) {
across++
return struct {
Tickets []struct{} `json:"tickets"`
}{}, nil
}}))
must(ops.Register(r, ops.Op[empty, struct {
Version string `json:"version"`
}]{Name: "meta.version", Doc: "test double", Kind: ops.Query, Auth: ops.Anonymous, Route: "GET /api/v1/version",
Run: func(context.Context, empty) (struct {
Version string `json:"version"`
}, error) {
return struct {
Version string `json:"version"`
}{"t"}, nil
}}))
h := HandlerWith(r, nil, nil, []Option{WithSessions(fakeSessions{user: "jan", roles: map[string]ops.Auth{"": ops.Token}})})
get := func(path string) int {
req := httptest.NewRequest("GET", path, nil)
req.AddCookie(&http.Cookie{Name: sessionCookie, Value: "good"})
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `id="dash-feed"`) {
t.Fatalf("%s: %d, not the dashboard", path, w.Code)
}
return across
}
for _, path := range []string{"/", "/?kind=c", "/?kind=w"} {
if n := get(path); n != 0 {
t.Errorf("FAIL: %s asked for the waiting band, which it does not draw", path)
}
}
if n := get("/?kind=t"); n != 1 {
t.Errorf("FAIL: ticket changes did not ask for its band (%d)", n)
}
}