Files
glpi-ai-agent/internal/glpikb/sync_test.go
T
jbergner 523e597e7a
release-tag / release-image (push) Has been cancelled
RC-2
2026-07-28 20:15:43 +02:00

53 lines
1.9 KiB
Go

package glpikb
import (
"context"
"path/filepath"
"testing"
"time"
"github.com/example/glpi-ai-agent/internal/config"
"github.com/example/glpi-ai-agent/internal/metrics"
"github.com/example/glpi-ai-agent/internal/model"
)
type fakeSource struct{}
func (fakeSource) DiscoverKnowledgeBasePath(context.Context, string) (string, error) {
return "/Knowledge/KnowbaseItem", nil
}
func (fakeSource) ListKnowledgeBaseItems(context.Context, string, int, string) ([]model.GLPIKnowledgeItem, error) {
return []model.GLPIKnowledgeItem{{ID: 12, Title: "Benutzerkonto gesperrt", Content: "<p>Bitte entsperren.</p>", CategoryIDs: []int64{9}, ModifiedAt: "now"}}, nil
}
func (fakeSource) GetCategories(context.Context) ([]model.Category, error) {
return []model.Category{{ID: 2, Name: "Active Directory", KnowbaseCategoryID: 9}}, nil
}
type fakeStore struct{ docs []model.KnowledgeDoc }
func (f *fakeStore) ReplaceExternalSource(_ context.Context, _ string, d []model.KnowledgeDoc) error {
f.docs = append([]model.KnowledgeDoc(nil), d...)
return nil
}
func (f *fakeStore) Count() int { return len(f.docs) }
func TestSyncMapsGLPIKBAndAutoReplyWhitelist(t *testing.T) {
cfg := config.Config{DataDir: t.TempDir(), GLPIKBEnabled: true, GLPIKBPath: "auto", GLPIKBLimit: 50, GLPIKBSyncInterval: time.Minute, GLPIKBSource: "glpi-kb", GLPIKBAutoReply: true, GLPIKBAutoReplyCategoryIDs: []int64{9}, CommunicationLanguage: "de-DE", CommunicationStyle: "formal", GLPITimeout: time.Second}
st := &fakeStore{}
m := metrics.New()
s := New(cfg, fakeSource{}, st, m)
if err := s.Sync(context.Background()); err != nil {
t.Fatal(err)
}
if len(st.docs) != 1 {
t.Fatalf("docs=%d", len(st.docs))
}
d := st.docs[0]
if d.ID != "GLPI-KB-12" || !d.AutoReply || len(d.Categories) != 1 || d.Categories[0] != 2 || d.Text != "Bitte entsperren." || d.AnswerHTML != "<p>Bitte entsperren.</p>" {
t.Fatalf("unexpected doc: %+v", d)
}
if _, err := filepath.Abs(cfg.DataDir); err != nil {
t.Fatal(err)
}
}