2026-06-06 15:44:00 +08:00
|
|
|
import base64
|
|
|
|
|
import importlib
|
2026-04-07 23:38:23 +08:00
|
|
|
import os
|
|
|
|
|
import sys
|
2026-06-06 15:44:00 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2026-04-07 23:38:23 +08:00
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
os.environ["JOB_BACKEND"] = "memory"
|
|
|
|
|
|
|
|
|
|
CURRENT_DIR = Path(__file__).resolve().parent
|
|
|
|
|
BACKEND_DIR = CURRENT_DIR.parent
|
|
|
|
|
if str(BACKEND_DIR) not in sys.path:
|
|
|
|
|
sys.path.insert(0, str(BACKEND_DIR))
|
2026-04-07 23:38:23 +08:00
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
import job_handlers # type: ignore
|
|
|
|
|
import job_system # type: ignore
|
2026-05-24 23:30:32 +08:00
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
main = importlib.import_module("main")
|
2026-04-07 23:38:23 +08:00
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
HEADERS = {"X-API-Key": main.API_KEY}
|
2026-04-07 23:38:23 +08:00
|
|
|
|
|
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
def setup_function():
|
|
|
|
|
job_system.reset_job_manager()
|
|
|
|
|
main._handlers_registered = False
|
2026-04-07 23:38:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DummyRequest:
|
|
|
|
|
def __init__(self, host=None, headers=None):
|
|
|
|
|
class Client:
|
|
|
|
|
pass
|
|
|
|
|
self.client = Client() if host is not None else None
|
|
|
|
|
if self.client is not None:
|
|
|
|
|
self.client.host = host
|
|
|
|
|
self.headers = headers or {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_preview_short_text():
|
|
|
|
|
assert main._preview("Hello") == "Hello"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_preview_long_text_truncated():
|
|
|
|
|
long_text = "a" * 100
|
|
|
|
|
assert main._preview(long_text) == long_text[:80] + "..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sanitize_markdown_strips_image_markdown():
|
2026-06-06 15:44:00 +08:00
|
|
|
assert "" not in main._sanitize_converted_markdown("text ")
|
2026-04-07 23:38:23 +08:00
|
|
|
|
|
|
|
|
|
2026-05-31 16:38:10 +08:00
|
|
|
def test_sanitize_inline_completion_strips_prefill():
|
2026-06-06 15:44:00 +08:00
|
|
|
assert main.sanitize_inline_completion_content("系统非常适合写作", prefill="系统") == "非常适合写作"
|
2026-04-07 23:38:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_client_ip_header_overrides_host():
|
|
|
|
|
req = DummyRequest(host="1.2.3.4", headers={"X-Client-IP": "5.6.7.8"})
|
|
|
|
|
assert main.get_client_ip(req) == "5.6.7.8"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_completions_wrong_api_key_returns_401():
|
2026-06-06 15:44:00 +08:00
|
|
|
with TestClient(main.app) as client:
|
|
|
|
|
resp = client.post("/v1/completions", json={
|
|
|
|
|
"prefix": "hello", "suffix": "", "languageId": "markdown",
|
|
|
|
|
"model_thinking": "low", "privacy_mode": True,
|
|
|
|
|
})
|
2026-04-07 23:38:23 +08:00
|
|
|
assert resp.status_code == 401
|
|
|
|
|
|
|
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
def test_post_completions_returns_sse_done(monkeypatch):
|
2026-04-07 23:38:23 +08:00
|
|
|
async def fake_call(*args, **kwargs):
|
2026-06-06 15:44:00 +08:00
|
|
|
return {"content": "系统done", "think": ""}
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(job_handlers, "call_ollama", fake_call)
|
|
|
|
|
with TestClient(main.app) as client:
|
|
|
|
|
with client.stream("POST", "/v1/completions", headers=HEADERS, json={
|
|
|
|
|
"prefix": "hello", "suffix": "", "languageId": "markdown",
|
|
|
|
|
"model_thinking": "low", "privacy_mode": True,
|
|
|
|
|
}) as resp:
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
body = "".join(resp.iter_text())
|
2026-05-31 16:38:10 +08:00
|
|
|
assert "event: queued" in body
|
|
|
|
|
assert "event: started" in body
|
2026-06-06 15:44:00 +08:00
|
|
|
assert "event: result" in body
|
2026-05-24 23:30:32 +08:00
|
|
|
assert "event: done" in body
|
|
|
|
|
|
|
|
|
|
|
2026-04-07 23:38:23 +08:00
|
|
|
def test_post_ocr_mocked(monkeypatch):
|
|
|
|
|
async def fake_ocr(*args, **kwargs):
|
|
|
|
|
return "OCR result text"
|
|
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
monkeypatch.setattr(job_handlers, "call_vlm_ocr", fake_ocr)
|
2026-04-07 23:38:23 +08:00
|
|
|
img_b64 = base64.b64encode(b"pretend image data").decode()
|
2026-06-06 15:44:00 +08:00
|
|
|
with TestClient(main.app) as client:
|
|
|
|
|
with client.stream("POST", "/v1/ocr", headers=HEADERS, json={
|
|
|
|
|
"image": img_b64, "filename": "test.jpg", "language": "auto",
|
|
|
|
|
}) as resp:
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
body = "".join(resp.iter_text())
|
|
|
|
|
assert "OCR result text" in body
|
2026-04-07 23:38:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_convert_txt_returns_markdown():
|
|
|
|
|
content = base64.b64encode(b"hello world").decode()
|
2026-06-06 15:44:00 +08:00
|
|
|
with TestClient(main.app) as client:
|
|
|
|
|
with client.stream("POST", "/v1/convert", headers=HEADERS, json={
|
|
|
|
|
"file": content, "filename": "sample.txt",
|
|
|
|
|
}) as resp:
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
body = "".join(resp.iter_text())
|
|
|
|
|
assert "hello world" in body
|
2026-04-07 23:38:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_convert_unsupported_extension_returns_500():
|
|
|
|
|
content = base64.b64encode(b"data").decode()
|
2026-06-06 15:44:00 +08:00
|
|
|
with TestClient(main.app) as client:
|
|
|
|
|
resp = client.post("/v1/convert", headers=HEADERS, json={
|
|
|
|
|
"file": content, "filename": "sample.xlsx",
|
|
|
|
|
})
|
2026-04-07 23:38:23 +08:00
|
|
|
assert resp.status_code == 500
|
|
|
|
|
assert "仅支持" in resp.json()["error"]
|