Adds 'hyperguild brain pass-rate <skill> [--window 7d] [--json]' calling the new /pass-rate endpoint. Human output: tdd: 47 / 50 = 94% (window: 7d) or 'no data (window: 7d)' when pass_rate is null. PassRateResult mirrors the response envelope; PassRate is *float64 so null is preserved across decode.
132 lines
4.3 KiB
Go
132 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBrainClient_Query_Success(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
assert.Equal(t, "/query", r.URL.Path)
|
|
|
|
body, _ := io.ReadAll(r.Body)
|
|
var got struct {
|
|
Query string `json:"query"`
|
|
Limit int `json:"limit"`
|
|
}
|
|
require.NoError(t, json.Unmarshal(body, &got))
|
|
assert.Equal(t, "find-h", got.Query)
|
|
assert.Equal(t, 3, got.Limit)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"results":[{"path":"knowledge/x.md","title":"x","excerpt":"...","score":7}]}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := &brainClient{baseURL: srv.URL, http: srv.Client()}
|
|
res, err := c.Query(context.Background(), "find-h", 3)
|
|
require.NoError(t, err)
|
|
require.Len(t, res.Results, 1)
|
|
assert.Equal(t, "knowledge/x.md", res.Results[0].Path)
|
|
assert.Equal(t, 7, res.Results[0].Score)
|
|
}
|
|
|
|
func TestBrainClient_Query_TransportError(t *testing.T) {
|
|
c := &brainClient{baseURL: "http://127.0.0.1:1", http: http.DefaultClient}
|
|
_, err := c.Query(context.Background(), "x", 5)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestBrainClient_Query_Non200(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
_, _ = w.Write([]byte("boom"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := &brainClient{baseURL: srv.URL, http: srv.Client()}
|
|
_, err := c.Query(context.Background(), "x", 5)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "500")
|
|
}
|
|
|
|
func TestBrainClient_Write_Success(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, "/write", r.URL.Path)
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
body, _ := io.ReadAll(r.Body)
|
|
var got struct {
|
|
Type string `json:"type"`
|
|
Slug string `json:"slug"`
|
|
Content string `json:"content"`
|
|
}
|
|
require.NoError(t, json.Unmarshal(body, &got))
|
|
assert.Equal(t, "knowledge", got.Type)
|
|
assert.Equal(t, "find-h", got.Slug)
|
|
assert.Equal(t, "# body\n", got.Content)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"path":"knowledge/find-h.md"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := &brainClient{baseURL: srv.URL, http: srv.Client()}
|
|
res, err := c.Write(context.Background(), "knowledge", "find-h", strings.NewReader("# body\n"))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "knowledge/find-h.md", res.Path)
|
|
}
|
|
|
|
func TestBrainClient_PassRate_Success(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, http.MethodGet, r.Method)
|
|
assert.Equal(t, "/pass-rate", r.URL.Path)
|
|
assert.Equal(t, "tdd", r.URL.Query().Get("skill"))
|
|
assert.Equal(t, "7d", r.URL.Query().Get("window"))
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"skill":"tdd","window":"7d","pass":47,"fail":3,"skip":0,"total":50,"pass_rate":0.94}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := &brainClient{baseURL: srv.URL, http: srv.Client()}
|
|
res, err := c.PassRate(context.Background(), "tdd", "7d")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "tdd", res.Skill)
|
|
assert.Equal(t, 47, res.Pass)
|
|
assert.Equal(t, 3, res.Fail)
|
|
require.NotNil(t, res.PassRate)
|
|
assert.InDelta(t, 0.94, *res.PassRate, 0.001)
|
|
}
|
|
|
|
func TestBrainClient_PassRate_NullRate(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"skill":"tdd","window":"7d","pass":0,"fail":0,"skip":0,"total":0,"pass_rate":null}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := &brainClient{baseURL: srv.URL, http: srv.Client()}
|
|
res, err := c.PassRate(context.Background(), "tdd", "7d")
|
|
require.NoError(t, err)
|
|
assert.Nil(t, res.PassRate)
|
|
}
|
|
|
|
func TestNewBrainClient_DefaultURL(t *testing.T) {
|
|
t.Setenv("BRAIN_URL", "")
|
|
c := newBrainClient()
|
|
assert.Equal(t, "http://koala:30330", c.baseURL)
|
|
}
|
|
|
|
func TestNewBrainClient_OverrideURL(t *testing.T) {
|
|
t.Setenv("BRAIN_URL", "http://localhost:9999")
|
|
c := newBrainClient()
|
|
assert.Equal(t, "http://localhost:9999", c.baseURL)
|
|
}
|