/ internal / update / cache_test.go
cache_test.go
 1  package update
 2  
 3  import (
 4  	"path/filepath"
 5  	"testing"
 6  	"time"
 7  )
 8  
 9  func TestCacheShouldCheck_NoCacheFile(t *testing.T) {
10  	dir := t.TempDir()
11  	c := NewUpdateCache(filepath.Join(dir, "update-check.json"))
12  	if !c.ShouldCheck() {
13  		t.Fatal("expected ShouldCheck=true when no cache file exists")
14  	}
15  }
16  
17  func TestCacheShouldCheck_RecentCheck(t *testing.T) {
18  	dir := t.TempDir()
19  	c := NewUpdateCache(filepath.Join(dir, "update-check.json"))
20  	c.Record("0.1.3")
21  	if c.ShouldCheck() {
22  		t.Fatal("expected ShouldCheck=false immediately after Record")
23  	}
24  }
25  
26  func TestCacheShouldCheck_StaleCheck(t *testing.T) {
27  	dir := t.TempDir()
28  	path := filepath.Join(dir, "update-check.json")
29  	c := NewUpdateCache(path)
30  	c.Record("0.1.3")
31  	c.data.LastCheck = time.Now().Add(-25 * time.Hour)
32  	c.save()
33  	c2 := NewUpdateCache(path)
34  	if !c2.ShouldCheck() {
35  		t.Fatal("expected ShouldCheck=true after 25h")
36  	}
37  }
38  
39  func TestCacheLatestVersion(t *testing.T) {
40  	dir := t.TempDir()
41  	c := NewUpdateCache(filepath.Join(dir, "update-check.json"))
42  	c.Record("0.1.3")
43  	if v := c.LatestVersion(); v != "0.1.3" {
44  		t.Fatalf("expected 0.1.3, got %s", v)
45  	}
46  }