package web
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/artifact"
"craftdesign.group/quarry/internal/index"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// The whole path, over the real ops and the real store: a pin posted from
// the board is kept per account, shows on the next load as the first row
// of the pinned group, and is refused for a repository the account
// cannot read, with the answer a missing repository gets.
func TestPinSurvivesFromPostToBoard(t *testing.T) {
odb, err := oltp.Open(filepath.Join(t.TempDir(), "oltp.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { odb.Close() })
repos := map[string]*api.Repo{}
for i, name := range []string{"alpha", "beta", "gamma"} {
ix, err := index.Open(filepath.Join(t.TempDir(), name+".db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ix.Close() })
if err := ix.FoldNow([]index.Parsed{{Hash: strings.Repeat(string(rune('a'+i)), 64), Art: &artifact.Artifact{
Type: artifact.Manifest, Date: "2026-08-1" + string(rune('0'+i)) + "T09:00:00", User: "dan", Comment: "tip of " + name}}}); err != nil {
t.Fatal(err)
}
repos[name] = &api.Repo{Chat: name, Index: ix}
}
r := ops.NewRegistry()
if err := api.RegisterAll(r, api.Deps{Version: "v", Repos: api.NewRepos(repos), Pins: odb,
Accounts: stubAccts{grants: map[string]map[string]string{"dan": {"alpha": "write", "beta": "write", "gamma": "write"}}},
}); err != nil {
t.Fatal(err)
}
h := HandlerWith(r, nil, nil, []Option{WithSessions(fakeSessions{user: "dan"})})
do := func(method, path string, form url.Values) (int, string) {
var req *http.Request
if method == "POST" {
req = httptest.NewRequest(method, path, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else {
req = httptest.NewRequest(method, path, nil)
}
req.AddCookie(&http.Cookie{Name: sessionCookie, Value: "good"})
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
return w.Code, w.Body.String()
}
if _, body := do("GET", "/home", nil); !sameNames(boardOrder(body), "gamma", "beta", "alpha") || strings.Contains(body, `data-hook="board-pinned"`) {
t.Fatalf("FAIL: the unpinned board is %v, want last activity first with no pinned group", boardOrder(body))
}
for _, name := range []string{"alpha", "beta"} {
if code, _ := do("POST", "/home/pin", url.Values{"repo": {name}, "pinned": {"true"}}); code != http.StatusSeeOther {
t.Fatalf("FAIL: pinning %s answered %d", name, code)
}
}
_, body := do("GET", "/home", nil)
pinned, rest := strings.Index(body, `data-hook="board-pinned"`), strings.Index(body, `data-hook="board-rest"`)
if pinned < 0 || rest < pinned || !sameNames(boardOrder(body[pinned:rest]), "alpha", "beta") || !sameNames(boardOrder(body[rest:]), "gamma") {
t.Errorf("FAIL: after two pins the board is %v", boardOrder(body))
}
if code, _ := do("POST", "/home/pin", url.Values{"repo": {"alpha"}, "pinned": {"false"}}); code != http.StatusSeeOther {
t.Fatalf("FAIL: unpinning answered %d", code)
}
if _, body = do("GET", "/home", nil); !sameNames(boardOrder(body), "beta", "gamma", "alpha") {
t.Errorf("FAIL: after unpinning alpha the board is %v", boardOrder(body))
}
// A repository that is not there is refused, and so is its pin.
if code, _ := do("POST", "/home/pin", url.Values{"repo": {"nonesuch"}, "pinned": {"true"}}); code != http.StatusNotFound {
t.Errorf("FAIL: pinning a missing repository answered %d", code)
}
// The wire: me.repos names the place of each pin and the newest event,
// under literal keys.
got, err := invoke(ops.WithUser(context.Background(), "dan"), r, "me.repos", url.Values{})
if err != nil {
t.Fatal(err)
}
wire, _ := json.Marshal(got)
for _, want := range []string{`"pin":1`, `"latest":{`, `"kind":"ci"`, `"comment":"tip of beta"`} {
if !strings.Contains(string(wire), want) {
t.Errorf("FAIL: me.repos carries no %s: %s", want, wire)
}
}
if !t.Failed() {
t.Log("ok")
}
}