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
package social.grain
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import social.grain.feature.stories.StoryCommentsViewModel
import social.grain.support.Fixtures
import social.grain.support.Harness
import social.grain.support.MainDispatcherRule
import social.grain.support.awaitCall
import social.grain.support.awaitValue
import social.grain.support.json
import social.grain.support.settle
/**
* Comments on whichever story is on screen.
*
* The viewer swipes between stories without leaving the screen, so the badge
* has to be right for every one of them without paying for a full thread fetch
* per swipe — which is what the preview cache is for, and what makes "paging
* back to a story already seen" the interesting case.
*/
@RunWith(AndroidJUnit4::class)
class StoryCommentsViewModelTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
private lateinit var harness: Harness
private val nsid = "social.grain.comment.getThread"
private val first = "at://did:plc:alice/social.grain.story/one"
private val second = "at://did:plc:alice/social.grain.story/two"
private fun thread(vararg comments: String, total: Int? = null) =
"""{"comments":[${comments.joinToString(",")}]""" +
(total?.let { ""","totalCount":$it""" } ?: "") + "}"
@Before
fun setUp() {
harness = Harness()
harness.server.on(nsid, thread(json(Fixtures.comment(text = "First one")), total = 4))
}
@After
fun tearDown() = harness.tearDown()
private fun viewModel() = StoryCommentsViewModel(harness.client, harness.auth)
@Test
fun switchingToAStoryReadsItsPreviewAndCount() {
val model = viewModel()
model.switchToStory(first)
val state = model.state.awaitValue("the preview") { it.firstComment != null }
assertEquals("First one", state.firstComment!!.text)
assertEquals(4, state.totalCount)
assertEquals("The preview is one comment, not the thread", "1", harness.server.lastCall(nsid).params["limit"])
}
@Test
fun switchingToTheStoryAlreadyShowingCostsNothing() {
val model = viewModel()
model.switchToStory(first)
model.state.awaitValue("the preview") { it.firstComment != null }
model.switchToStory(first)
settle()
assertEquals(1, harness.server.countOf(nsid))
}
@Test
fun pagingBackToAStoryAlreadySeenRedrawsFromTheCache() {
val model = viewModel()
model.switchToStory(first)
model.state.awaitValue("the first preview") { it.firstComment != null }
harness.server.on(nsid, thread(json(Fixtures.comment(text = "Second one")), total = 1))
model.switchToStory(second)
model.state.awaitValue("the second preview") { it.firstComment?.text == "Second one" }
val fetches = harness.server.countOf(nsid)
model.switchToStory(first)
val back = model.state.awaitValue("the cached preview") { it.firstComment?.text == "First one" }
assertEquals(4, back.totalCount)
settle()
assertEquals("No refetch for a story already seen", fetches, harness.server.countOf(nsid))
}
@Test
fun openingTheSheetReadsTheWholeThread() {
val model = viewModel()
model.switchToStory(first)
model.state.awaitValue("the preview") { it.firstComment != null }
harness.server.on(
nsid,
thread(json(Fixtures.comment()), json(Fixtures.comment(rkey = "c2", text = "Two")), total = 2),
)
model.loadComments()
val state = model.state.awaitValue("the thread") { it.hasLoaded }
assertEquals(2, state.comments.size)
assertEquals(2, state.totalCount)
assertNull(harness.server.lastCall(nsid).params["limit"]?.takeIf { it == "1" })
}
@Test
fun loadingWithNoStoryOnScreenDoesNothing() {
viewModel().loadComments()
settle()
assertEquals(0, harness.server.countOf(nsid))
}
@Test
fun aThreadThatFailsSaysSo() {
val model = viewModel()
model.switchToStory(first)
model.state.awaitValue("the preview") { it.firstComment != null }
harness.server.on(nsid, """{"error":"no"}""", code = 500)
model.loadComments()
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals("Couldn't load comments.", state.errorMessage)
assertTrue(state.hasLoaded)
model.dismissError()
assertNull(model.state.value.errorMessage)
}
@Test
fun aFailedPreviewLeavesTheBadgeAloneWithoutRaisingAnError() {
harness.server.on(nsid, """{"error":"no"}""", code = 500)
val model = viewModel()
model.switchToStory(first)
harness.server.awaitCall(nsid)
settle()
assertNull(model.state.value.firstComment)
assertNull(model.state.value.errorMessage)
}
@Test
fun postingACommentInvalidatesTheCachedPreview() {
harness.server.on("com.atproto.repo.createRecord", """{"uri":"at://did:plc:me/comment/new"}""")
val model = viewModel()
model.switchToStory(first)
model.state.awaitValue("the preview") { it.firstComment != null }
harness.server.on(
nsid,
thread(json(Fixtures.comment(text = "Mine")), json(Fixtures.comment(rkey = "c2")), total = 5),
)
model.postComment(" Mine ", replyTo = null)
val state = model.state.awaitValue("the reread thread") { it.comments.size == 2 }
assertEquals(5, state.totalCount)
assertFalse(state.isPosting)
assertTrue(harness.server.awaitCall("com.atproto.repo.createRecord").body.contains("\"text\":\"Mine\""))
}
@Test
fun anEmptyCommentIsNotPostedAndNeitherIsOneWithNoStory() {
val model = viewModel()
model.postComment("Hello", replyTo = null)
model.switchToStory(first)
model.state.awaitValue("the preview") { it.firstComment != null }
model.postComment(" ", replyTo = null)
settle()
assertEquals(0, harness.server.countOf("com.atproto.repo.createRecord"))
}
@Test
fun aCommentThatFailsToPostSaysSo() {
harness.server.on("com.atproto.repo.createRecord", """{"error":"no"}""", code = 500)
val model = viewModel()
model.switchToStory(first)
model.state.awaitValue("the preview") { it.firstComment != null }
model.postComment("Mine", replyTo = null)
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals("Couldn't post that comment.", state.errorMessage)
assertFalse(state.isPosting)
}
@Test
fun deletingACommentTakesItsRepliesAndOneOffTheCount() {
val root = Fixtures.comment(rkey = "root")
harness.server.on(
nsid,
thread(json(root), json(Fixtures.comment(rkey = "reply", replyTo = root.uri)), total = 2),
)
val model = viewModel()
model.switchToStory(first)
model.loadComments()
val loaded = model.state.awaitValue("the thread") { it.comments.size == 2 }
model.deleteComment(loaded.comments.first())
val state = model.state.awaitValue("the rows to go") { it.comments.isEmpty() }
assertEquals(1, state.totalCount)
}
@Test
fun aDeleteThatFailsPutsTheRowsBack() {
harness.server.on("com.atproto.repo.deleteRecord", """{"error":"no"}""", code = 500)
val model = viewModel()
model.switchToStory(first)
model.loadComments()
val loaded = model.state.awaitValue("the thread") { it.hasLoaded }
model.deleteComment(loaded.comments.first())
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals(1, state.comments.size)
}
@Test
fun favoritingACommentFlipsItThenKeepsTheRecordUri() {
harness.server.on("com.atproto.repo.createRecord", """{"uri":"at://did:plc:me/fav/1"}""")
val model = viewModel()
model.switchToStory(first)
model.loadComments()
val comment = model.state.awaitValue("the thread") { it.hasLoaded }.comments.single()
model.toggleFavorite(comment)
assertEquals(1, model.state.value.comments.single().favCount)
model.state.awaitValue("the record uri") {
it.comments.single().viewer?.fav == "at://did:plc:me/fav/1"
}
}
@Test
fun aCommentFavoriteThatFailsIsPutBack() {
harness.server.on("com.atproto.repo.createRecord", """{"error":"no"}""", code = 500)
val model = viewModel()
model.switchToStory(first)
model.loadComments()
val comment = model.state.awaitValue("the thread") { it.hasLoaded }.comments.single()
model.toggleFavorite(comment)
val state = model.state.awaitValue("the revert") { it.comments.single().viewer?.fav == null }
assertEquals(0, state.comments.single().favCount)
}
@Test
fun unfavoritingACommentDeletesTheRecord() {
harness.server.on(
nsid,
thread(json(Fixtures.comment(favUri = "at://did:plc:me/social.grain.favorite/cf1"))),
)
val model = viewModel()
model.switchToStory(first)
model.loadComments()
val comment = model.state.awaitValue("the thread") { it.hasLoaded }.comments.single()
model.toggleFavorite(comment)
assertTrue(harness.server.awaitCall("com.atproto.repo.deleteRecord").body.contains("cf1"))
}
@Test
fun repliesAreGroupedUnderTheirRoot() {
val root = Fixtures.comment(rkey = "root", text = "Root")
harness.server.on(
nsid,
thread(json(root), json(Fixtures.comment(rkey = "r", text = "Reply", replyTo = root.uri))),
)
val model = viewModel()
model.switchToStory(first)
model.loadComments()
val state = model.state.awaitValue("the thread") { it.comments.size == 2 }
assertEquals(1, state.threads.size)
assertEquals("Reply", state.threads.single().replies.single().text)
}
}