mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* fix(cli): batch render, cloud render and publish print project text as visible escapes A batch render printed each failed row's error straight to the terminal. The error can quote file names and attribute values from the project, so control characters in them could move the cursor, rewrite earlier lines, set the window title or write the clipboard. That line, and the manifest path in the batch header, now go through terminalLines() and terminalText(), which keep real line breaks and show every other control character as an escape. cloud render does the same for the media lines it offloads, the "also stays in the zip" line, the largest-files list, the aspect-ratio detection line and the names of fonts that are not on this computer. publish does the same for the composition path in the blank-entry hint and for the publish failure message. The manifest file and --json output are unchanged. * fix(cli): sign-in callback ignores requests without the login's state The browser sign-in listens on a local port for the redirect back from the sign-in page. It acted on an error in that redirect before checking the state value, so any page or local process that reached the port could end a pending sign-in and put its own text in the terminal. A request with a missing or wrong state also ended the sign-in. Now only a request carrying the login's state can end it. Any other request gets the same 400 page as before and the listener keeps waiting for the real redirect or the timeout. A sign-in that is cancelled or fails on the sign-in page still carries the state, so it reports the failure as it does today. The error text from the redirect, and the "Sign-in failed" line, now show control characters as visible escapes. * fix(cli): variable issues and oversize file list print control characters as escapes Variable issue lines from render and render --batch printed the variable ids and allowed options from the project's data-composition-variables as they were. Each line now goes through terminalText(), so control characters, line feeds included, show as visible escapes. The issue objects and their messages are unchanged for other callers. When a cloud render project is over the upload limit, the list of its largest files now escapes each path, line feeds included, so a file name cannot add a line of its own. Font names in the cloud render notice are looked up as written and escaped only when printed, so a name with a trailing control character keeps its look-alike suggestion. * fix(cli): a callback from another sign-in says the terminal is still waiting The 400 page for a callback without the login's state now tells the person to finish signing in from the newest tab, since the terminal keeps waiting instead of ending the login.
560 lines
18 KiB
TypeScript
560 lines
18 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve } from "node:path";
|
|
import type { ReferenceBackground } from "@hyperframes/producer";
|
|
import {
|
|
BatchRenderInputError,
|
|
parseBatchRows,
|
|
prepareBatchRender,
|
|
resolveOutputTemplate,
|
|
runBatchRender,
|
|
} from "./batchRender.js";
|
|
|
|
let tmpDir: string;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = mkdtempSync(join(tmpdir(), "hf-local-batch-render-"));
|
|
});
|
|
|
|
afterEach(() => {
|
|
rmSync(tmpDir, { recursive: true, force: true });
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
function writeJson(name: string, content: string): string {
|
|
const path = join(tmpDir, name);
|
|
writeFileSync(path, content, "utf8");
|
|
return path;
|
|
}
|
|
|
|
function writeIndex(schema = "[]"): string {
|
|
return writeJson(
|
|
"index.html",
|
|
`<html data-composition-variables='${schema}'><body><div data-composition-id="root"></div></body></html>`,
|
|
);
|
|
}
|
|
|
|
function expectBatchError(fn: () => unknown, title: string): BatchRenderInputError {
|
|
try {
|
|
fn();
|
|
} catch (error: unknown) {
|
|
expect(error).toBeInstanceOf(BatchRenderInputError);
|
|
if (error instanceof BatchRenderInputError) {
|
|
expect(error.title).toBe(title);
|
|
return error;
|
|
}
|
|
}
|
|
throw new Error("Expected BatchRenderInputError");
|
|
}
|
|
|
|
describe("parseBatchRows", () => {
|
|
it("parses a JSON array of variable rows", () => {
|
|
expect(parseBatchRows('[{"name":"Alice"},{"name":"Bob"}]', "rows.json")).toEqual([
|
|
{ name: "Alice" },
|
|
{ name: "Bob" },
|
|
]);
|
|
});
|
|
|
|
it("parses an object with a rows array", () => {
|
|
expect(parseBatchRows('{"rows":[{"name":"Alice"}]}', "rows.json")).toEqual([{ name: "Alice" }]);
|
|
});
|
|
|
|
it("rejects non-object rows", () => {
|
|
const error = expectBatchError(
|
|
() => parseBatchRows('[{"name":"Alice"},null]', "rows.json"),
|
|
"Invalid batch row",
|
|
);
|
|
expect(error.message).toMatch(/Row 1/);
|
|
});
|
|
});
|
|
|
|
describe("resolveOutputTemplate", () => {
|
|
it("replaces row placeholders and index", () => {
|
|
expect(resolveOutputTemplate("renders/{name}-{index}.mp4", { name: "Alice" }, 3)).toBe(
|
|
"renders/Alice-3.mp4",
|
|
);
|
|
});
|
|
|
|
it("rejects missing placeholder keys", () => {
|
|
const error = expectBatchError(
|
|
() => resolveOutputTemplate("renders/{slug}.mp4", { name: "Alice" }, 0),
|
|
"Invalid output template",
|
|
);
|
|
expect(error.message).toMatch(/Missing value/);
|
|
});
|
|
});
|
|
|
|
describe("prepareBatchRender", () => {
|
|
it.each(["utf8", "utf16le"] as const)("reads a %s batch file with a BOM", (encoding) => {
|
|
const batchPath = join(tmpDir, "rows.json");
|
|
writeFileSync(batchPath, '\uFEFF[{"name":"Café","title":"Hello\\uFEFFworld"}]', encoding);
|
|
const prepared = prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(tmpDir, "renders", "{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
expect(prepared.rows).toEqual([
|
|
{
|
|
index: 0,
|
|
variables: { name: "Café", title: "Hello\uFEFFworld" },
|
|
outputPath: resolve(tmpDir, "renders", "Café.mp4"),
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("resolves output paths and the manifest path", () => {
|
|
const batchPath = writeJson("rows.json", '[{"name":"Alice"},{"name":"Bob"}]');
|
|
const outDir = join(tmpDir, "renders");
|
|
const prepared = prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(outDir, "{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
expect(prepared.rows.map((row) => row.outputPath)).toEqual([
|
|
resolve(outDir, "Alice.mp4"),
|
|
resolve(outDir, "Bob.mp4"),
|
|
]);
|
|
expect(prepared.manifestPath).toBe(resolve(outDir, "manifest.json"));
|
|
});
|
|
|
|
it("keeps row values that name subfolders inside the --output folder", () => {
|
|
const batchPath = writeJson("rows.json", '[{"name":"eu/acme"},{"name":"x/../northstar"}]');
|
|
const outDir = join(tmpDir, "renders");
|
|
const prepared = prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(outDir, "{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
expect(prepared.rows.map((row) => row.outputPath)).toEqual([
|
|
resolve(outDir, "eu", "acme.mp4"),
|
|
resolve(outDir, "northstar.mp4"),
|
|
]);
|
|
});
|
|
|
|
it.each([
|
|
{ template: join("renders", "{name}.mp4"), name: "../escaped" },
|
|
{ template: join("renders", "v{name}.mp4"), name: "/../../escaped" },
|
|
])("refuses a row value that leaves the folder of $template", ({ template, name }) => {
|
|
const batchPath = writeJson("rows.json", JSON.stringify([{ name }]));
|
|
const error = expectBatchError(
|
|
() =>
|
|
prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(tmpDir, template),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
}),
|
|
"Batch output outside --output folder",
|
|
);
|
|
expect(error.message).toMatch(/Row 0 resolves to/);
|
|
});
|
|
|
|
it.each(["{name}.mp4", join("{name}", "out.mp4")])(
|
|
"refuses a row value that leaves the working folder when %s starts with a placeholder",
|
|
(template) => {
|
|
const batchPath = writeJson("rows.json", JSON.stringify([{ name: "../escaped" }]));
|
|
const error = expectBatchError(
|
|
() =>
|
|
prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: template,
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
}),
|
|
"Batch output outside --output folder",
|
|
);
|
|
expect(error.message).toMatch(/Row 0 resolves to/);
|
|
},
|
|
);
|
|
|
|
it("keeps a template that starts with a placeholder inside the working folder", () => {
|
|
const batchPath = writeJson("rows.json", '[{"name":"acme"}]');
|
|
const prepared = prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join("{index}", "{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
expect(prepared.rows.map((row) => row.outputPath)).toEqual([resolve("0", "acme.mp4")]);
|
|
});
|
|
|
|
it("keeps a backslash in a row value or file name on every platform", () => {
|
|
const batchPath = writeJson("rows.json", '[{"name":"a\\\\b"}]');
|
|
const outDir = join(tmpDir, "renders\\");
|
|
const prepared = prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: `${outDir}{name}.mp4`,
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
expect(prepared.rows[0]?.outputPath).toBe(resolve(`${outDir}a\\b.mp4`));
|
|
});
|
|
|
|
it("lets the rows name the whole path when the template is a bare {path}", () => {
|
|
const elsewhere = join(tmpDir, "elsewhere", "launch.mp4");
|
|
const batchPath = writeJson("rows.json", JSON.stringify([{ path: elsewhere }]));
|
|
const prepared = prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: "{path}",
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
expect(prepared.rows[0]?.outputPath).toBe(resolve(elsewhere));
|
|
});
|
|
|
|
it("rejects output collisions before rendering", () => {
|
|
const batchPath = writeJson("rows.json", '[{"name":"Alice"},{"name":"Bob"}]');
|
|
const error = expectBatchError(
|
|
() =>
|
|
prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(tmpDir, "same.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
}),
|
|
"Batch output collision",
|
|
);
|
|
expect(error.message).toMatch(/Rows 0 and 1/);
|
|
});
|
|
|
|
it.each([
|
|
{ template: "{name}.mp4", rows: [{ name: "portrait" }, { name: "landscape" }] },
|
|
{
|
|
template: "{aspect}/{name}.mp4",
|
|
rows: [
|
|
{ aspect: "portrait", name: "launch" },
|
|
{ aspect: "landscape", name: "launch" },
|
|
],
|
|
},
|
|
])("preserves a batch input at the manifest destination for $template", ({ template, rows }) => {
|
|
const original = JSON.stringify(rows);
|
|
const batchPath = writeJson("manifest.json", original);
|
|
const error = expectBatchError(
|
|
() =>
|
|
prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(tmpDir, template),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
}),
|
|
"Batch manifest collision",
|
|
);
|
|
|
|
expect(error.message).toContain(batchPath);
|
|
expect(error.hint).toContain("separate output directory");
|
|
expect(readFileSync(batchPath, "utf8")).toBe(original);
|
|
});
|
|
|
|
it("fails strict variable validation per row", () => {
|
|
const batchPath = writeJson("rows.json", '[{"title":"Hello"},{"title":3}]');
|
|
const schema = '[{"id":"title","type":"string","label":"Title","default":"Untitled"}]';
|
|
const error = expectBatchError(
|
|
() =>
|
|
prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(tmpDir, "{index}.mp4"),
|
|
indexPath: writeIndex(schema),
|
|
strictVariables: true,
|
|
quiet: true,
|
|
json: true,
|
|
}),
|
|
"Variable validation failed",
|
|
);
|
|
expect(error.message).toMatch(/row 1/);
|
|
});
|
|
|
|
it("counts non-strict variable validation issues without failing", () => {
|
|
const batchPath = writeJson("rows.json", '[{"title":3}]');
|
|
const schema = '[{"id":"title","type":"string","label":"Title","default":"Untitled"}]';
|
|
const prepared = prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(tmpDir, "{index}.mp4"),
|
|
indexPath: writeIndex(schema),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
expect(prepared.variableIssueCount).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe("runBatchRender", () => {
|
|
// Windows refuses control characters in file names.
|
|
it.skipIf(process.platform === "win32")(
|
|
"prints row output paths with control characters as visible escapes",
|
|
async () => {
|
|
// Built rather than written: a literal control character in the source is a lint error.
|
|
const esc = String.fromCharCode(27);
|
|
const prepared = prepareBatchRender({
|
|
batchPath: writeJson("rows.json", '[{"name":"A\\u001b]0;x\\u0007"}]'),
|
|
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
const log = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
|
|
await runBatchRender({
|
|
prepared,
|
|
concurrency: 1,
|
|
failFast: false,
|
|
quiet: false,
|
|
json: false,
|
|
renderOne: async () => ({ durationMs: 3000, renderTimeMs: 42 }),
|
|
});
|
|
const human = log.mock.calls.map((call) => call.join(" ")).join("\n");
|
|
|
|
expect(human.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "")).not.toContain(esc);
|
|
expect(human).toContain("A\\x1b]0;x\\x07.mp4");
|
|
},
|
|
);
|
|
|
|
it("prints a row's render error with control characters as visible escapes", async () => {
|
|
const esc = String.fromCharCode(27);
|
|
const prepared = prepareBatchRender({
|
|
batchPath: writeJson("rows.json", '[{"name":"Alice"}]'),
|
|
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
const message = `Composition is empty: ${esc}]0;x\x07scene.html\r\nsecond line`;
|
|
const log = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
|
|
const manifest = await runBatchRender({
|
|
prepared,
|
|
concurrency: 1,
|
|
failFast: false,
|
|
quiet: false,
|
|
json: false,
|
|
renderOne: async () => {
|
|
throw new Error(message);
|
|
},
|
|
});
|
|
const human = log.mock.calls.map((call) => call.join(" ")).join("\n");
|
|
|
|
expect(human.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "")).not.toContain(esc);
|
|
expect(human).toContain(
|
|
"Row 0 failed: Composition is empty: \\x1b]0;x\\x07scene.html\\x0d\nsecond line",
|
|
);
|
|
expect(manifest.rows[0]?.error).toBe(message);
|
|
});
|
|
|
|
it("renders from manifest.json when the generated manifest has a separate directory", async () => {
|
|
const original = '[{"name":"Alice"},{"name":"Bob"}]';
|
|
const batchPath = writeJson("manifest.json", original);
|
|
const prepared = prepareBatchRender({
|
|
batchPath,
|
|
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
const manifest = await runBatchRender({
|
|
prepared,
|
|
concurrency: 1,
|
|
failFast: false,
|
|
quiet: true,
|
|
json: false,
|
|
renderOne: async () => ({ durationMs: 3000, renderTimeMs: 42 }),
|
|
});
|
|
|
|
expect(manifest.completed).toBe(2);
|
|
expect(readFileSync(batchPath, "utf8")).toBe(original);
|
|
expect(JSON.parse(readFileSync(prepared.manifestPath, "utf8"))).toMatchObject({
|
|
completed: 2,
|
|
batchPath,
|
|
});
|
|
});
|
|
|
|
it("writes a manifest with completed rows", async () => {
|
|
const prepared = prepareBatchRender({
|
|
batchPath: writeJson("rows.json", '[{"name":"Alice"}]'),
|
|
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
const manifest = await runBatchRender({
|
|
prepared,
|
|
concurrency: 1,
|
|
failFast: false,
|
|
quiet: true,
|
|
json: false,
|
|
renderOne: async () => ({ durationMs: 3000, renderTimeMs: 42 }),
|
|
});
|
|
|
|
expect(manifest.completed).toBe(1);
|
|
expect(manifest.failed).toBe(0);
|
|
expect(manifest.rows[0]).toMatchObject({
|
|
index: 0,
|
|
status: "completed",
|
|
durationMs: 3000,
|
|
renderTimeMs: 42,
|
|
error: null,
|
|
});
|
|
expect(readFileSync(prepared.manifestPath, "utf8")).toContain('"status": "completed"');
|
|
});
|
|
|
|
it("copies each row's reference background into the manifest and leaves a failed row null", async () => {
|
|
const references: ReferenceBackground[] = [
|
|
{ kind: "solid", rgb: [12, 34, 56] },
|
|
{ kind: "none" },
|
|
{ kind: "unsupported_non_solid" },
|
|
{ kind: "unknown" },
|
|
];
|
|
const prepared = prepareBatchRender({
|
|
batchPath: writeJson(
|
|
"rows.json",
|
|
JSON.stringify([...references.map((_, id) => ({ name: `row${id}` })), { name: "bad" }]),
|
|
),
|
|
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
const manifest = await runBatchRender({
|
|
prepared,
|
|
concurrency: 1,
|
|
failFast: false,
|
|
quiet: true,
|
|
json: false,
|
|
renderOne: async (row) => {
|
|
const reference = references[row.index];
|
|
if (!reference) throw new Error("render failed");
|
|
return { renderTimeMs: 10, reference_background: reference };
|
|
},
|
|
});
|
|
|
|
expect(manifest.rows.map((row) => row.reference_background)).toEqual([...references, null]);
|
|
const written = JSON.parse(readFileSync(prepared.manifestPath, "utf8")) as {
|
|
rows: Array<{ reference_background: unknown }>;
|
|
};
|
|
expect(written.rows.map((row) => row.reference_background)).toEqual([...references, null]);
|
|
});
|
|
|
|
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"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: true,
|
|
});
|
|
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
|
|
|
|
await runBatchRender({
|
|
prepared,
|
|
concurrency: 1,
|
|
failFast: false,
|
|
quiet: true,
|
|
json: true,
|
|
renderOne: async () => ({ renderTimeMs: 10 }),
|
|
});
|
|
|
|
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 () => {
|
|
const prepared = prepareBatchRender({
|
|
batchPath: writeJson("rows.json", '[{"name":"Alice"},{"name":"Bob"}]'),
|
|
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
const seen: number[] = [];
|
|
const manifest = await runBatchRender({
|
|
prepared,
|
|
concurrency: 1,
|
|
failFast: false,
|
|
quiet: true,
|
|
json: false,
|
|
renderOne: async (row) => {
|
|
seen.push(row.index);
|
|
if (row.index === 0) throw new Error("boom");
|
|
return { renderTimeMs: 10 };
|
|
},
|
|
});
|
|
|
|
expect(seen).toEqual([0, 1]);
|
|
expect(manifest.failed).toBe(1);
|
|
expect(manifest.completed).toBe(1);
|
|
});
|
|
|
|
it("marks unstarted rows skipped when fail-fast is enabled", async () => {
|
|
const prepared = prepareBatchRender({
|
|
batchPath: writeJson("rows.json", '[{"name":"Alice"},{"name":"Bob"},{"name":"Cleo"}]'),
|
|
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
|
|
indexPath: writeIndex(),
|
|
strictVariables: false,
|
|
quiet: true,
|
|
json: false,
|
|
});
|
|
|
|
const seen: number[] = [];
|
|
const manifest = await runBatchRender({
|
|
prepared,
|
|
concurrency: 1,
|
|
failFast: true,
|
|
quiet: true,
|
|
json: false,
|
|
renderOne: async (row) => {
|
|
seen.push(row.index);
|
|
if (row.index === 1) throw new Error("boom");
|
|
return { renderTimeMs: 10 };
|
|
},
|
|
});
|
|
|
|
expect(seen).toEqual([0, 1]);
|
|
expect(manifest.rows.map((row) => row.status)).toEqual(["completed", "failed", "skipped"]);
|
|
expect(manifest.skipped).toBe(1);
|
|
});
|
|
});
|