Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: pdev test command + deduplicate test logic

Datacf7dbcb62aee
diff --git a/pdev b/pdev
index 765948ad..bcadff47 100755
--- a/pdev
+++ b/pdev
@@ -20,6 +20,9 @@ COMMANDS=(
"check:πŸ§ͺ:All checks (Go + Dashboard)"
" check go:πŸ§ͺ:Go only"
" check dashboard:πŸ§ͺ:Dashboard only"
+ "test:πŸ”¬:All tests (unit + integration)"
+ " test unit:πŸ”¬:Unit tests only"
+ " test integration:πŸ”¬:Integration tests only"
"build:πŸš€:Build & run server + dashboard"
"doctor:🩺:Setup dev environment"
"security:πŸ”’:Gosec security scan"
@@ -76,6 +79,21 @@ run_command() {
echo ""
exec bash scripts/check-dashboard.sh
;;
+ "test")
+ echo " ${ACCENT}${BOLD}πŸ”¬ All tests${NC}"
+ echo ""
+ exec bash scripts/test.sh all
+ ;;
+ "test unit")
+ echo " ${ACCENT}${BOLD}πŸ”¬ Unit tests${NC}"
+ echo ""
+ exec bash scripts/test.sh unit
+ ;;
+ "test integration")
+ echo " ${ACCENT}${BOLD}πŸ”¬ Integration tests${NC}"
+ echo ""
+ exec bash scripts/test.sh integration
+ ;;
"build") exec bash scripts/dev.sh ;;
"doctor") exec bash scripts/doctor.sh ;;
"security") exec bash scripts/check-gosec.sh ;;
@@ -153,6 +171,13 @@ if [ $# -gt 0 ]; then
run_command "check"
fi
;;
+ test)
+ if [ $# -gt 1 ]; then
+ run_command "test $2"
+ else
+ run_command "test"
+ fi
+ ;;
*) run_command "$1" ;;
esac
fi
diff --git a/scripts/check.sh b/scripts/check.sh
index 7788a49f..d51f2c4f 100755
--- a/scripts/check.sh
+++ b/scripts/check.sh
@@ -2,7 +2,9 @@
set -e
# check.sh β€” Local pre-push checks matching GitHub Actions CI
-# Uses gotestsum for structured test output with summaries
+# Runs: format β†’ vet β†’ build β†’ tests (via test.sh) β†’ lint
+
+cd "$(dirname "$0")/.."
BOLD=$'\033[1m'
ACCENT=$'\033[38;2;251;191;36m'
@@ -12,151 +14,24 @@ ERROR=$'\033[38;2;230;57;70m'
MUTED=$'\033[38;2;90;100;128m'
NC=$'\033[0m'
-CORE_REGEX='^Test(Health|Orchestrator_|Navigate_|Tabs_|Config_|Metrics_|Cookies_|Error_|Eval_|Upload_|Screenshot_)'
-TMPDIR_CHECK=$(mktemp -d)
-trap 'rm -rf "$TMPDIR_CHECK" pinchtab coverage.out 2>/dev/null' EXIT
-
-ok() { echo -e " ${SUCCESS}βœ“${NC} $1"; }
-fail() { echo -e " ${ERROR}βœ—${NC} $1"; [ -n "${2:-}" ] && echo -e " ${MUTED}$2${NC}"; }
-hint() { echo -e " ${MUTED}$1${NC}"; }
+ok() { echo -e " ${SUCCESS}βœ“${NC} $1"; }
+fail() { echo -e " ${ERROR}βœ—${NC} $1"; [ -n "${2:-}" ] && echo -e " ${MUTED}$2${NC}"; }
+hint() { echo -e " ${MUTED}$1${NC}"; }
section() {
echo ""
echo -e "${ACCENT}${BOLD}$1${NC}"
}
-# Parse gotestsum JSON events and print summary
-test_summary() {
- local json_file="$1"
- local label="$2"
-
- if [ ! -s "$json_file" ]; then
- echo -e " ${MUTED}No test events recorded${NC}"
- return
- fi
-
- local total=0 pass=0 fail=0 skip=0
- read total pass fail skip <<<"$(jq -r \
- 'select(.Test != null and (.Action == "pass" or .Action == "fail" or .Action == "skip"))
- | [.Package, .Test, .Action] | @tsv' "$json_file" \
- | awk -F'\t' 'NF == 3 { key = $1 "\t" $2; status[key] = $3 }
- END {
- for (k in status) {
- t++
- if (status[k] == "pass") p++
- else if (status[k] == "fail") f++
- else if (status[k] == "skip") s++
- }
- printf "%d %d %d %d\n", t+0, p+0, f+0, s+0
- }')"
-
- echo ""
- echo -e " ${BOLD}$label${NC}"
- echo -e " ${MUTED}────────────────────────────${NC}"
- echo -e " Total: ${BOLD}$total${NC}"
- [ "$pass" -gt 0 ] && echo -e " Passed: ${SUCCESS}$pass${NC}"
- [ "$fail" -gt 0 ] && echo -e " Failed: ${ERROR}$fail${NC}"
- [ "$skip" -gt 0 ] && echo -e " Skipped: ${ACCENT}$skip${NC}"
-
- # Show failed test names
- if [ "$fail" -gt 0 ]; then
- echo ""
- echo -e " ${ERROR}Failed tests:${NC}"
- jq -r 'select(.Test != null and .Action == "fail") | " βœ— \(.Test)"' "$json_file" | sort -u
- fi
-}
-
-# ── Start ────────────────────────────────────────────────────────────
+trap 'rm -f pinchtab coverage.out 2>/dev/null' EXIT
echo ""
echo -e " ${ACCENT}${BOLD}πŸ¦€ Pinchtab Check${NC}"
echo -e " ${INFO}Running pre-push checks (matches GitHub Actions CI)...${NC}"
-# ── Check gotestsum ──────────────────────────────────────────────────
-
-HAS_GOTESTSUM=false
-if command -v gotestsum &>/dev/null; then
- HAS_GOTESTSUM=true
-fi
-
-# Run unit tests with dots progress
-# Usage: run_unit_tests <json_file> <go test args...>
-run_unit_tests() {
- local json_file="$1"; shift
- local exit_code=0
-
- if $HAS_GOTESTSUM; then
- gotestsum --format dots --jsonfile "$json_file" -- "$@" 2>&1 || exit_code=$?
- else
- go test "$@" 2>&1 | grep -E '^(ok|FAIL|---) ' || exit_code=${PIPESTATUS[0]}
- fi
-
- echo ""
- return $exit_code
-}
-
-# Run integration tests with live progress counter
-# Pipes go test -json directly for real-time output
-# Usage: run_integration_tests <json_file> <go test args...>
-run_integration_tests() {
- local json_file="$1"; shift
- local exit_code=0
- local count=0
-
- # Stream JSON events from go test, tee to file, show live progress
- go test -json "$@" 2>&1 | tee "$json_file" | while IFS= read -r line; do
- local action test_name
- action=$(echo "$line" | jq -r '.Action // empty' 2>/dev/null) || continue
- test_name=$(echo "$line" | jq -r '.Test // empty' 2>/dev/null) || continue
-
- [ -z "$test_name" ] && continue
-
- # Track elapsed time from Elapsed field
- local elapsed
- elapsed=$(echo "$line" | jq -r '.Elapsed // empty' 2>/dev/null)
-
- # Truncate long test names
- local display_name="$test_name"
- local max_len=40
- if [ ${#display_name} -gt $max_len ]; then
- display_name="${display_name:0:$((max_len - 1))}…"
- fi
-
- case "$action" in
- run)
- printf "\r ${MUTED}β–Έ %-${max_len}s${NC} \r" "$display_name"
- ;;
- pass)
- count=$((count + 1))
- if [ -n "$elapsed" ]; then
- printf "\r ${SUCCESS}βœ“${NC} ${MUTED}[%2d]${NC} %-${max_len}s ${MUTED}%6ss${NC}\n" "$count" "$display_name" "$elapsed"
- else
- printf "\r ${SUCCESS}βœ“${NC} ${MUTED}[%2d]${NC} %-${max_len}s\n" "$count" "$display_name"
- fi
- ;;
- fail)
- count=$((count + 1))
- if [ -n "$elapsed" ]; then
- printf "\r ${ERROR}βœ—${NC} ${MUTED}[%2d]${NC} %-${max_len}s ${MUTED}%6ss${NC}\n" "$count" "$display_name" "$elapsed"
- else
- printf "\r ${ERROR}βœ—${NC} ${MUTED}[%2d]${NC} %-${max_len}s\n" "$count" "$display_name"
- fi
- ;;
- skip)
- count=$((count + 1))
- printf "\r ${ACCENT}Β·${NC} ${MUTED}[%2d]${NC} %-${max_len}s ${MUTED} skip${NC}\n" "$count" "$display_name"
- ;;
- esac
- done
- exit_code=${PIPESTATUS[0]}
- printf "\r%*s\r" 60 "" # clear last line
-
- return $exit_code
-}
-
# ── Format ───────────────────────────────────────────────────────────
-section "Format Check"
+section "Format"
unformatted=$(gofmt -l .)
if [ -n "$unformatted" ]; then
@@ -169,7 +44,7 @@ ok "gofmt"
# ── Vet ──────────────────────────────────────────────────────────────
-section "Go Vet"
+section "Vet"
if ! go vet ./... 2>&1; then
fail "go vet"
@@ -187,57 +62,9 @@ if ! go build -o pinchtab ./cmd/pinchtab 2>&1; then
fi
ok "go build"
-# ── Unit Tests ───────────────────────────────────────────────────────
-
-section "Unit Tests"
-
-UNIT_JSON="$TMPDIR_CHECK/unit-events.json"
-
-if ! run_unit_tests "$UNIT_JSON" \
- -count=1 -coverprofile=coverage.out -covermode=atomic ./...; then
- fail "Unit tests"
- test_summary "$UNIT_JSON" "Unit Test Results"
- exit 1
-fi
-ok "Unit tests"
-test_summary "$UNIT_JSON" "Unit Test Results"
-
-echo ""
-echo -e " ${BOLD}Coverage${NC}"
-echo -e " ${MUTED}────────────────────────────${NC}"
-go tool cover -func=coverage.out | tail -1 | awk '{printf " %s\n", $0}'
+# ── Tests (delegates to test.sh) ─────────────────────────────────────
-# ── Integration Tests (Core) ────────────────────────────────────────
-
-section "Integration Tests (Core)"
-
-CORE_JSON="$TMPDIR_CHECK/core-events.json"
-
-if ! run_integration_tests "$CORE_JSON" \
- -tags integration -timeout 10m -count=1 \
- -run "$CORE_REGEX" ./tests/integration/; then
- fail "Integration core"
- test_summary "$CORE_JSON" "Core Test Results"
- exit 1
-fi
-ok "Integration core"
-test_summary "$CORE_JSON" "Core Test Results"
-
-# ── Integration Tests (Rest) ────────────────────────────────────────
-
-section "Integration Tests (Rest)"
-
-REST_JSON="$TMPDIR_CHECK/rest-events.json"
-
-if ! run_integration_tests "$REST_JSON" \
- -tags integration -timeout 12m -count=1 \
- -run '^Test' -skip "$CORE_REGEX" ./tests/integration/; then
- fail "Integration rest"
- test_summary "$REST_JSON" "Rest Test Results"
- exit 1
-fi
-ok "Integration rest"
-test_summary "$REST_JSON" "Rest Test Results"
+bash scripts/test.sh all
# ── Lint ─────────────────────────────────────────────────────────────
diff --git a/scripts/test.sh b/scripts/test.sh
new file mode 100755
index 00000000..151a2ba1
--- /dev/null
+++ b/scripts/test.sh
@@ -0,0 +1,176 @@
+#!/bin/bash
+set -e
+
+# test.sh β€” Run Go tests with optional scope
+# Usage: test.sh [unit|integration|all]
+# Default: all
+
+cd "$(dirname "$0")/.."
+
+BOLD=$'\033[1m'
+ACCENT=$'\033[38;2;251;191;36m'
+SUCCESS=$'\033[38;2;0;229;204m'
+ERROR=$'\033[38;2;230;57;70m'
+MUTED=$'\033[38;2;90;100;128m'
+NC=$'\033[0m'
+
+CORE_REGEX='^Test(Health|Orchestrator_|Navigate_|Tabs_|Config_|Metrics_|Cookies_|Error_|Eval_|Upload_|Screenshot_)'
+
+ok() { echo -e " ${SUCCESS}βœ“${NC} $1"; }
+fail() { echo -e " ${ERROR}βœ—${NC} $1"; }
+
+section() {
+ echo ""
+ echo -e "${ACCENT}${BOLD}$1${NC}"
+}
+
+# Parse gotestsum JSON and print summary
+test_summary() {
+ local json_file="$1"
+ local label="$2"
+
+ [ ! -s "$json_file" ] && return
+
+ local total=0 pass=0 fail=0 skip=0
+ read total pass fail skip <<<"$(jq -r \
+ 'select(.Test != null and (.Action == "pass" or .Action == "fail" or .Action == "skip"))
+ | [.Package, .Test, .Action] | @tsv' "$json_file" \
+ | awk -F'\t' 'NF == 3 { key = $1 "\t" $2; status[key] = $3 }
+ END {
+ for (k in status) {
+ t++
+ if (status[k] == "pass") p++
+ else if (status[k] == "fail") f++
+ else if (status[k] == "skip") s++
+ }
+ printf "%d %d %d %d\n", t+0, p+0, f+0, s+0
+ }')"
+
+ echo ""
+ echo -e " ${BOLD}$label${NC}"
+ echo -e " ${MUTED}────────────────────────────${NC}"
+ echo -e " Total: ${BOLD}$total${NC}"
+ [ "$pass" -gt 0 ] && echo -e " Passed: ${SUCCESS}$pass${NC}"
+ [ "$fail" -gt 0 ] && echo -e " Failed: ${ERROR}$fail${NC}"
+ [ "$skip" -gt 0 ] && echo -e " Skipped: ${ACCENT}$skip${NC}"
+
+ if [ "$fail" -gt 0 ]; then
+ echo ""
+ echo -e " ${ERROR}Failed tests:${NC}"
+ jq -r 'select(.Test != null and .Action == "fail") | " βœ— \(.Test)"' "$json_file" | sort -u
+ fi
+}
+
+# Live progress for integration tests
+run_integration() {
+ local json_file="$1"; shift
+ local count=0
+ local max_len=40
+
+ go test -json "$@" 2>&1 | tee "$json_file" | while IFS= read -r line; do
+ local action test_name elapsed
+ action=$(echo "$line" | jq -r '.Action // empty' 2>/dev/null) || continue
+ test_name=$(echo "$line" | jq -r '.Test // empty' 2>/dev/null) || continue
+ elapsed=$(echo "$line" | jq -r '.Elapsed // empty' 2>/dev/null)
+
+ [ -z "$test_name" ] && continue
+
+ local display="$test_name"
+ if [ ${#display} -gt $max_len ]; then
+ display="${display:0:$((max_len - 1))}…"
+ fi
+
+ case "$action" in
+ run) printf "\r ${MUTED}β–Έ %-${max_len}s${NC} \r" "$display" ;;
+ pass) count=$((count + 1))
+ if [ -n "$elapsed" ]; then
+ printf "\r ${SUCCESS}βœ“${NC} ${MUTED}[%2d]${NC} %-${max_len}s ${MUTED}%6ss${NC}\n" "$count" "$display" "$elapsed"
+ else
+ printf "\r ${SUCCESS}βœ“${NC} ${MUTED}[%2d]${NC} %-${max_len}s\n" "$count" "$display"
+ fi ;;
+ fail) count=$((count + 1))
+ if [ -n "$elapsed" ]; then
+ printf "\r ${ERROR}βœ—${NC} ${MUTED}[%2d]${NC} %-${max_len}s ${MUTED}%6ss${NC}\n" "$count" "$display" "$elapsed"
+ else
+ printf "\r ${ERROR}βœ—${NC} ${MUTED}[%2d]${NC} %-${max_len}s\n" "$count" "$display"
+ fi ;;
+ skip) count=$((count + 1))
+ printf "\r ${ACCENT}Β·${NC} ${MUTED}[%2d]${NC} %-${max_len}s ${MUTED} skip${NC}\n" "$count" "$display" ;;
+ esac
+ done
+ return ${PIPESTATUS[0]}
+}
+
+SCOPE="${1:-all}"
+TMPDIR_TEST=$(mktemp -d)
+trap 'rm -rf "$TMPDIR_TEST"' EXIT
+
+HAS_GOTESTSUM=false
+command -v gotestsum &>/dev/null && HAS_GOTESTSUM=true
+
+# ── Unit tests ───────────────────────────────────────────────────────
+
+if [ "$SCOPE" = "all" ] || [ "$SCOPE" = "unit" ]; then
+ section "Unit Tests"
+
+ UNIT_JSON="$TMPDIR_TEST/unit.json"
+
+ if $HAS_GOTESTSUM; then
+ if ! gotestsum --format dots --jsonfile "$UNIT_JSON" -- -count=1 ./...; then
+ fail "Unit tests"
+ test_summary "$UNIT_JSON" "Unit Test Results"
+ exit 1
+ fi
+ else
+ if ! go test -json -count=1 ./... > "$UNIT_JSON" 2>&1; then
+ fail "Unit tests"
+ test_summary "$UNIT_JSON" "Unit Test Results"
+ exit 1
+ fi
+ fi
+ ok "Unit tests"
+ test_summary "$UNIT_JSON" "Unit Test Results"
+fi
+
+# ── Integration tests (Core) ────────────────────────────────────────
+
+if [ "$SCOPE" = "all" ] || [ "$SCOPE" = "integration" ]; then
+ section "Integration Tests (Core)"
+
+ CORE_JSON="$TMPDIR_TEST/core.json"
+
+ if ! run_integration "$CORE_JSON" \
+ -tags integration -timeout 10m -count=1 \
+ -run "$CORE_REGEX" ./tests/integration/; then
+ fail "Integration core"
+ test_summary "$CORE_JSON" "Core Test Results"
+ exit 1
+ fi
+ printf "\r%*s\r" 60 ""
+ ok "Integration core"
+ test_summary "$CORE_JSON" "Core Test Results"
+
+ # ── Integration tests (Rest) ────────────────────────────────────
+
+ section "Integration Tests (Rest)"
+
+ REST_JSON="$TMPDIR_TEST/rest.json"
+
+ if ! run_integration "$REST_JSON" \
+ -tags integration -timeout 12m -count=1 \
+ -run '^Test' -skip "$CORE_REGEX" ./tests/integration/; then
+ fail "Integration rest"
+ test_summary "$REST_JSON" "Rest Test Results"
+ exit 1
+ fi
+ printf "\r%*s\r" 60 ""
+ ok "Integration rest"
+ test_summary "$REST_JSON" "Rest Test Results"
+fi
+
+# ── Summary ──────────────────────────────────────────────────────────
+
+section "Summary"
+echo ""
+echo -e " ${SUCCESS}${BOLD}All tests passed!${NC}"
+echo ""