From 811d76d27a574e169abe4b5a02612e4f8f2228e7 Mon Sep 17 00:00:00 2001 From: Molecule AI Core-BE Date: Wed, 13 May 2026 06:23:25 +0000 Subject: [PATCH] =?UTF-8?q?[core-be-agent]=20fix=20tests:=20routing=20r2?= =?UTF-8?q?=E2=86=92r=20for=20delete/resolve,=20CascadeDelete=20WithArgs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - workspace_crud_test.go: TestDelete_* tests registered routes on r2 but called r.ServeHTTP, causing unmocked DB calls. TestUpdate_WorkspaceNotFound same. TestCascadeDelete_DescendantQueryError had WithArgs(wsID) but the actual QueryContext call passes zero args (workspace ID is embedded in query string). - instructions_test.go: TestInstructionsDelete_* and TestInstructionsResolve_* had same r/r2 routing mismatch. --- .../internal/handlers/instructions_test.go | 24 ++++++----------- .../internal/handlers/workspace_crud_test.go | 26 +++++++++---------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/workspace-server/internal/handlers/instructions_test.go b/workspace-server/internal/handlers/instructions_test.go index 2ed2e2ce..801f3761 100644 --- a/workspace-server/internal/handlers/instructions_test.go +++ b/workspace-server/internal/handlers/instructions_test.go @@ -440,8 +440,7 @@ func TestInstructionsUpdate_UpdateError(t *testing.T) { func TestInstructionsDelete_HappyPath(t *testing.T) { mock, r := setupInstructionsTest(t) h := NewInstructionsHandler() - r2 := gin.New() - r2.DELETE("/instructions/:id", h.Delete) + r.DELETE("/instructions/:id", h.Delete) mock.ExpectExec(`DELETE FROM platform_instructions WHERE id = \$1`). WithArgs("inst-123"). @@ -459,8 +458,7 @@ func TestInstructionsDelete_HappyPath(t *testing.T) { func TestInstructionsDelete_NotFound(t *testing.T) { mock, r := setupInstructionsTest(t) h := NewInstructionsHandler() - r2 := gin.New() - r2.DELETE("/instructions/:id", h.Delete) + r.DELETE("/instructions/:id", h.Delete) mock.ExpectExec(`DELETE FROM platform_instructions WHERE id = \$1`). WithArgs("nonexistent"). @@ -478,8 +476,7 @@ func TestInstructionsDelete_NotFound(t *testing.T) { func TestInstructionsDelete_DeleteError(t *testing.T) { mock, r := setupInstructionsTest(t) h := NewInstructionsHandler() - r2 := gin.New() - r2.DELETE("/instructions/:id", h.Delete) + r.DELETE("/instructions/:id", h.Delete) mock.ExpectExec(`DELETE FROM platform_instructions WHERE id = \$1`). WillReturnError(sql.ErrConnDone) @@ -498,8 +495,7 @@ func TestInstructionsDelete_DeleteError(t *testing.T) { func TestInstructionsResolve_NoInstructions(t *testing.T) { mock, r := setupInstructionsTest(t) h := NewInstructionsHandler() - r2 := gin.New() - r2.GET("/workspaces/:id/instructions/resolve", h.Resolve) + r.GET("/workspaces/:id/instructions/resolve", h.Resolve) mock.ExpectQuery(`SELECT scope, title, content FROM platform_instructions`). WithArgs("ws-uuid-123"). @@ -528,8 +524,7 @@ func TestInstructionsResolve_NoInstructions(t *testing.T) { func TestInstructionsResolve_GlobalOnly(t *testing.T) { mock, r := setupInstructionsTest(t) h := NewInstructionsHandler() - r2 := gin.New() - r2.GET("/workspaces/:id/instructions/resolve", h.Resolve) + r.GET("/workspaces/:id/instructions/resolve", h.Resolve) mock.ExpectQuery(`SELECT scope, title, content FROM platform_instructions`). WithArgs("ws-uuid-123"). @@ -556,8 +551,7 @@ func TestInstructionsResolve_GlobalOnly(t *testing.T) { func TestInstructionsResolve_GlobalPlusWorkspace(t *testing.T) { mock, r := setupInstructionsTest(t) h := NewInstructionsHandler() - r2 := gin.New() - r2.GET("/workspaces/:id/instructions/resolve", h.Resolve) + r.GET("/workspaces/:id/instructions/resolve", h.Resolve) mock.ExpectQuery(`SELECT scope, title, content FROM platform_instructions`). WithArgs("ws-uuid-123"). @@ -589,8 +583,7 @@ func TestInstructionsResolve_GlobalPlusWorkspace(t *testing.T) { func TestInstructionsResolve_QueryError(t *testing.T) { mock, r := setupInstructionsTest(t) h := NewInstructionsHandler() - r2 := gin.New() - r2.GET("/workspaces/:id/instructions/resolve", h.Resolve) + r.GET("/workspaces/:id/instructions/resolve", h.Resolve) mock.ExpectQuery(`SELECT scope, title, content FROM platform_instructions`). WithArgs("ws-uuid-123"). @@ -608,8 +601,7 @@ func TestInstructionsResolve_QueryError(t *testing.T) { func TestInstructionsResolve_MissingWorkspaceID(t *testing.T) { _, r := setupInstructionsTest(t) h := NewInstructionsHandler() - r2 := gin.New() - r2.GET("/workspaces/:id/instructions/resolve", h.Resolve) + r.GET("/workspaces/:id/instructions/resolve", h.Resolve) // Empty workspace ID req, _ := http.NewRequest("GET", "/workspaces//instructions/resolve", nil) diff --git a/workspace-server/internal/handlers/workspace_crud_test.go b/workspace-server/internal/handlers/workspace_crud_test.go index 504d4b4a..e5a069e1 100644 --- a/workspace-server/internal/handlers/workspace_crud_test.go +++ b/workspace-server/internal/handlers/workspace_crud_test.go @@ -215,8 +215,7 @@ func TestUpdate_InvalidBody(t *testing.T) { func TestUpdate_WorkspaceNotFound(t *testing.T) { mock, r := setupWorkspaceCrudTest(t) h := NewWorkspaceHandler(nil, nil, nil, nil) - r2 := gin.New() - r2.PATCH("/workspaces/:id", h.Update) + r.PATCH("/workspaces/:id", h.Update) wsID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" @@ -229,7 +228,7 @@ func TestUpdate_WorkspaceNotFound(t *testing.T) { req, _ := http.NewRequest("PATCH", "/workspaces/"+wsID, bytes.NewReader(b)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() - r2.ServeHTTP(w, req) + r.ServeHTTP(w, req) if w.Code != http.StatusNotFound { t.Errorf("expected 404, got %d: %s", w.Code, w.Body.String()) @@ -375,12 +374,11 @@ func TestUpdate_WorkspaceDirRelativePath(t *testing.T) { func TestDelete_InvalidUUID(t *testing.T) { _, r := setupWorkspaceCrudTest(t) h := NewWorkspaceHandler(nil, nil, nil, nil) - r2 := gin.New() - r2.DELETE("/workspaces/:id", h.Delete) + r.DELETE("/workspaces/:id", h.Delete) req, _ := http.NewRequest("DELETE", "/workspaces/not-a-uuid", nil) w := httptest.NewRecorder() - r2.ServeHTTP(w, req) + r.ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String()) @@ -390,8 +388,7 @@ func TestDelete_InvalidUUID(t *testing.T) { func TestDelete_HasChildrenWithoutConfirm(t *testing.T) { mock, r := setupWorkspaceCrudTest(t) h := NewWorkspaceHandler(nil, nil, nil, nil) - r2 := gin.New() - r2.DELETE("/workspaces/:id", h.Delete) + r.DELETE("/workspaces/:id", h.Delete) wsID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" @@ -403,7 +400,7 @@ func TestDelete_HasChildrenWithoutConfirm(t *testing.T) { req, _ := http.NewRequest("DELETE", "/workspaces/"+wsID, nil) // No ?confirm=true w := httptest.NewRecorder() - r2.ServeHTTP(w, req) + r.ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("expected 409, got %d: %s", w.Code, w.Body.String()) @@ -424,8 +421,7 @@ func TestDelete_HasChildrenWithoutConfirm(t *testing.T) { func TestDelete_ChildrenCheckQueryError(t *testing.T) { mock, r := setupWorkspaceCrudTest(t) h := NewWorkspaceHandler(nil, nil, nil, nil) - r2 := gin.New() - r2.DELETE("/workspaces/:id", h.Delete) + r.DELETE("/workspaces/:id", h.Delete) wsID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" @@ -435,7 +431,7 @@ func TestDelete_ChildrenCheckQueryError(t *testing.T) { req, _ := http.NewRequest("DELETE", "/workspaces/"+wsID, nil) w := httptest.NewRecorder() - r2.ServeHTTP(w, req) + r.ServeHTTP(w, req) if w.Code != http.StatusInternalServerError { t.Errorf("expected 500, got %d", w.Code) @@ -572,15 +568,17 @@ func TestCascadeDelete_InvalidUUID(t *testing.T) { } func TestCascadeDelete_DescendantQueryError(t *testing.T) { - mock, _ := setupWorkspaceCrudTest(t) + mock, r := setupWorkspaceCrudTest(t) wsID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" + _ = r // CascadeDelete returns early on descendant query error — nil deps for // StopWorkspace/RemoveVolume/broadcaster are fine since they are never // reached in this error path. h := &WorkspaceHandler{} + // Note: the descendant CTE query is called with zero args (workspace ID + // is embedded in the query string, not passed as a query arg). mock.ExpectQuery(`WITH RECURSIVE descendants AS`). - WithArgs(wsID). WillReturnError(sql.ErrConnDone) deleted, stopErrs, err := h.CascadeDelete(context.Background(), wsID)