Skip to content

Commit a6ed56c

Browse files
committed
fix(sdk): address PR #2098 review feedback on variable CRUD
- validateOp now handles declareVariable/removeVariable (E_NO_ROOT when no composition root), matching setVariableValue's existing case — previously comp.can() returned E_UNKNOWN_OP for both. - removeVariable's undo-inverse now tags its {decl, index} reinsert payload with __kind: "reinsert" instead of relying on structural "decl"/"index" key presence to disambiguate it from a plain declareVariable patch. VariableDecl has an open index signature, so a real variable schema could legally declare its own "decl"/"index" fields and be misinterpreted by the old structural check; a regression test pins the exact collision. - getVariableValue's return type tightened from `unknown` to `string | number | boolean | FontValue | ImageValue | undefined`, matching setVariableValue's parameter type for round-trip symmetry. The underlying unknown-typed read is cast once at this SDK boundary. - Added a redo test for declareVariable/removeVariable (existing tests only covered undo).
1 parent 371c14c commit a6ed56c

6 files changed

Lines changed: 92 additions & 11 deletions

File tree

‎packages/sdk/src/engine/apply-patches.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,19 @@ function applyOne(parsed: ParsedDocument, patch: JsonPatchOp, p: ParsedPath): vo
256256
if (patch.op === "remove") {
257257
removeVariableDecl(parsed.document, p.id);
258258
} else {
259-
// Undo of removeVariable bundles {decl, index} to reinsert at the
260-
// original array position; a plain declareVariable forward/replace
261-
// patch carries the bare decl. Disambiguate on shape — VariableDecl's
262-
// own index signature means "in" narrowing can't fully eliminate it
263-
// from the union, so re-cast explicitly once shape is confirmed.
259+
// Undo of removeVariable bundles {__kind: "reinsert", decl, index} to
260+
// reinsert at the original array position; a plain declareVariable
261+
// forward/replace patch carries the bare decl. Disambiguate on the
262+
// __kind tag, not structural "decl"/"index" key presence — VariableDecl
263+
// has an open index signature, so a genuine variable schema could
264+
// legally declare its own "decl"/"index" fields, and "in" narrowing
265+
// alone can't rule that out.
264266
const value = patch.value;
265-
if (value && typeof value === "object" && "decl" in value && "index" in value) {
267+
if (
268+
value &&
269+
typeof value === "object" &&
270+
(value as { __kind?: unknown }).__kind === "reinsert"
271+
) {
266272
const wrapped = value as { decl: VariableDecl; index: number };
267273
declareVariableDecl(parsed.document, wrapped.decl, { atIndex: wrapped.index });
268274
} else {

‎packages/sdk/src/engine/mutate.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,25 @@ describe("declareVariable", () => {
999999
applyPatchesToDocument(parsed, edited.inverse);
10001000
expect(serializeDocument(parsed)).toBe(before);
10011001
});
1002+
1003+
it("a decl whose own extension data includes 'decl'/'index' keys isn't mistaken for a wrapped reinsert", () => {
1004+
// VariableDecl has an open index signature, so a real, weird schema can
1005+
// legally carry fields named "decl"/"index" as ordinary extension data —
1006+
// this must NOT be confused with removeVariable's {__kind: "reinsert",
1007+
// decl, index} inverse-patch wrapper, which is disambiguated by __kind,
1008+
// not by structural key presence.
1009+
const parsed = fresh();
1010+
const pathologicalDecl = {
1011+
id: "weird-var",
1012+
type: "string",
1013+
label: "Weird",
1014+
default: "x",
1015+
decl: "not-a-real-decl",
1016+
index: 999,
1017+
};
1018+
applyOp(parsed, { type: "declareVariable", decl: pathologicalDecl });
1019+
expect(readVarDecl(parsed, "weird-var")).toEqual(pathologicalDecl);
1020+
});
10021021
});
10031022

10041023
describe("removeVariable", () => {
@@ -1129,6 +1148,29 @@ describe("validateOp", () => {
11291148
it("returns ok:true for setCompositionMetadata (no target)", () => {
11301149
expect(validateOp(fresh(), { type: "setCompositionMetadata", width: 100 }).ok).toBe(true);
11311150
});
1151+
1152+
it("returns ok:true for declareVariable / removeVariable when a root exists", () => {
1153+
expect(
1154+
validateOp(fresh(), {
1155+
type: "declareVariable",
1156+
decl: { id: "v1", type: "string", label: "V1", default: "x" },
1157+
}).ok,
1158+
).toBe(true);
1159+
expect(validateOp(fresh(), { type: "removeVariable", id: "v1" }).ok).toBe(true);
1160+
});
1161+
1162+
it("returns ok:false / E_NO_ROOT for declareVariable / removeVariable with no root", () => {
1163+
const parsed = parseMutable(`no elements at all — just text`);
1164+
const r1 = validateOp(parsed, {
1165+
type: "declareVariable",
1166+
decl: { id: "v1", type: "string", label: "V1", default: "x" },
1167+
});
1168+
expect(r1.ok).toBe(false);
1169+
if (!r1.ok) expect(r1.code).toBe("E_NO_ROOT");
1170+
const r2 = validateOp(parsed, { type: "removeVariable", id: "v1" });
1171+
expect(r2.ok).toBe(false);
1172+
if (!r2.ok) expect(r2.code).toBe("E_NO_ROOT");
1173+
});
11321174
});
11331175

11341176
// ─── Phase 3b ops — graceful when no GSAP script, feature-detectable ────────

‎packages/sdk/src/engine/mutate.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,10 +933,13 @@ function handleRemoveVariable(parsed: ParsedDocument, id: string): MutationResul
933933
const path = variableDeclPath(id);
934934
// Bundle the original array index so undo reinserts at the same position
935935
// instead of appending — mirrors handleRemoveElement's {html, parentId,
936-
// siblingIndex} inverse value.
936+
// siblingIndex} inverse value. Tagged with __kind (rather than relying on
937+
// structural "decl"/"index" key presence) because VariableDecl has an open
938+
// index signature — a genuine variable schema could legally declare its own
939+
// "decl"/"index" fields, which a structural check alone can't rule out.
937940
return {
938941
forward: [patchRemove(path)],
939-
inverse: [patchAdd(path, { decl: removed.decl, index: removed.index })],
942+
inverse: [patchAdd(path, { __kind: "reinsert", decl: removed.decl, index: removed.index })],
940943
};
941944
}
942945

@@ -1546,6 +1549,8 @@ export function validateOp(parsed: ParsedDocument, op: EditOp): CanResult {
15461549
return CAN_OK;
15471550
}
15481551
case "setVariableValue":
1552+
case "declareVariable":
1553+
case "removeVariable":
15491554
if (findRoot(parsed.document) === null)
15501555
return canErr("E_NO_ROOT", "Composition root element not found.");
15511556
return CAN_OK;

‎packages/sdk/src/session.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,4 +625,23 @@ describe("variable declarations (Composition API)", () => {
625625
{ id: "brand-color", type: "color", label: "Brand color", default: "#0066cc" },
626626
]);
627627
});
628+
629+
it("declareVariable / removeVariable both support redo after undo", async () => {
630+
const comp = await openComposition(VARIABLES_HTML);
631+
632+
comp.declareVariable({ id: "brand-title", type: "string", label: "Title", default: "Hi" });
633+
comp.undo();
634+
comp.redo();
635+
expect(comp.listVariables()).toEqual([
636+
{ id: "brand-color", type: "color", label: "Brand color", default: "#0066cc" },
637+
{ id: "brand-title", type: "string", label: "Title", default: "Hi" },
638+
]);
639+
640+
comp.removeVariable("brand-color");
641+
comp.undo();
642+
comp.redo();
643+
expect(comp.listVariables()).toEqual([
644+
{ id: "brand-title", type: "string", label: "Title", default: "Hi" },
645+
]);
646+
});
628647
});

‎packages/sdk/src/session.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,17 @@ class CompositionImpl implements Composition {
155155
this.dispatch({ type: "setVariableValue", id, value });
156156
}
157157

158-
getVariableValue(id: string): unknown {
159-
return readVariableDefault(this.parsed.document, id);
158+
getVariableValue(id: string): string | number | boolean | FontValue | ImageValue | undefined {
159+
// readVariableDefault genuinely can't narrow beyond unknown — the schema
160+
// isn't validated at read time — so the cast lives here at the SDK
161+
// boundary rather than pushing it onto every caller of getVariableValue.
162+
return readVariableDefault(this.parsed.document, id) as
163+
| string
164+
| number
165+
| boolean
166+
| FontValue
167+
| ImageValue
168+
| undefined;
160169
}
161170

162171
listVariables(): CompositionVariable[] {

‎packages/sdk/src/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ export interface Composition {
410410
addElement(parent: HfId | null, index: number, html: string): HfId;
411411
setVariableValue(id: string, value: string | number | boolean | FontValue | ImageValue): void;
412412
/** Current `default` value for a declared variable, or undefined if undeclared/unset. */
413-
getVariableValue(id: string): unknown;
413+
getVariableValue(id: string): string | number | boolean | FontValue | ImageValue | undefined;
414414
/** Every declared variable's full schema (id/type/label/default/…), or [] when none. */
415415
listVariables(): CompositionVariable[];
416416
/**

0 commit comments

Comments
 (0)