- Imported packages from separate repos: - github.com/Snider/config -> pkg/config - github.com/Snider/display -> pkg/display - github.com/Snider/help -> pkg/help - github.com/Snider/i18n -> pkg/i18n - github.com/Snider/updater -> pkg/updater - Moved core code from root to pkg/core - Flattened nested package structures - Updated all import paths to github.com/Snider/Core/pkg/* - Added Display interface to Core - Updated go.work for workspace modules Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package updater
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetLatestUpdateFromURL(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
handler http.HandlerFunc
|
|
expectError bool
|
|
expectedVersion string
|
|
expectedURL string
|
|
}{
|
|
{
|
|
name: "Valid latest.json",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, `{"version": "v1.1.0", "url": "http://example.com/release.zip"}`)
|
|
},
|
|
expectedVersion: "v1.1.0",
|
|
expectedURL: "http://example.com/release.zip",
|
|
},
|
|
{
|
|
name: "Invalid JSON",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, `{"version": "v1.1.0", "url": "http://example.com/release.zip"`) // Missing closing brace
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "Missing version",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, `{"url": "http://example.com/release.zip"}`)
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "Missing URL",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, `{"version": "v1.1.0"}`)
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "Server error",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
},
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
server := httptest.NewServer(tc.handler)
|
|
defer server.Close()
|
|
|
|
info, err := GetLatestUpdateFromURL(server.URL)
|
|
|
|
if (err != nil) != tc.expectError {
|
|
t.Errorf("Expected error: %v, got: %v", tc.expectError, err)
|
|
}
|
|
|
|
if !tc.expectError {
|
|
if info.Version != tc.expectedVersion {
|
|
t.Errorf("Expected version: %s, got: %s", tc.expectedVersion, info.Version)
|
|
}
|
|
if info.URL != tc.expectedURL {
|
|
t.Errorf("Expected URL: %s, got: %s", tc.expectedURL, info.URL)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|