package webset_test

import (
	"fmt"
	"fusenapi/constants"
	fstests "fusenapi/utils/fstests"
	"testing"

	"github.com/474420502/requests"
	"github.com/tidwall/gjson"
)

func TestWebSetLogic(t *testing.T) {
	var err error
	var resp *requests.Response
	var result gjson.Result

	// constants.FONT_CONFIG ? 这个还没有测试的例子
	testTypes := []constants.TypeWebSet{constants.POLICY_CONFIG, constants.CLAUSE_CONFIG, constants.FAQ_CONFIG}

	// 获取 session,并携带 JWT token
	ses := fstests.GetSesssion()
	tp := ses.Get(fmt.Sprintf("http://%s:%d/api/web-set/setting", cnf.Host, cnf.Port))

	for _, tType := range testTypes {
		tp.QueryParam("type").Set(tType)
		// log.Println(tp.GetRawURL())
		// 向服务器发送 GET 请求,获取 cookie 信息
		resp, err = tp.TestExecute(gserver)
		if err != nil {
			t.Error(err)
		}

		// 使用 gjson 解析返回的 json 数据
		result = resp.Json() // gjson

		// log.Println(result)
		// 检查返回值中的 code 字段是否存在,并且值是否为 200
		result = result.Get("code")
		if !result.Exists() {
			t.Error("code is not exists")
		}
		if result.Int() != 200 {
			t.Error("code != 200")
		}

		// 检查返回值中的 msg 字段是否存在,并且值是否为 "success"
		result = resp.Json().Get("msg")
		if !result.Exists() {
			t.Error("msg is not exists")
		}
		if result.String() != "success" {
			t.Error(result.String())
		}

		// 检查返回值中的 data 字段是否存在
		result = resp.Json().Get("data")
		if !result.Exists() {
			t.Error("data is not exists")
		}

		// 根据data的不同,分别校验
		intro := result.Get("introduction")
		if intro.Exists() {
			// 如果有introduction,则校验introduction和list
			if !intro.Exists() {
				t.Error("introduction is not exists")
			}
			list := result.Get("list")
			if !list.Exists() {
				t.Error("list is not exists")
			}
			// ...
		} else {
			// 如果没有introduction,则校验icon、items和title
			icon := result.Get("icon")
			if !icon.Exists() {
				t.Error("icon is not exists")
			}
			items := result.Get("items")
			if !items.Exists() {
				t.Error("items is not exists")
			}
			title := result.Get("title")
			if !title.Exists() {
				t.Error("title is not exists")
			}
			// ...
		}
	}

}