aboutsummaryrefslogtreecommitdiffstats
path: root/main_test.go
blob: fd36fddb8f463817239d365ab589687041254249 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main

import (
	"os"
	"path/filepath"
	"testing"

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

func TestRunHelp(t *testing.T) {
	err := Run([]string{"--help"})
	if err != nil {
		t.Errorf("Run(--help) should not error, got %v", err)
	}
}

func TestRunInvalidFlag(t *testing.T) {
	err := Run([]string{"--invalid"})
	if err == nil {
		t.Error("Run(--invalid) should error")
	}
}

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

	// Use --update flag
	err := Run([]string{"-u", "-d", config.Config.DBFile})
	if err != nil {
		t.Errorf("Run(-u) should not error, got %v", err)
	}
}

func TestBackgroundCrawlZero(t *testing.T) {
	backgroundCrawl(0) // Should return immediately
}

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

	// Use config.Config.Port = -1 to signal Run to exit instead of starting server
	config.Config.Port = -1
	err := Run([]string{"-d", config.Config.DBFile})
	if err != nil {
		t.Errorf("Run should not error with Port=-1, got %v", err)
	}
}

func TestRunAdd(t *testing.T) {
	dbPath := filepath.Join(t.TempDir(), "test_add.db")
	err := Run([]string{"-d", dbPath, "-a", "http://example.com/rss"})
	if err != nil {
		t.Errorf("Run -a failed: %v", err)
	}
}

func TestRunExport(t *testing.T) {
	dbPath := filepath.Join(t.TempDir(), "test_export.db")
	err := Run([]string{"-d", dbPath, "-x", "text"})
	if err != nil {
		t.Errorf("Run -x failed: %v", err)
	}
}

func TestRunOptions(t *testing.T) {
	dbPath := filepath.Join(t.TempDir(), "test_options.db")
	err := Run([]string{"-d", dbPath, "-v", "-i", "-s", "-1"})
	if err != nil {
		t.Errorf("Run with options failed: %v", err)
	}
}

func TestRunSetPassword(t *testing.T) {
	dbPath := filepath.Join(t.TempDir(), "test_pass.db")
	err := Run([]string{"-d", dbPath, "-p", "newpassword"})
	if err != nil {
		t.Errorf("Run -p should succeed, got %v", err)
	}
	if config.Config.DigestPassword != "newpassword" {
		t.Errorf("Expected password to be updated")
	}
}

func TestRunConfigError(t *testing.T) {
	err := Run([]string{"-c", "/nonexistent/config.yaml"})
	if err == nil {
		t.Error("Run should error for nonexistent config file")
	}
}

func TestRunExportFormat(t *testing.T) {
	dbPath := filepath.Join(t.TempDir(), "test_export_format.db")
	err := Run([]string{"-d", dbPath, "-x", "json"})
	if err != nil {
		t.Errorf("Run -x json failed: %v", err)
	}
}

func TestRunConfigInvalidContent(t *testing.T) {
	tmpDir := t.TempDir()
	confPath := filepath.Join(tmpDir, "bad.yaml")
	os.WriteFile(confPath, []byte("invalid: : yaml"), 0644)
	err := Run([]string{"-c", confPath})
	if err == nil {
		t.Error("Run should error for malformed config file")
	}
}

func TestRunNoArgs(t *testing.T) {
	dbPath := filepath.Join(t.TempDir(), "test_noargs.db")
	config.Config.Port = -1
	err := Run([]string{"-d", dbPath})
	if err != nil {
		t.Errorf("Run with no args failed: %v", err)
	}
}