<commit_before>package realtime

import (
	"encoding/json"
	"fmt"
	"reflect"
	"testing"
)

type TestRawConnection struct {
	Incoming chan []byte
	Outgoing chan []byte
}

func (c TestRawConnection) ID() string {
	return "testing"
}

func (c TestRawConnection) Receive() (payload []byte, err error) {
	v := <-c.Incoming
	if v == nil {
		return nil, fmt.Errorf("Incoming Error")
	}
	return v, nil
}

func (c TestRawConnection) Send(payload []byte) (err error) {
	if payload == nil {
		return fmt.Errorf("Outgoing Error")
	}
	c.Outgoing <- payload
	return
}

func NewTestRawConnection(inBuffer, outBuffer int) TestRawConnection {
	return TestRawConnection{
		Incoming: make(chan []byte, inBuffer),
		Outgoing: make(chan []byte, outBuffer),
	}
}

func TestSuccessfulReceive(t *testing.T) {
	raw := NewTestRawConnection(1, 0)
	conn := New(raw)

	raw.Incoming <- []byte(`{
			"type": "test",
			"body": "this is a test"
		}`)

	result, err := conn.Receive()
	if err != nil {
		t.Fatal("Unexpected error", err)
	}

	if result.EventType() != "test" {
		t.Fatal("Event types did not match", "test", result.EventType())
	}

	v := make(map[string]string)
	err = json.Unmarshal(result.Payload(), &v)
	if err != nil {
		t.Fatal("Failed to unmarshal payload", err)
	}

	expected := map[string]string{
		"type": "test",
		"body": "this is a test",
	}

	if !reflect.DeepEqual(expected, v) {
		t.Fatal("Payload did not match", expected, v)
	}
}

func TestInvalidJson(t *testing.T) {
	raw := NewTestRawConnection(1, 0)
	conn := New(raw)

	raw.Incoming <- []byte(`{
			"type": "test",
			INVALID JSON
		}`)

	result, err := conn.Receive()
	if result != nil {
		t.Fatal("Expected nil result", result)
	}

	if err == nil {
		t.Fatal("Expected error")
	}
}

func TestReceiveError(t *testing.T) {
	raw := NewTestRawConnection(0, 0)
	conn := New(raw)

	close(raw.Incoming)

	result, err := conn.Receive()
	if result != nil {
		t.Fatal("Expected nil result", result)
	}

	if err == nil {
		t.Fatal("Expected error")
	}
}

func TestSuccessfulSend(t *testing.T) {
	raw := NewTestRawConnection(0, 1)
	conn := New(raw)

	event := realTimeEvent{
		eventType: "test",
		payload: []byte(`{
				"type": "test",
				"body": "this is a test"
			}`),
	}

	err := conn.Send(event)
	if err != nil {
		t.Fatal("Unexpected error", err)
	}

	payload := <-raw.Outgoing

	if !reflect.DeepEqual(payload, event.Payload()) {
		t.Fatal("Payload did not match", event.Payload(), payload)
	}
}

func TestSendError(t *testing.T) {
	raw := NewTestRawConnection(0, 1)
	conn := New(raw)

	event := realTimeEvent{
		eventType: "test",
		payload:   nil,<commit_msg>Use closures instead of channels to drive test data<commit_after>package realtime

import (
	"encoding/json"
	"fmt"
	"reflect"
	"testing"
)

type TestRawConnection struct {
	receive func() ([]byte, error)
	send    func([]byte) error
}

func (c TestRawConnection) ID() string {
	return "testing"
}

func (c TestRawConnection) Receive() (payload []byte, err error) {
	return c.receive()
}

func (c TestRawConnection) Send(payload []byte) (err error) {
	return c.send(payload)
}

func TestSuccessfulReceive(t *testing.T) {
	called := false
	raw := TestRawConnection{
		receive: func() ([]byte, error) {
			if called {
				t.Fatal("RawConnection.Receive called more than once")
			}
			called = true
			return []byte(`{ "type": "test", "body": "this is a test" }`), nil
		},
	}
	conn := New(raw)

	result, err := conn.Receive()
	if err != nil {
		t.Fatal("Unexpected error", err)
	}

	if result.EventType() != "test" {
		t.Fatal("Event types did not match", "test", result.EventType())
	}

	v := make(map[string]string)
	err = json.Unmarshal(result.Payload(), &v)
	if err != nil {
		t.Fatal("Failed to unmarshal payload", err)
	}

	expected := map[string]string{
		"type": "test",
		"body": "this is a test",
	}

	if !reflect.DeepEqual(expected, v) {
		t.Fatal("Payload did not match", expected, v)
	}
}

func TestInvalidJson(t *testing.T) {
	called := false
	raw := TestRawConnection{
		receive: func() ([]byte, error) {
			if called {
				t.Fatal("RawConnection.Receive called more than once")
			}
			called = true
			return []byte(`{ "type": "test", INVALID JSON }`), nil
		},
	}
	conn := New(raw)

	result, err := conn.Receive()
	if result != nil {
		t.Fatal("Expected nil result", result)
	}

	if err == nil {
		t.Fatal("Expected error")
	}
}

func TestReceiveError(t *testing.T) {
	raw := TestRawConnection{
		receive: func() ([]byte, error) {
			return nil, fmt.Errorf("Receive error")
		},
	}
	conn := New(raw)

	result, err := conn.Receive()
	if result != nil {
		t.Fatal("Expected nil result", result)
	}

	if err == nil {
		t.Fatal("Expected error")
	}
}

func TestSuccessfulSend(t *testing.T) {
	var payload []byte
	raw := TestRawConnection{
		send: func(p []byte) error {
			if payload != nil {
				t.Fatal("RawConnection.Send called more than once")
			}
			payload = p
			return nil
		},
	}
	conn := New(raw)

	event := realTimeEvent{
		eventType: "test",
		payload: []byte(`{
				"type": "test",
				"body": "this is a test"
			}`),
	}

	err := conn.Send(event)
	if err != nil {
		t.Fatal("Unexpected error", err)
	}

	if !reflect.DeepEqual(payload, event.Payload()) {
		t.Fatal("Payload did not match", event.Payload(), payload)
	}
}

func TestSendError(t *testing.T) {
	raw := TestRawConnection{
		send: func(p []byte) error {
			return fmt.Errorf("Outgoing error")
		},
	}
	conn := New(raw)

	event := realTimeEvent{
		eventType: "test",
		payload:   nil,
{'fraction_non_alphanumeric': 0.16678832116788322, 'fraction_numerical': 0.0018248175182481751, 'mean_word_length': 3.749566724436742, 'pattern_counts': {'":': 14, '<': 8, '<?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': '93139', 'n_tokens_mistral': 2155, 'n_tokens_neox': 1959, 'n_words': 530}