KnowledgeGraphServiceTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. namespace Tests\Unit\Services;
  3. use Tests\TestCase;
  4. use App\Services\KnowledgeGraphService;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\Log;
  7. class KnowledgeGraphServiceTest extends TestCase
  8. {
  9. protected KnowledgeGraphService $service;
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. $this->service = new KnowledgeGraphService();
  14. }
  15. /** @test */
  16. public function it_initializes_with_correct_base_url()
  17. {
  18. // 默认URL
  19. $this->assertEquals('http://localhost:5011', $this->service->baseUrl);
  20. // 测试自定义URL
  21. $this->service = new KnowledgeGraphService();
  22. $this->service->baseUrl = 'http://knowledge-api.test';
  23. $this->assertEquals('http://knowledge-api.test', $this->service->baseUrl);
  24. }
  25. /** @test */
  26. public function it_trims_trailing_slash_from_base_url()
  27. {
  28. $this->service->baseUrl = 'http://localhost:5011/';
  29. $this->assertEquals('http://localhost:5011', $this->service->baseUrl);
  30. }
  31. /** @test */
  32. public function list_knowledge_points_returns_formatted_data()
  33. {
  34. Http::fake([
  35. 'localhost:5011/knowledge-points/*' => Http::response([
  36. 'data' => [
  37. [
  38. 'id' => 'kp_1',
  39. 'kp_code' => 'KP1001',
  40. 'cn_name' => '因式分解基础',
  41. 'category' => '数学',
  42. 'phase' => '初中',
  43. 'grade' => 8,
  44. 'importance' => 5,
  45. ],
  46. ],
  47. ], 200),
  48. ]);
  49. $result = $this->service->listKnowledgePoints();
  50. $this->assertIsArray($result);
  51. $this->assertCount(1, $result);
  52. $this->assertEquals('kp_1', $result[0]['id']);
  53. $this->assertEquals('KP1001', $result[0]['kp_code']);
  54. $this->assertEquals('因式分解基础', $result[0]['cn_name']);
  55. $this->assertEquals('数学', $result[0]['category']);
  56. $this->assertEquals('初中', $result[0]['phase']);
  57. }
  58. /** @test */
  59. public function list_knowledge_points_returns_fallback_data_on_error()
  60. {
  61. Http::fake([
  62. 'localhost:5011/knowledge-points/*' => Http::response([], 500),
  63. ]);
  64. $result = $this->service->listKnowledgePoints();
  65. // 应该返回备用数据
  66. $this->assertIsArray($result);
  67. $this->assertGreaterThan(0, count($result));
  68. $this->assertArrayHasKey('id', $result[0]);
  69. $this->assertArrayHasKey('kp_code', $result[0]);
  70. $this->assertArrayHasKey('cn_name', $result[0]);
  71. }
  72. /** @test */
  73. public function list_knowledge_points_handles_multiple_field_names()
  74. {
  75. Http::fake([
  76. 'localhost:5011/knowledge-points/*' => Http::response([
  77. 'data' => [
  78. [
  79. 'kp_id' => 'kp_1', // 使用kp_id而不是id
  80. 'kp_name' => '因式分解', // 使用kp_name而不是cn_name
  81. ],
  82. ],
  83. ], 200),
  84. ]);
  85. $result = $this->service->listKnowledgePoints();
  86. $this->assertEquals('kp_1', $result[0]['id']);
  87. $this->assertEquals('KP_UNKNOWN', $result[0]['kp_code']);
  88. $this->assertEquals('因式分解', $result[0]['cn_name']);
  89. }
  90. /** @test */
  91. public function get_skills_by_knowledge_point_returns_formatted_data()
  92. {
  93. Http::fake([
  94. 'localhost:5011/graph/node/*' => Http::response([
  95. 'skills' => [
  96. [
  97. 'skill_code' => 'SK001',
  98. 'skill_name' => '计算能力',
  99. 'weight' => 0.9,
  100. ],
  101. ],
  102. ], 200),
  103. ]);
  104. $result = $this->service->getSkillsByKnowledgePoint('KP1001');
  105. $this->assertIsArray($result);
  106. $this->assertCount(1, $result);
  107. $this->assertEquals('SK001', $result[0]['code']);
  108. $this->assertEquals('计算能力', $result[0]['name']);
  109. $this->assertEquals(0.9, $result[0]['weight']);
  110. }
  111. /** @test */
  112. public function get_skills_by_knowledge_point_returns_empty_on_error()
  113. {
  114. Http::fake([
  115. 'localhost:5011/graph/node/*' => Http::response([], 500),
  116. ]);
  117. $result = $this->service->getSkillsByKnowledgePoint('KP1001');
  118. $this->assertIsArray($result);
  119. $this->assertEmpty($result);
  120. }
  121. /** @test */
  122. public function list_skills_returns_formatted_data()
  123. {
  124. Http::fake([
  125. 'localhost:5011/skills/*' => Http::response([
  126. 'data' => [
  127. [
  128. 'id' => 'sk_1',
  129. 'skill_code' => 'SK001',
  130. 'skill_name' => '计算能力',
  131. 'skill_type' => '基础技能',
  132. ],
  133. ],
  134. ], 200),
  135. ]);
  136. $result = $this->service->listSkills();
  137. $this->assertIsArray($result);
  138. $this->assertCount(1, $result);
  139. $this->assertEquals('sk_1', $result[0]['id']);
  140. $this->assertEquals('SK001', $result[0]['code']);
  141. $this->assertEquals('计算能力', $result[0]['name']);
  142. $this->assertEquals('基础技能', $result[0]['category']);
  143. }
  144. /** @test */
  145. public function list_relations_returns_formatted_data()
  146. {
  147. Http::fake([
  148. 'localhost:5011/graph/relations' => Http::response([
  149. 'data' => [
  150. [
  151. 'source' => 'R01',
  152. 'target' => 'R02',
  153. 'relation_type' => 'PREREQUISITE',
  154. 'relation_direction' => 'DOWNSTREAM',
  155. 'weight' => 0.9,
  156. 'description' => '前置依赖',
  157. ],
  158. ],
  159. ], 200),
  160. ]);
  161. $result = $this->service->listRelations();
  162. $this->assertIsArray($result);
  163. $this->assertCount(1, $result);
  164. $this->assertEquals('R01', $result[0]['source_kp']);
  165. $this->assertEquals('R02', $result[0]['target_kp']);
  166. $this->assertEquals('PREREQUISITE', $result[0]['relation_type']);
  167. $this->assertEquals('DOWNSTREAM', $result[0]['relation_direction']);
  168. $this->assertEquals(0.9, $result[0]['weight']);
  169. }
  170. /** @test */
  171. public function export_graph_returns_api_data()
  172. {
  173. $graphData = [
  174. 'nodes' => [
  175. ['id' => 'R01', 'label' => '有理数'],
  176. ],
  177. 'edges' => [
  178. ['source' => 'R01', 'target' => 'R02'],
  179. ],
  180. ];
  181. Http::fake([
  182. 'localhost:5011/graph/export' => Http::response($graphData, 200),
  183. ]);
  184. $result = $this->service->exportGraph();
  185. $this->assertIsArray($result);
  186. $this->assertArrayHasKey('nodes', $result);
  187. $this->assertArrayHasKey('edges', $result);
  188. $this->assertCount(1, $result['nodes']);
  189. $this->assertCount(1, $result['edges']);
  190. }
  191. /** @test */
  192. public function export_graph_returns_empty_on_error()
  193. {
  194. Http::fake([
  195. 'localhost:5011/graph/export' => Http::response([], 500),
  196. ]);
  197. $result = $this->service->exportGraph();
  198. $this->assertIsArray($result);
  199. $this->assertArrayHasKey('nodes', $result);
  200. $this->assertArrayHasKey('edges', $result);
  201. $this->assertEmpty($result['nodes']);
  202. $this->assertEmpty($result['edges']);
  203. }
  204. /** @test */
  205. public function check_health_returns_true_when_healthy()
  206. {
  207. Http::fake([
  208. 'localhost:5011/health' => Http::response([], 200),
  209. ]);
  210. $result = $this->service->checkHealth();
  211. $this->assertTrue($result);
  212. }
  213. /** @test */
  214. public function check_health_returns_false_when_unhealthy()
  215. {
  216. Http::fake([
  217. 'localhost:5011/health' => Http::response([], 500),
  218. ]);
  219. $result = $this->service->checkHealth();
  220. $this->assertFalse($result);
  221. }
  222. /** @test */
  223. public function check_health_returns_false_on_exception()
  224. {
  225. Http::fake([
  226. 'localhost:5011/health' => Http::throw(new \Exception('Connection failed')),
  227. ]);
  228. $result = $this->service->checkHealth();
  229. $this->assertFalse($result);
  230. }
  231. /** @test */
  232. public function get_mastery_color_returns_correct_colors()
  233. {
  234. $this->assertEquals('#10b981', $this->service->getMasteryColor(0.9));
  235. $this->assertEquals('#3b82f6', $this->service->getMasteryColor(0.7));
  236. $this->assertEquals('#f59e0b', $this->service->getMasteryColor(0.5));
  237. $this->assertEquals('#f97316', $this->service->getMasteryColor(0.3));
  238. $this->assertEquals('#ef4444', $this->service->getMasteryColor(0.1));
  239. }
  240. /** @test */
  241. public function get_mastery_color_handles_edge_cases()
  242. {
  243. $this->assertEquals('#10b981', $this->service->getMasteryColor(1.0));
  244. $this->assertEquals('#ef4444', $this->service->getMasteryColor(0.0));
  245. $this->assertEquals('#ef4444', $this->service->getMasteryColor(-0.1));
  246. }
  247. /** @test */
  248. public function get_student_mastery_returns_api_data()
  249. {
  250. Http::fake([
  251. 'localhost:5010/api/mastery/*' => Http::response([
  252. 'masteries' => [
  253. ['kp_code' => 'R01', 'mastery_level' => 0.85, 'confidence_level' => 0.8],
  254. ],
  255. ], 200),
  256. ]);
  257. $result = $this->service->getStudentMastery(1001);
  258. $this->assertIsArray($result);
  259. $this->assertArrayHasKey('masteries', $result);
  260. $this->assertCount(1, $result['masteries']);
  261. }
  262. /** @test */
  263. public function get_student_mastery_returns_mock_data_on_error()
  264. {
  265. Http::fake([
  266. 'localhost:5010/api/mastery/*' => Http::response([], 500),
  267. ]);
  268. $result = $this->service->getStudentMastery(1001);
  269. $this->assertIsArray($result);
  270. $this->assertArrayHasKey('masteries', $result);
  271. $this->assertCount(5, $result['masteries']);
  272. }
  273. /** @test */
  274. public function get_student_statistics_returns_api_data()
  275. {
  276. Http::fake([
  277. 'localhost:5010/api/mastery/*/statistics' => Http::response([
  278. 'total_knowledge_points' => 8,
  279. 'average_mastery' => 0.65,
  280. 'high_mastery_count' => 2,
  281. 'medium_mastery_count' => 3,
  282. 'low_mastery_count' => 3,
  283. ], 200),
  284. ]);
  285. $result = $this->service->getStudentStatistics(1001);
  286. $this->assertIsArray($result);
  287. $this->assertEquals(8, $result['total_knowledge_points']);
  288. $this->assertEquals(0.65, $result['average_mastery']);
  289. $this->assertEquals(2, $result['high_mastery_count']);
  290. }
  291. /** @test */
  292. public function get_student_statistics_returns_mock_data_on_error()
  293. {
  294. Http::fake([
  295. 'localhost:5010/api/mastery/*/statistics' => Http::response([], 500),
  296. ]);
  297. $result = $this->service->getStudentStatistics(1001);
  298. $this->assertIsArray($result);
  299. $this->assertEquals(5, $result['total_knowledge_points']);
  300. $this->assertEquals(0.594, $result['average_mastery']);
  301. }
  302. /** @test */
  303. public function get_student_statistics_handles_api_exception()
  304. {
  305. Http::fake([
  306. 'localhost:5010/api/mastery/*/statistics' => Http::throw(new \Exception('API Error')),
  307. ]);
  308. $result = $this->service->getStudentStatistics(1001);
  309. $this->assertIsArray($result);
  310. $this->assertArrayHasKey('total_knowledge_points', $result);
  311. $this->assertArrayHasKey('average_mastery', $result);
  312. }
  313. /** @test */
  314. public function import_graph_calls_api_correctly()
  315. {
  316. Http::fake([
  317. 'localhost:5011/import/graph' => Http::response([
  318. 'success' => true,
  319. 'imported' => 100,
  320. ], 200),
  321. ]);
  322. $treeData = ['nodes' => []];
  323. $edgesData = ['edges' => []];
  324. $result = $this->service->importGraph($treeData, $edgesData);
  325. $this->assertTrue($result);
  326. }
  327. /** @test */
  328. public function import_graph_handles_api_error()
  329. {
  330. Http::fake([
  331. 'localhost:5011/import/graph' => Http::response([
  332. 'success' => false,
  333. 'error' => 'Invalid data',
  334. ], 400),
  335. ]);
  336. $treeData = ['nodes' => []];
  337. $edgesData = ['edges' => []];
  338. $result = $this->service->importGraph($treeData, $edgesData);
  339. $this->assertFalse($result);
  340. }
  341. /** @test */
  342. public function get_knowledge_point_calls_correct_endpoint()
  343. {
  344. Http::fake([
  345. 'localhost:5011/knowledge-points/*' => Http::response([
  346. 'id' => 'KP1001',
  347. 'name' => '因式分解基础',
  348. ], 200),
  349. ]);
  350. $result = $this->service->getKnowledgePoint('KP1001');
  351. $this->assertIsArray($result);
  352. $this->assertEquals('KP1001', $result['id']);
  353. }
  354. /** @test */
  355. public function create_knowledge_point_calls_correct_endpoint()
  356. {
  357. Http::fake([
  358. 'localhost:5011/knowledge-points/*' => Http::response([], 201),
  359. ]);
  360. $data = [
  361. 'kp_code' => 'KP1001',
  362. 'cn_name' => '因式分解基础',
  363. 'category' => '数学',
  364. ];
  365. $result = $this->service->createKnowledgePoint($data);
  366. $this->assertTrue($result);
  367. }
  368. /** @test */
  369. public function update_knowledge_point_calls_correct_endpoint()
  370. {
  371. Http::fake([
  372. 'localhost:5011/knowledge-points/*' => Http::response([], 200),
  373. ]);
  374. $data = [
  375. 'cn_name' => '因式分解进阶',
  376. ];
  377. $result = $this->service->updateKnowledgePoint('KP1001', $data);
  378. $this->assertTrue($result);
  379. }
  380. /** @test */
  381. public function delete_knowledge_point_calls_correct_endpoint()
  382. {
  383. Http::fake([
  384. 'localhost:5011/knowledge-points/*' => Http::response([], 204),
  385. ]);
  386. $result = $this->service->deleteKnowledgePoint('KP1001');
  387. $this->assertTrue($result);
  388. }
  389. }