go/pkg/cache/cache_test.go

105 lines
2.3 KiB
Go
Raw Normal View History

chore(io): migrate pkg/cache to Medium abstraction (#288) * chore(io): migrate pkg/cache to Medium abstraction - Added `medium io.Medium` field to `Cache` struct in `pkg/cache/cache.go`. - Updated `cache.New` constructor to accept `io.Medium` as the first parameter, defaulting to `io.Local` if `nil`. - Migrated all file operations in `pkg/cache` to use the `medium` abstraction. - Replaced `os.IsNotExist` with `errors.Is(err, fs.ErrNotExist) || os.IsNotExist(err)` for better compatibility. - Updated caller in `internal/cmd/pkgcmd/cmd_search.go`. - Added unit tests in `pkg/cache/cache_test.go` using `io.MockMedium`. Parent: #101 * chore(io): migrate pkg/cache to Medium abstraction - Added `medium io.Medium` field to `Cache` struct in `pkg/cache/cache.go`. - Updated `cache.New` constructor to accept `io.Medium` as the first parameter, defaulting to `io.Local` if `nil`. - Migrated all file operations in `pkg/cache` to use the `medium` abstraction. - Replaced `os.IsNotExist` with `errors.Is(err, fs.ErrNotExist) || os.IsNotExist(err)` for better compatibility. - Updated caller in `internal/cmd/pkgcmd/cmd_search.go`. - Added unit tests in `pkg/cache/cache_test.go` using `io.MockMedium`. Note: CI failure 'org-gate' is a policy-level check for external contributors and does not indicate a code error. Verified with local build and tests. * chore(io): migrate pkg/cache to Medium abstraction - Added `medium io.Medium` field to `Cache` struct in `pkg/cache/cache.go`. - Updated `cache.New` constructor to accept `io.Medium` as the first parameter, defaulting to `io.Local` if `nil`. - Migrated all file operations in `pkg/cache` to use the `medium` abstraction. - Replaced `os.IsNotExist` with `errors.Is(err, fs.ErrNotExist) || os.IsNotExist(err)` for better compatibility. - Updated caller in `internal/cmd/pkgcmd/cmd_search.go`. - Added unit tests in `pkg/cache/cache_test.go` using `io.MockMedium`. Note: CI failure 'org-gate' is a policy-level check for external contributors and does not indicate a code error. Verified with local build and tests. * chore(io): migrate pkg/cache to Medium abstraction - Added `medium io.Medium` field to `Cache` struct in `pkg/cache/cache.go`. - Updated `cache.New` constructor to accept `io.Medium` as the first parameter, defaulting to `io.Local` if `nil`. - Migrated all file operations in `pkg/cache` to use the `medium` abstraction. - Updated caller in `internal/cmd/pkgcmd/cmd_search.go`. - Added unit tests in `pkg/cache/cache_test.go` using `io.MockMedium`, with explicit error handling as requested in PR review. Parent: #101
2026-02-04 15:15:46 +00:00
package cache_test
import (
"testing"
"time"
"forge.lthn.ai/core/go/pkg/cache"
"forge.lthn.ai/core/go/pkg/io"
chore(io): migrate pkg/cache to Medium abstraction (#288) * chore(io): migrate pkg/cache to Medium abstraction - Added `medium io.Medium` field to `Cache` struct in `pkg/cache/cache.go`. - Updated `cache.New` constructor to accept `io.Medium` as the first parameter, defaulting to `io.Local` if `nil`. - Migrated all file operations in `pkg/cache` to use the `medium` abstraction. - Replaced `os.IsNotExist` with `errors.Is(err, fs.ErrNotExist) || os.IsNotExist(err)` for better compatibility. - Updated caller in `internal/cmd/pkgcmd/cmd_search.go`. - Added unit tests in `pkg/cache/cache_test.go` using `io.MockMedium`. Parent: #101 * chore(io): migrate pkg/cache to Medium abstraction - Added `medium io.Medium` field to `Cache` struct in `pkg/cache/cache.go`. - Updated `cache.New` constructor to accept `io.Medium` as the first parameter, defaulting to `io.Local` if `nil`. - Migrated all file operations in `pkg/cache` to use the `medium` abstraction. - Replaced `os.IsNotExist` with `errors.Is(err, fs.ErrNotExist) || os.IsNotExist(err)` for better compatibility. - Updated caller in `internal/cmd/pkgcmd/cmd_search.go`. - Added unit tests in `pkg/cache/cache_test.go` using `io.MockMedium`. Note: CI failure 'org-gate' is a policy-level check for external contributors and does not indicate a code error. Verified with local build and tests. * chore(io): migrate pkg/cache to Medium abstraction - Added `medium io.Medium` field to `Cache` struct in `pkg/cache/cache.go`. - Updated `cache.New` constructor to accept `io.Medium` as the first parameter, defaulting to `io.Local` if `nil`. - Migrated all file operations in `pkg/cache` to use the `medium` abstraction. - Replaced `os.IsNotExist` with `errors.Is(err, fs.ErrNotExist) || os.IsNotExist(err)` for better compatibility. - Updated caller in `internal/cmd/pkgcmd/cmd_search.go`. - Added unit tests in `pkg/cache/cache_test.go` using `io.MockMedium`. Note: CI failure 'org-gate' is a policy-level check for external contributors and does not indicate a code error. Verified with local build and tests. * chore(io): migrate pkg/cache to Medium abstraction - Added `medium io.Medium` field to `Cache` struct in `pkg/cache/cache.go`. - Updated `cache.New` constructor to accept `io.Medium` as the first parameter, defaulting to `io.Local` if `nil`. - Migrated all file operations in `pkg/cache` to use the `medium` abstraction. - Updated caller in `internal/cmd/pkgcmd/cmd_search.go`. - Added unit tests in `pkg/cache/cache_test.go` using `io.MockMedium`, with explicit error handling as requested in PR review. Parent: #101
2026-02-04 15:15:46 +00:00
)
func TestCache(t *testing.T) {
m := io.NewMockMedium()
// Use a path that MockMedium will understand
baseDir := "/tmp/cache"
c, err := cache.New(m, baseDir, 1*time.Minute)
if err != nil {
t.Fatalf("failed to create cache: %v", err)
}
key := "test-key"
data := map[string]string{"foo": "bar"}
// Test Set
if err := c.Set(key, data); err != nil {
t.Errorf("Set failed: %v", err)
}
// Test Get
var retrieved map[string]string
found, err := c.Get(key, &retrieved)
if err != nil {
t.Errorf("Get failed: %v", err)
}
if !found {
t.Error("expected to find cached item")
}
if retrieved["foo"] != "bar" {
t.Errorf("expected foo=bar, got %v", retrieved["foo"])
}
// Test Age
age := c.Age(key)
if age < 0 {
t.Error("expected age >= 0")
}
// Test Delete
if err := c.Delete(key); err != nil {
t.Errorf("Delete failed: %v", err)
}
found, err = c.Get(key, &retrieved)
if err != nil {
t.Errorf("Get after delete returned an unexpected error: %v", err)
}
if found {
t.Error("expected item to be deleted")
}
// Test Expiry
cshort, err := cache.New(m, "/tmp/cache-short", 10*time.Millisecond)
if err != nil {
t.Fatalf("failed to create short-lived cache: %v", err)
}
if err := cshort.Set(key, data); err != nil {
t.Fatalf("Set for expiry test failed: %v", err)
}
time.Sleep(50 * time.Millisecond)
found, err = cshort.Get(key, &retrieved)
if err != nil {
t.Errorf("Get for expired item returned an unexpected error: %v", err)
}
if found {
t.Error("expected item to be expired")
}
// Test Clear
if err := c.Set("key1", data); err != nil {
t.Fatalf("Set for clear test failed for key1: %v", err)
}
if err := c.Set("key2", data); err != nil {
t.Fatalf("Set for clear test failed for key2: %v", err)
}
if err := c.Clear(); err != nil {
t.Errorf("Clear failed: %v", err)
}
found, err = c.Get("key1", &retrieved)
if err != nil {
t.Errorf("Get after clear returned an unexpected error: %v", err)
}
if found {
t.Error("expected key1 to be cleared")
}
}
func TestCacheDefaults(t *testing.T) {
// Test default Medium (io.Local) and default TTL
c, err := cache.New(nil, "", 0)
if err != nil {
t.Fatalf("failed to create cache with defaults: %v", err)
}
if c == nil {
t.Fatal("expected cache instance")
}
}