This commit is contained in:
2026-09-24 14:27:43 +07:00
parent e9d7c6d391
commit 79b70bed12
6 changed files with 83 additions and 11 deletions

View File

@@ -230,7 +230,8 @@ def test_extract_fixed_schema_end_to_end_and_callback(tmp_path):
thread.start()
try:
url = f"http://127.0.0.1:{server.server_port}/v1/chat/completions"
settings = replace(settings_for(tmp_path), ocr_url=url, kie_enabled=True, kie_url=url)
settings = replace(settings_for(tmp_path), ocr_url=url, kie_enabled=True, kie_url=url,
kie_max_tokens=4096)
source = tmp_path / "quyet-dinh.pdf"
make_pdf(source, count=2)
app = create_app(settings)
@@ -246,6 +247,7 @@ def test_extract_fixed_schema_end_to_end_and_callback(tmp_path):
assert len(kie_requests) == 1
assert len(kie_requests[0]["messages"][0]["content"]) == 3
assert kie_requests[0]["model"] == settings.kie_model
assert kie_requests[0]["max_tokens"] == 4096
extraction = client.get(f"/jobs/{job_id}/extraction").json()
assert extraction["status"] == "done"
@@ -359,6 +361,7 @@ def test_rejects_truncated_plain_text_and_invalid_bbox():
def test_real_http_ocr_and_callback(tmp_path):
callbacks = []
auth_headers = []
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
@@ -370,8 +373,20 @@ def test_real_http_ocr_and_callback(tmp_path):
'<div data-label="Paragraph" data-bbox="1 1 500 500">Test</div>'}}]}
else:
callbacks.append(body)
answer = {"ok": True}
auth_headers.append(self.headers.get("Authorization"))
answer = {"detail": "DAG Run already exists"}
data = json.dumps(answer).encode()
self.send_response(200 if self.path == "/v1/chat/completions" else 409)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_GET(self):
assert self.path == "/api/v1/dags/test/dagRuns/callback_test_001"
assert self.headers.get("Authorization") == auth_headers[0]
data = json.dumps({"dag_run_id": callbacks[0]["dag_run_id"],
"conf": callbacks[0]["conf"]}).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
@@ -386,18 +401,21 @@ def test_real_http_ocr_and_callback(tmp_path):
thread.start()
try:
settings = replace(settings_for(tmp_path),
ocr_url=f"http://127.0.0.1:{server.server_port}/v1/chat/completions")
ocr_url=f"http://127.0.0.1:{server.server_port}/v1/chat/completions",
callback_basic_user="callback_handler",
callback_basic_password="callback_handler")
source = tmp_path / "input.pdf"
make_pdf(source, count=1)
app = create_app(settings)
with TestClient(app) as client:
reply = client.post("/process", json={"request_id": str(uuid4()), "file_path": str(source),
"dag_run_id": "callback_test_001",
"callback_url": f"http://127.0.0.1:{server.server_port}/callback"})
"callback_url": f"http://127.0.0.1:{server.server_port}/api/v1/dags/test/dagRuns"})
assert reply.status_code == 202
drain(Worker(settings), settings)
assert client.get(f"/jobs/{reply.json()['job_id']}").json()["callback_state"] == "delivered"
assert callbacks[0]["dag_run_id"] == "callback_test_001"
assert auth_headers == ["Basic " + base64.b64encode(b"callback_handler:callback_handler").decode("ascii")]
assert set(callbacks[0]) == {"dag_run_id", "conf"}
conf = callbacks[0]["conf"]
assert conf["request_id"] == reply.json()["request_id"]
@@ -410,6 +428,16 @@ def test_real_http_ocr_and_callback(tmp_path):
assert images[0]["page_no"] == 1
assert images[0]["media_type"] == "image/png"
assert base64.b64decode(images[0]["data_base64"]).startswith(b"\x89PNG\r\n\x1a\n")
# Cùng dag_run_id nhưng job khác phải giữ lỗi 409, không đánh dấu delivered.
second = client.post("/process", json={"request_id": str(uuid4()), "file_path": str(source),
"dag_run_id": "callback_test_001",
"callback_url": f"http://127.0.0.1:{server.server_port}/api/v1/dags/test/dagRuns"})
assert second.status_code == 202
drain(Worker(settings), settings)
second_status = client.get(f"/jobs/{second.json()['job_id']}").json()
assert second_status["callback_state"] == "pending"
assert "409" in second_status["callback_error"]
finally:
server.shutdown()
server.server_close()