package web
import (
"context"
"net/http/httptest"
"strings"
"testing"
"craftdesign.group/quarry/internal/ops"
)
// A folder page of nested folders: each folder row links to its own
// tree page and each file row to its file, in the order the API gave.
func TestTreePageNestedFolderRows(t *testing.T) {
r := viewRegistry(t, nil)
hash := strings.Repeat("cd", 32)
type treeRequest struct {
Repo string `json:"repo"`
Version string `json:"version"`
Path string `json:"path"`
}
type treeAnswer struct {
Version string `json:"version"`
Path string `json:"path"`
Entries []treeRow `json:"entries"`
}
if err := ops.Register(r, ops.Op[treeRequest, treeAnswer]{
Name: "tree.list", Kind: ops.Query, Auth: ops.Anonymous, Caps: "o",
Doc: "test tree", Route: "GET /api/v1/r/{repo}/tree/{version}/{path...}",
Run: func(_ context.Context, in treeRequest) (treeAnswer, error) {
return treeAnswer{Version: hash, Path: in.Path, Entries: []treeRow{
{Name: "b", Path: "a/b", Dir: true, Files: 12},
{Name: "c", Path: "a/c", Dir: true, Files: 3},
{Name: "one.txt", Path: "a/one.txt", Size: 10, Blob: strings.Repeat("ef", 32)},
}}, nil
},
}); err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
HandlerWith(r, nil, nil, nil).ServeHTTP(w, httptest.NewRequest("GET", "/r/test/tree/"+hash+"/a", nil))
if w.Code != 200 {
t.Fatalf("FAIL: folder page returned %d: %s", w.Code, w.Body.String())
}
body := w.Body.String()
last := -1
for _, href := range []string{"/tree/" + hash + "/a/b", "/tree/" + hash + "/a/c", "/file/" + hash + "/a/one.txt"} {
i := strings.Index(body, `href="/r/test`+href+`"`)
if i < 0 {
t.Errorf("FAIL: no link to %s", href)
continue
}
if i < last {
t.Errorf("FAIL: %s is out of order", href)
}
last = i
}
if !t.Failed() {
t.Log("ok")
}
}