package server
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestCommandCompletionReadACL(t *testing.T) {
s, token, _ := searchServerFixture(t)
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
w := configRequest(s, token, "GET", "/_/api/v1/path-picker?path=/docs&mode=children", nil)
if w.Code != 200 || strings.Contains(w.Body.String(), "hidden") || !strings.Contains(w.Body.String(), "nebula guide?#%") {
t.Fatalf("ACL completion: %d %s", w.Code, w.Body.String())
}
}
func TestCommandPathCompletion(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
createUserSpacePage(t, s, domain, "/manual", "
Parent document and folder
")
for i := 0; i < 260; i++ {
if _, err := s.store.CreatePageAtPathInDomain(domain, fmt.Sprintf("/manual/a%03d", i)); err != nil {
t.Fatal(err)
}
}
createUserSpacePage(t, s, domain, "/manual/日本語 #?", "plain")
w := configRequest(s, token, "GET", "/_/api/v1/path-picker?path=/manual&mode=children&prefix=日本語", nil)
var result struct {
Path string `json:"path"`
Entries []pathPickerEntry `json:"entries"`
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result.Path != "/manual" || len(result.Entries) != 1 || result.Entries[0].Name != "日本語 #?" {
t.Fatalf("completion: %d %s", w.Code, w.Body.String())
}
w = configRequest(s, "", "GET", "/_/api/v1/path-picker?path=/manual&mode=children", nil)
if w.Code == 200 {
t.Fatal("anonymous API listed private paths")
}
}
func TestCommandAndHighlightChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain, _ := editorFixture(t)
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
s.mux.HandleFunc("/notes", s.handleSemanticRoute)
s.mux.HandleFunc("/", s.handleHome)
createUserSpacePage(t, s, domain, "/notes", `Code sample
# comment
def hello():
return "<script>literal</script>"
`)
createUserSpacePage(t, s, domain, "/notes/alpha", "Alpha
")
createUserSpacePage(t, s, domain, "/notes/alpine", "Alpine
")
createUserSpacePage(t, s, domain, "/日本語 #?/child", "Unicode
")
script, err := os.ReadFile("internal/server/testdata/command-highlight.js")
if err != nil {
t.Fatal(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
r.Header.Set("Authorization", "Bearer "+token)
if r.URL.Path == "/fixture" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte(""))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, width := range []string{"1440", "375"} {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
shot := ""
if base := os.Getenv("PAGESWIKI_COMMAND_SCREENSHOT"); base != "" {
shot = base + "-" + width + ".png"
}
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/fixture", t.TempDir(), shot, width, "950").CombinedOutput()
cancel()
if err != nil {
t.Fatalf("%s: %v\n%s", width, err, out)
}
}
}