A model's web search, when no provider can search, goes to the search APIs the user added; each of them needed a key except a SearXNG of the user's own. You.com answers with no key too, so it is one a user can add in one step. - provider: SearchVendor has KeyOptional (You.com, SearXNG); Ready and check let a vendor that has it go with no key. You.com is listed between Firecrawl and SearXNG, its keys at you.com/platform. - gateway: with no key, GET https://api.you.com/v1/agents/search?query= &count=6 (YouFreeSearch); with a key, POST https://ydc-index.io/v1/search {"query","count"} with X-API-Key, as its API reference says; with a base URL of the user's own and no key, GET <url>/v1/agents/search. Both replies are read from results.web then results.news: the snippets, else the description, and page_age. You.com's errors are a code beside a message, so the message is what the user is told (a 402 "prepaid credit balance has been depleted" is a refused key, the next key or API is asked). Its requests carry the User-Agent tag You.com asked for, "youdotcom-integration/yetone-magpie", after magpie's own. - gui: Settings › Web search lets You.com be added with the key empty, its placeholder saying so (zh, zh-TW, ja, de). - cli: magpie search add you [<key>]; the help lists it. New network addresses: api.you.com (keyless) and ydc-index.io (keyed), asked only when the user adds You.com, with the search query only. Checked against the real service on 10-11: a keyless GET to api.you.com/v1/agents/search answered 200 (testdata/you/free_search.json is that reply, cut to 3 hits), and a real run through askSearchAPIKeys returned 6 pages. ydc-index.io/v1/search answered 402 with no key (testdata/you/keyed_402.json, real bytes) and 403 to a made-up key. The keyless endpoint isn't in the API reference; You.com's own docs pages for third-party tools describe it (free, 100 a day per address, no account), as LiteLLM, ragflow and AnythingLLM use it. you.com/terms bars automated use of its website and apps, not the API. The keyed request shape is from its API reference only: no key with credit was tried. Run: go test -tags nogui . ./internal/gui/ ./internal/provider/ ./internal/backup/ green; ./internal/gateway/ green but TestTurnDoneSaidAtTheTurnsEnd (#1629, fixed in its own commit); search-api.test.cjs in Chromium and WebKit, en/zh, 980/420px green; gui-ja, gui-de and i18n-keys green; gofmt -l; go vet -tags nogui for linux, windows and darwin. The tests make no network request (a stub RoundTripper). Without the change, TestYouSearch, TestYouKeyOutOfCredit, TestYouNeedsNoKey and TestSearchCLIYouWithoutKey fail, and with the GUI's keyOptional check taken out search-api.test.cjs fails in all four Chromium runs (Add sends nothing).
122 lines
4.0 KiB
Go
122 lines
4.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// youAsked is one request a fake You.com was asked.
|
|
type youAsked struct {
|
|
method, url, key, agent, body string
|
|
}
|
|
|
|
// youStub answers every request as You.com did (testdata/you), with no
|
|
// network: free_search.json is a real answer of its free search, the
|
|
// keyed Search API's answer having the same shape (its API reference);
|
|
// keyed_402.json is the keyed API's real answer to a key with no credit.
|
|
type youStub struct {
|
|
mu sync.Mutex
|
|
asked []youAsked
|
|
code int
|
|
file string
|
|
}
|
|
|
|
func (y *youStub) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
var body string
|
|
if r.Body != nil {
|
|
b, _ := io.ReadAll(r.Body)
|
|
body = string(b)
|
|
}
|
|
y.mu.Lock()
|
|
y.asked = append(y.asked, youAsked{r.Method, r.URL.String(), r.Header.Get("X-API-Key"), r.Header.Get("User-Agent"), body})
|
|
y.mu.Unlock()
|
|
b, err := os.ReadFile("testdata/you/" + y.file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &http.Response{StatusCode: y.code, Status: http.StatusText(y.code), Header: http.Header{"Content-Type": {"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(string(b))), Request: r}, nil
|
|
}
|
|
|
|
func youServer(t *testing.T, code int, file string) (*Server, *youStub) {
|
|
t.Helper()
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
y := &youStub{code: code, file: file}
|
|
s := New()
|
|
s.client = &http.Client{Transport: y}
|
|
return s, y
|
|
}
|
|
|
|
// #1626: You.com is asked with no key at its free search, and with a key
|
|
// at its Search API, its key in X-API-Key; both read into the pages found.
|
|
func TestYouSearch(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
api provider.SearchAPI
|
|
method string
|
|
url string
|
|
key string
|
|
}{
|
|
{"free", provider.SearchAPI{Vendor: "you"}, "GET", "https://api.you.com/v1/agents/search?count=6&query=go+context", ""},
|
|
{"keyed", provider.SearchAPI{Vendor: "you", Key: "ydc-k"}, "POST", "https://ydc-index.io/v1/search", "ydc-k"},
|
|
{"own address", provider.SearchAPI{Vendor: "you", URL: "https://you.example.com/"}, "GET", "https://you.example.com/v1/agents/search?count=6&query=go+context", ""},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
s, y := youServer(t, 200, "free_search.json")
|
|
pages, err := s.askSearchAPI(context.Background(), c.api, "go context")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(y.asked) != 1 {
|
|
t.Fatalf("asked %+v", y.asked)
|
|
}
|
|
a := y.asked[0]
|
|
if a.method != c.method || a.url != c.url || a.key != c.key || !strings.HasPrefix(a.agent, SearchAgent+" ") {
|
|
t.Fatalf("asked %+v", a)
|
|
}
|
|
if c.method == "POST" && a.body != `{"count":6,"query":"go context"}` {
|
|
t.Fatalf("body %s", a.body)
|
|
}
|
|
if len(pages) != 3 || pages[0].URL != "https://golangbot.com/context-timeout-cancellation/" ||
|
|
pages[0].Title != "Golang Context - Cancellation, Timeout and Propagation | golangbot.com" ||
|
|
!strings.HasPrefix(pages[0].Text, "Context is a package in the standard library") ||
|
|
pages[0].Age != "2026-07-08T10:28:15" || pages[2].URL == "" || pages[2].Text == "" {
|
|
t.Fatalf("pages %+v", pages)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A You.com key with no credit left is a refusal of the key: the next
|
|
// key, or the next search API, is asked.
|
|
func TestYouKeyOutOfCredit(t *testing.T) {
|
|
s, _ := youServer(t, 402, "keyed_402.json")
|
|
_, err := s.askSearchAPI(context.Background(), provider.SearchAPI{Vendor: "you", Key: "ydc-k"}, "q")
|
|
var r searchRefused
|
|
if !errors.As(err, &r) || r.status != 402 || !strings.Contains(err.Error(), "prepaid credit balance has been depleted") {
|
|
t.Fatalf("%v", err)
|
|
}
|
|
}
|
|
|
|
// You.com, like SearXNG, can be added with no key; the others still can't.
|
|
func TestYouNeedsNoKey(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "you"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if as := provider.SearchAPIs(); len(as) != 1 || as[0].Vendor != "you" || !as[0].Ready() {
|
|
t.Fatalf("%+v", as)
|
|
}
|
|
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "tavily"}); err == nil {
|
|
t.Fatal("Tavily added with no key")
|
|
}
|
|
}
|