<commit_before>package stun

import (
	"testing"
	"time"
)

func TestAgent_ProcessInTransaction(t *testing.T) {
	m := New()
	a := NewAgent(AgentOptions{
		Handler: func(e Event) {
			if e.Error != nil {
				t.Errorf("got error: %s", e.Error)
			}
			if !e.Message.Equal(m) {
				t.Errorf("%s (got) != %s (expected)", e.Message, m)
			}
		},
	})
	if err := m.NewTransactionID(); err != nil {
		t.Fatal(err)
	}
	if err := a.Start(m.TransactionID, time.Time{}); err != nil {
		t.Fatal(err)
	}
	if err := a.Process(m); err != nil {
		t.Error(err)
	}
	if err := a.Close(); err != nil {
		t.Error(err)
	}
}

func TestAgent_Process(t *testing.T) {
	m := New()
	a := NewAgent(AgentOptions{
		Handler: func(e Event) {
			if e.Error != nil {
				t.Errorf("got error: %s", e.Error)
			}
			if !e.Message.Equal(m) {
				t.Errorf("%s (got) != %s (expected)", e.Message, m)
			}
		},
	})
	if err := m.NewTransactionID(); err != nil {
		t.Fatal(err)
	}
	if err := a.Process(m); err != nil {
		t.Error(err)
	}
	if err := a.Close(); err != nil {
		t.Error(err)
	}
	if err := a.Process(m); err != ErrAgentClosed {
		t.Errorf("closed agent should return <%s>, but got <%s>",
			ErrAgentClosed, err,
		)
	}
}

func TestAgent_Start(t *testing.T) {
	a := NewAgent(AgentOptions{
		Handler: noopHandler,
	})
	id := NewTransactionID()
	deadline := time.Now().AddDate(0, 0, 1)
	if err := a.Start(id, deadline); err != nil {
		t.Errorf("failed to statt transaction: %s", err)
	}
	if err := a.Start(id, deadline); err != ErrTransactionExists {
		t.Errorf("duplicate start should return <%s>, got <%s>",
			ErrTransactionExists, err,
		)
	}
	if err := a.Close(); err != nil {
		t.Error(err)
	}
	id = NewTransactionID()
	if err := a.Start(id, deadline); err != ErrAgentClosed {
		t.Errorf("start on closed agent should return <%s>, got <%s>",
			ErrAgentClosed, err,
		)
	}
}

func TestAgent_Stop(t *testing.T) {
	called := make(chan Event, 1)
	a := NewAgent(AgentOptions{
		Handler: func(e Event) {
			called <- e<commit_msg>agent: add test for SetHandler on closed agent<commit_after>package stun

import (
	"testing"
	"time"
)

func TestAgent_ProcessInTransaction(t *testing.T) {
	m := New()
	a := NewAgent(AgentOptions{
		Handler: func(e Event) {
			if e.Error != nil {
				t.Errorf("got error: %s", e.Error)
			}
			if !e.Message.Equal(m) {
				t.Errorf("%s (got) != %s (expected)", e.Message, m)
			}
		},
	})
	if err := m.NewTransactionID(); err != nil {
		t.Fatal(err)
	}
	if err := a.Start(m.TransactionID, time.Time{}); err != nil {
		t.Fatal(err)
	}
	if err := a.Process(m); err != nil {
		t.Error(err)
	}
	if err := a.Close(); err != nil {
		t.Error(err)
	}
}

func TestAgent_Process(t *testing.T) {
	m := New()
	a := NewAgent(AgentOptions{
		Handler: func(e Event) {
			if e.Error != nil {
				t.Errorf("got error: %s", e.Error)
			}
			if !e.Message.Equal(m) {
				t.Errorf("%s (got) != %s (expected)", e.Message, m)
			}
		},
	})
	if err := m.NewTransactionID(); err != nil {
		t.Fatal(err)
	}
	if err := a.Process(m); err != nil {
		t.Error(err)
	}
	if err := a.Close(); err != nil {
		t.Error(err)
	}
	if err := a.Process(m); err != ErrAgentClosed {
		t.Errorf("closed agent should return <%s>, but got <%s>",
			ErrAgentClosed, err,
		)
	}
}

func TestAgent_Start(t *testing.T) {
	a := NewAgent(AgentOptions{
		Handler: noopHandler,
	})
	id := NewTransactionID()
	deadline := time.Now().AddDate(0, 0, 1)
	if err := a.Start(id, deadline); err != nil {
		t.Errorf("failed to statt transaction: %s", err)
	}
	if err := a.Start(id, deadline); err != ErrTransactionExists {
		t.Errorf("duplicate start should return <%s>, got <%s>",
			ErrTransactionExists, err,
		)
	}
	if err := a.Close(); err != nil {
		t.Error(err)
	}
	id = NewTransactionID()
	if err := a.Start(id, deadline); err != ErrAgentClosed {
		t.Errorf("start on closed agent should return <%s>, got <%s>",
			ErrAgentClosed, err,
		)
	}
	if err := a.SetHandler(nil); err != ErrAgentClosed {
		t.Errorf("SetHandler on closed agent should return <%s>, got <%s>",
			ErrAgentClosed, err,
		)
	}
}

func TestAgent_Stop(t *testing.T) {
	called := make(chan Event, 1)
	a := NewAgent(AgentOptions{
		Handler: func(e Event) {
			called <- e
{'fraction_non_alphanumeric': 0.18947368421052632, 'fraction_numerical': 0.0019138755980861245, 'mean_word_length': 3.2926078028747434, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, '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': '3162845', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1637, 'n_words': 406}