package domainblock import ( "testing" "git.nakama.town/fmartingr/butterrobot/internal/model" ) func TestExtractDomains(t *testing.T) { tests := []struct { name string text string expected []string }{ { name: "No URLs", text: "Hello, world!", expected: []string{}, }, { name: "Single URL", text: "Check out https://example.com for more info", expected: []string{"example.com"}, }, { name: "Multiple URLs", text: "Check out https://example.com and http://test.example.org for more info", expected: []string{"example.com", "test.example.org"}, }, { name: "URL with path", text: "Check out https://example.com/path/to/resource", expected: []string{"example.com"}, }, { name: "URL with port", text: "Check out https://example.com:8080/path/to/resource", expected: []string{"example.com"}, }, { name: "URL with subdomain", text: "Check out https://sub.example.com", expected: []string{"sub.example.com"}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { domains := extractDomains(test.text) if len(domains) != len(test.expected) { t.Errorf("Expected %d domains, got %d", len(test.expected), len(domains)) return } for i, domain := range domains { if domain != test.expected[i] { t.Errorf("Expected domain %s, got %s", test.expected[i], domain) } } }) } } func TestOnMessage(t *testing.T) { plugin := New() tests := []struct { name string text string blockedDomains string expectBlocked bool }{ { name: "No blocked domains", text: "Check out https://example.com", blockedDomains: "", expectBlocked: false, }, { name: "No matching domain", text: "Check out https://example.com", blockedDomains: "bad.com, evil.org", expectBlocked: false, }, { name: "Matching domain", text: "Check out https://example.com", blockedDomains: "example.com, evil.org", expectBlocked: true, }, { name: "Matching subdomain", text: "Check out https://sub.example.com", blockedDomains: "example.com", expectBlocked: true, }, { name: "Multiple domains, one matching", text: "Check out https://example.com and https://good.org", blockedDomains: "bad.com, example.com", expectBlocked: true, }, { name: "Spaces in blocked domains list", text: "Check out https://example.com", blockedDomains: "bad.com, example.com , evil.org", expectBlocked: true, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { msg := &model.Message{ Text: test.text, Chat: "test-chat", ID: "test-id", Channel: &model.Channel{ ID: 1, }, } config := map[string]interface{}{ "blocked_domains": test.blockedDomains, } responses := plugin.OnMessage(msg, config) if test.expectBlocked { if responses == nil || len(responses) == 0 { t.Errorf("Expected message to be blocked, but it wasn't") } } else { if responses != nil && len(responses) > 0 { t.Errorf("Expected message not to be blocked, but it was") } } }) } }