Feat: add POSIX DB projection
This commit is contained in:
148
Backend/internal/posixproj/projector_test.go
Normal file
148
Backend/internal/posixproj/projector_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package posixproj
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestScanRootBuildsProjectedNodesFromBootstrapShape(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "POSIX")
|
||||
|
||||
mustMkdirAll(t, filepath.Join(root, "catalog", "packs"))
|
||||
mustMkdirAll(t, filepath.Join(root, "catalog", "standalone"))
|
||||
mustMkdirAll(t, filepath.Join(root, "departments", "department-primary-department", "teams", "team-primary-team"))
|
||||
mustMkdirAll(t, filepath.Join(root, "projects", "project-primary-project", "tree"))
|
||||
mustMkdirAll(t, filepath.Join(root, "users", "personals"))
|
||||
|
||||
mustWriteJSON(t, filepath.Join(root, "settings.json"), map[string]any{
|
||||
"installation": map[string]any{
|
||||
"id": "installation-1",
|
||||
"name": "MangoPig",
|
||||
"isBootstrapped": true,
|
||||
},
|
||||
"organization": map[string]any{
|
||||
"id": "org-1",
|
||||
"name": "Primary Organization",
|
||||
"slug": "primary-organization",
|
||||
},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "layout.json"), map[string]any{
|
||||
"type": "tenant-layout",
|
||||
"home": map[string]any{"defaultProjectSlug": "primary-project"},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "departments", "department-primary-department", "settings.json"), map[string]any{
|
||||
"id": "dept-1",
|
||||
"name": "Primary Department",
|
||||
"slug": "primary-department",
|
||||
"type": "department",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "departments", "department-primary-department", "users.json"), map[string]any{
|
||||
"users": []map[string]any{{"id": "admin-1"}},
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "departments", "department-primary-department", "teams", "team-primary-team", "settings.json"), map[string]any{
|
||||
"id": "team-1",
|
||||
"name": "Primary Team",
|
||||
"slug": "primary-team",
|
||||
"type": "team",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "settings.json"), map[string]any{
|
||||
"id": "project-1",
|
||||
"name": "Primary Project",
|
||||
"slug": "primary-project",
|
||||
"type": "project",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "projects", "project-primary-project", "home.json"), map[string]any{
|
||||
"type": "project-home",
|
||||
"project": "primary-project",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "users", "settings.json"), map[string]any{
|
||||
"primaryAdminId": "admin-1",
|
||||
})
|
||||
mustWriteJSON(t, filepath.Join(root, "users", "data.json"), map[string]any{
|
||||
"users": []map[string]any{{"id": "admin-1", "email": "ronald@example.com"}},
|
||||
})
|
||||
|
||||
nodes, err := ScanRoot(root)
|
||||
if err != nil {
|
||||
t.Fatalf("ScanRoot() error = %v", err)
|
||||
}
|
||||
|
||||
index := make(map[string]Node, len(nodes))
|
||||
for _, node := range nodes {
|
||||
index[node.Path] = node
|
||||
}
|
||||
|
||||
rootNode, ok := index[rootProjectionPath]
|
||||
if !ok {
|
||||
t.Fatalf("expected synthetic root node")
|
||||
}
|
||||
if rootNode.LogicalType != "tenant_root" {
|
||||
t.Fatalf("expected root logical type tenant_root, got %q", rootNode.LogicalType)
|
||||
}
|
||||
if rootNode.OrganizationSlug != "primary-organization" {
|
||||
t.Fatalf("expected root organization slug primary-organization, got %q", rootNode.OrganizationSlug)
|
||||
}
|
||||
|
||||
tenantSettings := index["settings.json"]
|
||||
if tenantSettings.LogicalType != "tenant" || tenantSettings.FileRole != "settings" {
|
||||
t.Fatalf("unexpected tenant settings classification: %#v", tenantSettings)
|
||||
}
|
||||
if tenantSettings.InstallationID != "installation-1" {
|
||||
t.Fatalf("expected installation id installation-1, got %q", tenantSettings.InstallationID)
|
||||
}
|
||||
|
||||
deptSettings := index["departments/department-primary-department/settings.json"]
|
||||
if deptSettings.DepartmentSlug != "primary-department" {
|
||||
t.Fatalf("expected department slug primary-department, got %q", deptSettings.DepartmentSlug)
|
||||
}
|
||||
if deptSettings.ResourceID != "dept-1" {
|
||||
t.Fatalf("expected department resource id dept-1, got %q", deptSettings.ResourceID)
|
||||
}
|
||||
|
||||
teamSettings := index["departments/department-primary-department/teams/team-primary-team/settings.json"]
|
||||
if teamSettings.TeamSlug != "primary-team" {
|
||||
t.Fatalf("expected team slug primary-team, got %q", teamSettings.TeamSlug)
|
||||
}
|
||||
|
||||
projectSettings := index["projects/project-primary-project/settings.json"]
|
||||
if projectSettings.ProjectSlug != "primary-project" {
|
||||
t.Fatalf("expected project slug primary-project, got %q", projectSettings.ProjectSlug)
|
||||
}
|
||||
if projectSettings.ResourceName != "Primary Project" {
|
||||
t.Fatalf("expected project resource name Primary Project, got %q", projectSettings.ResourceName)
|
||||
}
|
||||
|
||||
projectTree := index["projects/project-primary-project/tree"]
|
||||
if projectTree.LogicalType != "project_tree" || projectTree.NodeKind != NodeKindDirectory {
|
||||
t.Fatalf("unexpected project tree node: %#v", projectTree)
|
||||
}
|
||||
|
||||
usersData := index["users/data.json"]
|
||||
if usersData.LogicalType != "users" || usersData.FileRole != "data" {
|
||||
t.Fatalf("unexpected users data classification: %#v", usersData)
|
||||
}
|
||||
if usersData.Checksum == "" || usersData.SizeBytes == 0 {
|
||||
t.Fatalf("expected users/data.json checksum and size to be populated: %#v", usersData)
|
||||
}
|
||||
}
|
||||
|
||||
func mustMkdirAll(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||
t.Fatalf("MkdirAll(%q) error = %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustWriteJSON(t *testing.T, path string, payload any) {
|
||||
t.Helper()
|
||||
bytes, err := json.MarshalIndent(payload, "", " ")
|
||||
if err != nil {
|
||||
t.Fatalf("MarshalIndent(%q) error = %v", path, err)
|
||||
}
|
||||
bytes = append(bytes, '\n')
|
||||
if err := os.WriteFile(path, bytes, 0o644); err != nil {
|
||||
t.Fatalf("WriteFile(%q) error = %v", path, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user