video.controller.test.ts356 lines, 12 KB
| 1 | import request from "supertest"; |
| 2 | import { app } from "../../index"; |
| 3 | import { VideoService } from "../../services/video.service"; |
| 4 | import { AuthService } from "../../services/auth.service"; |
| 5 | import { JobsService } from "../../services/jobs.service"; |
| 6 | import { SubscriptionService } from "../../services/subscription.service"; |
| 7 | import { StatusCodes } from "http-status-codes"; |
| 8 | |
| 9 | // Mock services |
| 10 | jest.mock("../../services/video.service"); |
| 11 | jest.mock("../../services/auth.service"); |
| 12 | jest.mock("../../services/jobs.service"); |
| 13 | jest.mock("../../services/subscription.service"); |
| 14 | |
| 15 | describe("VideoController", () => { |
| 16 | const mockUserId = "user-123"; |
| 17 | const mockVideoId = "video-123"; |
| 18 | const mockVideoUrl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"; |
| 19 | const mockToken = "test-token"; |
| 20 | |
| 21 | const mockVideoInfo = { |
| 22 | title: "Mock Video Title", |
| 23 | description: "Mock description", |
| 24 | duration: 180, |
| 25 | author: "Mock Author", |
| 26 | videoUrl: mockVideoUrl, |
| 27 | thumbnail: "https://example.com/thumbnail.jpg", |
| 28 | }; |
| 29 | |
| 30 | const mockUser = { |
| 31 | id: mockUserId, |
| 32 | email: "test@example.com", |
| 33 | name: "Test User", |
| 34 | }; |
| 35 | |
| 36 | const mockVideo = { |
| 37 | id: mockVideoId, |
| 38 | url: mockVideoUrl, |
| 39 | title: "Mock Video Title", |
| 40 | description: "Mock description", |
| 41 | duration: 180, |
| 42 | author: "Mock Author", |
| 43 | thumbnail: "https://example.com/thumbnail.jpg", |
| 44 | status: "completed", |
| 45 | createdAt: new Date().toISOString(), |
| 46 | updatedAt: new Date().toISOString(), |
| 47 | transcription: { |
| 48 | text: "Mock transcription text", |
| 49 | confidence: 0.95, |
| 50 | isMusic: false, |
| 51 | createdAt: new Date().toISOString(), |
| 52 | }, |
| 53 | analysis: { |
| 54 | summary: "Mock summary", |
| 55 | keyPoints: ["Point 1", "Point 2"], |
| 56 | sentiment: "positive", |
| 57 | topics: ["Topic 1", "Topic 2"], |
| 58 | suggestedTags: ["Tag 1", "Tag 2"], |
| 59 | createdAt: new Date().toISOString(), |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | beforeEach(() => { |
| 64 | jest.clearAllMocks(); |
| 65 | |
| 66 | // Mock AuthService.verifyToken for authentication |
| 67 | (AuthService.verifyToken as jest.Mock).mockReturnValue({ |
| 68 | userId: mockUserId, |
| 69 | email: "test@example.com", |
| 70 | }); |
| 71 | |
| 72 | // Mock AuthService.getUserById |
| 73 | (AuthService.getUserById as jest.Mock).mockResolvedValue(mockUser); |
| 74 | }); |
| 75 | |
| 76 | describe("POST /api/v1/videos/info", () => { |
| 77 | it("should get video info", async () => { |
| 78 | // Setup |
| 79 | const mockVideoInfo = { |
| 80 | title: "Test Video", |
| 81 | duration: 60, |
| 82 | author: "Test Author", |
| 83 | thumbnail: "https://example.com/thumbnail.jpg", |
| 84 | }; |
| 85 | (VideoService.getVideoInfo as jest.Mock).mockResolvedValueOnce( |
| 86 | mockVideoInfo |
| 87 | ); |
| 88 | |
| 89 | // Execute - requires authentication now |
| 90 | const response = await request(app) |
| 91 | .post("/api/v1/videos/info") |
| 92 | .set("Authorization", `Bearer ${mockToken}`) |
| 93 | .send({ |
| 94 | url: mockVideoUrl, |
| 95 | }); |
| 96 | |
| 97 | // Assert |
| 98 | expect(response.status).toBe(StatusCodes.OK); |
| 99 | expect(response.body).toHaveProperty("status", "success"); |
| 100 | expect(response.body.data).toEqual(mockVideoInfo); |
| 101 | }); |
| 102 | |
| 103 | it("should handle error when getting video info", async () => { |
| 104 | // Setup |
| 105 | (VideoService.getVideoInfo as jest.Mock).mockRejectedValueOnce( |
| 106 | new Error("Failed to get video info") |
| 107 | ); |
| 108 | |
| 109 | // Execute - requires authentication now |
| 110 | const response = await request(app) |
| 111 | .post("/api/v1/videos/info") |
| 112 | .set("Authorization", `Bearer ${mockToken}`) |
| 113 | .send({ url: mockVideoUrl }); |
| 114 | |
| 115 | // Assert |
| 116 | expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 117 | expect(response.body).toHaveProperty("status", "error"); |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | describe("POST /api/v1/videos/download", () => { |
| 122 | it("should download audio", async () => { |
| 123 | // Setup |
| 124 | const mockVideoInfo = { |
| 125 | title: "Test Video", |
| 126 | duration: 60, |
| 127 | author: "Test Author", |
| 128 | thumbnail: "https://example.com/thumbnail.jpg", |
| 129 | }; |
| 130 | const mockAudioPath = "/path/to/audio.mp3"; |
| 131 | (VideoService.getVideoInfo as jest.Mock).mockResolvedValueOnce( |
| 132 | mockVideoInfo |
| 133 | ); |
| 134 | (VideoService.downloadAudio as jest.Mock).mockResolvedValueOnce( |
| 135 | mockAudioPath |
| 136 | ); |
| 137 | |
| 138 | // Execute - requires authentication now |
| 139 | const response = await request(app) |
| 140 | .post("/api/v1/videos/download") |
| 141 | .set("Authorization", `Bearer ${mockToken}`) |
| 142 | .send({ |
| 143 | url: mockVideoUrl, |
| 144 | }); |
| 145 | |
| 146 | // Assert |
| 147 | expect(response.status).toBe(StatusCodes.OK); |
| 148 | expect(VideoService.getVideoInfo).toHaveBeenCalledWith(mockVideoUrl); |
| 149 | expect(VideoService.downloadAudio).toHaveBeenCalledWith(mockVideoUrl); |
| 150 | }); |
| 151 | }); |
| 152 | |
| 153 | describe("POST /api/v1/videos/transcribe", () => { |
| 154 | it("should create transcription job", async () => { |
| 155 | // Setup |
| 156 | const mockJobId = "job-123"; |
| 157 | (VideoService.getVideoInfo as jest.Mock).mockResolvedValueOnce( |
| 158 | mockVideoInfo |
| 159 | ); |
| 160 | ( |
| 161 | SubscriptionService.checkSubscriptionLimits as jest.Mock |
| 162 | ).mockResolvedValueOnce(true); |
| 163 | (JobsService.addTranscriptionJob as jest.Mock).mockResolvedValueOnce({ |
| 164 | jobId: mockJobId, |
| 165 | }); |
| 166 | (SubscriptionService.incrementUsage as jest.Mock).mockResolvedValueOnce( |
| 167 | true |
| 168 | ); |
| 169 | |
| 170 | // Execute - requires authentication |
| 171 | const response = await request(app) |
| 172 | .post("/api/v1/videos/transcribe") |
| 173 | .set("Authorization", `Bearer ${mockToken}`) |
| 174 | .send({ url: mockVideoUrl }); |
| 175 | |
| 176 | // Assert |
| 177 | expect(response.status).toBe(StatusCodes.OK); |
| 178 | expect(response.body).toHaveProperty("status", "success"); |
| 179 | expect(response.body.data).toHaveProperty("jobId", mockJobId); |
| 180 | expect(response.body.data).toHaveProperty("videoInfo", mockVideoInfo); |
| 181 | expect(response.body.data.message).toContain("Transcription job created"); |
| 182 | expect(SubscriptionService.checkSubscriptionLimits).toHaveBeenCalledWith( |
| 183 | mockUserId, |
| 184 | mockVideoInfo.duration |
| 185 | ); |
| 186 | expect(JobsService.addTranscriptionJob).toHaveBeenCalledWith( |
| 187 | mockVideoUrl, |
| 188 | mockVideoInfo, |
| 189 | mockUser |
| 190 | ); |
| 191 | expect(SubscriptionService.incrementUsage).toHaveBeenCalledWith( |
| 192 | mockUserId, |
| 193 | mockVideoInfo.duration |
| 194 | ); |
| 195 | }); |
| 196 | |
| 197 | it("should handle subscription limit error", async () => { |
| 198 | // Setup |
| 199 | (VideoService.getVideoInfo as jest.Mock).mockResolvedValueOnce( |
| 200 | mockVideoInfo |
| 201 | ); |
| 202 | ( |
| 203 | SubscriptionService.checkSubscriptionLimits as jest.Mock |
| 204 | ).mockRejectedValueOnce(new Error("Subscription limit reached")); |
| 205 | |
| 206 | // Execute - requires authentication |
| 207 | const response = await request(app) |
| 208 | .post("/api/v1/videos/transcribe") |
| 209 | .set("Authorization", `Bearer ${mockToken}`) |
| 210 | .send({ url: mockVideoUrl }); |
| 211 | |
| 212 | // Assert |
| 213 | expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 214 | expect(response.body).toHaveProperty("status", "error"); |
| 215 | expect(JobsService.addTranscriptionJob).not.toHaveBeenCalled(); |
| 216 | expect(SubscriptionService.incrementUsage).not.toHaveBeenCalled(); |
| 217 | }); |
| 218 | }); |
| 219 | |
| 220 | describe("GET /api/v1/videos/transcription/:jobId", () => { |
| 221 | it("should get job status", async () => { |
| 222 | // Setup |
| 223 | const mockJobId = "job-123"; |
| 224 | const mockStatus = { status: "completed" }; |
| 225 | (JobsService.getJobStatus as jest.Mock).mockResolvedValueOnce(mockStatus); |
| 226 | |
| 227 | // Execute - requires authentication |
| 228 | const response = await request(app) |
| 229 | .get(`/api/v1/videos/transcription/${mockJobId}`) |
| 230 | .set("Authorization", `Bearer ${mockToken}`); |
| 231 | |
| 232 | // Assert |
| 233 | expect(response.status).toBe(StatusCodes.OK); |
| 234 | expect(JobsService.getJobStatus).toHaveBeenCalledWith(mockJobId); |
| 235 | }); |
| 236 | }); |
| 237 | |
| 238 | describe("GET /api/v1/videos", () => { |
| 239 | it("should get user videos", async () => { |
| 240 | // Setup |
| 241 | (VideoService.getUserVideos as jest.Mock).mockResolvedValueOnce([ |
| 242 | mockVideo, |
| 243 | ]); |
| 244 | |
| 245 | // Execute - requires authentication |
| 246 | const response = await request(app) |
| 247 | .get("/api/v1/videos") |
| 248 | .set("Authorization", `Bearer ${mockToken}`); |
| 249 | |
| 250 | // Assert |
| 251 | expect(response.status).toBe(StatusCodes.OK); |
| 252 | expect(response.body).toHaveProperty("status", "success"); |
| 253 | expect(Array.isArray(response.body.data)).toBe(true); |
| 254 | expect(response.body.data[0]).toHaveProperty("id", mockVideoId); |
| 255 | expect(VideoService.getUserVideos).toHaveBeenCalledWith(mockUserId); |
| 256 | }); |
| 257 | }); |
| 258 | |
| 259 | describe("GET /api/v1/videos/:id", () => { |
| 260 | it("should get video by ID", async () => { |
| 261 | // Setup |
| 262 | (VideoService.getVideoById as jest.Mock).mockResolvedValueOnce(mockVideo); |
| 263 | |
| 264 | // Execute - requires authentication |
| 265 | const response = await request(app) |
| 266 | .get(`/api/v1/videos/${mockVideoId}`) |
| 267 | .set("Authorization", `Bearer ${mockToken}`); |
| 268 | |
| 269 | // Assert |
| 270 | expect(response.status).toBe(StatusCodes.OK); |
| 271 | expect(response.body).toHaveProperty("status", "success"); |
| 272 | expect(response.body.data).toHaveProperty("id", mockVideoId); |
| 273 | expect(VideoService.getVideoById).toHaveBeenCalledWith( |
| 274 | mockVideoId, |
| 275 | mockUserId |
| 276 | ); |
| 277 | }); |
| 278 | |
| 279 | it("should handle video not found", async () => { |
| 280 | // Setup |
| 281 | (VideoService.getVideoById as jest.Mock).mockRejectedValueOnce( |
| 282 | new Error("Video not found") |
| 283 | ); |
| 284 | |
| 285 | // Execute - requires authentication |
| 286 | const response = await request(app) |
| 287 | .get(`/api/v1/videos/${mockVideoId}`) |
| 288 | .set("Authorization", `Bearer ${mockToken}`); |
| 289 | |
| 290 | // Assert |
| 291 | expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 292 | expect(response.body).toHaveProperty("status", "error"); |
| 293 | }); |
| 294 | }); |
| 295 | |
| 296 | describe("GET /api/v1/videos/jobs", () => { |
| 297 | it("should get all jobs for user", async () => { |
| 298 | // Setup |
| 299 | const mockJobs = [ |
| 300 | { id: "job-1", status: "completed" }, |
| 301 | { id: "job-2", status: "processing" }, |
| 302 | ]; |
| 303 | (JobsService.getAllJobs as jest.Mock).mockResolvedValueOnce(mockJobs); |
| 304 | |
| 305 | // Execute - requires authentication |
| 306 | const response = await request(app) |
| 307 | .get("/api/v1/videos/jobs") |
| 308 | .set("Authorization", `Bearer ${mockToken}`); |
| 309 | |
| 310 | // Assert |
| 311 | expect(response.status).toBe(StatusCodes.OK); |
| 312 | expect(response.body).toHaveProperty("status", "success"); |
| 313 | expect(Array.isArray(response.body.data)).toBe(true); |
| 314 | expect(response.body.data).toHaveLength(2); |
| 315 | expect(JobsService.getAllJobs).toHaveBeenCalledWith(mockUserId); |
| 316 | }); |
| 317 | }); |
| 318 | |
| 319 | describe("POST /api/v1/videos/jobs/:jobId/cancel", () => { |
| 320 | it("should cancel a job", async () => { |
| 321 | // Setup |
| 322 | const mockJobId = "job-123"; |
| 323 | (JobsService.cancelJob as jest.Mock).mockResolvedValueOnce({ |
| 324 | success: true, |
| 325 | message: "Job cancelled successfully", |
| 326 | }); |
| 327 | |
| 328 | // Execute - requires authentication |
| 329 | const response = await request(app) |
| 330 | .post(`/api/v1/videos/jobs/${mockJobId}/cancel`) |
| 331 | .set("Authorization", `Bearer ${mockToken}`); |
| 332 | |
| 333 | // Assert |
| 334 | expect(response.status).toBe(StatusCodes.OK); |
| 335 | expect(response.body).toHaveProperty("status", "success"); |
| 336 | expect(JobsService.cancelJob).toHaveBeenCalledWith(mockJobId); |
| 337 | }); |
| 338 | |
| 339 | it("should handle job cancellation error", async () => { |
| 340 | // Setup |
| 341 | const mockJobId = "job-123"; |
| 342 | (JobsService.cancelJob as jest.Mock).mockRejectedValueOnce( |
| 343 | new Error("Cannot cancel completed job") |
| 344 | ); |
| 345 | |
| 346 | // Execute - requires authentication |
| 347 | const response = await request(app) |
| 348 | .post(`/api/v1/videos/jobs/${mockJobId}/cancel`) |
| 349 | .set("Authorization", `Bearer ${mockToken}`); |
| 350 | |
| 351 | // Assert |
| 352 | expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR); |
| 353 | expect(response.body).toHaveProperty("status", "error"); |
| 354 | }); |
| 355 | }); |
| 356 | }); |