Skip to content

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

7.9 KB
Raw Blame
package bridge

import (
	"context"
	"os"
	"path/filepath"
	"sync/atomic"
	"testing"
	"time"

	"github.com/pinchtab/pinchtab/internal/config"
)

func withMockedProcessLookup(t *testing.T, pidsFor func() []int) (termCount, killCount *atomic.Int32) {
	t.Helper()
	origFind := findChromePIDsByProfileDirFunc
	origTerm := terminateChromeByProfileDirFunc
	origKill := killChromeByProfileDirFunc
	origPoll := chromeExitPollInterval

	var term atomic.Int32
	var kill atomic.Int32

	findChromePIDsByProfileDirFunc = func(string) []int { return pidsFor() }
	terminateChromeByProfileDirFunc = func(string) int {
		term.Add(1)
		return len(pidsFor())
	}
	killChromeByProfileDirFunc = func(string) int {
		kill.Add(1)
		return len(pidsFor())
	}
	chromeExitPollInterval = 5 * time.Millisecond

	t.Cleanup(func() {
		findChromePIDsByProfileDirFunc = origFind
		terminateChromeByProfileDirFunc = origTerm
		killChromeByProfileDirFunc = origKill
		chromeExitPollInterval = origPoll
	})

	return &term, &kill
}

func TestCleanup_RemovesTempProfileDir(t *testing.T) {
	tmpDir := t.TempDir()
	profileDir := filepath.Join(tmpDir, "pinchtab-profile-test")
	if err := os.MkdirAll(profileDir, 0o755); err != nil {
		t.Fatal(err)
	}

	ctx := context.TODO()
	b := New(ctx, ctx, &config.RuntimeConfig{})
	b.tempProfileDir = profileDir

	b.Cleanup()

	if _, err := os.Stat(profileDir); !os.IsNotExist(err) {
		t.Errorf("expected temp profile dir to be removed, but it still exists")
	}
	if b.tempProfileDir != "" {
		t.Errorf("expected tempProfileDir to be cleared, got %q", b.tempProfileDir)
	}
}

func TestCleanup_NoTempDir(t *testing.T) {
	ctx := context.TODO()
	b := New(ctx, ctx, &config.RuntimeConfig{})
	b.Cleanup()
}

func withShortenedGrace(t *testing.T, grace, termGrace time.Duration) {
	t.Helper()
	origGrace := BridgeShutdownGracePeriod
	origTerm := bridgeShutdownTermGrace
	origFast := bridgeFastShutdownGrace
	BridgeShutdownGracePeriod = grace
	bridgeShutdownTermGrace = termGrace
	bridgeFastShutdownGrace = grace
	t.Cleanup(func() {
		BridgeShutdownGracePeriod = origGrace
		bridgeShutdownTermGrace = origTerm
		bridgeFastShutdownGrace = origFast
	})
}

func TestCleanup_WaitsForChromeExit_BeforeSIGKILL(t *testing.T) {
	withShortenedGrace(t, 500*time.Millisecond, 200*time.Millisecond)

	start := time.Now()
	// Report alive for the first ~100ms then gone.
	termCount, killCount := withMockedProcessLookup(t, func() []int {
		if time.Since(start) < 100*time.Millisecond {
			return []int{12345}
		}
		return nil
	})

	ctx := context.TODO()
	b := New(ctx, ctx, &config.RuntimeConfig{ProfileDir: "/tmp/pinchtab-test-profile"})
	b.Cleanup()

	if got := termCount.Load(); got != 0 {
		t.Errorf("expected 0 SIGTERM escalations when chrome exits during grace, got %d", got)
	}
	if got := killCount.Load(); got != 0 {
		t.Errorf("expected 0 SIGKILL escalations when chrome exits during grace, got %d", got)
	}
}

func TestCleanup_FallsBackToSIGKILL_WhenChromeWontDie(t *testing.T) {
	withShortenedGrace(t, 80*time.Millisecond, 80*time.Millisecond)

	termCount, killCount := withMockedProcessLookup(t, func() []int {
		return []int{67890} // always alive
	})

	ctx := context.TODO()
	b := New(ctx, ctx, &config.RuntimeConfig{ProfileDir: "/tmp/pinchtab-test-profile"})
	b.Cleanup()

	if got := termCount.Load(); got == 0 {
		t.Errorf("expected SIGTERM escalation for persistent chrome profile, got 0")
	}
	if got := killCount.Load(); got == 0 {
		t.Errorf("expected SIGKILL escalation when chrome never exits, got 0")
	}
}

func TestCleanup_TempProfileUsesFastKill(t *testing.T) {
	withShortenedGrace(t, 20*time.Millisecond, 20*time.Millisecond)
	termCount, killCount := withMockedProcessLookup(t, func() []int {
		return []int{67890}
	})

	profileDir := t.TempDir()
	ctx := context.TODO()
	b := New(ctx, ctx, &config.RuntimeConfig{ProfileDir: "/tmp/pinchtab-test-profile"})
	b.tempProfileDir = profileDir
	b.Cleanup()

	if got := termCount.Load(); got != 0 {
		t.Errorf("temp profile cleanup must not SIGTERM, got %d", got)
	}
	if got := killCount.Load(); got == 0 {
		t.Errorf("expected SIGKILL escalation for temp profile when chrome never exits, got 0")
	}
}

func TestCleanup_CloakTermBeforeKill(t *testing.T) {
	withShortenedGrace(t, 80*time.Millisecond, 80*time.Millisecond)

	var termOrder, killOrder atomic.Int64
	var step atomic.Int64

	origFind := findChromePIDsByProfileDirFunc
	origTerm := terminateChromeByProfileDirFunc
	origKill := killChromeByProfileDirFunc
	origPoll := chromeExitPollInterval
	t.Cleanup(func() {
		findChromePIDsByProfileDirFunc = origFind
		terminateChromeByProfileDirFunc = origTerm
		killChromeByProfileDirFunc = origKill
		chromeExitPollInterval = origPoll
	})
	chromeExitPollInterval = 5 * time.Millisecond

	findChromePIDsByProfileDirFunc = func(string) []int { return []int{42} }
	terminateChromeByProfileDirFunc = func(string) int {
		termOrder.Store(step.Add(1))
		return 1
	}
	killChromeByProfileDirFunc = func(string) int {
		killOrder.Store(step.Add(1))
		return 1
	}

	ctx := context.TODO()
	b := New(ctx, ctx, &config.RuntimeConfig{
		DefaultBrowser: config.BrowserCloak,
		ProfileDir:     "/tmp/pinchtab-test-profile",
	})
	b.Cleanup()

	to := termOrder.Load()
	ko := killOrder.Load()
	if to == 0 {
		t.Fatalf("expected SIGTERM to be called, but it was not")
	}
	if ko == 0 {
		t.Fatalf("expected SIGKILL to be called, but it was not")
	}
	if to >= ko {
		t.Errorf("expected SIGTERM before SIGKILL, got term=%d kill=%d", to, ko)
	}
}

func TestCleanup_RemoteCDP_SkipsKillLogic(t *testing.T) {
	withShortenedGrace(t, 20*time.Millisecond, 20*time.Millisecond)
	termCount, killCount := withMockedProcessLookup(t, func() []int { return []int{1} })

	ctx := context.TODO()
	b := New(ctx, ctx, &config.RuntimeConfig{
		ProfileDir:   "/tmp/pinchtab-test-profile",
		RemoteCDPURL: "http://127.0.0.1:9222",
	})
	b.Cleanup()

	if got := termCount.Load(); got != 0 {
		t.Errorf("remote-CDP cleanup must not SIGTERM, got %d", got)
	}
	if got := killCount.Load(); got != 0 {
		t.Errorf("remote-CDP cleanup must not SIGKILL, got %d", got)
	}
}

func TestCleanup_CDPAttachURLOnly_SkipsKillLogic(t *testing.T) {
	withShortenedGrace(t, 20*time.Millisecond, 20*time.Millisecond)
	termCount, killCount := withMockedProcessLookup(t, func() []int { return []int{1} })

	ctx := context.TODO()
	b := New(ctx, ctx, &config.RuntimeConfig{
		ProfileDir:   "/tmp/pinchtab-test-profile",
		CDPAttachURL: "http://127.0.0.1:9222",
	})
	b.Cleanup()

	if got := termCount.Load(); got != 0 {
		t.Errorf("CDP attach cleanup must not SIGTERM, got %d", got)
	}
	if got := killCount.Load(); got != 0 {
		t.Errorf("CDP attach cleanup must not SIGKILL, got %d", got)
	}
}

func TestRestartBrowser_CDPAttachURLOnly_SkipsKillLogic(t *testing.T) {
	origDrain := browserRestartDrainWindow
	browserRestartDrainWindow = time.Millisecond
	t.Cleanup(func() { browserRestartDrainWindow = origDrain })
	_, killCount := withMockedProcessLookup(t, func() []int { return []int{1} })

	cfg := &config.RuntimeConfig{
		ProfileDir:         t.TempDir(),
		CDPAttachURL:       "http://127.0.0.1:1",
		AttachAllowHosts:   []string{"127.0.0.1"},
		AttachAllowSchemes: []string{"http"},
	}
	b := New(context.Background(), nil, cfg)
	if err := b.RestartBrowser(cfg); err == nil {
		t.Fatal("restart against an unreachable external CDP endpoint unexpectedly succeeded")
	}
	if got := killCount.Load(); got != 0 {
		t.Errorf("external restart must not SIGKILL, got %d", got)
	}
}

func TestFetchPauseSuppressionLifecycle(t *testing.T) {
	b := &Bridge{}
	flag := b.fetchPauseSuppression("t1")
	if flag == nil || flag.Load() {
		t.Fatal("fresh flag should exist and be false")
	}
	b.SetFetchPauseSuppressed("t1", true)
	if !flag.Load() {
		t.Fatal("the same flag instance must observe SetFetchPauseSuppressed")
	}
	b.dropFetchPauseSuppression("t1")
	if recreated := b.fetchPauseSuppression("t1"); recreated == flag || recreated.Load() {
		t.Fatal("dropped flag should be recreated fresh and unset")
	}
	if b.fetchPauseSuppression("") != nil {
		t.Fatal("empty tab ID must not allocate a flag")
	}
}