File size: 6,375 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Workspace acceptance checks against real SQL rows and attachment bytes."""
from __future__ import annotations
import hashlib
import os
import tempfile
import unittest
from pathlib import Path
from sqlalchemy.engine import URL
from workspace_service import WorkspaceService
from workspace_service.errors import WorkspaceError

A={"tenant_id":"demo-tenant","user_id":"u-lin","trace_id":"test-a"}
B={"tenant_id":"demo-tenant","user_id":"u-zhou","trace_id":"test-b"}

class WorkspaceContracts(unittest.TestCase):
    def setUp(self):
        root=Path(os.getenv("WORKOS_TEST_TMP","D:/codex/cache/workos-workspace-tests")).resolve()
        root.mkdir(parents=True,exist_ok=True)
        self.temp=tempfile.TemporaryDirectory(dir=root)
        self.path=Path(self.temp.name)
        assert self.path.resolve().is_relative_to(root)
        self.url=URL.create("sqlite+pysqlite",database=str(self.path/"workspace.db"))
        self.kw={"database_url":self.url,"file_root":self.path/"files",
                 "skill_lookup":lambda user,sid:{"version":1} if sid=="document-draft" else self.fail("unknown skill")}
        self.ws=WorkspaceService(**self.kw)
    def tearDown(self):
        self.ws.engine.dispose()
        self.temp.cleanup()
    def err(self,status,call):
        with self.assertRaises(WorkspaceError) as raised:call()
        self.assertEqual(raised.exception.status,status)
    def test_home_matters_isolation_restart_and_idempotency(self):
        self.assertEqual(self.ws.get_or_create_home(A),self.ws.get_or_create_home(A))
        self.assertNotEqual(self.ws.get_or_create_home(A),self.ws.get_or_create_home(B))
        matter,_=self.ws.create(A,"培训通知","key-create-001")
        same,replayed=self.ws.create(A,"培训通知","key-create-001")
        self.assertTrue(replayed)
        self.assertEqual(matter,same)
        self.err(409,lambda:self.ws.create(A,"另一事项","key-create-001"))
        self.err(404,lambda:self.ws.get(B,matter["workspace_id"]))
        self.assertEqual(self.ws.list(B)["items"],[])
        renamed=self.ws.rename(A,matter["workspace_id"],"培训通知修订",1)
        self.assertEqual(renamed["version"],2)
        self.err(409,lambda:self.ws.rename(A,matter["workspace_id"],"旧版",1))
        second=WorkspaceService(**self.kw)
        self.assertEqual(second.get(A,matter["workspace_id"])["title"],"培训通知修订")
        second.engine.dispose()
    def test_messages_pagination_and_skill_binding(self):
        w,_=self.ws.create(A,"事项","key-create-002");wid=w["workspace_id"]
        for i in range(5):
            self.ws.message(A,wid,f"消息{i}",f"message-key-{i:03}")
        first=self.ws.messages(A,wid,limit=2)
        self.assertEqual(len(first["items"]),2)
        second=self.ws.messages(A,wid,cursor=int(first["next_cursor"]),limit=2)
        self.assertEqual(len(second["items"]),2)
        all_ids=[x["message_id"] for x in first["items"]+second["items"]]
        self.assertEqual(len(set(all_ids)),4)
        repeat,replayed=self.ws.message(A,wid,"消息0","message-key-000")
        self.assertTrue(replayed)
        self.assertEqual(len(self.ws.messages(A,wid)["items"]),5)
        self.err(404,lambda:self.ws.messages(B,wid))
        bound=self.ws.bind_skills(A,wid,["document-draft"],1)
        self.assertEqual(bound["version"],2)
        self.assertEqual(self.ws.get_skill_ids(A,wid),["document-draft"])
        self.err(409,lambda:self.ws.bind_skills(A,wid,[],1))
    def test_attachment_bytes_limits_and_outbox(self):
        w,_=self.ws.create(A,"资料","key-create-003");wid=w["workspace_id"]
        content=b"official document"
        attachment,_=self.ws.attachment(A,wid,"incoming.txt",content,"key-upload-000")
        self.assertEqual(self.ws.attachment_content(A,attachment["attachment_id"]),content)
        self.assertEqual(attachment["sha256"],hashlib.sha256(content).hexdigest())
        self.err(404,lambda:self.ws.attachment_content(B,attachment["attachment_id"]))
        self.err(422,lambda:self.ws.attachment(A,wid,"fake.pdf",content,"key-upload-bad"))
        for i in range(1,5):self.ws.attachment(A,wid,f"doc{i}.txt",content,f"key-upload-{i:03}")
        self.err(409,lambda:self.ws.attachment(A,wid,"doc5.txt",content,"key-upload-005"))
        seen=[]
        def consumer(e):
            seen.append(e["event_type"])
            if e["event_type"]=="document.ingest.requested":
                self.ws.apply_ingest_result(A,e["payload"]["attachment_id"],"ready","stub-doc")
        self.assertEqual(self.ws.dispatch_pending(consumer,event_types={"document.ingest.requested"}),5)
        self.assertIn("document.ingest.requested",seen)
        self.assertEqual(self.ws.attachments(A,wid)["items"][0]["parse_status"],"ready")
        self.assertTrue(any(e["event_type"]=="workspace.created" for e in self.ws.pending_events()))
        self.assertEqual(self.ws.dispatch_pending(consumer,event_types={"document.ingest.requested"}),0)
    def test_artifact_versions_approval_and_owner(self):
        w,_=self.ws.create(A,"草稿","key-create-004");wid=w["workspace_id"]
        draft,_=self.ws.create_draft(A,wid,"run-1","第一版",["cite-1"],"key-draft-001")
        edited=self.ws.edit(A,draft["artifact_id"],"第二版",1)
        self.assertEqual(edited["version"],2)
        self.assertEqual(self.ws.artifact_version_content(A,draft["artifact_id"],1),"第一版")
        self.err(409,lambda:self.ws.edit(A,draft["artifact_id"],"旧稿覆盖",1))
        self.err(409,lambda:self.ws.approve(A,draft["artifact_id"],1,draft["content_hash"]))
        approved=self.ws.approve(A,draft["artifact_id"],2,edited["content_hash"])
        self.assertEqual(approved["approval_status"],"approved")
        third=self.ws.edit(A,draft["artifact_id"],"第三版",2)
        self.assertEqual(third["approval_status"],"unconfirmed")
        self.err(409,lambda:self.ws.approved_version(A,draft["artifact_id"],3,third["content_hash"]))
        self.assertEqual(self.ws.approved_version(A,draft["artifact_id"],2,edited["content_hash"])["content"],"第二版")
        self.err(404,lambda:self.ws.artifact(B,draft["artifact_id"]))
        self.err(404,lambda:self.ws.artifact_version_content(B,draft["artifact_id"],1))
if __name__=="__main__":unittest.main()