test(media): cover media detail flows (#559)

This commit is contained in:
InfinityPacer
2026-07-18 20:46:38 +08:00
committed by GitHub
parent c491195b19
commit 126a204b2a
8 changed files with 1302 additions and 81 deletions

View File

@@ -34,6 +34,14 @@ export function createMediaInfo(overrides: Partial<MediaInfo> = {}): MediaInfo {
}
}
/** 构造后端无法识别媒体时返回的空详情。 */
export function createEmptyMediaInfo(): MediaInfo {
return {
episode_run_time: [],
origin_country: [],
}
}
/** 构造季选择弹窗使用的最小季信息。 */
export function createMediaSeason(overrides: Partial<MediaSeason> = {}): MediaSeason {
seasonSeed += 1

View File

@@ -4,10 +4,13 @@ import { HttpResponse, http, type JsonBodyType } from 'msw'
const API_BASE_URL = 'http://localhost/api/v1/'
export const mediaApiUrls = {
details: (mediaId: string) => new URL(`media/${mediaId}`, API_BASE_URL).href,
episodeGroups: (tmdbId: number) => new URL(`media/groups/${tmdbId}`, API_BASE_URL).href,
exists: new URL('mediaserver/exists', API_BASE_URL).href,
existsRemote: new URL('mediaserver/exists_remote', API_BASE_URL).href,
groupSeasons: (episodeGroup: string) => new URL(`media/group/seasons/${episodeGroup}`, API_BASE_URL).href,
notExists: new URL('mediaserver/notexists', API_BASE_URL).href,
play: (itemId: string) => new URL(`mediaserver/play/${itemId}`, API_BASE_URL).href,
seasons: new URL('media/seasons', API_BASE_URL).href,
}
@@ -23,17 +26,41 @@ export function mediaExistsHandler(
}
export function mediaDetailsHandler(
tmdbId: number,
mediaId: number | string,
response: MediaInfo,
status = 200,
onRequest: (url: URL) => void = () => {},
) {
return http.get(new URL(`media/tmdb:${tmdbId}`, API_BASE_URL).href, ({ request }) => {
const normalizedMediaId = typeof mediaId === 'number' ? `tmdb:${mediaId}` : mediaId
return http.get(mediaApiUrls.details(normalizedMediaId), ({ request }) => {
onRequest(new URL(request.url))
return HttpResponse.json(response as unknown as JsonBodyType, { status })
})
}
export function mediaRemoteExistsHandler(
response: Record<number, number[]>,
status = 200,
onRequest: (payload: Record<string, unknown>) => void | Promise<void> = () => {},
) {
return http.post(mediaApiUrls.existsRemote, async ({ request }) => {
await onRequest((await request.json()) as Record<string, unknown>)
return HttpResponse.json(response as JsonBodyType, { status })
})
}
export function mediaPlayHandler(
itemId: string,
response: { data?: Record<string, unknown>; message?: string; success: boolean },
status = 200,
onRequest: () => void = () => {},
) {
return http.get(mediaApiUrls.play(itemId), () => {
onRequest()
return HttpResponse.json(response as JsonBodyType, { status })
})
}
export function tmdbSeasonEpisodesHandler(
tmdbId: number,
season: number,