package test

import (
	"path/filepath"
	"runtime"
	_ "sws_xcx/routers"
	"sws_xcx/service"
	"testing"

	//beego "github.com/beego/beego/v2/server/web"
	//"github.com/beego/beego/v2/core/logs"
	"github.com/astaxie/beego"
	"github.com/medivhzhan/weapp/v3/auth"
	// "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)
}

// TestGet is a sample to run an endpoint test
func TestGet(t *testing.T) {
	// r, _ := http.NewRequest("GET", "/v1/object", nil)
	// w := httptest.NewRecorder()
	// beego.BeeApp.Handlers.ServeHTTP(w, r)

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

	// Convey("Subject: Test Station Endpoint\n", 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 TestWxappSessionCode(t *testing.T) {

	sdk := service.GetWxSdk()
	wxcli := sdk.NewAuth()

	code := "0d3eVp100qtUES1jgD300t5yN21eVp1t"

	resp, err := wxcli.Code2Session(&auth.Code2SessionRequest{JsCode: code, GrantType: "authorization_code", Appid: beego.AppConfig.String("appid"), Secret: beego.AppConfig.String("appsecret")})
	if err != nil {
		t.Error(err)
		return
	}
	if resp.CommonError.ErrCode != 0 {
		t.Errorf("err code:%v msg:%v", resp.CommonError.ErrCode, resp.CommonError.ErrMSG)
		return
	}
	t.Logf("resp: %+v", *resp)

}
