|
| 1 | +package signal |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestNormalizeClientID(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + maxLen int |
| 12 | + expected string |
| 13 | + }{ |
| 14 | + // 有效情况 |
| 15 | + {"simple alphanumeric", "user123", 64, "user123"}, |
| 16 | + {"with hyphen", "user-123", 64, "user-123"}, |
| 17 | + {"with underscore", "user_123", 64, "user_123"}, |
| 18 | + {"uppercase", "USER123", 64, "USER123"}, |
| 19 | + {"mixed case", "User_123-ABC", 64, "User_123-ABC"}, |
| 20 | + {"only numbers", "123456", 64, "123456"}, |
| 21 | + {"single char", "a", 64, "a"}, |
| 22 | + {"exact max length", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 62, "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"}, |
| 23 | + |
| 24 | + // 无效情况 - 应返回空字符串 |
| 25 | + {"empty string", "", 64, ""}, |
| 26 | + {"only spaces", " ", 64, ""}, |
| 27 | + {"with space", "user 123", 64, ""}, |
| 28 | + {"with special char @", "user@123", 64, ""}, |
| 29 | + {"with special char !", "user!123", 64, ""}, |
| 30 | + {"with special char #", "user#123", 64, ""}, |
| 31 | + {"with special char $", "user$123", 64, ""}, |
| 32 | + {"with special char %", "user%123", 64, ""}, |
| 33 | + {"with special char &", "user&123", 64, ""}, |
| 34 | + {"with special char *", "user*123", 64, ""}, |
| 35 | + {"with dot", "user.123", 64, ""}, |
| 36 | + {"with plus", "user+123", 64, ""}, |
| 37 | + {"with equals", "user=123", 64, ""}, |
| 38 | + {"with slash", "user/123", 64, ""}, |
| 39 | + {"with backslash", "user\\123", 64, ""}, |
| 40 | + {"with colon", "user:123", 64, ""}, |
| 41 | + {"with semicolon", "user;123", 64, ""}, |
| 42 | + {"with comma", "user,123", 64, ""}, |
| 43 | + {"with question", "user?123", 64, ""}, |
| 44 | + {"with brackets", "user[123]", 64, ""}, |
| 45 | + {"with parentheses", "user(123)", 64, ""}, |
| 46 | + {"with braces", "user{123}", 64, ""}, |
| 47 | + {"with angle brackets", "user<123>", 64, ""}, |
| 48 | + {"with pipe", "user|123", 64, ""}, |
| 49 | + {"with tilde", "user~123", 64, ""}, |
| 50 | + {"with backtick", "user`123", 64, ""}, |
| 51 | + {"with quote", "user'123", 64, ""}, |
| 52 | + {"with double quote", "user\"123", 64, ""}, |
| 53 | + {"exceeds max length", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZx", 62, ""}, |
| 54 | + {"control char tab", "user\t123", 64, ""}, |
| 55 | + {"control char newline", "user\n123", 64, ""}, |
| 56 | + {"unicode chinese", "用户123", 64, ""}, |
| 57 | + {"unicode emoji", "user😀123", 64, ""}, |
| 58 | + |
| 59 | + // 边界情况 |
| 60 | + {"leading spaces", " user123", 64, "user123"}, |
| 61 | + {"trailing spaces", "user123 ", 64, "user123"}, |
| 62 | + {"both sides spaces", " user123 ", 64, "user123"}, |
| 63 | + {"max length 1", "a", 1, "a"}, |
| 64 | + {"exceeds max length 1", "ab", 1, ""}, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + got := normalizeClientID(tt.input, tt.maxLen) |
| 70 | + if got != tt.expected { |
| 71 | + t.Errorf("normalizeClientID(%q, %d) = %q, want %q", tt.input, tt.maxLen, got, tt.expected) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestNormalizeRoomName(t *testing.T) { |
| 78 | + tests := []struct { |
| 79 | + name string |
| 80 | + input string |
| 81 | + maxLen int |
| 82 | + expected string |
| 83 | + }{ |
| 84 | + // 有效情况 |
| 85 | + {"simple room", "room1", 64, "room1"}, |
| 86 | + {"with space", "room 1", 64, "room 1"}, |
| 87 | + {"with hyphen", "room-1", 64, "room-1"}, |
| 88 | + {"with underscore", "room_1", 64, "room_1"}, |
| 89 | + {"with dot", "room.1", 64, "room.1"}, |
| 90 | + {"chinese characters", "房间一", 64, "房间一"}, |
| 91 | + {"mixed language", "团队 room.1", 64, "团队 room.1"}, |
| 92 | + {"single char", "a", 64, "a"}, |
| 93 | + {"exact max length", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 62, "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"}, |
| 94 | + {"special chars allowed", "room-1_v2.0", 64, "room-1_v2.0"}, |
| 95 | + |
| 96 | + // 无效情况 - 应返回空字符串 |
| 97 | + {"empty string", "", 64, ""}, |
| 98 | + {"only spaces", " ", 64, ""}, |
| 99 | + {"exceeds max length", "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZx", 62, ""}, |
| 100 | + {"control char tab", "room\t1", 64, ""}, |
| 101 | + {"control char newline", "room\n1", 64, ""}, |
| 102 | + {"control char carriage return", "room\r1", 64, ""}, |
| 103 | + {"control char null", "room\x001", 64, ""}, |
| 104 | + {"control char bell", "room\x071", 64, ""}, |
| 105 | + {"control char escape", "room\x1b1", 64, ""}, |
| 106 | + |
| 107 | + // 边界情况 |
| 108 | + {"leading spaces", " room1", 64, "room1"}, |
| 109 | + {"trailing spaces", "room1 ", 64, "room1"}, |
| 110 | + {"both sides spaces", " room1 ", 64, "room1"}, |
| 111 | + {"max length 1", "a", 1, "a"}, |
| 112 | + {"exceeds max length 1", "ab", 1, ""}, |
| 113 | + {"emoji allowed", "room😀1", 64, "room😀1"}, |
| 114 | + } |
| 115 | + |
| 116 | + for _, tt := range tests { |
| 117 | + t.Run(tt.name, func(t *testing.T) { |
| 118 | + got := normalizeRoomName(tt.input, tt.maxLen) |
| 119 | + if got != tt.expected { |
| 120 | + t.Errorf("normalizeRoomName(%q, %d) = %q, want %q", tt.input, tt.maxLen, got, tt.expected) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestNormalizeClientIDConsistency(t *testing.T) { |
| 127 | + // 测试多次调用结果一致 |
| 128 | + input := "user_123-ABC" |
| 129 | + for i := 0; i < 10; i++ { |
| 130 | + got := normalizeClientID(input, 64) |
| 131 | + if got != input { |
| 132 | + t.Errorf("normalizeClientID inconsistent on iteration %d: got %q, want %q", i, got, input) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestNormalizeRoomNameConsistency(t *testing.T) { |
| 138 | + // 测试多次调用结果一致 |
| 139 | + input := "团队 room.1" |
| 140 | + for i := 0; i < 10; i++ { |
| 141 | + got := normalizeRoomName(input, 64) |
| 142 | + if got != input { |
| 143 | + t.Errorf("normalizeRoomName inconsistent on iteration %d: got %q, want %q", i, got, input) |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func TestNormalizeClientIDWithMaxClientIDLength(t *testing.T) { |
| 149 | + // 测试使用实际常量 MaxClientIDLength |
| 150 | + tests := []struct { |
| 151 | + name string |
| 152 | + input string |
| 153 | + expected string |
| 154 | + }{ |
| 155 | + {"valid id", "user123", "user123"}, |
| 156 | + {"too long", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ""}, // 65 chars |
| 157 | + } |
| 158 | + |
| 159 | + for _, tt := range tests { |
| 160 | + t.Run(tt.name, func(t *testing.T) { |
| 161 | + got := normalizeClientID(tt.input, MaxClientIDLength) |
| 162 | + if got != tt.expected { |
| 163 | + t.Errorf("normalizeClientID(%q, MaxClientIDLength) = %q, want %q", tt.input, got, tt.expected) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestNormalizeRoomNameWithMaxRoomIDLength(t *testing.T) { |
| 170 | + // 测试使用实际常量 MaxRoomIDLength |
| 171 | + tests := []struct { |
| 172 | + name string |
| 173 | + input string |
| 174 | + expected string |
| 175 | + }{ |
| 176 | + {"valid room", "room1", "room1"}, |
| 177 | + {"too long", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ""}, // 65 chars |
| 178 | + } |
| 179 | + |
| 180 | + for _, tt := range tests { |
| 181 | + t.Run(tt.name, func(t *testing.T) { |
| 182 | + got := normalizeRoomName(tt.input, MaxRoomIDLength) |
| 183 | + if got != tt.expected { |
| 184 | + t.Errorf("normalizeRoomName(%q, MaxRoomIDLength) = %q, want %q", tt.input, got, tt.expected) |
| 185 | + } |
| 186 | + }) |
| 187 | + } |
| 188 | +} |
0 commit comments