fix(ingestion): validate required query field in Query handler
Empty or whitespace-only queries would silently pass through to search, returning meaningless results. Also removed the Domain field from queryRequest — it was accepted but silently ignored since search.Query has no domain parameter, which would confuse callers. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mathiasbq/hyperguild/ingestion/internal/search"
|
"github.com/mathiasbq/hyperguild/ingestion/internal/search"
|
||||||
@@ -25,9 +26,8 @@ func NewHandler(brainDir string, logger *slog.Logger) *Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type queryRequest struct {
|
type queryRequest struct {
|
||||||
Query string `json:"query"`
|
Query string `json:"query"`
|
||||||
Domain string `json:"domain,omitempty"`
|
Limit int `json:"limit,omitempty"`
|
||||||
Limit int `json:"limit,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type writeRequest struct {
|
type writeRequest struct {
|
||||||
@@ -42,6 +42,10 @@ func (h *Handler) Query(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "invalid JSON", http.StatusBadRequest)
|
http.Error(w, "invalid JSON", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if strings.TrimSpace(req.Query) == "" {
|
||||||
|
http.Error(w, "query is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
if req.Limit == 0 {
|
if req.Limit == 0 {
|
||||||
req.Limit = 5
|
req.Limit = 5
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,17 @@ func TestWrite_CreatesRawFile(t *testing.T) {
|
|||||||
assert.Contains(t, string(content), "Some content.")
|
assert.Contains(t, string(content), "Some content.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQuery_RequiresQuery(t *testing.T) {
|
||||||
|
_, h := setup(t)
|
||||||
|
body, _ := json.Marshal(map[string]any{"limit": 5})
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/query", bytes.NewReader(body))
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
|
h.Query(rec, req)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusBadRequest, rec.Code)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWrite_GeneratesFilenameIfAbsent(t *testing.T) {
|
func TestWrite_GeneratesFilenameIfAbsent(t *testing.T) {
|
||||||
dir, h := setup(t)
|
dir, h := setup(t)
|
||||||
body, _ := json.Marshal(map[string]any{"content": "auto name"})
|
body, _ := json.Marshal(map[string]any{"content": "auto name"})
|
||||||
|
|||||||
Reference in New Issue
Block a user