|
1 | 1 | import { EventEmitter } from "node:events"; |
2 | | -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | | -import { join } from "node:path"; |
| 4 | +import { dirname, join } from "node:path"; |
5 | 5 | import { afterEach, describe, it, expect, vi } from "vitest"; |
6 | 6 | import { ENCODER_PRESETS, getEncoderPreset, buildEncoderArgs } from "./chunkEncoder.js"; |
7 | 7 | import { renderProvenanceArgs } from "../utils/renderProvenance.js"; |
@@ -229,6 +229,50 @@ describe("encodeFramesFromDir ffmpegEncodeTimeout", () => { |
229 | 229 | }); |
230 | 230 |
|
231 | 231 | describe("encodeFramesChunkedConcat ffmpegEncodeTimeout", () => { |
| 232 | + it("isolates concurrent encodes and preserves pre-existing chunk files", async () => { |
| 233 | + const { spawn, calls } = createSpawnSpy(); |
| 234 | + vi.resetModules(); |
| 235 | + vi.doMock("child_process", () => ({ spawn })); |
| 236 | + const { encodeFramesChunkedConcat } = await import("./chunkEncoder.js"); |
| 237 | + const { root, framesDir } = createFrameFixture(); |
| 238 | + const legacyDir = join(root, "chunk-encode"); |
| 239 | + mkdirSync(legacyDir); |
| 240 | + const legacyList = join(legacyDir, "concat-list.txt"); |
| 241 | + writeFileSync(legacyList, "another render's concat list"); |
| 242 | + |
| 243 | + const results = ["first.mp4", "second.mp4"].map((name) => |
| 244 | + encodeFramesChunkedConcat( |
| 245 | + framesDir, |
| 246 | + "frame_%06d.png", |
| 247 | + join(root, name), |
| 248 | + tinyEncodeOptions, |
| 249 | + 30, |
| 250 | + ), |
| 251 | + ); |
| 252 | + expect(calls).toHaveLength(2); |
| 253 | + const chunkPaths = calls.map((call) => call.args.at(-1)); |
| 254 | + for (const call of [...calls]) emitClose(call.proc, 0); |
| 255 | + await flushManagedProcessResolution(); |
| 256 | + expect(calls).toHaveLength(4); |
| 257 | + const lists = calls.slice(2).map((call) => call.args[call.args.indexOf("-i") + 1]); |
| 258 | + for (const call of calls.slice(2)) emitClose(call.proc, 0); |
| 259 | + const completed = await Promise.all(results); |
| 260 | + |
| 261 | + expect(completed.every((result) => result.success && result.framesEncoded === 2)).toBe(true); |
| 262 | + expect(new Set(chunkPaths).size).toBe(2); |
| 263 | + expect(new Set(lists).size).toBe(2); |
| 264 | + for (let index = 0; index < 2; index++) { |
| 265 | + const chunkPath = chunkPaths[index]; |
| 266 | + const list = lists[index]; |
| 267 | + if (!chunkPath || !list) throw new Error("Expected chunk and concat paths"); |
| 268 | + expect(dirname(chunkPath)).not.toBe(legacyDir); |
| 269 | + expect(dirname(list)).toBe(dirname(chunkPath)); |
| 270 | + expect(dirname(dirname(list))).toBe(root); |
| 271 | + expect(readFileSync(list, "utf8")).toBe(`file '${chunkPath.replace(/'/g, "'\\''")}'`); |
| 272 | + } |
| 273 | + expect(readFileSync(legacyList, "utf8")).toBe("another render's concat list"); |
| 274 | + }); |
| 275 | + |
232 | 276 | it("passes config timeout to per-chunk encodes", async () => { |
233 | 277 | vi.useFakeTimers(); |
234 | 278 | const { spawn, calls } = createSpawnSpy(); |
|
0 commit comments