|
1 | 1 | import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
2 | 2 | import { execFileSync, spawnSync } from "node:child_process"; |
3 | | -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
4 | 4 | import { tmpdir } from "node:os"; |
5 | 5 | import { join } from "node:path"; |
6 | 6 | import { selectFirstFrameAfterEach } from "../commands/snapshot.js"; |
@@ -128,15 +128,54 @@ describe("a file ffprobe cannot read", () => { |
128 | 128 | expect(ffprobeReason(new Error("boom"), "x.mp4")).toBeUndefined(); |
129 | 129 | }); |
130 | 130 |
|
| 131 | + it("keeps the specific cause of a cut-short file over ffprobe's generic last line", () => { |
| 132 | + const err = { |
| 133 | + stderr: Buffer.from( |
| 134 | + "[mov,mp4,m4a,3gp,3g2,mj2 @ 0x12e808c00] moov atom not found\nhalf.mp4: Invalid data found when processing input\n", |
| 135 | + ), |
| 136 | + }; |
| 137 | + const reason = ffprobeReason(err, "half.mp4"); |
| 138 | + expect(reason).toBe("moov atom not found"); |
| 139 | + expect(unreadableFix(reason, true)).toMatch(/download it again/); |
| 140 | + // A web page named .mp4 gets the same two lines from ffprobe: only the header tells them apart. |
| 141 | + expect(unreadableFix(reason, false)).toMatch(/not a video/); |
| 142 | + }); |
| 143 | + |
131 | 144 | it.each([ |
132 | | - ["moov atom not found", /download it again/], |
133 | | - ["Invalid data found when processing input", /not a video/], |
134 | | - ["Permission denied", /permissions/], |
135 | | - [undefined, /pass a video file/], |
136 | | - ])("answers %s", (reason, fix) => { |
137 | | - expect(unreadableFix(reason)).toMatch(fix); |
| 145 | + ["moov atom not found", true, /download it again/], |
| 146 | + ["moov atom not found", false, /not a video/], |
| 147 | + ["End of file", false, /download it again/], |
| 148 | + ["Invalid data found when processing input", false, /not a video/], |
| 149 | + ["Permission denied", false, /permissions/], |
| 150 | + [undefined, false, /pass a video file/], |
| 151 | + ])("answers %s (MP4 header: %s)", (reason, mp4Header, fix) => { |
| 152 | + expect(unreadableFix(reason, mp4Header)).toMatch(fix); |
138 | 153 | }); |
139 | 154 |
|
| 155 | + it.skipIf(!ffmpeg)( |
| 156 | + "tells a cut-short download to download again, not that it is no video", |
| 157 | + () => { |
| 158 | + const dir = mkdtempSync(join(tmpdir(), "hf-cut-")); |
| 159 | + const full = join(dir, "full.mp4"); |
| 160 | + const half = join(dir, "half.mp4"); |
| 161 | + execFileSync("ffmpeg", [ |
| 162 | + ...["-v", "error", "-f", "lavfi", "-i", "testsrc=size=640x360:rate=30:duration=4"], |
| 163 | + ...["-pix_fmt", "yuv420p", full], |
| 164 | + ]); |
| 165 | + const bytes = readFileSync(full); |
| 166 | + writeFileSync(half, bytes.subarray(0, bytes.length >> 1)); |
| 167 | + try { |
| 168 | + probeVideo(half); |
| 169 | + expect.unreachable(); |
| 170 | + } catch (err) { |
| 171 | + expect((err as Fix).message).toMatch(/moov atom not found/); |
| 172 | + expect((err as Fix).fix).toMatch(/download it again/); |
| 173 | + } finally { |
| 174 | + rmSync(dir, { recursive: true, force: true }); |
| 175 | + } |
| 176 | + }, |
| 177 | + ); |
| 178 | + |
140 | 179 | it.skipIf(!ffmpeg)("puts the reason and its fix on the error", () => { |
141 | 180 | const dir = mkdtempSync(join(tmpdir(), "hf-probe-")); |
142 | 181 | const page = join(dir, "ref.mp4"); |
|
0 commit comments