oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "strings"
5 "testing"
6 "unicode/utf8"
7
8 "github.com/carlosframework/docs/internal/export"
9)
10
11// bars is the only arithmetic in the thumbnail path that is not simply
12// reported from the layout, so it is the part worth pinning.
13
14func TestBarsOfNothingIsNothing(t *testing.T) {
15 if got := bars(nil); got != nil {
16 t.Errorf("got %+v, want nil", got)
17 }
18}
19
20func TestABarSitsInsideTheLineItStandsFor(t *testing.T) {
21 line := export.LineBox{Left: 72, Top: 100, Width: 300, Height: 16.8, Size: 12}
22 got := bars([]export.LineBox{line})
23 if len(got) != 1 {
24 t.Fatalf("got %d bars, want 1", len(got))
25 }
26 b := got[0]
27 if b.X != 72 {
28 t.Errorf("X = %d, want the line's own left edge 72", b.X)
29 }
30 if b.W != 300 {
31 t.Errorf("W = %d, want the line's measured width 300", b.W)
32 }
33 // Vertically centred in the line box, and never taller than it —
34 // a bar that overflowed its line would overlap its neighbour and
35 // a page of text would read as one solid block.
36 if float64(b.Y) < line.Top-1 || float64(b.Y+b.H) > line.Top+line.Height+1 {
37 t.Errorf("bar [%d, %d] is not inside the line box [%.1f, %.1f]",
38 b.Y, b.Y+b.H, line.Top, line.Top+line.Height)
39 }
40}
41
42// A heading draws heavier than body text, and it does so because the
43// type really is bigger — this is what carries the distinction without
44// colour (WCAG 2.2, and AGENTS.md's "colour never carries meaning
45// alone").
46func TestAHeadingBarIsTallerThanABodyBar(t *testing.T) {
47 got := bars([]export.LineBox{
48 {Left: 72, Top: 100, Width: 200, Height: 25, Size: 19.2},
49 {Left: 72, Top: 130, Width: 200, Height: 16.8, Size: 12},
50 })
51 if len(got) != 2 {
52 t.Fatalf("got %d bars, want 2", len(got))
53 }
54 if got[0].H <= got[1].H {
55 t.Errorf("heading bar %d is not taller than body bar %d", got[0].H, got[1].H)
56 }
57}
58
59// The floor. A bar of literally correct height at this scale is a
60// fraction of a pixel; without a minimum, small type vanishes.
61func TestATinyLineStillDrawsSomething(t *testing.T) {
62 got := bars([]export.LineBox{{Left: 72, Top: 100, Width: 200, Height: 4, Size: 1}})
63 if len(got) != 1 || got[0].H <= 0 {
64 t.Fatalf("a tiny line drew nothing: %+v", got)
65 }
66}
67
68// The cache's whole freshness story is this test: an edit changes
69// updated_at, and a changed updated_at makes the old entry
70// unreachable without anyone having called an invalidate.
71func TestAChangedUpdatedAtMissesTheCache(t *testing.T) {
72 p := newPreviews()
73 want := []previewBar{{X: 1, Y: 2, W: 3, H: 4}}
74 p.put(7, 1000, previewEntry{bars: want})
75
76 if got, ok := p.get(7, 1000); !ok || len(got.bars) != 1 || got.bars[0] != want[0] {
77 t.Errorf("same version did not hit: got %+v ok=%v", got, ok)
78 }
79 if _, ok := p.get(7, 1001); ok {
80 t.Error("a document edited since caching still hit the cache")
81 }
82 if _, ok := p.get(8, 1000); ok {
83 t.Error("a different document hit another document's entry")
84 }
85}
86
87// A cached failure is a hit, not a miss: the answer is known and it is
88// "draw nothing". Without this a document whose log will not replay is
89// laid out again on every single list render.
90func TestACachedFailureIsAHit(t *testing.T) {
91 p := newPreviews()
92 p.put(7, 1000, previewEntry{})
93 got, ok := p.get(7, 1000)
94 if !ok {
95 t.Fatal("a cached failure missed, so it will be retried on every render")
96 }
97 if got.bars != nil || got.excerpt != "" {
98 t.Errorf("a cached failure returned a preview: %+v", got)
99 }
100}
101
102func TestTheCacheIsBounded(t *testing.T) {
103 p := newPreviews()
104 for i := int64(0); i < maxPreviews+10; i++ {
105 p.put(i, 1, previewEntry{bars: []previewBar{{X: 1}}})
106 }
107 p.mu.Lock()
108 n := len(p.by)
109 p.mu.Unlock()
110 if n > maxPreviews {
111 t.Errorf("cache holds %d entries, past its %d bound", n, maxPreviews)
112 }
113}
114
115// Overflowing the bound must not empty it. This is the bug an earlier
116// version had: the map was wiped when full, and combined with
117// thumbnailBudget that left an instance with more documents than the
118// bound permanently cold — everything past the budget drew the empty
119// box on every render, for ever, because each render wiped what the
120// last one warmed.
121func TestOverflowingTheCacheKeepsItWarm(t *testing.T) {
122 p := newPreviews()
123 for i := int64(0); i < maxPreviews*2; i++ {
124 p.put(i, 1, previewEntry{bars: []previewBar{{X: 1}}})
125 }
126 p.mu.Lock()
127 n := len(p.by)
128 p.mu.Unlock()
129 if n < maxPreviews {
130 t.Errorf("cache holds only %d entries after overflowing — it was emptied rather than trimmed", n)
131 }
132}
133
134// Re-caching the same document must not evict anything: a list that
135// re-renders the same rows would otherwise churn the map for no reason.
136func TestReplacingAnEntryEvictsNothing(t *testing.T) {
137 p := newPreviews()
138 for i := int64(0); i < maxPreviews; i++ {
139 p.put(i, 1, previewEntry{bars: []previewBar{{X: 1}}})
140 }
141 for i := int64(0); i < maxPreviews; i++ {
142 p.put(i, 2, previewEntry{bars: []previewBar{{X: 2}}})
143 }
144 p.mu.Lock()
145 n := len(p.by)
146 p.mu.Unlock()
147 if n != maxPreviews {
148 t.Errorf("cache holds %d entries after re-caching the same documents, want %d", n, maxPreviews)
149 }
150}
151
152// The line under a row's title is the document's opening words: body
153// text in order, headings skipped, whitespace folded, cut at a word.
154func TestExcerpt(t *testing.T) {
155 text := func(s string) export.Node { return export.Node{Type: "text", Text: s} }
156 block := func(typ string, kids ...export.Node) export.Node { return export.Node{Type: typ, Content: kids} }
157 doc := block("doc",
158 block("heading", text("Early years")),
159 block("paragraph", text("Before 1887, "), text("Millbrook drew its water")),
160 block("bullet_list", block("list_item", block("paragraph", text("from wells")))),
161 )
162 if got, want := excerpt(doc), "Before 1887, Millbrook drew its water from wells"; got != want {
163 t.Errorf("excerpt = %q, want %q", got, want)
164 }
165 if got := excerpt(block("doc", block("heading", text("Only a heading")), block("paragraph"))); got != "" {
166 t.Errorf("a document with no body text has excerpt %q", got)
167 }
168 long := strings.Repeat("word ", 100)
169 got := excerpt(block("doc", block("paragraph", text(long))))
170 if n := utf8.RuneCountInString(got); n > excerptRunes+1 || !strings.HasSuffix(got, "word…") {
171 t.Errorf("a long document's excerpt is %d runes, ending %q", n, got[len(got)-12:])
172 }
173}
174