oficina / memoria Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chunk
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestSplitCarriesTheHeadingTrail(t *testing.T) {
9 got := Split("# FY2024\n\n## Revenue\n\n" + strings.Repeat("word ", 60) + "\n\n## Costs\n\n" + strings.Repeat("cost ", 60))
10 if len(got) != 2 {
11 t.Fatalf("want 2 chunks, got %d", len(got))
12 }
13 if got[0].Heading() != "FY2024 › Revenue" {
14 t.Errorf("first trail = %q", got[0].Heading())
15 }
16 if got[1].Heading() != "FY2024 › Costs" {
17 t.Errorf("second trail = %q", got[1].Heading())
18 }
19 // The trail must be in the indexable text, or a query naming the
20 // section cannot find a passage that never repeats it.
21 if !strings.Contains(got[1].Indexable(), "Costs") {
22 t.Error("heading absent from indexable text")
23 }
24}
25
26// The rule that pays for itself: a chunk that straddles a heading cites
27// wrongly whichever section you label it with.
28func TestNoChunkStraddlesAHeading(t *testing.T) {
29 // Two short sections that would happily fit in one chunk by size.
30 got := Split("## Alpha\n\n" + strings.Repeat("a ", 50) + "\n\n## Beta\n\n" + strings.Repeat("b ", 50))
31 for _, c := range got {
32 if strings.Contains(c.Text, "a ") && strings.Contains(c.Text, "b ") {
33 t.Fatalf("chunk %d mixes two sections: %q", c.Ordinal, c.Text[:40])
34 }
35 }
36 if len(got) != 2 {
37 t.Fatalf("want 2 chunks, got %d", len(got))
38 }
39}
40
41func TestLongTextSplitsNearTarget(t *testing.T) {
42 // Twelve paragraphs of 100 words each: 1200 words, target 400.
43 var b strings.Builder
44 for i := 0; i < 12; i++ {
45 b.WriteString(strings.Repeat("filler ", 100))
46 b.WriteString("\n\n")
47 }
48 got := Split(b.String())
49 if len(got) < 2 || len(got) > 4 {
50 t.Fatalf("1200 words at target %d gave %d chunks", TargetWords, len(got))
51 }
52 for _, c := range got {
53 if n := len(strings.Fields(c.Text)); n > TargetWords*2 {
54 t.Errorf("chunk %d has %d words, far over target", c.Ordinal, n)
55 }
56 }
57}
58
59func TestOrdinalsAreDense(t *testing.T) {
60 got := Split("## A\n\n" + strings.Repeat("x ", 500) + "\n\n## B\n\nshort tail")
61 for i, c := range got {
62 if c.Ordinal != i {
63 t.Fatalf("chunk at index %d has ordinal %d; a gap reads as a missing citation", i, c.Ordinal)
64 }
65 }
66}
67
68func TestEmptyInputYieldsNoChunks(t *testing.T) {
69 for _, in := range []string{"", " ", "\n\n\n", "# Heading with no body"} {
70 if got := Split(in); len(got) != 0 {
71 t.Errorf("Split(%q) gave %d chunks, want 0", in, len(got))
72 }
73 }
74}
75
76// A document that starts at "##" with no "#" above it pads the trail, and
77// an unfiltered join renders "› Revenue" — a separator with nothing before
78// it. Every earlier fixture started at "#", so nothing caught this until
79// the page was looked at.
80func TestHeadingDropsEmptyTrailSegments(t *testing.T) {
81 got := Split("## Revenue\n\n" + strings.Repeat("word ", 60))
82 if len(got) != 1 {
83 t.Fatalf("want 1 chunk, got %d", len(got))
84 }
85 if h := got[0].Heading(); h != "Revenue" {
86 t.Errorf("Heading() = %q, want %q", h, "Revenue")
87 }
88 if strings.HasPrefix(got[0].Heading(), "›") || strings.Contains(got[0].Heading(), " › ") {
89 t.Errorf("heading has a dangling separator: %q", got[0].Heading())
90 }
91}
92
93// Deeper skips too: "###" with nothing above it.
94func TestDeepSkipDoesNotDangle(t *testing.T) {
95 got := Split("### Deep\n\n" + strings.Repeat("word ", 60))
96 if len(got) != 1 || got[0].Heading() != "Deep" {
97 t.Fatalf("Heading() = %q, want %q", got[0].Heading(), "Deep")
98 }
99}
100
101// The commonest Markdown style: a heading with no blank line after it.
102// The paragraph-based chunker filed the whole thing as one chunk of body
103// text with an empty trail, so those documents indexed with no heading at
104// all — and the seeded corpus is full of them.
105func TestHeadingWithNoBlankLineAfterIt(t *testing.T) {
106 got := Split("## Revenue\nJune revenue was 1,350,000.\n\n## Costs\nHosting fell after the migration.")
107 if len(got) != 2 {
108 t.Fatalf("want 2 chunks, got %d: %+v", len(got), got)
109 }
110 if got[0].Heading() != "Revenue" || got[1].Heading() != "Costs" {
111 t.Errorf("headings are %q and %q", got[0].Heading(), got[1].Heading())
112 }
113 if strings.Contains(got[0].Text, "##") {
114 t.Errorf("the heading line leaked into the body: %q", got[0].Text)
115 }
116}
117
118// A "#" inside a fenced code block is a shell comment. Treating it as a
119// heading filed everything after it under a trail like "now run it".
120func TestHashInAFencedBlockIsNotAHeading(t *testing.T) {
121 got := Split("# Real heading\n\n```sh\nmake build\n\n# now run it\n\n./app\n```\n\nProse after the block.")
122 for _, c := range got {
123 if strings.Contains(c.Heading(), "now run it") {
124 t.Fatalf("a shell comment became a heading: trail %q", c.Heading())
125 }
126 }
127 if len(got) == 0 || got[0].Heading() != "Real heading" {
128 t.Fatalf("the real heading was lost: %+v", got)
129 }
130}
131
132func TestTildeFencesToo(t *testing.T) {
133 got := Split("# Top\n\n~~~\n# not a heading\n~~~\n\nBody text here.")
134 for _, c := range got {
135 if strings.Contains(c.Heading(), "not a heading") {
136 t.Fatalf("a tilde-fenced comment became a heading: %q", c.Heading())
137 }
138 }
139}
140
141// A document with no blank lines — a CSV, a log, hard-wrapped prose —
142// became one chunk of the whole file, whose single citation read
143// "passage 0" for everything. The form invites .csv, and a CSV never has
144// a blank line.
145func TestNoBlankLinesStillSplits(t *testing.T) {
146 var b strings.Builder
147 for i := 0; i < 4000; i++ {
148 b.WriteString("alpha,beta,gamma,delta,epsilon\n")
149 }
150 got := Split(b.String())
151 if len(got) < 2 {
152 t.Fatalf("a 4000-line CSV produced %d chunk(s); MaxWords is not binding", len(got))
153 }
154 for _, c := range got {
155 if n := len(strings.Fields(c.Text)); n > MaxWords*2 {
156 t.Errorf("chunk %d has %d words, over twice the ceiling", c.Ordinal, n)
157 }
158 }
159}
160
161// No text may be lost. Every word of the input must appear in some chunk.
162func TestSplitLosesNoText(t *testing.T) {
163 in := "# Top\n\nFirst para here.\n## Second\nNo blank line above.\n\n```\ncode # here\n```\n\nLast para."
164 var joined string
165 for _, c := range Split(in) {
166 joined += " " + c.Text
167 }
168 for _, want := range []string{"First para here.", "No blank line above.", "code # here", "Last para."} {
169 if !strings.Contains(joined, want) {
170 t.Errorf("lost %q from the output", want)
171 }
172 }
173}
174