Files
wehub-resource-sync f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

97 lines
3.1 KiB
Go

package server_test
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.kenn.io/agentsview/internal/db"
)
type trashHandlerResponse struct {
Sessions []db.Session `json:"sessions"`
}
type emptyTrashHandlerResponse struct {
Deleted int `json:"deleted"`
}
func TestSessionManagementRenameHandler(t *testing.T) {
te := setup(t)
te.seedSession(t, "s1", "alpha", 2)
w := te.patch(t, "/api/v1/sessions/s1/rename", `{"display_name":"Pinned investigation"}`)
require.Equal(t, http.StatusOK, w.Code, "body: %s", w.Body.String())
renamed := decode[db.Session](t, w)
require.NotNil(t, renamed.DisplayName)
assert.Equal(t, "Pinned investigation", *renamed.DisplayName)
w = te.patch(t, "/api/v1/sessions/s1/rename", `{"display_name":""}`)
require.Equal(t, http.StatusOK, w.Code, "body: %s", w.Body.String())
cleared := decode[db.Session](t, w)
assert.Nil(t, cleared.DisplayName)
w = te.patch(t, "/api/v1/sessions/missing/rename", `{"display_name":"Nope"}`)
require.Equal(t, http.StatusNotFound, w.Code, "body: %s", w.Body.String())
assertErrorResponse(t, w, "session not found")
}
func TestSessionManagementTrashRestoreAndPermanentDeleteHandlers(t *testing.T) {
te := setup(t)
te.seedSession(t, "s1", "alpha", 2)
w := te.del(t, "/api/v1/sessions/s1")
require.Equal(t, http.StatusNoContent, w.Code, "body: %s", w.Body.String())
w = te.get(t, "/api/v1/trash")
require.Equal(t, http.StatusOK, w.Code, "body: %s", w.Body.String())
trash := decode[trashHandlerResponse](t, w)
require.Len(t, trash.Sessions, 1)
assert.Equal(t, "s1", trash.Sessions[0].ID)
w = te.post(t, "/api/v1/sessions/s1/restore", `{}`)
require.Equal(t, http.StatusNoContent, w.Code, "body: %s", w.Body.String())
w = te.post(t, "/api/v1/sessions/s1/restore", `{}`)
require.Equal(t, http.StatusNotFound, w.Code, "body: %s", w.Body.String())
assertErrorResponse(t, w, "session not found or not in trash")
w = te.del(t, "/api/v1/sessions/s1/permanent")
require.Equal(t, http.StatusConflict, w.Code, "body: %s", w.Body.String())
assertErrorResponse(t, w, "session not found or not in trash")
w = te.del(t, "/api/v1/sessions/s1")
require.Equal(t, http.StatusNoContent, w.Code, "body: %s", w.Body.String())
w = te.del(t, "/api/v1/sessions/s1/permanent")
require.Equal(t, http.StatusNoContent, w.Code, "body: %s", w.Body.String())
got, err := te.db.GetSessionFull(context.Background(), "s1")
require.NoError(t, err)
assert.Nil(t, got)
}
func TestSessionManagementEmptyTrashHandler(t *testing.T) {
te := setup(t)
te.seedSession(t, "s1", "alpha", 2)
te.seedSession(t, "s2", "beta", 2)
for _, id := range []string{"s1", "s2"} {
w := te.del(t, "/api/v1/sessions/"+id)
require.Equal(t, http.StatusNoContent, w.Code, "delete %s body: %s", id, w.Body.String())
}
w := te.del(t, "/api/v1/trash")
require.Equal(t, http.StatusOK, w.Code, "body: %s", w.Body.String())
resp := decode[emptyTrashHandlerResponse](t, w)
assert.Equal(t, 2, resp.Deleted)
w = te.get(t, "/api/v1/trash")
require.Equal(t, http.StatusOK, w.Code, "body: %s", w.Body.String())
trash := decode[trashHandlerResponse](t, w)
assert.Empty(t, trash.Sessions)
}