From 16186a344a7b61633cb7342aac37ac56ad83d261 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Thu, 12 Feb 2026 19:55:05 -0800 Subject: =?UTF-8?q?Add=20comprehensive=20test=20suite=20=E2=80=94=2081%=20?= =?UTF-8?q?cross-package=20coverage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug fixes: - config: remove unused log import - item: fix Printf format %d->%t for boolean ReadState - util: update stale config.Read -> config.Init, remove config.Config.DBServer Test files added: - config/config_test.go: Init, readConfig, addDefaults (100%) - vlog/vlog_test.go: Printf, Println verbose/silent (100%) - models/db_test.go: InitDB tests - models/feed/feed_test.go: CRUD, filter, Categories, NewFeed, ResolveFeedURL (87%) - models/item/item_test.go: CRUD, Filter with category/search/starred, rewriteImages (71%) - exporter/exporter_test.go: all export formats (91%) - importer/importer_test.go: InsertIItem, ImportJSON (90%) - crawler/crawler_test.go: GetFeedContent, CrawlFeed, CrawlWorker, Crawl (89%) - web/web_test.go: auth, login/logout, stream, item, feed, category, export, crawl, imageProxy handlers (77%) Remaining 0% functions require HTTP/rice.MustFindBox/main entry and can't be unit tested without refactoring (see tickets NK-gqkh96, NK-6q9nyg). --- config/config_test.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 config/config_test.go (limited to 'config/config_test.go') diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..0e700f2 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,126 @@ +package config + +import ( + "os" + "path/filepath" + "testing" +) + +func TestInitEmpty(t *testing.T) { + // Reset global + Config = Settings{} + + err := Init("") + if err != nil { + t.Fatalf("Init with empty string should not error: %v", err) + } + + // Defaults should be set + if Config.DBFile != "neko.db" { + t.Errorf("expected default DBFile 'neko.db', got %q", Config.DBFile) + } + if Config.Port != 4994 { + t.Errorf("expected default Port 4994, got %d", Config.Port) + } + if Config.CrawlMinutes != 60 { + t.Errorf("expected default CrawlMinutes 60, got %d", Config.CrawlMinutes) + } +} + +func TestInitWithValidFile(t *testing.T) { + Config = Settings{} + + dir := t.TempDir() + configPath := filepath.Join(dir, "config.yaml") + content := []byte("database: test.db\nhttp: 8080\npassword: secret\nminutes: 30\nimageproxy: true\n") + if err := os.WriteFile(configPath, content, 0644); err != nil { + t.Fatal(err) + } + + err := Init(configPath) + if err != nil { + t.Fatalf("Init should not error with valid file: %v", err) + } + + if Config.DBFile != "test.db" { + t.Errorf("expected DBFile 'test.db', got %q", Config.DBFile) + } + if Config.Port != 8080 { + t.Errorf("expected Port 8080, got %d", Config.Port) + } + if Config.DigestPassword != "secret" { + t.Errorf("expected password 'secret', got %q", Config.DigestPassword) + } + if Config.CrawlMinutes != 30 { + t.Errorf("expected CrawlMinutes 30, got %d", Config.CrawlMinutes) + } + if !Config.ProxyImages { + t.Error("expected ProxyImages true") + } +} + +func TestInitWithMissingFile(t *testing.T) { + Config = Settings{} + err := Init("/nonexistent/config.yaml") + if err == nil { + t.Fatal("Init with missing file should return error") + } +} + +func TestInitWithInvalidYAML(t *testing.T) { + Config = Settings{} + + dir := t.TempDir() + configPath := filepath.Join(dir, "bad.yaml") + content := []byte("{{{{not valid yaml at all") + if err := os.WriteFile(configPath, content, 0644); err != nil { + t.Fatal(err) + } + + err := Init(configPath) + if err == nil { + t.Fatal("Init with invalid YAML should return error") + } +} + +func TestAddDefaultsNoOverride(t *testing.T) { + // When values are already set, addDefaults should not overwrite + Config = Settings{ + DBFile: "custom.db", + Port: 9999, + CrawlMinutes: 120, + } + addDefaults() + + if Config.DBFile != "custom.db" { + t.Errorf("addDefaults should not override DBFile, got %q", Config.DBFile) + } + if Config.Port != 9999 { + t.Errorf("addDefaults should not override Port, got %d", Config.Port) + } + if Config.CrawlMinutes != 120 { + t.Errorf("addDefaults should not override CrawlMinutes, got %d", Config.CrawlMinutes) + } +} + +func TestReadConfigValid(t *testing.T) { + Config = Settings{} + + dir := t.TempDir() + configPath := filepath.Join(dir, "config.yaml") + content := []byte("database: mydb.db\nhttp: 5000\n") + if err := os.WriteFile(configPath, content, 0644); err != nil { + t.Fatal(err) + } + + err := readConfig(configPath) + if err != nil { + t.Fatalf("readConfig should not error: %v", err) + } + if Config.DBFile != "mydb.db" { + t.Errorf("expected DBFile 'mydb.db', got %q", Config.DBFile) + } + if Config.Port != 5000 { + t.Errorf("expected Port 5000, got %d", Config.Port) + } +} -- cgit v1.2.3