Refactor: add stable folder layout foundation

This commit is contained in:
MangoPig
2026-06-25 21:53:29 +01:00
parent da1b210865
commit 24d1e472a2
7 changed files with 743 additions and 110 deletions
+71 -3
View File
@@ -5,6 +5,7 @@ import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
@@ -166,6 +167,9 @@ func TestCreateProjectHierarchyFolderOnDiskCreatesExpectedFolderShape(t *testing
}
folderPayload := readJSONFileForTest[map[string]any](t, filepath.Join(createdFolderPath, "folder.json"))
if strings.TrimSpace(asStringForTest(folderPayload["id"])) == "" {
t.Fatalf("expected created folder to have stable id, got %#v", folderPayload["id"])
}
if folderPayload["name"] != "Design System" {
t.Fatalf("expected folder name Design System, got %#v", folderPayload["name"])
}
@@ -282,6 +286,9 @@ func TestRenameProjectHierarchyFolderOnDiskRenamesFolderShape(t *testing.T) {
}
folderPayload := readJSONFileForTest[map[string]any](t, filepath.Join(renamedFolderPath, "folder.json"))
if strings.TrimSpace(asStringForTest(folderPayload["id"])) == "" {
t.Fatalf("expected renamed folder to preserve stable id, got %#v", folderPayload["id"])
}
if folderPayload["name"] != "Platform Design" {
t.Fatalf("expected renamed folder name Platform Design, got %#v", folderPayload["name"])
}
@@ -388,6 +395,9 @@ func TestMoveProjectHierarchyFolderOnDiskMovesFolderToNewParent(t *testing.T) {
}
folderPayload := readJSONFileForTest[map[string]any](t, filepath.Join(movedFolderPath, "folder.json"))
if strings.TrimSpace(asStringForTest(folderPayload["id"])) == "" {
t.Fatalf("expected moved folder to preserve stable id, got %#v", folderPayload["id"])
}
if folderPayload["name"] != "Research" {
t.Fatalf("expected moved folder name Research, got %#v", folderPayload["name"])
}
@@ -477,9 +487,9 @@ func TestMoveProjectHierarchyFolderOnDiskRejectsDescendantTarget(t *testing.T) {
func TestBuildProjectHierarchyFolderTreeBuildsNestedStructure(t *testing.T) {
rows := []projectHierarchyFolderRow{
{Path: "projects/project-primary-project/children/folder-design", ParentPath: projectHierarchyRootPath("primary-project"), Label: "Design"},
{Path: "projects/project-primary-project/children/folder-design/children/folder-research", ParentPath: "projects/project-primary-project/children/folder-design/children", Label: "Research"},
{Path: "projects/project-primary-project/children/folder-ops", ParentPath: projectHierarchyRootPath("primary-project"), Label: "Ops"},
{ID: "folder-design-id", Path: "projects/project-primary-project/children/folder-design", ParentPath: projectHierarchyRootPath("primary-project"), Label: "Design"},
{ID: "folder-research-id", Path: "projects/project-primary-project/children/folder-design/children/folder-research", ParentPath: "projects/project-primary-project/children/folder-design/children", Label: "Research"},
{ID: "folder-ops-id", Path: "projects/project-primary-project/children/folder-ops", ParentPath: projectHierarchyRootPath("primary-project"), Label: "Ops"},
}
folders := buildProjectHierarchyFolderTree(rows, projectHierarchyRootPath("primary-project"))
@@ -492,6 +502,64 @@ func TestBuildProjectHierarchyFolderTreeBuildsNestedStructure(t *testing.T) {
if len(folders[0].Children) != 1 || folders[0].Children[0].Label != "Research" {
t.Fatalf("unexpected nested folder structure: %#v", folders[0].Children)
}
if folders[0].ID != "folder-design-id" || folders[0].Path != "projects/project-primary-project/children/folder-design" {
t.Fatalf("expected design folder to retain stable id/path, got %#v", folders[0])
}
if folders[0].Children[0].ID != "folder-research-id" || folders[1].ID != "folder-ops-id" {
t.Fatalf("expected nested/top-level folder ids to be preserved, got %#v / %#v", folders[0].Children[0], folders[1])
}
}
func TestApplyProjectHierarchyFolderOrderingOrdersRootAndChildrenByStableID(t *testing.T) {
folders := []ProjectHierarchyFolderRecord{
{
ID: "folder-design-id",
Path: "projects/project-primary-project/children/folder-design",
Label: "Design",
Children: []ProjectHierarchyFolderRecord{
{ID: "folder-research-id", Path: "projects/project-primary-project/children/folder-design/children/folder-research", Label: "Research"},
{ID: "folder-assets-id", Path: "projects/project-primary-project/children/folder-design/children/folder-assets", Label: "Assets"},
},
},
{ID: "folder-ops-id", Path: "projects/project-primary-project/children/folder-ops", Label: "Ops"},
{ID: "folder-qa-id", Path: "projects/project-primary-project/children/folder-qa", Label: "QA"},
}
ordered := applyProjectHierarchyFolderOrdering(folders, map[string][]string{
projectFolderOrderRootKey: {"folder-qa-id", "folder-design-id"},
"folder-design-id": {"folder-assets-id", "folder-research-id"},
})
if len(ordered) != 3 {
t.Fatalf("expected 3 ordered root folders, got %d", len(ordered))
}
if ordered[0].ID != "folder-qa-id" || ordered[1].ID != "folder-design-id" || ordered[2].ID != "folder-ops-id" {
t.Fatalf("unexpected ordered root ids: %#v", ordered)
}
if len(ordered[1].Children) != 2 {
t.Fatalf("expected design folder children to be preserved, got %#v", ordered[1].Children)
}
if ordered[1].Children[0].ID != "folder-assets-id" || ordered[1].Children[1].ID != "folder-research-id" {
t.Fatalf("unexpected ordered child ids: %#v", ordered[1].Children)
}
}
func TestInsertFolderOrderReordersWithinSameParent(t *testing.T) {
folderOrder := map[string][]string{
projectFolderOrderRootKey: {"folder-a", "folder-b", "folder-c"},
}
insertFolderOrder(folderOrder, "", "folder-c", 0)
got := folderOrder[projectFolderOrderRootKey]
if len(got) != 3 || got[0] != "folder-c" || got[1] != "folder-a" || got[2] != "folder-b" {
t.Fatalf("unexpected reordered root children: %#v", got)
}
}
func asStringForTest(value any) string {
text, _ := value.(string)
return text
}
func readJSONFileForTest[T any](t *testing.T, path string) T {