signal-bot/main_test.go
copilot-swe-agent[bot] 4e260c0551 Add Go implementation of Signal Bot for improved performance
Co-authored-by: kuhyx <147418882+kuhyx@users.noreply.github.com>
2025-12-01 15:15:42 +00:00

208 lines
5.1 KiB
Go

package main
import (
"testing"
)
func TestNewStringCounter(t *testing.T) {
counter := NewStringCounter()
if counter == nil {
t.Fatal("NewStringCounter returned nil")
}
if counter.StringMap == nil {
t.Fatal("StringMap is nil")
}
if len(counter.StringMap) != 0 {
t.Fatal("StringMap should be empty")
}
}
func TestUpdateStringMap(t *testing.T) {
counter := NewStringCounter()
// First update
result := counter.UpdateStringMap("uuid1", "Alice")
if result["uuid1"].Count != 1 {
t.Errorf("Expected count 1, got %d", result["uuid1"].Count)
}
if result["uuid1"].CommonName != "Alice" {
t.Errorf("Expected name 'Alice', got '%s'", result["uuid1"].CommonName)
}
// Second update for same user
result = counter.UpdateStringMap("uuid1", "Alice")
if result["uuid1"].Count != 2 {
t.Errorf("Expected count 2, got %d", result["uuid1"].Count)
}
// Update for different user
result = counter.UpdateStringMap("uuid2", "Bob")
if result["uuid2"].Count != 1 {
t.Errorf("Expected count 1 for Bob, got %d", result["uuid2"].Count)
}
if len(result) != 2 {
t.Errorf("Expected 2 users, got %d", len(result))
}
}
func TestGetCommonName(t *testing.T) {
counter := NewStringCounter()
counter.UpdateStringMap("uuid1", "Alice")
name := counter.GetCommonName("uuid1")
if name != "Alice" {
t.Errorf("Expected 'Alice', got '%s'", name)
}
name = counter.GetCommonName("nonexistent")
if name != "" {
t.Errorf("Expected empty string, got '%s'", name)
}
}
func TestReset(t *testing.T) {
counter := NewStringCounter()
counter.UpdateStringMap("uuid1", "Alice")
counter.UpdateStringMap("uuid2", "Bob")
counter.Reset()
if len(counter.StringMap) != 0 {
t.Errorf("Expected empty map after reset, got %d entries", len(counter.StringMap))
}
}
func TestGetStringMapJSON(t *testing.T) {
counter := NewStringCounter()
// Empty map
json := counter.GetStringMapJSON()
if json != "{}" {
t.Errorf("Expected '{}', got '%s'", json)
}
// With data
counter.UpdateStringMap("uuid1", "Alice")
json = counter.GetStringMapJSON()
if json == "{}" {
t.Error("Expected non-empty JSON")
}
}
func TestContainsString(t *testing.T) {
slice := []string{"apple", "banana", "cherry"}
if !containsString(slice, "banana") {
t.Error("Expected to find 'banana'")
}
if containsString(slice, "orange") {
t.Error("Did not expect to find 'orange'")
}
if containsString(nil, "apple") {
t.Error("Expected false for nil slice")
}
if containsString([]string{}, "apple") {
t.Error("Expected false for empty slice")
}
}
func TestCatCommands(t *testing.T) {
expectedCommands := []string{"!kot", "!cat", "!meow"}
for _, cmd := range expectedCommands {
if !containsString(catCommands, cmd) {
t.Errorf("Expected '%s' to be in catCommands", cmd)
}
}
}
func TestDogCommands(t *testing.T) {
expectedCommands := []string{"!pies", "!dog", "!woof"}
for _, cmd := range expectedCommands {
if !containsString(dogCommands, cmd) {
t.Errorf("Expected '%s' to be in dogCommands", cmd)
}
}
}
func TestIsMessageReaction(t *testing.T) {
// Message without reaction
msg := &SignalMessage{}
if isMessageReaction(msg) {
t.Error("Expected false for message without reaction")
}
// Message with data message reaction
msg = &SignalMessage{}
msg.Envelope.DataMessage.Reaction.Emoji = "👍"
if !isMessageReaction(msg) {
t.Error("Expected true for message with data message reaction")
}
// Message with sync message reaction
msg = &SignalMessage{}
msg.Envelope.SyncMessage.Reaction.Emoji = "❤️"
if !isMessageReaction(msg) {
t.Error("Expected true for message with sync message reaction")
}
}
func TestShouldCount(t *testing.T) {
// Normal message should count
msg := &SignalMessage{}
msg.Envelope.DataMessage.Message = "Hello"
if !shouldCount(msg) {
t.Error("Expected normal message to be counted")
}
// Message with sticker should not count
msg = &SignalMessage{}
msg.Envelope.DataMessage.Sticker.PackID = "some-pack-id"
if shouldCount(msg) {
t.Error("Expected message with sticker to not be counted")
}
// Sync message with sticker should not count
msg = &SignalMessage{}
msg.Envelope.SyncMessage.SentMessage.Sticker.PackID = "some-pack-id"
if shouldCount(msg) {
t.Error("Expected sync message with sticker to not be counted")
}
}
func TestExtractMessageContent(t *testing.T) {
// Test data message extraction
msg := &SignalMessage{}
msg.Envelope.DataMessage.Timestamp = 12345
msg.Envelope.DataMessage.Message = "Hello"
content := extractMessageContent(msg)
if content == nil {
t.Fatal("Expected content to be non-nil")
}
if content.Message != "Hello" {
t.Errorf("Expected message 'Hello', got '%s'", content.Message)
}
// Test sync message extraction
msg = &SignalMessage{}
msg.Envelope.SyncMessage.SentMessage.Timestamp = 12345
msg.Envelope.SyncMessage.SentMessage.Message = "World"
content = extractMessageContent(msg)
if content == nil {
t.Fatal("Expected content to be non-nil")
}
if content.Message != "World" {
t.Errorf("Expected message 'World', got '%s'", content.Message)
}
// Test empty message
msg = &SignalMessage{}
content = extractMessageContent(msg)
if content != nil {
t.Error("Expected nil for empty message")
}
}