- notification 서비스 테스트 추가 - workflow lifecycle 관리 구현 - config, scheduler, workflow 서비스 개선
114 lines
2.7 KiB
Go
114 lines
2.7 KiB
Go
package notification_test
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"github.com/nomadcode/nomadcode-core/internal/notification"
|
|
)
|
|
|
|
type logRecord struct {
|
|
msg string
|
|
args map[string]any
|
|
}
|
|
|
|
type spyHandler struct {
|
|
records []logRecord
|
|
}
|
|
|
|
func (h *spyHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
|
return true
|
|
}
|
|
|
|
func (h *spyHandler) Handle(ctx context.Context, r slog.Record) error {
|
|
args := make(map[string]any)
|
|
r.Attrs(func(a slog.Attr) bool {
|
|
args[a.Key] = a.Value.Any()
|
|
return true
|
|
})
|
|
h.records = append(h.records, logRecord{
|
|
msg: r.Message,
|
|
args: args,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (h *spyHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return h
|
|
}
|
|
|
|
func (h *spyHandler) WithGroup(name string) slog.Handler {
|
|
return h
|
|
}
|
|
|
|
func TestNotifyTaskCompletedUsesTaskEventCompleted(t *testing.T) {
|
|
spy := &spyHandler{}
|
|
logger := slog.New(spy)
|
|
|
|
service := notification.NewService(nil, logger)
|
|
|
|
err := service.NotifyTaskCompleted(context.Background(), notification.TaskNotification{
|
|
TaskID: "task-123",
|
|
Title: "Test Task",
|
|
Status: "completed",
|
|
Message: "Finished successfully",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if len(spy.records) == 0 {
|
|
t.Fatal("expected log records, got none")
|
|
}
|
|
|
|
found := false
|
|
for _, rec := range spy.records {
|
|
if rec.msg == "task event notification requested" {
|
|
found = true
|
|
if rec.args["type"] != notification.TaskEventCompleted {
|
|
t.Errorf("expected event type %s, got %v", notification.TaskEventCompleted, rec.args["type"])
|
|
}
|
|
if rec.args["task_id"] != "task-123" {
|
|
t.Errorf("expected task_id task-123, got %v", rec.args["task_id"])
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Error("expected 'task event notification requested' log message, not found")
|
|
}
|
|
}
|
|
|
|
func TestNotifyTaskEventAllowsFailedWithoutMattermost(t *testing.T) {
|
|
spy := &spyHandler{}
|
|
logger := slog.New(spy)
|
|
|
|
service := notification.NewService(nil, logger)
|
|
|
|
err := service.NotifyTaskEvent(context.Background(), notification.TaskEvent{
|
|
Type: notification.TaskEventFailed,
|
|
TaskID: "task-456",
|
|
Title: "Failed Task",
|
|
Status: "failed",
|
|
Message: "Something went wrong",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
found := false
|
|
for _, rec := range spy.records {
|
|
if rec.msg == "task event notification requested" {
|
|
found = true
|
|
if rec.args["type"] != notification.TaskEventFailed {
|
|
t.Errorf("expected event type %s, got %v", notification.TaskEventFailed, rec.args["type"])
|
|
}
|
|
if rec.args["task_id"] != "task-456" {
|
|
t.Errorf("expected task_id task-456, got %v", rec.args["task_id"])
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Error("expected 'task event notification requested' log message, not found")
|
|
}
|
|
}
|