Repository navigation
Expand file tree
/
Copy pathlineup_test.go
More file actions
302 lines (281 loc) · 10 KB
/
Copy pathlineup_test.go
File metadata and controls
302 lines (281 loc) · 10 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"archive/zip"
"bytes"
"math"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
var now = time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)
func country(code string) Place {
k := census().Countries[code]
return Place{Kind: CountryPlace, Name: k.Names[0], Country: code, Population: k.Population}
}
// The arithmetic has to give back what went into it: every nurse in the
// United States, spread across ages and sexes and summed again, is the
// number of nurses the BLS counted.
func TestTheNursesAddUp(t *testing.T) {
a := &Account{Now: now, Facts: []Fact{
{Kind: Lives, Places: []Place{country("US")}},
{Kind: Job, Value: "Registered nurses"},
}}
got := count(a).People
if want := 3528000.0; math.Abs(got-want)/want > 0.02 {
t.Errorf("the count has %.0f nurses in the US, the BLS has %.0f", got, want)
}
}
// An age said once is two years of births: the person may or may not have
// had their birthday since.
func TestAnAgeIsTwoYearsOfPeople(t *testing.T) {
a := &Account{Now: now, Facts: []Fact{
{Kind: Lives, Places: []Place{country("ES")}},
{Kind: Age, Lo: 1990, Hi: 1991},
}}
es := census().Countries["ES"]
got := count(a).People
// Spain has about 700,000 people for each year of age in the thirties.
one := (es.Male[35] + es.Female[35])
if got < 1.5*one || got > 2.5*one {
t.Errorf("two birth years in Spain came to %.0f people, one year of age is %.0f", got, one)
}
}
func TestAgesThatCannotBothBeTrue(t *testing.T) {
a := &Account{Now: now, Facts: []Fact{
{Kind: Age, Lo: 1984, Hi: 1985, When: time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
{Kind: Age, Lo: 1994, Hi: 1995, When: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)},
}}
l := count(a)
if len(l.Conflicts) != 1 {
t.Fatalf("thirty in 2015 and thirty in 2025 gave %d conflicts", len(l.Conflicts))
}
}
func TestGrammarGivesASex(t *testing.T) {
a := &Account{Now: now, Gender: [2]int{0, 5}, Langs: map[string]int{"es": 10}}
l := count(a)
for _, s := range l.Steps {
if s.Kind == Sex && strings.Contains(s.Plain, "woman") && strings.Contains(s.Plain, "grammar") {
return
}
}
t.Errorf("five feminine sentences and nothing else gave %+v", l.Steps)
}
func TestTheBandHoldsTheAnswer(t *testing.T) {
fs, _ := factsIn("en", "I'm 34, I'm a nurse in Leeds and my 5 year old daughter is sick.")
l := count(&Account{Now: now, Facts: fs})
if !(l.Low < l.People && l.People < l.High) {
t.Errorf("%.1f is not between %.1f and %.1f", l.People, l.Low, l.High)
}
}
func TestPlacesThatShareAName(t *testing.T) {
m, ok := places().find("Valencia is lovely", 0)
if !ok {
t.Fatal("Valencia not found")
}
// Spanish alone cannot tell Spain's Valencia from Venezuela's, so the
// answer is both. The hours can.
if got := settle(m.places, settleHint{language: "es"}); len(got) < 2 {
t.Errorf("Valencia written in Spanish, with nothing else, settled on %+v", got)
}
spain := settleHint{language: "es", band: band{known: true, lo: 0, hi: 180}}
if got := settle(m.places, spain); len(got) != 1 || got[0].Country != "ES" {
t.Errorf("Valencia written in Spanish at Spanish hours settled on %+v", got)
}
m, _ = places().find("London calling", 0)
if got := settle(m.places, settleHint{}); len(got) != 1 || got[0].Country != "GB" {
t.Errorf("London settled on %+v", got)
}
m, _ = places().find("Columbus", 0)
if got := settle(m.places, settleHint{regions: map[string]bool{"US.OH": true}}); len(got) != 1 || got[0].Region != "US.OH" {
t.Errorf("Columbus in a post that mentioned Ohio settled on %+v", got)
}
}
func TestPlaceSpellings(t *testing.T) {
for text, country := range map[string]string{
"Malaga": "ES",
"Málaga": "ES",
"St. Louis, Missouri": "US",
"the Netherlands": "NL",
"the States": "US",
"NYC": "US",
"EE. UU.": "US",
"Austin, TX": "US",
"Mallorca": "ES",
} {
m, ok := places().find(text, 0)
if !ok {
t.Errorf("%q was not found", text)
continue
}
if m.places[0].Country != country {
t.Errorf("%q found in %s", text, m.places[0].Country)
}
}
// A town called Surprise exists. The word does not make one.
if m, ok := places().find("surprise party", 0); ok {
t.Errorf("a lowercase word found %+v", m.places)
}
}
func TestReadingTheClock(t *testing.T) {
var posts []Post
for i := range 240 {
// Awake from seven in the morning to eleven at night, UTC.
hour := 7 + i%16
posts = append(posts, Post{When: time.Date(2025, 1, 1+i%28, hour, 0, 0, 0, time.UTC)})
}
b := readClock(posts)
if !b.known || !b.covers(0) || !b.covers(60) || b.covers(-300) {
t.Errorf("a London day read as %+v", b)
}
if readClock(posts[:30]).known {
t.Error("thirty posts should be too few to read a night from")
}
}
func TestPeopleAreRounded(t *testing.T) {
for n, want := range map[float64]string{
789431: "790,000", 12: "12", 0.3: "fewer than one", 8.19e9: "8,200,000,000", 1234: "1,200",
} {
if got := people(n); got != want {
t.Errorf("%v printed as %q, want %q", n, got, want)
}
}
}
func TestColumnsCountLetters(t *testing.T) {
a := columns("Málaga", "x")[0]
b := columns("Malaga", "x")[0]
if runes(a) != runes(b) {
t.Errorf("an accent moved the column: %q and %q", a, b)
}
}
func write(t *testing.T, dir, name, body string) string {
t.Helper()
p := filepath.Join(dir, name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
return p
}
func TestReadingReddit(t *testing.T) {
dir := t.TempDir()
write(t, dir, "comments.csv", string(rune(0xfeff))+"id,permalink,date,ip,subreddit,gildings,link,parent,body,media\n"+
"a1,,2024-02-03 10:11:12 UTC,,x,0,,,\"I'm a nurse,\nand this is a second line\",\n"+
"a2,,2024-02-04 10:11:12 UTC,,x,0,,,[deleted],\n")
write(t, dir, "messages.csv", "id,body\n1,something private\n")
in := readInputs([]string{dir})
if len(in.Posts) != 1 {
t.Fatalf("read %d posts, want 1: %+v", len(in.Posts), in.Posts)
}
if !strings.Contains(in.Posts[0].Text, "second line") || in.Posts[0].When.Day() != 3 {
t.Errorf("the post came out as %+v", in.Posts[0])
}
if _, skipped := in.Skipped["messages.csv"]; !skipped {
t.Error("private messages were not left alone")
}
}
func TestReadingAnXArchiveInAZip(t *testing.T) {
var buf bytes.Buffer
z := zip.NewWriter(&buf)
add := func(name, body string) {
w, err := z.Create(name)
if err != nil {
t.Fatal(err)
}
w.Write([]byte(body))
}
add("data/tweets.js", `window.YTD.tweets.part0 = [
{"tweet": {"created_at": "Wed Oct 10 20:19:24 +0000 2018", "full_text": "Fish & chips again"}},
{"tweet": {"created_at": "Wed Oct 10 21:19:24 +0000 2018", "full_text": "RT @someone: I'm a doctor"}}
]`)
add("data/profile.js", `window.YTD.profile.part0 = [{"profile": {"description": {"bio": "Firefighter", "location": "Calgary, Alberta"}}}]`)
add("data/direct-messages.js", `window.YTD.direct_messages.part0 = []`)
z.Close()
p := write(t, t.TempDir(), "archive.zip", buf.String())
in := readInputs([]string{p})
var texts []string
for _, post := range in.Posts {
texts = append(texts, post.Text)
}
all := strings.Join(texts, "|")
if !strings.Contains(all, "Fish & chips") || strings.Contains(all, "doctor") {
t.Errorf("the tweets came out as %q", all)
}
a := readAccount(in, now)
l := count(a)
if len(l.Where) == 0 || l.Where[0].Country != "CA" {
t.Errorf("the profile location was not read: %+v", l.Where)
}
if _, skipped := in.Skipped["data/direct-messages.js"]; !skipped {
t.Error("direct messages were not left alone")
}
}
func TestReadingMastodonAndHackerNews(t *testing.T) {
dir := t.TempDir()
write(t, dir, "outbox.json", `{"orderedItems": [
{"type": "Create", "object": {"content": "<p>> I'm a lawyer</p><p>I live in Lisbon.</p>", "published": "2024-05-01T10:00:00Z"}},
{"type": "Announce", "object": "https://example.org/somebody-else"}
]}`)
write(t, dir, "hn.json", `{"hits": [{"comment_text": "I'm a teacher.", "created_at": "2023-05-01T14:00:00Z"}]}`)
in := readInputs([]string{dir})
if len(in.Posts) != 2 {
t.Fatalf("read %d posts, want 2", len(in.Posts))
}
a := readAccount(in, now)
if has(a.Facts, Job, "Lawyers") {
t.Error("a quoted line was read as the author's job")
}
if !has(a.Facts, Job, "Elementary and middle school teachers") || !has(a.Facts, Lives, "") {
t.Errorf("read %+v", a.Facts)
}
}
func TestAFileItCannotReadIsSaidToBeUnread(t *testing.T) {
p := write(t, t.TempDir(), "photo.png", "not text")
in := readInputs([]string{p})
if len(in.Gaps) == 0 {
t.Error("a file that is not a post went by without a word")
}
}
func TestTheDraftSaysWhatItCosts(t *testing.T) {
in := &Input{Names: []string{"quiet"}, Kinds: map[string]int{}, Skipped: map[string]string{}}
in.add(Post{Text: "Great thread, thanks for sharing it with everybody here.", Where: "a.txt"}, "text files")
a := readAccount(in, now)
before := count(a)
r := &Report{}
after := draftFinding(r, a, before, "I'm a nurse in Leeds and I'm 34.", false)
if after.People >= before.People/100 {
t.Errorf("a job, a city and an age only took the lineup from %.0f to %.0f", before.People, after.People)
}
var out bytes.Buffer
r.render(&out)
if !strings.Contains(out.String(), "divides by") || strings.Contains(out.String(), "Leeds") {
t.Errorf("the draft report was:\n%s", out.String())
}
}
// Two runs on the same posts give the same report, to the character. Sums
// over maps are the usual way this breaks.
func TestTheSameAnswerTwice(t *testing.T) {
run := func() string {
in := &Input{Names: []string{"same"}, Kinds: map[string]int{}, Skipped: map[string]string{}}
for i, text := range []string{
"I write in English and I live in Valencia.",
"I'm 40 and I speak Catalan at home.",
"I'm a teacher, my wife is too.",
} {
in.add(Post{Text: text, When: now.AddDate(0, -i, 0), Where: "row"}, "posts")
}
a := readAccount(in, now)
var out bytes.Buffer
writeUp(in, a, count(a), true).render(&out)
return out.String()
}
first := run()
for range 5 {
if again := run(); again != first {
t.Fatalf("the report moved between runs:\n%s\n---\n%s", first, again)
}
}
}