from fam_core.chat_handler.chat_handler import _format_events def test_multiple_persons_and_attention(): rows = [{ "ts": "2026-08-21 16:11:25", "camera_name": "客厅", "description": "两人在客厅玩耍", "person_list_json": '["汤圆", "爷爷"]', "is_attention_event": True, }] out = _format_events(rows) assert out == "[2026-08-21 16:11 客厅] 汤圆,爷爷: 两人在客厅玩耍 [关注事件]" def test_no_person_defaults_to_wu_ren(): rows = [{ "ts": "2026-08-21 16:11:25", "camera_name": "客厅", "description": "空房间", "person_list_json": '[]', "is_attention_event": False, }] out = _format_events(rows) assert "无人:" in out assert "[关注事件]" not in out def test_person_list_already_decoded_list(): """pymysql 某些驱动/字段类型可能已经把 JSON 解码成 list,不是字符串。""" rows = [{ "ts": "2026-08-21 16:11:25", "camera_name": "客厅", "description": "在客厅", "person_list_json": ["汤圆"], "is_attention_event": False, }] out = _format_events(rows) assert "汤圆:" in out def test_malformed_person_list_json_degrades_gracefully(): rows = [{ "ts": "2026-08-21 16:11:25", "camera_name": "客厅", "description": "在客厅", "person_list_json": "not valid json", "is_attention_event": False, }] out = _format_events(rows) assert "无人:" in out def test_multiple_rows_joined_by_newline(): rows = [ {"ts": "2026-08-21 16:11:25", "camera_name": "客厅", "description": "第一条", "person_list_json": '["汤圆"]', "is_attention_event": False}, {"ts": "2026-08-21 16:12:00", "camera_name": "客厅", "description": "第二条", "person_list_json": '["汤圆"]', "is_attention_event": False}, ] out = _format_events(rows) assert len(out.split("\n")) == 2