File size: 9,062 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
set -e

# test.sh β€” Run Go tests with optional scope
# Usage: test.sh [unit|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'

SYSTEM_REGEX='^(TestProxy_InstanceIsolation|TestOrchestrator_(HealthCheck|HashBasedIDs|PortAllocation|PortReuse|ListInstances|FirstRequestLazyChrome|AggregateTabsEndpoint|StopNonexistent|InstanceCleanup))$'

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 | split("/")[0]), .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}"

  local failed_packages
  failed_packages="$(
    jq -r '
      select(.Package != null and (.Test == null or .Test == "") and (.Action == "pass" or .Action == "fail" or .Action == "skip"))
      | [.Package, .Action] | @tsv
    ' "$json_file" \
      | awk -F'\t' 'NF == 2 { status[$1] = $2 }
        END {
          for (pkg in status) {
            if (status[pkg] == "fail") {
              print pkg
            }
          }
        }' \
      | sort
  )"

  if [ -n "$failed_packages" ]; then
    echo ""
    echo -e "    ${ERROR}Failed packages:${NC}"
    while IFS= read -r pkg; do
      [ -n "$pkg" ] && echo "      βœ— $pkg"
    done <<<"$failed_packages"

    echo ""
    echo -e "    ${ERROR}Failure details:${NC}"
    while IFS= read -r pkg; do
      [ -z "$pkg" ] && continue
      echo "      $pkg"
      jq -r --arg pkg "$pkg" '
        select(.Package == $pkg and .Action == "output")
        | .Output
      ' "$json_file" \
        | sed '/^[[:space:]]*$/d' \
        | sed '/^=== RUN/d' \
        | sed '/^--- PASS:/d' \
        | sed '/^PASS$/d' \
        | tail -n 20 \
        | sed 's/^/        /'
      echo ""
    done <<<"$failed_packages"
  fi

  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 go test -json streams
run_go_test_json() {
  local json_file="$1"; shift
  local label="${1:-tests}"
  shift
  local completed=0
  local passed=0
  local failed=0
  local skipped=0
  local max_len=40
  local interactive=false
  local line_open=false
  local current_package=""
  local status_file="${json_file}.status"

  : > "$status_file"

  if [ -t 1 ]; then
    interactive=true
  fi

  render_progress() {
    local display="$1"
    if $interactive; then
      printf "\r\033[2K    ${MUTED}β–Έ${NC} ${BOLD}%-11s${NC} ${MUTED}pass:%d fail:%d skip:%d${NC} %s" \
        "$label" "$passed" "$failed" "$skipped" "$display"
      line_open=true
    fi
  }

  clear_progress_line() {
    if $interactive && $line_open; then
      printf "\r\033[2K"
      line_open=false
    fi
  }

  go test -json "$@" 2>&1 | tee "$json_file" | while IFS= read -r line; do
    local action test_name package_name elapsed output_text
    action=$(echo "$line" | jq -r '.Action // empty' 2>/dev/null) || continue
    test_name=$(echo "$line" | jq -r '.Test // empty' 2>/dev/null) || continue
    package_name=$(echo "$line" | jq -r '.Package // empty' 2>/dev/null) || continue
    elapsed=$(echo "$line" | jq -r '.Elapsed // empty' 2>/dev/null)
    output_text=$(echo "$line" | jq -r '.Output // empty' 2>/dev/null)

    if [ -z "$test_name" ]; then
      case "$action" in
        start)
          if [ -n "$package_name" ]; then
            current_package="$package_name"
            if $interactive; then
              render_progress "${MUTED}${package_name}${NC}"
            else
              printf "    ${MUTED}β–Έ${NC} ${MUTED}package${NC} %s\n" "$package_name"
            fi
          fi
          ;;
        output)
          if [ -n "$output_text" ] && [[ "$output_text" =~ ^panic:|^FAIL[[:space:]]|^---[[:space:]]FAIL ]]; then
            if ! $interactive; then
              output_text=${output_text%$'\n'}
              printf "      %s\n" "$output_text"
            fi
          fi
          ;;
        pass)
          if [ -n "$package_name" ] && $interactive; then
            render_progress "${SUCCESS}${package_name}${NC}"
          fi
          ;;
        fail)
          if [ -n "$package_name" ]; then
            if $interactive; then
              render_progress "${ERROR}${package_name}${NC}"
            else
              if [ -n "$elapsed" ]; then
                printf "    ${ERROR}βœ—${NC} ${MUTED}package${NC} %s ${MUTED}%6ss${NC}\n" "$package_name" "$elapsed"
              else
                printf "    ${ERROR}βœ—${NC} ${MUTED}package${NC} %s\n" "$package_name"
              fi
            fi
          fi
          ;;
      esac
      continue
    fi

    local display="$test_name"
    local top_level="$test_name"
    if [[ "$top_level" == *"/"* ]]; then
      top_level="${top_level%%/*}"
    fi
    if [ ${#display} -gt $max_len ]; then
      display="${display:0:$((max_len - 1))}…"
    fi

    case "$action" in
      run)
            if $interactive; then
              render_progress "${current_package} ${MUTED}${display}${NC}"
            fi ;;
      pass)
            if ! grep -Fq "$package_name	$top_level" "$status_file"; then
              printf '%s\t%s\n' "$package_name" "$top_level" >> "$status_file"
              completed=$((completed + 1))
              passed=$((passed + 1))
            fi
            if $interactive; then
              render_progress "${current_package} ${SUCCESS}${display}${NC}"
            fi ;;
      fail)
            if ! grep -Fq "$package_name	$top_level" "$status_file"; then
              printf '%s\t%s\n' "$package_name" "$top_level" >> "$status_file"
              completed=$((completed + 1))
              failed=$((failed + 1))
            fi
            if $interactive; then
              render_progress "${current_package} ${ERROR}${display}${NC}"
            else
              if [ -n "$elapsed" ]; then
                printf "    ${ERROR}βœ—${NC} ${MUTED}[%2d]${NC} %-${max_len}s ${MUTED}%6ss${NC}\n" "$completed" "$display" "$elapsed"
              else
                printf "    ${ERROR}βœ—${NC} ${MUTED}[%2d]${NC} %-${max_len}s\n" "$completed" "$display"
              fi
            fi ;;
      skip)
            if ! grep -Fq "$package_name	$top_level" "$status_file"; then
              printf '%s\t%s\n' "$package_name" "$top_level" >> "$status_file"
              completed=$((completed + 1))
              skipped=$((skipped + 1))
            fi
            if $interactive; then
              render_progress "${current_package} ${ACCENT}${display}${NC}"
            fi ;;
    esac
  done
  local test_status=${PIPESTATUS[0]}
  clear_progress_line
  return "$test_status"
}

SCOPE="${1:-all}"
TMPDIR_TEST=$(mktemp -d)
trap 'rm -rf "$TMPDIR_TEST"' EXIT

# ── Unit tests ───────────────────────────────────────────────────────

if [ "$SCOPE" = "all" ] || [ "$SCOPE" = "unit" ]; then
  section "test:πŸ”¬:go unit"

  UNIT_JSON="$TMPDIR_TEST/unit.json"

  if ! run_go_test_json "$UNIT_JSON" "unit" -p 1 -count=1 ./...; then
    fail "test:πŸ”¬:go unit"
    test_summary "$UNIT_JSON" "Unit Test Results"
    exit 1
  fi
  ok "test:πŸ”¬:go unit"
  test_summary "$UNIT_JSON" "Unit Test Results"
fi

# ── Dashboard ────────────────────────────────────────────────────────

if [ "$SCOPE" = "all" ] || [ "$SCOPE" = "dashboard" ]; then
  section "test:πŸ”¬:dashboard"
  ./scripts/test-dashboard.sh
fi

# ── Summary ──────────────────────────────────────────────────────────

section "Summary"
echo ""
echo -e "  ${SUCCESS}${BOLD}All tests passed!${NC}"
echo ""