fix(trading): 必填选项自动填值跳过「選択してください」占位项,并把选项开放给接口

trading 自动填 choice 时取 values[0],而必填 select 的 values[0] 恒为 id=0 的
「選択してください」——等于把「请选择」当答案提交。4 份真实样本一致(真值从
id=200 起)。同时 /api/item_detail 完全不返回 options,调用方即使想显式指定
choice 也无从知道合法取值。

- purchase_contract.py:新增 ItemOption / ItemOptionValue 与 parse_options /
  auto_choice_for / format_choice。占位判定以结构为主(value_id == 0),日文
  文案仅作兜底。放 shared 是因为「接口声明的合法取值」与「下单实际提交的值」
  必须同源,否则两边各判一次迟早再次分叉
- item.py / scrape.py:ItemDetailData 增 options、has_required_options、
  unfillable_required_options;只解析一次,两个派生结果都取自同一份结果
- site_interact.py:auto_choice_for 取第一个非占位候选;必填项填不出值时
  报错点名是哪些选项,让调用方知道该在 intent.choice 里补什么
- auto_choice_for 只自动填必填项:非必填项要不要选是业务决定,不是我们该替
  调用方做的选择
- README / docs:补 options[] → intent.choice、variants[] → intent.variant_id
  的对照,修掉 order-gateway 示例里已不存在的 "options": {} 字段

真账号验证(scripts/probe_option_choice.py,仅加购不结算不支付):两个商品
提交 確認した / 了解致しました。均被站点接受,购物车 count=2,跑完清空恢复
原状。探针刻意走生产的 add_to_cart_payload 并从其日志截获实际 payload——
probe_purchase_block_v2.py 自己抄了一遍字段构造,与生产代码同错,正是这个
bug 当初藏住的原因。

未覆盖:这两家店铺本身不校验该选项(旧的占位值当年也被收下),所以只证明新值
走得通、语义上才是真答案,证明不了旧值会被拒;必填自由文本项(
unfillable_required_options)无真实样本,仅离线测试覆盖。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-28 16:07:33 +08:00
co-authored by Claude Opus 5
parent 8381896eeb
commit b577d3ac8d
9 changed files with 731 additions and 26 deletions
+89
View File
@@ -180,6 +180,95 @@ def test_extract_fields_required_options_auto_picks_first_value():
assert fields["form_fields"]["choice"] == "サイズ:S"
def test_extract_fields_required_options_skips_placeholder_value():
"""回归:必填 select 的 values[0] 是「選択してください」占位项,不能当答案提交
真实商品页(.probe/checkout/03-add-fail-2.html 等 3 份样本)里必填 select 的
第一个候选恒为 id=0 的占位项,真实取值从 id=200 起。旧实现直接取 values[0],
等于把「請選擇」填进 choice 提交上去。
"""
state = {
"item": {"itemId": 1, "variantId": "v"},
"purchase": {
"sku": {"inventoryType": "single"},
"sellType": {"normalPurchase": {"basketDomain": "https://x/add", "purchaseCondition": "enabled"}},
"information": {
"options": [
{
"id": 100,
"name": "この商品は「トライタン製/プラスチック」です",
"type": "select",
"isRequired": True,
"values": [
{"id": 0, "name": "選択してください"},
{"id": 200, "name": "確認した"},
],
}
]
},
},
"shop": {"information": {"shopId": 1}},
}
fields = _extract_purchase_fields(state, intent_override={})
assert fields["has_required_options"] is True
assert fields["form_fields"]["choice"] == "この商品は「トライタン製/プラスチック」です:確認した"
assert fields["unfillable_required_options"] == []
def test_extract_fields_required_free_text_option_cannot_be_auto_filled():
"""必填自由文本项(type=text,无候选值)自动填不了,要点名报出来"""
state = {
"item": {"itemId": 1, "variantId": "v"},
"purchase": {
"sku": {"inventoryType": "single"},
"sellType": {"normalPurchase": {"basketDomain": "https://x/add", "purchaseCondition": "enabled"}},
"information": {
"options": [
{"id": 101, "name": "【お名前】", "type": "text", "isRequired": True},
]
},
},
"shop": {"information": {"shopId": 1}},
}
fields = _extract_purchase_fields(state, intent_override={})
assert fields["has_required_options"] is True
# 填不出来就不填,也不拿占位/空值凑数
assert "choice" not in fields["form_fields"]
assert fields["unfillable_required_options"] == ["【お名前】"]
def test_extract_fields_skips_optional_options_when_auto_filling():
"""非必填项不替上游做业务决定(如「置き配を希望する」),只自动填必填项"""
state = {
"item": {"itemId": 1, "variantId": "v"},
"purchase": {
"sku": {"inventoryType": "single"},
"sellType": {"normalPurchase": {"basketDomain": "https://x/add", "purchaseCondition": "enabled"}},
"information": {
"options": [
{
"id": 100,
"name": "必須確認",
"type": "select",
"isRequired": True,
"values": [{"id": 0, "name": "選択してください"}, {"id": 200, "name": "了解"}],
},
{
"id": 101,
"name": "「置き配」希望について",
"type": "select",
"isRequired": False,
"values": [{"id": 200, "name": "置き配を希望しない"}],
},
]
},
},
"shop": {"information": {"shopId": 1}},
}
fields = _extract_purchase_fields(state, intent_override={})
assert fields["form_fields"]["choice"] == "必須確認:了解"
def test_extract_fields_intent_choice_override_accepts_list_and_str():
state = {
"item": {"itemId": 1, "variantId": "v"},