Files
pulse/internal/api/config_handlers_delete_test.go
T
rcourtman ec28bb3314 Accept aggregator semantic IDs on node mutation endpoints
The unified /api/connections aggregator emits IDs as {type}:{name}
(e.g. "pve:delly"), but the PUT/DELETE/refresh-cluster/test endpoints
only parsed the legacy {type}-{index} array-position form. That left
the new Connection surface unable to drive any mutation against the
entries it lists.

HandleUpdateNode, HandleDeleteNode, HandleRefreshClusterNodes, and
HandleTestNode now route the incoming ID through a shared
resolveNodeID helper: colon-form resolves by Name (404 on miss),
dash-form keeps the existing index semantics. Frontend connection
client gains setEnabled/remove that dispatch to the right per-type
endpoint by ID prefix.
2026-04-19 20:42:22 +01:00

132 lines
3.6 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
)
func TestHandleDeleteNode(t *testing.T) {
// Setup temporary directory for config persistence
tempDir, err := os.MkdirTemp("", "pulse-delete-node-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
// Setup dummy configuration with a PVE node
dummyCfg := &config.Config{
PVEInstances: []config.PVEInstance{
{Name: "pve1", Host: "10.0.0.1"},
{Name: "pve2", Host: "10.0.0.2"},
},
PBSInstances: []config.PBSInstance{
{Name: "pbs1", Host: "10.0.0.3"},
},
}
dummyCfg.DataPath = tempDir
// Create handler with dummy persistence
handler := newTestConfigHandlers(t, dummyCfg)
tests := []struct {
name string
nodeID string
expectedStatus int
verifyDeletion func(*testing.T, *config.Config)
}{
{
name: "success_delete_pve_node",
nodeID: "pve-0", // Delete first PVE node (pve1)
expectedStatus: http.StatusOK,
verifyDeletion: func(t *testing.T, c *config.Config) {
if len(c.PVEInstances) != 1 {
t.Errorf("expected 1 PVE instance, got %d", len(c.PVEInstances))
}
if len(c.PVEInstances) > 0 && c.PVEInstances[0].Name != "pve2" {
t.Errorf("expected remaining node to be pve2, got %s", c.PVEInstances[0].Name)
}
},
},
{
name: "success_delete_pbs_node",
nodeID: "pbs-0",
expectedStatus: http.StatusOK,
verifyDeletion: func(t *testing.T, c *config.Config) {
if len(c.PBSInstances) != 0 {
t.Errorf("expected 0 PBS instances, got %d", len(c.PBSInstances))
}
},
},
{
name: "fail_invalid_format",
nodeID: "invalidid",
expectedStatus: http.StatusBadRequest,
verifyDeletion: nil,
},
{
name: "fail_invalid_index",
nodeID: "pve-999",
expectedStatus: http.StatusNotFound,
verifyDeletion: nil,
},
{
name: "fail_invalid_type",
nodeID: "unknown-0",
expectedStatus: http.StatusNotFound, // Handler returns 404 for unknown types
verifyDeletion: nil,
},
{
// Semantic ID form emitted by the unified connections aggregator.
// Looks up the remaining PVE ("pve2") by name after earlier deletes.
name: "success_semantic_id_pve_by_name",
nodeID: "pve:pve2",
expectedStatus: http.StatusOK,
verifyDeletion: func(t *testing.T, c *config.Config) {
if len(c.PVEInstances) != 0 {
t.Errorf("expected 0 PVE instances after semantic-id delete, got %d", len(c.PVEInstances))
}
},
},
{
// Name that no longer exists after the previous delete.
name: "fail_semantic_id_unknown_name",
nodeID: "pve:pve2",
expectedStatus: http.StatusNotFound,
verifyDeletion: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("DELETE", "/api/config/nodes/"+tt.nodeID, nil)
w := httptest.NewRecorder()
handler.HandleDeleteNode(w, req)
if w.Code != tt.expectedStatus {
t.Errorf("handler returned wrong status code: got %v want %v", w.Code, tt.expectedStatus)
}
if tt.verifyDeletion != nil {
tt.verifyDeletion(t, dummyCfg)
}
// For successful requests, ensure response is valid JSON
if w.Code == http.StatusOK {
var response map[string]string
if err := json.NewDecoder(w.Body).Decode(&response); err != nil {
t.Errorf("response was not valid JSON: %v", err)
}
if response["status"] != "success" {
t.Errorf("expected status 'success', got '%s'", response["status"])
}
}
})
}
}