High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(e2e): stop assertions inverting on large outputs under pipefail
Darshan Jain3008e2c879fb
diff --git a/tests/e2e/helpers/api-assertions.sh b/tests/e2e/helpers/api-assertions.shindex e0c3293d..ab42830e 100644--- a/tests/e2e/helpers/api-assertions.sh+++ b/tests/e2e/helpers/api-assertions.sh@@ -98,7 +98,7 @@ assert_contains() {local needle="$2"local desc="${3:-contains '$needle'}"- if echo "$haystack" | grep -q "$needle"; then+ if grep -q -- "$needle" <<<"$haystack"; thenpass_assert "$desc"elsefail_assert "$desc (not found)"@@ -110,7 +110,7 @@ assert_not_contains() {local needle="$2"local desc="${3:-does not contain '$needle'}"- if echo "$haystack" | grep -q "$needle"; then+ if grep -q -- "$needle" <<<"$haystack"; thenfail_assert "$desc (found when should be absent)"elsepass_assert "$desc"@@ -162,7 +162,7 @@ assert_input_not_contains() {local valuevalue=$(echo "$RESULT" | jq -r '.result // empty')- if echo "$value" | grep -qi "$forbidden"; then+ if grep -qi -- "$forbidden" <<<"$value"; thenfail_assert "$desc: found '$forbidden' in value '$value'"return 1fi@@ -197,7 +197,7 @@ assert_contains_any() {local patterns="$2"local desc="${3:-contains expected pattern}"- if echo "$haystack" | grep -qE "$patterns"; then+ if grep -qE -- "$patterns" <<<"$haystack"; thenpass_assert "$desc"elsefail_assert "$desc (no match for /$patterns/ in: $haystack)"@@ -242,9 +242,9 @@ assert_table_page() {local text="$1"local checks=0- echo "$text" | grep -q "Alice Johnson" && ((checks++))- echo "$text" | grep -q "[email protected]" && ((checks++))- echo "$text" | grep -q "Active" && ((checks++))+ grep -q -- "Alice Johnson" <<<"$text" && ((checks++))+ grep -q -- "[email protected]" <<<"$text" && ((checks++))+ grep -q -- "Active" <<<"$text" && ((checks++))if [ "$checks" -ge 3 ]; thenpass_assert "table.html: found expected table data"@@ -256,7 +256,9 @@ assert_table_page() {assert_index_page() {local snap="$1"- if echo "$snap" | jq -e '.title' | grep -q "E2E Test"; then+ local snap_title+ snap_title=$(jq -r '.title // empty' <<<"$snap")+ if grep -q -- "E2E Test" <<<"$snap_title"; thenpass_assert "index.html: correct title"elsefail_assert "index.html: wrong title"diff --git a/tests/e2e/helpers/api-http.sh b/tests/e2e/helpers/api-http.shindex af01c140..91fd0623 100644--- a/tests/e2e/helpers/api-http.sh+++ b/tests/e2e/helpers/api-http.sh@@ -373,7 +373,7 @@ assert_instance_list_absent() {assert_instance_id_prefix() {local instance_id="$1"- if echo "$instance_id" | grep -q "^inst_"; then+ if grep -q -- "^inst_" <<<"$instance_id"; thenpass_assert "instance ID has inst_ prefix: $instance_id"elsefail_assert "bad ID format: $instance_id"diff --git a/tests/e2e/helpers/api-snapshot.sh b/tests/e2e/helpers/api-snapshot.shindex 511ca089..6dbed22e 100644--- a/tests/e2e/helpers/api-snapshot.sh+++ b/tests/e2e/helpers/api-snapshot.sh@@ -50,7 +50,7 @@ assert_value() {local actualactual=$(get_value "$role" "$name")- if echo "$actual" | grep -qF "$expected"; then+ if grep -qF -- "$expected" <<<"$actual"; thenpass_assert "$name = '$actual'"elsefail_assert "$name: expected '$expected', got '$actual'"diff --git a/tests/e2e/helpers/autosolver.sh b/tests/e2e/helpers/autosolver.shindex 95101664..5da82639 100644--- a/tests/e2e/helpers/autosolver.sh+++ b/tests/e2e/helpers/autosolver.sh@@ -278,7 +278,7 @@ autosolver_title_has_challenge() {local patternfor pattern in "${AUTOSOLVER_CHALLENGE_PATTERNS[@]}"; do- if printf '%s' "$title_lower" | grep -q "$pattern"; then+ if grep -q -- "$pattern" <<<"$title_lower"; thenreturn 0fidonediff --git a/tests/e2e/helpers/cli.sh b/tests/e2e/helpers/cli.shindex e22e6867..d53a613c 100644--- a/tests/e2e/helpers/cli.sh+++ b/tests/e2e/helpers/cli.sh@@ -87,7 +87,7 @@ assert_output_contains() {local expected="$1"local desc="${2:-output contains '$expected'}"- if echo "$PT_OUT" | grep -q "$expected"; then+ if grep -q -- "$expected" <<<"$PT_OUT"; thenpass_assert "$desc"elsefail_assert "$desc"@@ -209,7 +209,7 @@ assert_output_not_contains() {local forbidden="$1"local desc="${2:-output does not contain '$forbidden'}"- if echo "$PT_OUT" | grep -q "$forbidden"; then+ if grep -q -- "$forbidden" <<<"$PT_OUT"; thenfail_assert "$desc"echo -e " ${RED} output was: $PT_OUT${NC}"elsediff --git a/tests/e2e/pipefail_rules_test.go b/tests/e2e/pipefail_rules_test.gonew file mode 100644index 00000000..324f2128--- /dev/null+++ b/tests/e2e/pipefail_rules_test.go@@ -0,0 +1,93 @@+package e2e++import (+ "os"+ "path/filepath"+ "regexp"+ "strconv"+ "strings"+ "testing"+)++// The helper layer is small; if the walk finds fewer files than this it matched+// the wrong directory and the rule below would pass for the wrong reason.+const minHelperFilesScanned = 5++// pipesIntoEarlyExitReader matches `… | grep -q …`, in any of the flag spellings+// used in this tree (-q, -qi, -qE, -qF), reached through a pipe.+//+// Under `set -o pipefail` — which helpers/base.sh sets for every scenario — this+// shape is a race, not a style preference. grep -q exits the moment it matches,+// which closes the read end of the pipe; if the writer has not finished, it dies+// of SIGPIPE with status 141 and pipefail promotes that to the status of the+// whole pipeline. The assertion then reads the opposite of the truth:+//+// assert_output_contains reports a FAILURE on output that does contain the needle+// assert_output_not_contains reports a PASS on output that does contain the needle+//+// The second is the dangerous one: it is how "token not leaked to stdout" is+// checked. Both flip once the payload outgrows the pipe buffer, so they are+// invisible until an endpoint's output grows.+//+// Read from a here-string instead: `grep -q -- "$needle" <<<"$haystack"`.+var pipesIntoEarlyExitReader = regexp.MustCompile(`\|\s*grep\s+-[a-zA-Z]*q`)++func loadHelperFiles(t *testing.T) map[string]string {+ t.Helper()++ files := map[string]string{}+ entries, err := os.ReadDir("helpers")+ if err != nil {+ t.Fatalf("cannot read helpers/, so this rule would check nothing: %v", err)+ }+ for _, e := range entries {+ if e.IsDir() || !strings.HasSuffix(e.Name(), ".sh") {+ continue+ }+ path := filepath.Join("helpers", e.Name())+ body, readErr := os.ReadFile(path)+ if readErr != nil {+ t.Fatalf("read %s: %v", path, readErr)+ }+ files[path] = string(body)+ }+ if len(files) < minHelperFilesScanned {+ t.Fatalf("scanned %d helper files, want at least %d; the walk matched almost nothing and this rule would pass vacuously", len(files), minHelperFilesScanned)+ }+ return files+}++func TestNoHelperPipesIntoGrepQ(t *testing.T) {+ var offenders []string+ for path, body := range loadHelperFiles(t) {+ for i, line := range strings.Split(body, "\n") {+ if strings.HasPrefix(strings.TrimSpace(line), "#") {+ continue+ }+ if pipesIntoEarlyExitReader.MatchString(line) {+ offenders = append(offenders, path+":"+strconv.Itoa(i+1)+": "+strings.TrimSpace(line))+ }+ }+ }+ if len(offenders) > 0 {+ t.Fatalf("piping into `grep -q` under `set -o pipefail` inverts the assertion "++ "once the payload outgrows the pipe buffer: grep exits early, the writer takes "++ "SIGPIPE, and pipefail returns 141 for a pipeline that actually matched. Read "++ "from a here-string instead — `grep -q -- \"$needle\" <<<\"$haystack\"`.\n %s",+ strings.Join(offenders, "\n "))+ }+}++// helpers/base.sh sets pipefail for every scenario, which is what makes the rule+// above load-bearing rather than stylistic. If that line ever goes away the rule+// should be revisited, not silently kept.+func TestHelpersStillSetPipefail(t *testing.T) {+ body, err := os.ReadFile(filepath.Join("helpers", "base.sh"))+ if err != nil {+ t.Fatalf("read helpers/base.sh: %v", err)+ }+ if !strings.Contains(string(body), "pipefail") {+ t.Fatal("helpers/base.sh no longer sets pipefail; TestNoHelperPipesIntoGrepQ was written " ++ "for that mode and its rationale needs rechecking before it is trusted or removed")+ }+}