package main import ( "net/http/httptest" "os" "path/filepath" "strings" "testing" ) func TestHandleTreeExpandTo(t *testing.T) { root := t.TempDir() for _, d := range []string{"Projects/A/Deep", "Other/B"} { if err := os.MkdirAll(filepath.Join(root, filepath.FromSlash(d)), 0755); err != nil { t.Fatal(err) } } h := &handler{root: root} get := func(urlPath, query string) string { r := httptest.NewRequest("GET", urlPath+"?"+query, nil) w := httptest.NewRecorder() h.handleTree(w, r, urlPath, filepath.Join(root, filepath.FromSlash(strings.Trim(urlPath, "/")))) return w.Body.String() } // A chain below the listed folder is pre-expanded. if body := get("/Projects/", "tree&expandTo=/Projects/A"); !strings.Contains(body, `href="/Projects/A/Deep/"`) { t.Errorf("expandTo below the folder did not expand:\n%s", body) } // Lazy children keep the picker options. if body := get("/", "tree&pick=folder&root=1"); !strings.Contains(body, `hx-get="/Projects/?tree&pick=folder"`) || !strings.Contains(body, `value="/"`) { t.Errorf("picker rows missing lazy URL or root row:\n%s", body) } w := httptest.NewRecorder() h.handleTree(w, httptest.NewRequest("GET", "/?tree&pick=bogus", nil), "/", root) if w.Code != 400 { t.Errorf("bad pick mode: got %d, want 400", w.Code) } }