File size: 4,374 Bytes
daa4d56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Public API integration and ingestion recovery against a real SQLite database."""
import os
import sys
import tempfile
import unittest
from unittest.mock import patch
from pathlib import Path
from fastapi.testclient import TestClient

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "APIGateway"))
from app.main import create_app
from app.stubs import Providers


class GatewayKnowledgeTests(unittest.TestCase):
    def setUp(self):
        self.tmp = tempfile.TemporaryDirectory()
        self.env = patch.dict(os.environ, {"WORKOS_WORKSPACE_DATA_DIR": self.tmp.name})
        self.env.start()
        self.providers = Providers()
        self.client = TestClient(create_app(self.providers))
        self.client.__enter__()
        self.a, self.b = self.login("lin"), self.login("zhou")
        self.wa = self.create(self.a)
        self.wb = self.create(self.b)

    def tearDown(self):
        self.client.__exit__(None, None, None)
        self.providers.workspace.engine.dispose()
        self.env.stop()
        self.tmp.cleanup()

    def login(self, user):
        return {"Authorization": "Bearer " + self.client.post("/api/v1/auth/login",
            json={"username": user, "password": "demo123"}).json()["access_token"]}

    def create(self, headers):
        r = self.client.post("/api/v1/workspaces", headers={**headers, "Idempotency-Key": "new-matter-001"}, json={"title": "测试事项"})
        self.assertEqual(r.status_code, 201)
        return r.json()["workspace_id"]

    def upload(self):
        r = self.client.post(f"/api/v1/workspaces/{self.wa}/attachments", headers={**self.a, "Idempotency-Key": "upload-001"},
            files={"file": ("test.md", "HTTP_PRIVATE_TEST 来文期限".encode(), "text/markdown")})
        self.assertEqual(r.status_code, 201)
        self.assertEqual(r.json()["parse_status"], "ready")

    def test_private_search_original_and_document_status(self):
        self.upload()
        payload = {"workspace_id": self.wa, "query": "HTTP_PRIVATE_TEST", "top_k": 3}
        result = self.client.post("/api/v1/knowledge/search", headers=self.a, json=payload)
        self.assertEqual(result.status_code, 200)
        cid = result.json()["items"][0]["citation_id"]
        path = "/api/v1/citations/" + cid
        self.assertEqual(self.client.get(path, headers=self.a).status_code, 404)
        self.assertEqual(self.client.get(path, headers=self.a, params={"workspace_id": self.wa}).status_code, 200)
        self.assertEqual(self.client.get(path, headers=self.b, params={"workspace_id": self.wb}).status_code, 404)
        self.assertEqual(self.client.post("/api/v1/knowledge/search", headers=self.b,
            json={**payload, "workspace_id": self.wb}).json()["items"], [])
        docs = self.client.get(f"/api/v1/workspaces/{self.wa}/knowledge-documents", headers=self.a)
        self.assertTrue(any(d["source_type"] == "attachment" and d["status"] == "ready" for d in docs.json()["items"]))
        self.assertEqual(self.client.get(f"/api/v1/workspaces/{self.wa}/knowledge-documents", headers=self.b).status_code, 404)

    def test_health_reports_real_knowledge_and_search_input_errors(self):
        deps = self.client.get("/health/ready", headers=self.a).json()["dependencies"]
        self.assertEqual(next(d for d in deps if d["module_id"] == "knowledge")["mode"], "sql-bm25-persistent")
        self.assertEqual(self.client.post("/api/v1/knowledge/search", headers=self.a,
            json={"workspace_id": self.wa, "query": " ", "top_k": 3}).status_code, 422)
        self.assertEqual(self.client.post("/api/v1/knowledge/search", headers=self.a,
            json={"workspace_id": self.wa, "query": "资料", "base_ids": ["forbidden"]}).status_code, 404)

    def test_startup_replays_unpublished_ingestion(self):
        user = {"tenant_id": "demo-tenant", "user_id": "u-lin"}
        self.providers.workspace.attachment(user, self.wa, "restart.md", b"STARTUP_REPLAY", "replay-001")
        self.client.__exit__(None, None, None)
        self.client.__enter__()
        result = self.client.post("/api/v1/knowledge/search", headers=self.a,
            json={"workspace_id": self.wa, "query": "STARTUP_REPLAY"})
        self.assertTrue(result.json()["items"])


if __name__ == "__main__":
    unittest.main()