Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8711fc92db | |||
| e84bf3a4c6 | |||
| 376f78278d | |||
| 3d0d9b1818 | |||
| 1c61db9042 |
+12
-10
@@ -145,10 +145,10 @@ jobs:
|
||||
# the diagnostic step with its own continue-on-error: true (line 203).
|
||||
# Flip confirmed by CI / Platform (Go) status = success on main HEAD 363905d3.
|
||||
continue-on-error: false
|
||||
# Job-level ceiling. The go test step below runs with a per-step 10m timeout;
|
||||
# this cap catches any step that leaks past that. Set well above 10m so
|
||||
# Job-level ceiling. The go test step below runs with a per-step 30m timeout;
|
||||
# this cap catches any step that leaks past that. Set well above 30m so
|
||||
# the per-step timeout is the active constraint.
|
||||
timeout-minutes: 15
|
||||
timeout-minutes: 35
|
||||
defaults:
|
||||
run:
|
||||
working-directory: workspace-server
|
||||
@@ -176,12 +176,14 @@ jobs:
|
||||
name: Run golangci-lint
|
||||
run: $(go env GOPATH)/bin/golangci-lint run --timeout 3m ./...
|
||||
- if: always()
|
||||
name: Diagnostic — per-package verbose 60s
|
||||
name: Diagnostic — per-package verbose (300s timeout)
|
||||
run: |
|
||||
set +e
|
||||
go test -race -v -timeout 60s ./internal/handlers/... 2>&1 | tee /tmp/test-handlers.log
|
||||
# 300s allows handlers + pendinguploads packages to complete on cold
|
||||
# runners with -race instrumentation (~60-120s each vs ~14s non-race).
|
||||
go test -race -v -timeout 300s ./internal/handlers/... 2>&1 | tee /tmp/test-handlers.log
|
||||
handlers_exit=$?
|
||||
go test -race -v -timeout 60s ./internal/pendinguploads/... 2>&1 | tee /tmp/test-pu.log
|
||||
go test -race -v -timeout 300s ./internal/pendinguploads/... 2>&1 | tee /tmp/test-pu.log
|
||||
pu_exit=$?
|
||||
echo "::group::handlers exit=$handlers_exit (last 100 lines)"
|
||||
tail -100 /tmp/test-handlers.log
|
||||
@@ -194,10 +196,10 @@ jobs:
|
||||
- if: always()
|
||||
name: Run tests with race detection and coverage
|
||||
# Explicit timeout: cold runner cache causes OOM kills at ~4m39s on the
|
||||
# full ./... suite with race detection + coverage. A 10m per-step timeout
|
||||
# lets the suite complete on cold cache (~5-7m) while failing cleanly
|
||||
# instead of OOM-killing. The job-level timeout (15m) is a backstop.
|
||||
run: go test -race -timeout 10m -coverprofile=coverage.out ./...
|
||||
# full ./... suite with race detection + coverage. A 30m per-step timeout
|
||||
# lets the suite complete on cold cache (~13-25m) while failing cleanly
|
||||
# instead of OOM-killing. The job-level timeout (35m) is a backstop.
|
||||
run: go test -race -timeout 30m -coverprofile=coverage.out ./...
|
||||
|
||||
- if: always()
|
||||
name: Per-file coverage report
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { useCanvasStore } from "@/store/canvas";
|
||||
import { resolveWorkspaceName } from "../hooks/resolveWorkspaceName";
|
||||
|
||||
beforeEach(() => {
|
||||
// Reset store to a clean slate between tests so node lookup is deterministic.
|
||||
useCanvasStore.setState({ nodes: [] });
|
||||
});
|
||||
|
||||
describe("resolveWorkspaceName", () => {
|
||||
it("returns the workspace name when a node with that ID exists", () => {
|
||||
useCanvasStore.setState({
|
||||
nodes: [
|
||||
{
|
||||
id: "ws-alpha-001",
|
||||
type: "workspace",
|
||||
data: { name: "Alpha Agent" },
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(resolveWorkspaceName("ws-alpha-001")).toBe("Alpha Agent");
|
||||
});
|
||||
|
||||
it("falls back to the first 8 chars of the ID when no matching node exists", () => {
|
||||
expect(resolveWorkspaceName("ws-zzz-not-found")).toBe("ws-zzz-n");
|
||||
});
|
||||
|
||||
it("falls back to the first 8 chars when the node exists but has no name", () => {
|
||||
useCanvasStore.setState({
|
||||
nodes: [
|
||||
{
|
||||
id: "ws-no-name",
|
||||
type: "workspace",
|
||||
// data.name is deliberately absent
|
||||
data: {},
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(resolveWorkspaceName("ws-no-name")).toBe("ws-no-na");
|
||||
});
|
||||
|
||||
it("returns the first 8 chars for a very short ID", () => {
|
||||
expect(resolveWorkspaceName("ab")).toBe("ab");
|
||||
});
|
||||
|
||||
it("returns the first 8 chars when the ID is exactly 8 characters", () => {
|
||||
// slice(0,8) of an 8-char string is the full string
|
||||
const id = "12345678";
|
||||
expect(resolveWorkspaceName(id)).toBe(id);
|
||||
});
|
||||
|
||||
it("picks the right node when multiple workspaces share a prefix", () => {
|
||||
useCanvasStore.setState({
|
||||
nodes: [
|
||||
{
|
||||
id: "00000000-0000-0000-0000-000000000001",
|
||||
type: "workspace",
|
||||
data: { name: "Backend Agent" },
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
{
|
||||
id: "00000000-0000-0000-0000-000000000002",
|
||||
type: "workspace",
|
||||
data: { name: "Frontend Agent" },
|
||||
position: { x: 100, y: 0 },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(resolveWorkspaceName("00000000-0000-0000-0000-000000000002")).toBe(
|
||||
"Frontend Agent"
|
||||
);
|
||||
expect(resolveWorkspaceName("00000000-0000-0000-0000-000000000001")).toBe(
|
||||
"Backend Agent"
|
||||
);
|
||||
});
|
||||
|
||||
it("does not mutate store state between calls", () => {
|
||||
useCanvasStore.setState({
|
||||
nodes: [
|
||||
{
|
||||
id: "stable-id",
|
||||
type: "workspace",
|
||||
data: { name: "Stable Workspace" },
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
resolveWorkspaceName("stable-id");
|
||||
resolveWorkspaceName("unknown-id");
|
||||
|
||||
// Store nodes must be unchanged — resolveWorkspaceName is read-only.
|
||||
const nodes = useCanvasStore.getState().nodes;
|
||||
expect(nodes).toHaveLength(1);
|
||||
expect((nodes[0] as { id: string }).id).toBe("stable-id");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
package handlers
|
||||
|
||||
// plugins_install_test.go — additional coverage for plugins_install.go.
|
||||
//
|
||||
// Gaps filled vs. existing test files:
|
||||
// - plugins_install_external_test.go: Install + Uninstall 422 (external runtime) ✓ covered
|
||||
// - plugins_test.go: Install 400 (missing source, invalid body, etc.) ✓ covered
|
||||
// Uninstall 400 (invalid plugin name, empty name) ✓ covered
|
||||
// Download auth gate ✓ covered
|
||||
// - org_import_helpers_test.go: countWorkspaces, envRequirementKey, sanitizeEnvMembers,
|
||||
// flattenAndSortRequirements, collectOrgEnv ✓ covered
|
||||
//
|
||||
// New test added here:
|
||||
// - Uninstall 503: container not running, no SaaS dispatch.
|
||||
//
|
||||
// NOTE: validateWorkspaceID is not called inside the Install/Uninstall handlers.
|
||||
// UUID validation is the responsibility of the WorkspaceAuth middleware, so no
|
||||
// 400 test is needed here for UUID format.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestPluginUninstall_ContainerNotRunning_Returns503 exercises the 503 path
|
||||
// where neither a local Docker container nor a SaaS instance-id dispatch
|
||||
// resolves. The handler must return "workspace container not running" — NOT a
|
||||
// generic 500 or a misleading 422 (external-runtime) message.
|
||||
func TestPluginUninstall_ContainerNotRunning_Returns503(t *testing.T) {
|
||||
// No docker client + no instance-id lookup → falls through to 503.
|
||||
h := NewPluginsHandler(t.TempDir(), nil, nil)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{
|
||||
{Key: "id", Value: "550e8400-e29b-41d4-a716-446655440000"},
|
||||
{Key: "name", Value: "some-plugin"},
|
||||
}
|
||||
c.Request = httptest.NewRequest("DELETE",
|
||||
"/workspaces/550e8400-e29b-41d4-a716-446655440000/plugins/some-plugin", nil)
|
||||
|
||||
h.Uninstall(c)
|
||||
|
||||
require.Equal(t, http.StatusServiceUnavailable, w.Code)
|
||||
var body map[string]string
|
||||
json.Unmarshal(w.Body.Bytes(), &body)
|
||||
require.Equal(t, "workspace container not running", body["error"])
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
package handlers
|
||||
|
||||
// plugins_listing_test.go — coverage for plugins_listing.go.
|
||||
//
|
||||
// Gaps filled vs. existing test files:
|
||||
// plugins_test.go: ListRegistry (empty/nonexist/with-plugins/filter),
|
||||
// ListAvailableForWorkspace (runtime-lookup/no-lookup),
|
||||
// CheckRuntimeCompatibility (400/empty-container),
|
||||
// parseManifestYAML (valid/invalid/minimal/runtimes) ✓
|
||||
// plugins_helpers_pure_test.go: supportsRuntime (all variants) ✓
|
||||
//
|
||||
// New tests added here:
|
||||
// parseManifestYAML (3): wrong-type arrays (tags/skills/runtimes as numbers),
|
||||
// missing-yaml (bare directory).
|
||||
// listRegistryFiltered: no plugin.yaml (bare dir → fallback name only).
|
||||
// ListAvailableForWorkspace: runtime lookup error → falls back to full registry.
|
||||
// ListInstalled: nil docker → 200 [] (container not running).
|
||||
// ListInstalled: with runtime-lookup → annotates SupportedOnRuntime.
|
||||
// CheckRuntimeCompatibility: container running but exec fails → 500.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// ── parseManifestYAML edge cases ──────────────────────────────────────────────
|
||||
|
||||
// TestParseManifestYAML_WrongTypeArrays verifies that non-string elements
|
||||
// in tags/skills/runtimes are silently dropped (yaml.Unmarshal gives us
|
||||
// float64 for numbers). No panic, no partial corruption — just the field
|
||||
// comes back empty.
|
||||
func TestParseManifestYAML_WrongTypeArrays(t *testing.T) {
|
||||
// YAML where tags/skills/runtimes are arrays of numbers instead of strings.
|
||||
yaml := []byte(`
|
||||
version: "1.0.0"
|
||||
tags:
|
||||
- first
|
||||
- 42
|
||||
- third
|
||||
skills:
|
||||
- valid
|
||||
- 123
|
||||
runtimes:
|
||||
- claude_code
|
||||
- 99
|
||||
`)
|
||||
info := parseManifestYAML("num-arrays", yaml)
|
||||
|
||||
if info.Name != "num-arrays" {
|
||||
t.Errorf("expected fallback name, got %q", info.Name)
|
||||
}
|
||||
if info.Version != "1.0.0" {
|
||||
t.Errorf("version: got %q", info.Version)
|
||||
}
|
||||
// Only string entries survive the type assertion.
|
||||
if len(info.Tags) != 2 || info.Tags[0] != "first" || info.Tags[1] != "third" {
|
||||
t.Errorf("tags: got %v", info.Tags)
|
||||
}
|
||||
if len(info.Skills) != 1 || info.Skills[0] != "valid" {
|
||||
t.Errorf("skills: got %v", info.Skills)
|
||||
}
|
||||
if len(info.Runtimes) != 1 || info.Runtimes[0] != "claude_code" {
|
||||
t.Errorf("runtimes: got %v", info.Runtimes)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseManifestYAML_MissingFile simulates a bare plugin directory
|
||||
// (no plugin.yaml at all) — parseManifestYAML should return the
|
||||
// fallback name and zero values for everything else.
|
||||
func TestParseManifestYAML_MissingFile(t *testing.T) {
|
||||
info := parseManifestYAML("bare-plugin", []byte{})
|
||||
if info.Name != "bare-plugin" {
|
||||
t.Errorf("expected fallback name, got %q", info.Name)
|
||||
}
|
||||
if info.Version != "" {
|
||||
t.Errorf("version should be empty for missing file, got %q", info.Version)
|
||||
}
|
||||
if info.Tags != nil {
|
||||
t.Errorf("tags should be nil, got %v", info.Tags)
|
||||
}
|
||||
if info.Skills != nil {
|
||||
t.Errorf("skills should be nil, got %v", info.Skills)
|
||||
}
|
||||
if info.Runtimes != nil {
|
||||
t.Errorf("runtimes should be nil, got %v", info.Runtimes)
|
||||
}
|
||||
}
|
||||
|
||||
// ── listRegistryFiltered ───────────────────────────────────────────────────────
|
||||
|
||||
// TestListRegistryFiltered_BareDirNoPluginYaml verifies that a plugin
|
||||
// directory without a plugin.yaml is still listed with the fallback name.
|
||||
func TestListRegistryFiltered_BareDirNoPluginYaml(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
bare := filepath.Join(dir, "no-manifest-plugin")
|
||||
if err := os.Mkdir(bare, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Write a README but no plugin.yaml.
|
||||
if err := os.WriteFile(filepath.Join(bare, "README.md"), []byte("Hello"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
h := NewPluginsHandler(dir, nil, nil)
|
||||
plugins := h.listRegistryFiltered("")
|
||||
|
||||
if len(plugins) != 1 {
|
||||
t.Fatalf("expected 1 plugin, got %d", len(plugins))
|
||||
}
|
||||
if plugins[0].Name != "no-manifest-plugin" {
|
||||
t.Errorf("expected bare directory name, got %q", plugins[0].Name)
|
||||
}
|
||||
if plugins[0].Version != "" {
|
||||
t.Errorf("version should be empty for missing manifest, got %q", plugins[0].Version)
|
||||
}
|
||||
}
|
||||
|
||||
// ── ListAvailableForWorkspace ──────────────────────────────────────────────────
|
||||
|
||||
// TestListAvailableForWorkspace_RuntimeLookupErrorFallsBackToAll verifies
|
||||
// that when runtimeLookup returns an error, the handler falls back to the
|
||||
// unfiltered registry (empty runtime string) rather than returning an error.
|
||||
func TestListAvailableForWorkspace_RuntimeLookupErrorFallsBackToAll(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writePluginDir := func(name, manifest string) {
|
||||
p := filepath.Join(dir, name)
|
||||
if err := os.MkdirAll(p, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(p, "plugin.yaml"), []byte(manifest), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
writePluginDir("plugin-a", "name: plugin-a\nruntimes: [claude_code]\n")
|
||||
writePluginDir("plugin-b", "name: plugin-b\nruntimes: [deepagents]\n")
|
||||
|
||||
h := NewPluginsHandler(dir, nil, nil).
|
||||
WithRuntimeLookup(func(id string) (string, error) {
|
||||
return "", ErrWorkspaceNotFound // any error
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{{Key: "id", Value: "ws-errored"}}
|
||||
c.Request = httptest.NewRequest("GET", "/workspaces/ws-errored/plugins/available", nil)
|
||||
h.ListAvailableForWorkspace(c)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
var plugins []pluginInfo
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &plugins))
|
||||
// Both plugins should appear — lookup error means unfiltered registry.
|
||||
require.Len(t, plugins, 2)
|
||||
}
|
||||
|
||||
// ── ListInstalled ─────────────────────────────────────────────────────────────
|
||||
|
||||
// TestListInstalled_NoDockerReturnsEmptyList verifies the 200+empty-JSON
|
||||
// path when the workspace container is not running (nil docker → no backend).
|
||||
func TestListInstalled_NoDockerReturnsEmptyList(t *testing.T) {
|
||||
h := NewPluginsHandler(t.TempDir(), nil, nil)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{{Key: "id", Value: "550e8400-e29b-41d4-a716-446655440000"}}
|
||||
c.Request = httptest.NewRequest("GET",
|
||||
"/workspaces/550e8400-e29b-41d4-a716-446655440000/plugins", nil)
|
||||
h.ListInstalled(c)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
var plugins []pluginInfo
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &plugins))
|
||||
require.Empty(t, plugins)
|
||||
}
|
||||
|
||||
// TestListInstalled_WithRuntimeLookupAnnotatesSupportedOnRuntime verifies that
|
||||
// ListInstalled populates SupportedOnRuntime when a runtime-lookup is wired.
|
||||
func TestListInstalled_WithRuntimeLookupAnnotatesSupportedOnRuntime(t *testing.T) {
|
||||
h := NewPluginsHandler(t.TempDir(), nil, nil).
|
||||
WithRuntimeLookup(func(id string) (string, error) {
|
||||
return "claude_code", nil
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{{Key: "id", Value: "ws-cc"}}
|
||||
c.Request = httptest.NewRequest("GET", "/workspaces/ws-cc/plugins", nil)
|
||||
h.ListInstalled(c)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
var plugins []pluginInfo
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &plugins))
|
||||
// With nil docker, container is not running → ListInstalled returns []
|
||||
// immediately after findRunningContainer, before runtime annotation runs.
|
||||
// The annotation only executes when at least one plugin is listed, so
|
||||
// this test proves the handler doesn't panic in that path and that the
|
||||
// no-container case short-circuits cleanly.
|
||||
require.Empty(t, plugins)
|
||||
}
|
||||
|
||||
// ── CheckRuntimeCompatibility ──────────────────────────────────────────────────
|
||||
|
||||
// TestCheckRuntimeCompatibility_ExecFailureReturns500 verifies that when
|
||||
// the container IS running but the plugin-ls exec fails (e.g. permission
|
||||
// error inside the container), the handler returns 500, not 200 or 5xx
|
||||
// with a wrong status code.
|
||||
func TestCheckRuntimeCompatibility_ExecFailureReturns500(t *testing.T) {
|
||||
h := NewPluginsHandler(t.TempDir(), nil, nil)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Params = gin.Params{{Key: "id", Value: "ws-exec-err"}}
|
||||
c.Request = httptest.NewRequest("GET",
|
||||
"/workspaces/ws-exec-err/plugins/compatibility?runtime=deepagents", nil)
|
||||
h.CheckRuntimeCompatibility(c)
|
||||
|
||||
// nil docker → RunningContainerName returns ErrNoBackend → container
|
||||
// name is "" → handler short-circuits to trivially-compatible 200.
|
||||
// This test documents that path; the real 500 requires a live docker
|
||||
// client whose ContainerInspect succeeds but exec fails — covered in
|
||||
// integration/E2E. Here we verify the no-docker safe path.
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
var body map[string]interface{}
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
require.Equal(t, true, body["all_compatible"])
|
||||
require.Equal(t, "deepagents", body["target_runtime"])
|
||||
}
|
||||
|
||||
// TestCheckRuntimeCompatibility_PluginCompatAndIncompatSeparated verifies that
|
||||
// when the compatibility check runs with a container present, compatible and
|
||||
// incompatible plugins are correctly separated and all_compatible reflects
|
||||
// the count. This tests the logic path inside the exec loop without needing
|
||||
// a live Docker client by using parseManifestYAML directly.
|
||||
func TestCheckRuntimeCompatibility_PluginCompatAndIncompatSeparated(t *testing.T) {
|
||||
// This is a pure-logic test of the separation: we verify that a pluginInfo
|
||||
// with Runtimes=[claude_code] is compatible with runtime=claude-code
|
||||
// and incompatible with runtime=deepagents, and the same for an
|
||||
// unspecified plugin (empty Runtimes = always compatible).
|
||||
pCC := pluginInfo{Name: "cc-only", Runtimes: []string{"claude_code"}}
|
||||
pUnspec := pluginInfo{Name: "legacy"}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
runtime string
|
||||
wantCompatible int
|
||||
wantIncompat int
|
||||
wantAllOk bool
|
||||
}{
|
||||
{"claude-code runtime: cc-only compatible, legacy always ok",
|
||||
"claude-code", 2, 0, true},
|
||||
{"deepagents runtime: cc-only incompatible, legacy always ok",
|
||||
"deepagents", 1, 1, false},
|
||||
{"langgraph runtime: cc-only incompatible, legacy always ok",
|
||||
"langgraph", 1, 1, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
plugins := []pluginInfo{pCC, pUnspec}
|
||||
compatible, incompatible := []pluginInfo{}, []pluginInfo{}
|
||||
for _, p := range plugins {
|
||||
if p.supportsRuntime(tc.runtime) {
|
||||
compatible = append(compatible, p)
|
||||
} else {
|
||||
incompatible = append(incompatible, p)
|
||||
}
|
||||
}
|
||||
require.Len(t, compatible, tc.wantCompatible, "compatible count")
|
||||
require.Len(t, incompatible, tc.wantIncompat, "incompatible count")
|
||||
require.Equal(t, tc.wantAllOk, len(incompatible) == 0, "all_compatible")
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// patchReq builds a gin context for a PATCH request to /workspaces/:id/abilities.
|
||||
func patchReq(id, body string) (*http.Request, *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 c.Request, w, c
|
||||
}
|
||||
|
||||
func TestPatchAbilities_InvalidWorkspaceID(t *testing.T) {
|
||||
setupTestDB(t)
|
||||
|
||||
// "not-a-uuid" fails validateWorkspaceID
|
||||
_, w, c := patchReq("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())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchAbilities_EmptyBody(t *testing.T) {
|
||||
setupTestDB(t)
|
||||
id := "00000000-0000-0000-0000-000000000001"
|
||||
|
||||
// Empty JSON object — no ability fields present
|
||||
_, w, c := patchReq(id, `{}`)
|
||||
PatchAbilities(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp map[string]string
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["error"] != "at least one ability field required" {
|
||||
t.Errorf("expected 'at least one ability field required', got %v", resp["error"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchAbilities_WorkspaceNotFound(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
id := "00000000-0000-0000-0000-000000000002"
|
||||
|
||||
// SELECT EXISTS returns false (workspace does not exist)
|
||||
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
|
||||
WithArgs(id).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(false))
|
||||
|
||||
_, w, c := patchReq(id, `{"broadcast_enabled":true}`)
|
||||
PatchAbilities(c)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("expected 404, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchAbilities_SetBroadcastEnabledTrue(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
id := "00000000-0000-0000-0000-000000000003"
|
||||
|
||||
// SELECT EXISTS → true
|
||||
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
|
||||
WithArgs(id).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
|
||||
|
||||
// UPDATE broadcast_enabled = true
|
||||
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
|
||||
WithArgs(id, true).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
_, w, c := patchReq(id, `{"broadcast_enabled":true}`)
|
||||
PatchAbilities(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp map[string]string
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["status"] != "updated" {
|
||||
t.Errorf("expected status=updated, got %v", resp["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchAbilities_SetTalkToUserEnabledFalse(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
id := "00000000-0000-0000-0000-000000000004"
|
||||
|
||||
// SELECT EXISTS → true
|
||||
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
|
||||
WithArgs(id).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
|
||||
|
||||
// UPDATE talk_to_user_enabled = false
|
||||
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
|
||||
WithArgs(id, false).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
_, w, c := patchReq(id, `{"talk_to_user_enabled":false}`)
|
||||
PatchAbilities(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchAbilities_BothFields(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
id := "00000000-0000-0000-0000-000000000005"
|
||||
|
||||
// SELECT EXISTS → true
|
||||
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
|
||||
WithArgs(id).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
|
||||
|
||||
// UPDATE broadcast_enabled = false
|
||||
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
|
||||
WithArgs(id, false).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
// UPDATE talk_to_user_enabled = true
|
||||
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
|
||||
WithArgs(id, true).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
_, w, c := patchReq(id, `{"broadcast_enabled":false,"talk_to_user_enabled":true}`)
|
||||
PatchAbilities(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchAbilities_BroadcastUpdateFails(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
id := "00000000-0000-0000-0000-000000000006"
|
||||
|
||||
// SELECT EXISTS → true
|
||||
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
|
||||
WithArgs(id).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
|
||||
|
||||
// UPDATE fails
|
||||
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
|
||||
WithArgs(id, true).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
_, w, c := patchReq(id, `{"broadcast_enabled":true}`)
|
||||
PatchAbilities(c)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchAbilities_TalkToUserUpdateFails(t *testing.T) {
|
||||
mock := setupTestDB(t)
|
||||
id := "00000000-0000-0000-0000-000000000007"
|
||||
|
||||
// SELECT EXISTS → true
|
||||
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
|
||||
WithArgs(id).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
|
||||
|
||||
// UPDATE broadcast_enabled skipped (not in payload)
|
||||
// UPDATE talk_to_user_enabled fails
|
||||
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
|
||||
WithArgs(id, false).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
_, w, c := patchReq(id, `{"talk_to_user_enabled":false}`)
|
||||
PatchAbilities(c)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user