import unittest from pathlib import Path from types import SimpleNamespace from fastapi.testclient import TestClient from src.adapters.persistence.local_identity import LocalIdentity from src.apps.workbench.erp_flow import ERPFlow from src.apps.workbench.web import create_web from src.tests.runtime.fixtures import workspace class BusinessWebTests(unittest.TestCase): def setUp(self): folder=Path(self.enterContext(workspace())) self.identity=LocalIdentity(folder/'identity.sqlite') self.identity.seed_projects();self.identity.seed_demo_users() service=ERPFlow(self.identity,folder/'knowledge.sqlite') self.client=self.enterContext(TestClient(create_web(SimpleNamespace(identity=self.identity,business=service,erp=service,shutdown=lambda:None)))) self.client.headers['Origin']='http://127.0.0.1:8789' def login(self,username='programmer'): response=self.client.post('/api/session/login',json={'username':username,'password':'VibeCoding@2026'}) self.assertEqual(response.status_code,200) session=self.client.get('/api/session').json() self.client.headers['X-CSRF-Token']=session['csrf'] return self.identity.actor(session['actor']['user_id']) def test_business_endpoints_require_real_session_and_csrf(self): self.assertEqual(self.client.get('/api/business/scenarios').status_code,401) self.login() response=self.client.post('/api/business/ask',json={'scenario':'erp','question':'查询最新完成订单'},headers={'X-CSRF-Token':'bad'}) self.assertEqual(response.status_code,403) response=self.client.post('/api/business/actions',json={'scenario':'wms','action':'receive','entity_id':'SKU-DEMO-001','parameters':{'quantity':5},'expected_version':True,'request_key':'test-key-123'}) self.assertEqual(response.status_code,422) def test_all_scenarios_expose_actual_stdio_health(self): self.login();response=self.client.get('/api/business/scenarios') self.assertEqual(response.status_code,200) self.assertEqual({s['scenario'] for s in response.json()},{'erp','wms','equipment'}) for s in response.json(): self.assertEqual(s['health']['transport'],'stdio') self.assertTrue(s['health']['process_exited']);self.assertTrue(s['tools']) def test_unknown_action_parameters_cannot_change_record_owner(self): self.login() original=self.client.post('/api/business/ask',json={'scenario':'wms','question':'查询SKU-DEMO-001库存'}).json()['records'][0] response=self.client.post('/api/business/actions',json={'scenario':'wms','action':'receive','entity_id':'SKU-DEMO-001','parameters':{'quantity':5,'owner':'manager'},'expected_version':original['version'],'request_key':'test-key-123'}) self.assertEqual(response.status_code,422) self.assertEqual(response.json()['code'],'ACTION_INVALID') current=self.client.post('/api/business/ask',json={'scenario':'wms','question':'查询SKU-DEMO-001库存'}).json()['records'][0] self.assertEqual(original['content_digest'],current['content_digest']) def test_membership_revocation_applies_to_ask_actions_and_reset(self): actor=self.login() token,_=self.identity.login('manager','VibeCoding@2026');manager=self.identity.session(token)[0] self.identity.set_member(manager,'wms-demo',actor.user_id,False) self.assertEqual(self.client.post('/api/business/ask',json={'scenario':'wms','question':'查询库存'}).status_code,404) self.assertEqual(self.client.post('/api/business/reset',json={'scenario':'wms','request_key':'test-reset-key'}).status_code,404) self.assertEqual(self.client.post('/api/business/actions',json={'scenario':'wms','action':'receive','entity_id':'SKU-DEMO-001','parameters':{'quantity':5},'expected_version':1,'request_key':'test-key-123'}).status_code,404) self.assertEqual({s['scenario'] for s in self.client.get('/api/business/scenarios').json()},{'erp','equipment'})