Skip to content

Commit b4123be

Browse files
committed
feat(studio): move, resize and rotate, verified by reading back
`studio_transform` does what a drag does, and then checks. The box in the result is READ BACK after the write, never echoed from the request, and `applied` lists what actually took effect. That is not belt-and-braces. The plan for this unit said to re-derive the geometry handlers' behaviour rather than trust any description of them, and doing that turned up three different behaviours behind one interface. The handlers on `DomEditActionsValue` are the GSAP-AWARE wrappers, aliased in `useDomEditSession.ts:534-538`, not the CSS ones in `useDomGeometryCommits.ts` that an earlier note in this workstream described. `handleGsapAwarePathOffsetCommit` and `handleGsapAwareRotationCommit` are `if (gsapCommitMutation) { ...intercept... }` with no else branch. Their own comments say the absence is deliberate: position and rotation are written as GSAP code and there is no CSS fallback to write to. So they can return having done nothing. `handleGsapAwareBoxSizeCommit` is not like the other two. It runs through `runGestureTransaction` with separate scale and width/height routes, so resize works more generally. Reading back is what turns that middle case from a silent lie into a reported one. A move that did nothing comes back in `unchanged` with a reason. Three smaller decisions: Operations re-read between each other, so a move is judged against the box AFTER a resize in the same call. Comparing against the original would credit the resize's change to the move. Rotation is reported as dispatched, not verified. `rotate` is an individual transform property and does not appear in the computed transform, so there is no honest box-derived signal, and claiming one would be worse than saying so. x pairs with y and width pairs with height. Accepting one alone would mean inventing the other from the current value, which moves the element somewhere the caller did not ask for. The pairing rule and its minimum live in one `parsePair` helper rather than as four separate branches.
1 parent f766c84 commit b4123be

5 files changed

Lines changed: 427 additions & 4 deletions

File tree

‎packages/studio/src/webmcp/StudioAgentTools.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export function StudioAgentTools() {
3030
applyDomSelection,
3131
handleDomTextCommit,
3232
handleDomStyleCommit,
33+
handleDomPathOffsetCommit,
34+
handleDomBoxSizeCommit,
35+
handleDomRotationCommit,
3336
} = useDomEditActionsContext();
3437

3538
const getSnapshot = useCallback((): StudioLookSnapshot => {
@@ -85,6 +88,15 @@ export function StudioAgentTools() {
8588
getWriteBlockedReason: () => writeBlockedReason,
8689
setText: (value, fieldKey) => handleDomTextCommit(value, fieldKey),
8790
setStyle: (property, value) => handleDomStyleCommit(property, value),
91+
// Measured, not authored: the tool compares this before and after to
92+
// tell a real change from a handler that did nothing and resolved.
93+
readBox: (selection) => {
94+
const rect = selection.element.getBoundingClientRect();
95+
return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
96+
},
97+
moveTo: (selection, next) => handleDomPathOffsetCommit(selection, next),
98+
resizeTo: (selection, next) => handleDomBoxSizeCommit(selection, next),
99+
rotateTo: (selection, next) => handleDomRotationCommit(selection, next),
88100
getGsapDiagnostics: () => ({
89101
animations: selectedGsapAnimations,
90102
multipleTimelines: gsapMultipleTimelines,
@@ -101,6 +113,9 @@ export function StudioAgentTools() {
101113
writeBlockedReason,
102114
handleDomTextCommit,
103115
handleDomStyleCommit,
116+
handleDomPathOffsetCommit,
117+
handleDomBoxSizeCommit,
118+
handleDomRotationCommit,
104119
domEditSelection,
105120
selectedGsapAnimations,
106121
gsapMultipleTimelines,
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// @vitest-environment jsdom
2+
import { describe, expect, it, vi } from "vitest";
3+
import {
4+
studioTransform,
5+
type ElementBox,
6+
type StudioTransformResult,
7+
type TransformToolDeps,
8+
} from "./transformTools";
9+
import { expectFailure, expectOk, previewElement, selectionFor } from "../webmcpTestUtils";
10+
11+
/**
12+
* A stand-in for the rendered box. happy-dom and jsdom report all-zero rects,
13+
* so the box is injected rather than measured; these tests are about what the
14+
* tool concludes from a box, not about layout.
15+
*/
16+
function boxStore(initial: ElementBox) {
17+
const box = { ...initial };
18+
return {
19+
read: () => ({ ...box }),
20+
set: (next: Partial<ElementBox>) => Object.assign(box, next),
21+
};
22+
}
23+
24+
function transformDeps(overrides: Partial<TransformToolDeps> = {}): TransformToolDeps {
25+
const element = previewElement('<h1 id="headline">Ship it</h1>', "headline");
26+
return {
27+
getCurrentSelection: () => selectionFor(element),
28+
getWriteBlockedReason: () => null,
29+
readBox: () => ({ x: 0, y: 0, width: 100, height: 50 }),
30+
moveTo: async () => undefined,
31+
resizeTo: async () => undefined,
32+
rotateTo: async () => undefined,
33+
...overrides,
34+
};
35+
}
36+
37+
describe("studioTransform", () => {
38+
it("reports the box read back, not the box requested", async () => {
39+
const store = boxStore({ x: 0, y: 0, width: 100, height: 50 });
40+
// The handler lands somewhere other than asked, which is what a clamp or a
41+
// layout constraint does.
42+
const resizeTo = vi.fn(async () => store.set({ width: 300, height: 120 }));
43+
44+
const result = await studioTransform(transformDeps({ readBox: store.read, resizeTo }), {
45+
width: 999,
46+
height: 999,
47+
});
48+
49+
const ok = expectOk<StudioTransformResult>(result);
50+
expect(ok.box.width).toBe(300);
51+
expect(ok.box.height).toBe(120);
52+
expect(ok.applied).toContain("resize");
53+
});
54+
55+
it("reports a silent no-op as unchanged instead of success", async () => {
56+
// handleGsapAwarePathOffsetCommit is `if (gsapCommitMutation) {...}` with no
57+
// else branch. Without GSAP it resolves having written nothing, and echoing
58+
// the request back would be a lie the agent builds on.
59+
const store = boxStore({ x: 10, y: 10, width: 100, height: 50 });
60+
const moveTo = vi.fn(async () => undefined);
61+
62+
const result = expectFailure(
63+
await studioTransform(transformDeps({ readBox: store.read, moveTo }), { x: 500, y: 400 }),
64+
);
65+
66+
expect(moveTo).toHaveBeenCalled();
67+
expect(result.kind).toBe("blocked");
68+
expect(result.reason).toMatch(/did not move/);
69+
expect(result.hint).toMatch(/GSAP/);
70+
});
71+
72+
it("separates what landed from what did not, in one call", async () => {
73+
const store = boxStore({ x: 0, y: 0, width: 100, height: 50 });
74+
const resizeTo = vi.fn(async () => store.set({ width: 200, height: 80 }));
75+
const moveTo = vi.fn(async () => undefined);
76+
77+
const result = await studioTransform(transformDeps({ readBox: store.read, resizeTo, moveTo }), {
78+
x: 40,
79+
y: 40,
80+
width: 200,
81+
height: 80,
82+
});
83+
84+
const ok = expectOk<StudioTransformResult>(result);
85+
expect(ok.applied).toEqual(["resize"]);
86+
expect(ok.unchanged.move).toMatch(/did not move/);
87+
});
88+
89+
it("re-reads between operations so a later one sees the earlier result", async () => {
90+
const store = boxStore({ x: 0, y: 0, width: 100, height: 50 });
91+
const resizeTo = vi.fn(async () => store.set({ width: 200, height: 80 }));
92+
const moveTo = vi.fn(async () => store.set({ x: 40, y: 40 }));
93+
94+
const result = await studioTransform(transformDeps({ readBox: store.read, resizeTo, moveTo }), {
95+
x: 40,
96+
y: 40,
97+
width: 200,
98+
height: 80,
99+
});
100+
101+
// Move is judged against the box AFTER the resize. Comparing against the
102+
// original would credit the resize's change to the move.
103+
const ok = expectOk<StudioTransformResult>(result);
104+
expect(ok.applied).toEqual(["resize", "move"]);
105+
expect(ok.unchanged).toEqual({});
106+
});
107+
108+
it("reports rotation as dispatched rather than verified", async () => {
109+
// `rotate` is an individual transform property and does not appear in the
110+
// computed transform, so there is no honest box-derived signal for it.
111+
const rotateTo = vi.fn(async () => undefined);
112+
113+
const result = await studioTransform(transformDeps({ rotateTo }), { rotate: 15 });
114+
115+
const ok = expectOk<StudioTransformResult>(result);
116+
expect(rotateTo).toHaveBeenCalledWith(expect.anything(), { angle: 15 });
117+
expect(ok.applied).toEqual(["rotate"]);
118+
});
119+
120+
it("refuses to write while a conflict is waiting for the user", async () => {
121+
const moveTo = vi.fn();
122+
123+
const result = expectFailure(
124+
await studioTransform(
125+
transformDeps({ getWriteBlockedReason: () => "Auto-save is paused", moveTo }),
126+
{ x: 10, y: 10 },
127+
),
128+
);
129+
130+
expect(result.kind).toBe("blocked");
131+
expect(moveTo).not.toHaveBeenCalled();
132+
});
133+
134+
it("requires x and y together, and width and height together", async () => {
135+
const moveTo = vi.fn();
136+
const resizeTo = vi.fn();
137+
const deps = transformDeps({ moveTo, resizeTo });
138+
139+
expect(expectFailure(await studioTransform(deps, { x: 10 })).reason).toMatch(/together/);
140+
expect(expectFailure(await studioTransform(deps, { width: 10 })).reason).toMatch(/together/);
141+
expect(moveTo).not.toHaveBeenCalled();
142+
expect(resizeTo).not.toHaveBeenCalled();
143+
});
144+
145+
it("rejects a negative size and an empty request", async () => {
146+
const deps = transformDeps();
147+
148+
expect(expectFailure(await studioTransform(deps, { width: -1, height: 10 })).kind).toBe(
149+
"invalid",
150+
);
151+
expect(expectFailure(await studioTransform(deps, {})).reason).toMatch(/at least one/);
152+
});
153+
154+
it("rejects non-finite numbers rather than passing them to a handler", async () => {
155+
const moveTo = vi.fn();
156+
157+
const result = expectFailure(
158+
await studioTransform(transformDeps({ moveTo }), { x: Number.NaN, y: 10 }),
159+
);
160+
161+
expect(result.kind).toBe("invalid");
162+
expect(moveTo).not.toHaveBeenCalled();
163+
});
164+
165+
it("fails when nothing is selected", async () => {
166+
const moveTo = vi.fn();
167+
168+
const result = expectFailure(
169+
await studioTransform(transformDeps({ getCurrentSelection: () => null, moveTo }), {
170+
x: 1,
171+
y: 1,
172+
}),
173+
);
174+
175+
expect(result.kind).toBe("invalid");
176+
expect(result.hint).toMatch(/studio_select/);
177+
expect(moveTo).not.toHaveBeenCalled();
178+
});
179+
});

0 commit comments

Comments
 (0)