Skip to content

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

2.2 KB
Raw Blame
package bridge

import (
	"context"
	"errors"
	"strings"
	"testing"

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

// Only a genuine no-match must carry ErrSelectorNoMatch (→ 404); unsupported
// kinds and internal routing errors must NOT (→ 5xx).
func TestResolveUnifiedSelector_ErrorClassification(t *testing.T) {
	ctx := context.Background()

	if _, err := ResolveUnifiedSelectorInFrame(ctx, selector.Selector{Kind: selector.KindRef, Value: "e99"}, nil, ""); !errors.Is(err, ErrSelectorNoMatch) {
		t.Errorf("ref-not-found should be ErrSelectorNoMatch (404), got %v", err)
	}
	if _, err := ResolveUnifiedSelectorInFrame(ctx, selector.Selector{Kind: selector.KindSemantic, Value: "Save"}, nil, ""); err == nil || errors.Is(err, ErrSelectorNoMatch) {
		t.Errorf("semantic-at-resolver should be a non-no-match internal error (5xx), got %v", err)
	}
	if _, err := ResolveUnifiedSelectorInFrame(ctx, selector.Selector{Kind: "bogus", Value: "x"}, nil, ""); err == nil || errors.Is(err, ErrSelectorNoMatch) {
		t.Errorf("unknown selector kind should be non-no-match (5xx), got %v", err)
	}
}

func TestResolveSelectorAtFnSearchesOpenShadowRoots(t *testing.T) {
	if !strings.Contains(resolveSelectorAtFn, "shadowRoot") {
		t.Fatal("selector resolver should walk open shadow roots")
	}
	if !strings.Contains(resolveSelectorAtFn, "return pick(deepQueryAll(value))") {
		t.Fatal("css selectors should use deepQueryAll")
	}
	if strings.Contains(resolveSelectorAtFn, "return pick(Array.from(root.querySelectorAll(value)))") {
		t.Fatal("css selectors should not use document-only querySelectorAll")
	}
}

func TestResolveUnifiedSelector_FirstRef(t *testing.T) {
	cache := &RefCache{Refs: map[string]int64{"e5": 42}}
	got, err := ResolveUnifiedSelectorInFrame(context.Background(), selector.Parse("first:e5"), cache, "")
	if err != nil {
		t.Fatalf("ResolveUnifiedSelectorInFrame returned error: %v", err)
	}
	if got != 42 {
		t.Fatalf("node id = %d, want 42", got)
	}
}

func TestResolveUnifiedSelector_LastRefRejected(t *testing.T) {
	cache := &RefCache{Refs: map[string]int64{"e5": 42}}
	if _, err := ResolveUnifiedSelectorInFrame(context.Background(), selector.Parse("last:e5"), cache, ""); err == nil {
		t.Fatal("expected last:ref to fail")
	}
}