package http import "testing" // Library folders (migration 027): a folder is a content-less Page (isFolder) // reusing the parentId hierarchy. Deleting a folder reparents its children // (pages AND subfolders) to the folder's own parent, keeping them one level up. func TestPageFolders(t *testing.T) { srv := testServer(t) alice := newAPIUser(t, srv, "folders@superbrain.test") top := alice.request("POST", "/api/pages", map[string]any{"title": "Personal", "isFolder": true}, 201) topID := top["id"].(string) if top["isFolder"] != true { t.Fatalf("folder flag not stored: %v", top) } sub := alice.request("POST", "/api/pages", map[string]any{"title": "Travel", "isFolder": true, "parentId": topID}, 201)["id"].(string) page := alice.request("POST", "/api/pages", map[string]any{"title": "Lisbon", "parentId": sub}, 201)["id"].(string) loose := alice.request("POST", "/api/pages", map[string]any{"title": "Loose", "parentId": topID}, 201)["id"].(string) // isFolder is exposed in the list projection. found := false for _, p := range alice.getJSON("/api/pages", 200) { if p["id"] == topID { found = p["isFolder"] == true } } if !found { t.Fatal("isFolder missing from list projection") } // Deleting the nested folder promotes its page and keeps it under the // grandparent (top), not at the root. alice.request("DELETE", "/api/pages/"+sub, nil, 200) after := map[string]string{} for _, p := range alice.getJSON("/api/pages", 200) { parent, _ := p["parentId"].(string) after[p["id"].(string)] = parent } if after[page] != topID { t.Fatalf("page not reparented to grandparent: %v", after) } if _, ok := after[sub]; ok { t.Fatal("deleted folder still listed") } // Deleting the top folder promotes remaining children to the root. alice.request("DELETE", "/api/pages/"+topID, nil, 200) after = map[string]string{} for _, p := range alice.getJSON("/api/pages", 200) { parent, _ := p["parentId"].(string) after[p["id"].(string)] = parent } if after[page] != "" || after[loose] != "" { t.Fatalf("children not promoted to root: %v", after) } // A plain page delete still promotes children to the root (SET NULL). p1 := alice.request("POST", "/api/pages", map[string]any{"title": "P1"}, 201)["id"].(string) p2 := alice.request("POST", "/api/pages", map[string]any{"title": "P2", "parentId": p1}, 201)["id"].(string) alice.request("DELETE", "/api/pages/"+p1, nil, 200) for _, p := range alice.getJSON("/api/pages", 200) { if p["id"] == p2 && p["parentId"] != nil { t.Fatalf("plain page delete did not promote child: %v", p) } } }