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("ads_content=a827e6443149663e6d934392fffe24211c7895c5de901c79d0728f903c3aba6943df34363ebfe388dbb70bb824bd10dbed8504e66ed20070d7d5865ee17bd3caf7161039a7b0db08995ff9f9adcbfcd71b688a3c63d5f2ba88b737c4d1c5b016a5328a7df15b494d0c8afd34fd2798226ed9e1c8b2c6203b8ec3440f9df573a5fb093d4f5bf4130e99c9c55fe6cee5fbec2eeaf1d8315919ed1ff0d0490f4ff88d87d6a8094bfa18aa35f0d7349a633e1ea65297e9817b81cf35628fb4695e78bcd58eeb4ed645e2f7188c662727a98d0b587378bc973c92f8c566ea644e0cfe66e41ba11b0dfe8075b5ff8ce82f9e2d81ee7d99eefe9a899c790f8fe21eb30bca124429ae41a9dc95166e2a84763634de63ba01e53ce0f0788112558d16c08c40d324b910284f633d66d55cbea2dcbdc16f16c8f08f621d0a1b665550d3fb282b5c0b9bc54bd1c3b8a7f900a92acc560c452af6b8f9e13f1fd4fbcdc42e2ca110b471d708e63fda33f306c95acb96b15ea75fafbbeba979ddf7ccf91f777ed5bb810f493338ed4436c3ccebcb993b4cd8dd46610f29ec8d9ba6390f0a4f2821&new_ads_flag=1")
	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("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()))
	if err != nil {
		fmt.Printf("setup redis failed: %s\n", err)
		return
	}

	adslib.GetConf()

	router := setupRouter()

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

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