<commit_before>package core

import (
	"log"
	"testing"

	"github.com/jonog/redalert/checks"
	"github.com/jonog/redalert/notifiers"
)

func TestRegisterNotifiers(t *testing.T) {
	service := NewService()
	_, exists := service.notifiers["fake"]
	if exists {
		t.Fail()
	}
	_, exists = service.notifiers["fake2"]
	if exists {
		t.Fail()
	}
	err := service.RegisterNotifier(&fakeNotifier{"fake"})
	if err != nil {
		t.Fail()
	}
	err = service.RegisterNotifier(&fakeNotifier{"fake2"})
	if err != nil {
		t.Fail()
	}
	_, exists = service.notifiers["fake"]
	if !exists {
		t.Fail()
	}
	_, exists = service.notifiers["fake2"]
	if !exists {
		t.Fail()
	}
}

func TestRegisterNotifierDuplicate(t *testing.T) {
	service := NewService()
	err := service.RegisterNotifier(&fakeNotifier{"fake"})
	if err != nil {
		t.Fail()
	}
	err = service.RegisterNotifier(&fakeNotifier{"fake"})
	if err == nil {
		t.Fail()
	}
}

func TestRegisterCheck(t *testing.T) {
	checks.Register("fake", NewFakeChecker)
	check, err := NewCheck(checks.Config{
		Name: "myservice",
		Type: "fake",
	})
	if err != nil || check == nil {
		t.Fail()
	}
	service := NewService()
	if len(service.checks) != 0 {
		t.Fail()
	}
	err = service.RegisterCheck(check, []string{})
	if err != nil {
		t.Fail()
	}
	if len(service.checks) != 1 {
		t.Fail()
	}
	if service.checks[0].Name != "myservice" && service.checks[0].Type != "fake" {
		t.Fail()
	}
}

func TestRegisterCheckNotifications(t *testing.T) {
	checks.Register("fake", NewFakeChecker)
	service := NewService()
	err := service.RegisterNotifier(&fakeNotifier{"my_notifier"})
	if err != nil {
		t.Fail()
	}
	check, err := NewCheck(checks.Config{
		Name:       "myservice",
		Type:       "fake",
		SendAlerts: []string{"my_notifier"},
	})
	if err != nil || check == nil {
		t.Fail()
	}
	err = service.RegisterCheck(check, []string{"my_notifier"})
	if err != nil {
		t.Fail()
	}
	if len(service.checks[0].Notifiers) != 1 {
		t.Fail()
	}
	if service.checks[0].Notifiers[0].Name() != "my_notifier" {
		t.Fail()
	}
}

///////////////
// Helpers
///////////////

// Fake Notifier

type fakeNotifier struct {
	name string
}

func (n *fakeNotifier) Notify(msg notifiers.Message) error {
	return nil
}

func (n *fakeNotifier) Name() string {
	return n.name
}

// Fake Checker

type fakeChecker struct {
}

var NewFakeChecker = func(config checks.Config, logger *log.Logger) (checks.Checker, error) {
	return checks.Checker(&fakeChecker{}), nil
}

func (c *fakeChecker) Check() (checks.Metrics, error) {
	return checks.Metrics(make(map[string]*float64)), nil
}

func (c *fakeChecker) MetricInfo(metric string) checks.MetricInfo {
	return checks.MetricInfo{Unit: ""}
}

func (c *fakeChecker) RedAlertMessage() string {
	return "redalert"
}

func (c *fakeChecker) GreenAlertMessage() string {
	return "greenalert"
}
<commit_msg>Method required for fake checker.<commit_after>package core

import (
	"log"
	"testing"

	"github.com/jonog/redalert/checks"
	"github.com/jonog/redalert/notifiers"
)

func TestRegisterNotifiers(t *testing.T) {
	service := NewService()
	_, exists := service.notifiers["fake"]
	if exists {
		t.Fail()
	}
	_, exists = service.notifiers["fake2"]
	if exists {
		t.Fail()
	}
	err := service.RegisterNotifier(&fakeNotifier{"fake"})
	if err != nil {
		t.Fail()
	}
	err = service.RegisterNotifier(&fakeNotifier{"fake2"})
	if err != nil {
		t.Fail()
	}
	_, exists = service.notifiers["fake"]
	if !exists {
		t.Fail()
	}
	_, exists = service.notifiers["fake2"]
	if !exists {
		t.Fail()
	}
}

func TestRegisterNotifierDuplicate(t *testing.T) {
	service := NewService()
	err := service.RegisterNotifier(&fakeNotifier{"fake"})
	if err != nil {
		t.Fail()
	}
	err = service.RegisterNotifier(&fakeNotifier{"fake"})
	if err == nil {
		t.Fail()
	}
}

func TestRegisterCheck(t *testing.T) {
	checks.Register("fake", NewFakeChecker)
	check, err := NewCheck(checks.Config{
		Name: "myservice",
		Type: "fake",
	})
	if err != nil || check == nil {
		t.Fail()
	}
	service := NewService()
	if len(service.checks) != 0 {
		t.Fail()
	}
	err = service.RegisterCheck(check, []string{})
	if err != nil {
		t.Fail()
	}
	if len(service.checks) != 1 {
		t.Fail()
	}
	if service.checks[0].Name != "myservice" && service.checks[0].Type != "fake" {
		t.Fail()
	}
}

func TestRegisterCheckNotifications(t *testing.T) {
	checks.Register("fake", NewFakeChecker)
	service := NewService()
	err := service.RegisterNotifier(&fakeNotifier{"my_notifier"})
	if err != nil {
		t.Fail()
	}
	check, err := NewCheck(checks.Config{
		Name:       "myservice",
		Type:       "fake",
		SendAlerts: []string{"my_notifier"},
	})
	if err != nil || check == nil {
		t.Fail()
	}
	err = service.RegisterCheck(check, []string{"my_notifier"})
	if err != nil {
		t.Fail()
	}
	if len(service.checks[0].Notifiers) != 1 {
		t.Fail()
	}
	if service.checks[0].Notifiers[0].Name() != "my_notifier" {
		t.Fail()
	}
}

///////////////
// Helpers
///////////////

// Fake Notifier

type fakeNotifier struct {
	name string
}

func (n *fakeNotifier) Notify(msg notifiers.Message) error {
	return nil
}

func (n *fakeNotifier) Name() string {
	return n.name
}

// Fake Checker

type fakeChecker struct {
}

var NewFakeChecker = func(config checks.Config, logger *log.Logger) (checks.Checker, error) {
	return checks.Checker(&fakeChecker{}), nil
}

func (c *fakeChecker) Check() (checks.Metrics, error) {
	return checks.Metrics(make(map[string]*float64)), nil
}

func (c *fakeChecker) MetricInfo(metric string) checks.MetricInfo {
	return checks.MetricInfo{Unit: ""}
}

func (c *fakeChecker) RedAlertMessage() string {
	return "redalert"
}

func (c *fakeChecker) GreenAlertMessage() string {
	return "greenalert"
}

func (c *fakeChecker) MessageContext() string {
	return "message_context"
}

{'fraction_non_alphanumeric': 0.17779327174481435, 'fraction_numerical': 0.004531985358201151, 'mean_word_length': 3.9084687767322497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1834629', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2114, 'n_words': 441}