Files
hyperguild/internal/skills/brain/handlers_test.go
Mathias Bergqvist a67106026f
Some checks failed
CI / Lint / Test / Vet (push) Failing after 3s
CI / Mirror to GitHub (push) Has been skipped
fix(lint): satisfy errcheck for io.Copy, json.Encode, Body.Close, deferred Close
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 07:15:35 +02:00

66 lines
2.1 KiB
Go

// internal/skills/brain/handlers_test.go
package brain_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/mathiasbq/supervisor/internal/skills/brain"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHandle_BrainQuery_CallsIngestServer(t *testing.T) {
called := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/query", r.URL.Path)
called = true
_ = json.NewEncoder(w).Encode(map[string]any{
"results": []map[string]any{
{"path": "wiki/concepts/tdd.md", "title": "TDD", "excerpt": "Test-driven development.", "score": 3},
},
})
}))
defer srv.Close()
s := brain.New(brain.Config{IngestBaseURL: srv.URL})
args, _ := json.Marshal(map[string]string{"query": "test driven development"})
out, err := s.Handle(context.Background(), "brain_query", args)
require.NoError(t, err)
assert.True(t, called)
var result map[string]any
require.NoError(t, json.Unmarshal(out, &result))
results := result["results"].([]any)
assert.Len(t, results, 1)
}
func TestHandle_BrainWrite_CallsIngestServer(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/write", r.URL.Path)
var body map[string]string
require.NoError(t, json.NewDecoder(r.Body).Decode(&body))
assert.Equal(t, "concept", body["type"])
assert.Equal(t, "# Test\n\nSome learning.", body["content"])
json.NewEncoder(w).Encode(map[string]string{"path": "raw/test.md"})
}))
defer srv.Close()
s := brain.New(brain.Config{IngestBaseURL: srv.URL})
args, _ := json.Marshal(map[string]string{"content": "# Test\n\nSome learning.", "type": "concept"})
out, err := s.Handle(context.Background(), "brain_write", args)
require.NoError(t, err)
var result map[string]string
require.NoError(t, json.Unmarshal(out, &result))
assert.Equal(t, "raw/test.md", result["path"])
}
func TestHandle_UnknownTool_ReturnsError(t *testing.T) {
s := brain.New(brain.Config{IngestBaseURL: "http://localhost:3300"})
_, err := s.Handle(context.Background(), "brain_unknown", nil)
assert.Error(t, err)
}