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", "") // 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", "") // 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) } }