package noxy

import (
	"bytes"
	"testing"
	"testing/iotest"
)

// ErrorWriter always returns Err on Write calls.
type ErrorWriter struct {
	Err error
}

func (ew ErrorWriter) Write([]byte) (int, error) {
	return 0, ew.Err
}

func TestSinkTeeReader(t *testing.T) {
	const text = "hello"
	var w bytes.Buffer
	tee := SinkTeeReader(bytes.NewBufferString(text), &w)

	if err := iotest.TestReader(tee, []byte(text)); err != nil {
		t.Errorf("tee reader: %v", err)
	}
	if v := string(w.Bytes()); v != text {
		t.Errorf("b2 = %q; want %q", v, text)
	}
}

func TestSinkTeeReaderErrWriter(t *testing.T) {
	const text = "hello"
	tee := SinkTeeReader(bytes.NewBufferString(text), ErrorWriter{iotest.ErrTimeout})
	if err := iotest.TestReader(tee, []byte(text)); err != nil {
		t.Errorf("tee reader: %v", err)
	}
}