From 50453ba19c069423751428ccaa950f9aad40c42c Mon Sep 17 00:00:00 2001 From: laily Date: Tue, 26 May 2026 12:53:50 +0800 Subject: [PATCH] fix: stub hook send in tests for CI without /dev/tty Avoid opening /dev/tty during unit tests so GitHub Actions can run go test without a controlling terminal. Co-authored-by: Cursor --- internal/hook/cursor.go | 9 +++++++-- internal/hook/hook_test.go | 1 + internal/hook/stdin_test.go | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/hook/cursor.go b/internal/hook/cursor.go index effcfce..d525616 100644 --- a/internal/hook/cursor.go +++ b/internal/hook/cursor.go @@ -17,6 +17,11 @@ type cursorPayload struct { Status string `json:"status"` } +var ( + sendForHook = notify.SendForHookWithResult + sendAuto = notify.SendAutoWithResult +) + func RunCursor(r io.Reader, cfg config.Config, event string, _ io.Writer) error { var payload cursorPayload fromHook := !isInteractiveStdin(r) @@ -50,9 +55,9 @@ func RunCursor(r io.Reader, cfg config.Config, event string, _ io.Writer) error var result notify.SendResult var err error if fromHook { - result, err = notify.SendForHookWithResult(cfg.Notify.Protocol, title, body) + result, err = sendForHook(cfg.Notify.Protocol, title, body) } else { - result, err = notify.SendAutoWithResult(cfg.Notify.Protocol, title, body) + result, err = sendAuto(cfg.Notify.Protocol, title, body) } if err != nil { logx.Append("hook cursor event=%s send FAILED: %v", event, err) diff --git a/internal/hook/hook_test.go b/internal/hook/hook_test.go index 21dc222..f33efb0 100644 --- a/internal/hook/hook_test.go +++ b/internal/hook/hook_test.go @@ -10,6 +10,7 @@ import ( ) func TestCursorStopHookDisabled(t *testing.T) { + stubCursorSend(t) cfg := config.Default() cfg.Events.Stop = false err := RunCursor(bytes.NewReader([]byte(`{"workspace_roots":["/tmp/proj"]}`)), cfg, "stop", &bytes.Buffer{}) diff --git a/internal/hook/stdin_test.go b/internal/hook/stdin_test.go index 0254629..a77e9df 100644 --- a/internal/hook/stdin_test.go +++ b/internal/hook/stdin_test.go @@ -5,9 +5,26 @@ import ( "testing" "github.com/longbin/agent-notify/internal/config" + "github.com/longbin/agent-notify/internal/notify" ) +func stubCursorSend(t *testing.T) { + t.Helper() + t.Setenv("HOME", t.TempDir()) + prevHook := sendForHook + prevAuto := sendAuto + sendForHook = func(string, string, string) (notify.SendResult, error) { + return notify.SendResult{Method: "test"}, nil + } + sendAuto = sendForHook + t.Cleanup(func() { + sendForHook = prevHook + sendAuto = prevAuto + }) +} + func TestRunCursorEmptyStdinDoesNotBlock(t *testing.T) { + stubCursorSend(t) cfg := config.Default() err := RunCursor(bytes.NewReader(nil), cfg, "stop", nil) if err != nil { @@ -16,6 +33,7 @@ func TestRunCursorEmptyStdinDoesNotBlock(t *testing.T) { } func TestRunCursorWithPayload(t *testing.T) { + stubCursorSend(t) cfg := config.Default() err := RunCursor(bytes.NewReader([]byte(`{"workspace_roots":["/tmp/proj"]}`)), cfg, "stop", nil) if err != nil {