auto-form / tests /test_ai_clean.py
Menixis's picture
Claude Fable 5
fix: letter lost the duration sentence when AI paraphrasing dropped the 'ภายใน X วัน' signal — (1) build_letter falls back to the raw TOR clause when cleaning loses it, (2) generate_body rejects AI bodies missing the supplied day-number
b9e493a
Raw History Blame Contribute Delete
7.14 kB
# -*- coding: utf-8 -*-
"""ai_clean — deterministic paths only (AI_BACKEND=off via conftest)."""
from datetime import date
import ai_clean
TOR_CLAUSE = ("7. การส่งมอบงาน\n"
"ผู้รับจ้างต้องส่งมอบงานภายใน 90 (เก้าสิบ) วัน นับถัดจากวันที่ได้รับหนังสือ"
"แจ้งให้เริ่มปฏิบัติงาน โดยไม่นับรวมวันหยุดราชการ ทั้งนี้ ผู้รับจ้างต้องแจ้งล่วงหน้า")
FIELDS = {
"procurement_type": "งานจ้างบริการ",
"work": "งานจ้างเหมาบำรุงรักษาระบบท่อ",
"vendor": "บริษัท ทดสอบ จำกัด",
"unit": "ส่วนบำรุงรักษาระบบท่อ",
"start_date": "20 กรกฎาคม 2569",
"days": 90,
"delivery_clause": TOR_CLAUSE,
}
# --- parse_nguad -------------------------------------------------------------
def test_parse_nguad_empty_is_single():
assert ai_clean.parse_nguad("") == [{"label": "งวดเดียว", "number": None}]
assert ai_clean.parse_nguad(None) == [{"label": "งวดเดียว", "number": None}]
def test_parse_nguad_multi_dedup_sorted():
clause = "งวดที่ 2 ส่งรายงาน ... งวดที่ 1 ส่งแผน ... งวดที่ 2 (ซ้ำ)"
assert ai_clean.parse_nguad(clause) == [
{"label": "งวดที่ 1", "number": 1},
{"label": "งวดที่ 2", "number": 2},
]
def test_parse_nguad_unstructured_is_single():
assert ai_clean.parse_nguad("ส่งมอบภายใน 30 วัน") == \
[{"label": "งวดเดียว", "number": None}]
# --- _duration_from_tor ------------------------------------------------------
def test_duration_lifted_from_tor():
out = ai_clean._duration_from_tor(TOR_CLAUSE, None)
assert out.startswith("โดยกำหนดให้แล้วเสร็จภายใน 90 (เก้าสิบ) วัน")
assert "วันหยุดราชการ" in out
assert "ทั้งนี้" not in out # stops before the ทั้งนี้ tail
def test_duration_sla_fallback():
out = ai_clean._duration_from_tor("ส่งมอบ ณ คลังผู้ว่าจ้าง", "30")
assert out == ("โดยกำหนดให้แล้วเสร็จภายใน 30 วัน "
"นับถัดจาก วันที่ ปตท. มีหนังสือแจ้งให้ปฏิบัติงาน")
def test_duration_empty_when_no_source():
assert ai_clean._duration_from_tor("", "") == ""
# --- compose_body / generate_body -------------------------------------------
def test_compose_body_format():
body = ai_clean.compose_body(FIELDS)
paras = body.split("\n\n")
assert len(paras) == 2
assert paras[0].startswith("ตามที่ ส่วนบำรุงรักษาระบบท่อ บริษัท ปตท. จำกัด (มหาชน)")
assert "จัดจ้าง" in paras[0] # งานจ้าง → จัดจ้าง
assert "จึงขอแจ้งให้" in paras[1]
assert paras[1].endswith("นับถัดจากวันที่ 20 กรกฎาคม 2569 เป็นต้นไป")
def test_compose_body_buy_verb():
body = ai_clean.compose_body({**FIELDS, "procurement_type": "งานซื้อ"})
assert "จัดซื้อ" in body and "จัดจ้าง" not in body
def test_generate_body_off_backend_is_deterministic():
text, used_ai = ai_clean.generate_body(FIELDS)
assert used_ai is False
assert text == ai_clean.compose_body(FIELDS)
def test_clean_delivery_off_backend_uses_raw_fallback():
text, used_ai = ai_clean.clean_delivery(TOR_CLAUSE)
assert used_ai is False
assert text == ai_clean._raw_fallback(TOR_CLAUSE)
assert ai_clean.clean_delivery("") == ("", False)
# --- thai_date / _raw_fallback ----------------------------------------------
def test_thai_date():
assert ai_clean.thai_date(date(2026, 2, 27)) == "27 กุมภาพันธ์ 2569"
assert ai_clean.thai_date(None) == ""
def test_generate_body_rejects_ai_output_missing_duration(monkeypatch):
"""If a duration was supplied, an AI body that drops its day-number must be
rejected in favour of the deterministic composer (seen live 2026-07-16)."""
monkeypatch.setenv("AI_BACKEND", "powerautomate")
monkeypatch.setenv("AIBUILDER_FLOW_URL", "https://example.invalid/flow")
def fake_call(url, prompt, timeout=90):
# anchors present, but the 90-day duration silently dropped
return "ตามที่ ... ให้ดำเนินการ งาน ... จึงขอแจ้งให้ ... เป็นต้นไป"
monkeypatch.setattr(ai_clean, "_call_powerautomate", fake_call)
text, used_ai = ai_clean.generate_body(FIELDS, retries=1)
assert used_ai is False # off-format → deterministic fallback
assert text == ai_clean.compose_body(FIELDS)
assert "ภายใน 90 (เก้าสิบ) วัน" in text
def test_generate_body_accepts_ai_output_with_duration(monkeypatch):
monkeypatch.setenv("AI_BACKEND", "powerautomate")
monkeypatch.setenv("AIBUILDER_FLOW_URL", "https://example.invalid/flow")
good = ("ตามที่ บริษัท ปตท. จำกัด (มหาชน) ได้มีข้อตกลง ให้ดำเนินการ งาน "
"โดยกำหนดให้แล้วเสร็จภายใน 90 วัน\n\nจึงขอแจ้งให้ เริ่มเข้าดำเนินการ")
monkeypatch.setattr(ai_clean, "_call_powerautomate",
lambda url, prompt, timeout=90: good)
text, used_ai = ai_clean.generate_body(FIELDS, retries=1)
assert used_ai is True and text == good
def test_raw_fallback_drops_heading_and_filters_nguad():
clause = ("การส่งมอบงาน\n"
"แบ่งการส่งมอบเป็น 2 งวด\n"
"งวดที่ 1 ส่งแผนงานภายใน 15 วัน\n"
"งวดที่ 2 ส่งรายงานฉบับสมบูรณ์ภายใน 90 วัน")
out = ai_clean._raw_fallback(clause, "งวดที่ 2")
assert "การส่งมอบงาน" not in out.split()[0:1] # heading line dropped
assert "งวดที่ 2" in out
assert "งวดที่ 1" not in out
assert "แบ่งการส่งมอบเป็น 2 งวด" in out # lead-in kept