package websocket_test

import (
	"net/http"
	"testing"

	"github.com/fiskerinc/cloud-services/services/gateway/websocket"

	"github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestParseDeviceAndVersionFromRequest(t *testing.T) {
	req := &http.Request{Header: http.Header{}}
	req.Header.Add("User-Agent", "Fisker T.Rex 1.2.3 [abc123]")

	device, version := websocket.ParseDeviceAndVersionFromRequest(req)
	if device != "fisker" {
		t.Errorf(testhelper.TestErrorTemplate, "TestParseDeviceAndVersionFromRequest", "fisker", device)
	}
	if version != "1.2.3" {
		t.Errorf(testhelper.TestErrorTemplate, "TestParseDeviceAndVersionFromRequest", "1.2.3", version)
	}

	req = &http.Request{Header: http.Header{}}
	req.Header.Add("User-Agent", "HMI 1.2.3.4")

	_, version = websocket.ParseDeviceAndVersionFromRequest(req)
	if version != "1.2.3.4" {
		t.Errorf(testhelper.TestErrorTemplate, "TestParseDeviceAndVersionFromRequest", "1.2.3.4", version)
	}

	req = &http.Request{Header: http.Header{}}
	req.Header.Add("User-Agent", "Fisker T.Rex Ocean 1.2.3 [abc123]")

	device, version = websocket.ParseDeviceAndVersionFromRequest(req)
	if device != "fisker" {
		t.Errorf(testhelper.TestErrorTemplate, "TestParseDeviceAndVersionFromRequest", "fisker", device)
	}
	if version != "1.2.3" {
		t.Errorf(testhelper.TestErrorTemplate, "TestParseDeviceAndVersionFromRequest", "1.2.3", version)
	}
}
