feat: wire main loop with periodic sync+scan and sync_interval config
Replace compute-only run() with an immediate sync+scan followed by a ticker-driven periodic loop, add the sync.interval config field (default 6h) with defaults and validation, and add tests for the scheduling logic.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadConfig_Valid(t *testing.T) {
|
||||
@@ -338,3 +339,83 @@ func TestValidate_BoundaryPort(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_SyncIntervalDefault(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.yaml")
|
||||
|
||||
yaml := `
|
||||
navidrome:
|
||||
url: "http://localhost:4533"
|
||||
user: "u"
|
||||
password: "p"
|
||||
|
||||
musicbrainz:
|
||||
user_agent: "NaviWatcher/1.0 ( test@example.com )"
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(yaml), 0644); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig returned error: %v", err)
|
||||
}
|
||||
if cfg.Sync.Interval != DefaultSyncInterval {
|
||||
t.Errorf("expected default sync interval %v, got %v", DefaultSyncInterval, cfg.Sync.Interval)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_SyncIntervalParsed(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.yaml")
|
||||
|
||||
yaml := `
|
||||
navidrome:
|
||||
url: "http://localhost:4533"
|
||||
user: "u"
|
||||
password: "p"
|
||||
|
||||
musicbrainz:
|
||||
user_agent: "NaviWatcher/1.0 ( test@example.com )"
|
||||
|
||||
sync:
|
||||
interval: 30m
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(yaml), 0644); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig returned error: %v", err)
|
||||
}
|
||||
if cfg.Sync.Interval != 30*time.Minute {
|
||||
t.Errorf("expected sync interval 30m, got %v", cfg.Sync.Interval)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_InvalidSyncInterval(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.yaml")
|
||||
|
||||
yaml := `
|
||||
navidrome:
|
||||
url: "http://localhost:4533"
|
||||
user: "u"
|
||||
password: "p"
|
||||
|
||||
musicbrainz:
|
||||
user_agent: "NaviWatcher/1.0 ( test@example.com )"
|
||||
|
||||
sync:
|
||||
interval: -1s
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(yaml), 0644); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
|
||||
if _, err := LoadConfig(path); err == nil {
|
||||
t.Fatal("expected error for negative sync interval, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user