new-api/service/channel_monitor_templates_test.go
zizi a63300fc1f feat: add channel monitor safety helpers
Add default probe prompts and SSRF validation for channel monitor targets, with focused tests for blocked internal addresses.
2026-05-20 13:28:58 +08:00

64 lines
1.7 KiB
Go

package service
import (
"math/rand"
"testing"
"github.com/stretchr/testify/require"
)
func TestDefaultChannelMonitorRequestTemplatesHasTwentyShortEnglishPrompts(t *testing.T) {
t.Parallel()
require.Len(t, DefaultChannelMonitorRequestTemplates, 20)
for _, template := range DefaultChannelMonitorRequestTemplates {
require.NotEmpty(t, template)
require.LessOrEqual(t, len(template), 120)
}
}
func TestParseChannelMonitorRequestTemplatesUsesCustomTemplates(t *testing.T) {
t.Parallel()
templates := ParseChannelMonitorRequestTemplates(`["Say hello.","Solve 2+2."]`)
require.Equal(t, []string{"Say hello.", "Solve 2+2."}, templates)
}
func TestParseChannelMonitorRequestTemplatesFallsBackForEmptyInvalidOrBlankTemplates(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
raw string
}{
{name: "empty", raw: ""},
{name: "blank", raw: " "},
{name: "invalid json", raw: "{bad json"},
{name: "wrong json type", raw: `{"prompt":"hello"}`},
{name: "only empty strings", raw: `["", " "]`},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
templates := ParseChannelMonitorRequestTemplates(tc.raw)
require.Equal(t, DefaultChannelMonitorRequestTemplates, templates)
})
}
}
func TestSelectChannelMonitorRequestTemplateUsesDeterministicRandWhenProvided(t *testing.T) {
t.Parallel()
raw := `["first","second","third"]`
first := SelectChannelMonitorRequestTemplate(raw, rand.New(rand.NewSource(42)))
second := SelectChannelMonitorRequestTemplate(raw, rand.New(rand.NewSource(42)))
require.Equal(t, first, second)
require.Contains(t, []string{"first", "second", "third"}, first)
}