Skip to content

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

fix(e2e): track scenario-start snapshot success so a zero-tab baseline still closes opened tabs (PIN-381)

Luigi Agosti9eb2f2cd867f
diff --git a/tests/e2e/helpers/api-http.sh b/tests/e2e/helpers/api-http.sh
index 3f7b97cb..97984009 100644
--- a/tests/e2e/helpers/api-http.sh
+++ b/tests/e2e/helpers/api-http.sh
@@ -298,12 +298,28 @@ _e2e_snapshot_tab_ids() {
e2e_curl -s --max-time 5 "${E2E_SERVER}/tabs" 2>/dev/null | jq -r '.tabs[].id // empty' 2>/dev/null || true
}
+# Records the scenario's starting tab set and, separately, whether the snapshot
+# succeeded. A successful empty baseline (0 tabs) and a failed snapshot both leave
+# an empty id set, but cleanup must treat them oppositely, so the success flag is
+# tracked on its own rather than inferred from the contents.
+_e2e_record_tab_baseline() {
+ SCENARIO_TAB_BASELINE=""
+ SCENARIO_TAB_BASELINE_OK=0
+ local body status
+ body=$(e2e_curl -s --max-time 5 -w $'\n%{http_code}' "${E2E_SERVER}/tabs" 2>/dev/null) || return 0
+ status="${body##*$'\n'}"
+ [ "$status" = "200" ] || return 0
+ SCENARIO_TAB_BASELINE_OK=1
+ SCENARIO_TAB_BASELINE="$(printf '%s' "${body%$'\n'*}" | jq -r '.tabs[].id // empty' 2>/dev/null | tr '\n' ' ')"
+}
+
_e2e_close_leaked_tabs() {
local id
- # An empty baseline means the start-of-scenario GET /tabs failed or the instance
- # was gone; "close everything not in an empty set" would close the only tab and
- # tear down the browser, so skip and let the count check report the drift.
- [ -n "${SCENARIO_TAB_BASELINE// /}" ] || return 0
+ # Skip only when the start-of-scenario snapshot FAILED: "close everything not in
+ # an unknown set" could close the only tab and tear the browser down. A
+ # successful empty baseline means the scenario began with no tabs, so every tab
+ # present now is one it opened and must be closed.
+ [ "${SCENARIO_TAB_BASELINE_OK:-0}" = "1" ] || return 0
while read -r id; do
[ -n "$id" ] || continue
case " ${SCENARIO_TAB_BASELINE} " in
diff --git a/tests/e2e/helpers/base.sh b/tests/e2e/helpers/base.sh
index af4573f1..52eac273 100644
--- a/tests/e2e/helpers/base.sh
+++ b/tests/e2e/helpers/base.sh
@@ -263,10 +263,11 @@ assert_ref_json_jq() {
}
SCENARIO_TAB_BASELINE="${SCENARIO_TAB_BASELINE:-}"
+SCENARIO_TAB_BASELINE_OK="${SCENARIO_TAB_BASELINE_OK:-0}"
record_scenario_tab_baseline() {
- if declare -F _e2e_snapshot_tab_ids >/dev/null 2>&1; then
- SCENARIO_TAB_BASELINE="$(_e2e_snapshot_tab_ids | tr '\n' ' ')"
+ if declare -F _e2e_record_tab_baseline >/dev/null 2>&1; then
+ _e2e_record_tab_baseline
fi
}
diff --git a/tests/e2e/tab_cleanup_test.go b/tests/e2e/tab_cleanup_test.go
new file mode 100644
index 00000000..29f4441e
--- /dev/null
+++ b/tests/e2e/tab_cleanup_test.go
@@ -0,0 +1,95 @@
+package e2e
+
+import (
+ "os"
+ "os/exec"
+ "runtime"
+ "sort"
+ "strings"
+ "testing"
+)
+
+// tabCleanupHarness sources the real helpers, stubs the two I/O seams
+// (_e2e_snapshot_tab_ids and e2e_curl), then drives _e2e_close_leaked_tabs and
+// prints one "CLOSED <id>" line per tab it tried to close. Overriding the seams
+// after the source is what keeps the function under test the real one.
+// The close call inside _e2e_close_leaked_tabs redirects stdout/stderr to
+// /dev/null, so the stub records to $CLOSE_LOG (which the redirect does not
+// touch) rather than to stdout.
+const tabCleanupHarness = `
+set -uo pipefail
+source helpers/base.sh >/dev/null 2>&1
+source helpers/api-http.sh >/dev/null 2>&1
+_e2e_snapshot_tab_ids() { local t; for t in ${STUB_CURRENT:-}; do echo "$t"; done; }
+e2e_curl() {
+ local a
+ for a in "$@"; do
+ case "$a" in
+ *'"tabId"'*) printf 'CLOSED %s\n' "$(printf '%s' "$a" | sed -n 's/.*"tabId":"\([^"]*\)".*/\1/p')" >> "$CLOSE_LOG" ;;
+ esac
+ done
+}
+SCENARIO_TAB_BASELINE="${BASELINE:-}"
+SCENARIO_TAB_BASELINE_OK="${OK:-0}"
+_e2e_close_leaked_tabs
+`
+
+func runTabCleanup(t *testing.T, baseline, ok, current string) []string {
+ t.Helper()
+ closeLog := t.TempDir() + "/closed.log"
+ cmd := exec.Command("bash", "-c", tabCleanupHarness)
+ cmd.Dir = "."
+ cmd.Env = append(os.Environ(), "BASELINE="+baseline, "OK="+ok, "STUB_CURRENT="+current, "CLOSE_LOG="+closeLog)
+ if raw, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("harness failed: %v\n%s", err, raw)
+ }
+ logged, err := os.ReadFile(closeLog)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return nil
+ }
+ t.Fatalf("read close log: %v", err)
+ }
+ var closed []string
+ for _, line := range strings.Split(string(logged), "\n") {
+ if rest, ok := strings.CutPrefix(line, "CLOSED "); ok {
+ closed = append(closed, strings.TrimSpace(rest))
+ }
+ }
+ sort.Strings(closed)
+ return closed
+}
+
+func TestCloseLeakedTabsClosesOnlyNonBaselineTabs(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("the helper under test is a bash function")
+ }
+
+ t.Run("keeps the baseline tab and closes the scenario's, even when it is listed first", func(t *testing.T) {
+ // GET /tabs lists the current tab first (PIN-370); after a scenario that
+ // opened T1 the list is [T1, T0, T2] with baseline T0. Only T0 must survive.
+ got := runTabCleanup(t, "T0", "1", "T1 T0 T2")
+ want := []string{"T1", "T2"}
+ if strings.Join(got, ",") != strings.Join(want, ",") {
+ t.Fatalf("closed %v, want %v (baseline tab must survive, scenario tabs must close)", got, want)
+ }
+ })
+
+ t.Run("a successful empty baseline closes every tab the scenario opened", func(t *testing.T) {
+ // A scenario that starts with zero tabs (the normal state after a prior
+ // scenario closed its own) opens T1 and T2; both are leaks and must close.
+ // This is the case the empty-baseline skip got wrong.
+ got := runTabCleanup(t, "", "1", "T1 T2")
+ want := []string{"T1", "T2"}
+ if strings.Join(got, ",") != strings.Join(want, ",") {
+ t.Fatalf("closed %v, want %v (a successful empty baseline must close every open tab)", got, want)
+ }
+ })
+
+ t.Run("a failed start snapshot skips cleanup so it cannot close the only tab", func(t *testing.T) {
+ got := runTabCleanup(t, "", "0", "T1")
+ if len(got) != 0 {
+ t.Fatalf("closed %v, want none (a failed baseline snapshot must skip cleanup)", got)
+ }
+ })
+}