RelatedParagraphMapBuilderTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. use App\Services\RelatedParagraph\ParagraphMapBuilder;
  3. /*
  4. * 重建规则(documents/related-paragraphs-rebuild.md §3.1):继承、sn 边界、书名优先级、
  5. * 序言、补丁覆盖、有号没名字清零。
  6. */
  7. /**
  8. * @return array<int, array{0: string, 1: int, 2: int}> 段落号 => [book_name, begin, end]
  9. */
  10. function relatedMap(array $paragraphs, array $markers, array $titles = [1 => 100], array $snNames = [], array $patch = []): array
  11. {
  12. $built = (new ParagraphMapBuilder)->build($paragraphs, $markers, $titles, $snNames, $patch);
  13. return array_map(fn (array $row) => [$row['book_name'], $row['begin'], $row['end']], $built['rows']);
  14. }
  15. it('inherits the last anchor until the next one', function () {
  16. expect(relatedMap([1, 2, 3, 4], [1 => ['dn1'], 2 => ['para1'], 4 => ['para2-3']]))->toBe([
  17. 1 => ['dn1', 0, 0],
  18. 2 => ['dn1', 1, 1],
  19. 3 => ['dn1', 1, 1],
  20. 4 => ['dn1', 2, 3],
  21. ]);
  22. });
  23. it('lets the suffixed twin decide the name', function () {
  24. expect(relatedMap([1], [1 => ['para4', 'para4_dn1']], snNames: [100 => 'abhi1'])[1])->toBe(['dn1', 4, 4]);
  25. });
  26. it('stops inheriting at a book_titles boundary', function () {
  27. $map = relatedMap([1, 2, 3], [1 => ['vin6', 'para75']], titles: [1 => 100, 3 => 101]);
  28. expect($map[2])->toBe(['vin6', 75, 75])
  29. ->and($map[3])->toBe(['vin6', 0, 0]);
  30. });
  31. it('stops inheriting when the book name changes', function () {
  32. $map = relatedMap([1, 2, 3], [1 => ['kn10', 'para196'], 2 => ['kn11']]);
  33. expect($map[2])->toBe(['kn11', 0, 0])
  34. ->and($map[3])->toBe(['kn11', 0, 0]);
  35. });
  36. it('prefers the sn name table over in-file markers for bare anchors', function () {
  37. expect(relatedMap([1], [1 => ['para7']], snNames: [100 => 'abhi3'])[1])->toBe(['abhi3', 7, 7]);
  38. });
  39. it('keeps preface paragraphs under X_A even when the anchor carries the X suffix', function () {
  40. $map = relatedMap([1, 2, 3, 4], [1 => ['mn1_0'], 2 => ['para1', 'para1_mn1'], 3 => ['mn1_1'], 4 => ['para1_mn1']]);
  41. expect($map[2])->toBe(['mn1_A', 1, 1])
  42. ->and($map[3])->toBe(['mn1', 0, 0])
  43. ->and($map[4])->toBe(['mn1', 1, 1]);
  44. });
  45. it('clears numbers that end up without a name', function () {
  46. $built = (new ParagraphMapBuilder)->build([1, 2], [1 => ['para5']], [1 => 100], []);
  47. expect($built['rows'][1])->toMatchArray(['book_name' => '', 'begin' => 0, 'end' => 0, 'cleared' => true])
  48. ->and($built['rows'][2]['cleared'])->toBeTrue();
  49. });
  50. it('overrides and adds paragraphs from the patch table', function () {
  51. $patch = [
  52. 2 => ['book_name' => null, 'begin' => 82, 'end' => 83],
  53. 3 => ['book_name' => 'kn12', 'begin' => 9, 'end' => 9],
  54. ];
  55. expect(relatedMap([1, 2, 3], [1 => ['kn12', 'para81']], titles: [1 => 100, 3 => 101], patch: $patch))->toBe([
  56. 1 => ['kn12', 81, 81],
  57. 2 => ['kn12', 82, 83],
  58. 3 => ['kn12', 9, 9],
  59. ]);
  60. });
  61. it('gives a patched name to a paragraph that would otherwise be cleared', function () {
  62. expect(relatedMap([1], [1 => ['para5']], patch: [1 => ['book_name' => 'vin3', 'begin' => 5, 'end' => 5]])[1])
  63. ->toBe(['vin3', 5, 5]);
  64. });
  65. it('assigns book_id from book_titles and 0 before the first title', function () {
  66. $built = (new ParagraphMapBuilder)->build([1, 2, 5], [], [2 => 7, 5 => 8], []);
  67. expect(array_column($built['rows'], 'book_id'))->toBe([0, 7, 8]);
  68. });