File size: 1,714 Bytes
1a6d0d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { describe, expect, it } from "vitest";
import { allocatePageExcerpts } from "./pageExcerptAllocation";

describe("allocatePageExcerpts", () => {
  it("keeps every page whole when the budget is generous", () => {
    const contents = ["short page", "another short page"];

    expect(allocatePageExcerpts(contents, 1000)).toEqual(contents);
  });

  it("keeps the empty slots of results without page content", () => {
    expect(allocatePageExcerpts(["", "content", ""], 1000)).toEqual([
      "",
      "content",
      "",
    ]);
  });

  it("lets a short page keep its text while a long one is trimmed", () => {
    const short = "a short page.";
    const long = "a much longer page. ".repeat(200);

    const [firstExcerpt, secondExcerpt] = allocatePageExcerpts(
      [long, short],
      120,
    );

    expect(secondExcerpt).toBe(short);
    expect(firstExcerpt.endsWith("…")).toBe(true);
    expect(firstExcerpt.length).toBeLessThan(long.length);
  });

  it("rolls a short page's leftover budget over to the longer ones", () => {
    const tiny = "tiny.";
    const long = "a much longer page. ".repeat(200);

    const [tinyExcerpt, firstLong, secondLong] = allocatePageExcerpts(
      [tiny, long, long],
      120,
    );

    expect(tinyExcerpt).toBe(tiny);
    expect(firstLong.endsWith("…")).toBe(true);
    expect(secondLong.endsWith("…")).toBe(true);
    // The two long pages split what the tiny one did not need, evenly.
    expect(Math.abs(firstLong.length - secondLong.length)).toBeLessThan(20);
  });

  it("returns nothing when there is no budget left", () => {
    expect(allocatePageExcerpts(["content", "more content"], 0)).toEqual([
      "",
      "",
    ]);
  });
});