Skip to content

Commit fa1942d

Browse files
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
1 parent f3e5b81 commit fa1942d

2 files changed

Lines changed: 79 additions & 4 deletions

File tree

‎packages/cli/src/timeline/a2Commands.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ export async function runIds(args: Record<string, unknown>): Promise<void> {
7373
if (json) console.log(JSON.stringify(withMeta(result), null, 2));
7474
else console.log(`ids: stamped ${receipts.length} file${receipts.length === 1 ? "" : "s"}`);
7575
}
76+
type SettledClip = { verb: MutationVerb; ref: string };
7677
type ApplyEditResult =
77-
| { ok: true; file: string; after: string }
78+
| { ok: true; file: string; after: string; settled?: SettledClip }
7879
| { ok: false; reason: string; fix: string };
7980

8081
const PLAN_VERBS = new Set<MutationVerb>(["move", "trim", "split", "delete", "set", "duplicate"]);
@@ -154,16 +155,32 @@ function applyPlanEdit(
154155
};
155156
const decision = decideMutation(verb, context, { ...planEdit, _: [planEdit.ref] });
156157
if (!decision.ok) return { ok: false, reason: decision.reason, fix: decision.fix };
158+
const movesClip = verb === "move" || verb === "trim";
157159
const conflict = mutationConflict(
158160
verb as MutationVerb,
159-
planEdit.overwrite === true,
161+
planEdit.overwrite === true || movesClip,
160162
row,
161163
timeline,
162164
decision.nextStart,
163165
decision.nextDuration,
164166
);
165167
if (conflict) return { ok: false, reason: conflict.reason, fix: conflict.fix };
166-
return { ok: true, file: row.file, after: decision.after };
168+
const settled = movesClip && planEdit.overwrite !== true ? { verb, ref: row.ref } : undefined;
169+
return { ok: true, file: row.file, after: decision.after, ...(settled && { settled }) };
170+
}
171+
172+
function overlapAtEnd(
173+
settled: SettledClip[],
174+
timeline: ProjectTimeline,
175+
): { reason: string; fix: string } | null {
176+
for (const { verb, ref } of settled) {
177+
const found = resolveRef(timeline, ref);
178+
if (!found.ok) continue;
179+
const { row } = found;
180+
const conflict = mutationConflict(verb, false, row, timeline, row.start, row.duration);
181+
if (conflict) return conflict;
182+
}
183+
return null;
167184
}
168185

169186
type EditPlan = { ok: true; edits: unknown[] } | { ok: false; reason: string; fix: string };
@@ -200,13 +217,16 @@ async function applyPlanEdits(
200217
project: ReturnType<typeof resolveProject>,
201218
sourceByFile: Map<string, string>,
202219
): Promise<{ ok: true } | { ok: false; reason: string; fix: string }> {
220+
const settled: SettledClip[] = [];
203221
for (const edit of edits) {
204222
const result = applyPlanEdit(edit, timeline, project, sourceByFile);
205223
if (!result.ok) return result;
224+
if (result.settled) settled.push(result.settled);
206225
sourceByFile.set(result.file, result.after);
207226
timeline = await describeProject(project.indexPath, undefined, sourceByFile);
208227
}
209-
return { ok: true };
228+
const conflict = overlapAtEnd(settled, timeline);
229+
return conflict ? { ok: false, ...conflict } : { ok: true };
210230
}
211231

212232
export async function runApply(args: Record<string, unknown>): Promise<void> {

‎packages/cli/src/timeline/timeline.e2e.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,61 @@ describe("timeline edit command", () => {
822822
}
823823
});
824824

825+
describe("a plan that shifts adjacent clips", () => {
826+
const row = (id: string, start: number) =>
827+
`<div id="${id}" data-hf-id="${id}" data-start="${start}" data-duration="2" data-track-index="0"></div>`;
828+
const track = () => {
829+
const dir = mkdtempSync(join(tmpdir(), "hf-timeline-shift-"));
830+
writeFileSync(
831+
join(dir, "index.html"),
832+
`<div data-composition-id="main" data-duration="12">${row("a", 0)}${row("b", 2)}${row("c", 4)}</div>`,
833+
);
834+
return dir;
835+
};
836+
const apply = (dir: string, edits: object[]) => {
837+
const planPath = join(dir, "edits.json");
838+
writeFileSync(planPath, JSON.stringify(edits));
839+
return run(dir, "apply", planPath);
840+
};
841+
842+
it("applies when each clip moves forward ahead of the clip it passes", () => {
843+
const dir = track();
844+
try {
845+
const result = apply(dir, [
846+
{ verb: "move", ref: "#a", time: "1" },
847+
{ verb: "move", ref: "#b", time: "3" },
848+
{ verb: "move", ref: "#c", time: "5" },
849+
]);
850+
expect(result.status, result.stdout).toBe(0);
851+
const source = readFileSync(join(dir, "index.html"), "utf8");
852+
for (const [id, start] of [
853+
["a", 1],
854+
["b", 3],
855+
["c", 5],
856+
] as const)
857+
expect(source).toMatch(new RegExp(`id="${id}"[^>]*data-start="${start}"`));
858+
} finally {
859+
rmSync(dir, { recursive: true, force: true });
860+
}
861+
});
862+
863+
it("refuses a plan whose end state overlaps, and writes nothing", () => {
864+
const dir = track();
865+
try {
866+
const before = readFileSync(join(dir, "index.html"), "utf8");
867+
const result = apply(dir, [
868+
{ verb: "move", ref: "#a", time: "1" },
869+
{ verb: "move", ref: "#b", time: "3" },
870+
]);
871+
expect(result.status).toBe(2);
872+
expect(result.stdout).toContain("would overlap");
873+
expect(readFileSync(join(dir, "index.html"), "utf8")).toBe(before);
874+
} finally {
875+
rmSync(dir, { recursive: true, force: true });
876+
}
877+
});
878+
});
879+
825880
it("applies a JSON plan atomically and undoes its receipt", () => {
826881
const dir = project();
827882
try {

0 commit comments

Comments
 (0)