Files
Reims-Go/internal/app/update_cache_test.go

75 lines
2.2 KiB
Go

package app
import (
"testing"
"time"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/dataroot"
"github.com/Nwflower/reims-go/internal/version"
)
func TestAppUpdateCache_PerChannelFiveMinuteTTL(t *testing.T) {
if updateCacheTTL != 5*time.Minute {
t.Fatalf("updateCacheTTL = %v, want 5m", updateCacheTTL)
}
dir := t.TempDir()
a := &App{
cfg: &config.Config{DataRoot: dir, AppUpdateChannel: version.ChannelStable},
root: dataroot.Status{Dir: dir},
}
appUpdateCache.Lock()
orig := appUpdateCache.entries
appUpdateCache.entries = map[string]appUpdateCacheEntry{
version.ChannelStable: {
at: time.Now(),
info: version.UpdateInfo{Channel: version.ChannelStable, Current: "v0.2.13", Latest: "v0.2.13", HasUpdate: false},
},
version.ChannelAlpha: {
at: time.Now(),
info: version.UpdateInfo{Channel: version.ChannelAlpha, Current: "v0.2.13", Latest: "v0.2.14-alpha.1", HasUpdate: true},
},
}
appUpdateCache.Unlock()
t.Cleanup(func() {
appUpdateCache.Lock()
appUpdateCache.entries = orig
appUpdateCache.Unlock()
})
// Stable channel returns cached result immediately.
got, err := a.CheckAppUpdate(false)
if err != nil {
t.Fatalf("CheckAppUpdate(stable): %v", err)
}
if got.Channel != version.ChannelStable || got.HasUpdate {
t.Fatalf("stable got = %+v, want cached no-update", got)
}
// Switching to alpha must preserve per-channel cache and return cached alpha result.
if err := a.SetAppUpdateChannel(version.ChannelAlpha); err != nil {
t.Fatalf("SetAppUpdateChannel(alpha): %v", err)
}
got, err = a.CheckAppUpdate(false)
if err != nil {
t.Fatalf("CheckAppUpdate(alpha): %v", err)
}
if got.Channel != version.ChannelAlpha || !got.HasUpdate || got.Latest != "v0.2.14-alpha.1" {
t.Fatalf("alpha got = %+v, want cached alpha update", got)
}
// Switching back to stable still hits the stable cache.
if err := a.SetAppUpdateChannel(version.ChannelStable); err != nil {
t.Fatalf("SetAppUpdateChannel(stable): %v", err)
}
got, err = a.CheckAppUpdate(false)
if err != nil {
t.Fatalf("CheckAppUpdate(stable again): %v", err)
}
if got.Channel != version.ChannelStable || got.HasUpdate {
t.Fatalf("stable again got = %+v, want cached no-update", got)
}
}