Skip to content

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

fix(extract): a bare ref scopes or pins a field like ref: does and a bare XPath is refused like xpath:, matching what the docs and the --fields table print (PIN-394)

Luigi Agosti5d5ae1cd7212
diff --git a/internal/extract/extract_test.go b/internal/extract/extract_test.go
index e1a21931..759ce2db 100644
--- a/internal/extract/extract_test.go
+++ b/internal/extract/extract_test.go
@@ -320,6 +320,8 @@ func TestParseSchema_UnsupportedConstructs(t *testing.T) {
{"non-object root", `{"type":"array","properties":{"price":{"type":"string"}}}`, "type"},
{"css hint", `{"type":"object","properties":{"price":{"type":"number","x-pinchtab-hint":"css:.price"}}}`, "properties.price.x-pinchtab-hint"},
{"xpath hint", `{"type":"object","properties":{"price":{"type":"number","x-pinchtab-hint":"xpath://span"}}}`, "properties.price.x-pinchtab-hint"},
+ {"bare xpath hint", `{"type":"object","properties":{"price":{"type":"number","x-pinchtab-hint":"//span"}}}`, "properties.price.x-pinchtab-hint"},
+ {"bare xpath scope", `{"type":"object","properties":{"rows":{"type":"array","x-pinchtab-scope":"(//table)[1]","items":{"type":"object","properties":{"id":{"type":"string"}}}}}}`, "properties.rows.x-pinchtab-scope"},
{"css scope", `{"type":"object","properties":{"rows":{"type":"array","x-pinchtab-scope":"css:table","items":{"type":"object","properties":{"id":{"type":"string"}}}}}}`, "properties.rows.x-pinchtab-scope"},
{"array without items", `{"type":"object","properties":{"tags":{"type":"array"}}}`, "properties.tags.items"},
{"array of strings", `{"type":"object","properties":{"tags":{"type":"array","items":{"type":"string"}}}}`, "properties.tags.items.type"},
diff --git a/internal/extract/group_test.go b/internal/extract/group_test.go
index 81cd0ff6..b5b14c6e 100644
--- a/internal/extract/group_test.go
+++ b/internal/extract/group_test.go
@@ -167,7 +167,7 @@ func TestResolveArray_ScopeForcesContainer(t *testing.T) {
t.Errorf("links = %v, want 4 nav items", list)
}
- for _, scope := range []string{"ref:e63", "ref:e64"} {
+ for _, scope := range []string{"ref:e63", "ref:e64", "e63", "e64"} {
byRef := mustSchema(t, `{"type":"object","properties":{
"rows":{"type":"array","x-pinchtab-scope":"`+scope+`","items":{"type":"object","properties":{
"order":{"type":"integer","description":"order number"}}}}}}`)
@@ -363,3 +363,28 @@ func TestSchemaWithScopeRefusesABrowserSelectorNamingTheScope(t *testing.T) {
t.Fatalf("err = %v (%T), want an UnsupportedError at path scope", err, err)
}
}
+
+func TestRequestScopeAcceptsABareRefLikeTheFieldsTablePrintsIt(t *testing.T) {
+ nodes := loadSnapshot(t, "extract-list.json")
+ schema := mustSchema(t, `{"type":"object","properties":{
+ "entries":{"type":"array","items":{"type":"object","properties":{
+ "amount":{"type":"number","description":"price"}}}}}}`)
+ for _, scope := range []string{"ref:e64", "e64"} {
+ scoped, err := schema.WithScope(scope)
+ if err != nil {
+ t.Fatalf("WithScope(%q): %v", scope, err)
+ }
+ if n := len(items(t, Resolve(scoped, nodes, Options{}), "entries")); n != 5 {
+ t.Errorf("scope %q entries = %d, want the 5 table rows", scope, n)
+ }
+ }
+}
+
+func TestHintAcceptsABareRef(t *testing.T) {
+ schema := mustSchema(t, `{"type":"object","properties":{
+ "colour":{"type":"string","x-pinchtab-hint":"e5"}}}`)
+ got := Resolve(schema, productNodes(), Options{})
+ if got.Fields["colour"].Ref != "e5" || got.Data["colour"] != "Midnight Black" {
+ t.Errorf("colour = %+v data=%v, want the e5 node's text", got.Fields["colour"], got.Data["colour"])
+ }
+}
diff --git a/internal/extract/schema.go b/internal/extract/schema.go
index c86a8cd9..24faa459 100644
--- a/internal/extract/schema.go
+++ b/internal/extract/schema.go
@@ -199,11 +199,11 @@ func parseTarget(raw, path string) (target, error) {
if raw == "" {
return target{}, nil
}
- if !selector.HasKnownPrefix(raw) {
+ sel := selector.Parse(raw)
+ if sel.Kind == selector.KindCSS && !selector.HasKnownPrefix(raw) {
return target{kind: targetQuery, value: raw}, nil
}
- sel := selector.Parse(raw)
switch sel.Kind {
case selector.KindCSS, selector.KindXPath:
return target{}, &UnsupportedError{Path: path, Reason: string(sel.Kind) + " selectors need a browser and are not supported"}
diff --git a/tests/e2e/scenarios/api/extract-basic.sh b/tests/e2e/scenarios/api/extract-basic.sh
index f9015be3..4f6290c5 100755
--- a/tests/e2e/scenarios/api/extract-basic.sh
+++ b/tests/e2e/scenarios/api/extract-basic.sh
@@ -134,6 +134,10 @@ pt_post /extract -d "{\"schema\":${AMOUNTS_SCHEMA},\"scope\":\"ref:${TABLE_REF}\
assert_ok "extract scoped to ref:${TABLE_REF}"
assert_json_eq "$RESULT" '.data.entries | length' '5' "scope ref:<table ref> reads the same 5 rows"
+pt_post /extract -d "{\"schema\":${AMOUNTS_SCHEMA},\"scope\":\"${TABLE_REF}\"}"
+assert_ok "extract scoped to the bare ref ${TABLE_REF}"
+assert_json_eq "$RESULT" '.data.entries | length' '5' "a bare ref scope reads the same 5 rows as ref:"
+
pt_post /extract -d "{\"schema\":${PRODUCT_SCHEMA},\"scope\":\"ref:e99999\"}"
assert_ok "a scope that matches nothing still answers 200"
assert_json_eq "$RESULT" '.data' '{}' "no data outside a missing scope"
@@ -146,4 +150,7 @@ assert_http_error 400 "scope: css" "a CSS scope is a 400 naming scope"
pt_post /extract -d "{\"schema\":${AMOUNTS_SCHEMA},\"scope\":\"xpath://table\"}"
assert_http_error 400 "scope: xpath" "an XPath scope is a 400 naming scope"
+pt_post /extract -d "{\"schema\":${AMOUNTS_SCHEMA},\"scope\":\"//table\"}"
+assert_http_error 400 "scope: xpath" "a bare XPath scope is a 400 naming scope"
+
end_test
diff --git a/tests/e2e/scenarios/cli/extract-basic.sh b/tests/e2e/scenarios/cli/extract-basic.sh
index 547b7029..857a9b5e 100755
--- a/tests/e2e/scenarios/cli/extract-basic.sh
+++ b/tests/e2e/scenarios/cli/extract-basic.sh
@@ -114,6 +114,11 @@ assert_out_jq '.entries | length' '6' "unscoped amounts come from the 6-product
doc_ok 'pinchtab extract --schema amounts.schema.json --scope role:table'
assert_out_jq '.entries | length' '5' "--scope role:table reads the 5 table rows"
+pt_ok extract --schema "$SCHEMAS/amounts.schema.json" --scope role:table --json
+TABLE_REF=$(jq -r '.fields.entries.ref' <<<"$PT_OUT")
+pt_ok extract --schema "$SCHEMAS/amounts.schema.json" --scope "$TABLE_REF"
+assert_out_jq '.entries | length' '5' "--scope with the bare ref ${TABLE_REF} reads the same 5 rows"
+
end_test
# ─────────────────────────────────────────────────────────────────