TipitakaExportV3Test.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. use App\Models\BookTitle;
  3. use App\Models\Channel;
  4. use App\Models\PaliText;
  5. use App\Models\ProgressChapter;
  6. use App\Models\Tag;
  7. use App\Models\TagMap;
  8. use App\Services\V3\TipitakaExportQueue;
  9. use App\Services\V3\TipitakaExportService;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Support\Str;
  12. /*
  13. | 三藏下载 /v3/tipitaka-exports。这里断言的是形状而非契约(spectator 未安装)。
  14. | 生成在测试里同步调 TipitakaExportService::run(),队列换成 mock,不连 RabbitMQ;
  15. | 文件进的是 RedisCacheFake(TestCase 已换好)。
  16. */
  17. uses(RefreshDatabase::class);
  18. function exportPaliText(int $book, int $paragraph, int $level, int $chapterLen, string $toc = ''): PaliText
  19. {
  20. $row = new PaliText;
  21. $row->forceFill([
  22. 'book' => $book, 'paragraph' => $paragraph, 'level' => $level, 'class' => '', 'toc' => $toc,
  23. 'text' => '', 'html' => '', 'pcd_book_id' => 0, 'chapter_len' => $chapterLen, 'chapter_strlen' => 100,
  24. 'uid' => (string) Str::uuid(),
  25. ])->save();
  26. return $row;
  27. }
  28. function exportProgress(string $channel, int $book, int $para, float $progress, ?string $title = null): void
  29. {
  30. (new ProgressChapter)->forceFill([
  31. 'id' => random_int(1, PHP_INT_MAX), 'uid' => (string) Str::uuid(), 'book' => $book, 'para' => $para,
  32. 'lang' => 'zh-Hans', 'channel_id' => $channel, 'progress' => $progress, 'title' => $title,
  33. 'all_trans' => 0, 'public' => 0,
  34. ])->save();
  35. }
  36. /**
  37. * 一部作品 sn 9100 = book 9100 的 3–102 段(两章:4、50),归入长部 › 戒蕴品(分类 3)。
  38. * channel 收录了它,有作品标题、两个章标题和带脚注的正文。
  39. *
  40. * @return array{channel: string, owner: string, sn: int}
  41. */
  42. function exportFixture(): array
  43. {
  44. $root = exportPaliText(9100, 3, 1, 100, 'Sīlakkhandhavaggapāḷi');
  45. exportPaliText(9100, 4, 2, 46, '1. Brahmajālasuttaṃ');
  46. exportPaliText(9100, 50, 2, 53, '2. Sāmaññaphalasuttaṃ');
  47. foreach (['sutta', 'dīghanikāya', 'sīlakkhandhavagga', 'pāḷi'] as $name) {
  48. $tag = Tag::where('name', $name)->first();
  49. if (! $tag) {
  50. $tag = new Tag;
  51. $tag->forceFill(['id' => (string) Str::uuid(), 'name' => $name, 'color' => 0, 'owner_id' => (string) Str::uuid()])->save();
  52. }
  53. (new TagMap)->forceFill(['id' => (string) Str::uuid(), 'table_name' => 'pali_texts', 'anchor_id' => $root->uid, 'tag_id' => $tag->id])->save();
  54. }
  55. (new BookTitle)->forceFill(['sn' => 9100, 'book' => 9100, 'paragraph' => 3, 'title' => 'dīghanikāyapāḷi'])->save();
  56. $owner = makeStudio('export-owner');
  57. $channel = makeChannel($owner, 'export channel');
  58. exportProgress($channel, 9100, 3, 0.4, '戒蕴品');
  59. makeSentence($channel, 9100, 3, 1, '戒蕴品');
  60. makeSentence($channel, 9100, 4, 1, '梵网经');
  61. makeSentence($channel, 9100, 5, 1, '如是我闻{{note|第一条注}}。');
  62. makeSentence($channel, 9100, 5, 2, '一时佛住{{note|第二条注}}。');
  63. makeSentence($channel, 9100, 50, 1, '沙门果经');
  64. makeSentence($channel, 9100, 51, 1, '尔时国王{{note|第三条注}}。');
  65. return ['channel' => $channel, 'owner' => $owner, 'sn' => 9100];
  66. }
  67. /**
  68. * 换掉队列,返回收到的 [id, bulk] 列表。
  69. */
  70. function fakeExportQueue(): ArrayObject
  71. {
  72. $pushed = new ArrayObject;
  73. test()->mock(TipitakaExportQueue::class, function ($mock) use ($pushed) {
  74. $mock->shouldReceive('push')->andReturnUsing(function (string $id, bool $bulk) use ($pushed) {
  75. $pushed[] = [$id, $bulk];
  76. });
  77. });
  78. return $pushed;
  79. }
  80. /**
  81. * 发起并同步生成,返回就绪后的 data。
  82. *
  83. * @param array<string, mixed> $body
  84. * @return array<string, mixed>
  85. */
  86. function exportReady(array $body): array
  87. {
  88. $id = test()->postJson('/api/v3/tipitaka-exports', $body)->assertStatus(202)->json('data.id');
  89. app(TipitakaExportService::class)->run($id);
  90. return test()->postJson('/api/v3/tipitaka-exports', $body)->assertOk()->json('data');
  91. }
  92. /**
  93. * zip 字节 → [条目名 => 内容]
  94. *
  95. * @return array<string, string>
  96. */
  97. function unzipExport(string $bytes): array
  98. {
  99. $path = tempnam(sys_get_temp_dir(), 'export');
  100. file_put_contents($path, $bytes);
  101. $zip = new ZipArchive;
  102. $zip->open($path);
  103. $files = [];
  104. for ($i = 0; $i < $zip->numFiles; $i++) {
  105. $files[$zip->getNameIndex($i)] = $zip->getFromIndex($i);
  106. }
  107. $zip->close();
  108. unlink($path);
  109. return $files;
  110. }
  111. it('queues a work export and does not queue the same one twice', function () {
  112. $pushed = fakeExportQueue();
  113. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  114. $response = $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])
  115. ->assertStatus(202)
  116. ->assertHeader('Retry-After', '2');
  117. expect($response->json('data'))->toMatchArray([
  118. 'status' => 'queued', 'format' => 'html', 'title' => '戒蕴品', 'works' => 1, 'size' => null, 'download_url' => null,
  119. ])->and($response->json('data.id'))->toMatch('/^[0-9a-f]{40}$/')
  120. ->and($response->json('data.filename'))->toBe('戒蕴品-export channel-html.zip');
  121. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])->assertStatus(202);
  122. expect($pushed->getArrayCopy())->toBe([[$response->json('data.id'), false]]);
  123. });
  124. it('generates an html work export and serves it through the signed link', function () {
  125. fakeExportQueue();
  126. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  127. $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'html']);
  128. expect($data['status'])->toBe('ready')
  129. ->and($data['size'])->toBeInt()->toBeGreaterThan(0)
  130. ->and($data['download_url'])->toStartWith("/api/v3/tipitaka-exports/{$data['id']}/file?expires=");
  131. $this->getJson("/api/v3/tipitaka-exports/{$data['id']}")->assertOk()->assertJsonPath('data.status', 'ready');
  132. $file = $this->get($data['download_url'])
  133. ->assertOk()
  134. ->assertHeader('Content-Type', 'application/zip')
  135. ->assertHeader('ETag', '"'.$data['id'].'"')
  136. ->assertHeader('Accept-Ranges', 'bytes');
  137. expect($file->headers->get('Content-Disposition'))->toContain("filename*=utf-8''");
  138. $bytes = $file->streamedContent();
  139. expect(strlen($bytes))->toBe($data['size']);
  140. $files = unzipExport($bytes);
  141. expect(array_keys($files))->toBe(['戒蕴品.html']);
  142. expect($files['戒蕴品.html'])
  143. ->toContain('<!doctype html>')
  144. ->toContain('梵网经')
  145. ->toContain('如是我闻')
  146. ->toContain('href="#para-4"');
  147. // 断点续传与条件请求
  148. $part = $this->get($data['download_url'], ['Range' => 'bytes=10-19'])->assertStatus(206)
  149. ->assertHeader('Content-Range', "bytes 10-19/{$data['size']}");
  150. expect($part->streamedContent())->toBe(substr($bytes, 10, 10));
  151. $this->get($data['download_url'], ['Range' => 'bytes=999999999-'])->assertStatus(416);
  152. $this->get($data['download_url'], ['If-None-Match' => '"'.$data['id'].'"'])->assertStatus(304);
  153. });
  154. it('rejects the file link without a valid signature', function () {
  155. fakeExportQueue();
  156. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  157. $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'html']);
  158. $this->get("/api/v3/tipitaka-exports/{$data['id']}/file")->assertForbidden();
  159. $this->get($data['download_url'].'0')->assertForbidden();
  160. });
  161. it('splits markdown by chapter and renumbers footnotes per file', function () {
  162. fakeExportQueue();
  163. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  164. $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'markdown']);
  165. $files = unzipExport($this->get($data['download_url'])->streamedContent());
  166. expect(array_keys($files))->toEqualCanonicalizing(['戒蕴品/01-梵网经.md', '戒蕴品/02-沙门果经.md', '戒蕴品/README.md']);
  167. expect($files['戒蕴品/README.md'])->toContain('# 戒蕴品')->toContain('(<01-梵网经.md>)');
  168. // 两句各自从 [^1] 起编号,合进一章后是 1、2
  169. expect($files['戒蕴品/01-梵网经.md'])
  170. ->toStartWith("# 梵网经\n")
  171. ->toContain('如是我闻[^1]')
  172. ->toContain('一时佛住[^2]')
  173. ->toContain('[^1]: 第一条注')
  174. ->toContain('[^2]: 第二条注');
  175. expect($files['戒蕴品/02-沙门果经.md'])->toContain('尔时国王[^1]')->toContain('[^1]: 第三条注');
  176. });
  177. it('gives single-work epub and docx as the file itself', function (string $format, string $type, string $magic) {
  178. fakeExportQueue();
  179. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  180. $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => $format]);
  181. expect($data['filename'])->toBe("戒蕴品-export channel.{$format}")
  182. ->and($data['content_type'])->toBe($type);
  183. $bytes = $this->get($data['download_url'])->assertOk()->assertHeader('Content-Type', $type)->streamedContent();
  184. // epub 与 docx 本身都是 zip 容器
  185. expect(substr($bytes, 0, 2))->toBe($magic);
  186. })->with([
  187. 'epub' => ['epub', 'application/epub+zip', 'PK'],
  188. 'docx' => ['docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'PK'],
  189. ])->skip(fn () => trim((string) shell_exec('command -v pandoc')) === '', 'pandoc 不可用');
  190. it('builds the mobile offline package as gzip-encoded ndjson, only per work', function () {
  191. fakeExportQueue();
  192. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  193. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'format' => 'offline'])
  194. ->assertStatus(422)
  195. ->assertJsonValidationErrors('format');
  196. $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'offline']);
  197. expect($data['content_type'])->toBe('application/x-ndjson')
  198. ->and($data['filename'])->toBe('戒蕴品-export channel.ndjson');
  199. $file = $this->get($data['download_url'])
  200. ->assertHeader('Content-Type', 'application/x-ndjson')
  201. ->assertHeader('Content-Encoding', 'gzip');
  202. $lines = array_map(fn ($l) => json_decode($l, true), explode("\n", trim(gzdecode($file->streamedContent()))));
  203. expect($lines[0])->toMatchArray(['type' => 'header', 'format' => 1, 'id' => $data['id'], 'channel' => $channel, 'sn' => $sn, 'book' => 9100, 'para' => 3, 'paragraphs' => 5])
  204. ->and(end($lines))->toBe(['type' => 'end', 'count' => 5])
  205. ->and($lines[1])->toMatchArray(['type' => 'para', 'book' => 9100, 'para' => 3]);
  206. // 与阅读接口的 display 一字不差
  207. $display = $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9100&para=5")->json('data.0.display');
  208. expect(collect($lines)->firstWhere('para', 5)['html'])->toBe($display);
  209. });
  210. it('bundles the whole channel with an index page on the bulk queue', function () {
  211. $pushed = fakeExportQueue();
  212. ['channel' => $channel] = exportFixture();
  213. $data = exportReady(['channel' => $channel, 'format' => 'html']);
  214. expect($pushed[0][1])->toBeTrue()
  215. ->and($data['filename'])->toBe('export channel-html.zip');
  216. $files = unzipExport($this->get($data['download_url'])->streamedContent());
  217. expect(array_keys($files))->toEqualCanonicalizing(['export channel-html/index.html', 'export channel-html/works/9100-戒蕴品.html']);
  218. expect($files['export channel-html/index.html'])->toContain('href="works/9100-%E6%88%92%E8%95%B4%E5%93%81.html"')->toContain('data-q');
  219. expect($files['export channel-html/works/9100-戒蕴品.html'])->toContain('href="../index.html"');
  220. });
  221. it('keeps private channels to people who can read them', function () {
  222. $pushed = fakeExportQueue();
  223. ['channel' => $channel, 'owner' => $owner, 'sn' => $sn] = exportFixture();
  224. Channel::where('uid', $channel)->update(['status' => 10]);
  225. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])->assertForbidden();
  226. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'], authHeader(makeStudio('stranger')))->assertForbidden();
  227. $id = $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'], authHeader($owner))
  228. ->assertStatus(202)->json('data.id');
  229. // 状态也只给看得见的人
  230. $this->getJson("/api/v3/tipitaka-exports/{$id}")->assertForbidden();
  231. $this->getJson("/api/v3/tipitaka-exports/{$id}", authHeader($owner))->assertOk();
  232. expect($pushed)->toHaveCount(1);
  233. });
  234. it('validates the request', function () {
  235. fakeExportQueue();
  236. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  237. $this->postJson('/api/v3/tipitaka-exports', [])->assertStatus(422)->assertJsonValidationErrors(['channel', 'format']);
  238. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'format' => 'pdf'])->assertStatus(422)->assertJsonValidationErrors('format');
  239. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'category' => 3, 'format' => 'html'])
  240. ->assertStatus(422)->assertJsonValidationErrors('work');
  241. // 没有收录的作品、不存在的 channel
  242. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => 1, 'format' => 'html'])->assertNotFound();
  243. $this->postJson('/api/v3/tipitaka-exports', ['channel' => (string) Str::uuid(), 'work' => $sn, 'format' => 'html'])->assertNotFound();
  244. $this->getJson('/api/v3/tipitaka-exports/'.str_repeat('a', 40))->assertNotFound();
  245. });
  246. it('refuses selections larger than the limit', function () {
  247. $pushed = fakeExportQueue();
  248. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  249. config(['mint.tipitaka_export.max_source_chars' => 5]);
  250. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])
  251. ->assertStatus(422)
  252. ->assertJsonPath('type', fn ($type) => str_ends_with($type, 'export-too-large'));
  253. expect($pushed)->toHaveCount(0);
  254. });
  255. it('marks the export failed when the file is too big, and retries on the next request', function () {
  256. $pushed = fakeExportQueue();
  257. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  258. config(['mint.tipitaka_export.max_file_bytes' => 10]);
  259. $id = $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])->json('data.id');
  260. app(TipitakaExportService::class)->run($id);
  261. $this->getJson("/api/v3/tipitaka-exports/{$id}")->assertOk()->assertJsonPath('data.status', 'failed');
  262. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])
  263. ->assertStatus(202)->assertJsonPath('data.status', 'queued');
  264. expect($pushed)->toHaveCount(2);
  265. });
  266. it('shows the library download pages', function () {
  267. fakeExportQueue();
  268. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  269. exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'epub']);
  270. $this->get("/library/tipitaka/channels/{$channel}/work/{$sn}")
  271. ->assertOk()
  272. ->assertSee("/library/tipitaka/channels/{$channel}/work/{$sn}/download", false);
  273. $page = $this->get("/library/tipitaka/channels/{$channel}/work/{$sn}/download")->assertOk();
  274. $page->assertSee('data-format="epub"', false)
  275. ->assertSee('data-format="markdown"', false)
  276. ->assertDontSee('data-format="offline"', false)
  277. ->assertSee('/api/v3/tipitaka-exports/', false);
  278. expect(substr_count($page->getContent(), 'data-state="ready"'))->toBe(1);
  279. $this->get("/library/tipitaka/channels/{$channel}/download")->assertOk()->assertSee('戒蕴品');
  280. // 打包只有「全部」,没有按藏 / 分类的切换
  281. $this->get("/library/tipitaka/channels/{$channel}/download")->assertDontSee('tipitaka-pill', false);
  282. $this->get("/library/tipitaka/channels/{$channel}/work/1/download")->assertNotFound();
  283. })->skip(fn () => ! file_exists(public_path('build/manifest.json')), '需要先 npm run build');
  284. it('finds the work by book and paragraph for clients that only know coordinates', function () {
  285. fakeExportQueue();
  286. ['channel' => $channel, 'sn' => $sn] = exportFixture();
  287. $bySn = $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'offline'])->json('data.id');
  288. // 作品里的任一段都指向同一份(id 相同)
  289. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'book' => 9100, 'para' => 51, 'format' => 'offline'])
  290. ->assertStatus(202)
  291. ->assertJsonPath('data.id', $bySn);
  292. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'book' => 9100, 'format' => 'offline'])
  293. ->assertStatus(422)->assertJsonValidationErrors('para');
  294. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'book' => 9100, 'para' => 3, 'work' => $sn, 'format' => 'offline'])
  295. ->assertStatus(422)->assertJsonValidationErrors('work');
  296. // 不在任何作品里的段
  297. $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'book' => 9100, 'para' => 999, 'format' => 'offline'])->assertNotFound();
  298. });