package test

import (
	"net/http"
	"net/http/httptest"
	"path/filepath"
	"runtime"
	"testing"

	_ "nodemaster/routers"

	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/core/logs"
	. "github.com/smartystreets/goconvey/convey"
)

func init() {
	_, file, _, _ := runtime.Caller(0)
	apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator))))
	beego.TestBeegoInit(apppath)
}

func TestGetNode(t *testing.T) {
	r, _ := http.NewRequest("GET", "/v1/node", nil)
	w := httptest.NewRecorder()
	beego.BeeApp.Handlers.ServeHTTP(w, r)

	logs.Info("testing", "TestGetNode", "Code[%d]\n%s", w.Code, w.Body.String())

	Convey("GET /v1/node", t, func() {
		Convey("Status Code Should Be 200", func() {
			So(w.Code, ShouldEqual, 200)
		})
		Convey("The Result Should Not Be Empty", func() {
			So(w.Body.Len(), ShouldBeGreaterThan, 0)
		})
	})
}

func TestGetNodes(t *testing.T) {
	r, _ := http.NewRequest("GET", "/v1/nodes", nil)
	w := httptest.NewRecorder()
	beego.BeeApp.Handlers.ServeHTTP(w, r)

	logs.Info("testing", "TestGetNodes", "Code[%d]\n%s", w.Code, w.Body.String())

	Convey("GET /v1/nodes", t, func() {
		Convey("Status Code Should Be 200", func() {
			So(w.Code, ShouldEqual, 200)
		})
	})
}

func TestGetServices(t *testing.T) {
	r, _ := http.NewRequest("GET", "/v1/services", nil)
	w := httptest.NewRecorder()
	beego.BeeApp.Handlers.ServeHTTP(w, r)

	logs.Info("testing", "TestGetServices", "Code[%d]\n%s", w.Code, w.Body.String())

	Convey("GET /v1/services", t, func() {
		Convey("Status Code Should Be 200", func() {
			So(w.Code, ShouldEqual, 200)
		})
	})
}

func TestGetNodeBackupTargets(t *testing.T) {
	r, _ := http.NewRequest("GET", "/v1/node/backup/targets", nil)
	w := httptest.NewRecorder()
	beego.BeeApp.Handlers.ServeHTTP(w, r)

	logs.Info("testing", "TestGetNodeBackupTargets", "Code[%d]\n%s", w.Code, w.Body.String())

	Convey("GET /v1/node/backup/targets", t, func() {
		Convey("Status Code Should Be 200", func() {
			So(w.Code, ShouldEqual, 200)
		})
	})
}

func TestGetNodeBackupProgress(t *testing.T) {
	r, _ := http.NewRequest("GET", "/v1/node/backup/progress", nil)
	w := httptest.NewRecorder()
	beego.BeeApp.Handlers.ServeHTTP(w, r)

	logs.Info("testing", "TestGetNodeBackupProgress", "Code[%d]\n%s", w.Code, w.Body.String())

	Convey("GET /v1/node/backup/progress", t, func() {
		Convey("Status Code Should Be 200", func() {
			So(w.Code, ShouldEqual, 200)
		})
	})
}
