From c6e66bf45aa619b56275118ea904b84fe49be041 Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Tue, 29 Sep 2026 09:31:47 +0000 Subject: [PATCH] Test that a read of an older .meta file keeps the stored content type (closes #70) A read that finds no .meta file keeps nothing in memory, so the existing test no longer reaches the step that keeps a type StoreVariant put there. This one writes a .meta file with a different content type after the store, as one not yet rewritten by it would hold, and checks that the read leaves the stored type in memory. Model: opus-5-5 --- internal/imgcache/metacache_internal_test.go | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/internal/imgcache/metacache_internal_test.go b/internal/imgcache/metacache_internal_test.go index 14be0f4..bade726 100644 --- a/internal/imgcache/metacache_internal_test.go +++ b/internal/imgcache/metacache_internal_test.go @@ -2,6 +2,7 @@ package imgcache import ( "bytes" + "encoding/json" "errors" "fmt" "io" @@ -155,6 +156,54 @@ func TestReadDuringStoreKeepsStoredContentType(t *testing.T) { assertVariantServed(t, cache, testVariantKeyOne, content) } +// TestReadOfOlderMetaFileKeepsStoredContentType checks that a read +// which got its content type from a .meta file that StoreVariant had +// not yet rewritten cannot replace the type the store kept in memory. +// The test writes such a .meta file, with a different content type, +// after the store, then runs the part of GetVariant that comes after +// its check of memory. +func TestReadOfOlderMetaFileKeepsStoredContentType(t *testing.T) { + t.Parallel() + + cache, _ := newEvictionTestCache(t, 1<<20) + content := []byte("webp variant bytes") + + storeEvictionTestVariant(t, cache, testVariantKeyOne, content) + + olderMeta, err := json.Marshal(VariantMeta{ + ContentType: testContentTypeJPEG, + Size: int64(len(content)), + }) + if err != nil { + t.Fatalf("encoding .meta file: %v", err) + } + + metaPath := cache.variants.keyToPath(testVariantKeyOne) + ".meta" + + err = os.WriteFile(metaPath, olderMeta, StorageFilePerm) + if err != nil { + t.Fatalf("writing .meta file: %v", err) + } + + reader, _, contentType, err := cache.loadVariantWithMeta(testVariantKeyOne) + if err != nil { + t.Fatalf("loadVariantWithMeta(%s) error = %v", testVariantKeyOne, err) + } + + _ = reader.Close() + + if contentType != testContentTypeJPEG { + t.Fatalf("loadVariantWithMeta(%s) content type = %q, want %q from the .meta file", + testVariantKeyOne, contentType, testContentTypeJPEG) + } + + kept, _ := cache.metaCache.Get(testVariantKeyOne) + if kept != testContentTypeWebP { + t.Errorf("content type in memory = %q, want the stored %q", + kept, testContentTypeWebP) + } +} + // TestFailedReadDuringStoreKeepsStoredContentType checks that a read // which found no .meta file cannot leave application/octet-stream in // memory, even when another read has removed the content type