mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): timeline apply checks the overlap of a plan's end state, not each step (#7214)
* fix(cli): timeline apply checks the overlap of a plan's end state, not each step * fix(cli): drop two doc comments from the plan end-state check
This commit is contained in:
@@ -73,8 +73,9 @@ export async function runIds(args: Record<string, unknown>): Promise<void> {
|
||||
if (json) console.log(JSON.stringify(withMeta(result), null, 2));
|
||||
else console.log(`ids: stamped ${receipts.length} file${receipts.length === 1 ? "" : "s"}`);
|
||||
}
|
||||
type SettledClip = { verb: MutationVerb; ref: string };
|
||||
type ApplyEditResult =
|
||||
| { ok: true; file: string; after: string }
|
||||
| { ok: true; file: string; after: string; settled?: SettledClip }
|
||||
| { ok: false; reason: string; fix: string };
|
||||
|
||||
const PLAN_VERBS = new Set<MutationVerb>(["move", "trim", "split", "delete", "set", "duplicate"]);
|
||||
@@ -154,16 +155,32 @@ function applyPlanEdit(
|
||||
};
|
||||
const decision = decideMutation(verb, context, { ...planEdit, _: [planEdit.ref] });
|
||||
if (!decision.ok) return { ok: false, reason: decision.reason, fix: decision.fix };
|
||||
const movesClip = verb === "move" || verb === "trim";
|
||||
const conflict = mutationConflict(
|
||||
verb as MutationVerb,
|
||||
planEdit.overwrite === true,
|
||||
planEdit.overwrite === true || movesClip,
|
||||
row,
|
||||
timeline,
|
||||
decision.nextStart,
|
||||
decision.nextDuration,
|
||||
);
|
||||
if (conflict) return { ok: false, reason: conflict.reason, fix: conflict.fix };
|
||||
return { ok: true, file: row.file, after: decision.after };
|
||||
const settled = movesClip && planEdit.overwrite !== true ? { verb, ref: row.ref } : undefined;
|
||||
return { ok: true, file: row.file, after: decision.after, ...(settled && { settled }) };
|
||||
}
|
||||
|
||||
function overlapAtEnd(
|
||||
settled: SettledClip[],
|
||||
timeline: ProjectTimeline,
|
||||
): { reason: string; fix: string } | null {
|
||||
for (const { verb, ref } of settled) {
|
||||
const found = resolveRef(timeline, ref);
|
||||
if (!found.ok) continue;
|
||||
const { row } = found;
|
||||
const conflict = mutationConflict(verb, false, row, timeline, row.start, row.duration);
|
||||
if (conflict) return conflict;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
type EditPlan = { ok: true; edits: unknown[] } | { ok: false; reason: string; fix: string };
|
||||
@@ -200,13 +217,16 @@ async function applyPlanEdits(
|
||||
project: ReturnType<typeof resolveProject>,
|
||||
sourceByFile: Map<string, string>,
|
||||
): Promise<{ ok: true } | { ok: false; reason: string; fix: string }> {
|
||||
const settled: SettledClip[] = [];
|
||||
for (const edit of edits) {
|
||||
const result = applyPlanEdit(edit, timeline, project, sourceByFile);
|
||||
if (!result.ok) return result;
|
||||
if (result.settled) settled.push(result.settled);
|
||||
sourceByFile.set(result.file, result.after);
|
||||
timeline = await describeProject(project.indexPath, undefined, sourceByFile);
|
||||
}
|
||||
return { ok: true };
|
||||
const conflict = overlapAtEnd(settled, timeline);
|
||||
return conflict ? { ok: false, ...conflict } : { ok: true };
|
||||
}
|
||||
|
||||
export async function runApply(args: Record<string, unknown>): Promise<void> {
|
||||
|
||||
@@ -822,6 +822,61 @@ describe("timeline edit command", () => {
|
||||
}
|
||||
});
|
||||
|
||||
describe("a plan that shifts adjacent clips", () => {
|
||||
const row = (id: string, start: number) =>
|
||||
`<div id="${id}" data-hf-id="${id}" data-start="${start}" data-duration="2" data-track-index="0"></div>`;
|
||||
const track = () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-timeline-shift-"));
|
||||
writeFileSync(
|
||||
join(dir, "index.html"),
|
||||
`<div data-composition-id="main" data-duration="12">${row("a", 0)}${row("b", 2)}${row("c", 4)}</div>`,
|
||||
);
|
||||
return dir;
|
||||
};
|
||||
const apply = (dir: string, edits: object[]) => {
|
||||
const planPath = join(dir, "edits.json");
|
||||
writeFileSync(planPath, JSON.stringify(edits));
|
||||
return run(dir, "apply", planPath);
|
||||
};
|
||||
|
||||
it("applies when each clip moves forward ahead of the clip it passes", () => {
|
||||
const dir = track();
|
||||
try {
|
||||
const result = apply(dir, [
|
||||
{ verb: "move", ref: "#a", time: "1" },
|
||||
{ verb: "move", ref: "#b", time: "3" },
|
||||
{ verb: "move", ref: "#c", time: "5" },
|
||||
]);
|
||||
expect(result.status, result.stdout).toBe(0);
|
||||
const source = readFileSync(join(dir, "index.html"), "utf8");
|
||||
for (const [id, start] of [
|
||||
["a", 1],
|
||||
["b", 3],
|
||||
["c", 5],
|
||||
] as const)
|
||||
expect(source).toMatch(new RegExp(`id="${id}"[^>]*data-start="${start}"`));
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("refuses a plan whose end state overlaps, and writes nothing", () => {
|
||||
const dir = track();
|
||||
try {
|
||||
const before = readFileSync(join(dir, "index.html"), "utf8");
|
||||
const result = apply(dir, [
|
||||
{ verb: "move", ref: "#a", time: "1" },
|
||||
{ verb: "move", ref: "#b", time: "3" },
|
||||
]);
|
||||
expect(result.status).toBe(2);
|
||||
expect(result.stdout).toContain("would overlap");
|
||||
expect(readFileSync(join(dir, "index.html"), "utf8")).toBe(before);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("applies a JSON plan atomically and undoes its receipt", () => {
|
||||
const dir = project();
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user