@webiny/website-builder-sdk 0.0.0-unstable.df7a8bb475 → 0.0.0-unstable.e1999482d0

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 (362) hide show
  1. package/BindingsApi.js +29 -36
  2. package/BindingsApi.js.map +1 -1
  3. package/BindingsProcessor.js +34 -43
  4. package/BindingsProcessor.js.map +1 -1
  5. package/BindingsProcessor.test.js +82 -78
  6. package/BindingsProcessor.test.js.map +1 -1
  7. package/BindingsResolver.d.ts +4 -1
  8. package/BindingsResolver.js +92 -122
  9. package/BindingsResolver.js.map +1 -1
  10. package/BindingsResolver.test.js +399 -363
  11. package/BindingsResolver.test.js.map +1 -1
  12. package/ComponentInputTraverser.js +29 -49
  13. package/ComponentInputTraverser.js.map +1 -1
  14. package/ComponentManifestToAstConverter.d.ts +33 -1
  15. package/ComponentManifestToAstConverter.js +164 -21
  16. package/ComponentManifestToAstConverter.js.map +1 -1
  17. package/ComponentRegistry.d.ts +2 -1
  18. package/ComponentRegistry.js +40 -22
  19. package/ComponentRegistry.js.map +1 -1
  20. package/ComponentResolver.d.ts +6 -3
  21. package/ComponentResolver.js +30 -29
  22. package/ComponentResolver.js.map +1 -1
  23. package/ConstraintEvaluator.d.ts +41 -0
  24. package/ConstraintEvaluator.js +304 -0
  25. package/ConstraintEvaluator.js.map +1 -0
  26. package/ConstraintEvaluator.test.d.ts +1 -0
  27. package/ConstraintEvaluator.test.js +1771 -0
  28. package/ConstraintEvaluator.test.js.map +1 -0
  29. package/ContentSdk.d.ts +80 -4
  30. package/ContentSdk.js +190 -89
  31. package/ContentSdk.js.map +1 -1
  32. package/DocumentStore.d.ts +5 -0
  33. package/DocumentStore.js +49 -59
  34. package/DocumentStore.js.map +1 -1
  35. package/DocumentStore.test.d.ts +1 -0
  36. package/DocumentStore.test.js +126 -0
  37. package/DocumentStore.test.js.map +1 -0
  38. package/DocumentStoreManager.d.ts +9 -2
  39. package/DocumentStoreManager.js +36 -16
  40. package/DocumentStoreManager.js.map +1 -1
  41. package/EditingSdk.d.ts +19 -3
  42. package/EditingSdk.js +112 -121
  43. package/EditingSdk.js.map +1 -1
  44. package/ElementBindings.d.ts +28 -0
  45. package/ElementBindings.js +88 -0
  46. package/ElementBindings.js.map +1 -0
  47. package/ElementBindings.test.d.ts +1 -0
  48. package/ElementBindings.test.js +127 -0
  49. package/ElementBindings.test.js.map +1 -0
  50. package/ElementFactory.d.ts +13 -4
  51. package/ElementFactory.js +170 -164
  52. package/ElementFactory.js.map +1 -1
  53. package/ElementFactory.test.d.ts +1 -0
  54. package/ElementFactory.test.js +511 -0
  55. package/ElementFactory.test.js.map +1 -0
  56. package/ElementSelection.d.ts +20 -0
  57. package/ElementSelection.js +32 -0
  58. package/ElementSelection.js.map +1 -0
  59. package/ElementSelection.test.d.ts +1 -0
  60. package/ElementSelection.test.js +73 -0
  61. package/ElementSelection.test.js.map +1 -0
  62. package/Environment.d.ts +7 -0
  63. package/Environment.js +19 -19
  64. package/Environment.js.map +1 -1
  65. package/Environment.test.d.ts +1 -0
  66. package/Environment.test.js +39 -0
  67. package/Environment.test.js.map +1 -0
  68. package/FunctionConverter.js +8 -7
  69. package/FunctionConverter.js.map +1 -1
  70. package/GlobalComponentLanguage.d.ts +31 -0
  71. package/GlobalComponentLanguage.js +37 -0
  72. package/GlobalComponentLanguage.js.map +1 -0
  73. package/HashObject.js +11 -12
  74. package/HashObject.js.map +1 -1
  75. package/HotkeyManager.js +40 -47
  76. package/HotkeyManager.js.map +1 -1
  77. package/IBindingsUpdater.js +0 -3
  78. package/IRedirects.js +0 -3
  79. package/InheritanceProcessor.js +99 -139
  80. package/InheritanceProcessor.js.map +1 -1
  81. package/InheritanceProcessor.test.js +184 -177
  82. package/InheritanceProcessor.test.js.map +1 -1
  83. package/InheritedValueResolver.d.ts +6 -0
  84. package/InheritedValueResolver.js +22 -20
  85. package/InheritedValueResolver.js.map +1 -1
  86. package/InputBindingsProcessor.js +193 -289
  87. package/InputBindingsProcessor.js.map +1 -1
  88. package/InputRenderer.test.d.ts +1 -0
  89. package/InputRenderer.test.js +186 -0
  90. package/InputRenderer.test.js.map +1 -0
  91. package/InputsBindingsProcessor.test.js +464 -314
  92. package/InputsBindingsProcessor.test.js.map +1 -1
  93. package/InputsUpdater.d.ts +1 -1
  94. package/InputsUpdater.js +24 -26
  95. package/InputsUpdater.js.map +1 -1
  96. package/LayoutContentArea.d.ts +68 -0
  97. package/LayoutContentArea.js +78 -0
  98. package/LayoutContentArea.js.map +1 -0
  99. package/LayoutContentArea.test.d.ts +1 -0
  100. package/LayoutContentArea.test.js +111 -0
  101. package/LayoutContentArea.test.js.map +1 -0
  102. package/LiveSdk.d.ts +6 -3
  103. package/LiveSdk.js +16 -13
  104. package/LiveSdk.js.map +1 -1
  105. package/Logger.js +9 -8
  106. package/Logger.js.map +1 -1
  107. package/MouseTracker.js +77 -83
  108. package/MouseTracker.js.map +1 -1
  109. package/NullSdk.d.ts +5 -3
  110. package/NullSdk.js +25 -14
  111. package/NullSdk.js.map +1 -1
  112. package/PreviewDocument.d.ts +3 -1
  113. package/PreviewDocument.js +30 -30
  114. package/PreviewDocument.js.map +1 -1
  115. package/PreviewSdk.d.ts +7 -3
  116. package/PreviewSdk.js +23 -17
  117. package/PreviewSdk.js.map +1 -1
  118. package/PreviewViewport.d.ts +5 -0
  119. package/PreviewViewport.js +60 -63
  120. package/PreviewViewport.js.map +1 -1
  121. package/PreviewViewport.test.d.ts +1 -0
  122. package/PreviewViewport.test.js +27 -0
  123. package/PreviewViewport.test.js.map +1 -0
  124. package/RadioOptionInputs.test.d.ts +1 -0
  125. package/RadioOptionInputs.test.js +521 -0
  126. package/RadioOptionInputs.test.js.map +1 -0
  127. package/ResizeObserver.js +24 -31
  128. package/ResizeObserver.js.map +1 -1
  129. package/StylesBindingsProcessor.js +41 -79
  130. package/StylesBindingsProcessor.js.map +1 -1
  131. package/StylesUpdater.d.ts +1 -1
  132. package/StylesUpdater.js +23 -25
  133. package/StylesUpdater.js.map +1 -1
  134. package/Theme.js +30 -25
  135. package/Theme.js.map +1 -1
  136. package/ViewportManager.js +89 -101
  137. package/ViewportManager.js.map +1 -1
  138. package/asset/deliveryUrl.d.ts +41 -0
  139. package/asset/deliveryUrl.js +34 -0
  140. package/asset/deliveryUrl.js.map +1 -0
  141. package/asset/deliveryUrl.test.d.ts +1 -0
  142. package/asset/deliveryUrl.test.js +127 -0
  143. package/asset/deliveryUrl.test.js.map +1 -0
  144. package/asset/geometry.d.ts +7 -0
  145. package/asset/geometry.js +10 -0
  146. package/asset/geometry.js.map +1 -0
  147. package/asset/imageSrcSet.d.ts +63 -0
  148. package/asset/imageSrcSet.js +98 -0
  149. package/asset/imageSrcSet.js.map +1 -0
  150. package/asset/imageSrcSet.test.d.ts +1 -0
  151. package/asset/imageSrcSet.test.js +103 -0
  152. package/asset/imageSrcSet.test.js.map +1 -0
  153. package/asset/index.d.ts +5 -0
  154. package/asset/index.js +5 -0
  155. package/asset/normalize.d.ts +24 -0
  156. package/asset/normalize.js +106 -0
  157. package/asset/normalize.js.map +1 -0
  158. package/asset/normalize.test.d.ts +1 -0
  159. package/asset/normalize.test.js +263 -0
  160. package/asset/normalize.test.js.map +1 -0
  161. package/asset/types.d.ts +2 -0
  162. package/asset/types.js +0 -0
  163. package/componentLoader/ComponentLoader.d.ts +16 -0
  164. package/componentLoader/ComponentLoader.js +0 -0
  165. package/componentLoader/RegistryOnlyComponentLoader.d.ts +9 -0
  166. package/componentLoader/RegistryOnlyComponentLoader.js +8 -0
  167. package/componentLoader/RegistryOnlyComponentLoader.js.map +1 -0
  168. package/componentLoader/createComponentLoader.d.ts +7 -0
  169. package/componentLoader/createComponentLoader.js +10 -0
  170. package/componentLoader/createComponentLoader.js.map +1 -0
  171. package/componentLoader/defaultComponentLoader.d.ts +7 -0
  172. package/componentLoader/defaultComponentLoader.js +8 -0
  173. package/componentLoader/defaultComponentLoader.js.map +1 -0
  174. package/componentLoader/index.d.ts +3 -0
  175. package/componentLoader/index.js +3 -0
  176. package/constants.d.ts +1 -0
  177. package/constants.js +7 -5
  178. package/constants.js.map +1 -1
  179. package/contentClientState.test.d.ts +1 -0
  180. package/contentClientState.test.js +404 -0
  181. package/contentClientState.test.js.map +1 -0
  182. package/contentEntry/ContentEntryBindingsResolver.d.ts +33 -0
  183. package/contentEntry/ContentEntryBindingsResolver.js +52 -0
  184. package/contentEntry/ContentEntryBindingsResolver.js.map +1 -0
  185. package/contentEntry/ContentEntryCache.d.ts +49 -0
  186. package/contentEntry/ContentEntryCache.js +51 -0
  187. package/contentEntry/ContentEntryCache.js.map +1 -0
  188. package/contentEntry/resolveContentEntry.d.ts +74 -0
  189. package/contentEntry/resolveContentEntry.js +53 -0
  190. package/contentEntry/resolveContentEntry.js.map +1 -0
  191. package/contentEntry/resolveContentEntry.test.d.ts +1 -0
  192. package/contentEntry/resolveContentEntry.test.js +213 -0
  193. package/contentEntry/resolveContentEntry.test.js.map +1 -0
  194. package/createComponent.d.ts +5 -0
  195. package/createComponent.js +32 -0
  196. package/createComponent.js.map +1 -0
  197. package/createContentClient.d.ts +17 -0
  198. package/createContentClient.js +16 -0
  199. package/createContentClient.js.map +1 -0
  200. package/createContentClient.test.d.ts +1 -0
  201. package/createContentClient.test.js +165 -0
  202. package/createContentClient.test.js.map +1 -0
  203. package/createElement.js +5 -6
  204. package/createElement.js.map +1 -1
  205. package/createGlobalElementDocument.d.ts +6 -0
  206. package/createGlobalElementDocument.js +23 -0
  207. package/createGlobalElementDocument.js.map +1 -0
  208. package/createInput.d.ts +10 -2
  209. package/createInput.js +92 -143
  210. package/createInput.js.map +1 -1
  211. package/createTheme.js +2 -3
  212. package/createTheme.js.map +1 -1
  213. package/dataProviders/ApiClient.d.ts +13 -5
  214. package/dataProviders/ApiClient.js +58 -49
  215. package/dataProviders/ApiClient.js.map +1 -1
  216. package/dataProviders/ApiClient.test.d.ts +1 -0
  217. package/dataProviders/ApiClient.test.js +116 -0
  218. package/dataProviders/ApiClient.test.js.map +1 -0
  219. package/dataProviders/DefaultDataProvider.d.ts +7 -2
  220. package/dataProviders/DefaultDataProvider.js +103 -45
  221. package/dataProviders/DefaultDataProvider.js.map +1 -1
  222. package/dataProviders/DefaultDataProvider.test.d.ts +1 -0
  223. package/dataProviders/DefaultDataProvider.test.js +81 -0
  224. package/dataProviders/DefaultDataProvider.test.js.map +1 -0
  225. package/dataProviders/GET_EXPERIMENT_PAUSED.d.ts +1 -0
  226. package/dataProviders/GET_EXPERIMENT_PAUSED.js +17 -0
  227. package/dataProviders/GET_EXPERIMENT_PAUSED.js.map +1 -0
  228. package/dataProviders/GET_LAYOUT_BY_SLUG.d.ts +1 -0
  229. package/dataProviders/GET_LAYOUT_BY_SLUG.js +27 -0
  230. package/dataProviders/GET_LAYOUT_BY_SLUG.js.map +1 -0
  231. package/dataProviders/GET_PAGE_BY_ID.d.ts +1 -1
  232. package/dataProviders/GET_PAGE_BY_ID.js +10 -1
  233. package/dataProviders/GET_PAGE_BY_ID.js.map +1 -1
  234. package/dataProviders/GET_PAGE_BY_PATH.d.ts +1 -1
  235. package/dataProviders/GET_PAGE_BY_PATH.js +10 -1
  236. package/dataProviders/GET_PAGE_BY_PATH.js.map +1 -1
  237. package/dataProviders/GET_PAGE_EXPERIMENT.d.ts +1 -0
  238. package/dataProviders/GET_PAGE_EXPERIMENT.js +32 -0
  239. package/dataProviders/GET_PAGE_EXPERIMENT.js.map +1 -0
  240. package/dataProviders/GET_VARIANT_CONTENT.d.ts +1 -0
  241. package/dataProviders/GET_VARIANT_CONTENT.js +24 -0
  242. package/dataProviders/GET_VARIANT_CONTENT.js.map +1 -0
  243. package/dataProviders/LIST_PUBLISHED_PAGES.d.ts +1 -1
  244. package/dataProviders/LIST_PUBLISHED_PAGES.js +16 -5
  245. package/dataProviders/LIST_PUBLISHED_PAGES.js.map +1 -1
  246. package/dataProviders/NullDataProvider.d.ts +17 -4
  247. package/dataProviders/NullDataProvider.js +33 -13
  248. package/dataProviders/NullDataProvider.js.map +1 -1
  249. package/dataProviders/RedirectsProvider.js +24 -27
  250. package/dataProviders/RedirectsProvider.js.map +1 -1
  251. package/dataProviders/queries.test.d.ts +1 -0
  252. package/dataProviders/queries.test.js +53 -0
  253. package/dataProviders/queries.test.js.map +1 -0
  254. package/defaultBreakpoints.js +43 -22
  255. package/defaultBreakpoints.js.map +1 -1
  256. package/documentOperations/$addElementReferenceToParent.d.ts +1 -1
  257. package/documentOperations/$addElementReferenceToParent.js +30 -26
  258. package/documentOperations/$addElementReferenceToParent.js.map +1 -1
  259. package/documentOperations/AddElement.js +8 -7
  260. package/documentOperations/AddElement.js.map +1 -1
  261. package/documentOperations/AddToParent.d.ts +2 -2
  262. package/documentOperations/AddToParent.js +14 -13
  263. package/documentOperations/AddToParent.js.map +1 -1
  264. package/documentOperations/IDocumentOperation.js +0 -3
  265. package/documentOperations/RemoveElement.js +10 -7
  266. package/documentOperations/RemoveElement.js.map +1 -1
  267. package/documentOperations/SetGlobalInputBinding.js +23 -22
  268. package/documentOperations/SetGlobalInputBinding.js.map +1 -1
  269. package/documentOperations/SetGlobalStyleBinding.js +23 -23
  270. package/documentOperations/SetGlobalStyleBinding.js.map +1 -1
  271. package/documentOperations/SetInputBindingOverride.js +30 -29
  272. package/documentOperations/SetInputBindingOverride.js.map +1 -1
  273. package/documentOperations/SetStyleBindingOverride.js +30 -31
  274. package/documentOperations/SetStyleBindingOverride.js.map +1 -1
  275. package/documentOperations/index.js +9 -8
  276. package/documentOperations/index.js.map +1 -1
  277. package/experiments/PostHogAnalyticsProvider.d.ts +31 -0
  278. package/experiments/PostHogAnalyticsProvider.js +28 -0
  279. package/experiments/PostHogAnalyticsProvider.js.map +1 -0
  280. package/experiments/analyticsProvider.d.ts +4 -0
  281. package/experiments/analyticsProvider.js +11 -0
  282. package/experiments/analyticsProvider.js.map +1 -0
  283. package/experiments/bucketing.d.ts +27 -0
  284. package/experiments/bucketing.js +91 -0
  285. package/experiments/bucketing.js.map +1 -0
  286. package/experiments/bucketing.test.d.ts +1 -0
  287. package/experiments/bucketing.test.js +183 -0
  288. package/experiments/bucketing.test.js.map +1 -0
  289. package/experiments/index.d.ts +5 -0
  290. package/experiments/index.js +5 -0
  291. package/experiments/render.d.ts +82 -0
  292. package/experiments/render.js +149 -0
  293. package/experiments/render.js.map +1 -0
  294. package/experiments/types.d.ts +108 -0
  295. package/experiments/types.js +4 -0
  296. package/experiments/types.js.map +1 -0
  297. package/findMatchingAstNode.js +11 -13
  298. package/findMatchingAstNode.js.map +1 -1
  299. package/generateElementId.js +2 -1
  300. package/generateElementId.js.map +1 -1
  301. package/globalElements.d.ts +43 -0
  302. package/globalElements.js +79 -0
  303. package/globalElements.js.map +1 -0
  304. package/globalElements.test.d.ts +1 -0
  305. package/globalElements.test.js +343 -0
  306. package/globalElements.test.js.map +1 -0
  307. package/headersProvider.d.ts +1 -2
  308. package/headersProvider.js +4 -3
  309. package/headersProvider.js.map +1 -1
  310. package/image/geometry.d.ts +54 -0
  311. package/image/geometry.js +112 -0
  312. package/image/geometry.js.map +1 -0
  313. package/image/geometry.test.d.ts +1 -0
  314. package/image/geometry.test.js +190 -0
  315. package/image/geometry.test.js.map +1 -0
  316. package/image/index.d.ts +3 -0
  317. package/image/index.js +3 -0
  318. package/image/presets.d.ts +6 -0
  319. package/image/presets.js +20 -0
  320. package/image/presets.js.map +1 -0
  321. package/image/types.d.ts +10 -0
  322. package/image/types.js +0 -0
  323. package/index.d.ts +21 -0
  324. package/index.js +21 -2
  325. package/jsonPatch.js +5 -9
  326. package/jsonPatch.js.map +1 -1
  327. package/listItemId.d.ts +17 -0
  328. package/listItemId.js +14 -0
  329. package/listItemId.js.map +1 -0
  330. package/listItemId.test.d.ts +1 -0
  331. package/listItemId.test.js +265 -0
  332. package/listItemId.test.js.map +1 -0
  333. package/messages.d.ts +3 -0
  334. package/messages.js +15 -11
  335. package/messages.js.map +1 -1
  336. package/messenger/MessageOrigin.js +12 -11
  337. package/messenger/MessageOrigin.js.map +1 -1
  338. package/messenger/Messenger.js +58 -71
  339. package/messenger/Messenger.js.map +1 -1
  340. package/messenger/index.js +0 -2
  341. package/package.json +17 -16
  342. package/radioOptionInputs.d.ts +24 -0
  343. package/radioOptionInputs.js +26 -0
  344. package/radioOptionInputs.js.map +1 -0
  345. package/registerComponentGroup.js +5 -6
  346. package/registerComponentGroup.js.map +1 -1
  347. package/types/ShorthandCssProperties.js +0 -3
  348. package/types/WebsiteBuilderTheme.d.ts +12 -0
  349. package/types/WebsiteBuilderTheme.js +0 -3
  350. package/types.d.ts +338 -18
  351. package/types.js +0 -3
  352. package/withGlobalElementDefaults.d.ts +8 -0
  353. package/withGlobalElementDefaults.js +60 -0
  354. package/withGlobalElementDefaults.js.map +1 -0
  355. package/IBindingsUpdater.js.map +0 -1
  356. package/IRedirects.js.map +0 -1
  357. package/documentOperations/IDocumentOperation.js.map +0 -1
  358. package/index.js.map +0 -1
  359. package/messenger/index.js.map +0 -1
  360. package/types/ShorthandCssProperties.js.map +0 -1
  361. package/types/WebsiteBuilderTheme.js.map +0 -1
  362. package/types.js.map +0 -1
@@ -0,0 +1,1771 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { evaluateConstraints, evaluateDeleteConstraint } from "./ConstraintEvaluator.js";
3
+ function makeElement(id, componentName, parent) {
4
+ return {
5
+ type: "Webiny/Element",
6
+ id,
7
+ component: {
8
+ name: componentName
9
+ },
10
+ parent
11
+ };
12
+ }
13
+ function makeManifest(name, overrides) {
14
+ return {
15
+ name,
16
+ label: name,
17
+ inputs: [],
18
+ tags: [],
19
+ ...overrides
20
+ };
21
+ }
22
+ function makeDocument(elements, bindings = {}) {
23
+ const elementMap = {};
24
+ for (const el of elements)elementMap[el.id] = el;
25
+ return {
26
+ id: "doc-1",
27
+ state: {},
28
+ version: 1,
29
+ properties: {},
30
+ metadata: {},
31
+ extensions: {},
32
+ bindings,
33
+ elements: elementMap
34
+ };
35
+ }
36
+ describe("evaluateConstraints", ()=>{
37
+ it("should allow placement when there are no constraints", ()=>{
38
+ const parent = makeElement("parent-1", "Container");
39
+ const doc = makeDocument([
40
+ parent
41
+ ]);
42
+ const components = {
43
+ Container: makeManifest("Container"),
44
+ Button: makeManifest("Button")
45
+ };
46
+ const result = evaluateConstraints({
47
+ componentName: "Button",
48
+ parentId: "parent-1",
49
+ slot: "children",
50
+ document: doc,
51
+ components
52
+ });
53
+ expect(result.allowed).toBe(true);
54
+ expect(result.violation).toBeUndefined();
55
+ });
56
+ it("should block when component's parent constraint fails", ()=>{
57
+ const parent = makeElement("parent-1", "Container");
58
+ const doc = makeDocument([
59
+ parent
60
+ ]);
61
+ const components = {
62
+ Container: makeManifest("Container"),
63
+ TabPanel: makeManifest("TabPanel", {
64
+ constraints: [
65
+ (ctx)=>{
66
+ if ("Tabs" !== ctx.parent.name) return ctx.block("TabPanel must be inside Tabs");
67
+ }
68
+ ]
69
+ })
70
+ };
71
+ const result = evaluateConstraints({
72
+ componentName: "TabPanel",
73
+ parentId: "parent-1",
74
+ slot: "children",
75
+ document: doc,
76
+ components
77
+ });
78
+ expect(result.allowed).toBe(false);
79
+ expect(result.violation).toBeDefined();
80
+ expect(result.violation.message).toBe("TabPanel must be inside Tabs");
81
+ });
82
+ it("should allow when parent constraint passes", ()=>{
83
+ const parent = makeElement("parent-1", "Tabs");
84
+ const doc = makeDocument([
85
+ parent
86
+ ]);
87
+ const components = {
88
+ Tabs: makeManifest("Tabs"),
89
+ TabPanel: makeManifest("TabPanel", {
90
+ constraints: [
91
+ (ctx)=>{
92
+ if ("Tabs" !== ctx.parent.name) return ctx.block("TabPanel must be inside Tabs");
93
+ }
94
+ ]
95
+ })
96
+ };
97
+ const result = evaluateConstraints({
98
+ componentName: "TabPanel",
99
+ parentId: "parent-1",
100
+ slot: "children",
101
+ document: doc,
102
+ components
103
+ });
104
+ expect(result.allowed).toBe(true);
105
+ expect(result.violation).toBeUndefined();
106
+ });
107
+ it("should block when ancestor constraint fails", ()=>{
108
+ const root = makeElement("root", "Page");
109
+ const section = makeElement("section-1", "Section", {
110
+ id: "root",
111
+ slot: "children"
112
+ });
113
+ const doc = makeDocument([
114
+ root,
115
+ section
116
+ ]);
117
+ const components = {
118
+ Page: makeManifest("Page"),
119
+ Section: makeManifest("Section"),
120
+ ProductPrice: makeManifest("ProductPrice", {
121
+ constraints: [
122
+ (ctx)=>{
123
+ if (!ctx.isDescendantOf("ProductBox")) return ctx.block("ProductPrice must be inside a ProductBox");
124
+ }
125
+ ]
126
+ })
127
+ };
128
+ const result = evaluateConstraints({
129
+ componentName: "ProductPrice",
130
+ parentId: "section-1",
131
+ slot: "children",
132
+ document: doc,
133
+ components
134
+ });
135
+ expect(result.allowed).toBe(false);
136
+ expect(result.violation.message).toBe("ProductPrice must be inside a ProductBox");
137
+ });
138
+ it("should allow when grandparent matches ancestor constraint", ()=>{
139
+ const root = makeElement("root", "ProductBox");
140
+ const inner = makeElement("inner-1", "Container", {
141
+ id: "root",
142
+ slot: "children"
143
+ });
144
+ const doc = makeDocument([
145
+ root,
146
+ inner
147
+ ]);
148
+ const components = {
149
+ ProductBox: makeManifest("ProductBox"),
150
+ Container: makeManifest("Container"),
151
+ ProductPrice: makeManifest("ProductPrice", {
152
+ constraints: [
153
+ (ctx)=>{
154
+ if (!ctx.isDescendantOf("ProductBox")) return ctx.block("ProductPrice must be inside a ProductBox");
155
+ }
156
+ ]
157
+ })
158
+ };
159
+ const result = evaluateConstraints({
160
+ componentName: "ProductPrice",
161
+ parentId: "inner-1",
162
+ slot: "children",
163
+ document: doc,
164
+ components
165
+ });
166
+ expect(result.allowed).toBe(true);
167
+ });
168
+ it("should block via slot children limit constraint using slotChildCount", ()=>{
169
+ const parent = makeElement("parent-1", "Grid");
170
+ const child1 = makeElement("child-1", "Cell", {
171
+ id: "parent-1",
172
+ slot: "children"
173
+ });
174
+ const child2 = makeElement("child-2", "Cell", {
175
+ id: "parent-1",
176
+ slot: "children"
177
+ });
178
+ const doc = makeDocument([
179
+ parent,
180
+ child1,
181
+ child2
182
+ ], {
183
+ "parent-1": {
184
+ inputs: {
185
+ children: {
186
+ id: "inp-1",
187
+ type: "slot",
188
+ static: [
189
+ "child-1",
190
+ "child-2"
191
+ ]
192
+ }
193
+ }
194
+ }
195
+ });
196
+ const components = {
197
+ Grid: makeManifest("Grid"),
198
+ Cell: makeManifest("Cell", {
199
+ constraints: [
200
+ (ctx)=>{
201
+ if (!(ctx.slotChildCount() < 2)) return ctx.block("Maximum 2 children allowed");
202
+ }
203
+ ]
204
+ })
205
+ };
206
+ const result = evaluateConstraints({
207
+ componentName: "Cell",
208
+ parentId: "parent-1",
209
+ slot: "children",
210
+ document: doc,
211
+ components
212
+ });
213
+ expect(result.allowed).toBe(false);
214
+ expect(result.violation.message).toBe("Maximum 2 children allowed");
215
+ });
216
+ it("should block via countInstances when max instances reached", ()=>{
217
+ const root = makeElement("root", "Page");
218
+ const hero = makeElement("hero-1", "Hero", {
219
+ id: "root",
220
+ slot: "children"
221
+ });
222
+ const doc = makeDocument([
223
+ root,
224
+ hero
225
+ ]);
226
+ const components = {
227
+ Page: makeManifest("Page"),
228
+ Hero: makeManifest("Hero", {
229
+ constraints: [
230
+ (ctx)=>{
231
+ if (!(ctx.countInstances("Hero") < 1)) return ctx.block("Only one Hero per page");
232
+ }
233
+ ]
234
+ })
235
+ };
236
+ const result = evaluateConstraints({
237
+ componentName: "Hero",
238
+ parentId: "root",
239
+ slot: "children",
240
+ document: doc,
241
+ components
242
+ });
243
+ expect(result.allowed).toBe(false);
244
+ expect(result.violation.message).toBe("Only one Hero per page");
245
+ });
246
+ it("should not count the moved element itself in countInstances", ()=>{
247
+ const root = makeElement("root", "Page");
248
+ const hero = makeElement("hero-1", "Hero", {
249
+ id: "root",
250
+ slot: "children"
251
+ });
252
+ const doc = makeDocument([
253
+ root,
254
+ hero
255
+ ]);
256
+ const components = {
257
+ Page: makeManifest("Page"),
258
+ Hero: makeManifest("Hero", {
259
+ constraints: [
260
+ (ctx)=>{
261
+ if (ctx.countInstances("Hero") > 0) return ctx.block("Only one Hero per page");
262
+ }
263
+ ]
264
+ })
265
+ };
266
+ const params = {
267
+ componentName: "Hero",
268
+ parentId: "root",
269
+ slot: "children",
270
+ document: doc,
271
+ components
272
+ };
273
+ expect(evaluateConstraints({
274
+ ...params,
275
+ movedElementId: "hero-1"
276
+ }).allowed).toBe(true);
277
+ expect(evaluateConstraints(params).allowed).toBe(false);
278
+ });
279
+ it("should not evaluate parent's constraints against the placed component", ()=>{
280
+ const parent = makeElement("parent-1", "Tabs");
281
+ const doc = makeDocument([
282
+ parent
283
+ ]);
284
+ const components = {
285
+ Tabs: makeManifest("Tabs", {
286
+ constraints: [
287
+ (ctx)=>ctx.block("This should not run for children")
288
+ ]
289
+ }),
290
+ Button: makeManifest("Button")
291
+ };
292
+ const result = evaluateConstraints({
293
+ componentName: "Button",
294
+ parentId: "parent-1",
295
+ slot: "children",
296
+ document: doc,
297
+ components
298
+ });
299
+ expect(result.allowed).toBe(true);
300
+ });
301
+ it("should short-circuit on the first failed constraint", ()=>{
302
+ const parent = makeElement("parent-1", "Container");
303
+ const doc = makeDocument([
304
+ parent
305
+ ]);
306
+ const components = {
307
+ Container: makeManifest("Container"),
308
+ Widget: makeManifest("Widget", {
309
+ constraints: [
310
+ ()=>{},
311
+ (ctx)=>ctx.block("Always fails"),
312
+ (ctx)=>ctx.block("Also fails")
313
+ ]
314
+ })
315
+ };
316
+ const result = evaluateConstraints({
317
+ componentName: "Widget",
318
+ parentId: "parent-1",
319
+ slot: "children",
320
+ document: doc,
321
+ components
322
+ });
323
+ expect(result.allowed).toBe(false);
324
+ expect(result.violation.message).toBe("Always fails");
325
+ });
326
+ it("should allow placement when component manifest is not found", ()=>{
327
+ const parent = makeElement("parent-1", "Container");
328
+ const doc = makeDocument([
329
+ parent
330
+ ]);
331
+ const components = {
332
+ Container: makeManifest("Container")
333
+ };
334
+ const result = evaluateConstraints({
335
+ componentName: "Unknown",
336
+ parentId: "parent-1",
337
+ slot: "children",
338
+ document: doc,
339
+ components
340
+ });
341
+ expect(result.allowed).toBe(true);
342
+ });
343
+ it("should allow placement when parent element is not found", ()=>{
344
+ const doc = makeDocument([]);
345
+ const components = {
346
+ Button: makeManifest("Button")
347
+ };
348
+ const result = evaluateConstraints({
349
+ componentName: "Button",
350
+ parentId: "nonexistent",
351
+ slot: "children",
352
+ document: doc,
353
+ components
354
+ });
355
+ expect(result.allowed).toBe(true);
356
+ });
357
+ it("should handle multi-slot parent constraint checking specific slot", ()=>{
358
+ const parent = makeElement("parent-1", "TwoColumn");
359
+ const child = makeElement("child-1", "Widget", {
360
+ id: "parent-1",
361
+ slot: "leftColumn"
362
+ });
363
+ const doc = makeDocument([
364
+ parent,
365
+ child
366
+ ], {
367
+ "parent-1": {
368
+ inputs: {
369
+ leftColumn: {
370
+ id: "inp-left",
371
+ type: "slot",
372
+ static: [
373
+ "child-1"
374
+ ]
375
+ },
376
+ rightColumn: {
377
+ id: "inp-right",
378
+ type: "slot",
379
+ static: []
380
+ }
381
+ }
382
+ }
383
+ });
384
+ const components = {
385
+ TwoColumn: makeManifest("TwoColumn"),
386
+ Widget: makeManifest("Widget", {
387
+ constraints: [
388
+ (ctx)=>{
389
+ if (!(ctx.slotChildCount() < 1)) return ctx.block("Slot is full");
390
+ }
391
+ ]
392
+ })
393
+ };
394
+ const leftResult = evaluateConstraints({
395
+ componentName: "Widget",
396
+ parentId: "parent-1",
397
+ slot: "leftColumn",
398
+ document: doc,
399
+ components
400
+ });
401
+ expect(leftResult.allowed).toBe(false);
402
+ const rightResult = evaluateConstraints({
403
+ componentName: "Widget",
404
+ parentId: "parent-1",
405
+ slot: "rightColumn",
406
+ document: doc,
407
+ components
408
+ });
409
+ expect(rightResult.allowed).toBe(true);
410
+ });
411
+ it("should provide a default message when constraint has no message", ()=>{
412
+ const parent = makeElement("parent-1", "Container");
413
+ const doc = makeDocument([
414
+ parent
415
+ ]);
416
+ const components = {
417
+ Container: makeManifest("Container"),
418
+ Widget: makeManifest("Widget", {
419
+ constraints: [
420
+ (ctx)=>ctx.block("Blocked")
421
+ ]
422
+ })
423
+ };
424
+ const result = evaluateConstraints({
425
+ componentName: "Widget",
426
+ parentId: "parent-1",
427
+ slot: "children",
428
+ document: doc,
429
+ components
430
+ });
431
+ expect(result.violation.message).toBe("Blocked");
432
+ });
433
+ it("should use thrown error message as the violation message", ()=>{
434
+ const parent = makeElement("parent-1", "Container");
435
+ const doc = makeDocument([
436
+ parent
437
+ ]);
438
+ const components = {
439
+ Container: makeManifest("Container"),
440
+ Widget: makeManifest("Widget", {
441
+ constraints: [
442
+ (ctx)=>{
443
+ throw new Error(`Widget cannot be placed inside ${ctx.parent.name}`);
444
+ }
445
+ ]
446
+ })
447
+ };
448
+ const result = evaluateConstraints({
449
+ componentName: "Widget",
450
+ parentId: "parent-1",
451
+ slot: "children",
452
+ document: doc,
453
+ components
454
+ });
455
+ expect(result.allowed).toBe(false);
456
+ expect(result.violation.message).toBe("Widget cannot be placed inside Container");
457
+ });
458
+ it("should fall back to static message when a non-Error is thrown", ()=>{
459
+ const parent = makeElement("parent-1", "Container");
460
+ const doc = makeDocument([
461
+ parent
462
+ ]);
463
+ const components = {
464
+ Container: makeManifest("Container"),
465
+ Widget: makeManifest("Widget", {
466
+ constraints: [
467
+ ()=>{
468
+ throw "not an error object";
469
+ }
470
+ ]
471
+ })
472
+ };
473
+ const result = evaluateConstraints({
474
+ componentName: "Widget",
475
+ parentId: "parent-1",
476
+ slot: "children",
477
+ document: doc,
478
+ components
479
+ });
480
+ expect(result.allowed).toBe(false);
481
+ expect(result.violation.message).toBe("Cannot place Widget here.");
482
+ });
483
+ it("should fall back to default message when a non-Error is thrown and no static message", ()=>{
484
+ const parent = makeElement("parent-1", "Container");
485
+ const doc = makeDocument([
486
+ parent
487
+ ]);
488
+ const components = {
489
+ Container: makeManifest("Container"),
490
+ Widget: makeManifest("Widget", {
491
+ constraints: [
492
+ ()=>{
493
+ throw null;
494
+ }
495
+ ]
496
+ })
497
+ };
498
+ const result = evaluateConstraints({
499
+ componentName: "Widget",
500
+ parentId: "parent-1",
501
+ slot: "children",
502
+ document: doc,
503
+ components
504
+ });
505
+ expect(result.allowed).toBe(false);
506
+ expect(result.violation.message).toBe("Cannot place Widget here.");
507
+ });
508
+ describe("ctx.isChildOf", ()=>{
509
+ it("should return true when direct parent matches", ()=>{
510
+ const parent = makeElement("parent-1", "Tabs");
511
+ const doc = makeDocument([
512
+ parent
513
+ ]);
514
+ const components = {
515
+ Tabs: makeManifest("Tabs"),
516
+ TabPanel: makeManifest("TabPanel", {
517
+ constraints: [
518
+ (ctx)=>{
519
+ if (!ctx.isChildOf("Tabs")) return ctx.block("TabPanel must be a direct child of Tabs");
520
+ }
521
+ ]
522
+ })
523
+ };
524
+ const result = evaluateConstraints({
525
+ componentName: "TabPanel",
526
+ parentId: "parent-1",
527
+ slot: "children",
528
+ document: doc,
529
+ components
530
+ });
531
+ expect(result.allowed).toBe(true);
532
+ });
533
+ it("should return false when direct parent does not match", ()=>{
534
+ const parent = makeElement("parent-1", "Container");
535
+ const doc = makeDocument([
536
+ parent
537
+ ]);
538
+ const components = {
539
+ Container: makeManifest("Container"),
540
+ TabPanel: makeManifest("TabPanel", {
541
+ constraints: [
542
+ (ctx)=>{
543
+ if (!ctx.isChildOf("Tabs")) return ctx.block("TabPanel must be a direct child of Tabs");
544
+ }
545
+ ]
546
+ })
547
+ };
548
+ const result = evaluateConstraints({
549
+ componentName: "TabPanel",
550
+ parentId: "parent-1",
551
+ slot: "children",
552
+ document: doc,
553
+ components
554
+ });
555
+ expect(result.allowed).toBe(false);
556
+ });
557
+ });
558
+ describe("ctx.isDescendantOf", ()=>{
559
+ it("should return true when direct parent matches", ()=>{
560
+ const parent = makeElement("parent-1", "ProductBox");
561
+ const doc = makeDocument([
562
+ parent
563
+ ]);
564
+ const components = {
565
+ ProductBox: makeManifest("ProductBox"),
566
+ ProductPrice: makeManifest("ProductPrice", {
567
+ constraints: [
568
+ (ctx)=>{
569
+ if (!ctx.isDescendantOf("ProductBox")) return ctx.block("Blocked");
570
+ }
571
+ ]
572
+ })
573
+ };
574
+ const result = evaluateConstraints({
575
+ componentName: "ProductPrice",
576
+ parentId: "parent-1",
577
+ slot: "children",
578
+ document: doc,
579
+ components
580
+ });
581
+ expect(result.allowed).toBe(true);
582
+ });
583
+ it("should return true when grandparent matches", ()=>{
584
+ const root = makeElement("root", "ProductBox");
585
+ const inner = makeElement("inner", "Container", {
586
+ id: "root",
587
+ slot: "children"
588
+ });
589
+ const doc = makeDocument([
590
+ root,
591
+ inner
592
+ ]);
593
+ const components = {
594
+ ProductBox: makeManifest("ProductBox"),
595
+ Container: makeManifest("Container"),
596
+ ProductPrice: makeManifest("ProductPrice", {
597
+ constraints: [
598
+ (ctx)=>{
599
+ if (!ctx.isDescendantOf("ProductBox")) return ctx.block("Blocked");
600
+ }
601
+ ]
602
+ })
603
+ };
604
+ const result = evaluateConstraints({
605
+ componentName: "ProductPrice",
606
+ parentId: "inner",
607
+ slot: "children",
608
+ document: doc,
609
+ components
610
+ });
611
+ expect(result.allowed).toBe(true);
612
+ });
613
+ it("should return false when no ancestor matches", ()=>{
614
+ const root = makeElement("root", "Page");
615
+ const inner = makeElement("inner", "Section", {
616
+ id: "root",
617
+ slot: "children"
618
+ });
619
+ const doc = makeDocument([
620
+ root,
621
+ inner
622
+ ]);
623
+ const components = {
624
+ Page: makeManifest("Page"),
625
+ Section: makeManifest("Section"),
626
+ ProductPrice: makeManifest("ProductPrice", {
627
+ constraints: [
628
+ (ctx)=>{
629
+ if (!ctx.isDescendantOf("ProductBox")) return ctx.block("Blocked");
630
+ }
631
+ ]
632
+ })
633
+ };
634
+ const result = evaluateConstraints({
635
+ componentName: "ProductPrice",
636
+ parentId: "inner",
637
+ slot: "children",
638
+ document: doc,
639
+ components
640
+ });
641
+ expect(result.allowed).toBe(false);
642
+ });
643
+ });
644
+ describe("ctx.slotChildCount", ()=>{
645
+ it("should return the number of children in the target slot", ()=>{
646
+ const parent = makeElement("parent-1", "Grid");
647
+ const doc = makeDocument([
648
+ parent
649
+ ], {
650
+ "parent-1": {
651
+ inputs: {
652
+ children: {
653
+ id: "inp-1",
654
+ type: "slot",
655
+ static: [
656
+ "child-1",
657
+ "child-2",
658
+ "child-3"
659
+ ]
660
+ }
661
+ }
662
+ }
663
+ });
664
+ const components = {
665
+ Grid: makeManifest("Grid"),
666
+ Cell: makeManifest("Cell", {
667
+ constraints: [
668
+ (ctx)=>{
669
+ if (!(ctx.slotChildCount() < 3)) return ctx.block("Max 3 children");
670
+ }
671
+ ]
672
+ })
673
+ };
674
+ const result = evaluateConstraints({
675
+ componentName: "Cell",
676
+ parentId: "parent-1",
677
+ slot: "children",
678
+ document: doc,
679
+ components
680
+ });
681
+ expect(result.allowed).toBe(false);
682
+ });
683
+ it("should return 0 when slot has no bindings", ()=>{
684
+ const parent = makeElement("parent-1", "Container");
685
+ const doc = makeDocument([
686
+ parent
687
+ ]);
688
+ const components = {
689
+ Container: makeManifest("Container"),
690
+ Widget: makeManifest("Widget", {
691
+ constraints: [
692
+ (ctx)=>{
693
+ if (!(ctx.slotChildCount() < 5)) return ctx.block("Blocked");
694
+ }
695
+ ]
696
+ })
697
+ };
698
+ const result = evaluateConstraints({
699
+ componentName: "Widget",
700
+ parentId: "parent-1",
701
+ slot: "children",
702
+ document: doc,
703
+ components
704
+ });
705
+ expect(result.allowed).toBe(true);
706
+ });
707
+ });
708
+ describe("ctx.hasTag", ()=>{
709
+ it("should return true when the placed component has the tag", ()=>{
710
+ const parent = makeElement("parent-1", "Container");
711
+ const doc = makeDocument([
712
+ parent
713
+ ]);
714
+ const components = {
715
+ Container: makeManifest("Container"),
716
+ FunnelField: makeManifest("FunnelField", {
717
+ tags: [
718
+ "funnel-field",
719
+ "input"
720
+ ],
721
+ constraints: [
722
+ (ctx)=>{
723
+ if (ctx.hasTag("funnel-field")) return ctx.block("Funnel fields not allowed here");
724
+ }
725
+ ]
726
+ })
727
+ };
728
+ const result = evaluateConstraints({
729
+ componentName: "FunnelField",
730
+ parentId: "parent-1",
731
+ slot: "children",
732
+ document: doc,
733
+ components
734
+ });
735
+ expect(result.allowed).toBe(false);
736
+ });
737
+ it("should return false when the placed component does not have the tag", ()=>{
738
+ const parent = makeElement("parent-1", "Container");
739
+ const doc = makeDocument([
740
+ parent
741
+ ]);
742
+ const components = {
743
+ Container: makeManifest("Container"),
744
+ Button: makeManifest("Button", {
745
+ constraints: [
746
+ (ctx)=>{
747
+ if (ctx.hasTag("funnel-field")) return ctx.block("Blocked");
748
+ }
749
+ ]
750
+ })
751
+ };
752
+ const result = evaluateConstraints({
753
+ componentName: "Button",
754
+ parentId: "parent-1",
755
+ slot: "children",
756
+ document: doc,
757
+ components
758
+ });
759
+ expect(result.allowed).toBe(true);
760
+ });
761
+ });
762
+ describe("ctx.getAncestor", ()=>{
763
+ it("should return the matching ancestor element context", ()=>{
764
+ const root = makeElement("root", "ProductBox");
765
+ const inner = makeElement("inner", "Container", {
766
+ id: "root",
767
+ slot: "children"
768
+ });
769
+ const doc = makeDocument([
770
+ root,
771
+ inner
772
+ ]);
773
+ const components = {
774
+ ProductBox: makeManifest("ProductBox", {
775
+ tags: [
776
+ "product"
777
+ ]
778
+ }),
779
+ Container: makeManifest("Container"),
780
+ ProductPrice: makeManifest("ProductPrice", {
781
+ constraints: [
782
+ (ctx)=>{
783
+ const ancestor = ctx.getAncestor("ProductBox");
784
+ if (!(void 0 !== ancestor && "ProductBox" === ancestor.name)) return ctx.block("Blocked");
785
+ }
786
+ ]
787
+ })
788
+ };
789
+ const result = evaluateConstraints({
790
+ componentName: "ProductPrice",
791
+ parentId: "inner",
792
+ slot: "children",
793
+ document: doc,
794
+ components
795
+ });
796
+ expect(result.allowed).toBe(true);
797
+ });
798
+ it("should return undefined when no ancestor matches", ()=>{
799
+ const root = makeElement("root", "Page");
800
+ const doc = makeDocument([
801
+ root
802
+ ]);
803
+ const components = {
804
+ Page: makeManifest("Page"),
805
+ Widget: makeManifest("Widget", {
806
+ constraints: [
807
+ (ctx)=>{
808
+ if (void 0 === ctx.getAncestor("ProductBox")) return ctx.block("Blocked");
809
+ }
810
+ ]
811
+ })
812
+ };
813
+ const result = evaluateConstraints({
814
+ componentName: "Widget",
815
+ parentId: "root",
816
+ slot: "children",
817
+ document: doc,
818
+ components
819
+ });
820
+ expect(result.allowed).toBe(false);
821
+ });
822
+ });
823
+ describe("ctx.countInstances", ()=>{
824
+ it("should count instances of a component in the document", ()=>{
825
+ const root = makeElement("root", "Page");
826
+ const hero1 = makeElement("hero-1", "Hero", {
827
+ id: "root",
828
+ slot: "children"
829
+ });
830
+ const hero2 = makeElement("hero-2", "Hero", {
831
+ id: "root",
832
+ slot: "children"
833
+ });
834
+ const doc = makeDocument([
835
+ root,
836
+ hero1,
837
+ hero2
838
+ ]);
839
+ const components = {
840
+ Page: makeManifest("Page"),
841
+ Hero: makeManifest("Hero", {
842
+ constraints: [
843
+ (ctx)=>{
844
+ if (!(ctx.countInstances("Hero") < 2)) return ctx.block("Max 2 Heroes");
845
+ }
846
+ ]
847
+ })
848
+ };
849
+ const result = evaluateConstraints({
850
+ componentName: "Hero",
851
+ parentId: "root",
852
+ slot: "children",
853
+ document: doc,
854
+ components
855
+ });
856
+ expect(result.allowed).toBe(false);
857
+ });
858
+ });
859
+ describe("ctx.component", ()=>{
860
+ it("should expose the placed component's name and tags", ()=>{
861
+ const parent = makeElement("parent-1", "Container");
862
+ const doc = makeDocument([
863
+ parent
864
+ ]);
865
+ let capturedComponent;
866
+ const components = {
867
+ Container: makeManifest("Container"),
868
+ Widget: makeManifest("Widget", {
869
+ tags: [
870
+ "interactive",
871
+ "form"
872
+ ],
873
+ constraints: [
874
+ (ctx)=>{
875
+ capturedComponent = ctx.component;
876
+ }
877
+ ]
878
+ })
879
+ };
880
+ evaluateConstraints({
881
+ componentName: "Widget",
882
+ parentId: "parent-1",
883
+ slot: "children",
884
+ document: doc,
885
+ components
886
+ });
887
+ expect(capturedComponent).toBeDefined();
888
+ expect(capturedComponent.name).toBe("Widget");
889
+ expect(capturedComponent.tags).toEqual([
890
+ "interactive",
891
+ "form"
892
+ ]);
893
+ });
894
+ });
895
+ describe("parent.getParent", ()=>{
896
+ it("should return the grandparent element context", ()=>{
897
+ const root = makeElement("root", "Page");
898
+ const section = makeElement("section", "Section", {
899
+ id: "root",
900
+ slot: "children"
901
+ });
902
+ const doc = makeDocument([
903
+ root,
904
+ section
905
+ ]);
906
+ let grandparentName;
907
+ const components = {
908
+ Page: makeManifest("Page"),
909
+ Section: makeManifest("Section"),
910
+ Widget: makeManifest("Widget", {
911
+ constraints: [
912
+ (ctx)=>{
913
+ const grandparent = ctx.parent.getParent();
914
+ grandparentName = grandparent?.name;
915
+ }
916
+ ]
917
+ })
918
+ };
919
+ evaluateConstraints({
920
+ componentName: "Widget",
921
+ parentId: "section",
922
+ slot: "children",
923
+ document: doc,
924
+ components
925
+ });
926
+ expect(grandparentName).toBe("Page");
927
+ });
928
+ it("should return undefined for root element", ()=>{
929
+ const root = makeElement("root", "Page");
930
+ const doc = makeDocument([
931
+ root
932
+ ]);
933
+ let parentResult = "not-called";
934
+ const components = {
935
+ Page: makeManifest("Page"),
936
+ Widget: makeManifest("Widget", {
937
+ constraints: [
938
+ (ctx)=>{
939
+ parentResult = ctx.parent.getParent();
940
+ }
941
+ ]
942
+ })
943
+ };
944
+ evaluateConstraints({
945
+ componentName: "Widget",
946
+ parentId: "root",
947
+ slot: "children",
948
+ document: doc,
949
+ components
950
+ });
951
+ expect(parentResult).toBeUndefined();
952
+ });
953
+ });
954
+ describe("parent.childIndex / childCount / isFirstChild / isLastChild", ()=>{
955
+ it("should return -1 for elements not in a list slot", ()=>{
956
+ const root = makeElement("root", "Page");
957
+ const doc = makeDocument([
958
+ root
959
+ ]);
960
+ let capturedIndex;
961
+ let capturedCount;
962
+ const components = {
963
+ Page: makeManifest("Page"),
964
+ Widget: makeManifest("Widget", {
965
+ constraints: [
966
+ (ctx)=>{
967
+ capturedIndex = ctx.parent.childIndex();
968
+ capturedCount = ctx.parent.childCount();
969
+ }
970
+ ]
971
+ })
972
+ };
973
+ evaluateConstraints({
974
+ componentName: "Widget",
975
+ parentId: "root",
976
+ slot: "children",
977
+ document: doc,
978
+ components
979
+ });
980
+ expect(capturedIndex).toBe(-1);
981
+ expect(capturedCount).toBe(-1);
982
+ });
983
+ it("should parse child index from list slot path", ()=>{
984
+ const root = makeElement("root", "FunnelBuilder");
985
+ const step = makeElement("step-1", "Step", {
986
+ id: "root",
987
+ slot: "steps/2/step"
988
+ });
989
+ const doc = makeDocument([
990
+ root,
991
+ step
992
+ ], {
993
+ root: {
994
+ inputs: {
995
+ "steps/0/step": {
996
+ id: "s0",
997
+ type: "slot",
998
+ static: [
999
+ "step-0"
1000
+ ]
1001
+ },
1002
+ "steps/1/step": {
1003
+ id: "s1",
1004
+ type: "slot",
1005
+ static: [
1006
+ "step-other"
1007
+ ]
1008
+ },
1009
+ "steps/2/step": {
1010
+ id: "s2",
1011
+ type: "slot",
1012
+ static: [
1013
+ "step-1"
1014
+ ]
1015
+ }
1016
+ }
1017
+ }
1018
+ });
1019
+ let capturedIndex;
1020
+ let capturedCount;
1021
+ let capturedIsLast;
1022
+ let capturedIsFirst;
1023
+ const components = {
1024
+ FunnelBuilder: makeManifest("FunnelBuilder"),
1025
+ Step: makeManifest("Step"),
1026
+ Widget: makeManifest("Widget", {
1027
+ constraints: [
1028
+ (ctx)=>{
1029
+ capturedIndex = ctx.parent.childIndex();
1030
+ capturedCount = ctx.parent.childCount();
1031
+ capturedIsLast = ctx.parent.isLastChild();
1032
+ capturedIsFirst = ctx.parent.isFirstChild();
1033
+ }
1034
+ ]
1035
+ })
1036
+ };
1037
+ evaluateConstraints({
1038
+ componentName: "Widget",
1039
+ parentId: "step-1",
1040
+ slot: "children",
1041
+ document: doc,
1042
+ components
1043
+ });
1044
+ expect(capturedIndex).toBe(2);
1045
+ expect(capturedCount).toBe(3);
1046
+ expect(capturedIsLast).toBe(true);
1047
+ expect(capturedIsFirst).toBe(false);
1048
+ });
1049
+ it("should identify the first child correctly", ()=>{
1050
+ const root = makeElement("root", "FunnelBuilder");
1051
+ const step = makeElement("step-0", "Step", {
1052
+ id: "root",
1053
+ slot: "steps/0/step"
1054
+ });
1055
+ const doc = makeDocument([
1056
+ root,
1057
+ step
1058
+ ], {
1059
+ root: {
1060
+ inputs: {
1061
+ "steps/0/step": {
1062
+ id: "s0",
1063
+ type: "slot",
1064
+ static: [
1065
+ "step-0"
1066
+ ]
1067
+ },
1068
+ "steps/1/step": {
1069
+ id: "s1",
1070
+ type: "slot",
1071
+ static: [
1072
+ "step-1"
1073
+ ]
1074
+ }
1075
+ }
1076
+ }
1077
+ });
1078
+ let capturedIsFirst;
1079
+ let capturedIsLast;
1080
+ const components = {
1081
+ FunnelBuilder: makeManifest("FunnelBuilder"),
1082
+ Step: makeManifest("Step"),
1083
+ Widget: makeManifest("Widget", {
1084
+ constraints: [
1085
+ (ctx)=>{
1086
+ capturedIsFirst = ctx.parent.isFirstChild();
1087
+ capturedIsLast = ctx.parent.isLastChild();
1088
+ }
1089
+ ]
1090
+ })
1091
+ };
1092
+ evaluateConstraints({
1093
+ componentName: "Widget",
1094
+ parentId: "step-0",
1095
+ slot: "children",
1096
+ document: doc,
1097
+ components
1098
+ });
1099
+ expect(capturedIsFirst).toBe(true);
1100
+ expect(capturedIsLast).toBe(false);
1101
+ });
1102
+ it("should resolve position from bindings even when element.parent.slot is stale", ()=>{
1103
+ const root = makeElement("root", "FunnelBuilder");
1104
+ const step = makeElement("step-final", "Step", {
1105
+ id: "root",
1106
+ slot: "steps/1/step"
1107
+ });
1108
+ const doc = makeDocument([
1109
+ root,
1110
+ step
1111
+ ], {
1112
+ root: {
1113
+ inputs: {
1114
+ "steps/0/step": {
1115
+ id: "s0",
1116
+ type: "slot",
1117
+ static: "step-0"
1118
+ },
1119
+ "steps/1/step": {
1120
+ id: "s1",
1121
+ type: "slot",
1122
+ static: "step-1"
1123
+ },
1124
+ "steps/2/step": {
1125
+ id: "s2",
1126
+ type: "slot",
1127
+ static: "step-2"
1128
+ },
1129
+ "steps/3/step": {
1130
+ id: "s3",
1131
+ type: "slot",
1132
+ static: "step-final"
1133
+ }
1134
+ }
1135
+ }
1136
+ });
1137
+ let capturedIndex;
1138
+ let capturedIsLast;
1139
+ const components = {
1140
+ FunnelBuilder: makeManifest("FunnelBuilder"),
1141
+ Step: makeManifest("Step"),
1142
+ Widget: makeManifest("Widget", {
1143
+ constraints: [
1144
+ (ctx)=>{
1145
+ capturedIndex = ctx.parent.childIndex();
1146
+ capturedIsLast = ctx.parent.isLastChild();
1147
+ }
1148
+ ]
1149
+ })
1150
+ };
1151
+ evaluateConstraints({
1152
+ componentName: "Widget",
1153
+ parentId: "step-final",
1154
+ slot: "children",
1155
+ document: doc,
1156
+ components
1157
+ });
1158
+ expect(capturedIndex).toBe(3);
1159
+ expect(capturedIsLast).toBe(true);
1160
+ });
1161
+ it("should resolve position from direct list slot binding (static is an array of IDs)", ()=>{
1162
+ const root = makeElement("root", "FunnelBuilder");
1163
+ const step0 = makeElement("step-0", "Step", {
1164
+ id: "root",
1165
+ slot: "steps"
1166
+ });
1167
+ const step1 = makeElement("step-1", "Step", {
1168
+ id: "root",
1169
+ slot: "steps"
1170
+ });
1171
+ const step2 = makeElement("step-2", "Step", {
1172
+ id: "root",
1173
+ slot: "steps"
1174
+ });
1175
+ const doc = makeDocument([
1176
+ root,
1177
+ step0,
1178
+ step1,
1179
+ step2
1180
+ ], {
1181
+ root: {
1182
+ inputs: {
1183
+ steps: {
1184
+ id: "s",
1185
+ type: "slot",
1186
+ list: true,
1187
+ static: [
1188
+ "step-0",
1189
+ "step-1",
1190
+ "step-2"
1191
+ ]
1192
+ }
1193
+ }
1194
+ }
1195
+ });
1196
+ let captured = {};
1197
+ const comps = {
1198
+ FunnelBuilder: makeManifest("FunnelBuilder"),
1199
+ Step: makeManifest("Step"),
1200
+ Widget: makeManifest("Widget", {
1201
+ constraints: [
1202
+ (ctx)=>{
1203
+ captured = {
1204
+ index: ctx.parent.childIndex(),
1205
+ count: ctx.parent.childCount(),
1206
+ isFirst: ctx.parent.isFirstChild(),
1207
+ isLast: ctx.parent.isLastChild()
1208
+ };
1209
+ }
1210
+ ]
1211
+ })
1212
+ };
1213
+ evaluateConstraints({
1214
+ componentName: "Widget",
1215
+ parentId: "step-0",
1216
+ slot: "children",
1217
+ document: doc,
1218
+ components: comps
1219
+ });
1220
+ expect(captured.index).toBe(0);
1221
+ expect(captured.count).toBe(3);
1222
+ expect(captured.isFirst).toBe(true);
1223
+ expect(captured.isLast).toBe(false);
1224
+ evaluateConstraints({
1225
+ componentName: "Widget",
1226
+ parentId: "step-1",
1227
+ slot: "children",
1228
+ document: doc,
1229
+ components: comps
1230
+ });
1231
+ expect(captured.index).toBe(1);
1232
+ expect(captured.count).toBe(3);
1233
+ expect(captured.isFirst).toBe(false);
1234
+ expect(captured.isLast).toBe(false);
1235
+ evaluateConstraints({
1236
+ componentName: "Widget",
1237
+ parentId: "step-2",
1238
+ slot: "children",
1239
+ document: doc,
1240
+ components: comps
1241
+ });
1242
+ expect(captured.index).toBe(2);
1243
+ expect(captured.count).toBe(3);
1244
+ expect(captured.isFirst).toBe(false);
1245
+ expect(captured.isLast).toBe(true);
1246
+ });
1247
+ });
1248
+ describe("descendantConstraints", ()=>{
1249
+ it("should block a direct child via parent's descendantConstraints", ()=>{
1250
+ const parent = makeElement("parent-1", "Step");
1251
+ const doc = makeDocument([
1252
+ parent
1253
+ ]);
1254
+ const components = {
1255
+ Step: makeManifest("Step", {
1256
+ descendantConstraints: [
1257
+ (ctx)=>{
1258
+ if (ctx.hasTag("funnel-field")) return ctx.block("No funnel fields allowed");
1259
+ }
1260
+ ]
1261
+ }),
1262
+ TextField: makeManifest("TextField", {
1263
+ tags: [
1264
+ "funnel-field"
1265
+ ]
1266
+ })
1267
+ };
1268
+ const result = evaluateConstraints({
1269
+ componentName: "TextField",
1270
+ parentId: "parent-1",
1271
+ slot: "children",
1272
+ document: doc,
1273
+ components
1274
+ });
1275
+ expect(result.allowed).toBe(false);
1276
+ expect(result.violation.message).toBe("No funnel fields allowed");
1277
+ });
1278
+ it("should block a nested descendant via ancestor's descendantConstraints", ()=>{
1279
+ const step = makeElement("step-1", "Step");
1280
+ const container = makeElement("container-1", "Container", {
1281
+ id: "step-1",
1282
+ slot: "children"
1283
+ });
1284
+ const doc = makeDocument([
1285
+ step,
1286
+ container
1287
+ ]);
1288
+ const components = {
1289
+ Step: makeManifest("Step", {
1290
+ descendantConstraints: [
1291
+ (ctx)=>{
1292
+ if (ctx.hasTag("funnel-field")) return ctx.block("No funnel fields in this step");
1293
+ }
1294
+ ]
1295
+ }),
1296
+ Container: makeManifest("Container"),
1297
+ TextField: makeManifest("TextField", {
1298
+ tags: [
1299
+ "funnel-field"
1300
+ ]
1301
+ })
1302
+ };
1303
+ const result = evaluateConstraints({
1304
+ componentName: "TextField",
1305
+ parentId: "container-1",
1306
+ slot: "children",
1307
+ document: doc,
1308
+ components
1309
+ });
1310
+ expect(result.allowed).toBe(false);
1311
+ expect(result.violation.message).toBe("No funnel fields in this step");
1312
+ });
1313
+ it("should allow when descendantConstraints pass", ()=>{
1314
+ const step = makeElement("step-1", "Step");
1315
+ const doc = makeDocument([
1316
+ step
1317
+ ]);
1318
+ const components = {
1319
+ Step: makeManifest("Step", {
1320
+ descendantConstraints: [
1321
+ (ctx)=>{
1322
+ if (ctx.hasTag("funnel-field")) return ctx.block("Blocked");
1323
+ }
1324
+ ]
1325
+ }),
1326
+ Button: makeManifest("Button")
1327
+ };
1328
+ const result = evaluateConstraints({
1329
+ componentName: "Button",
1330
+ parentId: "step-1",
1331
+ slot: "children",
1332
+ document: doc,
1333
+ components
1334
+ });
1335
+ expect(result.allowed).toBe(true);
1336
+ });
1337
+ it("should not run parent's descendantConstraints for unrelated ancestors", ()=>{
1338
+ const page = makeElement("page", "Page");
1339
+ const step = makeElement("step-1", "Step", {
1340
+ id: "page",
1341
+ slot: "children"
1342
+ });
1343
+ const doc = makeDocument([
1344
+ page,
1345
+ step
1346
+ ]);
1347
+ const components = {
1348
+ Page: makeManifest("Page", {
1349
+ descendantConstraints: [
1350
+ (ctx)=>ctx.block("Page blocks everything")
1351
+ ]
1352
+ }),
1353
+ Step: makeManifest("Step"),
1354
+ Button: makeManifest("Button")
1355
+ };
1356
+ const result = evaluateConstraints({
1357
+ componentName: "Button",
1358
+ parentId: "step-1",
1359
+ slot: "children",
1360
+ document: doc,
1361
+ components
1362
+ });
1363
+ expect(result.allowed).toBe(false);
1364
+ expect(result.violation.message).toBe("Page blocks everything");
1365
+ });
1366
+ it("should evaluate component constraints before descendantConstraints", ()=>{
1367
+ const parent = makeElement("parent-1", "Step");
1368
+ const doc = makeDocument([
1369
+ parent
1370
+ ]);
1371
+ const components = {
1372
+ Step: makeManifest("Step", {
1373
+ descendantConstraints: [
1374
+ (ctx)=>ctx.block("descendant constraint")
1375
+ ]
1376
+ }),
1377
+ Widget: makeManifest("Widget", {
1378
+ constraints: [
1379
+ (ctx)=>ctx.block("component constraint")
1380
+ ]
1381
+ })
1382
+ };
1383
+ const result = evaluateConstraints({
1384
+ componentName: "Widget",
1385
+ parentId: "parent-1",
1386
+ slot: "children",
1387
+ document: doc,
1388
+ components
1389
+ });
1390
+ expect(result.violation.message).toBe("component constraint");
1391
+ });
1392
+ });
1393
+ });
1394
+ describe("evaluateDeleteConstraint", ()=>{
1395
+ describe("a layout's page content", ()=>{
1396
+ const root = makeElement("root", "Page");
1397
+ const box = makeElement("box", "Box", {
1398
+ id: "root",
1399
+ slot: "children"
1400
+ });
1401
+ const inner = makeElement("inner", "Box", {
1402
+ id: "box",
1403
+ slot: "children"
1404
+ });
1405
+ const content = makeElement("content", "Webiny/LayoutContent", {
1406
+ id: "inner",
1407
+ slot: "children"
1408
+ });
1409
+ const sibling = makeElement("sibling", "Widget", {
1410
+ id: "root",
1411
+ slot: "children"
1412
+ });
1413
+ const doc = makeDocument([
1414
+ root,
1415
+ box,
1416
+ inner,
1417
+ content,
1418
+ sibling
1419
+ ]);
1420
+ const components = {
1421
+ Page: makeManifest("Page"),
1422
+ Box: makeManifest("Box"),
1423
+ Widget: makeManifest("Widget"),
1424
+ "Webiny/LayoutContent": makeManifest("Webiny/LayoutContent", {
1425
+ label: "Page Content"
1426
+ })
1427
+ };
1428
+ it("blocks deleting an element that holds it", ()=>{
1429
+ for (const elementId of [
1430
+ "box",
1431
+ "inner"
1432
+ ]){
1433
+ const result = evaluateDeleteConstraint({
1434
+ elementId,
1435
+ document: doc,
1436
+ components
1437
+ });
1438
+ expect(result.allowed).toBe(false);
1439
+ expect(result.violation?.message).toBe("Box holds Main content, so it can't be deleted.");
1440
+ }
1441
+ });
1442
+ it("blocks deleting the page content itself", ()=>{
1443
+ const result = evaluateDeleteConstraint({
1444
+ elementId: "content",
1445
+ document: doc,
1446
+ components
1447
+ });
1448
+ expect(result.allowed).toBe(false);
1449
+ expect(result.violation?.message).toBe("Main content can't be deleted.");
1450
+ });
1451
+ it("allows deleting a named content area, and an element holding it", ()=>{
1452
+ const sidebar = makeElement("sidebar", "Webiny/NamedContent", {
1453
+ id: "aside",
1454
+ slot: "children"
1455
+ });
1456
+ const aside = makeElement("aside", "Box", {
1457
+ id: "root",
1458
+ slot: "children"
1459
+ });
1460
+ const withSidebar = makeDocument([
1461
+ root,
1462
+ box,
1463
+ inner,
1464
+ content,
1465
+ sibling,
1466
+ aside,
1467
+ sidebar
1468
+ ]);
1469
+ withSidebar.bindings.sidebar = {
1470
+ inputs: {
1471
+ area: {
1472
+ id: "a",
1473
+ type: "text",
1474
+ static: "sidebar"
1475
+ }
1476
+ }
1477
+ };
1478
+ for (const elementId of [
1479
+ "sidebar",
1480
+ "aside"
1481
+ ]){
1482
+ const result = evaluateDeleteConstraint({
1483
+ elementId,
1484
+ document: withSidebar,
1485
+ components
1486
+ });
1487
+ expect(result.allowed).toBe(true);
1488
+ }
1489
+ });
1490
+ it("allows deleting an element that doesn't hold it", ()=>{
1491
+ const result = evaluateDeleteConstraint({
1492
+ elementId: "sibling",
1493
+ document: doc,
1494
+ components
1495
+ });
1496
+ expect(result.allowed).toBe(true);
1497
+ });
1498
+ });
1499
+ it("should allow when canDelete is undefined", ()=>{
1500
+ const el = makeElement("el-1", "Widget", {
1501
+ id: "root",
1502
+ slot: "children"
1503
+ });
1504
+ const root = makeElement("root", "Page");
1505
+ const doc = makeDocument([
1506
+ root,
1507
+ el
1508
+ ]);
1509
+ const components = {
1510
+ Page: makeManifest("Page"),
1511
+ Widget: makeManifest("Widget")
1512
+ };
1513
+ const result = evaluateDeleteConstraint({
1514
+ elementId: "el-1",
1515
+ document: doc,
1516
+ components
1517
+ });
1518
+ expect(result.allowed).toBe(true);
1519
+ });
1520
+ it("should allow when canDelete is true", ()=>{
1521
+ const el = makeElement("el-1", "Widget", {
1522
+ id: "root",
1523
+ slot: "children"
1524
+ });
1525
+ const root = makeElement("root", "Page");
1526
+ const doc = makeDocument([
1527
+ root,
1528
+ el
1529
+ ]);
1530
+ const components = {
1531
+ Page: makeManifest("Page"),
1532
+ Widget: makeManifest("Widget", {
1533
+ canDelete: true
1534
+ })
1535
+ };
1536
+ const result = evaluateDeleteConstraint({
1537
+ elementId: "el-1",
1538
+ document: doc,
1539
+ components
1540
+ });
1541
+ expect(result.allowed).toBe(true);
1542
+ });
1543
+ it("should block when canDelete is false", ()=>{
1544
+ const el = makeElement("el-1", "GridColumn", {
1545
+ id: "root",
1546
+ slot: "children"
1547
+ });
1548
+ const root = makeElement("root", "Grid");
1549
+ const doc = makeDocument([
1550
+ root,
1551
+ el
1552
+ ]);
1553
+ const components = {
1554
+ Grid: makeManifest("Grid"),
1555
+ GridColumn: makeManifest("GridColumn", {
1556
+ canDelete: false
1557
+ })
1558
+ };
1559
+ const result = evaluateDeleteConstraint({
1560
+ elementId: "el-1",
1561
+ document: doc,
1562
+ components
1563
+ });
1564
+ expect(result.allowed).toBe(false);
1565
+ expect(result.violation.message).toBe("GridColumn cannot be deleted.");
1566
+ });
1567
+ it("should allow when canDelete check returns true", ()=>{
1568
+ const funnel = makeElement("funnel", "Funnel");
1569
+ const step1 = makeElement("step-1", "Step", {
1570
+ id: "funnel",
1571
+ slot: "steps/0/step"
1572
+ });
1573
+ const step2 = makeElement("step-2", "Step", {
1574
+ id: "funnel",
1575
+ slot: "steps/1/step"
1576
+ });
1577
+ const step3 = makeElement("step-3", "Step", {
1578
+ id: "funnel",
1579
+ slot: "steps/2/step"
1580
+ });
1581
+ const doc = makeDocument([
1582
+ funnel,
1583
+ step1,
1584
+ step2,
1585
+ step3
1586
+ ], {
1587
+ funnel: {
1588
+ inputs: {
1589
+ "steps/0/step": {
1590
+ id: "s0",
1591
+ type: "slot",
1592
+ static: "step-1"
1593
+ },
1594
+ "steps/1/step": {
1595
+ id: "s1",
1596
+ type: "slot",
1597
+ static: "step-2"
1598
+ },
1599
+ "steps/2/step": {
1600
+ id: "s2",
1601
+ type: "slot",
1602
+ static: "step-3"
1603
+ }
1604
+ }
1605
+ }
1606
+ });
1607
+ const components = {
1608
+ Funnel: makeManifest("Funnel"),
1609
+ Step: makeManifest("Step", {
1610
+ canDelete: (ctx)=>{
1611
+ if (!(ctx.countInstances("Step") > 2)) return ctx.block("Blocked");
1612
+ }
1613
+ })
1614
+ };
1615
+ const result = evaluateDeleteConstraint({
1616
+ elementId: "step-2",
1617
+ document: doc,
1618
+ components
1619
+ });
1620
+ expect(result.allowed).toBe(true);
1621
+ });
1622
+ it("should block when canDelete check returns false", ()=>{
1623
+ const funnel = makeElement("funnel", "Funnel");
1624
+ const step1 = makeElement("step-1", "Step", {
1625
+ id: "funnel",
1626
+ slot: "steps/0/step"
1627
+ });
1628
+ const step2 = makeElement("step-2", "Step", {
1629
+ id: "funnel",
1630
+ slot: "steps/1/step"
1631
+ });
1632
+ const doc = makeDocument([
1633
+ funnel,
1634
+ step1,
1635
+ step2
1636
+ ], {
1637
+ funnel: {
1638
+ inputs: {
1639
+ "steps/0/step": {
1640
+ id: "s0",
1641
+ type: "slot",
1642
+ static: "step-1"
1643
+ },
1644
+ "steps/1/step": {
1645
+ id: "s1",
1646
+ type: "slot",
1647
+ static: "step-2"
1648
+ }
1649
+ }
1650
+ }
1651
+ });
1652
+ const components = {
1653
+ Funnel: makeManifest("Funnel"),
1654
+ Step: makeManifest("Step", {
1655
+ canDelete: (ctx)=>{
1656
+ if (!(ctx.countInstances("Step") > 2)) return ctx.block("Need at least 2 steps");
1657
+ }
1658
+ })
1659
+ };
1660
+ const result = evaluateDeleteConstraint({
1661
+ elementId: "step-1",
1662
+ document: doc,
1663
+ components
1664
+ });
1665
+ expect(result.allowed).toBe(false);
1666
+ expect(result.violation.message).toBe("Need at least 2 steps");
1667
+ });
1668
+ it("should use thrown error message as violation message", ()=>{
1669
+ const root = makeElement("root", "Page");
1670
+ const el = makeElement("el-1", "Widget", {
1671
+ id: "root",
1672
+ slot: "children"
1673
+ });
1674
+ const doc = makeDocument([
1675
+ root,
1676
+ el
1677
+ ]);
1678
+ const components = {
1679
+ Page: makeManifest("Page"),
1680
+ Widget: makeManifest("Widget", {
1681
+ canDelete: ()=>{
1682
+ throw new Error("Cannot delete this widget right now");
1683
+ }
1684
+ })
1685
+ };
1686
+ const result = evaluateDeleteConstraint({
1687
+ elementId: "el-1",
1688
+ document: doc,
1689
+ components
1690
+ });
1691
+ expect(result.allowed).toBe(false);
1692
+ expect(result.violation.message).toBe("Cannot delete this widget right now");
1693
+ });
1694
+ it("should provide correct context to the check function", ()=>{
1695
+ const root = makeElement("root", "Funnel");
1696
+ const step = makeElement("step-1", "Step", {
1697
+ id: "root",
1698
+ slot: "steps/0/step"
1699
+ });
1700
+ const doc = makeDocument([
1701
+ root,
1702
+ step
1703
+ ], {
1704
+ root: {
1705
+ inputs: {
1706
+ "steps/0/step": {
1707
+ id: "s0",
1708
+ type: "slot",
1709
+ static: "step-1"
1710
+ },
1711
+ "steps/1/step": {
1712
+ id: "s1",
1713
+ type: "slot",
1714
+ static: "step-2"
1715
+ }
1716
+ }
1717
+ }
1718
+ });
1719
+ let capturedCtx;
1720
+ const components = {
1721
+ Funnel: makeManifest("Funnel"),
1722
+ Step: makeManifest("Step", {
1723
+ tags: [
1724
+ "funnel-step"
1725
+ ],
1726
+ canDelete: (ctx)=>{
1727
+ capturedCtx = ctx;
1728
+ }
1729
+ })
1730
+ };
1731
+ evaluateDeleteConstraint({
1732
+ elementId: "step-1",
1733
+ document: doc,
1734
+ components
1735
+ });
1736
+ expect(capturedCtx).toBeDefined();
1737
+ expect(capturedCtx.component.name).toBe("Step");
1738
+ expect(capturedCtx.component.tags).toEqual([
1739
+ "funnel-step"
1740
+ ]);
1741
+ expect(capturedCtx.parent.name).toBe("Funnel");
1742
+ expect(capturedCtx.slot).toBe("steps/0/step");
1743
+ expect(capturedCtx.isChildOf("Funnel")).toBe(true);
1744
+ expect(capturedCtx.hasTag("funnel-step")).toBe(true);
1745
+ });
1746
+ it("should allow when element is not found", ()=>{
1747
+ const doc = makeDocument([]);
1748
+ const components = {};
1749
+ const result = evaluateDeleteConstraint({
1750
+ elementId: "nonexistent",
1751
+ document: doc,
1752
+ components
1753
+ });
1754
+ expect(result.allowed).toBe(true);
1755
+ });
1756
+ it("should allow when manifest is not found", ()=>{
1757
+ const el = makeElement("el-1", "Unknown");
1758
+ const doc = makeDocument([
1759
+ el
1760
+ ]);
1761
+ const components = {};
1762
+ const result = evaluateDeleteConstraint({
1763
+ elementId: "el-1",
1764
+ document: doc,
1765
+ components
1766
+ });
1767
+ expect(result.allowed).toBe(true);
1768
+ });
1769
+ });
1770
+
1771
+ //# sourceMappingURL=ConstraintEvaluator.test.js.map