mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-08-18 04:04:28 +08:00
test(subscribe): cover popular and share workflows (#545)
This commit is contained in:
@@ -4,11 +4,14 @@ import type {
|
||||
MediaInfo,
|
||||
Site,
|
||||
Subscribe,
|
||||
SubscribeShare,
|
||||
SubscribeShareStatistics,
|
||||
TransferDirectoryConf,
|
||||
} from '@/api/types'
|
||||
import { createMediaInfo } from './media'
|
||||
|
||||
let subscribeSeed = 1000
|
||||
let subscribeShareSeed = 2000
|
||||
let siteSeed = 100
|
||||
|
||||
/** 构造满足前端订阅契约的最小记录。 */
|
||||
@@ -35,6 +38,42 @@ export function createSubscribe(overrides: Partial<Subscribe> = {}): Subscribe {
|
||||
}
|
||||
}
|
||||
|
||||
/** 构造订阅分享列表、卡片与复用弹窗共同使用的稳定记录。 */
|
||||
export function createSubscribeShare(overrides: Partial<SubscribeShare> = {}): SubscribeShare {
|
||||
subscribeShareSeed += 1
|
||||
return {
|
||||
backdrop: `https://images.example.com/share-backdrop-${subscribeShareSeed}.jpg`,
|
||||
count: 3,
|
||||
date: '2026-07-17 12:00:00',
|
||||
id: subscribeShareSeed,
|
||||
name: `分享媒体 ${subscribeShareSeed}`,
|
||||
poster: `https://images.example.com/share-poster-${subscribeShareSeed}.jpg`,
|
||||
share_comment: `分享说明 ${subscribeShareSeed}`,
|
||||
share_title: `分享标题 ${subscribeShareSeed}`,
|
||||
share_uid: `share-user-${subscribeShareSeed}`,
|
||||
share_user: `分享用户 ${subscribeShareSeed}`,
|
||||
subscribe_id: subscribeShareSeed + 1000,
|
||||
tmdbid: subscribeShareSeed,
|
||||
type: '电影',
|
||||
vote: 8.2,
|
||||
year: '2026',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
/** 构造分享排行榜使用的稳定聚合记录。 */
|
||||
export function createSubscribeShareStatistics(
|
||||
overrides: Partial<SubscribeShareStatistics> = {},
|
||||
): SubscribeShareStatistics {
|
||||
subscribeShareSeed += 1
|
||||
return {
|
||||
share_count: 2,
|
||||
share_user: `统计用户 ${subscribeShareSeed}`,
|
||||
total_reuse_count: 5,
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
/** 构造电影媒体信息。 */
|
||||
export function createSubscribeMovie(overrides: Partial<MediaInfo> = {}): MediaInfo {
|
||||
return createMediaInfo({ type: '电影', ...overrides })
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
import type { DownloaderConf, FilterRuleGroup, Site, Subscribe, TransferDirectoryConf } from '@/api/types'
|
||||
import type {
|
||||
DownloaderConf,
|
||||
FilterRuleGroup,
|
||||
MediaInfo,
|
||||
Site,
|
||||
Subscribe,
|
||||
SubscribeShare,
|
||||
SubscribeShareStatistics,
|
||||
TransferDirectoryConf,
|
||||
} from '@/api/types'
|
||||
import { HttpResponse, http, type JsonBodyType, type RequestHandler } from 'msw'
|
||||
|
||||
const API_BASE_URL = 'http://localhost/api/v1/'
|
||||
@@ -28,12 +37,20 @@ export const subscribeApiUrls = {
|
||||
filesById: (id: number) => new URL(`subscribe/files/${id}`, API_BASE_URL).href,
|
||||
historyById: (id: number) => new URL(`subscribe/history/${id}`, API_BASE_URL).href,
|
||||
historyByType: (type: SubscribeMediaType) => new URL(`subscribe/history/${type}`, API_BASE_URL).href,
|
||||
follow: new URL('subscribe/follow', API_BASE_URL).href,
|
||||
followSubscribers: new URL('system/setting/public/FollowSubscribers', API_BASE_URL).href,
|
||||
fork: new URL('subscribe/fork', API_BASE_URL).href,
|
||||
queryByMedia: (mediaId: string) => new URL(`subscribe/media/${mediaId}`, API_BASE_URL).href,
|
||||
list: new URL('subscribe/', API_BASE_URL).href,
|
||||
orderConfig: (type: SubscribeMediaType) =>
|
||||
new URL(`user/config/${type === '电影' ? 'SubscribeMovieOrder' : 'SubscribeTvOrder'}`, API_BASE_URL).href,
|
||||
popular: new URL('subscribe/popular', API_BASE_URL).href,
|
||||
resetById: (id: number) => new URL(`subscribe/reset/${id}`, API_BASE_URL).href,
|
||||
searchById: (id: number) => new URL(`subscribe/search/${id}`, API_BASE_URL).href,
|
||||
share: new URL('subscribe/share', API_BASE_URL).href,
|
||||
shareById: (id: number) => new URL(`subscribe/share/${id}`, API_BASE_URL).href,
|
||||
shareStatistics: new URL('subscribe/share/statistics', API_BASE_URL).href,
|
||||
shares: new URL('subscribe/shares', API_BASE_URL).href,
|
||||
sites: new URL('site/rss', API_BASE_URL).href,
|
||||
statusById: (id: number) => new URL(`subscribe/status/${id}`, API_BASE_URL).href,
|
||||
update: new URL('subscribe/', API_BASE_URL).href,
|
||||
@@ -54,6 +71,108 @@ export function subscribeListHandler(
|
||||
})
|
||||
}
|
||||
|
||||
export function popularSubscribesHandler(
|
||||
response: MediaInfo[] = [],
|
||||
status = 200,
|
||||
onRequest: (url: URL) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.get(subscribeApiUrls.popular, async ({ request }) => {
|
||||
await onRequest(new URL(request.url))
|
||||
return jsonResponse(response as unknown as JsonBodyType, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function subscribeSharesHandler(
|
||||
response: SubscribeShare[] = [],
|
||||
status = 200,
|
||||
onRequest: (url: URL) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.get(subscribeApiUrls.shares, async ({ request }) => {
|
||||
await onRequest(new URL(request.url))
|
||||
return jsonResponse(response as unknown as JsonBodyType, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function shareSubscribeHandler(
|
||||
response: SubscribeMutationResponse = { success: true },
|
||||
status = 200,
|
||||
onShare: (payload: SubscribeShare) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.post(subscribeApiUrls.share, async ({ request }) => {
|
||||
const payload = (await request.json()) as SubscribeShare
|
||||
await onShare(payload)
|
||||
return jsonResponse(response, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function forkSubscribeHandler(
|
||||
response: SubscribeMutationResponse = { data: { id: 1 }, success: true },
|
||||
status = 200,
|
||||
onFork: (payload: SubscribeShare) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.post(subscribeApiUrls.fork, async ({ request }) => {
|
||||
const payload = (await request.json()) as SubscribeShare
|
||||
await onFork(payload)
|
||||
return jsonResponse(response, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function followSubscribersSettingHandler(
|
||||
users: string[] = [],
|
||||
status = 200,
|
||||
onRequest: (url: URL) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.get(subscribeApiUrls.followSubscribers, async ({ request }) => {
|
||||
await onRequest(new URL(request.url))
|
||||
return jsonResponse({ data: { value: users }, success: status < 400 }, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function followSubscriberHandler(
|
||||
response: SubscribeMutationResponse = { success: true },
|
||||
status = 200,
|
||||
onRequest: (url: URL) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.post(subscribeApiUrls.follow, async ({ request }) => {
|
||||
await onRequest(new URL(request.url))
|
||||
return jsonResponse(response, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function unfollowSubscriberHandler(
|
||||
response: SubscribeMutationResponse = { success: true },
|
||||
status = 200,
|
||||
onRequest: (url: URL) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.delete(subscribeApiUrls.follow, async ({ request }) => {
|
||||
await onRequest(new URL(request.url))
|
||||
return jsonResponse(response, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function deleteSubscribeShareHandler(
|
||||
id: number,
|
||||
response: SubscribeMutationResponse = { success: true },
|
||||
status = 200,
|
||||
onRequest: (url: URL) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.delete(subscribeApiUrls.shareById(id), async ({ request }) => {
|
||||
await onRequest(new URL(request.url))
|
||||
return jsonResponse(response, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function subscribeShareStatisticsHandler(
|
||||
response: SubscribeShareStatistics[] = [],
|
||||
status = 200,
|
||||
onRequest: (url: URL) => void | Promise<void> = () => {},
|
||||
) {
|
||||
return http.get(subscribeApiUrls.shareStatistics, async ({ request }) => {
|
||||
await onRequest(new URL(request.url))
|
||||
return jsonResponse(response as unknown as JsonBodyType, status)
|
||||
})
|
||||
}
|
||||
|
||||
export function subscribeFilesHandler(
|
||||
id: number,
|
||||
response: JsonBodyType = { episodes: {}, subscribe: null },
|
||||
|
||||
Reference in New Issue
Block a user