""" AI助手Web客户端测试 测试Flask应用的各个端点 """ import unittest import json from app import app class TestWebClient(unittest.TestCase): """Web客户端测试类""" def setUp(self): """测试前准备""" self.app = app self.app.config['TESTING'] = True self.client = self.app.test_client() def test_index_page(self): """测试主页面""" response = self.client.get('/') self.assertEqual(response.status_code, 200) self.assertIn(b'AI', response.data) def test_health_check(self): """测试健康检查端点""" response = self.client.get('/api/health') self.assertEqual(response.status_code, 200) data = json.loads(response.data) self.assertEqual(data['code'], 200) self.assertEqual(data['message'], 'OK') self.assertIn('status', data['data']) self.assertEqual(data['data']['status'], 'healthy') def test_get_applications(self): """测试获取应用列表""" response = self.client.get('/api/applications') self.assertEqual(response.status_code, 200) data = json.loads(response.data) self.assertEqual(data['code'], 200) self.assertIn('data', data) self.assertIsInstance(data['data'], list) def test_send_message_without_content(self): """测试发送空消息""" response = self.client.post( '/api/chat/send', data=json.dumps({ 'appId': 15, 'message': '', 'chatId': None, 'stream': False }), content_type='application/json' ) self.assertEqual(response.status_code, 400) data = json.loads(response.data) self.assertEqual(data['code'], 400) def test_send_message_with_content(self): """测试发送正常消息""" response = self.client.post( '/api/chat/send', data=json.dumps({ 'appId': 15, 'message': '你好', 'chatId': None, 'stream': False }), content_type='application/json' ) self.assertEqual(response.status_code, 200) data = json.loads(response.data) self.assertEqual(data['code'], 200) self.assertIn('data', data) self.assertIn('answer', data['data']) def test_cors_headers(self): """测试CORS头""" response = self.client.get('/api/health') self.assertIn('Access-Control-Allow-Origin', response.headers) def test_static_files(self): """测试静态文件访问""" # 测试CSS文件 response = self.client.get('/static/css/style.css') self.assertEqual(response.status_code, 200) # 测试JS文件 response = self.client.get('/static/js/main.js') self.assertEqual(response.status_code, 200) class TestAPIResponses(unittest.TestCase): """API响应格式测试""" def setUp(self): """测试前准备""" self.app = app self.app.config['TESTING'] = True self.client = self.app.test_client() def test_response_format(self): """测试响应格式统一性""" response = self.client.get('/api/health') data = json.loads(response.data) # 检查必需字段 self.assertIn('code', data) self.assertIn('message', data) self.assertIn('data', data) # 检查字段类型 self.assertIsInstance(data['code'], int) self.assertIsInstance(data['message'], str) def test_error_response_format(self): """测试错误响应格式""" response = self.client.post( '/api/chat/send', data=json.dumps({'message': ''}), content_type='application/json' ) data = json.loads(response.data) # 错误响应也应该有统一格式 self.assertIn('code', data) self.assertIn('message', data) self.assertEqual(data['code'], 400) def run_tests(): """运行所有测试""" # 创建测试套件 loader = unittest.TestLoader() suite = unittest.TestSuite() # 添加测试 suite.addTests(loader.loadTestsFromTestCase(TestWebClient)) suite.addTests(loader.loadTestsFromTestCase(TestAPIResponses)) # 运行测试 runner = unittest.TextTestRunner(verbosity=2) result = runner.run(suite) # 返回测试结果 return result.wasSuccessful() if __name__ == '__main__': import sys success = run_tests() sys.exit(0 if success else 1)