sonda/internal/storage/waveform_test.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

60 lines
2.1 KiB
Go

package storage
import (
"bytes"
"context"
"testing"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/internal/waveform"
)
func TestTheShapeOfATrackIsKeptAndReplaced(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewWaveformRepo(db)
var id int64
if err := db.QueryRow("INSERT INTO tracks (title) VALUES ('song') RETURNING id").Scan(&id); err != nil {
t.Fatal(err)
}
if _, found, err := repo.Get(ctx, id); err != nil || found {
t.Fatalf("before any save: found %v, %v", found, err)
}
peaks := bytes.Repeat([]byte{0, 128, 255}, 40)
if err := repo.Save(ctx, id, waveform.Record{SizeBytes: 123456, MTimeUnix: 1_700_000_000, Peaks: peaks}); err != nil {
t.Fatal(err)
}
got, found, err := repo.Get(ctx, id)
if err != nil || !found || got.SizeBytes != 123456 || got.MTimeUnix != 1_700_000_000 || !bytes.Equal(got.Peaks, peaks) {
t.Fatalf("Get = %+v, %v, %v", got, found, err)
}
// Saving again replaces it (the file changed).
if err := repo.Save(ctx, id, waveform.Record{SizeBytes: 7, MTimeUnix: 8, Peaks: nil}); err != nil {
t.Fatal(err)
}
got, _, _ = repo.Get(ctx, id)
if got.SizeBytes != 7 || got.MTimeUnix != 8 || len(got.Peaks) != 0 {
t.Errorf("after the second save: %+v: no peaks means the file cannot be decoded, and that is kept too", got)
}
// A track that goes takes its shape with it.
if _, err := db.Exec("DELETE FROM tracks WHERE id = ?", id); err != nil {
t.Fatal(err)
}
if _, found, _ := repo.Get(ctx, id); found {
t.Error("the shape of a deleted track is still kept")
}
})
}
func TestTheShapeOfATrackThatIsGoneIsNotAnError(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
repo := NewWaveformRepo(db)
if err := repo.Save(context.Background(), 999, waveform.Record{SizeBytes: 1, MTimeUnix: 1, Peaks: []byte{1}}); err != nil {
t.Errorf("Save for a track that is not there: %v", err)
}
if _, found, _ := repo.Get(context.Background(), 999); found {
t.Error("a shape was kept for a track that is not there")
}
})
}