mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 10:33:40 +01:00
Completes #1369. The delivery path landed in 23695681a with config-only
gating; this surfaces the two fields so operators can turn finding
notifications off or restrict them to critical findings without editing
config by hand.
The AI settings GET response and update request carry
patrol_finding_notifications_enabled and
patrol_finding_notify_min_severity, with the same warning-or-critical
validation the alert-trigger severity field uses. The Patrol settings
page gains a Notifications card between Triggers and Model readiness,
mirroring the alert-trigger toggle-plus-severity pattern, with the
severity select shown only while notifications are enabled.
Exercised end to end against a scratch backend in mock mode. A severity
change and an explicit opt-out both survive save, reload, and the GET
round trip, and the opt-out hides the severity select immediately.
83 lines
2.6 KiB
Go
83 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAISettingsHandler_UpdateSettingsPersistsFindingNotificationPolicy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tmp := t.TempDir()
|
|
cfg := &config.Config{DataPath: tmp}
|
|
persistence := config.NewConfigPersistence(tmp)
|
|
handler := newTestAISettingsHandler(cfg, persistence, nil)
|
|
|
|
body, err := json.Marshal(AISettingsUpdateRequest{
|
|
PatrolFindingNotificationsEnabled: ptr(false),
|
|
PatrolFindingNotifyMinSeverity: ptr("Critical"),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
req := newLoopbackRequest(http.MethodPut, "/api/settings/ai/update", bytes.NewReader(body))
|
|
rec := httptest.NewRecorder()
|
|
handler.HandleUpdateAISettings(rec, req)
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
|
var resp AISettingsResponse
|
|
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp))
|
|
require.False(t, resp.PatrolFindingNotificationsEnabled)
|
|
require.Equal(t, "critical", resp.PatrolFindingNotifyMinSeverity)
|
|
|
|
saved, err := persistence.LoadAIConfig()
|
|
require.NoError(t, err)
|
|
require.False(t, saved.PatrolFindingNotificationsEnabled)
|
|
require.Equal(t, "critical", saved.PatrolFindingNotifyMinSeverity)
|
|
}
|
|
|
|
func TestAISettingsHandler_UpdateSettingsRejectsInvalidFindingNotifySeverity(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tmp := t.TempDir()
|
|
cfg := &config.Config{DataPath: tmp}
|
|
persistence := config.NewConfigPersistence(tmp)
|
|
handler := newTestAISettingsHandler(cfg, persistence, nil)
|
|
|
|
body, err := json.Marshal(AISettingsUpdateRequest{
|
|
PatrolFindingNotifyMinSeverity: ptr("emergency"),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
req := newLoopbackRequest(http.MethodPut, "/api/settings/ai/update", bytes.NewReader(body))
|
|
rec := httptest.NewRecorder()
|
|
handler.HandleUpdateAISettings(rec, req)
|
|
|
|
require.Equal(t, http.StatusBadRequest, rec.Code, rec.Body.String())
|
|
require.Contains(t, rec.Body.String(), "patrol_finding_notify_min_severity")
|
|
}
|
|
|
|
func TestAISettingsHandler_GetSettingsReportsFindingNotificationDefaults(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tmp := t.TempDir()
|
|
cfg := &config.Config{DataPath: tmp}
|
|
persistence := config.NewConfigPersistence(tmp)
|
|
handler := newTestAISettingsHandler(cfg, persistence, nil)
|
|
|
|
req := newLoopbackRequest(http.MethodGet, "/api/settings/ai", nil)
|
|
rec := httptest.NewRecorder()
|
|
handler.HandleGetAISettings(rec, req)
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
|
var resp AISettingsResponse
|
|
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp))
|
|
require.True(t, resp.PatrolFindingNotificationsEnabled)
|
|
require.Equal(t, "warning", resp.PatrolFindingNotifyMinSeverity)
|
|
}
|