package tests

import (
	"fmt"
	"github.com/fiskerinc/cloud-services/services/jetfire/handlers"
	"github.com/fiskerinc/cloud-services/services/jetfire/services"
	"github.com/fiskerinc/cloud-services/services/jetfire/utils"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/grpc/kafka_grpc"
	"github.com/stretchr/testify/assert"
)

func TestHandleSignalMessage(t *testing.T) {
	cache := services.GetVehicleCache()

	ptrArray := make([]*kafka_grpc.GRPC_CANSignal, len(testCanSignalBatch))
	for i := range testCanSignalBatch {
		ptrArray[i] = &testCanSignalBatch[i]
	}

	services.ResetCacheVars()

	cache = services.GetVehicleCache()
	cache.Clear()

	handlers.HandleSignalBatch(ptrArray, cache, nil)

	assert.Equal(t, len(cache.Cache), 1)

	// testing large timestamp gap; trigger new trip
	state := cache.Cache[testVIN]
	tripStartTime := utils.FloatToTime(1511.0)
	assert.WithinDuration(t, state.TripStart, tripStartTime, 1e8)
	assert.Equal(t, state.TripID, fmt.Sprintf("%s_%d", testVIN, 1511))
	assert.WithinDuration(t, state.Timestamp, utils.FloatToTime(1512.0), 1e8)
	assert.Equal(t, state.StateValues["BMS_PwrBattRmngCpSOC"], 45.0)
	assert.Equal(t, state.StateValues["ESP_VehSpd"], 11.0)
}
