Add GET /fs/browse directory-listing endpoint

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-01 20:15:00 -04:00
parent 1a925b3a05
commit a108964457
3 changed files with 130 additions and 0 deletions
+72
View File
@@ -110,3 +110,75 @@ func TestPostSettingsInvalidJSONIs400(t *testing.T) {
t.Errorf("applier called on invalid json; want 0, got %d", fa.called)
}
}
func TestBrowseListsDirsAndMarkdown(t *testing.T) {
dir := t.TempDir()
if err := os.Mkdir(filepath.Join(dir, "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "profile.md"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
s := newTestServer(t)
s.SetSettings(filepath.Join(t.TempDir(), "settings.json"), settings.Settings{}, func(settings.Settings) error { return nil })
r := s.Router()
req := httptest.NewRequest(http.MethodGet, "/fs/browse?dir="+dir, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", w.Code)
}
var resp struct {
Dir string `json:"dir"`
Parent string `json:"parent"`
Entries []struct {
Name string `json:"name"`
Path string `json:"path"`
IsDir bool `json:"is_dir"`
} `json:"entries"`
}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.Parent != filepath.Dir(dir) {
t.Errorf("parent = %q, want %q", resp.Parent, filepath.Dir(dir))
}
var names []string
for _, e := range resp.Entries {
names = append(names, e.Name)
}
hasSub, hasMd, hasTxt := false, false, false
for _, e := range resp.Entries {
switch e.Name {
case "sub":
hasSub = e.IsDir
case "profile.md":
hasMd = !e.IsDir
case "notes.txt":
hasTxt = true
}
}
if !hasSub || !hasMd {
t.Errorf("missing dir or .md entry; names = %v", names)
}
if hasTxt {
t.Errorf(".txt should be filtered out; names = %v", names)
}
}
func TestBrowseBadDirIs400(t *testing.T) {
s := newTestServer(t)
s.SetSettings(filepath.Join(t.TempDir(), "settings.json"), settings.Settings{}, func(settings.Settings) error { return nil })
r := s.Router()
req := httptest.NewRequest(http.MethodGet, "/fs/browse?dir=/no/such/dir/xyz", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", w.Code)
}
}