Compare commits

...

3 Commits

Author SHA1 Message Date
core-be 072d80a588 test(handlers): add pure function coverage for delegation_sweeper.go and eic_tunnel_pool_setup.go
audit-force-merge / audit (pull_request) Has been skipped
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 15s
cascade-list-drift-gate / check (pull_request) Failing after 4s
Check migration collisions / Migration version collision check (pull_request) Successful in 8s
CI / Detect changes (pull_request) Successful in 15s
MCP Stdio Transport Regression / MCP stdio with regular-file stdout (pull_request) Successful in 1m18s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 22s
CI / Platform (Go) (pull_request) Successful in 4m48s
E2E API Smoke Test / detect-changes (pull_request) Successful in 7s
E2E Chat / detect-changes (pull_request) Successful in 8s
E2E Peer Visibility (literal MCP list_peers) / E2E Peer Visibility (pull_request) Has been skipped
E2E Staging Canvas (Playwright) / detect-changes (pull_request) Successful in 8s
CI / Canvas (Next.js) (pull_request) Successful in 5m58s
E2E Staging SaaS (full lifecycle) / pr-validate (pull_request) Successful in 30s
E2E Staging SaaS (full lifecycle) / E2E Staging SaaS (pull_request) Has been skipped
Handlers Postgres Integration / detect-changes (pull_request) Successful in 3s
Harness Replays / detect-changes (pull_request) Successful in 4s
CI / all-required (pull_request) Successful in 3m13s
Lint curl status-code capture / Scan workflows for curl status-capture pollution (pull_request) Successful in 4s
lint-continue-on-error-tracking / lint-continue-on-error-tracking (pull_request) Successful in 1m16s
CI / Python Lint & Test (pull_request) Successful in 6m55s
lint-mask-pr-atomicity / lint-mask-pr-atomicity (pull_request) Successful in 1m27s
Lint pre-flip continue-on-error / Verify continue-on-error flips have run-log proof (pull_request) Failing after 1m12s
lint-required-context-exists-in-bp / lint-required-context-exists-in-bp (pull_request) Failing after 1m21s
lint-required-no-paths / lint-required-no-paths (pull_request) Successful in 1m12s
publish-runtime-autobump / bump-and-tag (pull_request) Has been skipped
review-check-tests / review-check.sh regression tests (pull_request) Successful in 10s
publish-runtime-autobump / pr-validate (pull_request) Successful in 34s
E2E Staging External Runtime / E2E Staging External Runtime (pull_request) Successful in 5m12s
Runtime PR-Built Compatibility / detect-changes (pull_request) Successful in 10s
Lint workflow YAML (Gitea-1.22.6-hostile shapes) / Lint workflow YAML for Gitea-1.22.6-hostile shapes (pull_request) Successful in 1m27s
Secret scan / Scan diff for credential-shaped strings (pull_request) Failing after 27s
gate-check-v3 / gate-check (pull_request) Successful in 9s
qa-review / approved (pull_request) Successful in 5s
security-review / approved (pull_request) Successful in 4s
sop-checklist / na-declarations (pull_request) N/A: (none)
sop-checklist / all-items-acked (pull_request) Successful in 5s
sop-checklist / review-refire (pull_request) Has been skipped
sop-tier-check / tier-check (pull_request) Successful in 4s
Runtime Pin Compatibility / PyPI-latest install + import smoke (pull_request) Successful in 1m36s
Ops Scripts Tests / Ops scripts (unittest) (pull_request) Successful in 1m8s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Successful in 1m34s
CI / Canvas Deploy Reminder (pull_request) Has been skipped
Harness Replays / Harness Replays (pull_request) Successful in 2s
Handlers Postgres Integration / Handlers Postgres Integration (pull_request) Successful in 1m51s
Runtime PR-Built Compatibility / PR-built wheel + import smoke (pull_request) Successful in 2m37s
E2E Chat / E2E Chat (pull_request) Failing after 5m52s
E2E Staging Canvas (Playwright) / Canvas tabs E2E (pull_request) Successful in 10m6s
- envDuration: 6 test cases covering missing/valid/zero/negative/non-numeric/whitespace
- setupRealEICTunnel: empty instanceID returns error without calling SSH/DB

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 04:07:58 +00:00
core-be 53571f6525 test(handlers): add PatchAbilities regression coverage
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 3s
CI / Detect changes (pull_request) Successful in 4s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 9s
E2E API Smoke Test / detect-changes (pull_request) Successful in 4s
E2E Chat / detect-changes (pull_request) Successful in 4s
E2E Staging Canvas (Playwright) / detect-changes (pull_request) Successful in 4s
Handlers Postgres Integration / detect-changes (pull_request) Successful in 3s
Harness Replays / detect-changes (pull_request) Successful in 3s
Runtime PR-Built Compatibility / detect-changes (pull_request) Successful in 4s
Secret scan / Scan diff for credential-shaped strings (pull_request) Successful in 5s
lint-required-no-paths / lint-required-no-paths (pull_request) Successful in 53s
CI / Platform (Go) (pull_request) Successful in 4m46s
CI / Canvas (Next.js) (pull_request) Successful in 6m42s
CI / Python Lint & Test (pull_request) Successful in 6m32s
CI / all-required (pull_request) Successful in 4m35s
E2E Staging Canvas (Playwright) / Canvas tabs E2E (pull_request) Successful in 4s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Successful in 48s
Harness Replays / Harness Replays (pull_request) Successful in 2s
Runtime PR-Built Compatibility / PR-built wheel + import smoke (pull_request) Successful in 2s
CI / Canvas Deploy Reminder (pull_request) Has been skipped
Handlers Postgres Integration / Handlers Postgres Integration (pull_request) Successful in 1m27s
E2E Chat / E2E Chat (pull_request) Failing after 4m35s
gate-check-v3 / gate-check (pull_request) Successful in 2s
sop-checklist / na-declarations (pull_request) N/A: (none)
sop-checklist / all-items-acked (pull_request) Successful in 3s
sop-tier-check / tier-check (pull_request) Successful in 3s
qa-review / approved (pull_request) Refired via /qa-recheck by unknown
security-review / approved (pull_request) Refired via /security-recheck by unknown
Adds 10 test cases for PATCH /workspaces/:id/abilities:

Happy path:
- broadcast_enabled=true → 200
- broadcast_enabled=false → 200
- talk_to_user_enabled=true → 200
- both fields in one request → 200 (each UPDATE in order)

Input validation:
- empty body {} → 400
- non-JSON body → 400
- non-UUID workspace ID → 400

Database errors:
- workspace not found → 404
- DB error on existence check → 500
- DB error on broadcast_enabled UPDATE → 500
- DB error on talk_to_user_enabled UPDATE → 500

Covers workspace_abilities.go which was the only unreviewed handler
with zero test coverage. No production code changed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 03:53:13 +00:00
core-be 610a5df5bc fix(workspace-server): distinguish DB error from not-found in PatchAbilities
The existence-check condition `err != nil || !exists` conflated two
semantically different outcomes into a single 404 response:

  - err != nil    → DB/internal error → should be 500
  - !exists       → workspace absent  → 404 is correct

Fix: split into two explicit branches. DB errors now return 500 with
a logged reason. The not-found case remains 404.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 03:53:13 +00:00
3 changed files with 366 additions and 1 deletions
@@ -0,0 +1,95 @@
package handlers
import (
"context"
"os"
"testing"
"time"
)
// ── envDuration ──────────────────────────────────────────────────────────────
func TestEnvDuration_MissingEnvVar(t *testing.T) {
key := "TEST_DOES_NOT_EXIST_12345"
defer os.Unsetenv(key)
os.Unsetenv(key)
got := envDuration(key, 42*time.Second)
if got != 42*time.Second {
t.Errorf("missing env var: got %v, want 42s", got)
}
}
func TestEnvDuration_ValidPositiveSeconds(t *testing.T) {
key := "TEST_VALID_DURATION"
defer os.Unsetenv(key)
os.Setenv(key, "120")
got := envDuration(key, 30*time.Second)
if got != 120*time.Second {
t.Errorf("valid 120s: got %v, want 120s", got)
}
}
func TestEnvDuration_ValidOneSecond(t *testing.T) {
key := "TEST_ONE_SECOND"
defer os.Unsetenv(key)
os.Setenv(key, "1")
got := envDuration(key, 60*time.Second)
if got != 1*time.Second {
t.Errorf("valid 1s: got %v, want 1s", got)
}
}
func TestEnvDuration_NonNumericReturnsDefault(t *testing.T) {
key := "TEST_NON_NUMERIC"
defer os.Unsetenv(key)
os.Setenv(key, "abc")
got := envDuration(key, 10*time.Second)
if got != 10*time.Second {
t.Errorf("non-numeric: got %v, want 10s default", got)
}
}
func TestEnvDuration_ZeroReturnsDefault(t *testing.T) {
key := "TEST_ZERO"
defer os.Unsetenv(key)
os.Setenv(key, "0")
got := envDuration(key, 5*time.Second)
if got != 5*time.Second {
t.Errorf("zero: got %v, want 5s default", got)
}
}
func TestEnvDuration_NegativeReturnsDefault(t *testing.T) {
key := "TEST_NEGATIVE"
defer os.Unsetenv(key)
os.Setenv(key, "-10")
got := envDuration(key, 15*time.Second)
if got != 15*time.Second {
t.Errorf("negative: got %v, want 15s default", got)
}
}
func TestEnvDuration_WhitespaceReturnsDefault(t *testing.T) {
key := "TEST_WHITESPACE"
defer os.Unsetenv(key)
os.Setenv(key, " 30 ")
got := envDuration(key, 20*time.Second)
if got != 20*time.Second {
t.Errorf("whitespace: got %v, want 20s default", got)
}
}
// ── setupRealEICTunnel — empty instanceID ───────────────────────────────────
func TestSetupRealEICTunnel_EmptyInstanceID(t *testing.T) {
sess, cleanup, err := setupRealEICTunnel(context.Background(), "")
if err == nil {
t.Error("empty instanceID: expected error, got nil")
}
if sess != (eicSSHSession{}) {
t.Errorf("empty instanceID: session should be zero value, got %+v", sess)
}
if cleanup != nil {
t.Error("empty instanceID: cleanup should be nil on error")
}
}
@@ -51,7 +51,12 @@ func PatchAbilities(c *gin.Context) {
var exists bool
if err := db.DB.QueryRowContext(ctx,
`SELECT EXISTS(SELECT 1 FROM workspaces WHERE id = $1 AND status != 'removed')`, id,
).Scan(&exists); err != nil || !exists {
).Scan(&exists); err != nil {
log.Printf("PatchAbilities: workspace existence check for %s: %v", id, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"})
return
}
if !exists {
c.JSON(http.StatusNotFound, gin.H{"error": "workspace not found"})
return
}
@@ -0,0 +1,265 @@
package handlers
// workspace_abilities_test.go — regression tests for PATCH /workspaces/:id/abilities.
//
// The handler toggles two workspace-level ability flags:
// broadcast_enabled — workspace may POST /broadcast to send org-wide messages
// talk_to_user_enabled — workspace may deliver canvas chat messages via
// send_message_to_user / POST /notify
//
// Gated behind AdminAuth so workspace agents cannot self-modify their own
// ability flags. These tests cover the uncredentialed unit-path (AdminAuth
// middleware is tested separately).
import (
"bytes"
"database/sql"
"net/http"
"net/http/httptest"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/gin-gonic/gin"
)
// validUUID is a stable test workspace ID that passes uuid.Parse validation.
const validUUID = "00000000-0000-0000-0000-000000000001"
// buildAbilitiesCtx wires a gin.Context for PATCH /workspaces/:id/abilities.
func buildAbilitiesCtx(id string, body string) (*httptest.ResponseRecorder, *gin.Context) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "id", Value: id}}
c.Request = httptest.NewRequest("PATCH", "/workspaces/"+id+"/abilities",
bytes.NewBufferString(body))
c.Request.Header.Set("Content-Type", "application/json")
return w, c
}
// -------- Happy path --------
// PatchAbilities writes broadcast_enabled=true and returns 200.
func TestPatchAbilities_BroadcastEnabled_200(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnResult(sqlmock.NewResult(0, 1))
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// PatchAbilities writes broadcast_enabled=false and returns 200.
func TestPatchAbilities_BroadcastEnabledFalse_200(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, false).
WillReturnResult(sqlmock.NewResult(0, 1))
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":false}`)
PatchAbilities(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// PatchAbilities writes talk_to_user_enabled=true and returns 200.
func TestPatchAbilities_TalkToUserEnabled_200(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnResult(sqlmock.NewResult(0, 1))
w, c := buildAbilitiesCtx(validUUID, `{"talk_to_user_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// Both ability flags in the same request are each written with their own UPDATE.
func TestPatchAbilities_BothFields_200(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
// broadcast_enabled written first
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnResult(sqlmock.NewResult(0, 1))
// talk_to_user_enabled written second
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, false).
WillReturnResult(sqlmock.NewResult(0, 1))
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true,"talk_to_user_enabled":false}`)
PatchAbilities(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// -------- Input validation --------
// Empty body (neither field) → 400.
func TestPatchAbilities_NoAbilityFields_400(t *testing.T) {
setupTestDB(t)
setupTestRedis(t)
w, c := buildAbilitiesCtx(validUUID, `{}`)
PatchAbilities(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
// Non-JSON body → 400.
func TestPatchAbilities_InvalidJSON_400(t *testing.T) {
setupTestDB(t)
setupTestRedis(t)
w, c := buildAbilitiesCtx(validUUID, `not json at all`)
PatchAbilities(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
// Invalid (non-UUID) workspace ID → 400.
func TestPatchAbilities_InvalidWorkspaceID_400(t *testing.T) {
setupTestDB(t)
setupTestRedis(t)
w, c := buildAbilitiesCtx("not-a-uuid", `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
// -------- Database errors --------
// Workspace does not exist → 404.
func TestPatchAbilities_WorkspaceNotFound_404(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(false))
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusNotFound {
t.Errorf("expected 404, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// DB error on existence check → 500.
func TestPatchAbilities_DBErrorOnExistsCheck_500(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnError(sql.ErrConnDone)
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusInternalServerError {
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// DB error on broadcast_enabled UPDATE → 500.
func TestPatchAbilities_DBErrorOnBroadcastUpdate_500(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnError(sql.ErrConnDone)
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusInternalServerError {
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// DB error on talk_to_user_enabled UPDATE → 500.
func TestPatchAbilities_DBErrorOnTalkToUserUpdate_500(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnError(sql.ErrConnDone)
w, c := buildAbilitiesCtx(validUUID, `{"talk_to_user_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusInternalServerError {
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}