97 lines
2.9 KiB
Go
97 lines
2.9 KiB
Go
package ingest
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/local/glpi-neural-brain/internal/glpi"
|
|
"github.com/local/glpi-neural-brain/internal/graph"
|
|
"github.com/local/glpi-neural-brain/internal/persist"
|
|
)
|
|
|
|
type fakeGLPIKB struct{}
|
|
|
|
func (fakeGLPIKB) DiscoverKnowledgeBasePath(context.Context, string) (string, error) {
|
|
return "/Knowledge/KnowbaseItem", nil
|
|
}
|
|
func (fakeGLPIKB) ListKnowledgeBaseItems(context.Context, string, int, string) ([]glpi.KnowledgeItem, error) {
|
|
return []glpi.KnowledgeItem{{ID: 42, Title: "VPN Zugriff", Content: "<p>Gateway prüfen</p>", CategoryIDs: []int64{9}, Language: "de-DE", ModifiedAt: "now"}}, nil
|
|
}
|
|
func (fakeGLPIKB) GetCategories(context.Context) ([]glpi.ITILCategory, error) {
|
|
return []glpi.ITILCategory{{ID: 7, Name: "Netzwerk", CompleteName: "Infrastruktur > Netzwerk", KnowbaseCategoryID: 9}}, nil
|
|
}
|
|
|
|
func TestGLPIKBSyncAddsGraphAndQueuesCache(t *testing.T) {
|
|
dir := t.TempDir()
|
|
g, err := graph.Open(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := persist.New(g, nil, time.Minute)
|
|
cachePath := filepath.Join(dir, "cache", "glpi-kb-cache.json")
|
|
s := NewGLPIKBSyncer(GLPIKBConfig{Enabled: true, Path: "auto", Limit: 100, SyncInterval: time.Minute, Source: "glpi-kb", CachePath: cachePath}, fakeGLPIKB{}, g, nil, p)
|
|
if err := s.Sync(context.Background(), "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
status := s.Status()
|
|
if status.Documents != 1 || status.Path != "/Knowledge/KnowbaseItem" {
|
|
t.Fatalf("unexpected status: %+v", status)
|
|
}
|
|
snap := g.Snapshot()
|
|
found := false
|
|
for _, n := range snap.Nodes {
|
|
if n.ExternalID == "GLPI-KB-42" {
|
|
found = true
|
|
if n.Status != "production" || n.Origin != "glpi-kb" {
|
|
t.Fatalf("unexpected node: %+v", n)
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("GLPI KB node missing")
|
|
}
|
|
for _, n := range snap.Nodes {
|
|
if n.ExternalID == "GLPI-KB-42" {
|
|
content, ok := s.Content(n.ID)
|
|
if !ok || content != "Gateway prüfen" {
|
|
t.Fatalf("full GLPI content missing: %q %v", content, ok)
|
|
}
|
|
}
|
|
}
|
|
if _, err := os.Stat(cachePath); !os.IsNotExist(err) {
|
|
t.Fatalf("cache must be buffered before flush: %v", err)
|
|
}
|
|
version := g.Version()
|
|
if err := s.Sync(context.Background(), "test-unchanged"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if g.Version() != version {
|
|
t.Fatalf("unchanged GLPI sync changed graph version: %d -> %d", version, g.Version())
|
|
}
|
|
if err := p.Flush(context.Background(), "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(cachePath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g2, err := graph.Open(filepath.Join(dir, "reload"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s2 := NewGLPIKBSyncer(GLPIKBConfig{Enabled: true, CachePath: cachePath}, fakeGLPIKB{}, g2, nil, p)
|
|
if err := s2.LoadCache(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, n := range g2.Snapshot().Nodes {
|
|
if n.ExternalID == "GLPI-KB-42" {
|
|
content, ok := s2.Content(n.ID)
|
|
if !ok || content != "Gateway prüfen" {
|
|
t.Fatalf("cached full GLPI content missing: %q %v", content, ok)
|
|
}
|
|
}
|
|
}
|
|
}
|