video.service.test.ts251 lines, 7.3 KB
| 1 | // Mock dependencies before imports |
| 2 | jest.mock("fs/promises"); |
| 3 | jest.mock("youtube-dl-exec", () => jest.fn()); |
| 4 | jest.mock("ytdl-core", () => ({ |
| 5 | getVideoID: jest.fn(), |
| 6 | })); |
| 7 | jest.mock("@ffmpeg-installer/ffmpeg", () => ({ |
| 8 | path: "/mock/ffmpeg/path", |
| 9 | })); |
| 10 | |
| 11 | // Define mock repositories |
| 12 | const mockVideoRepository = { |
| 13 | find: jest.fn(), |
| 14 | findOne: jest.fn(), |
| 15 | }; |
| 16 | |
| 17 | // Mock AppDataSource |
| 18 | jest.mock("../../config/database", () => ({ |
| 19 | AppDataSource: { |
| 20 | getRepository: jest.fn().mockReturnValue(mockVideoRepository), |
| 21 | }, |
| 22 | })); |
| 23 | |
| 24 | import { mkdir } from "fs/promises"; |
| 25 | import { StatusCodes } from "http-status-codes"; |
| 26 | import youtubeDl from "youtube-dl-exec"; |
| 27 | import ytdl from "ytdl-core"; |
| 28 | import { Video } from "../../entities/video.entity"; |
| 29 | import { VideoInfo, VideoService } from "../../services/video.service"; |
| 30 | import { AppError } from "../../utils/errors"; |
| 31 | |
| 32 | describe("VideoService", () => { |
| 33 | // Mock data |
| 34 | const mockUserId = "user-123"; |
| 35 | const mockVideoId = "video-123"; |
| 36 | const mockVideoUrl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"; |
| 37 | const mockYoutubeId = "dQw4w9WgXcQ"; |
| 38 | |
| 39 | const mockVideo = { |
| 40 | id: mockVideoId, |
| 41 | url: mockVideoUrl, |
| 42 | title: "Mock Video Title", |
| 43 | description: "Mock description", |
| 44 | duration: 180, |
| 45 | author: "Mock Author", |
| 46 | thumbnail: "https://example.com/thumbnail.jpg", |
| 47 | status: "completed", |
| 48 | user: { id: mockUserId } as any, |
| 49 | transcription: null, |
| 50 | analysis: null, |
| 51 | createdAt: new Date(), |
| 52 | updatedAt: new Date(), |
| 53 | } as unknown as Video; |
| 54 | |
| 55 | const mockVideoInfo: VideoInfo = { |
| 56 | title: "Mock Video Title", |
| 57 | description: "Mock description", |
| 58 | duration: 180, |
| 59 | author: "Mock Author", |
| 60 | videoUrl: mockVideoUrl, |
| 61 | thumbnail: "https://example.com/thumbnail.jpg", |
| 62 | }; |
| 63 | |
| 64 | beforeEach(() => { |
| 65 | jest.clearAllMocks(); |
| 66 | }); |
| 67 | |
| 68 | describe("getUserVideos", () => { |
| 69 | it("should return videos for a user", async () => { |
| 70 | // Setup |
| 71 | mockVideoRepository.find.mockResolvedValueOnce([mockVideo]); |
| 72 | |
| 73 | // Execute |
| 74 | const result = await VideoService.getUserVideos(mockUserId); |
| 75 | |
| 76 | // Assert |
| 77 | expect(mockVideoRepository.find).toHaveBeenCalledWith({ |
| 78 | where: { user: { id: mockUserId } }, |
| 79 | relations: ["transcription", "analysis"], |
| 80 | order: { createdAt: "DESC" }, |
| 81 | }); |
| 82 | expect(result).toEqual([mockVideo]); |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | describe("getVideoById", () => { |
| 87 | it("should return a video by id", async () => { |
| 88 | // Setup |
| 89 | mockVideoRepository.findOne.mockResolvedValueOnce(mockVideo); |
| 90 | |
| 91 | // Execute |
| 92 | const result = await VideoService.getVideoById(mockVideoId, mockUserId); |
| 93 | |
| 94 | // Assert |
| 95 | expect(mockVideoRepository.findOne).toHaveBeenCalledWith({ |
| 96 | where: { |
| 97 | id: mockVideoId, |
| 98 | user: { id: mockUserId }, |
| 99 | }, |
| 100 | relations: ["transcription", "analysis"], |
| 101 | }); |
| 102 | expect(result).toEqual(mockVideo); |
| 103 | }); |
| 104 | |
| 105 | it("should throw error if video not found", async () => { |
| 106 | // Setup |
| 107 | mockVideoRepository.findOne.mockResolvedValueOnce(null); |
| 108 | |
| 109 | // Execute & Assert |
| 110 | await expect( |
| 111 | VideoService.getVideoById(mockVideoId, mockUserId) |
| 112 | ).rejects.toThrow("Video not found"); |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | describe("ensureDirectoryExists", () => { |
| 117 | it("should create directory if it doesn't exist", async () => { |
| 118 | // Setup |
| 119 | (mkdir as jest.Mock).mockResolvedValueOnce(undefined); |
| 120 | |
| 121 | // Execute |
| 122 | await VideoService.ensureDirectoryExists(); |
| 123 | |
| 124 | // Assert |
| 125 | expect(mkdir).toHaveBeenCalledWith(expect.any(String), { |
| 126 | recursive: true, |
| 127 | }); |
| 128 | }); |
| 129 | }); |
| 130 | |
| 131 | describe("getVideoInfo", () => { |
| 132 | it("should return video info", async () => { |
| 133 | // Setup |
| 134 | (ytdl.getVideoID as jest.Mock).mockReturnValue(mockYoutubeId); |
| 135 | (youtubeDl as unknown as jest.Mock).mockResolvedValueOnce({ |
| 136 | title: "Mock Video Title", |
| 137 | description: "Mock description", |
| 138 | duration: 180, |
| 139 | uploader: "Mock Author", |
| 140 | thumbnail: "https://example.com/thumbnail.jpg", |
| 141 | }); |
| 142 | |
| 143 | // Execute |
| 144 | const result = await VideoService.getVideoInfo(mockVideoUrl); |
| 145 | |
| 146 | // Assert |
| 147 | expect(result).toEqual(mockVideoInfo); |
| 148 | expect(youtubeDl).toHaveBeenCalledWith(mockVideoUrl, expect.any(Object)); |
| 149 | }); |
| 150 | |
| 151 | it("should throw error for invalid video info", async () => { |
| 152 | // Setup |
| 153 | (youtubeDl as unknown as jest.Mock).mockResolvedValueOnce({ |
| 154 | // Missing required fields |
| 155 | description: "Mock description", |
| 156 | }); |
| 157 | |
| 158 | // Execute & Assert |
| 159 | await expect(VideoService.getVideoInfo(mockVideoUrl)).rejects.toEqual( |
| 160 | new AppError( |
| 161 | StatusCodes.INTERNAL_SERVER_ERROR, |
| 162 | "Failed to get video info" |
| 163 | ) |
| 164 | ); |
| 165 | }); |
| 166 | |
| 167 | it("should handle private video error", async () => { |
| 168 | // Setup |
| 169 | (youtubeDl as unknown as jest.Mock).mockRejectedValueOnce( |
| 170 | new Error("Private video") |
| 171 | ); |
| 172 | |
| 173 | // Execute & Assert |
| 174 | await expect(VideoService.getVideoInfo(mockVideoUrl)).rejects.toEqual( |
| 175 | new AppError(StatusCodes.FORBIDDEN, "This video is private") |
| 176 | ); |
| 177 | }); |
| 178 | |
| 179 | it("should handle video not found error", async () => { |
| 180 | // Setup |
| 181 | (youtubeDl as unknown as jest.Mock).mockRejectedValueOnce( |
| 182 | new Error("not available") |
| 183 | ); |
| 184 | |
| 185 | // Execute & Assert |
| 186 | await expect(VideoService.getVideoInfo(mockVideoUrl)).rejects.toEqual( |
| 187 | new AppError(StatusCodes.NOT_FOUND, "Video not found") |
| 188 | ); |
| 189 | }); |
| 190 | }); |
| 191 | |
| 192 | describe("downloadAudio", () => { |
| 193 | it("should download audio and return path", async () => { |
| 194 | // Setup |
| 195 | const mockAudioPath = "/temp/audio/dQw4w9WgXcQ.mp3"; |
| 196 | (ytdl.getVideoID as jest.Mock).mockReturnValue(mockYoutubeId); |
| 197 | (mkdir as jest.Mock).mockResolvedValueOnce(undefined); |
| 198 | (youtubeDl as unknown as jest.Mock).mockResolvedValueOnce(undefined); |
| 199 | |
| 200 | // Mock fs.stat |
| 201 | jest |
| 202 | .spyOn(require("fs/promises"), "stat") |
| 203 | .mockResolvedValueOnce({ size: 1024 }); |
| 204 | |
| 205 | // Execute |
| 206 | const result = await VideoService.downloadAudio(mockVideoUrl); |
| 207 | |
| 208 | // Assert |
| 209 | expect(result).toContain(mockYoutubeId); |
| 210 | expect(mkdir).toHaveBeenCalled(); |
| 211 | expect(youtubeDl).toHaveBeenCalledWith(mockVideoUrl, expect.any(Object)); |
| 212 | }); |
| 213 | |
| 214 | it("should throw error if audio file is empty", async () => { |
| 215 | // Setup |
| 216 | (ytdl.getVideoID as jest.Mock).mockReturnValue(mockYoutubeId); |
| 217 | (mkdir as jest.Mock).mockResolvedValueOnce(undefined); |
| 218 | (youtubeDl as unknown as jest.Mock).mockResolvedValueOnce(undefined); |
| 219 | |
| 220 | // Mock fs.stat with zero file size |
| 221 | jest |
| 222 | .spyOn(require("fs/promises"), "stat") |
| 223 | .mockResolvedValueOnce({ size: 0 }); |
| 224 | |
| 225 | // Execute & Assert |
| 226 | await expect(VideoService.downloadAudio(mockVideoUrl)).rejects.toEqual( |
| 227 | new AppError( |
| 228 | StatusCodes.INTERNAL_SERVER_ERROR, |
| 229 | "Failed to download audio" |
| 230 | ) |
| 231 | ); |
| 232 | }); |
| 233 | |
| 234 | it("should handle ffmpeg error", async () => { |
| 235 | // Setup |
| 236 | (ytdl.getVideoID as jest.Mock).mockReturnValue(mockYoutubeId); |
| 237 | (mkdir as jest.Mock).mockResolvedValueOnce(undefined); |
| 238 | (youtubeDl as unknown as jest.Mock).mockRejectedValueOnce( |
| 239 | new Error("ffmpeg error") |
| 240 | ); |
| 241 | |
| 242 | // Execute & Assert |
| 243 | await expect(VideoService.downloadAudio(mockVideoUrl)).rejects.toEqual( |
| 244 | new AppError( |
| 245 | StatusCodes.INTERNAL_SERVER_ERROR, |
| 246 | "Failed to download audio" |
| 247 | ) |
| 248 | ); |
| 249 | }); |
| 250 | }); |
| 251 | }); |