|
| 1 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + type TestCase, |
| 4 | + mockTwitcastingResponses, |
| 5 | +} from "../../test/mock/twitcasting"; |
| 6 | +import { TwitcastingService } from "./index"; |
| 7 | + |
| 8 | +describe("TwitcastingService", () => { |
| 9 | + let twitcastingService: TwitcastingService; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + twitcastingService = new TwitcastingService("dummy_token"); |
| 13 | + }); |
| 14 | + |
| 15 | + describe("getVideos", () => { |
| 16 | + const testCases: TestCase<{ userIds: string[] }>[] = [ |
| 17 | + { |
| 18 | + name: "should fetch videos successfully", |
| 19 | + userIds: ["user_id_1"], |
| 20 | + mockResponses: [mockTwitcastingResponses.validMovies], |
| 21 | + expectedResult: { |
| 22 | + rawId: "movie_id_1", |
| 23 | + rawChannelID: "user_id_1", |
| 24 | + title: "🔴 Live Stream", |
| 25 | + platform: "twitcasting", |
| 26 | + status: "live", |
| 27 | + viewCount: 1000, |
| 28 | + tags: [], |
| 29 | + languageCode: "default", |
| 30 | + platformIconURL: |
| 31 | + "https://raw.githubusercontent.com/sugar-cat7/vspo-portal/main/service/server/assets/icon/twitcasting.png", |
| 32 | + link: "https://twitcasting.tv/user_id_1/movie/movie_id_1", |
| 33 | + statusColor: 16711680, // Red for live |
| 34 | + startedAt: new Date(1704067200 * 1000).toISOString(), // Convert UNIX timestamp |
| 35 | + endedAt: null, |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "should handle API error - invalid token", |
| 40 | + userIds: ["invalid"], |
| 41 | + mockResponses: [mockTwitcastingResponses.invalidToken], |
| 42 | + expectedError: |
| 43 | + "Failed to fetch videos for user invalid: 401 Unauthorized", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "should handle network error", |
| 47 | + userIds: ["error"], |
| 48 | + mockResponses: [mockTwitcastingResponses.networkError], |
| 49 | + expectedError: "Network error while fetching videos for user error", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "should handle invalid response format", |
| 53 | + userIds: ["invalid_response"], |
| 54 | + mockResponses: [mockTwitcastingResponses.invalidResponse], |
| 55 | + expectedError: |
| 56 | + "Failed to fetch videos for user invalid_response: 400 Bad Request", |
| 57 | + }, |
| 58 | + ]; |
| 59 | + |
| 60 | + it.concurrent.each(testCases)( |
| 61 | + "$name", |
| 62 | + async ({ userIds, expectedError, expectedResult }) => { |
| 63 | + const result = await twitcastingService.getVideos({ userIds }); |
| 64 | + |
| 65 | + if (expectedError) { |
| 66 | + expect(result.err).toBeDefined(); |
| 67 | + expect(result.err?.message).toContain(expectedError); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + expect(result.err).toBeUndefined(); |
| 72 | + if (result.err || !expectedResult) return; |
| 73 | + |
| 74 | + const videos = result.val; |
| 75 | + expect(videos).toHaveLength(2); // 2 videos in mock response |
| 76 | + const video = videos[0]; // Test first video (live) |
| 77 | + expect(video).toMatchObject(expectedResult); |
| 78 | + |
| 79 | + // Test second video (ended) |
| 80 | + const endedVideo = videos[1]; |
| 81 | + expect(endedVideo).toMatchObject({ |
| 82 | + rawId: "movie_id_2", |
| 83 | + rawChannelID: "user_id_1", |
| 84 | + title: "Past Stream", |
| 85 | + platform: "twitcasting", |
| 86 | + status: "ended", |
| 87 | + viewCount: 5000, |
| 88 | + startedAt: new Date(1704153600 * 1000).toISOString(), // Convert UNIX timestamp |
| 89 | + }); |
| 90 | + }, |
| 91 | + ); |
| 92 | + }); |
| 93 | +}); |
0 commit comments