aboutsummaryrefslogtreecommitdiffstats
path: root/internal/importer/import_format_test.go
blob: 9176383e056f7412f579563d82ff16853ed028c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package importer

import (
	"strings"
	"testing"

	"path/filepath"

	"adammathes.com/neko/config"
	"adammathes.com/neko/models"
	"adammathes.com/neko/models/feed"
)

func TestImportOPML(t *testing.T) {
	config.Config.DBFile = filepath.Join(t.TempDir(), "test.db")
	models.InitDB()
	defer models.DB.Close()

	opmlContent := `<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
  <head>
    <title>testing import</title>
  </head>
  <body>
    <outline text="Tech">
      <outline type="rss" text="Ars Technica" title="Ars Technica" xmlUrl="https://arstechnica.com/feed/" htmlUrl="https://arstechnica.com"/>
      <outline type="rss" text="The Verge" title="The Verge" xmlUrl="https://www.theverge.com/rss/index.xml" htmlUrl="https://www.theverge.com"/>
    </outline>
    <outline type="rss" text="XKCD" title="XKCD" xmlUrl="https://xkcd.com/rss.xml" htmlUrl="https://xkcd.com"/>
  </body>
</opml>`

	err := ImportOPML(strings.NewReader(opmlContent))
	if err != nil {
		t.Fatalf("ImportOPML failed: %v", err)
	}

	feeds, err := feed.All()
	if err != nil {
		t.Fatal(err)
	}

	if len(feeds) != 3 {
		t.Errorf("Expected 3 feeds, got %d", len(feeds))
	}

	foundArs := false
	foundXKCD := false
	for _, f := range feeds {
		if f.Url == "https://arstechnica.com/feed/" {
			foundArs = true
			if f.Category != "Tech" {
				t.Errorf("Expected category 'Tech' for Ars, got %q", f.Category)
			}
		}
		if f.Url == "https://xkcd.com/rss.xml" {
			foundXKCD = true
			if f.Category != "" {
				t.Errorf("Expected empty category for XKCD, got %q", f.Category)
			}
		}
	}

	if !foundArs {
		t.Error("Did not find Ars Technica in imported feeds")
	}
	if !foundXKCD {
		t.Error("Did not find XKCD in imported feeds")
	}
}

func TestImportText(t *testing.T) {
	config.Config.DBFile = filepath.Join(t.TempDir(), "test.db")
	models.InitDB()
	defer models.DB.Close()

	textContent := `
https://example.com/feed1
# comment
https://example.com/feed2
  https://example.com/feed3  
`

	err := ImportText(strings.NewReader(textContent))
	if err != nil {
		t.Fatalf("ImportText failed: %v", err)
	}

	feeds, err := feed.All()
	if err != nil {
		t.Fatal(err)
	}

	if len(feeds) != 3 {
		t.Errorf("Expected 3 feeds, got %d", len(feeds))
	}
}