Files
openserp/cmd/search_test.go
Rustem Kamalov 9cc69da758 feat(cli): clean search, add extract/format flags; harden engines & proxy rotation; fix bugs; update docs
- Add structured `search [engine] [query]` CLI: --limit/--lang/--region/--site/--file, --format (json|text|markdown|ndjson), --extract N, --search-timeout; Envelope and route logs to stderr with a --quiet default (fixes stdout pollution)
- Unify engines behind a single engineSpec registry (CLI + serve share it)
- Unify the extract knob to bool-or-int `extract=N` (drop extract_top); CLI and HTTP share core batch extraction, raw/rendered fetch, and clamp helpers
- Engines: Ecosia CF captcha detection (raw + browser), Yandex progressive-result wait, Google PAA poll + Has() existence probes, Bing title/desc attribute fallbacks
- Proxy: rotate challenged proxies out of the tag pool for one retry (X-Proxy-Attempts); browser health-ping skip window; opt-in WaitStable
2026-06-16 03:51:37 +03:00

83 lines
2.0 KiB
Go

package cmd
import (
"strings"
"testing"
"github.com/karust/openserp/core"
extractpkg "github.com/karust/openserp/extract"
)
func TestNormalizeCLIExtractTop(t *testing.T) {
tests := []struct {
name string
raw int
want int
wantErr bool
}{
{name: "disabled", raw: 0, want: 0},
{name: "one", raw: 1, want: 1},
{name: "clamped", raw: 20, want: maxCLIExtractTop},
{name: "negative", raw: -1, wantErr: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := normalizeCLIExtractTop(tc.raw)
if tc.wantErr {
if err == nil {
t.Fatal("expected error")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tc.want {
t.Fatalf("normalizeCLIExtractTop(%d) = %d, want %d", tc.raw, got, tc.want)
}
})
}
}
func TestApplyCLIExtractFlagSetsAutoMode(t *testing.T) {
previous := config
config.Extract = extractpkg.DefaultConfig()
defer func() { config = previous }()
query := core.Query{Text: "weather today"}
if err := applyCLIExtractFlag(&query, 2); err != nil {
t.Fatalf("applyCLIExtractFlag() error = %v", err)
}
if !query.Extract {
t.Fatal("expected query.Extract")
}
if query.ExtractTop != 2 {
t.Fatalf("ExtractTop = %d, want 2", query.ExtractTop)
}
if query.ExtractMode != string(extractpkg.ModeAuto) {
t.Fatalf("ExtractMode = %q, want auto", query.ExtractMode)
}
}
func TestApplyCLIExtractFlagRequiresEnabledConfig(t *testing.T) {
previous := config
config.Extract = extractpkg.Config{Enabled: false}
defer func() { config = previous }()
query := core.Query{Text: "weather today"}
err := applyCLIExtractFlag(&query, 1)
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "disabled") {
t.Fatalf("error = %q, want disabled message", err.Error())
}
}
func TestSearchCommandHasExtractFlag(t *testing.T) {
if searchCMD.Flags().Lookup("extract") == nil {
t.Fatal("expected search command to expose --extract")
}
}