|
| 1 | +import type { PlexWatchlistItem } from '@server/api/plextv'; |
| 2 | +import PlexTvAPI from '@server/api/plextv'; |
| 3 | +import { |
| 4 | + MediaRequestStatus, |
| 5 | + MediaStatus, |
| 6 | + MediaType, |
| 7 | +} from '@server/constants/media'; |
| 8 | +import { getRepository } from '@server/datasource'; |
| 9 | +import Media from '@server/entity/Media'; |
| 10 | +import { MediaRequest } from '@server/entity/MediaRequest'; |
| 11 | +import { User } from '@server/entity/User'; |
| 12 | +import { UserSettings } from '@server/entity/UserSettings'; |
| 13 | +import { Permission } from '@server/lib/permissions'; |
| 14 | +import { setupTestDb } from '@server/test/db'; |
| 15 | +import assert from 'node:assert/strict'; |
| 16 | +import { beforeEach, describe, it } from 'node:test'; |
| 17 | + |
| 18 | +let watchlistItems: PlexWatchlistItem[] = []; |
| 19 | + |
| 20 | +Object.defineProperty(PlexTvAPI.prototype, 'getWatchlist', { |
| 21 | + get() { |
| 22 | + return async () => ({ |
| 23 | + offset: 0, |
| 24 | + size: 20, |
| 25 | + totalSize: watchlistItems.length, |
| 26 | + items: watchlistItems, |
| 27 | + }); |
| 28 | + }, |
| 29 | + set() {}, |
| 30 | + configurable: true, |
| 31 | +}); |
| 32 | + |
| 33 | +let requestCalls: { mediaId: number; mediaType: MediaType }[] = []; |
| 34 | + |
| 35 | +Object.defineProperty(MediaRequest, 'request', { |
| 36 | + value: async (body: { mediaId: number; mediaType: MediaType }) => { |
| 37 | + requestCalls.push({ mediaId: body.mediaId, mediaType: body.mediaType }); |
| 38 | + return {} as MediaRequest; |
| 39 | + }, |
| 40 | + writable: true, |
| 41 | + configurable: true, |
| 42 | +}); |
| 43 | + |
| 44 | +import watchlistSync from '@server/lib/watchlistsync'; |
| 45 | + |
| 46 | +setupTestDb(); |
| 47 | + |
| 48 | +async function configureSyncUser(): Promise<User> { |
| 49 | + const userRepository = getRepository(User); |
| 50 | + const admin = await userRepository.findOneOrFail({ where: { id: 1 } }); |
| 51 | + |
| 52 | + admin.plexToken = 'test-plex-token'; |
| 53 | + admin.permissions = Permission.AUTO_REQUEST; |
| 54 | + await userRepository.save(admin); |
| 55 | + |
| 56 | + const userSettingsRepository = getRepository(UserSettings); |
| 57 | + await userSettingsRepository.save( |
| 58 | + new UserSettings({ |
| 59 | + user: admin, |
| 60 | + watchlistSyncMovies: true, |
| 61 | + watchlistSyncTv: true, |
| 62 | + }) |
| 63 | + ); |
| 64 | + |
| 65 | + return admin; |
| 66 | +} |
| 67 | + |
| 68 | +async function seedMedia( |
| 69 | + tmdbId: number, |
| 70 | + mediaType: MediaType, |
| 71 | + status: MediaStatus |
| 72 | +): Promise<void> { |
| 73 | + const mediaRepository = getRepository(Media); |
| 74 | + await mediaRepository.save( |
| 75 | + new Media({ |
| 76 | + tmdbId, |
| 77 | + mediaType, |
| 78 | + status, |
| 79 | + status4k: MediaStatus.UNKNOWN, |
| 80 | + }) |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +function movieItem(tmdbId: number, title: string): PlexWatchlistItem { |
| 85 | + return { ratingKey: `rk-${tmdbId}`, tmdbId, title, type: 'movie' }; |
| 86 | +} |
| 87 | + |
| 88 | +function showItem(tmdbId: number, title: string): PlexWatchlistItem { |
| 89 | + return { |
| 90 | + ratingKey: `rk-${tmdbId}`, |
| 91 | + tmdbId, |
| 92 | + tvdbId: tmdbId * 1000, |
| 93 | + title, |
| 94 | + type: 'show', |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +describe('WatchlistSync re-request gating', () => { |
| 99 | + beforeEach(() => { |
| 100 | + requestCalls = []; |
| 101 | + watchlistItems = []; |
| 102 | + }); |
| 103 | + |
| 104 | + it('re-requests DELETED watchlist items and skips non-requestable ones', async () => { |
| 105 | + await configureSyncUser(); |
| 106 | + |
| 107 | + await seedMedia(100, MediaType.MOVIE, MediaStatus.DELETED); |
| 108 | + await seedMedia(101, MediaType.MOVIE, MediaStatus.UNKNOWN); |
| 109 | + await seedMedia(102, MediaType.MOVIE, MediaStatus.AVAILABLE); |
| 110 | + await seedMedia(103, MediaType.MOVIE, MediaStatus.BLOCKLISTED); |
| 111 | + |
| 112 | + await seedMedia(200, MediaType.TV, MediaStatus.DELETED); |
| 113 | + await seedMedia(201, MediaType.TV, MediaStatus.AVAILABLE); |
| 114 | + |
| 115 | + watchlistItems = [ |
| 116 | + movieItem(100, 'Deleted Movie'), |
| 117 | + movieItem(101, 'Unknown Movie'), |
| 118 | + movieItem(102, 'Available Movie'), |
| 119 | + movieItem(103, 'Blocklisted Movie'), |
| 120 | + showItem(200, 'Deleted Show'), |
| 121 | + showItem(201, 'Available Show'), |
| 122 | + ]; |
| 123 | + |
| 124 | + await watchlistSync.syncWatchlist(); |
| 125 | + |
| 126 | + const requestedArray = requestCalls.map( |
| 127 | + (c) => `${c.mediaType}:${c.mediaId}` |
| 128 | + ); |
| 129 | + const requested = new Set(requestedArray); |
| 130 | + |
| 131 | + assert.strictEqual( |
| 132 | + requestedArray.length, |
| 133 | + requested.size, |
| 134 | + 'Each item should be requested exactly once' |
| 135 | + ); |
| 136 | + |
| 137 | + assert.ok( |
| 138 | + requested.has(`${MediaType.MOVIE}:100`), |
| 139 | + 'DELETED movie on the watchlist should be re-requested' |
| 140 | + ); |
| 141 | + |
| 142 | + assert.ok( |
| 143 | + requested.has(`${MediaType.MOVIE}:101`), |
| 144 | + 'UNKNOWN movie should be requested' |
| 145 | + ); |
| 146 | + assert.ok( |
| 147 | + !requested.has(`${MediaType.MOVIE}:102`), |
| 148 | + 'AVAILABLE movie should NOT be requested' |
| 149 | + ); |
| 150 | + assert.ok( |
| 151 | + !requested.has(`${MediaType.MOVIE}:103`), |
| 152 | + 'BLOCKLISTED movie should NOT be requested' |
| 153 | + ); |
| 154 | + |
| 155 | + assert.ok( |
| 156 | + requested.has(`${MediaType.TV}:200`), |
| 157 | + 'DELETED show should be re-requested' |
| 158 | + ); |
| 159 | + assert.ok( |
| 160 | + !requested.has(`${MediaType.TV}:201`), |
| 161 | + 'AVAILABLE show should NOT be requested' |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + it('re-requests DELETED watchlist items even when a stale auto-request exists', async () => { |
| 166 | + const user = await configureSyncUser(); |
| 167 | + |
| 168 | + await seedMedia(100, MediaType.MOVIE, MediaStatus.DELETED); |
| 169 | + |
| 170 | + const media = await getRepository(Media).findOneOrFail({ |
| 171 | + where: { tmdbId: 100, mediaType: MediaType.MOVIE }, |
| 172 | + }); |
| 173 | + |
| 174 | + await getRepository(MediaRequest).save( |
| 175 | + new MediaRequest({ |
| 176 | + type: MediaType.MOVIE, |
| 177 | + status: MediaRequestStatus.COMPLETED, |
| 178 | + media, |
| 179 | + requestedBy: user, |
| 180 | + is4k: false, |
| 181 | + isAutoRequest: true, |
| 182 | + }) |
| 183 | + ); |
| 184 | + |
| 185 | + watchlistItems = [movieItem(100, 'Deleted Movie')]; |
| 186 | + |
| 187 | + await watchlistSync.syncWatchlist(); |
| 188 | + |
| 189 | + const calls = requestCalls.filter( |
| 190 | + (c) => c.mediaType === MediaType.MOVIE && c.mediaId === 100 |
| 191 | + ); |
| 192 | + |
| 193 | + assert.strictEqual( |
| 194 | + calls.length, |
| 195 | + 1, |
| 196 | + 'DELETED movie should be re-requested even when a stale auto-request exists' |
| 197 | + ); |
| 198 | + }); |
| 199 | +}); |
0 commit comments