go-update/service_test.go
Claude 644986b8bb
chore(ax): Pass 1 AX compliance sweep — banned imports, test naming, comment style
- Remove fmt from updater.go, service.go, http_client.go, cmd.go, github.go, generic_http.go; replace with string concat, coreerr.E, cli.Print
- Remove strings from updater.go (inline byte comparisons) and service.go (inline helpers)
- Replace fmt.Sprintf in error paths with string concatenation throughout
- Add cli.Print for all stdout output in updater.go (CheckForUpdates, CheckOnly, etc.)
- Fix service_examples_test.go: restore original CheckForUpdates instead of setting nil
- Test naming: all test files now follow TestFile_Function_{Good,Bad,Ugly} with all three variants mandatory
- Comments: replace prose descriptions with usage-example style on all exported functions
- Remaining banned: strings/encoding/json in github.go and generic_http.go (no Core replacement in direct deps); os/os.exec in platform files (syscall-level, unavoidable without go-process)

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 08:42:13 +01:00

207 lines
5.7 KiB
Go

package updater
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestService_NewUpdateService_Good(t *testing.T) {
testCases := []struct {
name string
config UpdateServiceConfig
isGitHub bool
}{
{
name: "github URL detected as GitHub",
config: UpdateServiceConfig{
RepoURL: "https://github.com/owner/repo",
},
isGitHub: true,
},
{
name: "non-GitHub URL not detected as GitHub",
config: UpdateServiceConfig{
RepoURL: "https://example.com/updates",
},
isGitHub: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
service, err := NewUpdateService(tc.config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if service.isGitHub != tc.isGitHub {
t.Errorf("expected isGitHub=%v, got %v", tc.isGitHub, service.isGitHub)
}
})
}
}
func TestService_NewUpdateService_Bad(t *testing.T) {
_, err := NewUpdateService(UpdateServiceConfig{
RepoURL: "https://github.com/owner", // missing repo segment
})
if err == nil {
t.Error("expected error for invalid GitHub URL, got nil")
}
}
func TestService_NewUpdateService_Ugly(t *testing.T) {
// Empty RepoURL — treated as non-GitHub, no parse error
service, err := NewUpdateService(UpdateServiceConfig{RepoURL: ""})
if err != nil {
t.Fatalf("unexpected error for empty URL: %v", err)
}
if service.isGitHub {
t.Error("expected isGitHub=false for empty URL")
}
}
func TestService_Start_Good(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"version": "v1.1.0", "url": "http://example.com/release.zip"}`))
}))
defer server.Close()
testCases := []struct {
name string
config UpdateServiceConfig
checkOnlyGitHub int
checkAndDoGitHub int
checkOnlyHTTPCalls int
checkAndDoHTTPCalls int
}{
{
name: "GitHub NoCheck skips all checks",
config: UpdateServiceConfig{
RepoURL: "https://github.com/owner/repo",
CheckOnStartup: NoCheck,
},
},
{
name: "GitHub CheckOnStartup calls CheckOnly",
config: UpdateServiceConfig{
RepoURL: "https://github.com/owner/repo",
CheckOnStartup: CheckOnStartup,
},
checkOnlyGitHub: 1,
},
{
name: "GitHub CheckAndUpdateOnStartup calls CheckForUpdates",
config: UpdateServiceConfig{
RepoURL: "https://github.com/owner/repo",
CheckOnStartup: CheckAndUpdateOnStartup,
},
checkAndDoGitHub: 1,
},
{
name: "HTTP NoCheck skips all checks",
config: UpdateServiceConfig{
RepoURL: server.URL,
CheckOnStartup: NoCheck,
},
},
{
name: "HTTP CheckOnStartup calls CheckOnlyHTTP",
config: UpdateServiceConfig{
RepoURL: server.URL,
CheckOnStartup: CheckOnStartup,
},
checkOnlyHTTPCalls: 1,
},
{
name: "HTTP CheckAndUpdateOnStartup calls CheckForUpdatesHTTP",
config: UpdateServiceConfig{
RepoURL: server.URL,
CheckOnStartup: CheckAndUpdateOnStartup,
},
checkAndDoHTTPCalls: 1,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var checkOnlyGitHub, checkAndDoGitHub, checkOnlyHTTP, checkAndDoHTTP int
originalCheckOnly := CheckOnly
CheckOnly = func(owner, repo, channel string, forceSemVerPrefix bool, releaseURLFormat string) error {
checkOnlyGitHub++
return nil
}
defer func() { CheckOnly = originalCheckOnly }()
originalCheckForUpdates := CheckForUpdates
CheckForUpdates = func(owner, repo, channel string, forceSemVerPrefix bool, releaseURLFormat string) error {
checkAndDoGitHub++
return nil
}
defer func() { CheckForUpdates = originalCheckForUpdates }()
originalCheckOnlyHTTP := CheckOnlyHTTP
CheckOnlyHTTP = func(baseURL string) error {
checkOnlyHTTP++
return nil
}
defer func() { CheckOnlyHTTP = originalCheckOnlyHTTP }()
originalCheckForUpdatesHTTP := CheckForUpdatesHTTP
CheckForUpdatesHTTP = func(baseURL string) error {
checkAndDoHTTP++
return nil
}
defer func() { CheckForUpdatesHTTP = originalCheckForUpdatesHTTP }()
service, _ := NewUpdateService(tc.config)
if err := service.Start(); err != nil {
t.Errorf("unexpected error: %v", err)
}
if checkOnlyGitHub != tc.checkOnlyGitHub {
t.Errorf("GitHub CheckOnly calls: want %d, got %d", tc.checkOnlyGitHub, checkOnlyGitHub)
}
if checkAndDoGitHub != tc.checkAndDoGitHub {
t.Errorf("GitHub CheckForUpdates calls: want %d, got %d", tc.checkAndDoGitHub, checkAndDoGitHub)
}
if checkOnlyHTTP != tc.checkOnlyHTTPCalls {
t.Errorf("HTTP CheckOnly calls: want %d, got %d", tc.checkOnlyHTTPCalls, checkOnlyHTTP)
}
if checkAndDoHTTP != tc.checkAndDoHTTPCalls {
t.Errorf("HTTP CheckForUpdates calls: want %d, got %d", tc.checkAndDoHTTPCalls, checkAndDoHTTP)
}
})
}
}
func TestService_Start_Bad(t *testing.T) {
// GitHub unknown mode returns an error
service := &UpdateService{
config: UpdateServiceConfig{CheckOnStartup: StartupCheckMode(99)},
isGitHub: true,
owner: "owner",
repo: "repo",
}
if err := service.Start(); err == nil {
t.Error("expected error for unknown GitHub startup check mode")
}
// HTTP unknown mode returns an error
service = &UpdateService{
config: UpdateServiceConfig{RepoURL: "https://example.com", CheckOnStartup: StartupCheckMode(99)},
isGitHub: false,
}
if err := service.Start(); err == nil {
t.Error("expected error for unknown HTTP startup check mode")
}
}
func TestService_Start_Ugly(t *testing.T) {
// Start on a zero-value service (no config, no RepoURL) should not panic
service := &UpdateService{}
// NoCheck mode — returns nil without any action
if err := service.Start(); err != nil {
t.Errorf("unexpected error from zero-value service with NoCheck: %v", err)
}
}