package ui import ( "context" "net/http" "net/http/httptest" "sync/atomic" "testing" "git.stinnesbeck.com/nils/arr-tui/internal/radarr" "git.stinnesbeck.com/nils/arr-tui/internal/trakt" ) func TestListLookupsKeptOnDisk(t *testing.T) { var lookups, libraryAsks atomic.Int32 inLibrary := false srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/v3/movie": libraryAsks.Add(1) if inLibrary && r.URL.Query().Get("tmdbId") == "949" { w.Write([]byte(`[{"id":7,"title":"Heat","year":1995,"tmdbId":949}]`)) return } w.Write([]byte(`[{"id":3,"title":"Other","tmdbId":1}]`)) // ignores the filter case "/api/v3/movie/lookup": lookups.Add(1) w.Write([]byte(`[{"title":"Heat","year":1995,"tmdbId":949,"overview":"Cops and robbers."}]`)) } })) defer srv.Close() disk := &diskCache{dir: t.TempDir()} entry := trakt.Entry{Title: "Heat", IDs: trakt.IDs{TMDB: 949}} resolve := func() result { t.Helper() store := newLookupStore(disk) // a new run b := radarrBackend{radarr.NewClient(srv.URL, "k"), store} r, err := b.resolve(context.Background(), entry) if err != nil || r == nil { t.Fatalf("resolve: %v %v", r, err) } if err := store.save(); err != nil { t.Fatal(err) } return r } if r := resolve(); r.inLibrary() || lookups.Load() != 1 { t.Fatalf("first run: in library %v, %d lookups", r.inLibrary(), lookups.Load()) } r := resolve() if lookups.Load() != 1 || r.details().plot != "Cops and robbers." { t.Errorf("second run: %d lookups, plot %q", lookups.Load(), r.details().plot) } // Added meanwhile (elsewhere): the library answer wins over the stored lookup. inLibrary = true if r := resolve(); !r.inLibrary() || lookups.Load() != 1 { t.Errorf("after adding: in library %v, %d lookups", r.inLibrary(), lookups.Load()) } if libraryAsks.Load() != 3 { t.Errorf("library asked %d times", libraryAsks.Load()) } }