-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.sh
More file actions
executable file
·536 lines (418 loc) · 19 KB
/
testing.sh
File metadata and controls
executable file
·536 lines (418 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/bin/bash
# rawdoc automated test suite
# Focuses on: performance timing, tier escalation, failure handling
# All content goes to files — console shows only metrics and status
set -o pipefail
# ─── Config ───────────────────────────────────────────────────────────────────
RAWDOC="./rawdoc" # path to binary — adjust if needed
OUT_DIR="/tmp/rawdoc-tests"
LOG="$OUT_DIR/test-results.log"
PASS=0
FAIL=0
SKIP=0
TOTAL=0
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
DIM='\033[0;90m'
BOLD='\033[1m'
NC='\033[0m'
# ─── Build ────────────────────────────────────────────────────────────────────
echo -e "${CYAN}Building rawdoc...${NC}"
cd "$(dirname "$0")" || exit 1
if ! go build -ldflags="-s -w" -o rawdoc . 2>&1; then
echo -e "${RED}Build failed${NC}"
exit 1
fi
echo -e "${GREEN}Built $(./rawdoc --version)${NC}"
RAWDOC="./rawdoc"
echo ""
# ─── Setup ────────────────────────────────────────────────────────────────────
rm -rf "$OUT_DIR"
mkdir -p "$OUT_DIR/tier1" "$OUT_DIR/tier2" \
"$OUT_DIR/formats" "$OUT_DIR/crawl" "$OUT_DIR/failures" \
"$OUT_DIR/edge" "$OUT_DIR/selectors" "$OUT_DIR/perf"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${BOLD} rawdoc test suite${NC}"
echo -e "${DIM} $(date '+%Y-%m-%d %H:%M:%S')${NC}"
echo -e "${DIM} output: $OUT_DIR${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# Log header
echo "rawdoc test results — $(date '+%Y-%m-%d %H:%M:%S')" > "$LOG"
echo "=================================================" >> "$LOG"
echo "" >> "$LOG"
# ─── Helpers ─────────────────────────────────────────────────────────────────
format_size() {
local bytes=$1
if [ "$bytes" -ge 1048576 ]; then
echo "$(echo "scale=1; $bytes/1048576" | bc)MB"
elif [ "$bytes" -ge 1024 ]; then
echo "$(echo "scale=1; $bytes/1024" | bc)KB"
else
echo "${bytes}B"
fi
}
print_table_header() {
printf "${BOLD}%-2s %-28s %6s %8s %8s %14s %5s %-25s${NC}\n" \
"" "NAME" "TIME" "RAW" "OUTPUT" "TOKENS" "SAVED" "PREVIEW"
printf "${DIM}%-2s %-28s %6s %8s %8s %14s %5s %-25s${NC}\n" \
"" "────" "────" "───" "──────" "──────" "─────" "───────"
}
# ─── Test Runner ──────────────────────────────────────────────────────────────
run_test() {
local name="$1"
local outfile="$2"
shift 2
local cmd=("$@")
TOTAL=$((TOTAL + 1))
# Time the execution
local start_ms=$(($(date +%s%N)/1000000))
local stderr_file="$OUT_DIR/.stderr_tmp"
"${cmd[@]}" > "$outfile" 2>"$stderr_file"
local exit_code=$?
local end_ms=$(($(date +%s%N)/1000000))
local duration_ms=$((end_ms - start_ms))
# Get output size
local size=0
if [ -f "$outfile" ]; then
size=$(wc -c < "$outfile" | tr -d ' ')
fi
# Format duration
local duration_str
if [ $duration_ms -ge 1000 ]; then
duration_str="$(echo "scale=1; $duration_ms/1000" | bc)s"
else
duration_str="${duration_ms}ms"
fi
# Determine status
local status_icon status_color
if [ $exit_code -eq 0 ] && [ $size -gt 100 ]; then
status_icon="✓"
status_color="$GREEN"
PASS=$((PASS + 1))
elif [ $exit_code -eq 0 ] && [ $size -le 100 ]; then
status_icon="⚠"
status_color="$YELLOW"
SKIP=$((SKIP + 1))
else
status_icon="✗"
status_color="$RED"
FAIL=$((FAIL + 1))
fi
# Parse [data] line from stderr for machine-readable stats
local raw_bytes=0 out_bytes=0 raw_tokens=0 out_tokens=0 saved_pct=0
if [ -f "$stderr_file" ] && [ -s "$stderr_file" ]; then
local data_line
data_line=$(grep '\[data\]' "$stderr_file" | head -1)
if [ -n "$data_line" ]; then
raw_bytes=$(echo "$data_line" | grep -oP 'raw_bytes=\K\d+')
out_bytes=$(echo "$data_line" | grep -oP 'out_bytes=\K\d+')
raw_tokens=$(echo "$data_line" | grep -oP 'raw_tokens=\K\d+')
out_tokens=$(echo "$data_line" | grep -oP 'out_tokens=\K\d+')
saved_pct=$(echo "$data_line" | grep -oP 'saved_pct=\K\d+')
fi
fi
local raw_str=$(format_size "$raw_bytes")
local out_str=$(format_size "$out_bytes")
local token_str="${raw_tokens}→${out_tokens}"
local saved_str="↓${saved_pct}%"
# Preview — first non-empty content line
local preview=""
if [ -f "$outfile" ] && [ $size -gt 0 ]; then
preview=$(grep -v '^\s*$' "$outfile" 2>/dev/null | head -1 | cut -c1-25)
fi
# Console output — tabular
printf "${status_color}%s${NC} %-28s %6s %8s %8s %14s %5s ${DIM}%-25s${NC}\n" \
"$status_icon" "$name" "$duration_str" "$raw_str" "$out_str" "$token_str" "$saved_str" "$preview"
# Log output
printf "%-2s %-28s %6s %8s %8s %14s %5s %s\n" \
"$status_icon" "$name" "$duration_str" "$raw_str" "$out_str" "$token_str" "$saved_str" "$preview" >> "$LOG"
# Save stderr alongside output
if [ -s "$stderr_file" ]; then
cp "$stderr_file" "${outfile%.md}.stderr"
fi
rm -f "$stderr_file" "$stderr_file.saved"
}
run_test_expect_fail() {
local name="$1"
local outfile="$2"
shift 2
local cmd=("$@")
TOTAL=$((TOTAL + 1))
local start_ms=$(($(date +%s%N)/1000000))
local stderr_file="$OUT_DIR/.stderr_tmp"
"${cmd[@]}" > "$outfile" 2>"$stderr_file"
local exit_code=$?
local end_ms=$(($(date +%s%N)/1000000))
local duration_ms=$((end_ms - start_ms))
local duration_str
if [ $duration_ms -ge 1000 ]; then
duration_str="$(echo "scale=1; $duration_ms/1000" | bc)s"
else
duration_str="${duration_ms}ms"
fi
# For expected failures, non-zero exit = pass
local status_icon status_color
if [ $exit_code -ne 0 ]; then
status_icon="✓"
status_color="$GREEN"
PASS=$((PASS + 1))
else
status_icon="✗"
status_color="$RED"
FAIL=$((FAIL + 1))
fi
local err_msg=""
if [ -f "$stderr_file" ] && [ -s "$stderr_file" ]; then
err_msg=$(head -1 "$stderr_file" | sed 's/\x1b\[[0-9;]*m//g' | cut -c1-60)
fi
printf "${status_color}%s${NC} %-45s %8s exit=%-3d ${DIM}(expected≠0)${NC}" \
"$status_icon" "$name" "$duration_str" "$exit_code"
if [ -n "$err_msg" ]; then
printf " ${DIM}%s${NC}" "$err_msg"
fi
echo ""
printf "%-2s %-45s %8s exit=%d (expected≠0) %s\n" \
"$status_icon" "$name" "$duration_str" "$exit_code" "$err_msg" >> "$LOG"
if [ -s "$stderr_file" ]; then
cp "$stderr_file" "${outfile%.md}.stderr"
fi
rm -f "$stderr_file"
}
run_crawl_test() {
local name="$1"
local outdir="$2"
shift 2
local cmd=("$@")
TOTAL=$((TOTAL + 1))
local start_ms=$(($(date +%s%N)/1000000))
local stderr_file="$OUT_DIR/.stderr_tmp"
"${cmd[@]}" 2>"$stderr_file"
local exit_code=$?
local end_ms=$(($(date +%s%N)/1000000))
local duration_ms=$((end_ms - start_ms))
local duration_str
if [ $duration_ms -ge 1000 ]; then
duration_str="$(echo "scale=1; $duration_ms/1000" | bc)s"
else
duration_str="${duration_ms}ms"
fi
# Count files
local file_count=0
local total_size=0
if [ -d "$outdir" ]; then
file_count=$(find "$outdir" -name "*.md" | wc -l | tr -d ' ')
total_size=$(du -sb "$outdir" 2>/dev/null | cut -f1)
fi
local size_str
if [ $total_size -ge 1048576 ]; then
size_str="$(echo "scale=1; $total_size/1048576" | bc)MB"
elif [ $total_size -ge 1024 ]; then
size_str="$(echo "scale=1; $total_size/1024" | bc)KB"
else
size_str="${total_size}B"
fi
local status_icon status_color
if [ $exit_code -eq 0 ] && [ $file_count -gt 0 ]; then
status_icon="✓"
status_color="$GREEN"
PASS=$((PASS + 1))
else
status_icon="✗"
status_color="$RED"
FAIL=$((FAIL + 1))
fi
printf "${status_color}%s${NC} %-45s %8s %4d files %8s exit=%d\n" \
"$status_icon" "$name" "$duration_str" "$file_count" "$size_str" "$exit_code"
printf "%-2s %-45s %8s %d files %8s exit=%d\n" \
"$status_icon" "$name" "$duration_str" "$file_count" "$size_str" "$exit_code" >> "$LOG"
if [ -s "$stderr_file" ]; then
cp "$stderr_file" "$outdir/.stderr"
fi
rm -f "$stderr_file"
}
# ─── Section Header ──────────────────────────────────────────────────────────
section() {
echo ""
echo -e "${CYAN}── $1 ──${NC}"
print_table_header
echo "" >> "$LOG"
echo "── $1 ──" >> "$LOG"
}
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# TESTS
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# ── Tier 1: Plain HTTP ────────────────────────────────────────────────────────
section "TIER 1 — Plain HTTP (no protection)"
run_test "k8s/pods" \
"$OUT_DIR/tier1/k8s-pods.md" \
$RAWDOC https://kubernetes.io/docs/concepts/workloads/pods/ -v
run_test "k8s/ingress" \
"$OUT_DIR/tier1/k8s-ingress.md" \
$RAWDOC https://kubernetes.io/docs/concepts/services-networking/ingress/ -v
run_test "go-pkg/net-http" \
"$OUT_DIR/tier1/go-net-http.md" \
$RAWDOC https://pkg.go.dev/net/http -v
run_test "go/effective-go" \
"$OUT_DIR/tier1/effective-go.md" \
$RAWDOC https://go.dev/doc/effective_go -v
run_test "spring.io/blog" \
"$OUT_DIR/tier1/spring-blog.md" \
$RAWDOC https://spring.io/blog -v
run_test "mdn/http-status" \
"$OUT_DIR/tier1/mdn-http-status.md" \
$RAWDOC https://developer.mozilla.org/en-US/docs/Web/HTTP/Status -v
run_test "postgresql/select" \
"$OUT_DIR/tier1/pg-select.md" \
$RAWDOC https://www.postgresql.org/docs/current/sql-select.html -v
run_test "istio/traffic-mgmt" \
"$OUT_DIR/tier1/istio-traffic.md" \
$RAWDOC https://istio.io/latest/docs/concepts/traffic-management/ -v
run_test "helm/chart-templates" \
"$OUT_DIR/tier1/helm-templates.md" \
$RAWDOC https://helm.sh/docs/chart_template_guide/ -v
run_test "python/asyncio" \
"$OUT_DIR/tier1/python-asyncio.md" \
$RAWDOC https://docs.python.org/3/library/asyncio.html -v
run_test "gobyexample/goroutines" \
"$OUT_DIR/tier1/gobyexample-goroutines.md" \
$RAWDOC https://gobyexample.com/goroutines -v
run_test "redis/set-command" \
"$OUT_DIR/tier1/redis-set.md" \
$RAWDOC https://redis.io/docs/latest/commands/set/ -v
# ── Cloudflare Sites ────────────────────────────────────────────────────────
section "Cloudflare-protected sites (Tier 1 with browser headers)"
run_test "baeldung/spring-kafka" \
"$OUT_DIR/tier2/baeldung-spring-kafka.md" \
$RAWDOC https://www.baeldung.com/spring-kafka -v
run_test "baeldung/jpa-query" \
"$OUT_DIR/tier2/baeldung-jpa-query.md" \
$RAWDOC https://www.baeldung.com/spring-data-jpa-query -v
run_test "baeldung/spring-boot-start" \
"$OUT_DIR/tier2/baeldung-boot-start.md" \
$RAWDOC https://www.baeldung.com/spring-boot-start -v
run_test "baeldung/spring-security" \
"$OUT_DIR/tier2/baeldung-security.md" \
$RAWDOC https://www.baeldung.com/spring-security-login -v
run_test "baeldung/java-streams" \
"$OUT_DIR/tier2/baeldung-streams.md" \
$RAWDOC https://www.baeldung.com/java-streams -v
run_test "digitalocean/nginx-tutorial" \
"$OUT_DIR/tier2/do-nginx.md" \
$RAWDOC https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-22-04 -v
# ── Output Formats ────────────────────────────────────────────────────────────
section "OUTPUT FORMATS — same URL, different formats"
TEST_URL="https://kubernetes.io/docs/concepts/workloads/pods/"
run_test "format/markdown" \
"$OUT_DIR/formats/markdown.md" \
$RAWDOC "$TEST_URL" -f markdown -v
run_test "format/text" \
"$OUT_DIR/formats/text.txt" \
$RAWDOC "$TEST_URL" -f text -v
run_test "format/json" \
"$OUT_DIR/formats/json.json" \
$RAWDOC "$TEST_URL" -f json -v
run_test "format/yaml" \
"$OUT_DIR/formats/yaml.yaml" \
$RAWDOC "$TEST_URL" -f yaml -v
run_test "format/code-only" \
"$OUT_DIR/formats/code-only.md" \
$RAWDOC "$TEST_URL" --code-only -v
run_test "format/no-links" \
"$OUT_DIR/formats/no-links.md" \
$RAWDOC "$TEST_URL" --no-links -v
# ── Crawl Mode ────────────────────────────────────────────────────────────────
section "CRAWL MODE — multi-page"
run_crawl_test "crawl/k8s-workloads (depth=1, max=10)" \
"$OUT_DIR/crawl/k8s" \
$RAWDOC https://kubernetes.io/docs/concepts/workloads/ -d 1 --max-pages 10 -o "$OUT_DIR/crawl/k8s" -v
run_crawl_test "crawl/gobyexample (depth=1, max=10)" \
"$OUT_DIR/crawl/gobyexample" \
$RAWDOC https://gobyexample.com/ -d 1 --max-pages 10 -o "$OUT_DIR/crawl/gobyexample" -v
run_crawl_test "crawl/helm (depth=1, max=8)" \
"$OUT_DIR/crawl/helm" \
$RAWDOC https://helm.sh/docs/ -d 1 --max-pages 8 -o "$OUT_DIR/crawl/helm" -v
run_crawl_test "crawl/baeldung-spring (depth=1, max=5)" \
"$OUT_DIR/crawl/baeldung" \
$RAWDOC https://www.baeldung.com/spring-boot -d 1 --max-pages 5 --include "/spring-*" -o "$OUT_DIR/crawl/baeldung" -v
# ── Failure Handling ──────────────────────────────────────────────────────────
section "FAILURE HANDLING — expected failures"
run_test_expect_fail "fail/invalid-url" \
"$OUT_DIR/failures/invalid-url.md" \
$RAWDOC not-a-valid-url
run_test_expect_fail "fail/nonexistent-domain" \
"$OUT_DIR/failures/nonexistent.md" \
$RAWDOC https://this-domain-does-not-exist-xyz123.com/page
run_test_expect_fail "fail/http-403" \
"$OUT_DIR/failures/http-403.md" \
$RAWDOC https://httpstat.us/403 -v
run_test_expect_fail "fail/http-500" \
"$OUT_DIR/failures/http-500.md" \
$RAWDOC https://httpstat.us/500 -v
run_test_expect_fail "fail/http-429" \
"$OUT_DIR/failures/http-429.md" \
$RAWDOC https://httpstat.us/429 -v
run_test_expect_fail "fail/empty-url" \
"$OUT_DIR/failures/empty-url.md" \
$RAWDOC ""
# ── Edge Cases ────────────────────────────────────────────────────────────────
section "EDGE CASES"
run_test "edge/example.com (minimal)" \
"$OUT_DIR/edge/example-com.md" \
$RAWDOC https://example.com -v
run_test "edge/redirect-golang.org" \
"$OUT_DIR/edge/redirect-golang.md" \
$RAWDOC https://golang.org/ -v
run_test "edge/redirect-reactjs.org" \
"$OUT_DIR/edge/redirect-reactjs.md" \
$RAWDOC https://reactjs.org/ -v
run_test "edge/hacker-news (table layout)" \
"$OUT_DIR/edge/hackernews.md" \
$RAWDOC https://news.ycombinator.com/ -v
run_test "edge/gobyexample-hello (minimal)" \
"$OUT_DIR/edge/gobyexample-hello.md" \
$RAWDOC https://gobyexample.com/hello-world -v
# ── Flag Combinations ────────────────────────────────────────────────────────
section "FLAG COMBINATIONS"
run_test "flags/code-only+baeldung" \
"$OUT_DIR/edge/baeldung-code-only.md" \
$RAWDOC https://www.baeldung.com/spring-kafka --code-only -v
run_test "flags/json+baeldung" \
"$OUT_DIR/edge/baeldung-json.json" \
$RAWDOC https://www.baeldung.com/spring-kafka -f json -v
run_test "flags/no-links+mdn" \
"$OUT_DIR/edge/mdn-no-links.md" \
$RAWDOC https://developer.mozilla.org/en-US/docs/Web/HTTP/Status --no-links -v
run_test "flags/timeout-short" \
"$OUT_DIR/edge/timeout-short.md" \
$RAWDOC https://kubernetes.io/docs/concepts/workloads/pods/ --timeout 2s -v
run_test "flags/custom-header" \
"$OUT_DIR/edge/custom-header.md" \
$RAWDOC https://kubernetes.io/docs/concepts/workloads/pods/ -header "X-Test=rawdoc" -v
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# SUMMARY
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${BOLD} RESULTS${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e " Total: ${BOLD}$TOTAL${NC}"
echo -e " Passed: ${GREEN}$PASS${NC}"
echo -e " Failed: ${RED}$FAIL${NC}"
echo -e " Warning: ${YELLOW}$SKIP${NC}"
echo ""
echo -e " Output: ${DIM}$OUT_DIR${NC}"
echo -e " Log: ${DIM}$LOG${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Summary to log
echo "" >> "$LOG"
echo "=================================================" >> "$LOG"
echo "TOTAL: $TOTAL PASS: $PASS FAIL: $FAIL WARN: $SKIP" >> "$LOG"
# Exit with failure if any test failed
if [ $FAIL -gt 0 ]; then
exit 1
fi
exit 0