package main

import (
	"testing"

	"github.com/tidwall/gjson"

	"github.com/474420502/hunter"
)

// OpenrecRanking 获取排名任务
type OpenrecRankingTest struct {
	hunter.PreCurlUrl
}

// Execute 执行方法
func (or *OpenrecRankingTest) Execute(cxt *hunter.TaskContext) {

	resp, err := cxt.Hunt()
	if err != nil {
		panic(err)
	}
	t := cxt.GetShare("test").(*testing.T)
	if !gjson.ValidBytes(resp.Content()) {
		t.Error("source is not json format.")
	}
	result := gjson.GetBytes(resp.Content(), "0.rank")
	if result.Int() != 1 {
		t.Error("rank is error. result raw is ", result.Raw)
	}

	if cxt.Workflow().GetQuery().Get("page") != "1" {
		t.Error("workflow page error")
	}
	// t.Error(string(resp.Content()))
}

func TestRanking(t *testing.T) {
	curlBash := `curl 'https://public.openrec.tv/external/api/v5/channel-ranks?period=monthly&date=&tag=&page=1' \
	-H 'authority: public.openrec.tv' \
	-H 'accept: application/json, text/javascript, */*; q=0.01' \
	-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36' \
	-H 'origin: https://www.openrec.tv' \
	-H 'sec-fetch-site: same-site' \
	-H 'sec-fetch-mode: cors' \
	-H 'sec-fetch-dest: empty' \
	-H 'referer: https://www.openrec.tv/ranking' \
	-H 'accept-language: zh-CN,zh;q=0.9' \
	-H 'if-none-match: W/"25edb-aUYBdmLqZcr6DW4ZWKX9r2aqolg"' \
	--compressed`

	ort := &OpenrecRankingTest{}
	ort.PreCurlUrl = hunter.PreCurlUrl(curlBash)
	ht := hunter.NewHunter(ort)
	ht.SetShare("test", t)
	ht.Execute()
}

func TestRankingInsert(t *testing.T) {
	ht := hunter.NewHunter(openrecRanking)
	ht.Execute()
}