Files
software-station/internal/config/config_test.go
Sudo-Ivan 2f0af4c988 Refactor API and background updater functionality
- Updated the StartBackgroundUpdater function to accept a callback for software list updates, improving flexibility.
- Refactored the API handlers to utilize a proxied software list, enhancing data handling and response efficiency.
- Introduced a new method for refreshing the proxied software list, ensuring accurate data representation.
- Added unit tests for API handlers to validate functionality and response correctness.
2025-12-27 03:30:18 -06:00

72 lines
1.8 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{}
onUpdate := func(newList []models.Software) {
mu.Lock()
*softwareList = newList
mu.Unlock()
}
StartBackgroundUpdater(configPath, mockGitea.URL, "", 100*time.Millisecond, onUpdate)
// Wait for ticker
time.Sleep(250 * time.Millisecond)
mu.RLock()
length := len(*softwareList)
mu.RUnlock()
if length == 0 {
t.Error("softwareList was not updated by background updater")
}
}