package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
	"github.com/rafaeljusto/redigomock"
	"github.com/stretchr/testify/assert"
	"miads/adslib"
	"miads/adslib/ads_redis"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"
)

// func TestAds(t *testing.T) {
// 	router := setupRouter()
//
// 	w := httptest.NewRecorder()
// 	req, _ := http.NewRequest("POST", "/ads?req_source=mh&network_type=9&imei=867252039444772&idfa=&ip=182.37.81.64&ua=Mozilla%2F5.0+%28Linux%3B+Android+8.1.0%3B+MI+8+Build%2FOPM1.171019.026%3B+wv%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Version%2F4.0+Chrome%2F68.0.3440.91+Mobile+Safari%2F537.36&model=MI+8&brand=Xiaomi&android_id=867252039444772&platform=1", nil)
// 	router.ServeHTTP(w, req)
//
// 	assert.Equal(t, 200, w.Code)
// 	assert.Equal(t, "pong", w.Body.String())
// }

func TestDecrypt(t *testing.T) {
	router := setupRouter()

	w := httptest.NewRecorder()
	r := strings.NewReader("")
	req, _ := http.NewRequest("POST", "/ads", r)
	router.ServeHTTP(w, req)

	assert.Equal(t, 200, w.Code)
	assert.Equal(t, "pong", w.Body.String())
}

func TestAdsHandle(t *testing.T) {
	err := ads_redis.Setup()
	assert.Nil(t, err)

	conn := redigomock.NewConn()
	ads_redis.RedisConn = &redis.Pool{
		// Return the same connection mock for each Get() call.
		Dial:    func() (redis.Conn, error) { return conn, nil },
		MaxIdle: 10,
	}

	conn.Command("SISMEMBER", "arsi", "zhiku").Expect([]byte("true"))
	conn.Command("SISMEMBER", "arsi", "kuxin").Expect([]byte("true"))
	conn.Command("SISMEMBER", "ads_black_ip", "183.210.200.149").Expect([]byte("false"))
	conn.Command("GET", "ads_city_常州市").Expect("1156320400")
	conn.Command("GET", "acfv2_{kuxin}_{ads_click}").Expect("{\"channel_flag\": 1, \"weigth\": 100}")
	conn.Command("GET", "acfv2_{kuxin02}_{ads_click}").Expect("{\"channel_flag\": 1, \"weigth\": 100}")
	conn.Command("GET", "adsv2_xiaomi_0").Expect(int64(784))
	conn.Command("GET", "adsv3_xiaomi_VIEW_0").Expect(int64(0))
	conn.Command("GET", "adsv3_xiaomi_VIDEO_TIMER_0").Expect(int64(215))
	conn.Command("GET", "adsv3_xiaomi_CLICK_0").Expect(int64(24))
	conn.Command("GET", "adsv3_xiaomi_VIDEO_FINISH_0").Expect(int64(760))
	conn.Command("GET", "dim2_72710e3df592f07f6dbcc04b747f3653_'223.91.87.241'").Expect(int64(time.Now().Unix()))
	conn.Command("GET", "rsfreq_kuxin").Expect("[{\"freq_time\": 60, \"end_hour\": 23, \"begin_hour\": 0}]")
	conn.Command("GET", "adip_183210200149").Expect("1592485922")
	if err != nil {
		fmt.Printf("setup redis failed: %s\n", err)
		return
	}

	adslib.GetConf()

	router := setupRouter()

	w := httptest.NewRecorder()
	r := strings.NewReader("imei=868900043181108&mac=54:BA:D6:A5:EA:A1&os_version=9&ip=183.210.200.149&ua=Mozilla%2F5.0+%28Linux%3B+Android+9%3B+JKM-AL00+Build%2FHUAWEIJKM-AL00%3B+wv%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Version%2F4.0+Chrome%2F74.0.3729.136+Mobile+Safari%2F537.36&model=JKM-AL00&brand=HUAWEI&android_id=cf2447a8bd2e59f1&platform=1&req_source=kuxin&network_type=9&screen_size=2137x1080")
	req, _ := http.NewRequest("POST", "/ads", r)
	router.ServeHTTP(w, req)

	//assert.Equal(t, 200, w.Code)
	assert.Equal(t, "pong", w.Body.String())
}
