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
package logic

import (
	"rss_parrot/dal"
	"rss_parrot/shared"
	"slices"
	"strings"
	"testing"
	"time"
)

// Scenarios for what gets tooted when a feed misbehaves (items.go): every
// item is remembered by guid and link, and only one that is new and recent
// is tooted.

// newLedgerSim follows a feed with ten posts, a day apart, the last one an
// hour before the start, and lets a day go by.
func newLedgerSim(t *testing.T, configure func(*shared.Config)) (*sim, *simFeed) {
	s := newSim(t, configure)
	var posts []simPost
	for i := 10; i >= 1; i-- {
		posts = append(posts, simPost{
			title:     "post " + string(rune('a'+10-i)),
			published: simStart.Add(-time.Duration(i-1)*day - hour),
		})
	}
	feed := newSimFeed(t, s.clock, posts...)
	s.follow(feed)
	s.runUntil(simStart.Add(day))
	if b := s.msgr.broadcasts(); len(b) != 0 {
		t.Fatalf("broadcast %v without any new post", s.msgr.titles())
	}
	return s, feed
}

func assertBroadcasts(t *testing.T, s *sim, want ...string) {
	t.Helper()
	if got := s.msgr.titles(); !slices.Equal(got, want) {
		t.Errorf("broadcasts = %v, want %v", got, want)
	}
}

func TestLedgerRedatedArchiveIsNotTootedAgain(t *testing.T) {
	// With purging on, so the old posts are gone from feed_posts and toots by
	// the time they are re-dated: upstream would toot them all again.
	s, feed := newLedgerSim(t, func(cfg *shared.Config) {
		cfg.PostsMinCountKept = 1
		cfg.PostsMinDaysKept = 1
	})
	feed.addPost(simPost{title: "new", published: s.clock.Now().Add(hour)})
	s.runUntil(s.clock.Now().Add(day))
	assertBroadcasts(t, s, "new")

	feed.editPosts(func(p *simPost) { p.published = s.clock.Now() })
	s.runUntil(s.clock.Now().Add(3 * day))
	assertBroadcasts(t, s, "new")
}

func TestLedgerChangedIdentitiesAreNotTootedAgain(t *testing.T) {
	cases := []struct {
		name string
		edit func(p *simPost)
	}{
		{"links move to https", func(p *simPost) {
			p.link = strings.Replace(p.itemLink(), "https://", "http://", 1)
		}},
		{"links get tracking parameters", func(p *simPost) { p.link = p.itemLink() + "/?utm_source=rss" }},
		{"guids change", func(p *simPost) { p.guid = "urn:uuid:" + p.slug() }},
		{"guids and links change, dates stay", func(p *simPost) {
			p.guid = "urn:uuid:" + p.slug()
			p.link = "https://new.example.org/" + p.slug()
		}},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			s, feed := newLedgerSim(t, nil)
			feed.editPosts(c.edit)
			s.runUntil(s.clock.Now().Add(3 * day))
			assertBroadcasts(t, s)
		})
	}
}

func TestLedgerRenamedRecentPostIsNotTootedAgain(t *testing.T) {
	// A post an hour old is within the grace period, so its date does not
	// keep it from being tooted again under a new guid and link; that it is
	// dated exactly like the one that vanished does.
	s, feed := newLedgerSim(t, nil)
	recent := simPost{title: "recent", published: s.clock.Now().Add(-hour)}
	feed.addPost(recent)
	s.runUntil(s.clock.Now().Add(day))
	assertBroadcasts(t, s, "recent")

	feed.editPosts(func(p *simPost) {
		p.guid = "urn:uuid:" + p.slug()
		p.link = "https://new.example.org/" + p.slug()
	})
	s.runUntil(s.clock.Now().Add(3 * day))
	assertBroadcasts(t, s, "recent")
}

func TestLedgerTwoPostsOnTheSameDayAreBothTooted(t *testing.T) {
	// A feed that only has dates gives two posts of a day the same time; the
	// second is not taken for a renamed first, which is still in the feed.
	s, feed := newLedgerSim(t, nil)
	midnight := s.clock.Now().Truncate(day)
	feed.addPost(simPost{title: "first", published: midnight, visibleFrom: s.clock.Now().Add(hour)})
	s.runUntil(s.clock.Now().Add(6 * hour))
	feed.addPost(simPost{title: "second", published: midnight, visibleFrom: s.clock.Now().Add(hour)})
	s.runUntil(s.clock.Now().Add(day))
	assertBroadcasts(t, s, "first", "second")
}

func TestLedgerMigrationThatRedatesEverything(t *testing.T) {
	// New guids, new links and new dates at once: nothing tells these from
	// new posts. Without a cap they are all tooted; that is the price of
	// trusting feeds this far, and what max_new_items_per_fetch is for.
	migrate := func(s *sim, feed *simFeed) {
		feed.editPosts(func(p *simPost) {
			p.guid = "urn:uuid:" + p.slug()
			p.link = "https://new.example.org/" + p.slug()
			p.published = s.clock.Now().Add(-time.Minute)
		})
		s.runUntil(s.clock.Now().Add(day))
	}

	t.Run("no cap", func(t *testing.T) {
		s, feed := newLedgerSim(t, nil)
		migrate(s, feed)
		if n := len(s.msgr.broadcasts()); n != 10 {
			t.Errorf("%d tooted, want all 10", n)
		}
		s.assertNoDuplicateBroadcasts()
	})
	t.Run("capped at 3", func(t *testing.T) {
		s, feed := newLedgerSim(t, func(cfg *shared.Config) { cfg.MaxNewItemsPerFetch = 3 })
		migrate(s, feed)
		if n := len(s.msgr.broadcasts()); n != 3 {
			t.Errorf("%d tooted, want 3", n)
		}
		// The rest are remembered, never to be tooted.
		s.runUntil(s.clock.Now().Add(7 * day))
		if n := len(s.msgr.broadcasts()); n != 3 {
			t.Errorf("%d tooted a week later, want still 3", n)
		}
	})
	t.Run("cap not reached", func(t *testing.T) {
		s, feed := newLedgerSim(t, func(cfg *shared.Config) { cfg.MaxNewItemsPerFetch = 3 })
		feed.addPost(simPost{title: "one", published: s.clock.Now().Add(hour)})
		feed.addPost(simPost{title: "two", published: s.clock.Now().Add(hour)})
		s.runUntil(s.clock.Now().Add(day))
		if n := len(s.msgr.broadcasts()); n != 2 {
			t.Errorf("%d tooted, want 2", n)
		}
	})
}

func TestLedgerBackdatedNewPost(t *testing.T) {
	s, feed := newLedgerSim(t, nil)
	feed.addPost(simPost{title: "latest", published: s.clock.Now().Add(-hour)})
	s.runUntil(s.clock.Now().Add(6 * hour))

	// Published now, dated a day before the latest post: within the 48h
	// grace (a draft published with its original date, say).
	feed.addPost(simPost{title: "draft", published: s.clock.Now().Add(-31 * hour),
		visibleFrom: s.clock.Now()})
	// Published now, dated a week before it: not.
	feed.addPost(simPost{title: "ancient", published: s.clock.Now().Add(-7 * day),
		visibleFrom: s.clock.Now()})
	s.runUntil(s.clock.Now().Add(day))
	assertBroadcasts(t, s, "latest", "draft")
}

func TestLedgerConfiguredGrace(t *testing.T) {
	zero := 0
	s, feed := newLedgerSim(t, func(cfg *shared.Config) { cfg.BackdateGraceHours = &zero })
	feed.addPost(simPost{title: "latest", published: s.clock.Now().Add(-hour)})
	s.runUntil(s.clock.Now().Add(6 * hour))
	feed.addPost(simPost{title: "draft", published: s.clock.Now().Add(-31 * hour),
		visibleFrom: s.clock.Now()})
	s.runUntil(s.clock.Now().Add(day))
	assertBroadcasts(t, s, "latest")
}

func TestLedgerUndatedItemIsTootedOnceWhenFirstSeen(t *testing.T) {
	s, feed := newLedgerSim(t, nil)
	appears := s.clock.Now().Add(3 * hour)
	feed.addPost(simPost{title: "undated", noDate: true, visibleFrom: appears})
	s.runUntil(s.clock.Now().Add(3 * day))
	b := s.msgr.broadcasts()
	if len(b) != 1 || b[0].content != "undated" {
		t.Fatalf("broadcasts = %v, want [undated]", s.msgr.titles())
	}
	if lag := b[0].at.Sub(appears); lag < 0 || lag > 4*hour*12/10 {
		t.Errorf("tooted %s after it appeared, want at the next check", lag)
	}
	posts, err := s.repo.GetPostsPage(s.state(simHandle).Id, 0, 100)
	if err != nil {
		t.Fatal(err)
	}
	for _, p := range posts {
		if p.Title == "undated" && !p.PostTime.Equal(b[0].at) {
			t.Errorf("post time %s, want when it was first seen, %s", p.PostTime, b[0].at)
		}
	}
}

func TestLedgerItemsWithoutGuidAreMatchedByLink(t *testing.T) {
	s := newSim(t, nil)
	feed := newSimFeed(t, s.clock,
		simPost{title: "a", published: simStart.Add(-day), noGuid: true},
		simPost{title: "b", published: simStart.Add(-hour), noGuid: true},
	)
	s.follow(feed)
	feed.addPost(simPost{title: "c", published: simStart.Add(hour), noGuid: true})
	s.runUntil(simStart.Add(day))
	feed.editPosts(func(p *simPost) { p.published = s.clock.Now() })
	s.runUntil(simStart.Add(4 * day))
	assertBroadcasts(t, s, "c")
}

// An account followed before the ledger existed has posts in feed_posts and
// a feed_last_updated, but nothing in feed_items. Its first read follows
// upstream's rules, after that the ledger's.
func TestLedgerFirstReadOfAnAccountFromBeforeTheLedger(t *testing.T) {
	s := newSim(t, nil)
	feed := newSimFeed(t, s.clock,
		simPost{title: "old", published: simStart.Add(-3 * day)},
		simPost{title: "known", published: simStart.Add(-2 * hour)},
		// Published while the upgrade was rolled out: upstream would toot it.
		simPost{title: "missed", published: simStart.Add(-hour)},
	)
	if _, err := s.repo.AddAccountIfNotExist(&dal.Account{
		CreatedAt: simStart.Add(-30 * day), Handle: simHandle,
		UserUrl: "https://kakapo.example.com/u/" + simHandle, FeedUrl: feed.URL,
	}, ""); err != nil {
		t.Fatal(err)
	}
	s.addFollower(simHandle)
	acct, err := s.repo.GetAccount(simHandle)
	if err != nil {
		t.Fatal(err)
	}
	// What the old code left behind: feed_last_updated at the newest post
	// it saw, "known"; and the posts it saw, of which "known" has been
	// purged already. Within the backdate grace and in no table, it would
	// pass for new under the ledger's rules.
	for _, p := range feed.posts[:1] {
		if _, err := s.repo.AddFeedPostIfNew(acct.Id, &dal.FeedPost{
			PostGuidHash: int64(murmurHash(p.itemGuid() + "\t" + p.itemLink())),
			PostTime:     p.published, Link: p.itemLink(), Title: p.title,
		}); err != nil {
			t.Fatal(err)
		}
	}
	if err := s.repo.UpdateAccountFeedTimes(acct.Id, feed.posts[1].published, s.clock.Now()); err != nil {
		t.Fatal(err)
	}

	s.runUntil(simStart.Add(day))
	assertBroadcasts(t, s, "missed")

	feed.editPosts(func(p *simPost) { p.published = s.clock.Now() })
	s.runUntil(simStart.Add(3 * day))
	assertBroadcasts(t, s, "missed")
}