2026-06-18 01:40:12 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
2026-06-19 11:12:50 +00:00
|
|
|
|
|
|
|
|
"maisym.com/hsa/internal/receipt"
|
2026-06-18 01:40:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSnapshotWithBlobs(t *testing.T) {
|
|
|
|
|
s := newTestStore(t)
|
|
|
|
|
r := sampleReceipt(firstCategoryID(t, s))
|
|
|
|
|
if err := s.Insert(r); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dest := filepath.Join(t.TempDir(), "snap.db")
|
|
|
|
|
if err := s.SnapshotTo(dest, true); err != nil {
|
|
|
|
|
t.Fatalf("SnapshotTo: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snap, err := Open(dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer snap.Close()
|
|
|
|
|
|
|
|
|
|
got, err := snap.Get(r.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if string(got.ImageData) != string(r.ImageData) {
|
|
|
|
|
t.Errorf("blob not preserved: got %q", got.ImageData)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSnapshotWithoutBlobs(t *testing.T) {
|
|
|
|
|
s := newTestStore(t)
|
|
|
|
|
r := sampleReceipt(firstCategoryID(t, s))
|
|
|
|
|
if err := s.Insert(r); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-06-19 11:12:50 +00:00
|
|
|
// An attachment blob must be stripped too.
|
|
|
|
|
att := receipt.Attachment{
|
|
|
|
|
ID: "att1", ReceiptID: r.ID, UploadedBy: "jm", UploadedAt: r.UploadedAt,
|
|
|
|
|
FilePath: "attachments/2026/x.png", ImageData: []byte("blobbytes"),
|
|
|
|
|
FileSizeBytes: 9, OriginalFilename: "x.png", MimeType: "image/png",
|
|
|
|
|
}
|
|
|
|
|
if err := s.InsertAttachment(att); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-06-18 01:40:12 +00:00
|
|
|
|
|
|
|
|
dest := filepath.Join(t.TempDir(), "snap-noblob.db")
|
|
|
|
|
if err := s.SnapshotTo(dest, false); err != nil {
|
|
|
|
|
t.Fatalf("SnapshotTo: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snap, err := Open(dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer snap.Close()
|
|
|
|
|
|
|
|
|
|
// Metadata row survives, but the blob is stripped.
|
|
|
|
|
n, err := snap.CountActive()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if n != 1 {
|
|
|
|
|
t.Errorf("CountActive = %d, want 1 (metadata should survive)", n)
|
|
|
|
|
}
|
|
|
|
|
got, err := snap.Get(r.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(got.ImageData) != 0 {
|
2026-06-19 11:12:50 +00:00
|
|
|
t.Errorf("receipt blob not stripped: got %d bytes", len(got.ImageData))
|
2026-06-18 01:40:12 +00:00
|
|
|
}
|
|
|
|
|
if got.AmountCents != r.AmountCents {
|
|
|
|
|
t.Errorf("metadata lost: amount = %d, want %d", got.AmountCents, r.AmountCents)
|
|
|
|
|
}
|
2026-06-19 11:12:50 +00:00
|
|
|
// Attachment metadata survives, blob stripped.
|
|
|
|
|
gotAtt, err := snap.GetAttachment("att1")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(gotAtt.ImageData) != 0 {
|
|
|
|
|
t.Errorf("attachment blob not stripped: got %d bytes", len(gotAtt.ImageData))
|
|
|
|
|
}
|
|
|
|
|
if gotAtt.OriginalFilename != "x.png" {
|
|
|
|
|
t.Errorf("attachment metadata lost: %+v", gotAtt)
|
|
|
|
|
}
|
2026-06-18 01:40:12 +00:00
|
|
|
}
|