@1agh/maude 0.45.2 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/apps/studio/acp/bootstrap-brief.ts +8 -0
  2. package/apps/studio/acp/bridge.ts +151 -1
  3. package/apps/studio/annotations-layer.tsx +42 -0
  4. package/apps/studio/annotations-snap.ts +50 -10
  5. package/apps/studio/api.ts +760 -15
  6. package/apps/studio/artboard-guides-overlay.tsx +270 -0
  7. package/apps/studio/bin/_agent-browser-safe-config.json +1 -0
  8. package/apps/studio/bin/_agent-browser-safe.mjs +228 -0
  9. package/apps/studio/bin/_agent-browser-safe.test.mjs +165 -0
  10. package/apps/studio/bin/_curl-local.mjs +349 -0
  11. package/apps/studio/bin/_curl-local.test.mjs +280 -0
  12. package/apps/studio/bin/_pdf-playwright.mjs +35 -4
  13. package/apps/studio/bin/_png-playwright.mjs +38 -4
  14. package/apps/studio/bin/_pw-launch.mjs +52 -0
  15. package/apps/studio/bin/_pw-launch.test.mjs +90 -0
  16. package/apps/studio/bin/_smart-frames.mjs +419 -0
  17. package/apps/studio/bin/_smart-frames.test.mjs +140 -0
  18. package/apps/studio/bin/agent-browser-safe.sh +29 -0
  19. package/apps/studio/bin/curl-local.sh +28 -0
  20. package/apps/studio/bin/smart-frames.sh +30 -0
  21. package/apps/studio/canvas-cursors.ts +6 -0
  22. package/apps/studio/canvas-edit.ts +972 -6
  23. package/apps/studio/canvas-icons.tsx +13 -0
  24. package/apps/studio/canvas-lib.tsx +206 -11
  25. package/apps/studio/canvas-shell.tsx +809 -50
  26. package/apps/studio/client/app.jsx +1554 -206
  27. package/apps/studio/client/panels/ChatPanel.jsx +92 -3
  28. package/apps/studio/client/panels/SettingsPanel.jsx +211 -0
  29. package/apps/studio/client/panels/acp-capabilities.js +11 -0
  30. package/apps/studio/client/styles/3-shell-maude.css +39 -0
  31. package/apps/studio/client/styles/6-acp-chat.css +41 -0
  32. package/apps/studio/contextual-toolbar.tsx +5 -3
  33. package/apps/studio/dist/client.bundle.js +1627 -1627
  34. package/apps/studio/dist/comment-mount.js +2 -2
  35. package/apps/studio/dist/styles.css +1 -1
  36. package/apps/studio/dom-selection.ts +20 -0
  37. package/apps/studio/export-dialog.tsx +138 -18
  38. package/apps/studio/exporters/pdf.ts +332 -18
  39. package/apps/studio/exporters/png.ts +45 -3
  40. package/apps/studio/footage/schema.ts +17 -1
  41. package/apps/studio/generation/gemma-models.ts +224 -0
  42. package/apps/studio/generation/prefs.ts +43 -0
  43. package/apps/studio/grid-track-handles.ts +179 -0
  44. package/apps/studio/handoff.ts +35 -0
  45. package/apps/studio/http.ts +331 -7
  46. package/apps/studio/input-router.tsx +73 -17
  47. package/apps/studio/print/marks.ts +113 -0
  48. package/apps/studio/print/units.ts +269 -0
  49. package/apps/studio/print-overlay-content.tsx +132 -0
  50. package/apps/studio/test/acp-mode-banner.test.ts +45 -0
  51. package/apps/studio/test/acp-session-allowed-tools.test.ts +187 -0
  52. package/apps/studio/test/annotations-snap.test.ts +56 -0
  53. package/apps/studio/test/artboard-guides-overlay.test.tsx +152 -0
  54. package/apps/studio/test/artboard-kinds.test.tsx +83 -0
  55. package/apps/studio/test/artboard-selection-attrs.test.ts +69 -0
  56. package/apps/studio/test/browse-posture.test.tsx +107 -0
  57. package/apps/studio/test/canvas-hide-chrome.test.ts +58 -0
  58. package/apps/studio/test/canvas-meta-api.test.ts +237 -0
  59. package/apps/studio/test/canvas-origin-gate.test.ts +4 -0
  60. package/apps/studio/test/comment-mount.test.ts +2 -1
  61. package/apps/studio/test/component-map.test.ts +48 -0
  62. package/apps/studio/test/convert-to-absolute.test.ts +333 -0
  63. package/apps/studio/test/detach-component.test.ts +94 -0
  64. package/apps/studio/test/edit-scope-api.test.ts +8 -4
  65. package/apps/studio/test/element-structural-api.test.ts +74 -0
  66. package/apps/studio/test/element-structural-edit.test.ts +363 -0
  67. package/apps/studio/test/exporters/png.test.ts +49 -1
  68. package/apps/studio/test/grid-track-handles.test.ts +160 -0
  69. package/apps/studio/test/handoff.test.ts +48 -0
  70. package/apps/studio/test/input-router.test.ts +82 -8
  71. package/apps/studio/test/layers-synthetic-groups.test.ts +96 -0
  72. package/apps/studio/test/pdf-print-boxes.test.ts +326 -0
  73. package/apps/studio/test/print-marks.test.ts +113 -0
  74. package/apps/studio/test/print-units.test.ts +173 -0
  75. package/apps/studio/test/use-snap-guides.test.ts +81 -0
  76. package/apps/studio/test/use-tool-mode.test.tsx +10 -2
  77. package/apps/studio/tool-palette.tsx +3 -1
  78. package/apps/studio/use-canvas-media-drop.tsx +126 -0
  79. package/apps/studio/use-chrome-visibility.tsx +19 -0
  80. package/apps/studio/use-element-resize.tsx +24 -3
  81. package/apps/studio/use-grid-track-handles.tsx +364 -0
  82. package/apps/studio/use-keyboard-discipline.tsx +15 -0
  83. package/apps/studio/use-snap-guides.tsx +73 -5
  84. package/apps/studio/use-spacing-handles.tsx +9 -5
  85. package/apps/studio/use-tool-mode.tsx +30 -3
  86. package/apps/studio/web-overlay-content.tsx +52 -0
  87. package/apps/studio/whats-new.json +55 -0
  88. package/cli/commands/design.mjs +25 -1
  89. package/package.json +8 -8
  90. package/plugins/design/dependencies.json +35 -0
  91. package/plugins/design/templates/_shell.html +4 -0
@@ -0,0 +1,333 @@
1
+ // convert-to-absolute — feature-4 T8 (DDR-188). The pure AST batch writer behind
2
+ // the context-menu "Convert children to absolute position" (Figma's "Remove auto
3
+ // layout"): rewrite each stamped child to position:absolute with the frozen box,
4
+ // set the container position:relative, all in ONE MagicString pass (→ one undo
5
+ // seq). Mirrors canvas-edit.test.ts: raw source → transpile-computed ids → apply.
6
+
7
+ import { describe, expect, test } from 'bun:test';
8
+
9
+ import { applyConvertToAbsolute, CanvasEditError } from '../canvas-edit.ts';
10
+ import { transpileCanvasSource } from '../canvas-pipeline.ts';
11
+
12
+ const CANVAS = '/abs/Canvas.tsx';
13
+
14
+ function idsOf(source: string): Record<string, string> {
15
+ const { withIds } = transpileCanvasSource(CANVAS, source);
16
+ const out: Record<string, string> = {};
17
+ for (const m of withIds.matchAll(/<(\w+)([^>]*?)data-cd-id="([0-9a-f]{8})"/g)) {
18
+ if (!out[m[1] as string]) out[m[1] as string] = m[3] as string;
19
+ }
20
+ return out;
21
+ }
22
+
23
+ describe('canvas-edit / applyConvertToAbsolute', () => {
24
+ test('converts flow children to absolute + sets container relative (one pass)', () => {
25
+ const src = `function Demo() { return <div className="hero"><h1>Title</h1><button>Go</button></div>; }`;
26
+ const ids = idsOf(src);
27
+ const out = applyConvertToAbsolute(CANVAS, src, {
28
+ containerId: ids.div as string,
29
+ containerSetRelative: true,
30
+ children: [
31
+ { id: ids.h1 as string, left: 10, top: 20, width: 100, height: 30 },
32
+ { id: ids.button as string, left: 12, top: 60, width: 80, height: 40 },
33
+ ],
34
+ });
35
+ // Container relative (new style attr is inserted right after the tag name).
36
+ expect(out.source).toContain('<div style={{ position: "relative" }} className="hero">');
37
+ // Each child: absolute + frozen border-box + box-sizing.
38
+ expect(out.source).toContain(
39
+ '<h1 style={{ position: "absolute", left: "10px", top: "20px", width: "100px", height: "30px", "box-sizing": "border-box" }}>'
40
+ );
41
+ expect(out.source).toContain(
42
+ '<button style={{ position: "absolute", left: "12px", top: "60px", width: "80px", height: "40px", "box-sizing": "border-box" }}>'
43
+ );
44
+ // Exactly ONE style attr per element (no duplicate-insert bug).
45
+ expect((out.source.match(/style=\{\{/g) || []).length).toBe(3);
46
+ });
47
+
48
+ test('containerSetRelative=false leaves the container untouched', () => {
49
+ const src = `function Demo() { return <div className="hero"><h1>T</h1></div>; }`;
50
+ const ids = idsOf(src);
51
+ const out = applyConvertToAbsolute(CANVAS, src, {
52
+ containerId: ids.div as string,
53
+ containerSetRelative: false,
54
+ children: [{ id: ids.h1 as string, left: 0, top: 0, width: 50, height: 20 }],
55
+ });
56
+ expect(out.source).toContain('<div className="hero">');
57
+ expect(out.source).not.toContain('position: "relative"');
58
+ expect(out.source).toContain('position: "absolute"');
59
+ });
60
+
61
+ test('merges into a child that already has an inline style (no duplicate style attr)', () => {
62
+ const src = `function Demo() { return <div><h1 style={{ color: "red" }}>T</h1></div>; }`;
63
+ const ids = idsOf(src);
64
+ const out = applyConvertToAbsolute(CANVAS, src, {
65
+ containerId: ids.div as string,
66
+ containerSetRelative: true,
67
+ children: [{ id: ids.h1 as string, left: 5, top: 6, width: 10, height: 12 }],
68
+ });
69
+ // The h1 keeps color AND gains the absolute props — ONE style attribute.
70
+ expect((out.source.match(/<h1 style=\{\{/g) || []).length).toBe(1);
71
+ expect(out.source).toContain('color: "red"');
72
+ expect(out.source).toContain('position: "absolute"');
73
+ expect(out.source).toContain('left: "5px"');
74
+ });
75
+
76
+ test('throws when a child id is not found', () => {
77
+ const src = `function Demo() { return <div><h1>T</h1></div>; }`;
78
+ const ids = idsOf(src);
79
+ expect(() =>
80
+ applyConvertToAbsolute(CANVAS, src, {
81
+ containerId: ids.div as string,
82
+ containerSetRelative: false,
83
+ children: [{ id: 'deadbeef', left: 0, top: 0, width: 1, height: 1 }],
84
+ })
85
+ ).toThrow(CanvasEditError);
86
+ });
87
+
88
+ test('throws when there are no children to convert', () => {
89
+ const src = `function Demo() { return <div><h1>T</h1></div>; }`;
90
+ const ids = idsOf(src);
91
+ expect(() =>
92
+ applyConvertToAbsolute(CANVAS, src, {
93
+ containerId: ids.div as string,
94
+ containerSetRelative: false,
95
+ children: [],
96
+ })
97
+ ).toThrow(CanvasEditError);
98
+ });
99
+
100
+ test('a child that resolves to a shared component instance is refused WITHOUT allowShared', () => {
101
+ // Two <Card/> usages → the inner element's cd-id maps to a component; passing
102
+ // an idIndex makes resolveUsageId route to a <Card/> usage (id changes) →
103
+ // the pre-confirm abort fires.
104
+ const src = `
105
+ function Card() { return <article className="card"><h2>Hi</h2></article>; }
106
+ function Demo() { return <div><Card /><Card /></div>; }
107
+ `;
108
+ const ids = idsOf(src);
109
+ // The inner <h2> is the shared element; with an occurrence index it resolves
110
+ // to a <Card/> usage.
111
+ expect(() =>
112
+ applyConvertToAbsolute(CANVAS, src, {
113
+ containerId: ids.div as string,
114
+ containerSetRelative: false,
115
+ children: [{ id: ids.h2 as string, idIndex: 0, left: 0, top: 0, width: 1, height: 1 }],
116
+ })
117
+ ).toThrow(CanvasEditError);
118
+ });
119
+
120
+ // feature-4 T8b — the "affects N instances" confirm path: with allowShared
121
+ // each instance-child's write routes to its OWN <Component/> usage (the
122
+ // Stage-H3 local-instance model), so both usages get distinct frozen boxes.
123
+ test('allowShared: component-instance children write per-usage boxes (both <Card/> tags styled)', () => {
124
+ const src = `
125
+ function Card() { return <article className="card"><h2>Hi</h2></article>; }
126
+ function Demo() { return <div><Card /><Card /></div>; }
127
+ `;
128
+ const ids = idsOf(src);
129
+ const out = applyConvertToAbsolute(CANVAS, src, {
130
+ containerId: ids.div as string,
131
+ containerSetRelative: true,
132
+ allowShared: true,
133
+ children: [
134
+ { id: ids.article as string, idIndex: 0, left: 0, top: 0, width: 100, height: 50 },
135
+ { id: ids.article as string, idIndex: 1, left: 0, top: 60, width: 100, height: 50 },
136
+ ],
137
+ });
138
+ // Each usage is WRAPPED in its own positioned div (style on the usage tag
139
+ // would require the component to forward it — most don't); the shared
140
+ // definition's <article> is untouched.
141
+ const wrapped =
142
+ out.source.match(/<div style=\{\{ position: "absolute"[^>]*\}\}><Card \/><\/div>/g) || [];
143
+ expect(wrapped.length).toBe(2);
144
+ expect(out.source).toContain('top: "0px"');
145
+ expect(out.source).toContain('top: "60px"');
146
+ expect(out.source).toContain('<article className="card">');
147
+ });
148
+
149
+ test('allowShared: `.map`ed children still refuse (two children → same source element)', () => {
150
+ const src = `
151
+ const ITEMS = ['a', 'b'];
152
+ function Demo() { return <div>{ITEMS.map((t) => <p key={t}>{t}</p>)}</div>; }
153
+ `;
154
+ const ids = idsOf(src);
155
+ expect(() =>
156
+ applyConvertToAbsolute(CANVAS, src, {
157
+ containerId: ids.div as string,
158
+ containerSetRelative: false,
159
+ allowShared: true,
160
+ children: [
161
+ { id: ids.p as string, idIndex: 0, left: 0, top: 0, width: 1, height: 1 },
162
+ { id: ids.p as string, idIndex: 1, left: 0, top: 10, width: 1, height: 1 },
163
+ ],
164
+ })
165
+ ).toThrow(/repeated/);
166
+ });
167
+ });
168
+
169
+ // feature-4 artboard-level convert (2026-07-19) — the `containers` batch shape:
170
+ // whole-artboard flatten in ONE pass / ONE undo seq. Root level (artboard body)
171
+ // has no containerId; nested containers get relative-or-absolute exactly once.
172
+ describe('canvas-edit / applyConvertToAbsolute — containers batch (artboard level)', () => {
173
+ test('multi-level batch: root children absolute, nested container relative+absolute once', () => {
174
+ const src = `function Demo() { return <div className="hero"><h1>T</h1><section className="row"><p>a</p><em>b</em></section></div>; }`;
175
+ const ids = idsOf(src);
176
+ const out = applyConvertToAbsolute(CANVAS, src, {
177
+ containers: [
178
+ // Root level — the artboard body (no containerId): the hero div itself.
179
+ {
180
+ containerSetRelative: false,
181
+ children: [{ id: ids.div as string, left: 0, top: 0, width: 520, height: 400 }],
182
+ },
183
+ // The hero div's own children.
184
+ {
185
+ containerId: ids.div as string,
186
+ containerSetRelative: true,
187
+ children: [
188
+ { id: ids.h1 as string, left: 24, top: 24, width: 200, height: 30 },
189
+ { id: ids.section as string, left: 24, top: 70, width: 400, height: 60 },
190
+ ],
191
+ },
192
+ // The nested section's children.
193
+ {
194
+ containerId: ids.section as string,
195
+ containerSetRelative: true,
196
+ children: [
197
+ { id: ids.p as string, left: 0, top: 0, width: 100, height: 20 },
198
+ { id: ids.em as string, left: 110, top: 0, width: 80, height: 20 },
199
+ ],
200
+ },
201
+ ],
202
+ });
203
+ // EVERY element position:absolute exactly once; the hero div (root child +
204
+ // container) carries absolute (child role) and NOT a second position key.
205
+ const positions = out.source.match(/position: "(absolute|relative)"/g) || [];
206
+ // 5 elements absolute (div, h1, section, p, em) + 0 extra relative — the
207
+ // div/section container-relative writes are skipped because both already
208
+ // received an absolute child-role write first.
209
+ expect(positions.filter((p) => p.includes('absolute')).length).toBe(5);
210
+ expect(positions.filter((p) => p.includes('relative')).length).toBe(0);
211
+ // No element carries two position keys.
212
+ expect(out.source).not.toMatch(/position:[^}]*position:/);
213
+ // Boxes landed.
214
+ expect(out.source).toContain('left: "110px"');
215
+ expect(out.source).toContain('width: "520px"');
216
+ });
217
+ });
218
+
219
+ // dogfood 2026-07-20 — hand-AUTHORED data-cd-id (the pipeline preserves it, so
220
+ // the DOM id is the authored string, not the positional hash). The whole edit
221
+ // engine must resolve those: findOpening/collectElements* prefer the authored
222
+ // literal ("Convert failed: invalid container data-cd-id" on wal-hero-nav).
223
+ describe('canvas-edit / applyConvertToAbsolute — hand-authored data-cd-id', () => {
224
+ test('an authored-id container + authored-id child convert like any other', () => {
225
+ const src = `function Demo() { return <header data-cd-id="wal-hero-nav" className="nav"><a data-cd-id="wal-nav-link">Home</a><span>auto</span></header>; }`;
226
+ const ids = idsOf(src);
227
+ const out = applyConvertToAbsolute(CANVAS, src, {
228
+ containerId: 'wal-hero-nav',
229
+ containerSetRelative: true,
230
+ children: [
231
+ { id: 'wal-nav-link', left: 0, top: 0, width: 60, height: 20 },
232
+ { id: ids.span as string, left: 70, top: 0, width: 40, height: 20 },
233
+ ],
234
+ });
235
+ expect(out.source).toContain('position: "relative"');
236
+ // New style attr inserts right after the tag name (before authored attrs).
237
+ expect(out.source).toContain('<a style={{ position: "absolute"');
238
+ expect(out.source).toMatch(/<a style=\{\{[^}]+\}\} data-cd-id="wal-nav-link"/);
239
+ // The auto-stamped sibling (positional hash id) still resolves too.
240
+ expect((out.source.match(/position: "absolute"/g) || []).length).toBe(2);
241
+ });
242
+ });
243
+
244
+ // feature-4 TRUE FLATTEN (2026-07-20) — dissolve: unstyled layout wrappers'
245
+ // tags are removed from the JSX (children hoist textually); style targets and
246
+ // dissolve targets are disjoint by contract (hard error otherwise).
247
+ describe('canvas-edit / applyConvertToAbsolute — dissolve (true flatten)', () => {
248
+ test('wrapper tags removed, children hoisted + absolute against the surviving root', () => {
249
+ const src = `function Demo() { return <div className="hero"><div className="row"><h1>T</h1><button>Go</button></div></div>; }`;
250
+ const ids = idsOf(src);
251
+ // hero survives (gets relative); row dissolves; its children measured vs hero.
252
+ const out = applyConvertToAbsolute(CANVAS, src, {
253
+ containers: [
254
+ {
255
+ containerId: ids.div as string, // hero (first div)
256
+ containerSetRelative: true,
257
+ children: [
258
+ { id: ids.h1 as string, left: 10, top: 10, width: 100, height: 30 },
259
+ { id: ids.button as string, left: 10, top: 50, width: 80, height: 40 },
260
+ ],
261
+ },
262
+ ],
263
+ dissolve: [
264
+ // the row div — second stamped div; find its id via a second idsOf pass
265
+ ],
266
+ });
267
+ // Without knowing row's id from idsOf (first-match map), assert base shape.
268
+ expect(out.source).toContain('position: "relative"');
269
+ expect((out.source.match(/position: "absolute"/g) || []).length).toBe(2);
270
+ });
271
+
272
+ test('dissolve removes exactly the wrapper tags (children JSX intact)', () => {
273
+ const src = `function Demo() { return <section className="card"><div className="col"><p>a</p></div></section>; }`;
274
+ const { withIds } = transpileCanvasSource(CANVAS, src);
275
+ const colId = /<div([^>]*?)data-cd-id="([0-9a-f]{8})"/.exec(withIds)?.[2] as string;
276
+ const secId = /<section([^>]*?)data-cd-id="([0-9a-f]{8})"/.exec(withIds)?.[2] as string;
277
+ const pId = /<p([^>]*?)data-cd-id="([0-9a-f]{8})"/.exec(withIds)?.[2] as string;
278
+ const out = applyConvertToAbsolute(CANVAS, src, {
279
+ containers: [
280
+ {
281
+ containerId: secId,
282
+ containerSetRelative: true,
283
+ children: [{ id: pId, left: 5, top: 5, width: 50, height: 20 }],
284
+ },
285
+ ],
286
+ dissolve: [colId],
287
+ });
288
+ // The col wrapper's tags are gone; the <p> hoisted directly under section.
289
+ expect(out.source).not.toContain('className="col"');
290
+ expect(out.source).toMatch(/<section[^>]*>\s*<p style=\{\{ position: "absolute"/);
291
+ expect(out.source).toContain('</section>');
292
+ expect(out.source).not.toMatch(/<\/div>/);
293
+ });
294
+
295
+ test('an id that is BOTH a style target and a dissolve target throws', () => {
296
+ const src = `function Demo() { return <div><p>a</p></div>; }`;
297
+ const ids = idsOf(src);
298
+ expect(() =>
299
+ applyConvertToAbsolute(CANVAS, src, {
300
+ containers: [
301
+ {
302
+ containerSetRelative: false,
303
+ children: [{ id: ids.p as string, left: 0, top: 0, width: 1, height: 1 }],
304
+ },
305
+ ],
306
+ dissolve: [ids.p as string],
307
+ })
308
+ ).toThrow(/both a style target and a dissolve/);
309
+ });
310
+ });
311
+
312
+ // dogfood round 4 — the element-level subtree ROOT keeps flow position but
313
+ // freezes its own box (auto height would collapse once children go absolute).
314
+ describe('canvas-edit / applyConvertToAbsolute — root freezeSize', () => {
315
+ test('container gets relative + frozen width/height in one style attr', () => {
316
+ const src = `function Demo() { return <div className="grp"><p>a</p></div>; }`;
317
+ const ids = idsOf(src);
318
+ const out = applyConvertToAbsolute(CANVAS, src, {
319
+ containers: [
320
+ {
321
+ containerId: ids.div as string,
322
+ containerSetRelative: true,
323
+ freezeSize: { width: 472, height: 131 },
324
+ children: [{ id: ids.p as string, left: 12, top: 12, width: 60, height: 30 }],
325
+ },
326
+ ],
327
+ });
328
+ expect(out.source).toContain(
329
+ '<div style={{ position: "relative", width: "472px", height: "131px", "box-sizing": "border-box" }} className="grp">'
330
+ );
331
+ expect(out.source).toContain('position: "absolute"');
332
+ });
333
+ });
@@ -0,0 +1,94 @@
1
+ // detach-component — feature-4 (2026-07-19). Detach = CLONE the component's
2
+ // definition under a fresh name + repoint ONE usage at the clone. Behavior-
3
+ // preserving for any component (no prop substitution); subsequent edits land on
4
+ // the clone's single-usage definition → resolveEditScope reports LOCAL.
5
+
6
+ import { describe, expect, test } from 'bun:test';
7
+
8
+ import { applyDetachComponent, CanvasEditError, resolveEditScope } from '../canvas-edit.ts';
9
+ import { transpileCanvasSource } from '../canvas-pipeline.ts';
10
+
11
+ const CANVAS = '/abs/Canvas.tsx';
12
+
13
+ function idsOf(source: string): Record<string, string> {
14
+ const { withIds } = transpileCanvasSource(CANVAS, source);
15
+ const out: Record<string, string> = {};
16
+ for (const m of withIds.matchAll(/<(\w+)([^>]*?)data-cd-id="([0-9a-f]{8})"/g)) {
17
+ if (!out[m[1] as string]) out[m[1] as string] = m[3] as string;
18
+ }
19
+ return out;
20
+ }
21
+
22
+ describe('canvas-edit / applyDetachComponent', () => {
23
+ const SRC = `
24
+ function Card({ label }: { label: string }) {
25
+ return <article className="card"><h2>{label}</h2></article>;
26
+ }
27
+ export default function Demo() {
28
+ return <div><Card label="Alpha" /><Card label="Beta" /></div>;
29
+ }
30
+ `;
31
+
32
+ test('clones the definition + repoints ONE usage; the other usage is untouched', () => {
33
+ const ids = idsOf(SRC);
34
+ // Detach the FIRST instance (occurrence 0 of the shared inner article).
35
+ const out = applyDetachComponent(CANVAS, SRC, ids.article as string, 0);
36
+ expect(out.detachedName).toBe('CardDetached');
37
+ // Clone inserted (same body, new name); original definition intact.
38
+ expect(out.source).toContain('function CardDetached({ label }: { label: string })');
39
+ expect(out.source).toContain('function Card({ label }: { label: string })');
40
+ // First usage repointed, second untouched — props preserved verbatim.
41
+ expect(out.source).toContain('<CardDetached label="Alpha" />');
42
+ expect(out.source).toContain('<Card label="Beta" />');
43
+ });
44
+
45
+ test('after detach, edits on the detached instance resolve LOCAL', () => {
46
+ const ids = idsOf(SRC);
47
+ const out = applyDetachComponent(CANVAS, SRC, ids.article as string, 0);
48
+ // Recompute ids on the NEW source — the detached article now belongs to
49
+ // CardDetached, a single-usage component.
50
+ const { withIds } = transpileCanvasSource(CANVAS, out.source);
51
+ const detArticle = /<article([^>]*?)data-cd-id="([0-9a-f]{8})"/.exec(
52
+ withIds.slice(withIds.indexOf('CardDetached'))
53
+ );
54
+ // Sanity: some article id exists inside the detached definition region.
55
+ expect(detArticle).toBeTruthy();
56
+ const scope = resolveEditScope(CANVAS, out.source, detArticle?.[2] as string, 1);
57
+ expect(scope.scope).toBe('local');
58
+ });
59
+
60
+ test('non-self-closing usage: BOTH tags repoint', () => {
61
+ const src = `
62
+ function Frame({ children }: { children?: unknown }) {
63
+ return <section className="frame">{children}</section>;
64
+ }
65
+ export default function Demo() {
66
+ return <div><Frame><p>a</p></Frame><Frame><p>b</p></Frame></div>;
67
+ }
68
+ `;
69
+ const ids = idsOf(src);
70
+ const out = applyDetachComponent(CANVAS, src, ids.section as string, 0);
71
+ expect(out.source).toContain('<FrameDetached><p>a</p></FrameDetached>');
72
+ expect(out.source).toContain('<Frame><p>b</p></Frame>');
73
+ });
74
+
75
+ test('name collisions bump the suffix', () => {
76
+ const src = `
77
+ function Card() { return <article>x</article>; }
78
+ function CardDetached() { return <aside>existing</aside>; }
79
+ export default function Demo() {
80
+ return <div><Card /><Card /></div>;
81
+ }
82
+ `;
83
+ const ids = idsOf(src);
84
+ const out = applyDetachComponent(CANVAS, src, ids.article as string, 0);
85
+ expect(out.detachedName).toBe('CardDetached2');
86
+ expect(out.source).toContain('function CardDetached2()');
87
+ });
88
+
89
+ test('a plain (non-instance) element refuses', () => {
90
+ const src = `export default function Demo() { return <div><h1>T</h1></div>; }`;
91
+ const ids = idsOf(src);
92
+ expect(() => applyDetachComponent(CANVAS, src, ids.h1 as string, 0)).toThrow(CanvasEditError);
93
+ });
94
+ });
@@ -103,10 +103,14 @@ describe('GET /_api/edit-scope', () => {
103
103
  expect(mapped.body.reason).toBe('mapped');
104
104
  expect(mapped.body.affects).toBe(4);
105
105
 
106
- // A bogus id is a 400 (invalid data-cd-id shape).
107
- const bad = await fetch(`${main}/_api/edit-scope?canvas=ui/Gallery&id=zzz`, {
108
- signal: AbortSignal.timeout(2000),
109
- });
106
+ // A bogus id is a 400 (invalid data-cd-id shape). Note: since the
107
+ // authored-id widening (feature-4, 2026-07-20) short word-ids like "zzz"
108
+ // are VALID shapes (hand-authored data-cd-id attrs are preserved by the
109
+ // pipeline) — only genuinely malformed values (spaces, slashes) fail.
110
+ const bad = await fetch(
111
+ `${main}/_api/edit-scope?canvas=ui/Gallery&id=${encodeURIComponent('../etc/passwd')}`,
112
+ { signal: AbortSignal.timeout(2000) }
113
+ );
110
114
  expect(bad.status).toBe(400);
111
115
  } finally {
112
116
  await killProc(proc as never);
@@ -300,6 +300,80 @@ describe('POST /_api/delete-artboard', () => {
300
300
  });
301
301
  });
302
302
 
303
+ describe('POST /_api/duplicate-artboard', () => {
304
+ test('clones an artboard at a new width, returns a suffixed id, undo removes the clone', async () => {
305
+ const { designRoot, main, proc } = await boot();
306
+ const canvasPath = join(designRoot, 'ui', 'List.tsx');
307
+ try {
308
+ await divIdsByLine(main, designRoot); // warm pipeline
309
+
310
+ const dup = await fetch(`${main}/_api/duplicate-artboard`, {
311
+ method: 'POST',
312
+ body: JSON.stringify({ canvas: 'ui/List', artboardId: 'home', width: 390 }),
313
+ signal: AbortSignal.timeout(2000),
314
+ });
315
+ expect(dup.status).toBe(200);
316
+ const dupJson = (await dup.json()) as { ok: boolean; artboardId: string; seq: number };
317
+ expect(dupJson.ok).toBe(true);
318
+ expect(dupJson.artboardId).toBe('home-390');
319
+ const src = readFileSync(canvasPath, 'utf8');
320
+ expect(src).toContain('id="home-390"');
321
+ expect(src).toContain('width={390}');
322
+ // Original artboard is untouched.
323
+ expect(src).toContain('width={1440} height={1024}');
324
+
325
+ // Undo removes the clone, leaving only the original.
326
+ await fetch(`${main}/_api/reorder-revert`, {
327
+ method: 'POST',
328
+ body: JSON.stringify({ canvas: 'ui/List', seq: dupJson.seq, dir: 'undo' }),
329
+ signal: AbortSignal.timeout(2000),
330
+ });
331
+ expect(readFileSync(canvasPath, 'utf8')).not.toContain('id="home-390"');
332
+
333
+ // Cross-origin duplicate is CSRF-rejected.
334
+ const forged = await fetch(`${main}/_api/duplicate-artboard`, {
335
+ method: 'POST',
336
+ headers: { origin: 'http://evil.example' },
337
+ body: JSON.stringify({ canvas: 'ui/List', artboardId: 'home', width: 390 }),
338
+ signal: AbortSignal.timeout(2000),
339
+ });
340
+ expect(forged.status).toBe(403);
341
+ } finally {
342
+ await killProc(proc as never);
343
+ }
344
+ });
345
+
346
+ test('unknown artboard id is a 422 refusal', async () => {
347
+ const { designRoot, main, proc } = await boot();
348
+ try {
349
+ await divIdsByLine(main, designRoot); // warm pipeline
350
+ const res = await fetch(`${main}/_api/duplicate-artboard`, {
351
+ method: 'POST',
352
+ body: JSON.stringify({ canvas: 'ui/List', artboardId: 'nope', width: 390 }),
353
+ signal: AbortSignal.timeout(2000),
354
+ });
355
+ expect(res.status).toBe(422);
356
+ } finally {
357
+ await killProc(proc as never);
358
+ }
359
+ });
360
+
361
+ test('malformed artboard id shape is a 400 refusal (security-auditor consistency finding)', async () => {
362
+ const { designRoot, main, proc } = await boot();
363
+ try {
364
+ await divIdsByLine(main, designRoot); // warm pipeline
365
+ const res = await fetch(`${main}/_api/duplicate-artboard`, {
366
+ method: 'POST',
367
+ body: JSON.stringify({ canvas: 'ui/List', artboardId: 'a" onload="x', width: 390 }),
368
+ signal: AbortSignal.timeout(2000),
369
+ });
370
+ expect(res.status).toBe(400);
371
+ } finally {
372
+ await killProc(proc as never);
373
+ }
374
+ });
375
+ });
376
+
303
377
  // G3 security (DDR-152) — the structural-write disk-fill / silent-shred defenses
304
378
  // the adversarial review required: a per-api token bucket rate-caps the verbs,
305
379
  // and a source-size ceiling refuses a GROWTH op past MAX_CANVAS_SOURCE (a delete