package rss

import (
	"bytes"
	"local/rssmon3/config"
	"local/rssmon3/monitor"
	"log"
	"os"
	"testing"
)

func initRSS() {
	initRSSFeed()
}

func TestRSSNewRunUpdate(t *testing.T) {
	initRSS()

	rss := New(nil)

	config.Values().Can()
	if err := rss.Run(); err != nil {
		t.Fatal(err)
	}

	s := mockRSS()
	defer s.Close()

	f := newFeed("key")
	f.TitleFilter = "500"
	f.ContentFilter = "b"
	f.Tags = []string{"c"}
	f.URL = s.URL

	if err := f.save(); err != nil {
		t.Fatal(err)
	}

	for i := 0; i < 2; i++ {
		log.SetOutput(bytes.NewBuffer(nil))
		defer log.SetOutput(os.Stderr)
		if err := rss.update(&monitor.Item{Key: f.Key}); err != nil {
			t.Fatal(err)
		}
		log.SetOutput(os.Stderr)

		keys, err := config.Values().DB.List([]string{nsItems, f.Key})
		if err != nil {
			t.Fatal(err)
		}
		if len(keys) != 1 {
			t.Fatal(keys)
		}
		if keys[0] != "2018-07-10-19-00:https://roosterteeth.com/episode/rooster-teeth-podcast-2018-rooster-teeth-podcast-500" {
			t.Fatal(keys[0])
		}
	}
}
