mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-14 10:07:29 +08:00
autonomy notify: enforce notify allowlist from config and add guard test
This commit is contained in:
@@ -29,6 +29,7 @@ type Options struct {
|
||||
Workspace string
|
||||
DefaultNotifyChannel string
|
||||
DefaultNotifyChatID string
|
||||
NotifyAllowChats []string
|
||||
NotifyCooldownSec int
|
||||
NotifySameReasonCooldownSec int
|
||||
QuietHours string
|
||||
@@ -636,6 +637,18 @@ func (e *Engine) shouldNotify(key string, reason string) bool {
|
||||
if strings.TrimSpace(e.opts.DefaultNotifyChannel) == "" || strings.TrimSpace(e.opts.DefaultNotifyChatID) == "" {
|
||||
return false
|
||||
}
|
||||
if len(e.opts.NotifyAllowChats) > 0 {
|
||||
allowed := false
|
||||
for _, c := range e.opts.NotifyAllowChats {
|
||||
if c == e.opts.DefaultNotifyChatID {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
return false
|
||||
}
|
||||
}
|
||||
now := time.Now()
|
||||
if inQuietHours(now, e.opts.QuietHours) {
|
||||
return false
|
||||
|
||||
21
pkg/autonomy/engine_notify_allowlist_test.go
Normal file
21
pkg/autonomy/engine_notify_allowlist_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package autonomy
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestShouldNotify_RespectsNotifyAllowChats(t *testing.T) {
|
||||
e := &Engine{opts: Options{
|
||||
DefaultNotifyChannel: "telegram",
|
||||
DefaultNotifyChatID: "chat-1",
|
||||
NotifyAllowChats: []string{"chat-2", "chat-3"},
|
||||
NotifyCooldownSec: 1,
|
||||
NotifySameReasonCooldownSec: 1,
|
||||
}}
|
||||
if e.shouldNotify("k1", "") {
|
||||
t.Fatalf("expected notify to be blocked when chat not in allowlist")
|
||||
}
|
||||
|
||||
e.opts.NotifyAllowChats = []string{"chat-1"}
|
||||
if !e.shouldNotify("k2", "") {
|
||||
t.Fatalf("expected notify to pass when chat in allowlist")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user