|
| 1 | +package ytmusic |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "cliamp/playlist" |
| 8 | +) |
| 9 | + |
| 10 | +func TestRefreshInvalidatesAllCaches(t *testing.T) { |
| 11 | + t.Setenv("HOME", t.TempDir()) |
| 12 | + |
| 13 | + b := newBase(nil, "client-id", "client-secret", false) |
| 14 | + |
| 15 | + b.allPlaylists = []playlistEntry{{ID: "p1", Name: "One", TrackCount: 5}} |
| 16 | + b.classified = map[string]bool{"p1": true} |
| 17 | + b.trackCache["p1"] = []playlist.Track{{Path: "https://example/v", Title: "t"}} |
| 18 | + |
| 19 | + dc := b.ensureDiskCache() |
| 20 | + dc.setPlaylists(b.allPlaylists) |
| 21 | + dc.setTracks("p1", b.trackCache["p1"]) |
| 22 | + saveSnapshot(dc.snapshot()) |
| 23 | + |
| 24 | + if !dc.playlistsFresh() { |
| 25 | + t.Fatal("disk cache should be fresh before refresh") |
| 26 | + } |
| 27 | + |
| 28 | + b.refresh() |
| 29 | + |
| 30 | + if b.allPlaylists != nil { |
| 31 | + t.Error("allPlaylists not cleared") |
| 32 | + } |
| 33 | + if b.classified != nil { |
| 34 | + t.Error("classified not cleared") |
| 35 | + } |
| 36 | + if len(b.trackCache) != 0 { |
| 37 | + t.Errorf("trackCache not cleared: %d entries", len(b.trackCache)) |
| 38 | + } |
| 39 | + |
| 40 | + if b.disk.playlistsFresh() { |
| 41 | + t.Error("disk cache still reports fresh after refresh") |
| 42 | + } |
| 43 | + if !b.disk.PlaylistsAt.IsZero() { |
| 44 | + t.Errorf("PlaylistsAt should be zero, got %v", b.disk.PlaylistsAt) |
| 45 | + } |
| 46 | + if len(b.disk.Playlists) != 0 { |
| 47 | + t.Errorf("disk Playlists not cleared: %d entries", len(b.disk.Playlists)) |
| 48 | + } |
| 49 | + if len(b.disk.Tracks) != 0 { |
| 50 | + t.Errorf("disk Tracks not cleared: %d entries", len(b.disk.Tracks)) |
| 51 | + } |
| 52 | + |
| 53 | + reloaded := loadYTCache() |
| 54 | + if reloaded.playlistsFresh() { |
| 55 | + t.Error("reloaded disk cache still fresh after refresh") |
| 56 | + } |
| 57 | + if !reloaded.PlaylistsAt.Equal(time.Time{}) { |
| 58 | + t.Errorf("reloaded PlaylistsAt should be zero, got %v", reloaded.PlaylistsAt) |
| 59 | + } |
| 60 | + if len(reloaded.Tracks) != 0 { |
| 61 | + t.Errorf("reloaded disk Tracks not cleared: %d entries", len(reloaded.Tracks)) |
| 62 | + } |
| 63 | +} |
0 commit comments