43 lines
1.5 KiB
Go
43 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The assistant is on unless the user turned it off - for the web search
|
|
// and page reading as for the chat (a user who never touched the switch
|
|
// has no setting at all).
|
|
func TestAISearchAndFetchOnByDefault(t *testing.T) {
|
|
s, c := newTestServer(t)
|
|
sx := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"results":[{"title":"Intel","url":"https://example.com/intel","content":"процессоры"}]}`)
|
|
}))
|
|
defer sx.Close()
|
|
_ = s.db.SetConfig(cfgSearchEngine, "searxng")
|
|
_ = s.db.SetConfig(cfgSearchURL, sx.URL)
|
|
post := func(path, body string) string {
|
|
req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
|
|
req.AddCookie(c)
|
|
w := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(w, req)
|
|
return w.Body.String()
|
|
}
|
|
if got := post("/api/panel/ai/search", `{"q":"intel"}`); !strings.Contains(got, `"ok":true`) {
|
|
t.Fatalf("search by default: %s", got)
|
|
}
|
|
if got := post("/api/panel/ai/fetch", `{"url":"не адрес"}`); strings.Contains(got, "выключен") {
|
|
t.Fatalf("fetch by default: %s", got)
|
|
}
|
|
a, _ := s.db.AdminByUsername("adm")
|
|
_ = s.db.SetConfig(aiUserKey(a.ID), "0")
|
|
for _, p := range []string{"/api/panel/ai/search", "/api/panel/ai/fetch"} {
|
|
if got := post(p, `{"q":"intel","url":"https://example.com"}`); !strings.Contains(got, "выключен") {
|
|
t.Fatalf("%s with the assistant off: %s", p, got)
|
|
}
|
|
}
|
|
}
|