|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { postVideoResults } from "../postVideoResults"; |
| 3 | + |
| 4 | +vi.mock("../downloadVideoBuffer", () => ({ |
| 5 | + downloadVideoBuffer: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +const { downloadVideoBuffer } = await import("../downloadVideoBuffer"); |
| 9 | +const mockedDownload = vi.mocked(downloadVideoBuffer); |
| 10 | + |
| 11 | +describe("postVideoResults", () => { |
| 12 | + let thread: { post: ReturnType<typeof vi.fn> }; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + vi.clearAllMocks(); |
| 16 | + thread = { post: vi.fn().mockResolvedValue(undefined) }; |
| 17 | + }); |
| 18 | + |
| 19 | + it("downloads videos in parallel and posts each as a file upload", async () => { |
| 20 | + const buf1 = Buffer.from([0x01]); |
| 21 | + const buf2 = Buffer.from([0x02]); |
| 22 | + mockedDownload.mockResolvedValueOnce(buf1).mockResolvedValueOnce(buf2); |
| 23 | + |
| 24 | + const videos = [ |
| 25 | + { runId: "r1", status: "completed" as const, videoUrl: "https://cdn.example.com/v1.mp4" }, |
| 26 | + { runId: "r2", status: "completed" as const, videoUrl: "https://cdn.example.com/v2.mp4" }, |
| 27 | + ]; |
| 28 | + |
| 29 | + await postVideoResults(thread as never, videos, 0); |
| 30 | + |
| 31 | + expect(mockedDownload).toHaveBeenCalledTimes(2); |
| 32 | + expect(thread.post).toHaveBeenCalledTimes(2); |
| 33 | + expect(thread.post).toHaveBeenCalledWith( |
| 34 | + expect.objectContaining({ |
| 35 | + files: [expect.objectContaining({ filename: "v1.mp4" })], |
| 36 | + }), |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("falls back to URL link when download fails", async () => { |
| 41 | + mockedDownload.mockResolvedValue(null); |
| 42 | + |
| 43 | + const videos = [ |
| 44 | + { runId: "r1", status: "completed" as const, videoUrl: "https://cdn.example.com/v.mp4" }, |
| 45 | + ]; |
| 46 | + |
| 47 | + await postVideoResults(thread as never, videos, 0); |
| 48 | + |
| 49 | + expect(thread.post).toHaveBeenCalledWith( |
| 50 | + expect.stringContaining("https://cdn.example.com/v.mp4"), |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it("includes caption in markdown when present", async () => { |
| 55 | + mockedDownload.mockResolvedValue(Buffer.from([0x01])); |
| 56 | + |
| 57 | + const videos = [ |
| 58 | + { |
| 59 | + runId: "r1", |
| 60 | + status: "completed" as const, |
| 61 | + videoUrl: "https://cdn.example.com/v.mp4", |
| 62 | + captionText: "great song", |
| 63 | + }, |
| 64 | + ]; |
| 65 | + |
| 66 | + await postVideoResults(thread as never, videos, 0); |
| 67 | + |
| 68 | + expect(thread.post).toHaveBeenCalledWith( |
| 69 | + expect.objectContaining({ |
| 70 | + markdown: expect.stringContaining("great song"), |
| 71 | + }), |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it("posts failed run count when failedCount > 0", async () => { |
| 76 | + mockedDownload.mockResolvedValue(Buffer.from([0x01])); |
| 77 | + |
| 78 | + const videos = [ |
| 79 | + { runId: "r1", status: "completed" as const, videoUrl: "https://cdn.example.com/v.mp4" }, |
| 80 | + ]; |
| 81 | + |
| 82 | + await postVideoResults(thread as never, videos, 2); |
| 83 | + |
| 84 | + const lastCall = thread.post.mock.calls[thread.post.mock.calls.length - 1][0]; |
| 85 | + expect(lastCall).toContain("2"); |
| 86 | + expect(lastCall).toContain("failed"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("does not post failed message when failedCount is 0", async () => { |
| 90 | + mockedDownload.mockResolvedValue(Buffer.from([0x01])); |
| 91 | + |
| 92 | + const videos = [ |
| 93 | + { runId: "r1", status: "completed" as const, videoUrl: "https://cdn.example.com/v.mp4" }, |
| 94 | + ]; |
| 95 | + |
| 96 | + await postVideoResults(thread as never, videos, 0); |
| 97 | + |
| 98 | + expect(thread.post).toHaveBeenCalledTimes(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it("labels videos when there are multiple", async () => { |
| 102 | + mockedDownload.mockResolvedValue(Buffer.from([0x01])); |
| 103 | + |
| 104 | + const videos = [ |
| 105 | + { runId: "r1", status: "completed" as const, videoUrl: "https://cdn.example.com/v1.mp4" }, |
| 106 | + { runId: "r2", status: "completed" as const, videoUrl: "https://cdn.example.com/v2.mp4" }, |
| 107 | + ]; |
| 108 | + |
| 109 | + await postVideoResults(thread as never, videos, 0); |
| 110 | + |
| 111 | + expect(thread.post).toHaveBeenCalledWith( |
| 112 | + expect.objectContaining({ |
| 113 | + markdown: expect.stringContaining("Video 1"), |
| 114 | + }), |
| 115 | + ); |
| 116 | + }); |
| 117 | +}); |
0 commit comments