import pytest

from pallet.pallet_description_output import decode_output
from pallet.pallet_description_build import description_draft


def test_partial_structure_recovery_does_not_invent_observation_or_claim_valid_JSON():
    raw='{"支腿":2,"材质":"金属","横梁":1,"结构":"两支腿金属托盘","观察":支腿遮挡"}'
    p=decode_output(raw)
    assert p['valid'] is False and p['fields'] is None and p['structure_recovered'] and p['observation_withheld']
    assert p['recovered_fields']=={'支腿':2,'材质':'金属','横梁':1,'结构':'两支腿金属托盘','观察':None}
    assert p['raw']==raw
    d=description_draft(p)
    assert d['text']=='两支腿金属托盘' and d['legs']==2 and d['observation_state']=='unreviewed'
    assert '需人工填写' in d['observation'] and '支腿遮挡' not in d['observation']


@pytest.mark.parametrize('raw',[
    '两条腿，观察：遮挡',
    '{"支腿":9,"材质":"金属","横梁":1,"结构":"托盘","观察":坏值}',
    '{"支腿":2,"材质":"金属","横梁":true,"结构":"托盘","观察":坏值}',
    '{"支腿":2,"材质":"金属","横梁":1,"结构":4,"观察":坏值}',
    '{"支腿":2,"材质":"钢","横梁":1,"结构":"托盘","观察":坏值}',
    '{"支腿":2,"材质":"金属","横梁":1,"结构":"托盘","额外":"字段","观察":坏值}',
])
def test_invalid_structure_not_replaced_with_class_templates(raw):
    p=decode_output(raw)
    assert not p['valid'] and not p['structure_recovered'] and p['recovered_fields'] is None
    assert description_draft(p)['text']=='无法判断：模型结构输出未通过，需人工填写。'


def test_strict_valid_output_retained_without_recovery():
    p=decode_output('{"支腿":2,"材质":"塑料","横梁":1,"结构":"托盘","观察":"无法判断"}')
    assert p['valid'] and not p['structure_recovered'] and not p['observation_withheld']
    assert p['recovered_fields'] is None and description_draft(p)['observation']=='无法判断'
