- Unit tests use an in-memory fake database (internal/testutil/fakedb), temp folders and httptest; `go test ./...` needs no Docker or env vars - Integration tests (build tag "integration") start PostgreSQL with testcontainers: migrations step by step, SQL dumps (skipped if missing) and a full sync against a real database - Removed tests that only checked copies of the logic or had empty bodies, the broken root integration_test.go, compose.test.yaml and .env.test with hardcoded paths - testMusic MP3s replaced with tiny placeholders (sync never decodes audio), 194 MB -> 168 KB
82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package backend
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newGitea(t *testing.T, status int) (*httptest.Server, *atomic.Int32) {
|
|
var calls atomic.Int32
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls.Add(1)
|
|
w.WriteHeader(status)
|
|
json.NewEncoder(w).Encode(giteaRelease{
|
|
Id: 1,
|
|
Name: "v1.2.3",
|
|
Assets: []giteaAsset{
|
|
{Name: "MusicPlayer.exe", DownloadUrl: "https://example.com/MusicPlayer.exe"},
|
|
{Name: "MusicPlayer.x86_64", DownloadUrl: "https://example.com/MusicPlayer.x86_64"},
|
|
},
|
|
})
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
return server, &calls
|
|
}
|
|
|
|
func TestReleaseService(t *testing.T) {
|
|
server, _ := newGitea(t, http.StatusOK)
|
|
r := NewReleaseService(server.URL)
|
|
ctx := context.Background()
|
|
|
|
version, err := r.LatestVersion(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "v1.2.3", version)
|
|
|
|
names, err := r.AssetNames(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, []string{"MusicPlayer.exe", "MusicPlayer.x86_64"}, names)
|
|
|
|
url, err := r.AssetURL(ctx, ".exe")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "https://example.com/MusicPlayer.exe", url)
|
|
|
|
_, err = r.AssetURL(ctx, ".dmg")
|
|
assert.ErrorIs(t, err, ErrNotFound)
|
|
}
|
|
|
|
func TestReleaseServiceCachesRelease(t *testing.T) {
|
|
server, calls := newGitea(t, http.StatusOK)
|
|
r := NewReleaseService(server.URL)
|
|
now := time.Now()
|
|
r.now = func() time.Time { return now }
|
|
ctx := context.Background()
|
|
|
|
for range 3 {
|
|
_, err := r.LatestVersion(ctx)
|
|
require.NoError(t, err)
|
|
}
|
|
assert.Equal(t, int32(1), calls.Load())
|
|
|
|
now = now.Add(releaseCacheTTL + time.Second)
|
|
_, err := r.LatestVersion(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int32(2), calls.Load())
|
|
}
|
|
|
|
func TestReleaseServiceUpstreamErrors(t *testing.T) {
|
|
server, _ := newGitea(t, http.StatusInternalServerError)
|
|
_, err := NewReleaseService(server.URL).LatestVersion(context.Background())
|
|
assert.ErrorIs(t, err, ErrUpstream)
|
|
|
|
_, err = NewReleaseService("http://127.0.0.1:1").AssetNames(context.Background())
|
|
assert.ErrorIs(t, err, ErrUpstream)
|
|
}
|