Files
kandev/apps/backend/internal/slack/handlers_test.go
T
João Salavisa 7e0b46c227 feat(slack): cookie-mode integration that triages threads via a utility agent (#775)
* feat(slack): add cookie-mode integration that triages messages via utility agent

Adds an install-wide Slack integration that polls the connected user's
own messages for `!kandev <instruction>`, hands the thread context to a
configured utility agent with the Kandev MCP server attached, and
creates the resulting task. Auth uses the user's xoxc- token + d cookie
so it works on locked-down Enterprise Slack tenants where bot installs
aren't possible.

Shared infra:
- ACP host-utility executor accepts McpServers on session/new
- New MCP tool list_repositories_kandev so the triage agent can pick the
  right repo
- Template engine exposes Slack-specific variables for prompt injection
- secretadapter + healthpoll reused from jira/linear

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(slack): address PR review feedback round 9

- Drop dead `var _ = time.Second` no-op and its misleading comment in
  trigger_test.go.
- `RecordAuthHealth` now invalidates the cached client when the probe
  fails, so the poller self-recovers from credential rotation without
  requiring a re-save.
- Stable secondary sort key (`workspace_id DESC`) on the legacy →
  singleton migration so promoted-row selection is deterministic on
  `updated_at` ties.
- Extract `categorySlack` constant in `engine.go` and update the
  `VariableInfo.Category` doc-comment to list the full set of values.
- Realign `slack-integration/spec.md` with what shipped: cookie-mode +
  `!kandev` triage as v1, with bot install / User OAuth / reaction &
  slash & shortcut triggers moved under Future scope.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-05 09:03:42 +01:00

55 lines
2.0 KiB
Go

package slack
import (
"encoding/json"
"errors"
"testing"
ws "github.com/kandev/kandev/pkg/websocket"
)
// TestWsFail_MapsAPIErrorCodes locks the wire-level error codes wsFail
// produces for each input variant. wsFail mirrors writeClientError's HTTP
// mapping so WS clients see the same distinct errors (bad-request,
// unauthorized, forbidden, not-found, internal) instead of the previous
// "everything → INTERNAL_ERROR" collapse, and a regression here would put
// the WS path silently out of sync with the HTTP path again.
func TestWsFail_MapsAPIErrorCodes(t *testing.T) {
msg := &ws.Message{ID: "1", Action: ws.ActionSlackConfigTest}
cases := []struct {
name string
err error
wantCode string
}{
{"ErrNotConfigured → SLACK_NOT_CONFIGURED", ErrNotConfigured, errCodeSlackNotConfigured},
{"APIError 400 → BAD_REQUEST", &APIError{StatusCode: 400, Message: "bad"}, ws.ErrorCodeBadRequest},
{"APIError 401 → UNAUTHORIZED", &APIError{StatusCode: 401, Message: "unauth"}, ws.ErrorCodeUnauthorized},
{"APIError 403 → FORBIDDEN", &APIError{StatusCode: 403, Message: "forbidden"}, ws.ErrorCodeForbidden},
{"APIError 404 → NOT_FOUND", &APIError{StatusCode: 404, Message: "notfound"}, ws.ErrorCodeNotFound},
{"APIError 500 → INTERNAL_ERROR", &APIError{StatusCode: 500, Message: "server"}, ws.ErrorCodeInternalError},
{"plain error → INTERNAL_ERROR", errors.New("transient"), ws.ErrorCodeInternalError},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
resp, err := wsFail(msg, c.err)
if err != nil {
t.Fatalf("wsFail returned error: %v", err)
}
if resp == nil {
t.Fatal("wsFail returned nil response")
}
var payload ws.ErrorPayload
if jsonErr := json.Unmarshal(resp.Payload, &payload); jsonErr != nil {
t.Fatalf("decode payload: %v (raw=%s)", jsonErr, string(resp.Payload))
}
if payload.Code != c.wantCode {
t.Errorf("code mismatch: got %q want %q", payload.Code, c.wantCode)
}
if payload.Message == "" {
t.Error("expected non-empty message in error payload")
}
})
}
}