mirror of
https://github.com/skidoodle/safebin.git
synced 2026-04-28 03:07:41 +02:00
feat: replace fs scans with bbolt for fast, persistent metadata management
Signed-off-by: skidoodle <contact@albert.lol>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
func TestInitDB(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
db, err := InitDB(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("InitDB failed: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
dbPath := filepath.Join(tmpDir, DBFileName)
|
||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
||||
t.Error("Database file was not created")
|
||||
}
|
||||
|
||||
err = db.View(func(tx *bbolt.Tx) error {
|
||||
b := tx.Bucket([]byte(DBBucketName))
|
||||
if b == nil {
|
||||
t.Errorf("Bucket '%s' was not created", DBBucketName)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("View failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDB_MetadataLifecycle(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
db, err := InitDB(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
app := &App{
|
||||
Conf: Config{StorageDir: tmpDir, MaxMB: 100},
|
||||
DB: db,
|
||||
}
|
||||
|
||||
fileID := "test-file-id"
|
||||
fileSize := int64(1024)
|
||||
|
||||
if err := app.RegisterFile(fileID, fileSize); err != nil {
|
||||
t.Fatalf("RegisterFile failed: %v", err)
|
||||
}
|
||||
|
||||
err = db.View(func(tx *bbolt.Tx) error {
|
||||
b := tx.Bucket([]byte(DBBucketName))
|
||||
data := b.Get([]byte(fileID))
|
||||
if data == nil {
|
||||
t.Fatal("Metadata not found in DB")
|
||||
}
|
||||
|
||||
var meta FileMeta
|
||||
if err := json.Unmarshal(data, &meta); err != nil {
|
||||
t.Fatalf("Failed to unmarshal meta: %v", err)
|
||||
}
|
||||
|
||||
if meta.ID != fileID {
|
||||
t.Errorf("Want ID %s, got %s", fileID, meta.ID)
|
||||
}
|
||||
if meta.Size != fileSize {
|
||||
t.Errorf("Want Size %d, got %d", fileSize, meta.Size)
|
||||
}
|
||||
if meta.ExpiresAt.Before(time.Now()) {
|
||||
t.Error("Expiration time is in the past")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user