Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

9.0 KB
Raw Blame
package api

import (
	"bytes"
	"encoding/json"
	"strings"
	"testing"
	"time"

	"net/http"
	"net/http/httptest"

	"github.com/rcourtman/pulse-go-rewrite/internal/ai"
	"github.com/rcourtman/pulse-go-rewrite/internal/ai/knowledge"
	"github.com/rcourtman/pulse-go-rewrite/internal/config"
)

type stubLicenseChecker struct {
	allow bool
}

func (s stubLicenseChecker) HasFeature(feature string) bool {
	return s.allow
}

func (s stubLicenseChecker) GetLicenseStateString() (string, bool) {
	if s.allow {
		return "active", true
	}
	return "expired", false
}

func newTestAISettingsHandlerWithService(t *testing.T) *AISettingsHandler {
	t.Helper()
	tmp := t.TempDir()
	cfg := &config.Config{DataPath: tmp}
	persistence := config.NewConfigPersistence(tmp)

	handler := NewAISettingsHandler(nil, nil, nil)
	handler.defaultConfig = cfg
	handler.defaultPersistence = persistence
	handler.defaultAIService = ai.NewService(persistence, nil)
	return handler
}

func withLegacyAssistantStreamIdleInterval(t *testing.T, interval time.Duration) {
	t.Helper()
	previous := chatStreamIdleProgressInterval
	chatStreamIdleProgressInterval = interval
	t.Cleanup(func() {
		chatStreamIdleProgressInterval = previous
	})
}

func assertLegacyAssistantStreamIdleBeforeTerminal(t *testing.T, response string) {
	t.Helper()
	idleIndex := strings.Index(response, `"phase":"stream_idle"`)
	completeIndex := strings.Index(response, `"type":"complete"`)
	doneIndex := strings.LastIndex(response, `"type":"done"`)
	if idleIndex < 0 {
		t.Fatalf("response missing idle progress event: %s", response)
	}
	if !strings.Contains(response, chatStreamIdleProgressMessage) {
		t.Fatalf("response missing idle progress message: %s", response)
	}
	if completeIndex < 0 {
		t.Fatalf("response missing complete event: %s", response)
	}
	if doneIndex < 0 {
		t.Fatalf("response missing done event: %s", response)
	}
	if idleIndex > completeIndex {
		t.Fatalf("idle progress appeared after complete event: %s", response)
	}
	if idleIndex > doneIndex {
		t.Fatalf("idle progress appeared after done event: %s", response)
	}
}

func TestHandleExecuteStream_LicenseRequired(t *testing.T) {
	setMockModeForTest(t, true)

	handler := newTestAISettingsHandlerWithService(t)
	handler.defaultAIService.SetLicenseChecker(stubLicenseChecker{allow: false})

	body := `{"prompt":"hi","use_case":"autofix"}`
	req := httptest.NewRequest(http.MethodPost, "/api/ai/execute/stream", strings.NewReader(body))
	rec := httptest.NewRecorder()
	handler.HandleExecuteStream(rec, req)

	if rec.Code != http.StatusPaymentRequired {
		t.Fatalf("expected payment required, got %d", rec.Code)
	}
	if !strings.Contains(rec.Body.String(), "license_required") {
		t.Fatalf("expected license error body")
	}
}

func TestHandleExecuteStream_PromptRequired(t *testing.T) {
	setMockModeForTest(t, true)

	handler := newTestAISettingsHandlerWithService(t)

	body := `{"prompt":""}`
	req := httptest.NewRequest(http.MethodPost, "/api/ai/execute/stream", strings.NewReader(body))
	rec := httptest.NewRecorder()
	handler.HandleExecuteStream(rec, req)

	if rec.Code != http.StatusBadRequest {
		t.Fatalf("expected bad request, got %d", rec.Code)
	}
}

func TestHandleExecuteStream_Success(t *testing.T) {
	setMockModeForTest(t, true)

	handler := newTestAISettingsHandlerWithService(t)

	body := `{"prompt":"hello"}`
	req := httptest.NewRequest(http.MethodPost, "/api/ai/execute/stream", strings.NewReader(body))
	rec := httptest.NewRecorder()
	handler.HandleExecuteStream(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected OK, got %d", rec.Code)
	}
	if !strings.Contains(rec.Header().Get("Content-Type"), "text/event-stream") {
		t.Fatalf("expected SSE content type")
	}
	if !strings.Contains(rec.Body.String(), `"tool_calls":[]`) {
		t.Fatalf("expected stream completion event to retain tool_calls array, got %s", rec.Body.String())
	}
}

func TestHandleExecuteStream_EmitsIdleProgressWhileServiceIsSilent(t *testing.T) {
	setMockModeForTest(t, true)
	withLegacyAssistantStreamIdleInterval(t, 2*time.Millisecond)

	handler := newTestAISettingsHandlerWithService(t)

	body := `{"prompt":"disk usage"}`
	req := httptest.NewRequest(http.MethodPost, "/api/ai/execute/stream", strings.NewReader(body))
	rec := httptest.NewRecorder()
	handler.HandleExecuteStream(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected OK, got %d", rec.Code)
	}
	assertLegacyAssistantStreamIdleBeforeTerminal(t, rec.Body.String())
}

func TestHandleInvestigateAlert_EmitsIdleProgressWhileServiceIsSilent(t *testing.T) {
	setMockModeForTest(t, true)
	withLegacyAssistantStreamIdleInterval(t, 2*time.Millisecond)

	handler := newTestAISettingsHandlerWithService(t)

	body := `{"alertIdentifier":"alert-1","resource_id":"node-1","resource_name":"node-1","resource_type":"agent","alert_type":"cpu","level":"warning","message":"high cpu"}`
	req := newLoopbackRequest(http.MethodPost, "/api/ai/investigate-alert", strings.NewReader(body))
	rec := httptest.NewRecorder()
	handler.HandleInvestigateAlert(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected OK, got %d: %s", rec.Code, rec.Body.String())
	}
	assertLegacyAssistantStreamIdleBeforeTerminal(t, rec.Body.String())
}

func TestAIExecuteStreamCompleteEvent_UsesCanonicalEmptyCollections(t *testing.T) {
	payload, err := json.Marshal(emptyAIExecuteStreamCompleteEvent())
	if err != nil {
		t.Fatalf("marshal empty AI stream completion event: %v", err)
	}
	if !strings.Contains(string(payload), `"tool_calls":[]`) {
		t.Fatalf("expected empty AI stream completion event to retain tool_calls array, got %s", payload)
	}
}

func TestHandleExportGuestKnowledge(t *testing.T) {
	handler := newTestAISettingsHandlerWithService(t)
	if err := handler.defaultAIService.SaveGuestNote("guest-1", "VM 1", "vm", "ops", "Note", "Content"); err != nil {
		t.Fatalf("save note error: %v", err)
	}

	req := httptest.NewRequest(http.MethodGet, "/api/ai/knowledge/export?guest_id=guest-1", nil)
	rec := httptest.NewRecorder()
	handler.HandleExportGuestKnowledge(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected OK, got %d", rec.Code)
	}
	if !strings.Contains(rec.Header().Get("Content-Disposition"), "guest-1") {
		t.Fatalf("expected content disposition with guest id")
	}

	var exported knowledge.GuestKnowledge
	if err := json.NewDecoder(rec.Body).Decode(&exported); err != nil {
		t.Fatalf("decode error: %v", err)
	}
	if len(exported.Notes) == 0 {
		t.Fatalf("expected exported notes")
	}

	missingReq := httptest.NewRequest(http.MethodGet, "/api/ai/knowledge/export", nil)
	missingRec := httptest.NewRecorder()
	handler.HandleExportGuestKnowledge(missingRec, missingReq)
	if missingRec.Code != http.StatusBadRequest {
		t.Fatalf("expected bad request for missing guest id")
	}
}

func TestHandleImportGuestKnowledge(t *testing.T) {
	handler := newTestAISettingsHandlerWithService(t)
	if err := handler.defaultAIService.SaveGuestNote("guest-1", "VM 1", "vm", "ops", "Old", "Old content"); err != nil {
		t.Fatalf("save note error: %v", err)
	}

	invalidReq := httptest.NewRequest(http.MethodPost, "/api/ai/knowledge/import", strings.NewReader("{bad"))
	invalidRec := httptest.NewRecorder()
	handler.HandleImportGuestKnowledge(invalidRec, invalidReq)
	if invalidRec.Code != http.StatusBadRequest {
		t.Fatalf("expected bad request for invalid json")
	}

	methodReq := httptest.NewRequest(http.MethodGet, "/api/ai/knowledge/import", nil)
	methodRec := httptest.NewRecorder()
	handler.HandleImportGuestKnowledge(methodRec, methodReq)
	if methodRec.Code != http.StatusMethodNotAllowed {
		t.Fatalf("expected method not allowed")
	}

	emptyReq := httptest.NewRequest(http.MethodPost, "/api/ai/knowledge/import", strings.NewReader(`{"guest_id":""}`))
	emptyRec := httptest.NewRecorder()
	handler.HandleImportGuestKnowledge(emptyRec, emptyReq)
	if emptyRec.Code != http.StatusBadRequest {
		t.Fatalf("expected bad request for missing guest id")
	}

	noNotesReq := httptest.NewRequest(http.MethodPost, "/api/ai/knowledge/import", strings.NewReader(`{"guest_id":"guest-1","notes":[]}`))
	noNotesRec := httptest.NewRecorder()
	handler.HandleImportGuestKnowledge(noNotesRec, noNotesReq)
	if noNotesRec.Code != http.StatusBadRequest {
		t.Fatalf("expected bad request for empty notes")
	}

	payload := map[string]interface{}{
		"guest_id":   "guest-1",
		"guest_name": "VM 1",
		"guest_type": "vm",
		"merge":      false,
		"notes": []map[string]string{
			{"category": "ops", "title": "New", "content": "New content"},
			{"category": "", "title": "Skip", "content": "Bad"},
		},
	}
	body, _ := json.Marshal(payload)
	req := httptest.NewRequest(http.MethodPost, "/api/ai/knowledge/import", bytes.NewReader(body))
	rec := httptest.NewRecorder()
	handler.HandleImportGuestKnowledge(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected OK, got %d", rec.Code)
	}
	if !strings.Contains(rec.Body.String(), "\"imported\":1") {
		t.Fatalf("expected import count")
	}

	knowledge, err := handler.defaultAIService.GetGuestKnowledge("guest-1")
	if err != nil {
		t.Fatalf("get knowledge error: %v", err)
	}
	if len(knowledge.Notes) != 1 {
		t.Fatalf("expected only imported notes, got %d", len(knowledge.Notes))
	}
}