Files
software-station/internal/config/config_test.go
2025-12-27 02:57:25 -06:00

63 lines
1.7 KiB
Go

package config
import (
"net/http"
"net/http/httptest"
"os"
"software-station/internal/models"
"sync"
"testing"
"time"
)
func TestConfig(t *testing.T) {
// Test Local Config
configPath := "test_software.txt"
os.WriteFile(configPath, []byte("Owner/Repo\n#Comment\n\nOwner2/Repo2"), 0644)
defer os.Remove(configPath)
// Mock Gitea
mockGitea := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"description": "Test", "topics": [], "license": {"name": "MIT"}}`))
}))
defer mockGitea.Close()
list := LoadSoftware(configPath, mockGitea.URL, "")
if len(list) != 2 {
t.Errorf("expected 2 repos, got %d", len(list))
}
// Test Remote Config
mockRemote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Owner3/Repo3"))
}))
defer mockRemote.Close()
listRemote := LoadSoftware(mockRemote.URL, mockGitea.URL, "")
if len(listRemote) != 1 || listRemote[0].Name != "Repo3" {
t.Errorf("expected Repo3, got %v", listRemote)
}
}
func TestBackgroundUpdater(t *testing.T) {
configPath := "test_updater.txt"
os.WriteFile(configPath, []byte("Owner/Repo"), 0644)
defer os.Remove(configPath)
mockGitea := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"description": "Updated", "topics": [], "license": {"name": "MIT"}}`))
}))
defer mockGitea.Close()
var mu sync.RWMutex
softwareList := &[]models.Software{}
StartBackgroundUpdater(configPath, mockGitea.URL, "", &mu, softwareList, 100*time.Millisecond)
// Wait for ticker
time.Sleep(200 * time.Millisecond)
if len(*softwareList) == 0 {
t.Error("softwareList was not updated by background updater")
}
}