| 1 |
package tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"regexp" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"unicode" |
| 11 |
) |
| 12 |
|
| 13 |
// mismatch represents a discrepancy between expected and actual output lines in a test. |
| 14 |
type mismatch struct { |
| 15 |
Line int `json:"line"` // Line number (1-based) |
| 16 |
Expected string `json:"expected"` // Expected line content and length |
| 17 |
Got string `json:"got"` // Actual line content and length |
| 18 |
} |
| 19 |
|
| 20 |
// MaskEmail masks email addresses in a slice of strings, replacing all but the first character of the local part with asterisks. |
| 21 |
func MaskEmail(cells []string) []string { |
| 22 |
for i, cell := range cells { |
| 23 |
if strings.Contains(cell, "@") { |
| 24 |
parts := strings.Split(cell, "@") |
| 25 |
if len(parts) == 2 { |
| 26 |
masked := parts[0][:1] + strings.Repeat("*", len(parts[0])-1) + "@" + parts[1] |
| 27 |
cells[i] = masked |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
return cells |
| 32 |
} |
| 33 |
|
| 34 |
// MaskPassword masks strings that resemble passwords (containing "pass" or 8+ characters) with asterisks. |
| 35 |
func MaskPassword(cells []string) []string { |
| 36 |
for i, cell := range cells { |
| 37 |
if len(cell) > 0 && (strings.Contains(strings.ToLower(cell), "pass") || len(cell) >= 8) { |
| 38 |
cells[i] = strings.Repeat("*", len(cell)) |
| 39 |
} |
| 40 |
} |
| 41 |
return cells |
| 42 |
} |
| 43 |
|
| 44 |
// MaskCard masks credit card-like numbers, keeping only the last four digits visible. |
| 45 |
func MaskCard(cells []string) []string { |
| 46 |
for i, cell := range cells { |
| 47 |
// Check for card-like numbers (12+ digits, with or without dashes/spaces) |
| 48 |
if len(cell) >= 12 && (strings.Contains(cell, "-") || len(strings.ReplaceAll(cell, " ", "")) >= 12) { |
| 49 |
parts := strings.FieldsFunc(cell, func(r rune) bool { return r == '-' || r == ' ' }) |
| 50 |
masked := "" |
| 51 |
for j, part := range parts { |
| 52 |
if j < len(parts)-1 { |
| 53 |
masked += strings.Repeat("*", len(part)) |
| 54 |
} else { |
| 55 |
masked += part // Keep last 4 digits visible |
| 56 |
} |
| 57 |
if j < len(parts)-1 { |
| 58 |
masked += "-" |
| 59 |
} |
| 60 |
} |
| 61 |
cells[i] = masked |
| 62 |
} |
| 63 |
} |
| 64 |
return cells |
| 65 |
} |
| 66 |
|
| 67 |
// visualCheck compares rendered output against expected lines, reporting mismatches in a test. |
| 68 |
// It normalizes line endings, strips ANSI colors, and trims empty lines before comparison. |
| 69 |
func visualCheck(t *testing.T, name, output, expected string) bool { |
| 70 |
t.Helper() |
| 71 |
|
| 72 |
// Normalize line endings and split into lines |
| 73 |
normalize := func(s string) []string { |
| 74 |
s = strings.ReplaceAll(s, "\r\n", "\n") |
| 75 |
s = StripColors(s) |
| 76 |
return strings.Split(s, "\n") |
| 77 |
} |
| 78 |
|
| 79 |
expectedLines := normalize(expected) |
| 80 |
outputLines := normalize(output) |
| 81 |
|
| 82 |
// Trim empty lines from start and end |
| 83 |
trimEmpty := func(lines []string) []string { |
| 84 |
start, end := 0, len(lines) |
| 85 |
for start < end && strings.TrimSpace(lines[start]) == "" { |
| 86 |
start++ |
| 87 |
} |
| 88 |
for end > start && strings.TrimSpace(lines[end-1]) == "" { |
| 89 |
end-- |
| 90 |
} |
| 91 |
return lines[start:end] |
| 92 |
} |
| 93 |
|
| 94 |
expectedLines = trimEmpty(expectedLines) |
| 95 |
outputLines = trimEmpty(outputLines) |
| 96 |
|
| 97 |
// Check line counts |
| 98 |
if len(outputLines) != len(expectedLines) { |
| 99 |
ex := strings.Join(expectedLines, "\n") |
| 100 |
ot := strings.Join(outputLines, "\n") |
| 101 |
t.Errorf("%s: line count mismatch - expected %d, got %d", name, len(expectedLines), len(outputLines)) |
| 102 |
t.Errorf("Expected:\n%s\n", ex) |
| 103 |
t.Errorf("Got:\n%s\n", ot) |
| 104 |
return false |
| 105 |
} |
| 106 |
|
| 107 |
var mismatches []mismatch |
| 108 |
for i := 0; i < len(expectedLines) && i < len(outputLines); i++ { |
| 109 |
exp := strings.TrimSpace(expectedLines[i]) |
| 110 |
got := strings.TrimSpace(outputLines[i]) |
| 111 |
if exp != got { |
| 112 |
mismatches = append(mismatches, mismatch{ |
| 113 |
Line: i + 1, |
| 114 |
Expected: fmt.Sprintf("%s (%d)", exp, len(exp)), |
| 115 |
Got: fmt.Sprintf("%s (%d)", got, len(got)), |
| 116 |
}) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// Report mismatches |
| 121 |
if len(mismatches) > 0 { |
| 122 |
diff, _ := json.MarshalIndent(mismatches, "", " ") |
| 123 |
t.Errorf("%s: %d mismatches found:\n%s", name, len(mismatches), diff) |
| 124 |
t.Errorf("Full expected output:\n%s", expected) |
| 125 |
t.Errorf("Full actual output:\n%s", output) |
| 126 |
return false |
| 127 |
} |
| 128 |
|
| 129 |
return true |
| 130 |
} |
| 131 |
|
| 132 |
// visualCheckHTML compares rendered HTML output against expected lines, |
| 133 |
// trimming whitespace per line and ignoring blank lines. |
| 134 |
func visualCheckHTML(t *testing.T, name, output, expected string) bool { |
| 135 |
t.Helper() |
| 136 |
|
| 137 |
normalizeHTML := func(s string) []string { |
| 138 |
s = strings.ReplaceAll(s, "\r\n", "\n") // Normalize line endings |
| 139 |
lines := strings.Split(s, "\n") |
| 140 |
trimmedLines := make([]string, 0, len(lines)) |
| 141 |
for _, line := range lines { |
| 142 |
trimmed := strings.TrimSpace(line) |
| 143 |
if trimmed != "" { // Only keep non-blank lines |
| 144 |
trimmedLines = append(trimmedLines, trimmed) |
| 145 |
} |
| 146 |
} |
| 147 |
return trimmedLines |
| 148 |
} |
| 149 |
|
| 150 |
expectedLines := normalizeHTML(expected) |
| 151 |
outputLines := normalizeHTML(output) |
| 152 |
|
| 153 |
// Compare line counts |
| 154 |
if len(outputLines) != len(expectedLines) { |
| 155 |
t.Errorf("%s: line count mismatch - expected %d, got %d", name, len(expectedLines), len(outputLines)) |
| 156 |
t.Errorf("Expected (trimmed):\n%s", strings.Join(expectedLines, "\n")) |
| 157 |
t.Errorf("Got (trimmed):\n%s", strings.Join(outputLines, "\n")) |
| 158 |
// Optionally print full untrimmed for debugging exact whitespace |
| 159 |
// t.Errorf("Full Expected:\n%s", expected) |
| 160 |
// t.Errorf("Full Got:\n%s", output) |
| 161 |
return false |
| 162 |
} |
| 163 |
|
| 164 |
// Compare each line |
| 165 |
mismatches := []mismatch{} // Use mismatch struct from fn.go |
| 166 |
for i := 0; i < len(expectedLines); i++ { |
| 167 |
if expectedLines[i] != outputLines[i] { |
| 168 |
mismatches = append(mismatches, mismatch{ |
| 169 |
Line: i + 1, |
| 170 |
Expected: expectedLines[i], |
| 171 |
Got: outputLines[i], |
| 172 |
}) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if len(mismatches) > 0 { |
| 177 |
t.Errorf("%s: %d mismatches found:", name, len(mismatches)) |
| 178 |
for _, mm := range mismatches { |
| 179 |
t.Errorf(" Line %d:\n Expected: %s\n Got: %s", mm.Line, mm.Expected, mm.Got) |
| 180 |
} |
| 181 |
// Optionally print full outputs again on mismatch |
| 182 |
// t.Errorf("Full Expected:\n%s", expected) |
| 183 |
// t.Errorf("Full Got:\n%s", output) |
| 184 |
return false |
| 185 |
} |
| 186 |
|
| 187 |
return true |
| 188 |
} |
| 189 |
|
| 190 |
// ansiColorRegex matches ANSI color escape sequences. |
| 191 |
var ansiColorRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`) |
| 192 |
|
| 193 |
// StripColors removes ANSI color codes from a string. |
| 194 |
func StripColors(s string) string { |
| 195 |
return ansiColorRegex.ReplaceAllString(s, "") |
| 196 |
} |
| 197 |
|
| 198 |
// Regex to remove leading/trailing whitespace from lines AND blank lines for HTML comparison |
| 199 |
var ( |
| 200 |
htmlWhitespaceRegex = regexp.MustCompile(`(?m)^\s+|\s+$`) |
| 201 |
blankLineRegex = regexp.MustCompile(`(?m)^\s*\n`) |
| 202 |
) |
| 203 |
|
| 204 |
func normalizeHTMLStrict(s string) string { |
| 205 |
s = strings.ReplaceAll(s, "\r\n", "\n") |
| 206 |
s = strings.ReplaceAll(s, "\n", "") // Remove all newlines |
| 207 |
s = strings.ReplaceAll(s, "\t", "") // Remove tabs |
| 208 |
|
| 209 |
// Remove spaces after > and before <, effectively compacting tags |
| 210 |
s = regexp.MustCompile(`>\s+`).ReplaceAllString(s, ">") |
| 211 |
s = regexp.MustCompile(`\s+<`).ReplaceAllString(s, "<") |
| 212 |
|
| 213 |
// Trim overall leading/trailing space that might be left. |
| 214 |
return strings.TrimSpace(s) |
| 215 |
} |
| 216 |
|
| 217 |
// visualCheckCaption (helper function, potentially shared or adapted from your existing visualCheck) |
| 218 |
// Ensure this helper normalizes expected and got strings for reliable comparison |
| 219 |
|
| 220 |
func visualCheckCaption(t *testing.T, testName, got, expected string) bool { |
| 221 |
t.Helper() |
| 222 |
normalize := func(s string) string { |
| 223 |
s = strings.ReplaceAll(s, "\r\n", "\n") // Normalize newlines |
| 224 |
lines := strings.Split(s, "\n") |
| 225 |
var trimmedLines []string |
| 226 |
for _, l := range lines { |
| 227 |
trimmedLines = append(trimmedLines, strings.TrimSpace(l)) |
| 228 |
} |
| 229 |
// Join, then trim overall to handle cases where expected might have leading/trailing blank lines |
| 230 |
// but individual lines should keep their relative structure. |
| 231 |
return strings.TrimSpace(strings.Join(trimmedLines, "\n")) |
| 232 |
} |
| 233 |
|
| 234 |
gotNormalized := normalize(got) |
| 235 |
expectedNormalized := normalize(expected) |
| 236 |
|
| 237 |
if gotNormalized != expectedNormalized { |
| 238 |
// Use a more detailed diff output if available, or just print both. |
| 239 |
t.Errorf("%s: outputs do not match.\nExpected:\n```\n%s\n```\nGot:\n```\n%s\n```\n---Diff---\n%s", |
| 240 |
testName, expected, got, getDiff(expectedNormalized, gotNormalized)) // You might need a diff utility |
| 241 |
return false |
| 242 |
} |
| 243 |
return true |
| 244 |
} |
| 245 |
|
| 246 |
// A simple diff helper (replace with a proper library if needed) |
| 247 |
func getDiff(expected, actual string) string { |
| 248 |
expectedLines := strings.Split(expected, "\n") |
| 249 |
actualLines := strings.Split(actual, "\n") |
| 250 |
maxLen := max(len(actualLines), len(expectedLines)) |
| 251 |
var diff strings.Builder |
| 252 |
diff.WriteString("Line | Expected | Actual\n") |
| 253 |
diff.WriteString("-----|----------------------------------|----------------------------------\n") |
| 254 |
for i := 0; i < maxLen; i++ { |
| 255 |
eLine := "" |
| 256 |
if i < len(expectedLines) { |
| 257 |
eLine = expectedLines[i] |
| 258 |
} |
| 259 |
aLine := "" |
| 260 |
if i < len(actualLines) { |
| 261 |
aLine = actualLines[i] |
| 262 |
} |
| 263 |
marker := " " |
| 264 |
if eLine != aLine { |
| 265 |
marker = "!" |
| 266 |
} |
| 267 |
diff.WriteString(fmt.Sprintf("%4d %s| %-32s | %-32s\n", i+1, marker, eLine, aLine)) |
| 268 |
} |
| 269 |
return diff.String() |
| 270 |
} |
| 271 |
|
| 272 |
func getLastContentLine(buf *bytes.Buffer) string { |
| 273 |
content := buf.String() |
| 274 |
lines := strings.Split(content, "\n") |
| 275 |
|
| 276 |
// Search backwards for first non-border, non-empty line |
| 277 |
for i := len(lines) - 1; i >= 0; i-- { |
| 278 |
line := strings.TrimSpace(lines[i]) |
| 279 |
if line == "" || strings.Contains(line, "─") || |
| 280 |
strings.Contains(line, "┌") || strings.Contains(line, "└") { |
| 281 |
continue |
| 282 |
} |
| 283 |
return line |
| 284 |
} |
| 285 |
return "" |
| 286 |
} |
| 287 |
|
| 288 |
type Name struct { |
| 289 |
First string |
| 290 |
Last string |
| 291 |
} |
| 292 |
|
| 293 |
// this will be ignored since Format() is present |
| 294 |
func (n Name) String() string { |
| 295 |
return fmt.Sprintf("%s %s", n.First, n.Last) |
| 296 |
} |
| 297 |
|
| 298 |
// Note: Format() overrides String() if both exist. |
| 299 |
func (n Name) Format() string { |
| 300 |
return fmt.Sprintf("%s %s", clean(n.First), clean(n.Last)) |
| 301 |
} |
| 302 |
|
| 303 |
// clean ensures the first letter is capitalized and the rest are lowercase |
| 304 |
func clean(s string) string { |
| 305 |
s = strings.TrimSpace(strings.ToLower(s)) |
| 306 |
words := strings.Fields(s) |
| 307 |
s = strings.Join(words, "") |
| 308 |
|
| 309 |
if s == "" { |
| 310 |
return s |
| 311 |
} |
| 312 |
// Capitalize the first letter |
| 313 |
runes := []rune(s) |
| 314 |
runes[0] = unicode.ToUpper(runes[0]) |
| 315 |
return string(runes) |
| 316 |
} |