package news

import (
	"strings"
	"testing"

	"../utils"
)

type expectedResult struct {
	news 		*News
	teaser 		string
	paragraph 	string
	author 		string
	urlTags		[]string
	haystack	string

	source		*Source
	sourceUrl	string
}

func TestNews_Feed(t *testing.T) {
	expList := [4]*expectedResult{
		{
			news: &News{
				Source: &Source{Id: 1, Name: "Eurosport"},
				Sport: &Sport{Id: 1, Name: "Football", UrlName: "football"},
				Link: "https://www.eurosport.fr/football/bundesliga/2020-2021/dortmund-au-tapis-thuram-debloque-son-compteur_sto7905745/story.shtml",
			},
			teaser: "BUNDESLIGA – Le Borussia Dortmund et ses jeunes stars ont chuté",
			paragraph: "Etonnante Bundesliga. Dortmund battu, Leipzig tenu en échec samedi,",
			author: "Eurosport",
			urlTags: []string{"football", "bundesliga"},
		},
		{
			news: &News{
				Source: &Source{Id: 2, Name: "L'équipe"},
				Sport: &Sport{Id: 1, Name: "Football", UrlName: "football"},
				Link: "https://www.lequipe.fr/Football/Actualites/Mitchel-bakker-psg-je-vais-devoir-elever-mon-niveau-de-jeu/1176182",
			},
			teaser: "Mitchel Bakker, le latéral néerlandais du PSG",
			paragraph: "« Les absences de Juan Bernat et Layvin Kurzawa",
			author: "H. De.",
			urlTags: []string{"ligue-1", "paris-sg--fra-", "reims--fra-"},
		},
		{
			news: &News{
				Source: &Source{Id: 3, Name: "FFTT"},
				Sport: &Sport{Id: 6, Name: "Tennis de Table", UrlName: "tennis-de-table"},
				Link: "http://www.fftt.com/site/actualites/2020-09-22/laura-gasnier-page-qui-se-tourne-avec-bleues",
			},
			teaser: "Après 15 années en équipe de France, Laura Gasnier a décidé",
			paragraph: "Elle évoque un choix personnel qui a demandé plusieurs mois de réflexion",
			author: "",
			urlTags: []string{"equipe-de-france", "gasnier-laura"},
		},
		{
			news: &News{
				Source:  &Source{Id: 4, Name: "Foot Mercato"},
				Sport: &Sport{Id: 1, Name: "Football", UrlName: "football"},
				Link:    "https://www.footmercato.net/a3190892483125730002-real-madrid-personne-ne-veut-de-luka-jovic",
			},
			teaser:    "Alors que la date de fin du mercato approche considérablement,",
			paragraph: "Tic-tac, tic-tac... Le chrono défile, et le Real Madrid",
			author:    "Max Franco Sanchez",
			urlTags: []string{"football"},
		},
	}

	for _, exp := range expList {
		t.Logf("testing feed from %s", exp.news.Source.Name)
		if err := exp.news.Feed(); err != nil {
			t.Errorf("unexpected error : %s", err)
		}
		if !strings.HasPrefix(exp.news.Teaser, exp.teaser) {
			t.Errorf("unexpected teaser : %s", exp.news.Teaser)
		}
		if !strings.HasPrefix(exp.news.Content[0], exp.paragraph) {
			t.Errorf("unexpected content : %s", exp.news.Content[0])
		}
		if exp.news.Author != exp.author {
			t.Errorf("unexpected author : %s", exp.news.Author)
		}
		for _, urlTag := range exp.urlTags {
			if !utils.ArrayContains(exp.news.UrlTags, urlTag) {
				t.Errorf("urltags does not contain %s", urlTag)
			}
		}
	}
}

func TestSource_ListNews(t *testing.T) {
	expList := []*expectedResult{
		{
			source: &Source{Id: 1, Name: "Eurosport"},
			sourceUrl: "http://www.eurosport.fr/football/rss.xml",
		},
		{
			source: &Source{Id: 1, Name: "L'équipe"},
			sourceUrl: "https://www.lequipe.fr/rss/actu_rss_Football.xml",
		},
		{
			source: &Source{Id: 1, Name: "FFTT"},
			sourceUrl: "http://www.fftt.com/site/medias/flux/rss_competition.xml",
		},
		{
			source: &Source{Id: 1, Name: "Foot Mercato"},
			sourceUrl: "http://www.footmercato.net/flux-rss",
		},
	}

	for _, exp := range expList {
		t.Logf("testing newsList from %s", exp.source.Name)
		newsList, err := exp.source.ListNews(&Sport{Id: 1}, exp.sourceUrl)
		if err != nil {
			t.Errorf("unexpected error : %s", err)
		}
		if len(newsList) == 0 {
			t.Errorf("no news parsed from rss")
		}
		for _, n := range newsList {
			if n.Title == "" {
				t.Errorf("unexpected empty title")
			}
			if n.Image == "" {
				t.Errorf("unexpected empty image")
			}
		}
	}
}
