diff --git a/internal/db/cache_test.go b/internal/db/cache_test.go new file mode 100644 index 00000000..f534bc0a --- /dev/null +++ b/internal/db/cache_test.go @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: BSD-2-Clause +// +// Copyright (c) 2025 The FreeBSD Foundation. +// +// This software was developed by Hayzam Sherif +// of Alchemilla Ventures Pvt. Ltd. , +// under sponsorship from the FreeBSD Foundation. + +package db + +import ( + "bytes" + "testing" + "time" + + "github.com/alchemillahq/sylve/internal" + "github.com/dgraph-io/badger/v4" +) + +func TestSetupCacheAssignsGlobalAndPersistsRoundTrip(t *testing.T) { + prev := CacheDB + cfg := &internal.SylveConfig{ + DataPath: t.TempDir(), + } + + cache := SetupCache(cfg) + if cache == nil { + t.Fatal("expected non-nil cache instance") + } + + if CacheDB != cache { + t.Fatal("expected SetupCache to assign CacheDB global") + } + + t.Cleanup(func() { + _ = cache.Close() + CacheDB = prev + }) + + if err := SetValue("setup/probe", []byte("ok"), 30); err != nil { + t.Fatalf("failed_to_set_probe_value: %v", err) + } + + got, ok := GetValue("setup/probe") + if !ok { + t.Fatal("expected probe key to exist") + } + if !bytes.Equal(got, []byte("ok")) { + t.Fatalf("expected probe value %q, got %q", "ok", string(got)) + } +} + +func TestSetValueAndGetValueRoundTrip(t *testing.T) { + _ = installTempCacheDB(t) + + want := []byte("hello-cache") + if err := SetValue("greeting", want, 60); err != nil { + t.Fatalf("set_value_failed: %v", err) + } + + got, ok := GetValue("greeting") + if !ok { + t.Fatal("expected key to exist") + } + if !bytes.Equal(got, want) { + t.Fatalf("expected value %q, got %q", string(want), string(got)) + } +} + +func TestGetValueMissingKeyReturnsFalse(t *testing.T) { + _ = installTempCacheDB(t) + + got, ok := GetValue("missing") + if ok { + t.Fatal("expected missing key lookup to return ok=false") + } + if got != nil { + t.Fatalf("expected nil value for missing key, got %q", string(got)) + } +} + +func TestSetValueWithTTLExpires(t *testing.T) { + _ = installTempCacheDB(t) + + if err := SetValue("ephemeral", []byte("soon-gone"), 1); err != nil { + t.Fatalf("set_value_failed: %v", err) + } + + time.Sleep(2 * time.Second) + + got, ok := GetValue("ephemeral") + if ok { + t.Fatalf("expected key to expire, got value=%q", string(got)) + } +} + +func TestSetValueReturnsErrorAfterCacheClose(t *testing.T) { + cache := installTempCacheDB(t) + + if err := cache.Close(); err != nil { + t.Fatalf("failed_to_close_cache: %v", err) + } + + if err := SetValue("closed", []byte("x"), 10); err == nil { + t.Fatal("expected SetValue to fail on closed cache") + } +} + +func TestRunCacheGCDoesNotHang(t *testing.T) { + _ = installTempCacheDB(t) + + done := make(chan struct{}) + go func() { + RunCacheGC() + close(done) + }() + + select { + case <-done: + case <-time.After(2 * time.Second): + t.Fatal("RunCacheGC did not return in time") + } +} + +func installTempCacheDB(t *testing.T) *badger.DB { + t.Helper() + + prev := CacheDB + opts := badger.DefaultOptions(t.TempDir()). + WithLoggingLevel(badger.ERROR). + WithDetectConflicts(false) + + cache, err := badger.Open(opts) + if err != nil { + t.Fatalf("failed_to_open_badger_test_cache: %v", err) + } + + CacheDB = cache + t.Cleanup(func() { + _ = cache.Close() + CacheDB = prev + }) + + return cache +} diff --git a/internal/db/db.go b/internal/db/db.go index d8a9297e..1b9fcab0 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -71,6 +71,12 @@ func SetupDatabase(cfg *internal.SylveConfig, isTest bool) *gorm.DB { db.Exec("PRAGMA journal_mode = WAL") db.Exec("PRAGMA synchronous = NORMAL") + // Pre-migration fixups use the migrations tracking table, so ensure it + // exists before running any pre-migration logic. + if err := db.AutoMigrate(&models.Migrations{}); err != nil { + logger.L.Fatal().Msgf("Error bootstrapping migrations table: %v", err) + } + PreMigrationFixups(db) err = db.AutoMigrate(