Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions packages/cli/src/commands/batchRender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ function expectBatchError(fn: () => unknown, title: string): BatchRenderInputErr
throw new Error("Expected BatchRenderInputError");
}

function eventType(value: unknown): string | undefined {
if (value === null || typeof value !== "object" || Array.isArray(value)) return undefined;
return "type" in value && typeof value.type === "string" ? value.type : undefined;
}

describe("parseBatchRows", () => {
it("parses a JSON array of variable rows", () => {
expect(parseBatchRows('[{"name":"Alice"},{"name":"Bob"}]', "rows.json")).toEqual([
Expand Down Expand Up @@ -192,7 +187,7 @@ describe("runBatchRender", () => {
expect(readFileSync(prepared.manifestPath, "utf8")).toContain('"status": "completed"');
});

it("emits JSON progress events when json mode is enabled", async () => {
it("emits exactly one final JSON document when json mode is enabled", async () => {
const prepared = prepareBatchRender({
batchPath: writeJson("rows.json", '[{"name":"Alice"}]'),
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
Expand All @@ -212,12 +207,14 @@ describe("runBatchRender", () => {
renderOne: async () => ({ renderTimeMs: 10 }),
});

const events = log.mock.calls.map((call): unknown => JSON.parse(String(call[0])));
expect(events.map(eventType)).toEqual([
"batch-row-start",
"batch-row-complete",
"batch-complete",
]);
expect(log).toHaveBeenCalledTimes(1);
const result = JSON.parse(String(log.mock.calls[0]?.[0])) as {
type: string;
completed: number;
rows: Array<{ status: string }>;
};
expect(result).toMatchObject({ type: "batch-complete", completed: 1 });
expect(result.rows).toEqual([expect.objectContaining({ status: "completed" })]);
});

it("continues after row failure by default", async () => {
Expand Down
52 changes: 13 additions & 39 deletions packages/cli/src/commands/batchRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,6 @@ function writeManifest(manifest: BatchManifest): void {
writeFileSync(manifest.manifestPath, JSON.stringify(manifest, null, 2) + "\n", "utf8");
}

function emitJsonEvent(event: Record<string, unknown>, json: boolean): void {
if (json) console.log(JSON.stringify(event));
}

async function renderBatchRow(
row: PreparedBatchRow,
manifest: BatchManifest,
Expand All @@ -322,11 +318,6 @@ async function renderBatchRow(
manifestRow.status = "running";
manifestRow.startedAt = new Date().toISOString();
writeManifest(manifest);
emitJsonEvent(
{ type: "batch-row-start", index: row.index, outputPath: row.outputPath },
options.json,
);

if (!options.quiet && !options.json) {
console.log(c.dim(`Batch row ${row.index}: ${row.outputPath}`));
}
Expand All @@ -339,31 +330,12 @@ async function renderBatchRow(
manifestRow.renderTimeMs = result.renderTimeMs;
manifestRow.completedAt = new Date().toISOString();
writeManifest(manifest);
emitJsonEvent(
{
type: "batch-row-complete",
index: row.index,
outputPath: row.outputPath,
durationMs: manifestRow.durationMs,
renderTimeMs: manifestRow.renderTimeMs,
},
options.json,
);
return true;
} catch (error: unknown) {
manifestRow.status = "failed";
manifestRow.error = errorMessage(error);
manifestRow.completedAt = new Date().toISOString();
writeManifest(manifest);
emitJsonEvent(
{
type: "batch-row-error",
index: row.index,
outputPath: row.outputPath,
error: manifestRow.error,
},
options.json,
);
if (!options.quiet && !options.json) {
console.log(c.error(` Row ${row.index} failed: ${manifestRow.error}`));
}
Expand Down Expand Up @@ -423,17 +395,19 @@ export async function runBatchRender(options: RunBatchRenderOptions): Promise<Ba

if (stopLaunching) markUnstartedRowsSkipped(manifest);
writeManifest(manifest);
emitJsonEvent(
{
type: "batch-complete",
manifestPath: manifest.manifestPath,
total: manifest.total,
completed: manifest.completed,
failed: manifest.failed,
skipped: manifest.skipped,
},
options.json,
);
if (options.json) {
console.log(
JSON.stringify({
type: "batch-complete",
manifestPath: manifest.manifestPath,
total: manifest.total,
completed: manifest.completed,
failed: manifest.failed,
skipped: manifest.skipped,
rows: manifest.rows,
}),
);
}

if (!options.quiet && !options.json) {
console.log("");
Expand Down
Loading
Loading