<commit_before>package sse_test

import (
	"bytes"
	"io"
	"testing"

	"github.com/mubit/sse"
	"github.com/stretchr/testify/assert"
)

// Extracts decoder from a string
func newDecoder(data string) sse.Decoder {
	reader := bytes.NewReader([]byte(data))
	return sse.NewDecoder(reader)
}

func TestEOFIsReturned(t *testing.T) {
	decoder := newDecoder("")
	_, err := decoder.Decode()
	assert.Equal(t, io.EOF, err)
}

func TestEventNameAndData(t *testing.T) {
	decoder := newDecoder("event: some event\r\ndata: some event value\r\n\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "some event", ev.Name())
	assert.Equal(t, "some event value", string(ev.Data()))
}

func TestEventNameAndDataManyEvents(t *testing.T) {
	decoder := newDecoder("event: first event\r\ndata: first value\r\n\nevent: second event\r\ndata: second value\r\n\n")

	ev1, err := decoder.Decode()
	assert.NotNil(t, ev1)
	assert.Nil(t, err)
	assert.Equal(t, "first event", ev1.Name())
	assert.Equal(t, "first value", string(ev1.Data()))

	ev2, err := decoder.Decode()
	assert.NotNil(t, ev2)
	assert.Nil(t, err)
	assert.Equal(t, "second event", ev2.Name())
	assert.Equal(t, "second value", string(ev2.Data()))
}

func TestStocksExample(t *testing.T) {
	decoder := newDecoder("data: YHOO\ndata: +2\ndata: 10\n\n")
	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "YHOO\n+2\n10", string(ev.Data()))
}

func TestFirstWhitespaceIsIgnored(t *testing.T) {
	decoder := newDecoder("data: first\n\ndata: second\n\n")

	ev1, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "first", string(ev1.Data()))

	ev2, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "second", string(ev2.Data()))
}

func TestOnlyOneWhitespaceIsIgnored(t *testing.T) {
	decoder := newDecoder("data:   first\n\n") // 3 whitespaces
	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "  first", string(ev.Data())) // 2 whitespaces
}

func TestEventsWithNoDataThenWithNewLine(t *testing.T) {
	decoder := newDecoder("data\n\ndata\ndata\n\ndata:")

	ev1, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "", string(ev1.Data()))

	ev2, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "\n", string(ev2.Data()))
}

func TestCommentIsIgnoredAndDataIsNot(t *testing.T) {
	decoder := newDecoder(": test stream\n\ndata: first event\nid: 1\n\ndata:second event\nid\n\ndata:  third event\n\n")

	ev1, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "1", ev1.ID())
	assert.Equal(t, "first event", string(ev1.Data()))

	ev2, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "", ev2.ID())
	assert.Equal(t, "second event", string(ev2.Data()))

	ev3, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "", ev3.ID())
	assert.Equal(t, " third event", string(ev3.Data()))
}

func TestOneLineDataParseWithDoubleRN(t *testing.T) {
	decoder := newDecoder("data: this is a test\r\n\r\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "this is a test", string(ev.Data()))
}

func TestOneLineDataParseWithoutDoubleRN(t *testing.T) {
	decoder := newDecoder("data: this is a test\r\n\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "this is a test", string(ev.Data()))
}

func TestTwoLinesDataParseWithRNAndDoubleRN(t *testing.T) {
	decoder := newDecoder("data: this is \r\ndata: a test\r\n\r\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "this is \na test", string(ev.Data()))
}

func TestNewLineWithCR(t *testing.T) {
	decoder := newDecoder("event: name\ndata: some\rdata:  data\r\n\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "name", ev.Name())
	assert.Equal(t, "some\n data", string(ev.Data()))
}<commit_msg>decoder: add a test to demonstrate that issue #4 is fixed. }<commit_after>package sse_test

import (
	"bytes"
	"io"
	"testing"

	"github.com/mubit/sse"
	"github.com/stretchr/testify/assert"
)

// Extracts decoder from a string
func newDecoder(data string) sse.Decoder {
	reader := bytes.NewReader([]byte(data))
	return sse.NewDecoder(reader)
}

func TestEOFIsReturned(t *testing.T) {
	decoder := newDecoder("")
	_, err := decoder.Decode()
	assert.Equal(t, io.EOF, err)
}

func TestEventNameAndData(t *testing.T) {
	decoder := newDecoder("event: some event\r\ndata: some event value\r\n\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "some event", ev.Name())
	assert.Equal(t, "some event value", string(ev.Data()))
}

func TestEventNameAndDataManyEvents(t *testing.T) {
	decoder := newDecoder("event: first event\r\ndata: first value\r\n\nevent: second event\r\ndata: second value\r\n\n")

	ev1, err := decoder.Decode()
	assert.NotNil(t, ev1)
	assert.Nil(t, err)
	assert.Equal(t, "first event", ev1.Name())
	assert.Equal(t, "first value", string(ev1.Data()))

	ev2, err := decoder.Decode()
	assert.NotNil(t, ev2)
	assert.Nil(t, err)
	assert.Equal(t, "second event", ev2.Name())
	assert.Equal(t, "second value", string(ev2.Data()))
}

func TestStocksExample(t *testing.T) {
	decoder := newDecoder("data: YHOO\ndata: +2\ndata: 10\n\n")
	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "YHOO\n+2\n10", string(ev.Data()))
}

func TestFirstWhitespaceIsIgnored(t *testing.T) {
	decoder := newDecoder("data: first\n\ndata: second\n\n")

	ev1, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "first", string(ev1.Data()))

	ev2, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "second", string(ev2.Data()))
}

func TestOnlyOneWhitespaceIsIgnored(t *testing.T) {
	decoder := newDecoder("data:   first\n\n") // 3 whitespaces
	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "  first", string(ev.Data())) // 2 whitespaces
}

func TestEventsWithNoDataThenWithNewLine(t *testing.T) {
	decoder := newDecoder("data\n\ndata\ndata\n\ndata:")

	ev1, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "", string(ev1.Data()))

	ev2, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "\n", string(ev2.Data()))
}

func TestCommentIsIgnoredAndDataIsNot(t *testing.T) {
	decoder := newDecoder(": test stream\n\ndata: first event\nid: 1\n\ndata:second event\nid\n\ndata:  third event\n\n")

	ev1, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "1", ev1.ID())
	assert.Equal(t, "first event", string(ev1.Data()))

	ev2, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "", ev2.ID())
	assert.Equal(t, "second event", string(ev2.Data()))

	ev3, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "", ev3.ID())
	assert.Equal(t, " third event", string(ev3.Data()))
}

func TestOneLineDataParseWithDoubleRN(t *testing.T) {
	decoder := newDecoder("data: this is a test\r\n\r\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "this is a test", string(ev.Data()))
}

func TestOneLineDataParseWithoutDoubleRN(t *testing.T) {
	decoder := newDecoder("data: this is a test\r\n\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "this is a test", string(ev.Data()))
}

func TestTwoLinesDataParseWithRNAndDoubleRN(t *testing.T) {
	decoder := newDecoder("data: this is \r\ndata: a test\r\n\r\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "this is \na test", string(ev.Data()))
}

func TestNewLineWithCR(t *testing.T) {
	decoder := newDecoder("event: name\ndata: some\rdata:  data\r\n\n")

	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "name", ev.Name())
	assert.Equal(t, "some\n data", string(ev.Data()))
}

// Bug #4: decoder: pure CR not recognized as end of line #4
func TestPureLineFeedsWithCarriageReturn(t *testing.T) {
	decoder := newDecoder("event: name\rdata: some\rdata:  data\r\r")
	ev, err := decoder.Decode()
	assert.Nil(t, err)
	assert.Equal(t, "name", ev.Name())
	assert.Equal(t, "some\n data", string(ev.Data()))
}
{'fraction_non_alphanumeric': 0.1914216622120402, 'fraction_numerical': 0.009291078019600356, 'mean_word_length': 4.690079652425778, 'pattern_counts': {'":': 2, '<': 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': '5157167', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3075, 'n_words': 649}