1package writefreely 2 3import ( 4 "regexp" 5 "testing" 6 "time" 7) 8 9func TestUpdatesRoundTrip(t *testing.T) { 10 cache := newUpdatesCache(defaultUpdatesCacheTime) 11 t.Run("New Updates Cache", func(t *testing.T) { 12 13 if cache == nil { 14 t.Fatal("Returned nil cache") 15 } 16 17 if cache.frequency != defaultUpdatesCacheTime { 18 t.Fatalf("Got cache expiry frequency: %s but expected: %s", cache.frequency, defaultUpdatesCacheTime) 19 } 20 21 if cache.currentVersion != "v"+softwareVer { 22 t.Fatalf("Got current version: %s but expected: %s", cache.currentVersion, "v"+softwareVer) 23 } 24 }) 25 26 t.Run("Release URL", func(t *testing.T) { 27 url := cache.ReleaseNotesURL() 28 29 reg, err := regexp.Compile(`^https:\/\/blog.writefreely.org\/version(-\d+){1,}$`) 30 if err != nil { 31 t.Fatalf("Test Case Error: Failed to compile regex: %v", err) 32 } 33 match := reg.MatchString(url) 34 35 if !match { 36 t.Fatalf("Malformed Release URL: %s", url) 37 } 38 }) 39 40 t.Run("Check Now", func(t *testing.T) { 41 // ensure time between init and next check 42 time.Sleep(1 * time.Second) 43 44 prevLastCheck := cache.lastCheck 45 46 // force to known older version for latest and current 47 prevLatestVer := "v0.8.1" 48 cache.latestVersion = prevLatestVer 49 cache.currentVersion = "v0.8.0" 50 51 err := cache.CheckNow() 52 if err != nil { 53 t.Fatalf("Error should be nil, got: %v", err) 54 } 55 56 if prevLastCheck == cache.lastCheck { 57 t.Fatal("Expected lastCheck to update") 58 } 59 60 if cache.lastCheck.Before(prevLastCheck) { 61 t.Fatal("Last check should be newer than previous") 62 } 63 64 if prevLatestVer == cache.latestVersion { 65 t.Fatal("expected latestVersion to update") 66 } 67 68 }) 69 70 t.Run("Are Available", func(t *testing.T) { 71 if !cache.AreAvailable() { 72 t.Fatalf("Cache reports not updates but Current is %s and Latest is %s", cache.currentVersion, cache.latestVersion) 73 } 74 }) 75 76 t.Run("Latest Version", func(t *testing.T) { 77 gotLatest := cache.LatestVersion() 78 if gotLatest != cache.latestVersion { 79 t.Fatalf("Malformed latest version. Expected: %s but got: %s", cache.latestVersion, gotLatest) 80 } 81 }) 82} 83