58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// hyperguild#149: the ingestion REST API now requires a bearer token; the CLI
|
|
// sends BRAIN_MCP_TOKEN, or it gets 401 on every brain call.
|
|
func TestBrainClientSendsBrainMCPTokenOnQueryWriteAndPassRate(t *testing.T) {
|
|
var got []string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
got = append(got, r.URL.Path+" "+r.Header.Get("Authorization"))
|
|
switch r.URL.Path {
|
|
case "/write":
|
|
_, _ = w.Write([]byte(`{"path":"wiki/x/facts/y.md"}`))
|
|
return
|
|
case "/pass-rate":
|
|
_, _ = w.Write([]byte(`{}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"results":[]}`))
|
|
}))
|
|
defer srv.Close()
|
|
t.Setenv("BRAIN_URL", srv.URL)
|
|
t.Setenv("BRAIN_MCP_TOKEN", "cli-test-token")
|
|
|
|
c := newBrainClient()
|
|
_, err := c.Query(context.Background(), "x", 1)
|
|
require.NoError(t, err)
|
|
_, err = c.Write(context.Background(), "x", "facts", "y", strings.NewReader("# y"))
|
|
require.NoError(t, err)
|
|
// PR #152 review: PassRate sent no token and would 401 in production.
|
|
_, _ = c.PassRate(context.Background(), "tdd", "7d")
|
|
assert.Equal(t, []string{"/query Bearer cli-test-token", "/write Bearer cli-test-token",
|
|
"/pass-rate Bearer cli-test-token"}, got)
|
|
}
|
|
|
|
func TestBrainClientSendsNoAuthorizationWhenTokenUnset(t *testing.T) {
|
|
var auth string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
auth = r.Header.Get("Authorization")
|
|
_, _ = w.Write([]byte(`{"results":[]}`))
|
|
}))
|
|
defer srv.Close()
|
|
t.Setenv("BRAIN_URL", srv.URL)
|
|
t.Setenv("BRAIN_MCP_TOKEN", "")
|
|
_, err := newBrainClient().Query(context.Background(), "x", 1)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, auth)
|
|
}
|