package web
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/ops"
)
type acrossStubIn struct {
Number string `json:"number"`
Limit string `json:"limit"`
Title string `json:"title"`
Mine string `json:"mine"`
State string `json:"state"`
Repo string `json:"repo"`
}
type acrossStubOut struct {
Tickets []map[string]any `json:"tickets"`
Open int `json:"open"`
Closed int `json:"closed"`
}
// numberForge serves the cross-repository page over a number op that
// answers `by` for each number; the listing op itself finds nothing,
// as it does for digits no title holds.
func numberForge(t *testing.T, by map[string][]map[string]any, last *acrossStubIn) *httptest.Server {
t.Helper()
r := ops.NewRegistry()
reg := func(name, route string, run func(acrossStubIn) acrossStubOut) {
if err := ops.Register(r, ops.Op[acrossStubIn, acrossStubOut]{
Name: name, Doc: "stub", Kind: ops.Query, Auth: ops.Anonymous, Route: route,
Run: func(_ context.Context, in acrossStubIn) (acrossStubOut, error) { return run(in), nil },
}); err != nil {
t.Fatal(err)
}
}
reg("ticket.across", "GET /api/v1/stub/across", func(acrossStubIn) acrossStubOut { return acrossStubOut{} })
reg("ticket.number", "GET /api/v1/stub/number/{number}", func(in acrossStubIn) acrossStubOut {
*last = in
return acrossStubOut{Tickets: by[in.Number]}
})
if err := ops.Register(r, ops.Op[stubIn, struct {
Version string `json:"version"`
}]{
Name: "meta.version", Doc: "v", Kind: ops.Query, Auth: ops.Anonymous, Route: "GET /api/v1/stub/version",
Run: func(context.Context, stubIn) (struct {
Version string `json:"version"`
}, error) {
return struct {
Version string `json:"version"`
}{"v"}, nil
},
}); err != nil {
t.Fatal(err)
}
verify := Verify(func(string, string, string, ops.Auth) api.Decision { return api.Allowed })
srv := httptest.NewServer(HandlerWith(r, nil, nil, []Option{
WithSessions(fakeSessions{user: "dan", roles: map[string]ops.Auth{"": ops.Admin}}),
WithCaps(&guardPlane{stubCaps: stubCaps{letters: "aC3kfeimnopqrtvwxyz8LS"}}),
}, verify))
t.Cleanup(srv.Close)
return srv
}
func getAsDan(t *testing.T, srv *httptest.Server, path string) (*http.Response, string) {
t.Helper()
req, _ := http.NewRequest("GET", srv.URL+path, nil)
req.AddCookie(&http.Cookie{Name: sessionCookie, Value: "good"})
client := srv.Client()
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
return resp, string(b)
}
func numbered(repo, id string, n int) map[string]any {
return map[string]any{"repo": repo, "id": id, "number": n, "title": "t", "status": "Open", "updated": "2026-10-01T10:00:00"}
}
// A number carried by several repositories lists one row each, every
// row naming its repository and linking into it; a number carried once
// goes straight to it from the box; a number nothing carries is the
// page's ordinary title search, and its empty state.
func TestBareNumberOnTheCrossRepositoryPage(t *testing.T) {
var last acrossStubIn
srv := numberForge(t, map[string][]map[string]any{
"12": {numbered("shop", "aaa111", 12), numbered("garden", "bbb222", 12)},
"7": {numbered("shop", "ccc333", 7)},
}, &last)
// The box canonicalises and the page lists.
resp, _ := getAsDan(t, srv, "/tickets?q=%2312")
loc := resp.Header.Get("Location")
if u, err := url.Parse(loc); err != nil || resp.StatusCode != http.StatusSeeOther || u.Path != "/tickets" ||
strings.TrimPrefix(u.Query().Get("title"), "#") != "12" {
t.Fatalf("FAIL: several matches answered %d to %q, want the listing", resp.StatusCode, loc)
}
resp, body := getAsDan(t, srv, loc)
if resp.StatusCode != http.StatusOK {
t.Fatalf("FAIL: the listing answered %d", resp.StatusCode)
}
for _, want := range []string{`/r/shop/ticket/aaa111`, `/r/garden/ticket/bbb222`} {
if !strings.Contains(body, `href="`+want+`"`) {
t.Errorf("FAIL: no row linking %s", want)
}
}
if strings.Contains(body, `data-hook="mine-fell-back"`) {
t.Error("FAIL: the number's listing says it fell back from \"assigned to you\"")
}
if n := strings.Count(body, `class="t-repo"`); n != 2 {
t.Errorf("FAIL: %d rows name their repository, want 2", n)
}
// The page's own reach and repo filter ride along, so rows never come
// from outside what the page lists.
getAsDan(t, srv, "/tickets?title=12&repo=garden")
if last.Mine != "1" || last.Repo != "garden" || last.Number != "12" {
t.Errorf("FAIL: the op was asked %+v, want mine=1 repo=garden number=12", last)
}
getAsDan(t, srv, "/tickets?q=12&repo=shop")
if last.Repo != "shop" {
t.Errorf("FAIL: the box's repo filter did not reach the op: %+v", last)
}
// One carrier: straight there.
resp, _ = getAsDan(t, srv, "/tickets?q=7")
if got, want := resp.Header.Get("Location"), "/r/shop/ticket/7"; resp.StatusCode != http.StatusSeeOther || got != want {
t.Errorf("FAIL: one match answered %d to %q, want %q", resp.StatusCode, got, want)
}
// No carrier: the page's own search and empty state, no row.
resp, _ = getAsDan(t, srv, "/tickets?q=999")
if loc := resp.Header.Get("Location"); !strings.HasPrefix(loc, "/tickets?") {
t.Errorf("FAIL: no match redirected to %q", loc)
}
resp, body = getAsDan(t, srv, "/tickets?"+url.Values{"title": {"999"}}.Encode())
if resp.StatusCode != http.StatusOK || strings.Contains(body, `class="t-repo"`) {
t.Errorf("FAIL: no match answered %d with rows", resp.StatusCode)
}
if !t.Failed() {
t.Log("ok")
}
}