feat(brain): add Query client for skill handler context injection
This commit is contained in:
67
internal/brain/client_test.go
Normal file
67
internal/brain/client_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package brain_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/mathiasbq/supervisor/internal/brain"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestQueryEmptyBaseURL(t *testing.T) {
|
||||
result, err := brain.Query(context.Background(), "", "tdd patterns", 3)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestQueryEmptyQuery(t *testing.T) {
|
||||
result, err := brain.Query(context.Background(), "http://localhost:9999", "", 3)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestQueryFormatsResults(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/query", r.URL.Path)
|
||||
var req map[string]any
|
||||
require.NoError(t, json.NewDecoder(r.Body).Decode(&req))
|
||||
assert.Equal(t, "tdd patterns", req["query"])
|
||||
|
||||
json.NewEncoder(w).Encode(map[string]any{ //nolint:errcheck
|
||||
"results": []map[string]any{
|
||||
{"path": "knowledge/tdd.md", "title": "TDD Guide", "excerpt": "Always write tests first.", "score": 5},
|
||||
{"path": "knowledge/go.md", "title": "Go Conventions", "excerpt": "Use table-driven tests.", "score": 3},
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
result, err := brain.Query(context.Background(), srv.URL, "tdd patterns", 3)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, result, "## Relevant knowledge")
|
||||
assert.Contains(t, result, "TDD Guide")
|
||||
assert.Contains(t, result, "Always write tests first.")
|
||||
assert.Contains(t, result, "Go Conventions")
|
||||
}
|
||||
|
||||
func TestQueryEmptyResults(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(map[string]any{"results": []any{}}) //nolint:errcheck
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
result, err := brain.Query(context.Background(), srv.URL, "obscure query", 3)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestQueryUnavailableServerReturnsEmpty(t *testing.T) {
|
||||
// Brain unavailable — should degrade gracefully, no error
|
||||
result, err := brain.Query(context.Background(), "http://127.0.0.1:19999", "query", 3)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
Reference in New Issue
Block a user