package handlers_test

import (
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

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

func TestHandleCarUpdatesStatuses(t *testing.T) {
	redis.MockRedisConnection()
	services.SetRedisClientPool(tester.NewMockClientPool())
	tests := []th.BasicHttpTest{
		{
			Name:             "Missing query",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/carupdatesstatuses", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"car update ids required","error":"Bad Request"}`,
		},
		{
			Name:             "Bad car update ids",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/carupdatesstatuses?carupdateids=XXXXXXXXX", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"invalid id XXXXXXXXX","error":"Bad Request"}`,
		},
		{
			Name:             "Good request",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/carupdatesstatuses?carupdateids=100,101,102", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"statuses":[{"file_current":0,"file_total":0,"package_current":0,"package_total":0,"installed":0,"total_files":0,"car_update_id":0,"ecu":"","msg":"","err":0},{"file_current":0,"file_total":0,"package_current":0,"package_total":0,"installed":0,"total_files":0,"car_update_id":0,"ecu":"","msg":"","err":0},{"file_current":0,"file_total":0,"package_current":0,"package_total":0,"installed":0,"total_files":0,"car_update_id":0,"ecu":"","msg":"","err":0}]}`,
		},
	}

	th.RunBasicHttpTests(t, tests, handlers.HandleCarUpdatesStatuses)
}
