All checks were successful
release-tag / release-image (push) Successful in 2m30s
71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package engine
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/local/glpi-neural-brain/internal/activity"
|
|
"github.com/local/glpi-neural-brain/internal/config"
|
|
"github.com/local/glpi-neural-brain/internal/research"
|
|
)
|
|
|
|
func TestResearchDiagnosticPublishesVisibleEvents(t *testing.T) {
|
|
searx := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"results":[{"title":"ZFS history","url":"https://example.test/zfs","content":"Timeline evidence"}]}`))
|
|
}))
|
|
defer searx.Close()
|
|
|
|
broker := activity.New(20)
|
|
eng := &Engine{
|
|
Cfg: config.Config{ResearchEnabled: true},
|
|
Research: research.New(searx.URL),
|
|
Broker: broker,
|
|
}
|
|
result, err := eng.TestResearch(context.Background(), "zfs timeline", 4)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !result.OK || len(result.Results) != 1 || !result.Diagnostic.OK {
|
|
t.Fatalf("unexpected result: %+v", result)
|
|
}
|
|
events := broker.Recent()
|
|
if len(events) != 2 || events[0].Type != "research.test.started" || events[1].Type != "research.test.results" {
|
|
t.Fatalf("unexpected events: %+v", events)
|
|
}
|
|
if events[1].Metadata["result_count"] != 1 {
|
|
t.Fatalf("result count missing from event: %+v", events[1].Metadata)
|
|
}
|
|
}
|
|
|
|
func TestResearchDiagnosticIncludesFailureReason(t *testing.T) {
|
|
searx := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
_, _ = w.Write([]byte("json disabled"))
|
|
}))
|
|
defer searx.Close()
|
|
|
|
broker := activity.New(20)
|
|
eng := &Engine{
|
|
Cfg: config.Config{ResearchEnabled: true},
|
|
Research: research.New(searx.URL),
|
|
Broker: broker,
|
|
}
|
|
result, err := eng.TestResearch(context.Background(), "zfs timeline", 4)
|
|
if err == nil {
|
|
t.Fatal("expected failure")
|
|
}
|
|
if result.Diagnostic.ErrorKind != "http_status" || result.Diagnostic.HTTPStatus != http.StatusForbidden {
|
|
t.Fatalf("unexpected diagnostic: %+v", result.Diagnostic)
|
|
}
|
|
events := broker.Recent()
|
|
if len(events) != 2 || events[1].Type != "research.test.failed" {
|
|
t.Fatalf("unexpected events: %+v", events)
|
|
}
|
|
if events[1].Metadata["error_kind"] != "http_status" || events[1].Metadata["searxng_http_status"] != http.StatusForbidden {
|
|
t.Fatalf("failure details missing: %+v", events[1].Metadata)
|
|
}
|
|
}
|