/*
NAME
  metaEncode_test.go

DESCRIPTION
  See Readme.md

AUTHOR
  Saxon Nelson-Milton <saxon@ausocean.org>

LICENSE
  metaEncode_test.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)

  It is free software: you can redistribute it and/or modify them
  under the terms of the GNU General Public License as published by the
  Free Software Foundation, either version 3 of the License, or (at your
  option) any later version.

  It is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 for more details.

  You should have received a copy of the GNU General Public License
  along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/

package mts

import (
	"bytes"
	"testing"

	"bitbucket.org/ausocean/av/stream/mts/meta"
	"bitbucket.org/ausocean/av/stream/mts/psi"
)

const (
	errNotExpectedOut = "Unexpected output. \n Got : %v\n, Want: %v\n"
	errUnexpectedErr  = "Unexpected error: %v\n"
)

const fps = 25

// TestMetaEncode1 checks that we can externally add a single metadata entry to
// the mts global Meta meta.Data struct and then successfully have the mts encoder
// write this to psi.
func TestMetaEncode1(t *testing.T) {
	Meta = meta.New()
	var b []byte
	buf := bytes.NewBuffer(b)
	e := NewEncoder(buf, fps)
	Meta.Add("ts", "12345678")
	if err := e.writePSI(); err != nil {
		t.Errorf(errUnexpectedErr, err.Error())
	}
	out := buf.Bytes()
	got := out[PacketSize+4:]

	want := []byte{
		0x00, 0x02, 0xb0, 0x23, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x11,
		psi.MetadataTag,                                                               // Descriptor tag
		0x0f,                                                                          // Length of bytes to follow
		0x00, 0x10, 0x00, 0x0b, 't', 's', '=', '1', '2', '3', '4', '5', '6', '7', '8', // timestamp
		0x1b, 0xe1, 0x00, 0xf0, 0x00,
	}
	want = psi.AddCrc(want)
	want = psi.AddPadding(want)
	if !bytes.Equal(got, want) {
		t.Errorf(errNotExpectedOut, got, want)
	}
}

// TestMetaEncode2 checks that we can externally add two metadata entries to the
// Meta meta.Data global and then have the mts encoder successfully encode this
// into psi.
func TestMetaEncode2(t *testing.T) {
	Meta = meta.New()
	var b []byte
	buf := bytes.NewBuffer(b)
	e := NewEncoder(buf, fps)
	Meta.Add("ts", "12345678")
	Meta.Add("loc", "1234,4321,1234")
	if err := e.writePSI(); err != nil {
		t.Errorf(errUnexpectedErr, err.Error())
	}
	out := buf.Bytes()
	got := out[PacketSize+4:]
	want := []byte{
		0x00, 0x02, 0xb0, 0x36, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x24,
		psi.MetadataTag,                                                                     // Descriptor tag
		0x22,                                                                                // Length of bytes to follow
		0x00, 0x10, 0x00, 0x1e, 't', 's', '=', '1', '2', '3', '4', '5', '6', '7', '8', '\t', // timestamp
		'l', 'o', 'c', '=', '1', '2', '3', '4', ',', '4', '3', '2', '1', ',', '1', '2', '3', '4', // location
		0x1b, 0xe1, 0x00, 0xf0, 0x00,
	}
	want = psi.AddCrc(want)
	want = psi.AddPadding(want)
	if !bytes.Equal(got, want) {
		t.Errorf(errNotExpectedOut, got, want)
	}
}