hsa-app/internal/storage/snapshot_test.go
Jean-Michel Tremblay 8b7252dad4 Initial commit: HSA receipt tracker
Go app for capturing and archiving HSA-eligible receipts: OIDC/PKCE auth
against Authelia, SQLite storage with dual-write (filesystem + DB blob),
mobile-first upload, and DB export.

Adds AI receipt classification: a config.json catalog of people and
categories (seeded into the DB on startup), a prompt builder that derives
name-order/initial variants from the data (with same-surname ambiguity
handling), and an Anthropic tool-use client behind POST /classify. Tests
run against a mock endpoint; a live integration test is env-gated to the
cheapest model.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 21:40:12 -04:00

71 lines
1.5 KiB
Go

package storage
import (
"path/filepath"
"testing"
)
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)
}
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 {
t.Errorf("blob not stripped: got %d bytes", len(got.ImageData))
}
if got.AmountCents != r.AmountCents {
t.Errorf("metadata lost: amount = %d, want %d", got.AmountCents, r.AmountCents)
}
}