package main

import (
	"log"
	"testing"
)

func TestGetMaterialNewsList(t *testing.T) {
	SetupConfig()
	l := getNewList()
	log.Printf("news is : %s", l)
	nl := unmarshalNewsList(l)
	for k, v := range nl.Items {
		log.Printf("%d - message \n", k)
		log.Printf("title=%s", v.Content.NewsItem[0].Title)
	}

}
func TestGetMaterialVoiceList(t *testing.T) {
	SetupConfig()
	l := getVoiceList()
	log.Printf("voice is : %s", l)
}

func TestGetMaterialVideoList(t *testing.T) {
	SetupConfig()
	l := getVideoList()
	log.Printf("voice is : %s", l)
}

func TestGetMaterialImageList(t *testing.T) {
	SetupConfig()
	l := getImageList()
	log.Printf("voice is : %s", l)

}
func TestGetMaterialCount(t *testing.T) {
	SetupConfig()
	mc, err := getMaterialCount()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Voice: %d", mc.VoiceCount)
	log.Printf("Video: %d", mc.VideoCount)
	log.Printf("Image: %d", mc.ImageCount)
	log.Printf("News : %d", mc.NewsCount)
}

func TestUnmarshalImageList(t *testing.T) {
	msg := `
{"item": [
        {
            "media_id": "e2iNEiSxCX5TV1WbFd0TQCOZJ6FrmbKe7sjUrIZnkW0",
            "name": "hit_logo.jpg",
            "update_time": 1494466025,
            "url": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg"
        },
        {
            "media_id": "e2iNEiSxCX5TV1WbFd0TQOo3v5NmyZKIzG6erS8Heak",
            "name": "hit_font.jpg",
            "update_time": 1494466018,
            "url": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUvogs2hibSk6eqlcYZdZj3d5DLYMj4xAFuD9rp4e0s3O8B46Hgb6mETw\/0?wx_fmt=jpeg"
        },
        {
            "media_id": "e2iNEiSxCX5TV1WbFd0TQP9e-7cyXtbHoxptztJgz68",
            "name": "logo-gold-964x640.png",
            "update_time": 1492696182,
            "url": "http:\/\/mmbiz.qpic.cn\/mmbiz_png\/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw\/0?wx_fmt=png"
        }
    ],
    "total_count": 30,
    "item_count": 3
}`
	r := unmarshalImageList(msg)
	AssertEqual(t, r.TotalCount, 30, "Total count should be 30")
	AssertEqual(t, r.ItemCount, 3, "Item count should be 3")
	//individual items
	AssertEqual(t, r.Items[0].MediaID, "e2iNEiSxCX5TV1WbFd0TQCOZJ6FrmbKe7sjUrIZnkW0", "Item[0] media id mismatch")
	AssertEqual(t, r.Items[0].Name, "hit_logo.jpg", "Item[0].name expect [hit_logo.jpg] not ["+r.Items[0].Name+"]")
	AssertEqual(t, r.Items[0].UpdateTime, 1494466025, "Item[0] updatetime wrong")
	AssertEqual(t, r.Items[0].URL, "http://mmbiz.qpic.cn/mmbiz_jpg/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g/0?wx_fmt=jpeg", "Item[0] URL wrong")

	AssertEqual(t, r.Items[1].MediaID, "e2iNEiSxCX5TV1WbFd0TQOo3v5NmyZKIzG6erS8Heak", "Item[1] media id mismatch")
	AssertEqual(t, r.Items[1].Name, "hit_font.jpg", "Item[1].name expect [hit_font.jpg] not ["+r.Items[0].Name+"]")
	AssertEqual(t, r.Items[1].UpdateTime, 1494466018, "Item[1] updatetime wrong")
	AssertEqual(t, r.Items[1].URL, "http://mmbiz.qpic.cn/mmbiz_jpg/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUvogs2hibSk6eqlcYZdZj3d5DLYMj4xAFuD9rp4e0s3O8B46Hgb6mETw/0?wx_fmt=jpeg", "Item[1] URL wrong")

	AssertEqual(t, r.Items[2].MediaID, "e2iNEiSxCX5TV1WbFd0TQP9e-7cyXtbHoxptztJgz68", "Item[2] media id mismatch")
	AssertEqual(t, r.Items[2].Name, "logo-gold-964x640.png", "Item[2].name expect [logo-gold-964x640.png] not ["+r.Items[0].Name+"]")
	AssertEqual(t, r.Items[2].UpdateTime, 1492696182, "Item[2] update time mismatch ")
	AssertEqual(t, r.Items[2].URL, "http://mmbiz.qpic.cn/mmbiz_png/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw/0?wx_fmt=png", "Item[2] URL wrong")

}

func TestUnmarshalNewsList(t *testing.T) {
	msg := `
{
    "item": [
        {
            "media_id": "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw",
            "content": {
                "news_item": [
                    {
                        "title": "新人入会前几周推荐",
                        "author": "abc",
                        "digest": "excerpt",
                        "content": "<p>&nbsp;&nbsp;&nbsp;&nbsp;新人入会，前几周推荐做如下几件事情<\/p><p><img data-s=\"300,640\" data-type=\"jpeg\" data-src=\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg\" data-ratio=\"0.9035087719298246\" data-w=\"342\"  \/><\/p><p><br  \/><\/p>",
                        "content_source_url": "",
                        "thumb_media_id": "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM",
                        "show_cover_pic": 0,
                        "url": "http:\/\/mp.weixin.qq.com\/s?__biz=MzUxMDAzNDAwMA==&mid=100000008&idx=1&sn=cbfd3f5f5367ca67f9a1184eb394f788&chksm=790868374e7fe121ac5e1d27f6d082c3f8e8033eb4e7158615d0a69dc76ac2971e5650b28fac#rd",
                        "thumb_url": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg",
                        "need_open_comment": 0,
                        "only_fans_can_comment": 0
                    }
                ],
                "create_time": 1494488254,
                "update_time": 1494488268
            },
            "update_time": 1494488268
        },
        {
            "media_id": "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE",
            "content": {
                "news_item": [
                    {
                        "title": "测试消息",
                        "author": "孙鹏",
                        "digest": "加入校友会",
                        "content": "<p>加入校友会<\/p><p><img data-s=\"300,640\" data-type=\"png\" data-src=\"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw\/0?wx_fmt=png\" data-ratio=\"0.6765327695560254\" data-w=\"946\"  \/><\/p><p><br  \/><\/p>",
                        "content_source_url": "http:\/\/www.hitxy.org.au\/join",
                        "thumb_media_id": "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM",
                        "show_cover_pic": 0,
                        "url": "http:\/\/mp.weixin.qq.com\/s?__biz=MzUxMDAzNDAwMA==&mid=100000002&idx=1&sn=723bf16b52f88345e5a73ccde1c79bdb&chksm=7908683d4e7fe12bcaefa9c563809db5546ab97af7184ec648f20930fab5c1d95ee825409c0d#rd",
                        "thumb_url": "http:\/\/mmbiz.qpic.cn\/mmbiz_png\/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw\/0?wx_fmt=png",
                        "need_open_comment": 0,
                        "only_fans_can_comment": 0
                    }
                ],
                "create_time": 1492696196,
                "update_time": 1492696243
            },
            "update_time": 1492696243
        }
    ],
    "total_count": 20,
    "item_count": 2
}	
	`
	r := unmarshalNewsList(msg)
	AssertEqual(t, r.TotalCount, 20, "Total count should be 20")
	AssertEqual(t, r.ItemCount, 2, "Item count should be 2")
	cnt := len(r.Items)
	AssertEqual(t, cnt, 2, "")

	//first item
	item := r.Items[0]
	AssertEqual(t, item.MediaID, "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw", "Item[0] media ID wrong")
	AssertEqual(t, item.UpdateTime, 1494488268, "Item[0] update time wrong")
	AssertEqual(t, item.Content.CreateTime, 1494488254, "Item[0].Content Create time wrong")
	AssertEqual(t, item.Content.UpdateTime, 1494488268, "Item[0].Content Update time wrong")
	AssertEqual(t, item.Content.NewsItem[0].Title, "新人入会前几周推荐", "Item[0].Content.NewsItem[0].Title mismatch")
	AssertEqual(t, item.Content.NewsItem[0].Author, "abc", "Item[0].Content.NewsItem[0].Author should be abc")
	AssertEqual(t, item.Content.NewsItem[0].Digest, "excerpt", "Item[0].Content.NewsItem[0].Digest shold be excerpt")
	AssertEqual(t, item.Content.NewsItem[0].Content, "<p>&nbsp;&nbsp;&nbsp;&nbsp;新人入会，前几周推荐做如下几件事情</p><p><img data-s=\"300,640\" data-type=\"jpeg\" data-src=\"http://mmbiz.qpic.cn/mmbiz_jpg/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g/0?wx_fmt=jpeg\" data-ratio=\"0.9035087719298246\" data-w=\"342\"  /></p><p><br  /></p>", "Item[0].Content.NewsItem[0].Content wrong")
	AssertEqual(t, item.Content.NewsItem[0].ContentSourceURL, "", "Item[0].Content.NewsItem[0].ContentSourceURL wrong")
	AssertEqual(t, item.Content.NewsItem[0].ThumbMediaID, "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM", "Item[0].Content.NewsItem[0].ThumbMediaID wrong")
	AssertEqual(t, item.Content.NewsItem[0].ShowCoverPic, 0, "Item[0].Content.NewsItem[0].ShowCoverPIC should be 0")
	AssertEqual(t, item.Content.NewsItem[0].URL, "http://mp.weixin.qq.com/s?__biz=MzUxMDAzNDAwMA==&mid=100000008&idx=1&sn=cbfd3f5f5367ca67f9a1184eb394f788&chksm=790868374e7fe121ac5e1d27f6d082c3f8e8033eb4e7158615d0a69dc76ac2971e5650b28fac#rd", "Article URL is not right")
	AssertEqual(t, item.Content.NewsItem[0].ThumbURL, "http://mmbiz.qpic.cn/mmbiz_jpg/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g/0?wx_fmt=jpeg", "Article Thumb URL wrong")
	AssertEqual(t, item.Content.NewsItem[0].NeedOpenComment, 0, "Need Open Comment should be 0")
	AssertEqual(t, item.Content.NewsItem[0].OnlyFansCanComment, 0, "OnlyFansCanComment should be 0")

	//second item
	item = r.Items[1]
	AssertEqual(t, item.MediaID, "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE", "Item[1] media ID wrong")
	AssertEqual(t, item.UpdateTime, 1492696243, "Item[1] update time wrong")
	AssertEqual(t, item.Content.CreateTime, 1492696196, "Item[1].Content.CreateTime wrong")
	AssertEqual(t, item.Content.UpdateTime, 1492696243, "Item[1].Content.Update time wrong")
	AssertEqual(t, item.Content.NewsItem[0].Title, "测试消息", "Item[1].NewsItem[0].Title wrong")
	AssertEqual(t, item.Content.NewsItem[0].Author, "孙鹏", "Item[1].NewsItem[0].Author wrong")
	AssertEqual(t, item.Content.NewsItem[0].Digest, "加入校友会", "Item[1].NewsItem[0].Digest wrong")
	AssertEqual(t, item.Content.NewsItem[0].Content, "<p>加入校友会</p><p><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw/0?wx_fmt=png\" data-ratio=\"0.6765327695560254\" data-w=\"946\"  /></p><p><br  /></p>", "article content wrong")
	AssertEqual(t, item.Content.NewsItem[0].ContentSourceURL, "http://www.hitxy.org.au/join", "Item[1].NewsItem[0].ContentSourceURL wrong")
	AssertEqual(t, item.Content.NewsItem[0].ThumbMediaID, "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM", "Item[1].NewsItem[0].ThumbMediaID wrong")
	AssertEqual(t, item.Content.NewsItem[0].ShowCoverPic, 0, "Item[1].NewsItem[0].ShowCoverPic wrong")
	AssertEqual(t, item.Content.NewsItem[0].URL, "http://mp.weixin.qq.com/s?__biz=MzUxMDAzNDAwMA==&mid=100000002&idx=1&sn=723bf16b52f88345e5a73ccde1c79bdb&chksm=7908683d4e7fe12bcaefa9c563809db5546ab97af7184ec648f20930fab5c1d95ee825409c0d#rd", "Article URL is not right")
	AssertEqual(t, item.Content.NewsItem[0].ThumbURL, "http://mmbiz.qpic.cn/mmbiz_png/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw/0?wx_fmt=png", "Article Thumb URL wrong")
	AssertEqual(t, item.Content.NewsItem[0].NeedOpenComment, 0, "Need Open Comment should be 0")
	AssertEqual(t, item.Content.NewsItem[0].OnlyFansCanComment, 0, "OnlyFansCanComment should be 0")

}
