package handlers_test

import (
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

	m "github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
	th "github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestHandleUpdateManifestSUMSGet(t *testing.T) {
	mock := mocks.MockUpdateManifestVersions{}
	services.GetDB().SetUpdateManifestVersions(&mock)
	umvs := []m.SUMSVersion{{Version: "testv1", OSVersion: "1.2.3"}, {Version: "testv2", OSVersion: "1.2.3"}, {Version: "testv3", OSVersion: "1.2.3"}}

	tests := []mocks.DBHttpTest{
		{
			Name:             "Fetch all update manifest versions rows",
			Request:          th.MakeTestRequest(http.MethodGet, "/manifests/versions", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"version":"testv1","os_version":"1.2.3"},{"version":"testv2","os_version":"1.2.3"},{"version":"testv3","os_version":"1.2.3"}],"total":3}`,
			DBTestCase: mocks.DBTestCase{
				SetupMockResponse: func() {
					mock.SelectResponse = umvs
				},
			},
		},
	}

	mocks.RunDBTests(t, tests, handlers.HandleUpdateManifestSUMSGet, &mock)
}
