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
package social.grain
import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import okhttp3.OkHttpClient
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.data.location.LocationServices
import social.grain.feature.feed.CameraFeedViewModel
import social.grain.feature.feed.HashtagFeedViewModel
import social.grain.feature.feed.LocationFeedViewModel
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
import social.grain.support.testJson
/**
* The feeds reached by tapping something on a card — a hashtag, a place name.
*
* They share a base class, so most of this exercises that: paging, optimistic
* favourites, deletes. What the two subclasses add is one line each — which
* `feed` parameter goes out — and that line is exactly what a screen test
* cannot see.
*/
@RunWith(AndroidJUnit4::class)
class GalleryFeedViewModelTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
private lateinit var harness: Harness
@Before
fun setUp() {
harness = Harness()
harness.server
.on("social.grain.feed.getFeed", """{"items":[${json(Fixtures.gallery())}]}""")
}
@After
fun tearDown() = harness.tearDown()
private fun hashtag(tag: String = "fog") = HashtagFeedViewModel(
client = harness.client,
auth = harness.auth,
labelDefsCache = harness.labelDefinitions,
savedStateHandle = SavedStateHandle(mapOf("tag" to tag)),
)
private fun location(cell: String = "8928308280fffff", name: String = "Mission District") =
LocationFeedViewModel(
client = harness.client,
auth = harness.auth,
locations = LocationServices(OkHttpClient(), testJson),
labelDefsCache = harness.labelDefinitions,
savedStateHandle = SavedStateHandle(mapOf("cell" to cell, "name" to name)),
)
@Test
fun aHashtagFeedAsksForThatTag() {
val model = hashtag("fog")
val state = model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
assertEquals("fog", model.tag)
assertFalse(state.isLoading)
assertFalse(state.isEmpty)
assertEquals(harness.did, state.viewerDid)
val call = harness.server.lastCall("social.grain.feed.getFeed")
assertEquals("hashtag", call.params["feed"]?.substringAfterLast('/'))
assertEquals("fog", call.params["tag"])
}
@Test
fun aLocationFeedAsksForThatCellAndKeepsTheNameItWasGiven() {
val model = location()
model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
assertEquals("Mission District", model.locationName)
assertEquals("8928308280fffff", model.cell)
val call = harness.server.lastCall("social.grain.feed.getFeed")
assertEquals("location", call.params["feed"]?.substringAfterLast('/'))
assertEquals("8928308280fffff", call.params["location"])
// The index counts a place across every cell it folded in; the name
// is what makes the feed cover the same ground.
assertEquals("Mission District", call.params["name"])
}
@Test
fun aCameraFeedAsksForThatCamera() {
val model = CameraFeedViewModel(
client = harness.client,
auth = harness.auth,
labelDefsCache = harness.labelDefinitions,
savedStateHandle = SavedStateHandle(mapOf("name" to "Leica M6")),
)
model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
assertEquals("Leica M6", model.camera)
val call = harness.server.lastCall("social.grain.feed.getFeed")
assertEquals("camera", call.params["feed"]?.substringAfterLast('/'))
assertEquals("Leica M6", call.params["camera"])
}
@Test
fun anEmptyAnswerIsOnlyReportedAsEmptyOnceItHasAnswered() {
harness.server.on("social.grain.feed.getFeed", """{"items":[]}""")
val model = hashtag()
val state = model.state.awaitValue("the answer") { !it.isLoading }
assertTrue(state.isEmpty)
}
@Test
fun aFailedLoadFallsBackToTheFeedsOwnWording() {
harness.server.on("social.grain.feed.getFeed", """{"error":"no"}""", code = 500)
val model = hashtag()
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals("HTTP error 500", state.errorMessage)
assertFalse(state.isRefreshing)
model.dismissError()
assertNull(model.state.value.errorMessage)
}
@Test
fun pullingToRefreshShowsTheIndicatorAndReReads() {
val model = hashtag()
model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
val before = harness.server.countOf("social.grain.feed.getFeed")
model.refresh()
harness.server.awaitCall("social.grain.feed.getFeed", before + 1)
model.state.awaitValue("the refresh to finish") { !it.isRefreshing }
}
@Test
fun moreGalleriesAppendBehindTheCursor() {
harness.server.on(
"social.grain.feed.getFeed",
"""{"items":[${json(Fixtures.gallery())}],"cursor":"c1"}""",
)
val model = hashtag()
model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
harness.server.on(
"social.grain.feed.getFeed",
"""{"items":[${json(Fixtures.gallery(rkey = "g2", title = "Second"))}]}""",
)
model.loadMore()
val state = model.state.awaitValue("the second page") { it.galleries.size == 2 }
assertEquals("Second", state.galleries[1].title)
assertEquals("c1", harness.server.lastCall("social.grain.feed.getFeed").params["cursor"])
assertFalse(state.isLoadingMore)
}
@Test
fun aPageWithNoCursorIsTheEndOfTheFeed() {
val model = hashtag()
model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
val before = harness.server.countOf("social.grain.feed.getFeed")
model.loadMore()
settle()
assertEquals(before, harness.server.countOf("social.grain.feed.getFeed"))
}
@Test
fun favoritingFlipsTheHeartThenKeepsTheRecordUri() {
harness.server.on("com.atproto.repo.createRecord", """{"uri":"at://did:plc:me/fav/1"}""")
val model = hashtag()
val gallery = model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
.galleries.single()
model.toggleFavorite(gallery.uri)
assertEquals(13, model.state.value.galleries.single().favCount)
model.state.awaitValue("the record uri") {
it.galleries.single().viewer?.fav == "at://did:plc:me/fav/1"
}
}
@Test
fun aFavoriteThatFailsIsPutBack() {
harness.server.on("com.atproto.repo.createRecord", """{"error":"no"}""", code = 500)
val model = hashtag()
val gallery = model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
.galleries.single()
model.toggleFavorite(gallery.uri)
val state = model.state.awaitValue("the revert") { !it.galleries.single().isFavorited }
assertEquals(12, state.galleries.single().favCount)
}
@Test
fun unfavoritingDeletesTheRecord() {
harness.server.on(
"social.grain.feed.getFeed",
"""{"items":[${json(Fixtures.gallery(favUri = "at://did:plc:me/social.grain.favorite/f1"))}]}""",
)
val model = hashtag()
val gallery = model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
.galleries.single()
model.toggleFavorite(gallery.uri)
assertTrue(harness.server.awaitCall("com.atproto.repo.deleteRecord").body.contains("\"rkey\":\"f1\""))
assertEquals(11, model.state.value.galleries.single().favCount)
}
@Test
fun favoritingSomethingNotInTheFeedIsIgnored() {
val model = hashtag()
model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
model.toggleFavorite("at://nothing")
settle()
assertEquals(0, harness.server.countOf("com.atproto.repo.createRecord"))
}
@Test
fun theCommentCountFollowsTheSheetOpenedOverACard() {
val model = hashtag()
val gallery = model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
.galleries.single()
model.setCommentCount(gallery.uri, 7)
assertEquals(7, model.state.value.galleries.single().commentCount)
}
@Test
fun deletingAGalleryRemovesTheCardOnlyOnceTheWriteLands() {
val model = hashtag()
val gallery = model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
.galleries.single()
model.deleteGallery(gallery.uri)
model.state.awaitValue("the card to go") { it.galleries.isEmpty() }
}
@Test
fun aDeleteThatFailsLeavesTheCardAndSaysSo() {
harness.server.on(
"com.atproto.repo.applyWrites",
"""{"error":"no"}""",
code = 500,
)
val model = hashtag()
val gallery = model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
.galleries.single()
model.deleteGallery(gallery.uri)
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals("Couldn't delete that gallery.", state.errorMessage)
assertEquals(1, state.galleries.size)
}
}