Files
Pete/internal/poster/tracker_test.go
prosolis b617d403b7 Exclude !post from the daily cap
Manual !post overrides were counted toward daily_cap_total, so a few
forced posts could starve the round-robin rotation for the rest of the
day. Tag forced rows in post_log and skip them in CountAllPostsInWindow
so the cap only meters the auto-rotation.
2026-05-26 17:15:53 -07:00

90 lines
2.1 KiB
Go

package poster
import (
"path/filepath"
"testing"
"pete/internal/storage"
"maunium.net/go/mautrix/id"
)
func setupTrackerTestDB(t *testing.T) {
t.Helper()
dbPath := filepath.Join(t.TempDir(), "test.db")
if err := storage.Init(dbPath); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { storage.Close() })
}
func TestHandleReaction_KnownPost(t *testing.T) {
setupTrackerTestDB(t)
// Insert a post log entry
storage.InsertPostLog("story-1", "tech", "$post1:example.org", "", false)
// Handle a reaction to that post
HandleReaction(
id.RoomID("!room:example.org"),
id.EventID("$react1:example.org"),
id.EventID("$post1:example.org"),
"👍",
id.UserID("@user:example.org"),
)
// Verify reaction was recorded
var count int
storage.Get().QueryRow(`SELECT COUNT(*) FROM reactions WHERE post_guid = ?`, "story-1").Scan(&count)
if count != 1 {
t.Errorf("expected 1 reaction, got %d", count)
}
}
func TestHandleReaction_UnknownPost(t *testing.T) {
setupTrackerTestDB(t)
// Handle a reaction to an unknown event — should not crash or insert
HandleReaction(
id.RoomID("!room:example.org"),
id.EventID("$react1:example.org"),
id.EventID("$unknown:example.org"),
"👍",
id.UserID("@user:example.org"),
)
var count int
storage.Get().QueryRow(`SELECT COUNT(*) FROM reactions`).Scan(&count)
if count != 0 {
t.Errorf("expected 0 reactions for unknown post, got %d", count)
}
}
func TestHandleReaction_DuplicateIgnored(t *testing.T) {
setupTrackerTestDB(t)
storage.InsertPostLog("story-1", "tech", "$post1:example.org", "", false)
// Send same reaction twice
HandleReaction(
id.RoomID("!room:example.org"),
id.EventID("$react1:example.org"),
id.EventID("$post1:example.org"),
"👍",
id.UserID("@user:example.org"),
)
HandleReaction(
id.RoomID("!room:example.org"),
id.EventID("$react1:example.org"),
id.EventID("$post1:example.org"),
"👍",
id.UserID("@user:example.org"),
)
var count int
storage.Get().QueryRow(`SELECT COUNT(*) FROM reactions`).Scan(&count)
if count != 1 {
t.Errorf("expected 1 reaction (deduped), got %d", count)
}
}