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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package social.grain
import androidx.lifecycle.SavedStateHandle
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.assertNotNull
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.model.GrainProfile
import social.grain.data.api.markStoriesViewed
import kotlinx.serialization.json.decodeFromJsonElement
import social.grain.support.testJson
import social.grain.data.model.StoryViewerState
import social.grain.feature.stories.StoryViewerViewModel
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
/**
* The story viewer, which is a queue of authors inside a queue of stories.
*
* Three ways in, and they behave differently on purpose: the strip walks on to
* the next author, a ring anywhere else plays one account and stops, and the
* archive plays one expired story on its own. Getting that wrong means tapping
* past a friend's last story drops you into a stranger's.
*/
@RunWith(AndroidJUnit4::class)
class StoryViewerViewModelTest {
@get:Rule
val mainDispatcher = MainDispatcherRule()
private lateinit var harness: Harness
private val alice = Fixtures.profile(did = "did:plc:alice", handle = "alice.test")
private val bob =
Fixtures.profile(did = "did:plc:bob", handle = "bob.test", displayName = "Bob")
private fun stories(creator: GrainProfile, vararg rkeys: String) =
"""{"stories":[${rkeys.joinToString(",") { json(Fixtures.story(rkey = it, creator = creator)) }}]}"""
/**
* Answered per actor rather than with one fixed reply.
*
* The viewer prefetches the authors either side of the one on screen, so a
* fixed reply gets cached against whichever author the prefetch happened to
* ask about first — and the test then plays alice's stories under bob's
* name.
*/
private fun serveStories(aliceStories: List<String> = listOf("a1", "a2")) {
harness.server.on("social.grain.story.getStories") { call ->
when (call.params["actor"]) {
bob.did -> stories(bob, "b1")
alice.did -> stories(alice, *aliceStories.toTypedArray())
else -> """{"stories":[]}"""
}
}
}
@Before
fun setUp() {
harness = Harness()
serveStories()
}
@After
fun tearDown() = harness.tearDown()
private fun viewModel(
did: String = alice.did,
story: String = "",
solo: Boolean = false,
) = StoryViewerViewModel(
client = harness.client,
auth = harness.auth,
viewedStore = harness.viewedStories,
statusCache = harness.storyStatus,
labelDefsCache = harness.labelDefinitions,
savedStateHandle = SavedStateHandle(
mapOf("did" to did, "story" to story, "solo" to solo),
),
)
/**
* Two authors in the strip, alice first.
*
* The timestamps are pinned rather than left to default: the strip is
* ordered newest story first, so authors seeded a millisecond apart come
* back in whichever order the clock happened to give them.
*/
private fun seedStrip() {
harness.storyStatus.update(
listOf(
Fixtures.storyAuthor(
profile = alice,
storyCount = 2,
latestAt = Fixtures.recentIso(minutesAgo = 1),
),
Fixtures.storyAuthor(
profile = bob,
storyCount = 1,
latestAt = Fixtures.recentIso(minutesAgo = 30),
),
),
)
}
@Test
fun aWatchedStoryIsReportedToTheAppview() {
seedStrip()
harness.viewedStories.uploader = { uris ->
harness.client.markStoriesViewed(uris, social.grain.support.testAuth())
}
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
val call = harness.server.awaitCall("social.grain.story.markViewed")
assertEquals("POST", call.method)
assertEquals(
listOf("at://did:plc:alice/social.grain.story/a1"),
call.json["stories"]!!.let { testJson.decodeFromJsonElement<List<String>>(it) },
)
}
@Test
fun aStoryWatchedElsewhereIsSkippedOver() {
seedStrip()
harness.server.on("social.grain.story.getStories") { call ->
when (call.params["actor"]) {
alice.did -> """{"stories":[
${json(Fixtures.story(rkey = "a1", creator = alice).copy(viewer = StoryViewerState(viewed = true)))},
${json(Fixtures.story(rkey = "a2", creator = alice))}
]}"""
bob.did -> stories(bob, "b1")
else -> """{"stories":[]}"""
}
}
val model = viewModel()
val state = model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
assertEquals("Opens on the story the web has not shown yet", "a2", state.story!!.rkey)
}
@Test
fun openingFromTheStripStartsOnTheTappedAuthor() {
seedStrip()
val model = viewModel()
val state = model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
assertEquals(2, state.authors.size)
assertEquals("Newest story first", alice.did, state.authors.first().did)
assertEquals(alice.did, state.author!!.did)
assertEquals("a1", state.story!!.rkey)
assertFalse(state.isLoading)
assertFalse("Alice's story is not the viewer's", state.isOwnStory)
}
@Test
fun tappingForwardWalksTheStoriesThenTheAuthors() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
model.next()
assertEquals("a2", model.state.value.story!!.rkey)
model.next()
val onBob = model.state.awaitValue("bob's story") {
it.author?.did == bob.did && it.stories.isNotEmpty()
}
assertEquals("b1", onBob.story!!.rkey)
model.next()
model.state.awaitValue("the end") { it.isFinished }
}
@Test
fun tappingBackFromAnAuthorsFirstStoryLandsOnThePreviousAuthorsLast() {
seedStrip()
val model = viewModel(did = bob.did)
model.state.awaitValue("bob's story") { it.stories.isNotEmpty() }
model.previous()
val back = model.state.awaitValue("alice's last story") {
it.author?.did == alice.did && it.stories.isNotEmpty()
}
assertEquals("Coming backwards resumes at the end", "a2", back.story!!.rkey)
}
@Test
fun tappingBackAtTheVeryStartStaysPut() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
model.previous()
settle()
assertEquals("a1", model.state.value.story!!.rkey)
assertFalse(model.state.value.isFinished)
}
@Test
fun aSwipeJumpsWholeAuthorsRatherThanSingleStories() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
model.nextAuthor()
model.state.awaitValue("bob") { it.author?.did == bob.did && it.stories.isNotEmpty() }
model.previousAuthor()
model.state.awaitValue("alice again") {
it.author?.did == alice.did && it.stories.isNotEmpty()
}
model.previousAuthor()
settle()
assertEquals("Nobody before the first author", alice.did, model.state.value.author!!.did)
}
@Test
fun swipingPastTheLastAuthorCloses() {
seedStrip()
val model = viewModel(did = bob.did)
model.state.awaitValue("bob's story") { it.stories.isNotEmpty() }
model.nextAuthor()
model.state.awaitValue("the end") { it.isFinished }
}
@Test
fun aDragMountsTheAuthorOnTheFarSideAndCanBeTurnedBack() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
assertEquals(true, model.beginAuthorSwipe(forward = true))
val pending = model.state.awaitValue("bob mounted") { it.pending?.stories?.isNotEmpty() == true }
assertEquals(bob.did, pending.pending!!.author.did)
assertNotNull(pending.pending!!.story)
assertEquals("The author being watched is untouched", alice.did, pending.author!!.did)
model.cancelAuthorSwipe()
assertNull(model.state.value.pending)
assertEquals(alice.did, model.state.value.author!!.did)
}
@Test
fun aCommittedDragBecomesTheAuthorBeingWatched() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
model.beginAuthorSwipe(forward = true)
model.state.awaitValue("bob mounted") { it.pending?.stories?.isNotEmpty() == true }
model.commitAuthorSwipe()
val state = model.state.value
assertEquals(bob.did, state.author!!.did)
assertEquals("b1", state.story!!.rkey)
assertNull(state.pending)
}
@Test
fun aDragWithNobodyOnThatSideStaysInert() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
assertNull("There is nobody before alice", model.beginAuthorSwipe(forward = false))
assertNull(model.state.value.pending)
}
@Test
fun aDragThatCatchesAHandOffCarriesOnWithTheAuthorAlreadyComingIn() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
model.beginAuthorSwipe(forward = true)
assertEquals(
"The second drag keeps bob rather than mounting a second face",
true,
model.beginAuthorSwipe(forward = false),
)
}
@Test
fun committingADragWhoseStoriesNeverArrivedFallsBackToAPlainLoad() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
harness.server.on("social.grain.story.getStories", """{"error":"no"}""", code = 500)
model.beginAuthorSwipe(forward = true)
settle()
model.commitAuthorSwipe()
model.state.awaitValue("bob") { it.author?.did == bob.did }
}
@Test
fun aRingOutsideTheStripPlaysThatAccountAndStops() {
val model = viewModel(solo = true)
val state = model.state.awaitValue("the story") { it.stories.isNotEmpty() }
assertEquals("Nobody to walk on to", 1, state.authors.size)
assertEquals(alice.did, state.author!!.did)
model.next()
assertEquals("a2", model.state.value.story!!.rkey)
model.next()
model.state.awaitValue("the end") { it.isFinished }
}
@Test
fun anAccountWithNothingLiveJustCloses() {
harness.server.on("social.grain.story.getStories", """{"stories":[]}""")
val model = viewModel(solo = true)
model.state.awaitValue("the close") { it.isFinished }
}
@Test
fun anExpiredStoryIsNotPlayedAsALiveOne() {
harness.server.on(
"social.grain.story.getStories",
"""{"stories":[${json(Fixtures.story(rkey = "old", creator = alice, expired = true))}]}""",
)
val model = viewModel(solo = true)
model.state.awaitValue("the close") { it.isFinished }
}
@Test
fun theArchiveOpensOneStoryOnItsOwn() {
val uri = "at://did:plc:alice/social.grain.story/old"
harness.server.on(
"social.grain.story.getStory",
"""{"story":${json(Fixtures.story(rkey = "old", creator = alice, expired = true))}}""",
)
val model = viewModel(story = uri)
val state = model.state.awaitValue("the archived story") { it.stories.isNotEmpty() }
assertEquals(1, state.stories.size)
assertEquals(1, state.authors.size)
assertEquals("old", state.story!!.rkey)
assertEquals(uri, harness.server.lastCall("social.grain.story.getStory").params["story"])
}
@Test
fun anArchivedStoryThatIsGoneJustCloses() {
harness.server.on("social.grain.story.getStory", """{"story":null}""")
val model = viewModel(story = "at://x/social.grain.story/gone")
model.state.awaitValue("the close") { it.isFinished }
}
@Test
fun anAuthorTheStripHasNeverHeardOfIsRefetchedThenPlayedAlone() {
harness.server.on(
"social.grain.story.getAuthors",
"""{"authors":[${json(Fixtures.storyAuthor(profile = bob, storyCount = 1))}]}""",
)
val model = viewModel()
val state = model.state.awaitValue("alice alone") { it.stories.isNotEmpty() }
assertEquals("Alice isn't in the refetched strip, so she plays alone", 1, state.authors.size)
assertEquals(alice.did, state.author!!.did)
harness.server.awaitCall("social.grain.story.getAuthors")
}
@Test
fun watchingAStoryMarksItSeen() {
seedStrip()
val model = viewModel()
val state = model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
// The mark is written just after the state that announced the story.
settle()
assertTrue(harness.viewedStories.isViewed(state.story!!.uri))
}
@Test
fun theTimerTokenMovesWheneverTheStoryOnScreenDoes() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
val before = model.storyToken.value
model.next()
assertTrue(model.storyToken.value > before)
}
@Test
fun holdingTheScreenPausesAndReleasingResumes() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
model.setPaused(true)
assertTrue(model.state.value.isPaused)
model.setPaused(false)
assertFalse(model.state.value.isPaused)
}
@Test
fun favoritingAStoryFlipsItThenKeepsTheRecordUri() {
harness.server.on("com.atproto.repo.createRecord", """{"uri":"at://did:plc:me/fav/s1"}""")
val model = viewModel(solo = true)
model.state.awaitValue("the story") { it.stories.isNotEmpty() }
model.toggleFavorite()
assertTrue(model.state.value.story!!.isFavorited)
model.state.awaitValue("the record uri") {
it.story?.viewer?.fav == "at://did:plc:me/fav/s1"
}
}
@Test
fun aStoryFavoriteThatFailsIsPutBack() {
harness.server.on("com.atproto.repo.createRecord", """{"error":"no"}""", code = 500)
val model = viewModel(solo = true)
model.state.awaitValue("the story") { it.stories.isNotEmpty() }
model.toggleFavorite()
model.state.awaitValue("the revert") { it.story?.isFavorited == false }
}
@Test
fun unfavoritingAStoryDeletesTheRecord() {
harness.server.on(
"social.grain.story.getStories",
"""{"stories":[${json(
Fixtures.story(
rkey = "a1",
creator = alice,
favUri = "at://did:plc:me/social.grain.favorite/sf1",
),
)}]}""",
)
val model = viewModel(solo = true)
model.state.awaitValue("the story") { it.story?.isFavorited == true }
model.toggleFavorite()
assertTrue(harness.server.awaitCall("com.atproto.repo.deleteRecord").body.contains("sf1"))
}
@Test
fun deletingTheLastStoryClosesTheViewer() {
serveStories(aliceStories = listOf("a1"))
val model = viewModel(solo = true)
model.state.awaitValue("the story") { it.stories.isNotEmpty() }
model.deleteCurrentStory()
model.state.awaitValue("the close") { it.isFinished }
}
@Test
fun deletingOneOfSeveralStoriesStaysOpen() {
val model = viewModel(solo = true)
model.state.awaitValue("the story") { it.stories.size == 2 }
model.deleteCurrentStory()
val state = model.state.awaitValue("the remaining story") { it.stories.size == 1 }
assertEquals("a2", state.story!!.rkey)
assertFalse(state.isFinished)
}
@Test
fun aStoryDeleteThatFailsSaysSo() {
harness.server.on("com.atproto.repo.deleteRecord", """{"error":"no"}""", code = 500)
val model = viewModel(solo = true)
model.state.awaitValue("the story") { it.stories.isNotEmpty() }
model.deleteCurrentStory()
val state = model.state.awaitValue("the error") { it.errorMessage != null }
assertEquals("Couldn't delete that story.", state.errorMessage)
model.dismissError()
assertNull(model.state.value.errorMessage)
}
@Test
fun theViewersOwnStoryIsMarkedAsTheirs() {
val me = Fixtures.profile(did = harness.did, handle = harness.handle)
harness.server.on("social.grain.story.getStories", stories(me, "m1"))
val model = viewModel(did = harness.did, solo = true)
assertTrue(model.state.awaitValue("the story") { it.stories.isNotEmpty() }.isOwnStory)
}
@Test
fun theNeighboursFirstFramesArePreloaded() {
seedStrip()
val model = viewModel()
model.state.awaitValue("the first story") { it.stories.isNotEmpty() }
val urls = model.preloadUrls.awaitValue("the preload list") { it.isNotEmpty() }
assertTrue(urls.all { it.isNotEmpty() })
}
}