fix(hyperguild): brain Query uses POST /query with JSON body
The brain HTTP REST /query endpoint accepts POST with JSON
{query, limit}, not GET with URL query string. Surfaced by
Task 7 smoke testing — GET returned 405 Method Not Allowed.
The response shape ({results:[...]}) is unchanged; only the
request side flips to POST + JSON body. brainClient.Write was
already using POST + JSON body and is unaffected.
Tests updated to assert POST + JSON body on the Query path.
This commit is contained in:
@@ -49,9 +49,15 @@ func TestRunBrainQuery_JSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunBrainQuery_Limit(t *testing.T) {
|
func TestRunBrainQuery_Limit(t *testing.T) {
|
||||||
gotLimit := ""
|
gotLimit := -1
|
||||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
gotLimit = r.URL.Query().Get("limit")
|
body, _ := io.ReadAll(r.Body)
|
||||||
|
var p struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
}
|
||||||
|
_ = json.Unmarshal(body, &p)
|
||||||
|
gotLimit = p.Limit
|
||||||
_, _ = w.Write([]byte(`{"results":[]}`))
|
_, _ = w.Write([]byte(`{"results":[]}`))
|
||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
@@ -60,7 +66,7 @@ func TestRunBrainQuery_Limit(t *testing.T) {
|
|||||||
var out, errBuf bytes.Buffer
|
var out, errBuf bytes.Buffer
|
||||||
err := runBrain(context.Background(), []string{"query", "--limit", "12", "topic"}, strings.NewReader(""), &out, &errBuf)
|
err := runBrain(context.Background(), []string{"query", "--limit", "12", "topic"}, strings.NewReader(""), &out, &errBuf)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "12", gotLimit)
|
assert.Equal(t, 12, gotLimit)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunBrainQuery_MissingTopic(t *testing.T) {
|
func TestRunBrainQuery_MissingTopic(t *testing.T) {
|
||||||
|
|||||||
@@ -7,9 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,23 +47,29 @@ type QueryResult struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *brainClient) Query(ctx context.Context, topic string, limit int) (*QueryResult, error) {
|
func (c *brainClient) Query(ctx context.Context, topic string, limit int) (*QueryResult, error) {
|
||||||
q := url.Values{}
|
payload, err := json.Marshal(struct {
|
||||||
q.Set("q", topic)
|
Query string `json:"query"`
|
||||||
q.Set("limit", strconv.Itoa(limit))
|
Limit int `json:"limit"`
|
||||||
u := c.baseURL + "/query?" + q.Encode()
|
}{Query: topic, Limit: limit})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal payload: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
u := c.baseURL + "/query"
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, bytes.NewReader(payload))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("build request: %w", err)
|
return nil, fmt.Errorf("build request: %w", err)
|
||||||
}
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
resp, err := c.http.Do(req)
|
resp, err := c.http.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("brain GET /query: %w", err)
|
return nil, fmt.Errorf("brain POST /query: %w", err)
|
||||||
}
|
}
|
||||||
defer func() { _ = resp.Body.Close() }()
|
defer func() { _ = resp.Body.Close() }()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
return nil, fmt.Errorf("brain GET /query: status %d: %s", resp.StatusCode, string(body))
|
return nil, fmt.Errorf("brain POST /query: status %d: %s", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
var out QueryResult
|
var out QueryResult
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
||||||
|
|||||||
@@ -15,9 +15,18 @@ import (
|
|||||||
|
|
||||||
func TestBrainClient_Query_Success(t *testing.T) {
|
func TestBrainClient_Query_Success(t *testing.T) {
|
||||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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)
|
assert.Equal(t, "/query", r.URL.Path)
|
||||||
assert.Equal(t, "find-h", r.URL.Query().Get("q"))
|
|
||||||
assert.Equal(t, "3", r.URL.Query().Get("limit"))
|
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.Header().Set("Content-Type", "application/json")
|
||||||
_, _ = w.Write([]byte(`{"results":[{"path":"knowledge/x.md","title":"x","excerpt":"...","score":7}]}`))
|
_, _ = w.Write([]byte(`{"results":[{"path":"knowledge/x.md","title":"x","excerpt":"...","score":7}]}`))
|
||||||
}))
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user