152 lines
3.9 KiB
Go
152 lines
3.9 KiB
Go
package messages
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestConstants(t *testing.T) {
|
|
// Test that all constants are defined correctly
|
|
constants := map[string]string{
|
|
"Duplicate": Duplicate,
|
|
"Pow": Pow,
|
|
"Blocked": Blocked,
|
|
"RateLimited": RateLimited,
|
|
"Invalid": Invalid,
|
|
"Error": Error,
|
|
}
|
|
|
|
expected := map[string]string{
|
|
"Duplicate": "duplicate",
|
|
"Pow": "pow",
|
|
"Blocked": "blocked",
|
|
"RateLimited": "rate-limited",
|
|
"Invalid": "invalid",
|
|
"Error": "error",
|
|
}
|
|
|
|
for name, actual := range constants {
|
|
if actual != expected[name] {
|
|
t.Errorf("Constant %s = %q, want %q", name, actual, expected[name])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExamples(t *testing.T) {
|
|
// Test that Examples slice is not empty and contains expected messages
|
|
if len(Examples) == 0 {
|
|
t.Fatal("Examples slice should not be empty")
|
|
}
|
|
|
|
expectedCount := 8
|
|
if len(Examples) != expectedCount {
|
|
t.Errorf("Examples length = %d, want %d", len(Examples), expectedCount)
|
|
}
|
|
|
|
// Test that all examples are non-empty
|
|
for i, example := range Examples {
|
|
if len(example) == 0 {
|
|
t.Errorf("Example at index %d should not be empty", i)
|
|
}
|
|
}
|
|
|
|
// Test specific examples to ensure they contain expected prefixes
|
|
exampleStrings := make([]string, len(Examples))
|
|
for i, example := range Examples {
|
|
exampleStrings[i] = string(example)
|
|
}
|
|
|
|
// Check that examples contain the expected prefixes
|
|
prefixFound := make(map[string]bool)
|
|
for _, example := range exampleStrings {
|
|
if bytes.HasPrefix([]byte(example), []byte(Pow+":")) {
|
|
prefixFound[Pow] = true
|
|
}
|
|
if bytes.HasPrefix([]byte(example), []byte(Duplicate+":")) {
|
|
prefixFound[Duplicate] = true
|
|
}
|
|
if bytes.HasPrefix([]byte(example), []byte(Blocked+":")) {
|
|
prefixFound[Blocked] = true
|
|
}
|
|
if bytes.HasPrefix([]byte(example), []byte(RateLimited+":")) {
|
|
prefixFound[RateLimited] = true
|
|
}
|
|
if bytes.HasPrefix([]byte(example), []byte(Invalid+":")) {
|
|
prefixFound[Invalid] = true
|
|
}
|
|
if bytes.HasPrefix([]byte(example), []byte(Error+":")) {
|
|
prefixFound[Error] = true
|
|
}
|
|
}
|
|
|
|
// Verify that we have examples for most prefixes
|
|
expectedPrefixes := []string{Pow, Duplicate, Blocked, RateLimited, Invalid, Error}
|
|
for _, prefix := range expectedPrefixes {
|
|
if !prefixFound[prefix] {
|
|
t.Errorf("No example found with prefix %q", prefix)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRandomMessage(t *testing.T) {
|
|
// Test that RandomMessage returns valid messages from Examples
|
|
for i := 0; i < 100; i++ {
|
|
msg := RandomMessage()
|
|
|
|
// Check that the returned message is one of the Examples
|
|
found := false
|
|
for _, example := range Examples {
|
|
if bytes.Equal(msg, example) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
t.Errorf("RandomMessage() returned %q, which is not in Examples", string(msg))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRandomMessageDistribution(t *testing.T) {
|
|
// Test that RandomMessage can return all examples (distribution test)
|
|
// This test runs many iterations to increase the chance of hitting all examples
|
|
const iterations = 10000
|
|
found := make(map[string]bool)
|
|
|
|
for i := 0; i < iterations; i++ {
|
|
msg := RandomMessage()
|
|
found[string(msg)] = true
|
|
}
|
|
|
|
// Check that we found a reasonable number of different messages
|
|
// (not necessarily all, due to randomness, but should be more than 1)
|
|
if len(found) < 2 {
|
|
t.Errorf("RandomMessage() distribution test: found only %d unique messages in %d iterations", len(found), iterations)
|
|
}
|
|
|
|
// Verify all found messages are valid
|
|
for msgStr := range found {
|
|
msg := []byte(msgStr)
|
|
validMsg := false
|
|
for _, example := range Examples {
|
|
if bytes.Equal(msg, example) {
|
|
validMsg = true
|
|
break
|
|
}
|
|
}
|
|
if !validMsg {
|
|
t.Errorf("RandomMessage() returned invalid message: %q", msgStr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRandomMessageNotNil(t *testing.T) {
|
|
// Test that RandomMessage never returns nil
|
|
for i := 0; i < 10; i++ {
|
|
msg := RandomMessage()
|
|
if msg == nil {
|
|
t.Error("RandomMessage() should never return nil")
|
|
}
|
|
}
|
|
} |