// MIT License
//
// Copyright (c) 2020 Alex Howarth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package tilt

import (
	"errors"
	"testing"
)

func TestNewIBeacon(t *testing.T) {
	tt := []struct {
		name  string
		data  []byte
		err   error
		uuid  string
		major uint16
		minor uint16
	}{
		{
			name:  "Valid iBeacon",
			data:  []uint8{0x4c, 0x0, 0x2, 0x15, 0xa4, 0x95, 0xbb, 0x30, 0xc5, 0xb1, 0x4b, 0x44, 0xb5, 0x12, 0x13, 0x70, 0xf0, 0x2d, 0x74, 0xde, 0x0, 0x46, 0x3, 0xfc, 0xc5},
			err:   nil,
			uuid:  "a495bb30c5b14b44b5121370f02d74de",
			major: 70,
			minor: 1020,
		},
		{
			name: "Invalid iBeacon",
			data: []uint8{0x4c, 0x0, 0x2, 0x15},
			err:  ErrNotBeacon,
		},
	}

	for _, tc := range tt {
		t.Run(tc.name, func(t *testing.T) {
			got, err := NewIBeacon(tc.data)

			if tc.err != nil {
				// expecting an error
				if !errors.Is(err, tc.err) {
					t.Fatalf("Expected '%v' error, got '%v' error", tc.err, err)
				}
				return
			}
			if got.UUID != tc.uuid {
				t.Errorf("Expected %v, got %v", tc.uuid, got.UUID)
			}
			if got.Major != tc.major {
				t.Errorf("Expected %v, got %v", tc.major, got.Major)
			}
			if got.Minor != tc.minor {
				t.Errorf("Expected %v, got %v", tc.minor, got.Minor)
			}
		})
	}
}
