| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- use App\Models\BookTitle;
- use App\Models\Channel;
- use App\Models\PaliText;
- use App\Models\ProgressChapter;
- use App\Models\Tag;
- use App\Models\TagMap;
- use App\Services\V3\TipitakaExportQueue;
- use App\Services\V3\TipitakaExportService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Str;
- /*
- | 三藏下载 /v3/tipitaka-exports。这里断言的是形状而非契约(spectator 未安装)。
- | 生成在测试里同步调 TipitakaExportService::run(),队列换成 mock,不连 RabbitMQ;
- | 文件进的是 RedisCacheFake(TestCase 已换好)。
- */
- uses(RefreshDatabase::class);
- function exportPaliText(int $book, int $paragraph, int $level, int $chapterLen, string $toc = ''): PaliText
- {
- $row = new PaliText;
- $row->forceFill([
- 'book' => $book, 'paragraph' => $paragraph, 'level' => $level, 'class' => '', 'toc' => $toc,
- 'text' => '', 'html' => '', 'pcd_book_id' => 0, 'chapter_len' => $chapterLen, 'chapter_strlen' => 100,
- 'uid' => (string) Str::uuid(),
- ])->save();
- return $row;
- }
- function exportProgress(string $channel, int $book, int $para, float $progress, ?string $title = null): void
- {
- (new ProgressChapter)->forceFill([
- 'id' => random_int(1, PHP_INT_MAX), 'uid' => (string) Str::uuid(), 'book' => $book, 'para' => $para,
- 'lang' => 'zh-Hans', 'channel_id' => $channel, 'progress' => $progress, 'title' => $title,
- 'all_trans' => 0, 'public' => 0,
- ])->save();
- }
- /**
- * 一部作品 sn 9100 = book 9100 的 3–102 段(两章:4、50),归入长部 › 戒蕴品(分类 3)。
- * channel 收录了它,有作品标题、两个章标题和带脚注的正文。
- *
- * @return array{channel: string, owner: string, sn: int}
- */
- function exportFixture(): array
- {
- $root = exportPaliText(9100, 3, 1, 100, 'Sīlakkhandhavaggapāḷi');
- exportPaliText(9100, 4, 2, 46, '1. Brahmajālasuttaṃ');
- exportPaliText(9100, 50, 2, 53, '2. Sāmaññaphalasuttaṃ');
- foreach (['sutta', 'dīghanikāya', 'sīlakkhandhavagga', 'pāḷi'] as $name) {
- $tag = Tag::where('name', $name)->first();
- if (! $tag) {
- $tag = new Tag;
- $tag->forceFill(['id' => (string) Str::uuid(), 'name' => $name, 'color' => 0, 'owner_id' => (string) Str::uuid()])->save();
- }
- (new TagMap)->forceFill(['id' => (string) Str::uuid(), 'table_name' => 'pali_texts', 'anchor_id' => $root->uid, 'tag_id' => $tag->id])->save();
- }
- (new BookTitle)->forceFill(['sn' => 9100, 'book' => 9100, 'paragraph' => 3, 'title' => 'dīghanikāyapāḷi'])->save();
- $owner = makeStudio('export-owner');
- $channel = makeChannel($owner, 'export channel');
- exportProgress($channel, 9100, 3, 0.4, '戒蕴品');
- makeSentence($channel, 9100, 3, 1, '戒蕴品');
- makeSentence($channel, 9100, 4, 1, '梵网经');
- makeSentence($channel, 9100, 5, 1, '如是我闻{{note|第一条注}}。');
- makeSentence($channel, 9100, 5, 2, '一时佛住{{note|第二条注}}。');
- makeSentence($channel, 9100, 50, 1, '沙门果经');
- makeSentence($channel, 9100, 51, 1, '尔时国王{{note|第三条注}}。');
- return ['channel' => $channel, 'owner' => $owner, 'sn' => 9100];
- }
- /**
- * 换掉队列,返回收到的 [id, bulk] 列表。
- */
- function fakeExportQueue(): ArrayObject
- {
- $pushed = new ArrayObject;
- test()->mock(TipitakaExportQueue::class, function ($mock) use ($pushed) {
- $mock->shouldReceive('push')->andReturnUsing(function (string $id, bool $bulk) use ($pushed) {
- $pushed[] = [$id, $bulk];
- });
- });
- return $pushed;
- }
- /**
- * 发起并同步生成,返回就绪后的 data。
- *
- * @param array<string, mixed> $body
- * @return array<string, mixed>
- */
- function exportReady(array $body): array
- {
- $id = test()->postJson('/api/v3/tipitaka-exports', $body)->assertStatus(202)->json('data.id');
- app(TipitakaExportService::class)->run($id);
- return test()->postJson('/api/v3/tipitaka-exports', $body)->assertOk()->json('data');
- }
- /**
- * zip 字节 → [条目名 => 内容]
- *
- * @return array<string, string>
- */
- function unzipExport(string $bytes): array
- {
- $path = tempnam(sys_get_temp_dir(), 'export');
- file_put_contents($path, $bytes);
- $zip = new ZipArchive;
- $zip->open($path);
- $files = [];
- for ($i = 0; $i < $zip->numFiles; $i++) {
- $files[$zip->getNameIndex($i)] = $zip->getFromIndex($i);
- }
- $zip->close();
- unlink($path);
- return $files;
- }
- it('queues a work export and does not queue the same one twice', function () {
- $pushed = fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- $response = $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])
- ->assertStatus(202)
- ->assertHeader('Retry-After', '2');
- expect($response->json('data'))->toMatchArray([
- 'status' => 'queued', 'format' => 'html', 'title' => '戒蕴品', 'works' => 1, 'size' => null, 'download_url' => null,
- ])->and($response->json('data.id'))->toMatch('/^[0-9a-f]{40}$/')
- ->and($response->json('data.filename'))->toBe('戒蕴品-export channel-html.zip');
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])->assertStatus(202);
- expect($pushed->getArrayCopy())->toBe([[$response->json('data.id'), false]]);
- });
- it('generates an html work export and serves it through the signed link', function () {
- fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'html']);
- expect($data['status'])->toBe('ready')
- ->and($data['size'])->toBeInt()->toBeGreaterThan(0)
- ->and($data['download_url'])->toStartWith("/api/v3/tipitaka-exports/{$data['id']}/file?expires=");
- $this->getJson("/api/v3/tipitaka-exports/{$data['id']}")->assertOk()->assertJsonPath('data.status', 'ready');
- $file = $this->get($data['download_url'])
- ->assertOk()
- ->assertHeader('Content-Type', 'application/zip')
- ->assertHeader('ETag', '"'.$data['id'].'"')
- ->assertHeader('Accept-Ranges', 'bytes');
- expect($file->headers->get('Content-Disposition'))->toContain("filename*=utf-8''");
- $bytes = $file->streamedContent();
- expect(strlen($bytes))->toBe($data['size']);
- $files = unzipExport($bytes);
- expect(array_keys($files))->toBe(['戒蕴品.html']);
- expect($files['戒蕴品.html'])
- ->toContain('<!doctype html>')
- ->toContain('梵网经')
- ->toContain('如是我闻')
- ->toContain('href="#para-4"');
- // 断点续传与条件请求
- $part = $this->get($data['download_url'], ['Range' => 'bytes=10-19'])->assertStatus(206)
- ->assertHeader('Content-Range', "bytes 10-19/{$data['size']}");
- expect($part->streamedContent())->toBe(substr($bytes, 10, 10));
- $this->get($data['download_url'], ['Range' => 'bytes=999999999-'])->assertStatus(416);
- $this->get($data['download_url'], ['If-None-Match' => '"'.$data['id'].'"'])->assertStatus(304);
- });
- it('rejects the file link without a valid signature', function () {
- fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'html']);
- $this->get("/api/v3/tipitaka-exports/{$data['id']}/file")->assertForbidden();
- $this->get($data['download_url'].'0')->assertForbidden();
- });
- it('splits markdown by chapter and renumbers footnotes per file', function () {
- fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'markdown']);
- $files = unzipExport($this->get($data['download_url'])->streamedContent());
- expect(array_keys($files))->toEqualCanonicalizing(['戒蕴品/01-梵网经.md', '戒蕴品/02-沙门果经.md', '戒蕴品/README.md']);
- expect($files['戒蕴品/README.md'])->toContain('# 戒蕴品')->toContain('(<01-梵网经.md>)');
- // 两句各自从 [^1] 起编号,合进一章后是 1、2
- expect($files['戒蕴品/01-梵网经.md'])
- ->toStartWith("# 梵网经\n")
- ->toContain('如是我闻[^1]')
- ->toContain('一时佛住[^2]')
- ->toContain('[^1]: 第一条注')
- ->toContain('[^2]: 第二条注');
- expect($files['戒蕴品/02-沙门果经.md'])->toContain('尔时国王[^1]')->toContain('[^1]: 第三条注');
- });
- it('gives single-work epub and docx as the file itself', function (string $format, string $type, string $magic) {
- fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => $format]);
- expect($data['filename'])->toBe("戒蕴品-export channel.{$format}")
- ->and($data['content_type'])->toBe($type);
- $bytes = $this->get($data['download_url'])->assertOk()->assertHeader('Content-Type', $type)->streamedContent();
- // epub 与 docx 本身都是 zip 容器
- expect(substr($bytes, 0, 2))->toBe($magic);
- })->with([
- 'epub' => ['epub', 'application/epub+zip', 'PK'],
- 'docx' => ['docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'PK'],
- ])->skip(fn () => trim((string) shell_exec('command -v pandoc')) === '', 'pandoc 不可用');
- it('builds the mobile offline package as gzip-encoded ndjson, only per work', function () {
- fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'format' => 'offline'])
- ->assertStatus(422)
- ->assertJsonValidationErrors('format');
- $data = exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'offline']);
- expect($data['content_type'])->toBe('application/x-ndjson')
- ->and($data['filename'])->toBe('戒蕴品-export channel.ndjson');
- $file = $this->get($data['download_url'])
- ->assertHeader('Content-Type', 'application/x-ndjson')
- ->assertHeader('Content-Encoding', 'gzip');
- $lines = array_map(fn ($l) => json_decode($l, true), explode("\n", trim(gzdecode($file->streamedContent()))));
- expect($lines[0])->toMatchArray(['type' => 'header', 'format' => 1, 'id' => $data['id'], 'channel' => $channel, 'sn' => $sn, 'book' => 9100, 'para' => 3, 'paragraphs' => 5])
- ->and(end($lines))->toBe(['type' => 'end', 'count' => 5])
- ->and($lines[1])->toMatchArray(['type' => 'para', 'book' => 9100, 'para' => 3]);
- // 与阅读接口的 display 一字不差
- $display = $this->getJson("/api/v3/tipitaka-reading/{$channel}?book=9100¶=5")->json('data.0.display');
- expect(collect($lines)->firstWhere('para', 5)['html'])->toBe($display);
- });
- it('bundles the whole channel with an index page on the bulk queue', function () {
- $pushed = fakeExportQueue();
- ['channel' => $channel] = exportFixture();
- $data = exportReady(['channel' => $channel, 'format' => 'html']);
- expect($pushed[0][1])->toBeTrue()
- ->and($data['filename'])->toBe('export channel-html.zip');
- $files = unzipExport($this->get($data['download_url'])->streamedContent());
- expect(array_keys($files))->toEqualCanonicalizing(['export channel-html/index.html', 'export channel-html/works/9100-戒蕴品.html']);
- expect($files['export channel-html/index.html'])->toContain('href="works/9100-%E6%88%92%E8%95%B4%E5%93%81.html"')->toContain('data-q');
- expect($files['export channel-html/works/9100-戒蕴品.html'])->toContain('href="../index.html"');
- });
- it('keeps private channels to people who can read them', function () {
- $pushed = fakeExportQueue();
- ['channel' => $channel, 'owner' => $owner, 'sn' => $sn] = exportFixture();
- Channel::where('uid', $channel)->update(['status' => 10]);
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])->assertForbidden();
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'], authHeader(makeStudio('stranger')))->assertForbidden();
- $id = $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'], authHeader($owner))
- ->assertStatus(202)->json('data.id');
- // 状态也只给看得见的人
- $this->getJson("/api/v3/tipitaka-exports/{$id}")->assertForbidden();
- $this->getJson("/api/v3/tipitaka-exports/{$id}", authHeader($owner))->assertOk();
- expect($pushed)->toHaveCount(1);
- });
- it('validates the request', function () {
- fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- $this->postJson('/api/v3/tipitaka-exports', [])->assertStatus(422)->assertJsonValidationErrors(['channel', 'format']);
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'format' => 'pdf'])->assertStatus(422)->assertJsonValidationErrors('format');
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'category' => 3, 'format' => 'html'])
- ->assertStatus(422)->assertJsonValidationErrors('work');
- // 没有收录的作品、不存在的 channel
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => 1, 'format' => 'html'])->assertNotFound();
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => (string) Str::uuid(), 'work' => $sn, 'format' => 'html'])->assertNotFound();
- $this->getJson('/api/v3/tipitaka-exports/'.str_repeat('a', 40))->assertNotFound();
- });
- it('refuses selections larger than the limit', function () {
- $pushed = fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- config(['mint.tipitaka_export.max_source_chars' => 5]);
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])
- ->assertStatus(422)
- ->assertJsonPath('type', fn ($type) => str_ends_with($type, 'export-too-large'));
- expect($pushed)->toHaveCount(0);
- });
- it('marks the export failed when the file is too big, and retries on the next request', function () {
- $pushed = fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- config(['mint.tipitaka_export.max_file_bytes' => 10]);
- $id = $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])->json('data.id');
- app(TipitakaExportService::class)->run($id);
- $this->getJson("/api/v3/tipitaka-exports/{$id}")->assertOk()->assertJsonPath('data.status', 'failed');
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'html'])
- ->assertStatus(202)->assertJsonPath('data.status', 'queued');
- expect($pushed)->toHaveCount(2);
- });
- it('shows the library download pages', function () {
- fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- exportReady(['channel' => $channel, 'work' => $sn, 'format' => 'epub']);
- $this->get("/library/tipitaka/channels/{$channel}/work/{$sn}")
- ->assertOk()
- ->assertSee("/library/tipitaka/channels/{$channel}/work/{$sn}/download", false);
- $page = $this->get("/library/tipitaka/channels/{$channel}/work/{$sn}/download")->assertOk();
- $page->assertSee('data-format="epub"', false)
- ->assertSee('data-format="markdown"', false)
- ->assertDontSee('data-format="offline"', false)
- ->assertSee('/api/v3/tipitaka-exports/', false);
- expect(substr_count($page->getContent(), 'data-state="ready"'))->toBe(1);
- $this->get("/library/tipitaka/channels/{$channel}/download")->assertOk()->assertSee('戒蕴品');
- // 打包只有「全部」,没有按藏 / 分类的切换
- $this->get("/library/tipitaka/channels/{$channel}/download")->assertDontSee('tipitaka-pill', false);
- $this->get("/library/tipitaka/channels/{$channel}/work/1/download")->assertNotFound();
- })->skip(fn () => ! file_exists(public_path('build/manifest.json')), '需要先 npm run build');
- it('finds the work by book and paragraph for clients that only know coordinates', function () {
- fakeExportQueue();
- ['channel' => $channel, 'sn' => $sn] = exportFixture();
- $bySn = $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'work' => $sn, 'format' => 'offline'])->json('data.id');
- // 作品里的任一段都指向同一份(id 相同)
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'book' => 9100, 'para' => 51, 'format' => 'offline'])
- ->assertStatus(202)
- ->assertJsonPath('data.id', $bySn);
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'book' => 9100, 'format' => 'offline'])
- ->assertStatus(422)->assertJsonValidationErrors('para');
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'book' => 9100, 'para' => 3, 'work' => $sn, 'format' => 'offline'])
- ->assertStatus(422)->assertJsonValidationErrors('work');
- // 不在任何作品里的段
- $this->postJson('/api/v3/tipitaka-exports', ['channel' => $channel, 'book' => 9100, 'para' => 999, 'format' => 'offline'])->assertNotFound();
- });
|