import json import io import sqlite3 import subprocess import unittest from concurrent.futures import ThreadPoolExecutor from pathlib import Path from unittest.mock import patch, MagicMock from src.adapters.mcp.erp_knowledge import MCPClient, MCPServer, PROTOCOL from src.adapters.persistence.local_identity import LocalIdentity from src.apps.workbench.erp_flow import ERPFlow from src.modules.knowledge.erp_mcp import BusinessKnowledgeStore, TOOLS from src.modules.skill.domain.models import SkillError from src.tests.runtime.fixtures import workspace class BusinessMCPTests(unittest.TestCase): def setUp(self): self.folder = Path(self.enterContext(workspace())) self.identity = LocalIdentity(self.folder / 'identity.sqlite') self.identity.seed_projects() self.identity.seed_demo_users() with self.identity.connect() as db: users = {row['username']: row['id'] for row in db.execute('SELECT id,username FROM users')} self.alice = self.identity.actor(users['programmer']) self.bob = self.identity.actor(users['tester']) self.admin = self.identity.actor(users['manager']) self.path = self.folder / 'business.sqlite' self.flow = ERPFlow(self.identity, self.path) self.store = BusinessKnowledgeStore(self.path, self.identity, self.alice.user_id) def assert_code(self, code, operation): with self.assertRaises(SkillError) as raised: operation() self.assertEqual(raised.exception.code, code) def test_real_stdio_handshake_catalog_and_no_live_child(self): catalog = self.flow.catalog(self.alice) self.assertEqual(catalog['health']['status'], 'connected') self.assertEqual(catalog['health']['transport'], 'stdio') self.assertEqual(catalog['health']['protocol'], PROTOCOL) self.assertTrue(catalog['health']['process_exited']) self.assertTrue(catalog['health']['process_id']) self.assertIn('erp_receive_order', [t['name'] for t in catalog['tools']]) self.assertTrue(all(t['inputSchema']['additionalProperties'] is False for t in catalog['tools'])) def test_erp_receive_is_written_and_independently_read_through_mcp(self): asked = self.flow.ask(self.alice, 'PO-2026-1003 为什么未完成,如何完成收货?') action = asked['action'] self.assertEqual(action['parameters']['quantity'], 40) self.assertEqual(asked['records'][0]['received_qty'], 40) result = self.flow.apply(self.alice, action['entity_id'], 40, action['expected_version'], 'receipt-demo-0001') self.assertEqual(result['before']['status'], 'TO_RECEIVE') self.assertEqual(result['after']['status'], 'COMPLETED') self.assertEqual(result['readback'], result['after']) self.assertEqual(result['readback']['version'], 2) self.assertEqual([r['tool'] for r in result['tool_calls']], ['erp_receive_order', 'erp_order_get']) self.assertNotEqual(result['tool_calls'][0]['request_id'], result['tool_calls'][1]['request_id']) self.assertNotEqual(result['tool_calls'][0]['audit_id'], result['tool_calls'][1]['audit_id']) self.assertTrue(result['persisted']) self.assertIsNone(result['action']) reloaded = ERPFlow(self.identity, self.path).ask(self.alice, '查询PO-2026-1003') self.assertEqual(reloaded['orders'][0]['status'], 'COMPLETED') def test_order_query_latest_and_unfinished_preserve_unpaid_balance(self): result = self.flow.ask(self.alice, '查询最新完成的3笔采购订单,哪些仍未付款?') self.assertEqual([r['order_no'] for r in result['orders']], ['PO-2026-1006', 'PO-2026-1002', 'PO-2026-1001']) self.assertEqual(result['orders'][-1]['outstanding_cents'], 125000) self.assertIn('1250.00', result['diagnosis'][-1]['actions'][-1]) unfinished = self.flow.ask(self.alice, '查询最新未完成的订单') self.assertEqual({r['status'] for r in unfinished['orders']}, {'TO_BILL', 'TO_RECEIVE'}) def test_same_project_users_have_separate_business_records(self): self.flow.apply(self.alice, 'PO-2026-1003', 40, 1, 'receipt-own-0001') bob = self.flow.ask(self.bob, '查询PO-2026-1003') self.assertEqual(bob['orders'][0]['received_qty'], 40) self.assertEqual(bob['orders'][0]['version'], 1) self.assertEqual(bob['orders'][0]['status'], 'TO_RECEIVE') def test_receipt_idempotency_and_conflicting_reuse(self): first = self.flow.apply(self.alice, 'PO-2026-1003', 20, 1, 'receipt-key-0001') again = self.flow.apply(self.alice, 'PO-2026-1003', 20, 1, 'receipt-key-0001') self.assertTrue(again['replayed']) self.assertEqual(first['after']['received_qty'], again['readback']['received_qty']) self.assertEqual(again['readback']['version'], 2) self.assert_code('IDEMPOTENCY_CONFLICT', lambda: self.flow.apply(self.alice, 'PO-2026-1003', 21, 1, 'receipt-key-0001')) def test_receipt_version_and_quantity_boundaries_do_not_mutate(self): self.assert_code('VERSION_CONFLICT', lambda: self.flow.apply(self.alice, 'PO-2026-1003', 40, 2, 'version-conflict')) self.assert_code('RECEIPT_EXCEEDS_REMAINING', lambda: self.flow.apply(self.alice, 'PO-2026-1003', 41, 1, 'quantity-conflict')) for value in (True, 0, -1, 3.5, '40'): self.assert_code('TOOL_ARGUMENTS_INVALID', lambda: self.flow.apply(self.alice, 'PO-2026-1003', value, 1, 'bad-value-0001')) record = self.flow.ask(self.alice, '查询PO-2026-1003')['orders'][0] self.assertEqual(record['received_qty'], 40) self.assertEqual(record['version'], 1) def test_wms_receipt_and_issue_persist_without_negative_inventory(self): initial = self.flow.ask(self.alice, '查询库存并入库5件', 'wms') self.assertEqual(initial['records'][0]['quantity'], 20) result = self.flow.act(self.alice, 'wms', 'receive', 'SKU-DEMO-001', {'quantity': 5}, 1, 'stock-in-0001') self.assertEqual(result['readback']['quantity'], 25) self.assert_code('STOCK_INSUFFICIENT', lambda: self.flow.act(self.alice, 'wms', 'issue', 'SKU-DEMO-001', {'quantity': 26}, 2, 'stock-out-invalid')) issued = self.flow.act(self.alice, 'wms', 'issue', 'SKU-DEMO-001', {'quantity': 25}, 2, 'stock-out-0001') self.assertEqual(issued['readback']['quantity'], 0) self.assertEqual(self.flow.ask(self.bob, '查询库存', 'wms')['records'][0]['quantity'], 20) def test_equipment_closed_loop_and_illegal_transition(self): self.assert_code('WORK_ORDER_TRANSITION_INVALID', lambda: self.flow.act( self.alice, 'equipment', 'complete', 'WO-DEMO-001', {}, 1, 'work-skip-0001')) started = self.flow.act(self.alice, 'equipment', 'start', 'WO-DEMO-001', {}, 1, 'work-start-0001') self.assertEqual(started['readback']['status'], 'IN_PROGRESS') self.assertEqual(started['action']['kind'], 'complete') done = self.flow.act(self.alice, 'equipment', 'complete', 'WO-DEMO-001', {}, 2, 'work-complete-0001') self.assertEqual(done['readback']['status'], 'CLOSED') self.assertIsNone(done['action']) self.assertTrue(done['readback']['started_at']) self.assertTrue(done['readback']['completed_at']) self.assertEqual(self.flow.ask(self.bob, '查询工单', 'equipment')['records'][0]['status'], 'OPEN') def test_schema_rejects_user_scope_paths_sql_and_unknown_tools(self): for field in ('owner', 'project_id', 'sql', 'path', 'actor'): self.assert_code('TOOL_ARGUMENTS_INVALID', lambda: self.store.execute('erp_order_get', { 'order_no': 'PO-2026-1003', field: 'anything'})) self.assert_code('TOOL_NOT_ALLOWED', lambda: self.store.execute('exec', {'command': 'echo unsafe'})) self.assert_code('ACTION_INVALID', lambda: self.flow.act(self.alice, 'erp', 'pay', 'PO-2026-1003', {}, 1, 'not-permitted')) self.assert_code('TOOL_ARGUMENTS_INVALID', lambda: self.store.execute('erp_order_get', {'order_no': "' OR 1=1--"})) with sqlite3.connect(self.path) as db: denied = db.execute("SELECT COUNT(*) FROM business_audit WHERE outcome='rejected'").fetchone()[0] self.assertGreaterEqual(denied, 7) def test_membership_revocation_rechecked_inside_stdio_child(self): client = MCPClient(self.path, self.identity.path, self.alice.user_id) self.identity.set_member(self.admin, 'erp-demo', self.alice.user_id, False) self.assert_code('PROJECT_NOT_FOUND', lambda: client.exchange([('erp_order_get', {'order_no': 'PO-2026-1003'})])) self.assert_code('PROJECT_NOT_FOUND', lambda: self.flow.ask(self.alice, '查询订单')) self.assertTrue(self.flow.ask(self.alice, '查询库存', 'wms')['records']) def test_disabled_user_cannot_reuse_captured_actor(self): with self.identity.connect() as db: db.execute('UPDATE users SET enabled=0 WHERE id=?', (self.alice.user_id,)) self.assert_code('TOKEN_INVALID', lambda: self.flow.ask(self.alice, '查询订单')) def test_reset_increments_version_preserves_audit_and_user_scope(self): self.flow.apply(self.alice, 'PO-2026-1003', 40, 1, 'before-reset-0001') reset = self.flow.reset(self.alice, 'erp', 'reset-demo-0001') self.assertEqual(reset['orders'][0]['status'], 'TO_RECEIVE') self.assertEqual(reset['orders'][0]['version'], 3) self.assertEqual(self.flow.reset(self.alice, 'erp', 'reset-demo-0001')['orders'][0]['version'], 3) self.assert_code('VERSION_CONFLICT', lambda: self.flow.apply(self.alice, 'PO-2026-1003', 40, 1, 'stale-after-reset')) # Previously successful idempotency keys only replay the receipt; they never apply again. prior = self.flow.apply(self.alice, 'PO-2026-1003', 40, 1, 'before-reset-0001') self.assertTrue(prior['replayed']) self.assertTrue(prior['superseded']) self.assertEqual(prior['readback']['status'], 'TO_RECEIVE') self.assertEqual(prior['after']['status'], 'COMPLETED') self.assertIn('没有重复修改', prior['answer']) self.assertIn('后续操作', prior['answer']) with sqlite3.connect(self.path) as db: self.assertGreater(db.execute('SELECT COUNT(*) FROM business_audit').fetchone()[0], 5) self.assertEqual(self.flow.ask(self.bob, '查询PO-2026-1003')['records'][0]['version'], 1) def test_concurrent_mutations_only_one_expected_version_commits(self): self.store.execute('wms_stock_get', {'sku': 'SKU-DEMO-001'}) def attempt(key): try: return self.store.execute('wms_stock_adjust', {'sku': 'SKU-DEMO-001', 'action': 'receive', 'quantity': 5, 'expected_version': 1, 'request_key': key}) except SkillError as exc: return exc.code with ThreadPoolExecutor(max_workers=2) as pool: results = list(pool.map(attempt, ['concurrent-one', 'concurrent-two'])) self.assertEqual(sum(isinstance(r, dict) for r in results), 1) self.assertIn('VERSION_CONFLICT', results) self.assertEqual(self.store.execute('wms_stock_get', {'sku': 'SKU-DEMO-001'})['record']['quantity'], 25) def test_protocol_requires_initialize_and_does_not_leak_internal_errors(self): server = MCPServer(self.store) response = server.handle({'jsonrpc': '2.0', 'id': 1, 'method': 'tools/list'}) self.assertEqual(response['error']['code'], -32002) self.assertIn('error', server.handle({'jsonrpc': '2.0', 'id': 1, 'method': 'initialize', 'params': {'protocolVersion': 'wrong'}})) server.handle({'jsonrpc': '2.0', 'id': 1, 'method': 'initialize', 'params': {'protocolVersion': PROTOCOL}}) self.assertIsNone(server.handle({'jsonrpc': '2.0', 'method': 'notifications/initialized'})) with patch.object(self.store, 'execute', side_effect=RuntimeError('secret password unsafe path')): response = server.handle({'jsonrpc': '2.0', 'id': 2, 'method': 'tools/call', 'params': {'name': 'erp_order_get', 'arguments': {'order_no': 'PO-2026-1003'}}}) self.assertEqual(response['error']['message'], 'Internal MCP error') self.assertNotIn('secret', json.dumps(response)) def test_child_deadline_always_kills_and_waits(self): spawned, original = [], subprocess.Popen def launch(*args, **kwargs): child = original(*args, **kwargs) spawned.append(child) return child with patch('src.adapters.mcp.erp_knowledge.subprocess.Popen', side_effect=launch): self.assert_code('MCP_DEADLINE_EXCEEDED', lambda: MCPClient(self.path, self.identity.path, self.alice.user_id, timeout=0).exchange([])) self.assertEqual(len(spawned), 1) self.assertIsNotNone(spawned[0].poll()) def test_invalid_initialize_does_not_send_notification_or_tools(self): class ObservablePipe(io.BytesIO): def close(self): pass process = MagicMock() process.stdin = ObservablePipe() process.stdout = ObservablePipe((json.dumps({'jsonrpc': '2.0', 'id': 1, 'result': { 'protocolVersion': 'invalid', 'capabilities': {}}}) + '\n').encode()) process.poll.return_value = None with patch('src.adapters.mcp.erp_knowledge.subprocess.Popen', return_value=process): self.assert_code('MCP_PROTOCOL_INVALID', lambda: MCPClient(self.path, self.identity.path, self.alice.user_id).exchange([])) sent = [json.loads(line) for line in process.stdin.getvalue().splitlines()] self.assertEqual([message['method'] for message in sent], ['initialize']) process.kill.assert_called_once() process.wait.assert_called() def test_failed_call_prevents_later_mutation_from_being_sent(self): client = MCPClient(self.path, self.identity.path, self.alice.user_id) self.assert_code('TOOL_NOT_ALLOWED', lambda: client.exchange([ ('unknown_tool', {}), ('erp_receive_order', {'order_no': 'PO-2026-1003', 'quantity': 40, 'expected_version': 1, 'request_key': 'must-not-be-sent'})])) self.assertEqual(self.flow.ask(self.alice, '查询PO-2026-1003')['orders'][0]['received_qty'], 40) def test_zero_negative_decimal_and_oversize_question_quantities_are_rejected(self): for scenario, question in [('erp', 'PO-2026-1003 收货0件'), ('erp', 'PO-2026-1003 收货-5件'), ('wms', '入库0件'), ('wms', '出库-5件'), ('wms', '出库1.5件'), ('wms', '入库1000001件')]: self.assert_code('QUANTITY_INVALID', lambda: self.flow.ask(self.alice, question, scenario)) self.assertEqual(self.flow.ask(self.alice, '入库5件', 'wms')['action']['parameters']['quantity'], 5) self.assertEqual(self.flow.ask(self.alice, '先开始巡检再完成巡检', 'equipment')['action']['kind'], 'start') def test_wms_query_does_not_propose_write_and_reduction_never_means_receipt(self): self.assertIsNone(self.flow.ask(self.alice, '查询库存', 'wms')['action']) self.assertIsNone(self.flow.ask(self.alice, '给物料出库', 'wms')['action']) for text in ('减少5件', '扣减5件', '出库5件'): proposal = self.flow.ask(self.alice, text, 'wms')['action'] self.assertEqual(proposal['kind'], 'issue') self.assertEqual(proposal['parameters'], {'quantity': 5}) def test_wms_multiaction_and_negative_clauses_never_mix_direction_and_quantity(self): for question in ('SKU-DEMO-001 先入库5件,再出库3件', 'SKU-DEMO-001 不要出库,入库5件', 'SKU-DEMO-001 先出库3件再入库5件', 'SKU-DEMO-001 不要减少5件'): result = self.flow.ask(self.alice, question, 'wms') self.assertIsNone(result['action']) self.assertEqual(result['records'][0]['quantity'], 20) self.assertIn('请分步', result['answer']) self.assertEqual([call['tool'] for call in result['tool_calls']], ['knowledge_search', 'wms_stock_get']) for question, kind, quantity in [('SKU-DEMO-001 入库5件并复查', 'receive', 5), ('SKU-DEMO-001 出库3件并复查', 'issue', 3)]: proposal = self.flow.ask(self.alice, question, 'wms')['action'] self.assertEqual((proposal['kind'], proposal['parameters']['quantity']), (kind, quantity)) def test_erp_and_equipment_negation_remain_read_only(self): erp = self.flow.ask(self.alice, 'PO-2026-1003 不要收货,仅查询状态') self.assertIsNone(erp['action']) self.assertNotIn('proposal', erp) self.assertEqual(erp['orders'][0]['received_qty'], 40) for question in ('WO-DEMO-001 不要开始处理', 'WO-DEMO-001 不要完成,只开始处理'): equipment = self.flow.ask(self.alice, question, 'equipment') self.assertIsNone(equipment['action']) self.assertEqual(equipment['records'][0]['status'], 'OPEN') self.flow.act(self.alice, 'equipment', 'start', 'WO-DEMO-001', {}, 1, 'negation-start-01') equipment = self.flow.ask(self.alice, 'WO-DEMO-001 不要完成工单,只查询状态', 'equipment') self.assertIsNone(equipment['action']) self.assertEqual(equipment['records'][0]['status'], 'IN_PROGRESS') def test_procedure_question_only_retrieves_relevant_knowledge(self): result = self.flow.ask(self.alice, '来料质检不合格应该如何处理?') self.assertEqual(result['intent'], 'procedure_guidance') self.assertEqual(result['records'], []) self.assertEqual(result['citations'][0]['id'], 'PO-QUALITY') self.assertIn('质量检验', result['answer']) self.assertEqual([call['tool'] for call in result['tool_calls']], ['knowledge_search']) self.assertTrue(result['steps']) unknown = self.flow.ask(self.alice, '员工社保怎么规划') self.assertEqual(unknown['intent'], 'needs_clarification') self.assertEqual(unknown['citations'], []) def test_equipment_explicit_start_complete_intents_are_not_changed(self): for question in ('完成工单 WO-DEMO-001', '完成处理 WO-DEMO-001', '关闭工单 WO-DEMO-001'): result = self.flow.ask(self.alice, question, 'equipment') self.assertIsNone(result['action']) self.assertIn('需先开始', result['answer']) start = self.flow.ask(self.alice, 'WO-DEMO-001 开始处理并复查状态', 'equipment')['action'] self.assertEqual(start['kind'], 'start') self.flow.act(self.alice, 'equipment', 'start', 'WO-DEMO-001', {}, 1, 'explicit-start-01') repeated = self.flow.ask(self.alice, 'WO-DEMO-001 开始处理', 'equipment') self.assertIsNone(repeated['action']) self.assertIn('不会重复开始或自动完工', repeated['answer']) finish = self.flow.ask(self.alice, 'WO-DEMO-001 完成处理', 'equipment')['action'] self.assertEqual(finish['kind'], 'complete') self.flow.act(self.alice, 'equipment', 'complete', 'WO-DEMO-001', {}, 2, 'explicit-finish-01') self.assertIsNone(self.flow.ask(self.alice, 'WO-DEMO-001 开始处理', 'equipment')['action']) def test_historical_receipt_after_reset_is_distinct_from_current_readback(self): first = self.flow.act(self.alice, 'wms', 'receive', 'SKU-DEMO-001', {'quantity': 7}, 1, 'historical-in-01') self.assertEqual(first['after']['quantity'], 27) self.flow.reset(self.alice, 'wms', 'historical-reset-01') replay = self.flow.act(self.alice, 'wms', 'receive', 'SKU-DEMO-001', {'quantity': 7}, 1, 'historical-in-01') self.assertEqual(replay['after']['quantity'], 27) self.assertEqual(replay['readback']['quantity'], 20) self.assertTrue(replay['receipt_committed']) self.assertTrue(replay['superseded']) self.assertFalse(replay['readback_matches_receipt']) self.assertFalse(replay['persisted']) self.assertIn('20 → 27', replay['answer']) self.assertIn('没有重复修改', replay['answer']) def test_reset_key_replay_after_new_write_does_not_claim_new_reset(self): self.flow.reset(self.alice, 'wms', 'once-reset-0001') self.flow.act(self.alice, 'wms', 'receive', 'SKU-DEMO-001', {'quantity': 7}, 2, 'after-reset-in-01') replay = self.flow.reset(self.alice, 'wms', 'once-reset-0001') self.assertTrue(replay['replayed']) self.assertTrue(replay['superseded']) self.assertEqual(replay['after'][0]['quantity'], 20) self.assertEqual(replay['readback'][0]['quantity'], 27) self.assertIn('没有再次重置', replay['answer']) def test_rejected_business_actions_leave_auditable_evidence(self): self.assert_code('STOCK_INSUFFICIENT', lambda: self.store.execute('wms_stock_adjust', { 'sku': 'SKU-DEMO-001', 'action': 'issue', 'quantity': 100, 'expected_version': 1, 'request_key': 'negative-stock-check'})) with sqlite3.connect(self.path) as db: row = db.execute('SELECT owner,project,tool,outcome FROM business_audit').fetchone() self.assertEqual(row, (self.alice.user_id, 'wms-demo', 'wms_stock_adjust', 'rejected')) if __name__ == '__main__': unittest.main()