26_tipitaka_exports.php 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. use \WikipaliApi\Api\TipitakaExportsApi;
  3. use \WikipaliApi\Model\PostApiV3TipitakaExportsRequest;
  4. /*
  5. | 三藏下载 /v3/tipitaka-exports:发起 → 轮询 → 签名链接下载。
  6. | 发起是公开的(公开 channel 不用登录);生成要服务器上的 worker(mq:export.tipitaka)在跑,
  7. | 60 秒内没做完算失败。生成的文件留在 Redis 里 7 天,同一份再跑直接命中,不累积。
  8. */
  9. test('POST /v3/tipitaka-exports(作品 markdown)→ 200/202,轮询到 ready,签名链接能下载、支持 Range', function () {
  10. $body = new PostApiV3TipitakaExportsRequest([
  11. 'channel' => fixtures()['export_channel'],
  12. 'work' => fixtures()['export_work'],
  13. 'format' => 'markdown',
  14. ]);
  15. $res = request(fn () => api(TipitakaExportsApi::class)->postApiV3TipitakaExportsWithHttpInfo($body));
  16. if (! in_array($res['status'], [200, 202], true)) {
  17. assert_status(202, $res, 'tipitaka-exports POST');
  18. }
  19. $data = $res['body']['data'] ?? [];
  20. assert_has_key('id', $data, 'tipitaka-exports.data');
  21. assert_eq('markdown', $data['format'] ?? null, 'tipitaka-exports.data.format');
  22. $deadline = time() + 60;
  23. while (($data['status'] ?? null) !== 'ready') {
  24. if (($data['status'] ?? null) === 'failed') {
  25. fail('tipitaka-exports 生成失败:' . ($data['message'] ?? ''));
  26. }
  27. if (time() > $deadline) {
  28. fail('tipitaka-exports 60 秒内没有生成好(worker mq:export.tipitaka 在跑吗?)status=' . ($data['status'] ?? '?'));
  29. }
  30. sleep(2);
  31. $poll = request(fn () => api(TipitakaExportsApi::class)->getApiV3TipitakaExportsExportWithHttpInfo($data['id']));
  32. assert_status(200, $poll, 'tipitaka-exports GET');
  33. $data = $poll['body']['data'];
  34. }
  35. if (! str_starts_with((string) ($data['download_url'] ?? ''), '/api/v3/tipitaka-exports/')) {
  36. fail('tipitaka-exports.download_url 应是相对地址,实际 ' . ($data['download_url'] ?? 'null'));
  37. }
  38. // download_url 已带 /api 前缀,base_url 也带,去掉一层
  39. $path = substr($data['download_url'], strlen('/api'));
  40. $file = raw('GET', $path);
  41. assert_status(200, $file, 'tipitaka-exports file');
  42. assert_eq('application/zip', $file['headers']['content-type'][0] ?? null, 'tipitaka-exports file content-type');
  43. assert_eq((string) $data['size'], $file['headers']['content-length'][0] ?? null, 'tipitaka-exports file content-length');
  44. $part = raw('GET', $path, null, null, null, ['Range' => 'bytes=0-1']);
  45. assert_status(206, $part, 'tipitaka-exports file Range');
  46. assert_eq('bytes 0-1/' . $data['size'], $part['headers']['content-range'][0] ?? null, 'tipitaka-exports Content-Range');
  47. });
  48. test('GET /v3/tipitaka-exports/{id}/file 没有签名 → 403', function () {
  49. $res = raw('GET', '/v3/tipitaka-exports/' . str_repeat('0', 40) . '/file');
  50. assert_status(403, $res, 'tipitaka-exports file 无签名');
  51. });
  52. test('GET /v3/tipitaka-exports/{不存在} → 404', function () {
  53. $res = raw('GET', '/v3/tipitaka-exports/' . str_repeat('0', 40));
  54. assert_status(404, $res, 'tipitaka-exports 不存在');
  55. assert_problem($res, 'tipitaka-exports 不存在');
  56. });
  57. test('POST /v3/tipitaka-exports 缺 format → 422', function () {
  58. $res = raw('POST', '/v3/tipitaka-exports', null, ['channel' => fixtures()['export_channel']]);
  59. assert_status(422, $res, 'tipitaka-exports 缺 format');
  60. assert_field_error('format', $res, 'tipitaka-exports 缺 format');
  61. });
  62. test('POST /v3/tipitaka-exports 离线包不按作品 → 422', function () {
  63. $res = raw('POST', '/v3/tipitaka-exports', null, ['channel' => fixtures()['export_channel'], 'format' => 'offline']);
  64. assert_status(422, $res, 'tipitaka-exports offline 无 work');
  65. assert_field_error('format', $res, 'tipitaka-exports offline 无 work');
  66. });
  67. test('POST /v3/tipitaka-exports 没收录的作品 → 404', function () {
  68. $res = raw('POST', '/v3/tipitaka-exports', null, ['channel' => fixtures()['export_channel'], 'work' => 999999, 'format' => 'html']);
  69. assert_status(404, $res, 'tipitaka-exports 未收录作品');
  70. assert_problem($res, 'tipitaka-exports 未收录作品');
  71. });