package cmd import ( "bytes" "encoding/json" "io" "net/http" "net/http/httptest" "reflect" "testing" ) func TestSearchCmd_SendsDocTypeAndTagsList(t *testing.T) { var captured map[string]interface{} server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/v1/search" { t.Errorf("unexpected path: %s", r.URL.Path) } body, _ := io.ReadAll(r.Body) if err := json.Unmarshal(body, &captured); err != nil { t.Fatalf("failed to decode request body: %v", err) } w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"query":"q","results":[],"total_matches":0,"returned":0}`)) })) defer server.Close() var stdout bytes.Buffer rootCmd.SetOut(&stdout) rootCmd.SetArgs([]string{ "search", "oil level", "--engine", server.URL, "--format", "json", "--type", "pdf", "--tags", "manuals, ops,", }) if err := rootCmd.Execute(); err != nil { t.Fatalf("search command failed: %v", err) } if captured == nil { t.Fatal("no request captured by test server") } if got := captured["doc_type"]; got != "pdf" { t.Errorf("expected doc_type=pdf in body, got %v (full body: %v)", got, captured) } if _, present := captured["type"]; present { t.Error("body must not contain legacy 'type' key") } tags, ok := captured["tags"].([]interface{}) if !ok { t.Fatalf("expected tags to be a JSON array, got %T (%v)", captured["tags"], captured["tags"]) } want := []interface{}{"manuals", "ops"} if !reflect.DeepEqual(tags, want) { t.Errorf("expected tags %v, got %v", want, tags) } } func TestSplitTags(t *testing.T) { got := splitTags(" a ,b,, c ") want := []string{"a", "b", "c"} if !reflect.DeepEqual(got, want) { t.Errorf("splitTags: expected %v, got %v", want, got) } }