fix: 阻止售罄页误入规格采集 (#159)

This commit is contained in:
chengma
2026-08-11 15:49:12 +08:00
parent df1287f8eb
commit a875fbc981
3 changed files with 123 additions and 11 deletions
+27
View File
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<hierarchy rotation="0">
<node class="android.widget.FrameLayout" package="com.xunmeng.pinduoduo"
bounds="[0,0][1080,2340]" visible-to-user="true" enabled="true">
<node class="androidx.recyclerview.widget.RecyclerView"
package="com.xunmeng.pinduoduo" scrollable="true"
bounds="[0,0][1080,2214]" visible-to-user="true" enabled="true">
<node class="android.widget.TextView" package="com.xunmeng.pinduoduo"
text="商品已售罄,推荐以下相似商品"
bounds="[225,332][855,393]" visible-to-user="true" enabled="true" />
<node class="android.view.ViewGroup" package="com.xunmeng.pinduoduo"
clickable="true" bounds="[0,927][535,1690]"
visible-to-user="true" enabled="true">
<node class="android.widget.TextView" package="com.xunmeng.pinduoduo"
text="测试推荐商品甲" bounds="[24,1482][511,1531]"
visible-to-user="true" enabled="true" />
</node>
<node class="android.view.ViewGroup" package="com.xunmeng.pinduoduo"
clickable="true" bounds="[545,927][1080,1690]"
visible-to-user="true" enabled="true">
<node class="android.widget.TextView" package="com.xunmeng.pinduoduo"
text="测试推荐商品乙" bounds="[569,1482][1056,1531]"
visible-to-user="true" enabled="true" />
</node>
</node>
</node>
</hierarchy>
+52
View File
@@ -344,6 +344,9 @@ class PddCollectParserTest(unittest.TestCase):
cls.non_scrollable_spec_xml = (
FIXTURES / "pdd_spec_panel_non_scrollable.xml"
).read_text(encoding="utf-8")
cls.sold_out_xml = (
FIXTURES / "pdd_sold_out_recommendations.xml"
).read_text(encoding="utf-8")
def test_quantity_keeps_raw_value_and_approximate_flag(self):
result = parse_quantity("已拼1.2万+件")
@@ -483,6 +486,55 @@ class PddCollectParserTest(unittest.TestCase):
def test_goods_page_is_not_non_scrollable_spec_panel(self):
self.assertFalse(_is_spec_panel_open(self.home_xml))
def test_sold_out_recommendations_are_not_spec_options(self):
with self.assertRaises(PddCollectError) as raised:
parse_spec_panel(self.sold_out_xml)
self.assertEqual(raised.exception.code, "PDD_GOODS_UNAVAILABLE")
self.assertEqual(
raised.exception.diagnostics, {"page_reason": "out_of_stock"}
)
page_without_marker = self.sold_out_xml.replace(
"商品已售罄,推荐以下相似商品", "为你推荐以下相似商品"
)
with self.assertRaises(PddCollectError) as incomplete:
parse_spec_panel(page_without_marker)
self.assertEqual(incomplete.exception.code, "PDD_DATA_SPEC_INCOMPLETE")
def test_blank_pdd_tree_is_not_a_spec_panel(self):
xml_data = """<hierarchy>
<node class="android.widget.FrameLayout"
package="com.xunmeng.pinduoduo"
bounds="[0,0][1080,2340]" />
</hierarchy>"""
self.assertFalse(_is_spec_panel_open(xml_data))
with self.assertRaises(PddCollectError) as raised:
parse_spec_panel(xml_data)
self.assertEqual(raised.exception.code, "PDD_DATA_SPEC_INCOMPLETE")
def test_collect_stops_on_sold_out_page_without_clicking_recommendations(self):
device = FakeCollectDevice(self.home_xml, self.sold_out_xml)
service = PddCollectService(
PddDeviceService(lambda _serial: device),
"USB-001",
"client-001",
sleeper=lambda _seconds: None,
)
with self.assertRaises(PddCollectError) as raised:
service.collect(
FakeTask("https://mobile.yangkeduo.com/goods.html?goods_id=123")
)
self.assertEqual(raised.exception.code, "PDD_GOODS_UNAVAILABLE")
self.assertEqual(
raised.exception.diagnostics["page_reason"], "out_of_stock"
)
# 只点击商品页规格入口,不点击任何推荐商品。
self.assertEqual(len(device.clicks), 1)
def test_full_window_with_scattered_labels_is_not_spec_panel(self):
xml_data = """<hierarchy>
<node class="android.widget.FrameLayout" bounds="[0,0][1080,2400]">