diff options
| author | Adam Mathes <adam@adammathes.com> | 2026-02-13 18:43:03 -0800 |
|---|---|---|
| committer | Adam Mathes <adam@adammathes.com> | 2026-02-13 18:43:03 -0800 |
| commit | 21b4eec6c1e096573bcd5f2079bc21e23a960621 (patch) | |
| tree | 58c8fe2e86aef9859debd05344084e9060dc38c9 /main_test.go | |
| parent | 01d4bbe4b2842cb8c2e4319b6cf03d3050f38d06 (diff) | |
| download | neko-21b4eec6c1e096573bcd5f2079bc21e23a960621.tar.gz neko-21b4eec6c1e096573bcd5f2079bc21e23a960621.tar.bz2 neko-21b4eec6c1e096573bcd5f2079bc21e23a960621.zip | |
refactor(backend): improve testability and add tests (NK-6q9nyg)
Diffstat (limited to 'main_test.go')
| -rw-r--r-- | main_test.go | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..fd36fdd --- /dev/null +++ b/main_test.go @@ -0,0 +1,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) + } +} |
