From 3230d871079d8f324ee620c7dc589c7686b1377d Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 15 Oct 2026 04:56:23 +0000 Subject: [PATCH] Test that a cached variant is served while its URL is negative-cached Get refuses a cached variant with the negative-cache error while its source URL has a negative-cache row. The new test caches a variant, stores a row for its URL, and expects the variant to be served from the cache, and a variant not in the cache to be refused as before. It fails until Get checks the negative cache only on a miss (https://git.eeqj.de/sneak/pixa/issues/245). Model: opus-5-5 --- .../imgcache/negative_cache_internal_test.go | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/internal/imgcache/negative_cache_internal_test.go b/internal/imgcache/negative_cache_internal_test.go index f2c2115..e9b5f8a 100644 --- a/internal/imgcache/negative_cache_internal_test.go +++ b/internal/imgcache/negative_cache_internal_test.go @@ -94,6 +94,50 @@ func TestNegativeCache_Expired(t *testing.T) { } } +// TestService_Get_ServesCachedVariantOfNegativeCachedURL caches a variant of +// the test photo, then keeps a failed fetch of the photo's URL in the negative +// cache, as a failed fetch for another variant does once the source has been +// evicted. The cached variant is still served; a variant not in the cache is +// still refused. +func TestService_Get_ServesCachedVariantOfNegativeCachedURL(t *testing.T) { + t.Parallel() + + svc, fixtures := SetupTestService(t) + + resp, err := svc.Get(t.Context(), photoVariant(fixtures, 85, FitCover)) + if err != nil { + t.Fatalf("Get() first request error = %v", err) + } + + _ = resp.Content.Close() + + err = svc.cache.StoreNegative(t.Context(), photoVariant(fixtures, 85, FitCover), + httpStatusBadGateway, "upstream answered 503") + if err != nil { + t.Fatalf("StoreNegative() error = %v", err) + } + + resp, err = svc.Get(t.Context(), photoVariant(fixtures, 85, FitCover)) + if err != nil { + t.Fatalf("Get() of the cached variant error = %v", err) + } + + _ = resp.Content.Close() + + if resp.CacheStatus != CacheHit { + t.Errorf("cached variant CacheStatus = %q, want %q", + resp.CacheStatus, CacheHit) + } + + _, err = svc.Get(t.Context(), photoVariant(fixtures, 40, FitCover)) + t.Logf("Get() of a variant not in the cache: error = %v", err) + + if !errors.Is(err, ErrNegativeCached) { + t.Errorf("Get() of a variant not in the cache error = %v, want %v", + err, ErrNegativeCached) + } +} + func TestService_Get_ReturnsErrorForNegativeCachedURL(t *testing.T) { t.Parallel()