package protocol

import "bytes"
import "testing"

func TestMessageLogin (test *testing.T) {
	conduit := new(bytes.Buffer)
	message := MessageLogin {
		ID: 0,
		Version: Version { 1, 3 },
		User: "SomeUser",
		Key: []byte { 100, 2, 49 },
		Name: "SomeName",
		Description: "SomeDescription",
		Scheme: "SomeScheme",
		Host: "SomeHost",
		Path: "SomePath",
	}
	message.Send(conduit)

	ty, err := ReadType(conduit)
	if err != nil       { test.Fatal("when reading type code:", err) }
	if ty  != TypeLogin { test.Fatal("expected", TypeLogin, "got", ty) }

	got, err := ReadMessageLogin(conduit)
	if err != nil { test.Fatal("when reading message:", err) }

	equal :=
		message.ID          == got.ID          &&
		message.Version     == got.Version     &&
		message.User        == got.User        &&
		sliceEqual(message.Key, got.Key)       &&
		message.Name        == got.Name        &&
		message.Description == got.Description &&
		message.Scheme      == got.Scheme      &&
		message.Host        == got.Host        &&
		message.Path        == got.Path
	
	if !equal { test.Fatal("structures not equal") }

	if conduit.Len() != 0 {
		test.Fatal("buffer len", conduit.Len(), "instead of 0")
	}
}

func TestMessageStatus (test *testing.T) {
	conduit := new(bytes.Buffer)
	message := MessageStatus {
		ID: 0,
		Status: StatusBadMount,
	}
	message.Send(conduit)

	ty, err := ReadType(conduit)
	if err != nil        { test.Fatal("when reading type code:", err) }
	if ty  != TypeStatus { test.Fatal("expected", TypeStatus, "got", ty) }

	got, err := ReadMessageStatus(conduit)
	if err != nil { test.Fatal("when reading message:", err) }

	equal :=
		message.ID     == got.ID     &&
		message.Status == got.Status
	
	if !equal { test.Fatal("structures not equal") }

	if conduit.Len() != 0 {
		test.Fatal("buffer len", conduit.Len(), "instead of 0")
	}
}
