|
| 1 | +package com.example.xtreamplayer.content |
| 2 | + |
| 3 | +import com.example.xtreamplayer.Section |
| 4 | +import org.junit.Assert.assertFalse |
| 5 | +import org.junit.Assert.assertTrue |
| 6 | +import org.junit.Test |
| 7 | + |
| 8 | +class ContinueWatchingRepositoryWriteGuardTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + fun shouldSkipContinueWatchingWrite_returnsTrue_when_only_position_changes_within_delta() { |
| 12 | + val existing = entry() |
| 13 | + |
| 14 | + val shouldSkip = |
| 15 | + shouldSkipContinueWatchingWrite( |
| 16 | + existingEntry = existing, |
| 17 | + targetKey = existing.key, |
| 18 | + item = existing.item, |
| 19 | + positionMs = existing.positionMs + 1_000L, |
| 20 | + durationMs = existing.durationMs, |
| 21 | + parentItem = existing.parentItem, |
| 22 | + subtitleFileName = existing.subtitleFileName, |
| 23 | + subtitleLanguage = existing.subtitleLanguage, |
| 24 | + subtitleLabel = existing.subtitleLabel, |
| 25 | + subtitleOffsetMs = existing.subtitleOffsetMs, |
| 26 | + minSaveDeltaMs = 30_000L |
| 27 | + ) |
| 28 | + |
| 29 | + assertTrue(shouldSkip) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun shouldSkipContinueWatchingWrite_returnsFalse_when_metadata_changes() { |
| 34 | + val existing = entry() |
| 35 | + val changedItem = existing.item.copy(title = "Updated Title") |
| 36 | + |
| 37 | + val shouldSkip = |
| 38 | + shouldSkipContinueWatchingWrite( |
| 39 | + existingEntry = existing, |
| 40 | + targetKey = existing.key, |
| 41 | + item = changedItem, |
| 42 | + positionMs = existing.positionMs + 1_000L, |
| 43 | + durationMs = existing.durationMs, |
| 44 | + parentItem = existing.parentItem, |
| 45 | + subtitleFileName = existing.subtitleFileName, |
| 46 | + subtitleLanguage = existing.subtitleLanguage, |
| 47 | + subtitleLabel = existing.subtitleLabel, |
| 48 | + subtitleOffsetMs = existing.subtitleOffsetMs, |
| 49 | + minSaveDeltaMs = 30_000L |
| 50 | + ) |
| 51 | + |
| 52 | + assertFalse(shouldSkip) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun shouldSkipContinueWatchingWrite_returnsTrue_when_only_nonpersisted_item_fields_change() { |
| 57 | + val existing = entry() |
| 58 | + val changedItem = |
| 59 | + existing.item.copy( |
| 60 | + description = "Updated description", |
| 61 | + duration = "52m", |
| 62 | + rating = "4.5", |
| 63 | + seasonLabel = "Season 2", |
| 64 | + episodeNumber = "8", |
| 65 | + categoryId = "cat-9" |
| 66 | + ) |
| 67 | + |
| 68 | + val shouldSkip = |
| 69 | + shouldSkipContinueWatchingWrite( |
| 70 | + existingEntry = existing, |
| 71 | + targetKey = existing.key, |
| 72 | + item = changedItem, |
| 73 | + positionMs = existing.positionMs + 1_000L, |
| 74 | + durationMs = existing.durationMs, |
| 75 | + parentItem = existing.parentItem, |
| 76 | + subtitleFileName = existing.subtitleFileName, |
| 77 | + subtitleLanguage = existing.subtitleLanguage, |
| 78 | + subtitleLabel = existing.subtitleLabel, |
| 79 | + subtitleOffsetMs = existing.subtitleOffsetMs, |
| 80 | + minSaveDeltaMs = 30_000L |
| 81 | + ) |
| 82 | + |
| 83 | + assertTrue(shouldSkip) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun shouldSkipContinueWatchingWrite_returnsFalse_when_key_needs_migration() { |
| 88 | + val existing = entry() |
| 89 | + |
| 90 | + val shouldSkip = |
| 91 | + shouldSkipContinueWatchingWrite( |
| 92 | + existingEntry = existing, |
| 93 | + targetKey = "${existing.key}-new", |
| 94 | + item = existing.item, |
| 95 | + positionMs = existing.positionMs + 1_000L, |
| 96 | + durationMs = existing.durationMs, |
| 97 | + parentItem = existing.parentItem, |
| 98 | + subtitleFileName = existing.subtitleFileName, |
| 99 | + subtitleLanguage = existing.subtitleLanguage, |
| 100 | + subtitleLabel = existing.subtitleLabel, |
| 101 | + subtitleOffsetMs = existing.subtitleOffsetMs, |
| 102 | + minSaveDeltaMs = 30_000L |
| 103 | + ) |
| 104 | + |
| 105 | + assertFalse(shouldSkip) |
| 106 | + } |
| 107 | + |
| 108 | + private fun entry(): ContinueWatchingEntry { |
| 109 | + val item = |
| 110 | + ContentItem( |
| 111 | + id = "movie-1", |
| 112 | + title = "Movie", |
| 113 | + subtitle = "Subtitle", |
| 114 | + imageUrl = "https://img.example/poster.jpg", |
| 115 | + section = Section.MOVIES, |
| 116 | + contentType = ContentType.MOVIES, |
| 117 | + streamId = "stream-1", |
| 118 | + containerExtension = "mp4" |
| 119 | + ) |
| 120 | + return ContinueWatchingEntry( |
| 121 | + key = "https://service|user|MOVIES|stream-1", |
| 122 | + item = item, |
| 123 | + positionMs = 120_000L, |
| 124 | + durationMs = 3_600_000L, |
| 125 | + timestampMs = 1L, |
| 126 | + parentItem = null, |
| 127 | + subtitleFileName = "movie_en.srt", |
| 128 | + subtitleLanguage = "en", |
| 129 | + subtitleLabel = "English", |
| 130 | + subtitleOffsetMs = 0L |
| 131 | + ) |
| 132 | + } |
| 133 | +} |
0 commit comments