PacketServiceTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace Tests\Feature\Services;
  3. use App\Models\Channel;
  4. use App\Services\PacketService;
  5. use App\Services\SentenceService;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Str;
  9. use Tests\TestCase;
  10. use ZipArchive;
  11. use App\Http\Api\ChannelApi;
  12. /**
  13. * PacketService单元测试
  14. *
  15. * 测试PacketService的数据导出和打包功能
  16. */
  17. class PacketServiceTest extends TestCase
  18. {
  19. use RefreshDatabase;
  20. /**
  21. * 巴利文channel
  22. */
  23. private Channel $paliChannel;
  24. /**
  25. * 译文channel
  26. */
  27. private Channel $translationChannel;
  28. /**
  29. * 测试用的editor_uid
  30. */
  31. private string $editorUid;
  32. /**
  33. * SentenceService实例
  34. */
  35. private SentenceService $sentenceService;
  36. /**
  37. * 设置测试环境
  38. *
  39. * @return void
  40. */
  41. protected function setUp(): void
  42. {
  43. parent::setUp();
  44. // 调试:检查数据库连接
  45. dump(config('database.default')); // 应该是 pgsql
  46. dump(config('database.connections.pgsql.database')); // 应该是 mint_test
  47. // 生成测试用的editor_uid
  48. $this->editorUid = Str::uuid()->toString();
  49. // 初始化SentenceService
  50. $this->sentenceService = app(SentenceService::class);
  51. // 创建测试用的channels
  52. $orgChannelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  53. $this->paliChannel = Channel::find($orgChannelId);
  54. $this->translationChannel = Channel::find('00ae2c48-c204-4082-ae79-79ba2740d506');
  55. }
  56. /**
  57. * 清理测试环境
  58. *
  59. * @return void
  60. */
  61. protected function tearDown(): void
  62. {
  63. // 清理生成的文件
  64. Storage::deleteDirectory('packet');
  65. Storage::deleteDirectory('temp');
  66. parent::tearDown();
  67. }
  68. /**
  69. * 测试基本的导出功能
  70. *
  71. * @return void
  72. */
  73. public function test_export_creates_zip_file(): void
  74. {
  75. // 创建测试数据
  76. $this->createTestSentences();
  77. // 执行导出
  78. $service = new PacketService(
  79. $this->paliChannel->uid,
  80. [$this->translationChannel->uid]
  81. );
  82. $zipPath = $service->export();
  83. // 断言ZIP文件已创建
  84. $this->assertTrue(Storage::exists($zipPath));
  85. $this->assertStringStartsWith('packet/training_data_', $zipPath);
  86. $this->assertStringEndsWith('.zip', $zipPath);
  87. }
  88. /**
  89. * 测试ZIP文件内容结构
  90. *
  91. * @return void
  92. */
  93. public function test_zip_contains_correct_structure(): void
  94. {
  95. // 创建测试数据
  96. $this->createTestSentences();
  97. // 执行导出
  98. $service = new PacketService(
  99. $this->paliChannel->uid,
  100. [$this->translationChannel->uid]
  101. );
  102. $zipPath = $service->export();
  103. $fullPath = Storage::path($zipPath);
  104. // 打开ZIP文件检查内容
  105. $zip = new ZipArchive();
  106. $this->assertTrue($zip->open($fullPath));
  107. // 检查是否包含translations目录
  108. $expectedFile = 'translations/Chinese Translation.jsonl';
  109. $this->assertNotFalse($zip->locateName($expectedFile));
  110. $zip->close();
  111. }
  112. /**
  113. * 测试JSONL文件内容格式
  114. *
  115. * @return void
  116. */
  117. public function test_jsonl_file_format(): void
  118. {
  119. // 创建测试数据
  120. $this->createTestSentences();
  121. // 执行导出
  122. $service = new PacketService(
  123. $this->paliChannel->uid,
  124. [$this->translationChannel->uid]
  125. );
  126. $zipPath = $service->export();
  127. $fullPath = Storage::path($zipPath);
  128. // 解压并读取JSONL文件
  129. $zip = new ZipArchive();
  130. $zip->open($fullPath);
  131. $jsonlContent = $zip->getFromName('translations/Chinese Translation.jsonl');
  132. $zip->close();
  133. // 解析JSONL内容
  134. $lines = explode("\n", trim($jsonlContent));
  135. $this->assertGreaterThan(0, count($lines));
  136. // 检查第一行的格式
  137. $firstLine = json_decode($lines[0], true);
  138. $this->assertArrayHasKey('id', $firstLine);
  139. $this->assertArrayHasKey('pali', $firstLine);
  140. $this->assertArrayHasKey('translation', $firstLine);
  141. // 检查ID格式
  142. $this->assertMatchesRegularExpression('/^\d+-\d+-\d+-\d+$/', $firstLine['id']);
  143. }
  144. /**
  145. * 测试数据排序
  146. *
  147. * @return void
  148. */
  149. public function test_data_is_sorted_correctly(): void
  150. {
  151. // 创建乱序的测试数据
  152. $this->sentenceService->save([
  153. 'book_id' => 2,
  154. 'paragraph' => 1,
  155. 'word_start' => 1,
  156. 'word_end' => 5,
  157. 'content' => 'Pali text 2',
  158. 'channel_uid' => $this->paliChannel->uid,
  159. 'editor_uid' => $this->editorUid,
  160. ]);
  161. $this->sentenceService->save([
  162. 'book_id' => 2,
  163. 'paragraph' => 1,
  164. 'word_start' => 1,
  165. 'word_end' => 5,
  166. 'content' => 'Translation 2',
  167. 'channel_uid' => $this->translationChannel->uid,
  168. 'editor_uid' => $this->editorUid,
  169. ]);
  170. $this->sentenceService->save([
  171. 'book_id' => 1,
  172. 'paragraph' => 1,
  173. 'word_start' => 1,
  174. 'word_end' => 5,
  175. 'content' => 'Pali text 1',
  176. 'channel_uid' => $this->paliChannel->uid,
  177. 'editor_uid' => $this->editorUid,
  178. ]);
  179. $this->sentenceService->save([
  180. 'book_id' => 1,
  181. 'paragraph' => 1,
  182. 'word_start' => 1,
  183. 'word_end' => 5,
  184. 'content' => 'Translation 1',
  185. 'channel_uid' => $this->translationChannel->uid,
  186. 'editor_uid' => $this->editorUid,
  187. ]);
  188. // 执行导出
  189. $service = new PacketService(
  190. $this->paliChannel->uid,
  191. [$this->translationChannel->uid]
  192. );
  193. $zipPath = $service->export();
  194. $fullPath = Storage::path($zipPath);
  195. // 读取JSONL内容
  196. $zip = new ZipArchive();
  197. $zip->open($fullPath);
  198. $jsonlContent = $zip->getFromName('translations/Chinese Translation.jsonl');
  199. $zip->close();
  200. $lines = explode("\n", trim($jsonlContent));
  201. $firstLine = json_decode($lines[0], true);
  202. $secondLine = json_decode($lines[1], true);
  203. // 第一条应该是book_id=1的记录
  204. $this->assertEquals('1-1-1-5', $firstLine['id']);
  205. $this->assertEquals('2-1-1-5', $secondLine['id']);
  206. }
  207. /**
  208. * 测试跳过空译文
  209. *
  210. * @return void
  211. */
  212. public function test_skips_empty_translations(): void
  213. {
  214. // 创建有空译文的测试数据
  215. $this->sentenceService->save([
  216. 'book_id' => 1,
  217. 'paragraph' => 1,
  218. 'word_start' => 1,
  219. 'word_end' => 5,
  220. 'content' => 'Pali text',
  221. 'channel_uid' => $this->paliChannel->uid,
  222. 'editor_uid' => $this->editorUid,
  223. ]);
  224. $this->sentenceService->save([
  225. 'book_id' => 1,
  226. 'paragraph' => 1,
  227. 'word_start' => 1,
  228. 'word_end' => 5,
  229. 'content' => '', // 空译文
  230. 'channel_uid' => $this->translationChannel->uid,
  231. 'editor_uid' => $this->editorUid,
  232. ]);
  233. $this->sentenceService->save([
  234. 'book_id' => 1,
  235. 'paragraph' => 2,
  236. 'word_start' => 1,
  237. 'word_end' => 5,
  238. 'content' => 'Pali text 2',
  239. 'channel_uid' => $this->paliChannel->uid,
  240. 'editor_uid' => $this->editorUid,
  241. ]);
  242. $this->sentenceService->save([
  243. 'book_id' => 1,
  244. 'paragraph' => 2,
  245. 'word_start' => 1,
  246. 'word_end' => 5,
  247. 'content' => 'Valid translation',
  248. 'channel_uid' => $this->translationChannel->uid,
  249. 'editor_uid' => $this->editorUid,
  250. ]);
  251. // 执行导出
  252. $service = new PacketService(
  253. $this->paliChannel->uid,
  254. [$this->translationChannel->uid]
  255. );
  256. $zipPath = $service->export();
  257. $fullPath = Storage::path($zipPath);
  258. // 读取JSONL内容
  259. $zip = new ZipArchive();
  260. $zip->open($fullPath);
  261. $jsonlContent = $zip->getFromName('translations/Chinese Translation.jsonl');
  262. $zip->close();
  263. $lines = array_filter(explode("\n", trim($jsonlContent)));
  264. // 应该只有一条记录(跳过了空译文)
  265. $this->assertCount(1, $lines);
  266. }
  267. /**
  268. * 测试多个译文版本
  269. *
  270. * @return void
  271. */
  272. public function test_multiple_translation_channels(): void
  273. {
  274. // 创建第二个译文channel
  275. $secondTranslation = Channel::create([
  276. 'uid' => 'translation-2-test-uid',
  277. 'name' => 'English Translation',
  278. 'lang' => 'en',
  279. ]);
  280. // 创建测试数据
  281. $this->sentenceService->save([
  282. 'book_id' => 1,
  283. 'paragraph' => 1,
  284. 'word_start' => 1,
  285. 'word_end' => 5,
  286. 'content' => 'Pali text',
  287. 'channel_uid' => $this->paliChannel->uid,
  288. 'editor_uid' => $this->editorUid,
  289. ]);
  290. $this->sentenceService->save([
  291. 'book_id' => 1,
  292. 'paragraph' => 1,
  293. 'word_start' => 1,
  294. 'word_end' => 5,
  295. 'content' => 'Chinese translation',
  296. 'channel_uid' => $this->translationChannel->uid,
  297. 'editor_uid' => $this->editorUid,
  298. ]);
  299. $this->sentenceService->save([
  300. 'book_id' => 1,
  301. 'paragraph' => 1,
  302. 'word_start' => 1,
  303. 'word_end' => 5,
  304. 'content' => 'English translation',
  305. 'channel_uid' => $secondTranslation->uid,
  306. 'editor_uid' => $this->editorUid,
  307. ]);
  308. // 执行导出
  309. $service = new PacketService(
  310. $this->paliChannel->uid,
  311. [
  312. $this->translationChannel->uid,
  313. $secondTranslation->uid
  314. ]
  315. );
  316. $zipPath = $service->export();
  317. $fullPath = Storage::path($zipPath);
  318. // 检查ZIP包含两个文件
  319. $zip = new ZipArchive();
  320. $zip->open($fullPath);
  321. $this->assertNotFalse($zip->locateName('translations/Chinese Translation.jsonl'));
  322. $this->assertNotFalse($zip->locateName('translations/English Translation.jsonl'));
  323. $zip->close();
  324. }
  325. /**
  326. * 测试channel不存在时使用uid作为文件名
  327. *
  328. * @return void
  329. */
  330. public function test_uses_uid_when_channel_not_found(): void
  331. {
  332. // 创建测试数据,使用不存在的channel_uid
  333. $nonExistentUid = 'non-existent-uid';
  334. $this->sentenceService->save([
  335. 'book_id' => 1,
  336. 'paragraph' => 1,
  337. 'word_start' => 1,
  338. 'word_end' => 5,
  339. 'content' => 'Pali text',
  340. 'channel_uid' => $this->paliChannel->uid,
  341. 'editor_uid' => $this->editorUid,
  342. ]);
  343. $this->sentenceService->save([
  344. 'book_id' => 1,
  345. 'paragraph' => 1,
  346. 'word_start' => 1,
  347. 'word_end' => 5,
  348. 'content' => 'Translation',
  349. 'channel_uid' => $nonExistentUid,
  350. 'editor_uid' => $this->editorUid,
  351. ]);
  352. // 执行导出
  353. $service = new PacketService(
  354. $this->paliChannel->uid,
  355. [$nonExistentUid]
  356. );
  357. $zipPath = $service->export();
  358. $fullPath = Storage::path($zipPath);
  359. // 检查使用uid作为文件名
  360. $zip = new ZipArchive();
  361. $zip->open($fullPath);
  362. $expectedFile = "translations/{$nonExistentUid}.jsonl";
  363. $this->assertNotFalse($zip->locateName($expectedFile));
  364. $zip->close();
  365. }
  366. /**
  367. * 测试临时文件清理
  368. *
  369. * @return void
  370. */
  371. public function test_cleanup_temp_files(): void
  372. {
  373. // 创建测试数据
  374. $this->createTestSentences();
  375. // 执行导出
  376. $service = new PacketService(
  377. $this->paliChannel->uid,
  378. [$this->translationChannel->uid]
  379. );
  380. $service->export();
  381. // 检查临时目录已被清理
  382. $tempPath = storage_path('app/temp/packet');
  383. $this->assertDirectoryDoesNotExist($tempPath);
  384. }
  385. /**
  386. * 创建基础测试数据
  387. *
  388. * @return void
  389. */
  390. private function createTestSentences(): void
  391. {
  392. $this->sentenceService->save([
  393. 'book_id' => 1,
  394. 'paragraph' => 1,
  395. 'word_start' => 1,
  396. 'word_end' => 5,
  397. 'content' => 'Pali sentence content',
  398. 'channel_uid' => $this->paliChannel->uid,
  399. 'editor_uid' => $this->editorUid,
  400. ]);
  401. $this->sentenceService->save([
  402. 'book_id' => 1,
  403. 'paragraph' => 1,
  404. 'word_start' => 1,
  405. 'word_end' => 5,
  406. 'content' => 'Chinese translation content',
  407. 'channel_uid' => $this->translationChannel->uid,
  408. 'editor_uid' => $this->editorUid,
  409. ]);
  410. }
  411. }