Files
minstrel/internal/playevents/contextual_likes_test.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

125 lines
4.4 KiB
Go

package playevents
import (
"context"
"encoding/json"
"io"
"log/slog"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
)
func TestCaptureContextualLikeIfPlaying_NoOpenEvent_NoOp(t *testing.T) {
f := newFixture(t, 200_000)
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
q := dbq.New(f.pool)
if err := CaptureContextualLikeIfPlaying(context.Background(), q, f.user, f.track, logger); err != nil {
t.Fatalf("err: %v", err)
}
var count int
_ = f.pool.QueryRow(context.Background(), "SELECT count(*) FROM contextual_likes WHERE user_id=$1", f.user).Scan(&count)
if count != 0 {
t.Errorf("count = %d, want 0 (no open play_event)", count)
}
}
func TestCaptureContextualLikeIfPlaying_OpenEventWithVector_Inserts(t *testing.T) {
f := newFixture(t, 200_000)
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
// Start a play (creates an open play_event with a populated vector).
at := time.Now().UTC()
_, err := f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", at)
if err != nil {
t.Fatalf("RecordPlayStarted: %v", err)
}
// Like the same track — captures contextual signal.
q := dbq.New(f.pool)
if err := CaptureContextualLikeIfPlaying(context.Background(), q, f.user, f.track, logger); err != nil {
t.Fatalf("Capture: %v", err)
}
var rawVec []byte
if err := f.pool.QueryRow(context.Background(),
"SELECT session_vector FROM contextual_likes WHERE user_id=$1 AND track_id=$2",
f.user, f.track).Scan(&rawVec); err != nil {
t.Fatalf("query: %v", err)
}
var vec map[string]any
_ = json.Unmarshal(rawVec, &vec)
if vec == nil {
t.Errorf("vector empty: %s", rawVec)
}
}
func TestSoftDeleteContextualLikes_MarksActiveRowsDeleted(t *testing.T) {
f := newFixture(t, 200_000)
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
at := time.Now().UTC()
_, _ = f.w.RecordPlayStarted(context.Background(), f.user, f.track, "c", at)
q := dbq.New(f.pool)
_ = CaptureContextualLikeIfPlaying(context.Background(), q, f.user, f.track, logger)
// Now soft-delete.
if err := SoftDeleteContextualLikes(context.Background(), q, f.user, f.track); err != nil {
t.Fatalf("SoftDelete: %v", err)
}
var deletedAt pgtype.Timestamptz
if err := f.pool.QueryRow(context.Background(),
"SELECT deleted_at FROM contextual_likes WHERE user_id=$1 AND track_id=$2",
f.user, f.track).Scan(&deletedAt); err != nil {
t.Fatalf("query: %v", err)
}
if !deletedAt.Valid {
t.Errorf("deleted_at not set after SoftDelete")
}
}
// An unlike reaches the context captured on any copy of the song: the like
// is on the song, and the heart can be tapped on a different copy than the
// one that was playing.
func TestSoftDeleteContextualLikes_ReachesEveryCopyOfTheSong(t *testing.T) {
f := newFixture(t, 200_000)
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
played := f.track
var album, artist pgtype.UUID
if err := f.pool.QueryRow(ctx, "SELECT album_id, artist_id FROM tracks WHERE id = $1", played).Scan(&album, &artist); err != nil {
t.Fatalf("read track: %v", err)
}
other, err := f.q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "Track", AlbumID: album, ArtistID: artist, FilePath: "/tmp/track-fixture-copy.flac", DurationMs: 200_000,
})
if err != nil {
t.Fatalf("copy: %v", err)
}
if _, err := f.pool.Exec(ctx, "UPDATE tracks SET song_id = $1 WHERE id = $2", played, other.ID); err != nil {
t.Fatalf("link copies: %v", err)
}
_, _ = f.w.RecordPlayStarted(ctx, f.user, played, "c", time.Now().UTC())
if _, err := f.q.LikeTrack(ctx, dbq.LikeTrackParams{UserID: f.user, TrackID: played}); err != nil {
t.Fatalf("like: %v", err)
}
if err := CaptureContextualLikeIfPlaying(ctx, f.q, f.user, played, logger); err != nil {
t.Fatalf("capture: %v", err)
}
// Unliked through the other copy, as from another album holding the song.
if _, err := f.q.UnlikeTrack(ctx, dbq.UnlikeTrackParams{UserID: f.user, TrackID: other.ID}); err != nil {
t.Fatalf("unlike: %v", err)
}
if err := SoftDeleteContextualLikes(ctx, f.q, f.user, other.ID); err != nil {
t.Fatalf("SoftDelete: %v", err)
}
var active int
if err := f.pool.QueryRow(ctx,
"SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND deleted_at IS NULL", f.user).Scan(&active); err != nil {
t.Fatalf("count: %v", err)
}
if active != 0 {
t.Fatalf("%d contextual likes still active after unliking the song", active)
}
}