Files
glpi-neural-brain/internal/web/server_test.go
2026-08-04 05:28:51 +02:00

82 lines
2.8 KiB
Go

package web
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/local/glpi-neural-brain/internal/activity"
"github.com/local/glpi-neural-brain/internal/config"
"github.com/local/glpi-neural-brain/internal/engine"
"github.com/local/glpi-neural-brain/internal/graph"
"github.com/local/glpi-neural-brain/internal/model"
)
func TestRuntimeSettingsAndCategoriesAPI(t *testing.T) {
data := t.TempDir()
g, err := graph.Open(data)
if err != nil {
t.Fatal(err)
}
g.UpsertNode(model.Node{ID: "n1", Kind: "knowledge", Label: "GLPI", Origin: "test", Categories: []string{"GLPI"}})
g.UpsertNode(model.Node{ID: "n2", Kind: "knowledge", Label: "Ollama", Origin: "test", Categories: []string{"Ollama"}})
g.UpsertNode(model.Node{ID: "n3", Kind: "knowledge", Label: "Ohne Kategorie", Origin: "test"})
broker := activity.New(20)
eng := engine.New(config.Config{
DataDir: data,
PersistInterval: time.Minute,
RuntimeDefaultsConfigured: true,
LearningEnabled: true,
ThinkingEnabled: true,
DefaultView: "neural",
}, g, broker)
h := (&Server{Engine: eng, Graph: g, Broker: broker}).Handler()
body := `{"learning_enabled":false,"thinking_enabled":false,"learning_categories":["GLPI"],"display_categories":["Ollama"],"thinking_categories":["GLPI"],"view_mode":"honeycomb"}`
req := httptest.NewRequest(http.MethodPut, "/api/runtime-settings", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
res := httptest.NewRecorder()
h.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Fatalf("PUT runtime settings returned %d: %s", res.Code, res.Body.String())
}
var settings engine.RuntimeSettings
if err := json.NewDecoder(res.Body).Decode(&settings); err != nil {
t.Fatal(err)
}
if settings.LearningEnabled || settings.ThinkingEnabled || settings.ViewMode != "honeycomb" {
t.Fatalf("unexpected settings: %+v", settings)
}
res = httptest.NewRecorder()
h.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/api/categories", nil))
if res.Code != http.StatusOK {
t.Fatalf("GET categories returned %d", res.Code)
}
var categories struct {
Categories []engine.CategoryInfo `json:"categories"`
}
if err := json.NewDecoder(res.Body).Decode(&categories); err != nil {
t.Fatal(err)
}
foundUncategorized := false
for _, category := range categories.Categories {
if category.Name == "__uncategorized__" && category.Count == 1 {
foundUncategorized = true
}
}
if !foundUncategorized {
t.Fatalf("uncategorized virtual category missing: %+v", categories.Categories)
}
res = httptest.NewRecorder()
h.ServeHTTP(res, httptest.NewRequest(http.MethodPost, "/api/enrich?async=1", strings.NewReader(`{}`)))
if res.Code != http.StatusConflict {
t.Fatalf("disabled thinking should return 409, got %d: %s", res.Code, res.Body.String())
}
}