@assistant-ui/react-generative-ui 0.0.7 → 0.0.9

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 (181) hide show
  1. package/dist/JSONGenerativeUI.client.d.ts +0 -1
  2. package/dist/JSONGenerativeUI.client.d.ts.map +1 -1
  3. package/dist/JSONGenerativeUI.server.d.ts +0 -1
  4. package/dist/JSONGenerativeUI.server.d.ts.map +1 -1
  5. package/dist/JSONGenerativeUI.shared.d.ts +0 -1
  6. package/dist/JSONGenerativeUI.shared.d.ts.map +1 -1
  7. package/dist/actionRegistry.d.ts +2 -11
  8. package/dist/actionRegistry.d.ts.map +1 -1
  9. package/dist/actionRegistry.js.map +1 -1
  10. package/dist/buildPresentParameters.d.ts +5 -5
  11. package/dist/buildPresentParameters.d.ts.map +1 -1
  12. package/dist/buildPresentParameters.js +10 -5
  13. package/dist/buildPresentParameters.js.map +1 -1
  14. package/dist/constants.d.ts.map +1 -1
  15. package/dist/defineGenerativeComponents.d.ts +0 -1
  16. package/dist/defineGenerativeComponents.d.ts.map +1 -1
  17. package/dist/generativeUIToJSX.d.ts +15 -10
  18. package/dist/generativeUIToJSX.d.ts.map +1 -1
  19. package/dist/generativeUIToJSX.js +47 -17
  20. package/dist/generativeUIToJSX.js.map +1 -1
  21. package/dist/index.d.ts +4 -3
  22. package/dist/index.js +2 -2
  23. package/dist/index.server.d.ts +4 -3
  24. package/dist/index.server.js +2 -2
  25. package/dist/index.shared.d.ts +3 -3
  26. package/dist/index.shared.js +2 -2
  27. package/dist/ir.d.ts +6 -6
  28. package/dist/ir.d.ts.map +1 -1
  29. package/dist/ir.js +27 -1
  30. package/dist/ir.js.map +1 -1
  31. package/dist/node_modules/.pnpm/@types_json-schema@7.0.15/node_modules/@types/json-schema/index.d.ts +3 -3
  32. package/dist/node_modules/.pnpm/@types_json-schema@7.0.15/node_modules/@types/json-schema/index.d.ts.map +1 -1
  33. package/dist/renderGenerativeUI.d.ts +0 -1
  34. package/dist/renderGenerativeUI.d.ts.map +1 -1
  35. package/dist/slack/constants.d.ts +111 -0
  36. package/dist/slack/constants.d.ts.map +1 -0
  37. package/dist/slack/constants.js +162 -0
  38. package/dist/slack/constants.js.map +1 -0
  39. package/dist/slack/decodeBlockAction.d.ts +11 -0
  40. package/dist/slack/decodeBlockAction.d.ts.map +1 -0
  41. package/dist/slack/decodeBlockAction.js +40 -0
  42. package/dist/slack/decodeBlockAction.js.map +1 -0
  43. package/dist/slack/fromSlackBlocks.d.ts +30 -0
  44. package/dist/slack/fromSlackBlocks.d.ts.map +1 -0
  45. package/dist/slack/fromSlackBlocks.js +447 -0
  46. package/dist/slack/fromSlackBlocks.js.map +1 -0
  47. package/dist/slack/toSlackBlocks.d.ts +7 -0
  48. package/dist/slack/toSlackBlocks.d.ts.map +1 -0
  49. package/dist/slack/toSlackBlocks.js +649 -0
  50. package/dist/slack/toSlackBlocks.js.map +1 -0
  51. package/dist/slack/types.d.ts +178 -0
  52. package/dist/slack/types.d.ts.map +1 -0
  53. package/dist/slack/types.js +0 -0
  54. package/dist/slack.d.ts +5 -0
  55. package/dist/slack.js +4 -0
  56. package/dist/teams/boundSpec.d.ts +20 -0
  57. package/dist/teams/boundSpec.d.ts.map +1 -0
  58. package/dist/teams/boundSpec.js +67 -0
  59. package/dist/teams/boundSpec.js.map +1 -0
  60. package/dist/teams/constants.d.ts +57 -0
  61. package/dist/teams/constants.d.ts.map +1 -0
  62. package/dist/teams/constants.js +92 -0
  63. package/dist/teams/constants.js.map +1 -0
  64. package/dist/teams/decodeSubmitData.d.ts +25 -0
  65. package/dist/teams/decodeSubmitData.d.ts.map +1 -0
  66. package/dist/teams/decodeSubmitData.js +44 -0
  67. package/dist/teams/decodeSubmitData.js.map +1 -0
  68. package/dist/teams/toAdaptiveCard.d.ts +39 -0
  69. package/dist/teams/toAdaptiveCard.d.ts.map +1 -0
  70. package/dist/teams/toAdaptiveCard.js +496 -0
  71. package/dist/teams/toAdaptiveCard.js.map +1 -0
  72. package/dist/teams/toTeamsAttachments.d.ts +13 -0
  73. package/dist/teams/toTeamsAttachments.d.ts.map +1 -0
  74. package/dist/teams/toTeamsAttachments.js +95 -0
  75. package/dist/teams/toTeamsAttachments.js.map +1 -0
  76. package/dist/teams/types.d.ts +196 -0
  77. package/dist/teams/types.d.ts.map +1 -0
  78. package/dist/teams/types.js +0 -0
  79. package/dist/teams.d.ts +5 -0
  80. package/dist/teams.js +4 -0
  81. package/dist/types.d.ts +7 -4
  82. package/dist/types.d.ts.map +1 -1
  83. package/dist/vocabulary/alert.d.ts +6 -12
  84. package/dist/vocabulary/alert.d.ts.map +1 -1
  85. package/dist/vocabulary/alert.js +13 -4
  86. package/dist/vocabulary/alert.js.map +1 -1
  87. package/dist/vocabulary/collectFormValues.d.ts +23 -0
  88. package/dist/vocabulary/collectFormValues.d.ts.map +1 -0
  89. package/dist/vocabulary/collectFormValues.js +32 -0
  90. package/dist/vocabulary/collectFormValues.js.map +1 -0
  91. package/dist/vocabulary/data.d.ts +16 -17
  92. package/dist/vocabulary/data.d.ts.map +1 -1
  93. package/dist/vocabulary/data.js +244 -13
  94. package/dist/vocabulary/data.js.map +1 -1
  95. package/dist/vocabulary/dispatch.d.ts +11 -0
  96. package/dist/vocabulary/dispatch.d.ts.map +1 -0
  97. package/dist/vocabulary/dispatch.js +21 -0
  98. package/dist/vocabulary/dispatch.js.map +1 -0
  99. package/dist/vocabulary/fact.d.ts +1 -6
  100. package/dist/vocabulary/fact.d.ts.map +1 -1
  101. package/dist/vocabulary/fact.js.map +1 -1
  102. package/dist/vocabulary/form.d.ts +14 -0
  103. package/dist/vocabulary/form.d.ts.map +1 -0
  104. package/dist/vocabulary/form.js +23 -0
  105. package/dist/vocabulary/form.js.map +1 -0
  106. package/dist/vocabulary/icon.d.ts +44 -0
  107. package/dist/vocabulary/icon.d.ts.map +1 -0
  108. package/dist/vocabulary/icon.js +272 -0
  109. package/dist/vocabulary/icon.js.map +1 -0
  110. package/dist/vocabulary/index.d.ts +0 -1
  111. package/dist/vocabulary/index.d.ts.map +1 -1
  112. package/dist/vocabulary/index.js +7 -1
  113. package/dist/vocabulary/index.js.map +1 -1
  114. package/dist/vocabulary/interactive.d.ts +45 -33
  115. package/dist/vocabulary/interactive.d.ts.map +1 -1
  116. package/dist/vocabulary/interactive.js +78 -29
  117. package/dist/vocabulary/interactive.js.map +1 -1
  118. package/dist/vocabulary/layout.d.ts +27 -23
  119. package/dist/vocabulary/layout.d.ts.map +1 -1
  120. package/dist/vocabulary/layout.js +78 -14
  121. package/dist/vocabulary/layout.js.map +1 -1
  122. package/dist/vocabulary/list.d.ts +17 -0
  123. package/dist/vocabulary/list.d.ts.map +1 -0
  124. package/dist/vocabulary/list.js +48 -0
  125. package/dist/vocabulary/list.js.map +1 -0
  126. package/dist/vocabulary/media.d.ts +3 -9
  127. package/dist/vocabulary/media.d.ts.map +1 -1
  128. package/dist/vocabulary/media.js +17 -7
  129. package/dist/vocabulary/media.js.map +1 -1
  130. package/dist/vocabulary/text.d.ts +3 -17
  131. package/dist/vocabulary/text.d.ts.map +1 -1
  132. package/dist/vocabulary/text.js.map +1 -1
  133. package/package.json +15 -7
  134. package/src/actionRegistry.ts +2 -10
  135. package/src/buildPresentParameters.ts +15 -9
  136. package/src/generativeUIToJSX.test.ts +186 -0
  137. package/src/generativeUIToJSX.ts +112 -21
  138. package/src/index.shared.ts +3 -0
  139. package/src/ir.ts +31 -5
  140. package/src/renderGenerativeUI.test.tsx +16 -2
  141. package/src/slack/constants.ts +212 -0
  142. package/src/slack/decodeBlockAction.test.ts +135 -0
  143. package/src/slack/decodeBlockAction.ts +66 -0
  144. package/src/slack/fromSlackBlocks.test.ts +1145 -0
  145. package/src/slack/fromSlackBlocks.ts +709 -0
  146. package/src/slack/toSlackBlocks.test.ts +1936 -0
  147. package/src/slack/toSlackBlocks.ts +1319 -0
  148. package/src/slack/types.ts +230 -0
  149. package/src/slack.ts +37 -0
  150. package/src/teams/boundSpec.ts +102 -0
  151. package/src/teams/constants.ts +127 -0
  152. package/src/teams/decodeSubmitData.test.ts +148 -0
  153. package/src/teams/decodeSubmitData.ts +57 -0
  154. package/src/teams/toAdaptiveCard.test.ts +1410 -0
  155. package/src/teams/toAdaptiveCard.ts +762 -0
  156. package/src/teams/toTeamsAttachments.test.ts +29 -0
  157. package/src/teams/toTeamsAttachments.ts +137 -0
  158. package/src/teams/types.ts +237 -0
  159. package/src/teams.ts +34 -0
  160. package/src/vocabulary/alert.test.tsx +72 -2
  161. package/src/vocabulary/alert.tsx +24 -5
  162. package/src/vocabulary/collectFormValues.test.ts +127 -0
  163. package/src/vocabulary/collectFormValues.ts +58 -0
  164. package/src/vocabulary/data.test.tsx +503 -3
  165. package/src/vocabulary/data.tsx +426 -18
  166. package/src/vocabulary/dispatch.ts +22 -0
  167. package/src/vocabulary/form.test.tsx +112 -0
  168. package/src/vocabulary/form.tsx +34 -0
  169. package/src/vocabulary/icon.test.tsx +64 -0
  170. package/src/vocabulary/icon.tsx +174 -0
  171. package/src/vocabulary/index.test.ts +7 -0
  172. package/src/vocabulary/index.ts +6 -0
  173. package/src/vocabulary/interactive.dispatch.test.tsx +243 -1
  174. package/src/vocabulary/interactive.test.tsx +143 -3
  175. package/src/vocabulary/interactive.tsx +134 -30
  176. package/src/vocabulary/layout.test.tsx +231 -3
  177. package/src/vocabulary/layout.tsx +168 -15
  178. package/src/vocabulary/list.test.tsx +195 -0
  179. package/src/vocabulary/list.tsx +48 -0
  180. package/src/vocabulary/media.test.tsx +36 -4
  181. package/src/vocabulary/media.tsx +27 -8
@@ -0,0 +1,1145 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { UIElement } from "../ir";
3
+ import { INBOUND_BLOCK_CAP } from "./constants";
4
+ import { fromSlackBlocks } from "./fromSlackBlocks";
5
+ import { toSlackBlocks } from "./toSlackBlocks";
6
+ import type { ToSlackBlocksOptions } from "./types";
7
+
8
+ describe("fromSlackBlocks", () => {
9
+ describe("malformed input", () => {
10
+ it("returns empty nodes and a dropped warning for non-blocks input", () => {
11
+ const malformed: unknown[] = [
12
+ null,
13
+ undefined,
14
+ 42,
15
+ "just text",
16
+ true,
17
+ {},
18
+ { blocks: "nope" },
19
+ ];
20
+ for (const input of malformed) {
21
+ const { nodes, warnings } = fromSlackBlocks(input);
22
+ expect(nodes).toEqual([]);
23
+ expect(warnings).toEqual([
24
+ {
25
+ code: "dropped",
26
+ component: "Root",
27
+ detail: "Input could not be converted.",
28
+ },
29
+ ]);
30
+ }
31
+ });
32
+
33
+ it("accepts a bare blocks array", () => {
34
+ const { nodes } = fromSlackBlocks([{ type: "divider" }]);
35
+ expect(nodes).toEqual([{ $type: "Divider" }]);
36
+ });
37
+
38
+ it("accepts a { blocks } wrapper", () => {
39
+ const { nodes } = fromSlackBlocks({ blocks: [{ type: "divider" }] });
40
+ expect(nodes).toEqual([{ $type: "Divider" }]);
41
+ });
42
+
43
+ it("never throws for degenerate block entries", () => {
44
+ const degenerate: unknown[] = [
45
+ null,
46
+ undefined,
47
+ 42,
48
+ "x",
49
+ true,
50
+ [],
51
+ {},
52
+ () => {},
53
+ ];
54
+ expect(() => fromSlackBlocks(degenerate)).not.toThrow();
55
+ const { nodes, warnings } = fromSlackBlocks(degenerate);
56
+ expect(nodes).toEqual([]);
57
+ expect(warnings).toHaveLength(degenerate.length);
58
+ });
59
+ });
60
+
61
+ describe("bounded arrays", () => {
62
+ it("clamps a hostile proxied blocks array instead of hanging", () => {
63
+ const hostileArray = new Proxy([], {
64
+ get(target, prop) {
65
+ if (prop === "length") return 2 ** 31;
66
+ if (typeof prop === "string" && /^\d+$/.test(prop)) {
67
+ return { type: "divider" };
68
+ }
69
+ return Reflect.get(target, prop);
70
+ },
71
+ has(target, prop) {
72
+ if (prop === "length") return true;
73
+ if (typeof prop === "string" && /^\d+$/.test(prop)) return true;
74
+ return Reflect.has(target, prop);
75
+ },
76
+ });
77
+ const { nodes, warnings } = fromSlackBlocks(hostileArray);
78
+ expect(nodes).toHaveLength(INBOUND_BLOCK_CAP);
79
+ expect(warnings).toContainEqual({
80
+ code: "clamped",
81
+ component: "Root",
82
+ detail: `blocks were clamped to ${INBOUND_BLOCK_CAP} entries.`,
83
+ });
84
+ });
85
+ });
86
+
87
+ describe("unknown block types", () => {
88
+ it("skips an unrecognized block with a dropped warning", () => {
89
+ const { nodes, warnings } = fromSlackBlocks([
90
+ { type: "totally_made_up" },
91
+ ]);
92
+ expect(nodes).toEqual([]);
93
+ expect(warnings).toEqual([
94
+ {
95
+ code: "dropped",
96
+ component: "totally_made_up",
97
+ detail: "Unknown block type was dropped.",
98
+ },
99
+ ]);
100
+ });
101
+ });
102
+
103
+ describe("Header", () => {
104
+ it("inverts a header block", () => {
105
+ const { nodes } = fromSlackBlocks([
106
+ { type: "header", text: { type: "plain_text", text: "Title" } },
107
+ ]);
108
+ expect(nodes).toEqual([{ $type: "Header", text: "Title" }]);
109
+ });
110
+ });
111
+
112
+ describe("section", () => {
113
+ it("inverts section fields into one Fact per field", () => {
114
+ const { nodes, warnings } = fromSlackBlocks([
115
+ {
116
+ type: "section",
117
+ fields: [
118
+ { type: "mrkdwn", text: "*Status*\nActive" },
119
+ { type: "mrkdwn", text: "*Owner*\nAda" },
120
+ ],
121
+ },
122
+ ]);
123
+ expect(nodes).toEqual([
124
+ { $type: "Fact", label: "Status", value: "Active" },
125
+ { $type: "Fact", label: "Owner", value: "Ada" },
126
+ ]);
127
+ expect(warnings).toEqual([]);
128
+ });
129
+
130
+ it("falls back to a value-only Fact when a field does not match the label/value format", () => {
131
+ const { nodes, warnings } = fromSlackBlocks([
132
+ {
133
+ type: "section",
134
+ fields: [{ type: "mrkdwn", text: "no format here" }],
135
+ },
136
+ ]);
137
+ expect(nodes).toEqual([
138
+ { $type: "Fact", label: "", value: "no format here" },
139
+ ]);
140
+ expect(warnings).toEqual([
141
+ {
142
+ code: "fallback",
143
+ component: "Fact",
144
+ detail:
145
+ "A section field that did not match the label/value format was kept as a value-only fact.",
146
+ },
147
+ ]);
148
+ });
149
+
150
+ it("inverts a section with mrkdwn text and a button accessory into a ListView of one ListViewItem", () => {
151
+ const { nodes } = fromSlackBlocks([
152
+ {
153
+ type: "section",
154
+ text: { type: "mrkdwn", text: "Clickable" },
155
+ accessory: {
156
+ type: "button",
157
+ text: { type: "plain_text", text: "Open" },
158
+ action_id: "open_row",
159
+ },
160
+ },
161
+ ]);
162
+ expect(nodes).toEqual([
163
+ {
164
+ $type: "ListView",
165
+ children: [
166
+ {
167
+ $type: "ListViewItem",
168
+ $action: { type: "open_row" },
169
+ children: { $type: "Text", value: "Clickable" },
170
+ },
171
+ ],
172
+ },
173
+ ]);
174
+ });
175
+
176
+ it("inverts a plain mrkdwn section into Text", () => {
177
+ const { nodes } = fromSlackBlocks([
178
+ { type: "section", text: { type: "mrkdwn", text: "Hello" } },
179
+ ]);
180
+ expect(nodes).toEqual([{ $type: "Text", value: "Hello" }]);
181
+ });
182
+ });
183
+
184
+ describe("context", () => {
185
+ it("inverts a single-element context into a Caption", () => {
186
+ const { nodes } = fromSlackBlocks([
187
+ { type: "context", elements: [{ type: "mrkdwn", text: "Fine print" }] },
188
+ ]);
189
+ expect(nodes).toEqual([{ $type: "Caption", value: "Fine print" }]);
190
+ });
191
+
192
+ it("inverts a multi-element context into a Row of Captions, collapsing the Badge/Caption distinction", () => {
193
+ const { nodes } = fromSlackBlocks([
194
+ {
195
+ type: "context",
196
+ elements: [
197
+ { type: "mrkdwn", text: "New" },
198
+ { type: "mrkdwn", text: "since today" },
199
+ ],
200
+ },
201
+ ]);
202
+ expect(nodes).toEqual([
203
+ {
204
+ $type: "Row",
205
+ children: [
206
+ { $type: "Caption", value: "New" },
207
+ { $type: "Caption", value: "since today" },
208
+ ],
209
+ },
210
+ ]);
211
+ });
212
+ });
213
+
214
+ describe("Image", () => {
215
+ it("inverts an image block", () => {
216
+ const { nodes } = fromSlackBlocks([
217
+ { type: "image", image_url: "https://x/y.png", alt_text: "A photo" },
218
+ ]);
219
+ expect(nodes).toEqual([
220
+ { $type: "Image", src: "https://x/y.png", alt: "A photo" },
221
+ ]);
222
+ });
223
+ });
224
+
225
+ describe("Divider", () => {
226
+ it("inverts a divider block", () => {
227
+ const { nodes } = fromSlackBlocks([{ type: "divider" }]);
228
+ expect(nodes).toEqual([{ $type: "Divider" }]);
229
+ });
230
+ });
231
+
232
+ describe("actions", () => {
233
+ it("inverts a button, mapping style to buttonStyle only for primary/danger and deriving $action from the object-only value payload", () => {
234
+ const { nodes: primary } = fromSlackBlocks([
235
+ {
236
+ type: "actions",
237
+ elements: [
238
+ {
239
+ type: "button",
240
+ text: { type: "plain_text", text: "Buy" },
241
+ action_id: "buy",
242
+ value: JSON.stringify({ sku: "abc" }),
243
+ style: "primary",
244
+ },
245
+ ],
246
+ },
247
+ ]);
248
+ expect(primary).toEqual([
249
+ {
250
+ $type: "Button",
251
+ label: "Buy",
252
+ buttonStyle: "primary",
253
+ $action: { type: "buy", sku: "abc" },
254
+ },
255
+ ]);
256
+
257
+ const { nodes: danger } = fromSlackBlocks([
258
+ {
259
+ type: "actions",
260
+ elements: [
261
+ {
262
+ type: "button",
263
+ text: { type: "plain_text", text: "Delete" },
264
+ action_id: "del",
265
+ style: "danger",
266
+ },
267
+ ],
268
+ },
269
+ ]);
270
+ expect((danger[0] as UIElement)["buttonStyle"]).toBe("danger");
271
+
272
+ const { nodes: plain } = fromSlackBlocks([
273
+ {
274
+ type: "actions",
275
+ elements: [
276
+ {
277
+ type: "button",
278
+ text: { type: "plain_text", text: "Go" },
279
+ action_id: "go",
280
+ },
281
+ ],
282
+ },
283
+ ]);
284
+ expect(plain[0]).not.toHaveProperty("buttonStyle");
285
+ expect(plain[0]).toEqual({
286
+ $type: "Button",
287
+ label: "Go",
288
+ $action: { type: "go" },
289
+ });
290
+ });
291
+
292
+ it("inverts a static_select", () => {
293
+ const { nodes } = fromSlackBlocks([
294
+ {
295
+ type: "actions",
296
+ elements: [
297
+ {
298
+ type: "static_select",
299
+ action_id: "pick",
300
+ options: [
301
+ { text: { type: "plain_text", text: "Alpha" }, value: "a" },
302
+ { text: { type: "plain_text", text: "Beta" }, value: "b" },
303
+ ],
304
+ placeholder: { type: "plain_text", text: "Pick one" },
305
+ },
306
+ ],
307
+ },
308
+ ]);
309
+ expect(nodes).toEqual([
310
+ {
311
+ $type: "Select",
312
+ options: [
313
+ { label: "Alpha", value: "a" },
314
+ { label: "Beta", value: "b" },
315
+ ],
316
+ placeholder: "Pick one",
317
+ $action: { type: "pick" },
318
+ },
319
+ ]);
320
+ });
321
+
322
+ it("inverts a datepicker, reading value from initial_date and omitting it when absent", () => {
323
+ const { nodes: withDate } = fromSlackBlocks([
324
+ {
325
+ type: "actions",
326
+ elements: [
327
+ {
328
+ type: "datepicker",
329
+ action_id: "pick_date",
330
+ initial_date: "2026-07-15",
331
+ },
332
+ ],
333
+ },
334
+ ]);
335
+ expect(withDate).toEqual([
336
+ {
337
+ $type: "DatePicker",
338
+ value: "2026-07-15",
339
+ $action: { type: "pick_date" },
340
+ },
341
+ ]);
342
+
343
+ const { nodes: withoutDate } = fromSlackBlocks([
344
+ {
345
+ type: "actions",
346
+ elements: [{ type: "datepicker", action_id: "pick_date" }],
347
+ },
348
+ ]);
349
+ expect(withoutDate[0]).not.toHaveProperty("value");
350
+ });
351
+
352
+ it("inverts checkboxes, reading label/name from the first option and defaultChecked from initial_options", () => {
353
+ const { nodes: checked } = fromSlackBlocks([
354
+ {
355
+ type: "actions",
356
+ elements: [
357
+ {
358
+ type: "checkboxes",
359
+ action_id: "toggle",
360
+ options: [
361
+ { text: { type: "plain_text", text: "Agree" }, value: "agree" },
362
+ ],
363
+ initial_options: [
364
+ { text: { type: "plain_text", text: "Agree" }, value: "agree" },
365
+ ],
366
+ },
367
+ ],
368
+ },
369
+ ]);
370
+ expect(checked).toEqual([
371
+ {
372
+ $type: "Checkbox",
373
+ label: "Agree",
374
+ name: "agree",
375
+ defaultChecked: true,
376
+ $action: { type: "toggle" },
377
+ },
378
+ ]);
379
+
380
+ const { nodes: unchecked } = fromSlackBlocks([
381
+ {
382
+ type: "actions",
383
+ elements: [
384
+ {
385
+ type: "checkboxes",
386
+ action_id: "toggle",
387
+ options: [
388
+ { text: { type: "plain_text", text: "Agree" }, value: "agree" },
389
+ ],
390
+ },
391
+ ],
392
+ },
393
+ ]);
394
+ expect(unchecked[0]).not.toHaveProperty("defaultChecked");
395
+ });
396
+
397
+ it("inverts radio_buttons, reading defaultValue from initial_option and omitting it when absent", () => {
398
+ const { nodes: withValue } = fromSlackBlocks([
399
+ {
400
+ type: "actions",
401
+ elements: [
402
+ {
403
+ type: "radio_buttons",
404
+ action_id: "size",
405
+ options: [
406
+ { text: { type: "plain_text", text: "Small" }, value: "s" },
407
+ { text: { type: "plain_text", text: "Large" }, value: "l" },
408
+ ],
409
+ initial_option: {
410
+ text: { type: "plain_text", text: "Large" },
411
+ value: "l",
412
+ },
413
+ },
414
+ ],
415
+ },
416
+ ]);
417
+ expect(withValue).toEqual([
418
+ {
419
+ $type: "RadioGroup",
420
+ options: [
421
+ { label: "Small", value: "s" },
422
+ { label: "Large", value: "l" },
423
+ ],
424
+ defaultValue: "l",
425
+ $action: { type: "size" },
426
+ },
427
+ ]);
428
+
429
+ const { nodes: withoutValue } = fromSlackBlocks([
430
+ {
431
+ type: "actions",
432
+ elements: [
433
+ {
434
+ type: "radio_buttons",
435
+ action_id: "size",
436
+ options: [
437
+ { text: { type: "plain_text", text: "Small" }, value: "s" },
438
+ ],
439
+ },
440
+ ],
441
+ },
442
+ ]);
443
+ expect(withoutValue[0]).not.toHaveProperty("defaultValue");
444
+ });
445
+
446
+ it("skips an unrecognized action element with a dropped warning, keeping the rest", () => {
447
+ const { nodes, warnings } = fromSlackBlocks([
448
+ {
449
+ type: "actions",
450
+ elements: [
451
+ { type: "mystery_widget" },
452
+ {
453
+ type: "button",
454
+ text: { type: "plain_text", text: "Go" },
455
+ action_id: "go",
456
+ },
457
+ ],
458
+ },
459
+ ]);
460
+ expect(nodes).toEqual([
461
+ { $type: "Button", label: "Go", $action: { type: "go" } },
462
+ ]);
463
+ expect(warnings).toContainEqual({
464
+ code: "dropped",
465
+ component: "mystery_widget",
466
+ detail: "Unknown action element type was dropped.",
467
+ });
468
+ });
469
+ });
470
+
471
+ describe("Input", () => {
472
+ it("inverts an input block, passing multiline and placeholder through", () => {
473
+ const { nodes } = fromSlackBlocks([
474
+ {
475
+ type: "input",
476
+ label: { type: "plain_text", text: "Notes" },
477
+ element: {
478
+ type: "plain_text_input",
479
+ action_id: "notes",
480
+ multiline: true,
481
+ placeholder: { type: "plain_text", text: "Type here" },
482
+ },
483
+ },
484
+ ]);
485
+ expect(nodes).toEqual([
486
+ {
487
+ $type: "Input",
488
+ label: "Notes",
489
+ placeholder: "Type here",
490
+ multiline: true,
491
+ $action: { type: "notes" },
492
+ },
493
+ ]);
494
+ });
495
+
496
+ it("omits multiline when not set", () => {
497
+ const { nodes } = fromSlackBlocks([
498
+ {
499
+ type: "input",
500
+ label: { type: "plain_text", text: "Name" },
501
+ element: { type: "plain_text_input", action_id: "name" },
502
+ },
503
+ ]);
504
+ expect(nodes[0]).not.toHaveProperty("multiline");
505
+ });
506
+
507
+ it("drops an input block wrapping an unrecognized element", () => {
508
+ const { nodes, warnings } = fromSlackBlocks([
509
+ {
510
+ type: "input",
511
+ label: { type: "plain_text", text: "Name" },
512
+ element: { type: "mystery" },
513
+ },
514
+ ]);
515
+ expect(nodes).toEqual([]);
516
+ expect(warnings).toContainEqual({
517
+ code: "dropped",
518
+ component: "Input",
519
+ detail: "Unknown input element type was dropped.",
520
+ });
521
+ });
522
+ });
523
+
524
+ describe("Markdown", () => {
525
+ it("inverts a markdown block", () => {
526
+ const { nodes } = fromSlackBlocks([
527
+ { type: "markdown", text: "# Title" },
528
+ ]);
529
+ expect(nodes).toEqual([{ $type: "Markdown", value: "# Title" }]);
530
+ });
531
+ });
532
+
533
+ describe("Card", () => {
534
+ it("inverts a clean card with hero image, body, subtext, and confirm/cancel actions", () => {
535
+ const { nodes, warnings } = fromSlackBlocks([
536
+ {
537
+ type: "card",
538
+ hero_image: {
539
+ type: "image",
540
+ image_url: "https://x/y.png",
541
+ alt_text: "Package",
542
+ },
543
+ title: { type: "mrkdwn", text: "Order #42" },
544
+ body: { type: "mrkdwn", text: "Shipped" },
545
+ subtext: { type: "mrkdwn", text: "Updated just now" },
546
+ actions: [
547
+ {
548
+ type: "button",
549
+ text: { type: "plain_text", text: "Ship" },
550
+ action_id: "ship",
551
+ style: "primary",
552
+ },
553
+ {
554
+ type: "button",
555
+ text: { type: "plain_text", text: "Cancel" },
556
+ action_id: "cancel",
557
+ },
558
+ ],
559
+ },
560
+ ]);
561
+ expect(nodes).toEqual([
562
+ {
563
+ $type: "Card",
564
+ title: "Order #42",
565
+ children: [
566
+ { $type: "Image", src: "https://x/y.png", alt: "Package" },
567
+ { $type: "Text", value: "Shipped" },
568
+ { $type: "Caption", value: "Updated just now" },
569
+ ],
570
+ confirm: { label: "Ship", $action: { type: "ship" } },
571
+ cancel: { label: "Cancel", $action: { type: "cancel" } },
572
+ },
573
+ ]);
574
+ expect(warnings).toEqual([]);
575
+ });
576
+
577
+ it("appends card actions beyond confirm/cancel as Button children with a fallback warning", () => {
578
+ const { nodes, warnings } = fromSlackBlocks([
579
+ {
580
+ type: "card",
581
+ title: { type: "mrkdwn", text: "Order" },
582
+ actions: [
583
+ {
584
+ type: "button",
585
+ text: { type: "plain_text", text: "Yes" },
586
+ action_id: "yes",
587
+ },
588
+ {
589
+ type: "button",
590
+ text: { type: "plain_text", text: "No" },
591
+ action_id: "no",
592
+ },
593
+ {
594
+ type: "button",
595
+ text: { type: "plain_text", text: "Maybe" },
596
+ action_id: "maybe",
597
+ },
598
+ ],
599
+ },
600
+ ]);
601
+ const card = nodes[0] as UIElement;
602
+ expect(card["children"]).toEqual([
603
+ { $type: "Button", label: "Maybe", $action: { type: "maybe" } },
604
+ ]);
605
+ expect(warnings).toContainEqual({
606
+ code: "fallback",
607
+ component: "Card",
608
+ detail:
609
+ "Card actions beyond confirm and cancel were appended as Button children.",
610
+ });
611
+ });
612
+
613
+ it("reconstructs a single non-primary button as cancel, not confirm", () => {
614
+ const { nodes, warnings } = fromSlackBlocks([
615
+ {
616
+ type: "card",
617
+ title: { type: "mrkdwn", text: "Order" },
618
+ actions: [
619
+ {
620
+ type: "button",
621
+ text: { type: "plain_text", text: "Dismiss" },
622
+ action_id: "dismiss",
623
+ },
624
+ ],
625
+ },
626
+ ]);
627
+ expect(nodes).toEqual([
628
+ {
629
+ $type: "Card",
630
+ title: "Order",
631
+ cancel: { label: "Dismiss", $action: { type: "dismiss" } },
632
+ },
633
+ ]);
634
+ expect(nodes[0]).not.toHaveProperty("confirm");
635
+ expect(warnings).toEqual([]);
636
+ });
637
+ });
638
+
639
+ describe("Alert", () => {
640
+ it("maps alert level to tone, mapping error to danger and omitting tone for the default level", () => {
641
+ const cases: readonly [string, string | undefined][] = [
642
+ ["error", "danger"],
643
+ ["success", "success"],
644
+ ["warning", "warning"],
645
+ ["info", "info"],
646
+ ["default", undefined],
647
+ ];
648
+ for (const [level, tone] of cases) {
649
+ const { nodes } = fromSlackBlocks([
650
+ { type: "alert", text: { type: "mrkdwn", text: "x" }, level },
651
+ ]);
652
+ expect(nodes).toEqual([
653
+ {
654
+ $type: "Alert",
655
+ description: "x",
656
+ ...(tone !== undefined ? { tone } : {}),
657
+ },
658
+ ]);
659
+ }
660
+ });
661
+
662
+ it("keeps a joined title and description together in description, since the two cannot be separated", () => {
663
+ const { nodes } = fromSlackBlocks([
664
+ {
665
+ type: "alert",
666
+ text: { type: "mrkdwn", text: "Heads up\nSomething happened." },
667
+ level: "info",
668
+ },
669
+ ]);
670
+ expect(nodes).toEqual([
671
+ {
672
+ $type: "Alert",
673
+ description: "Heads up\nSomething happened.",
674
+ tone: "info",
675
+ },
676
+ ]);
677
+ });
678
+ });
679
+
680
+ describe("Carousel", () => {
681
+ it("inverts a carousel's card elements", () => {
682
+ const { nodes } = fromSlackBlocks([
683
+ {
684
+ type: "carousel",
685
+ elements: [
686
+ { type: "card", title: { type: "mrkdwn", text: "One" } },
687
+ { type: "card", title: { type: "mrkdwn", text: "Two" } },
688
+ ],
689
+ },
690
+ ]);
691
+ expect(nodes).toEqual([
692
+ {
693
+ $type: "Carousel",
694
+ children: [
695
+ { $type: "Card", title: "One" },
696
+ { $type: "Card", title: "Two" },
697
+ ],
698
+ },
699
+ ]);
700
+ });
701
+ });
702
+
703
+ describe("Table", () => {
704
+ it("inverts columns and typed rows", () => {
705
+ const { nodes } = fromSlackBlocks([
706
+ {
707
+ type: "data_table",
708
+ caption: "Table",
709
+ rows: [
710
+ [
711
+ { type: "raw_text", text: "Name" },
712
+ { type: "raw_text", text: "Age" },
713
+ ],
714
+ [
715
+ { type: "raw_text", text: "Ada" },
716
+ { type: "raw_number", text: "36" },
717
+ ],
718
+ ],
719
+ },
720
+ ]);
721
+ expect(nodes).toEqual([
722
+ {
723
+ $type: "Table",
724
+ columns: [{ label: "Name" }, { label: "Age" }],
725
+ rows: [["Ada", 36]],
726
+ },
727
+ ]);
728
+ });
729
+
730
+ it("omits columns when every header cell is empty, inverting the rows-only synthesis", () => {
731
+ const { nodes } = fromSlackBlocks([
732
+ {
733
+ type: "data_table",
734
+ caption: "Table",
735
+ rows: [
736
+ [
737
+ { type: "raw_text", text: "" },
738
+ { type: "raw_text", text: "" },
739
+ ],
740
+ [
741
+ { type: "raw_text", text: "Ada" },
742
+ { type: "raw_number", text: "36" },
743
+ ],
744
+ ],
745
+ },
746
+ ]);
747
+ expect(nodes).toEqual([{ $type: "Table", rows: [["Ada", 36]] }]);
748
+ });
749
+
750
+ it("keeps a raw_number cell as its raw string when the text is not a finite number", () => {
751
+ const { nodes } = fromSlackBlocks([
752
+ {
753
+ type: "data_table",
754
+ caption: "Table",
755
+ rows: [
756
+ [{ type: "raw_text", text: "Name" }],
757
+ [{ type: "raw_number", text: "N/A" }],
758
+ ],
759
+ },
760
+ ]);
761
+ expect(nodes).toEqual([
762
+ { $type: "Table", columns: [{ label: "Name" }], rows: [["N/A"]] },
763
+ ]);
764
+ });
765
+ });
766
+
767
+ describe("round trip", () => {
768
+ const fixtures: readonly {
769
+ readonly name: string;
770
+ readonly tree: unknown;
771
+ readonly options?: ToSlackBlocksOptions;
772
+ readonly expected: readonly UIElement[];
773
+ }[] = [
774
+ {
775
+ name: "Header",
776
+ tree: { $type: "Header", text: "Title" },
777
+ expected: [{ $type: "Header", text: "Title" }],
778
+ },
779
+ {
780
+ name: "Text",
781
+ tree: { $type: "Text", value: "Hello" },
782
+ expected: [{ $type: "Text", value: "Hello" }],
783
+ },
784
+ {
785
+ name: "Caption",
786
+ tree: { $type: "Caption", value: "Fine print" },
787
+ expected: [{ $type: "Caption", value: "Fine print" }],
788
+ },
789
+ {
790
+ name: "Image",
791
+ tree: { $type: "Image", src: "https://x/y.png", alt: "A photo" },
792
+ expected: [{ $type: "Image", src: "https://x/y.png", alt: "A photo" }],
793
+ },
794
+ {
795
+ name: "Divider",
796
+ tree: { $type: "Divider" },
797
+ expected: [{ $type: "Divider" }],
798
+ },
799
+ {
800
+ name: "Markdown",
801
+ tree: { $type: "Markdown", value: "# Title" },
802
+ expected: [{ $type: "Markdown", value: "# Title" }],
803
+ },
804
+ {
805
+ name: "Button with payload",
806
+ tree: {
807
+ $type: "Button",
808
+ label: "Buy",
809
+ buttonStyle: "primary",
810
+ $action: { type: "buy", sku: "abc" },
811
+ },
812
+ expected: [
813
+ {
814
+ $type: "Button",
815
+ label: "Buy",
816
+ buttonStyle: "primary",
817
+ $action: { type: "buy", sku: "abc" },
818
+ },
819
+ ],
820
+ },
821
+ {
822
+ name: "Select",
823
+ tree: {
824
+ $type: "Select",
825
+ options: [
826
+ { label: "Alpha", value: "a" },
827
+ { label: "Beta", value: "b" },
828
+ ],
829
+ placeholder: "Pick one",
830
+ $action: { type: "pick" },
831
+ },
832
+ expected: [
833
+ {
834
+ $type: "Select",
835
+ options: [
836
+ { label: "Alpha", value: "a" },
837
+ { label: "Beta", value: "b" },
838
+ ],
839
+ placeholder: "Pick one",
840
+ $action: { type: "pick" },
841
+ },
842
+ ],
843
+ },
844
+ {
845
+ name: "DatePicker with initial date",
846
+ tree: {
847
+ $type: "DatePicker",
848
+ value: "2026-07-15",
849
+ $action: { type: "pick_date" },
850
+ },
851
+ expected: [
852
+ {
853
+ $type: "DatePicker",
854
+ value: "2026-07-15",
855
+ $action: { type: "pick_date" },
856
+ },
857
+ ],
858
+ },
859
+ {
860
+ name: "Checkbox with defaultChecked",
861
+ tree: {
862
+ $type: "Checkbox",
863
+ label: "Agree",
864
+ name: "agree",
865
+ defaultChecked: true,
866
+ $action: { type: "toggle" },
867
+ },
868
+ expected: [
869
+ {
870
+ $type: "Checkbox",
871
+ label: "Agree",
872
+ name: "agree",
873
+ defaultChecked: true,
874
+ $action: { type: "toggle" },
875
+ },
876
+ ],
877
+ },
878
+ {
879
+ name: "RadioGroup with defaultValue",
880
+ tree: {
881
+ $type: "RadioGroup",
882
+ options: [
883
+ { label: "Small", value: "s" },
884
+ { label: "Large", value: "l" },
885
+ ],
886
+ defaultValue: "l",
887
+ $action: { type: "size" },
888
+ },
889
+ expected: [
890
+ {
891
+ $type: "RadioGroup",
892
+ options: [
893
+ { label: "Small", value: "s" },
894
+ { label: "Large", value: "l" },
895
+ ],
896
+ defaultValue: "l",
897
+ $action: { type: "size" },
898
+ },
899
+ ],
900
+ },
901
+ {
902
+ name: "Input",
903
+ tree: {
904
+ $type: "Input",
905
+ label: "Notes",
906
+ placeholder: "Type here",
907
+ multiline: true,
908
+ $action: { type: "notes" },
909
+ },
910
+ expected: [
911
+ {
912
+ $type: "Input",
913
+ label: "Notes",
914
+ placeholder: "Type here",
915
+ multiline: true,
916
+ $action: { type: "notes" },
917
+ },
918
+ ],
919
+ },
920
+ {
921
+ name: "Fact pair",
922
+ tree: [
923
+ { $type: "Fact", label: "Status", value: "Active" },
924
+ { $type: "Fact", label: "Owner", value: "Ada" },
925
+ ],
926
+ expected: [
927
+ { $type: "Fact", label: "Status", value: "Active" },
928
+ { $type: "Fact", label: "Owner", value: "Ada" },
929
+ ],
930
+ },
931
+ {
932
+ name: "simple Card with title and body text",
933
+ tree: {
934
+ $type: "Card",
935
+ title: "Order #42",
936
+ children: [{ $type: "Text", value: "Shipped" }],
937
+ },
938
+ expected: [
939
+ {
940
+ $type: "Card",
941
+ title: "Order #42",
942
+ children: [{ $type: "Text", value: "Shipped" }],
943
+ },
944
+ ],
945
+ },
946
+ {
947
+ name: "Alert (modal surface, where the native alert block round-trips; the message-surface fallback is not invertible into an Alert)",
948
+ tree: {
949
+ $type: "Alert",
950
+ description: "Something happened.",
951
+ tone: "danger",
952
+ },
953
+ options: { surface: "modal" },
954
+ expected: [
955
+ {
956
+ $type: "Alert",
957
+ description: "Something happened.",
958
+ tone: "danger",
959
+ },
960
+ ],
961
+ },
962
+ {
963
+ name: "Carousel of two simple Cards",
964
+ tree: {
965
+ $type: "Carousel",
966
+ children: [
967
+ {
968
+ $type: "Card",
969
+ title: "One",
970
+ children: [{ $type: "Text", value: "First" }],
971
+ },
972
+ {
973
+ $type: "Card",
974
+ title: "Two",
975
+ children: [{ $type: "Text", value: "Second" }],
976
+ },
977
+ ],
978
+ },
979
+ expected: [
980
+ {
981
+ $type: "Carousel",
982
+ children: [
983
+ {
984
+ $type: "Card",
985
+ title: "One",
986
+ children: [{ $type: "Text", value: "First" }],
987
+ },
988
+ {
989
+ $type: "Card",
990
+ title: "Two",
991
+ children: [{ $type: "Text", value: "Second" }],
992
+ },
993
+ ],
994
+ },
995
+ ],
996
+ },
997
+ {
998
+ name: "ListView of two actionable items",
999
+ tree: {
1000
+ $type: "ListView",
1001
+ children: [
1002
+ {
1003
+ $type: "ListViewItem",
1004
+ $action: { type: "open_one" },
1005
+ children: { $type: "Text", value: "Row one" },
1006
+ },
1007
+ {
1008
+ $type: "ListViewItem",
1009
+ $action: { type: "open_two" },
1010
+ children: { $type: "Text", value: "Row two" },
1011
+ },
1012
+ ],
1013
+ },
1014
+ expected: [
1015
+ {
1016
+ $type: "ListView",
1017
+ children: [
1018
+ {
1019
+ $type: "ListViewItem",
1020
+ $action: { type: "open_one" },
1021
+ children: { $type: "Text", value: "Row one" },
1022
+ },
1023
+ {
1024
+ $type: "ListViewItem",
1025
+ $action: { type: "open_two" },
1026
+ children: { $type: "Text", value: "Row two" },
1027
+ },
1028
+ ],
1029
+ },
1030
+ ],
1031
+ },
1032
+ {
1033
+ name: "Table with columns and mixed string/number rows",
1034
+ tree: {
1035
+ $type: "Table",
1036
+ columns: [{ label: "Name" }, { label: "Age" }],
1037
+ rows: [
1038
+ ["Ada", 36],
1039
+ ["Bob", 24],
1040
+ ],
1041
+ },
1042
+ expected: [
1043
+ {
1044
+ $type: "Table",
1045
+ columns: [{ label: "Name" }, { label: "Age" }],
1046
+ rows: [
1047
+ ["Ada", 36],
1048
+ ["Bob", 24],
1049
+ ],
1050
+ },
1051
+ ],
1052
+ },
1053
+ ];
1054
+
1055
+ for (const fixture of fixtures) {
1056
+ it(`round-trips ${fixture.name}`, () => {
1057
+ const { blocks } = toSlackBlocks(fixture.tree, fixture.options);
1058
+ const { nodes } = fromSlackBlocks(blocks);
1059
+ expect(nodes).toEqual(fixture.expected);
1060
+ });
1061
+ }
1062
+ });
1063
+ });
1064
+
1065
+ describe("fromSlackBlocks checkbox and radio caps", () => {
1066
+ it("marks defaultChecked only when the first option is initially selected", () => {
1067
+ const base = {
1068
+ type: "actions",
1069
+ elements: [
1070
+ {
1071
+ type: "checkboxes",
1072
+ action_id: "toggle",
1073
+ options: [
1074
+ { text: { type: "plain_text", text: "First" }, value: "first" },
1075
+ { text: { type: "plain_text", text: "Second" }, value: "second" },
1076
+ ],
1077
+ initial_options: [
1078
+ { text: { type: "plain_text", text: "Second" }, value: "second" },
1079
+ ],
1080
+ },
1081
+ ],
1082
+ };
1083
+ const { nodes } = fromSlackBlocks([base]);
1084
+ expect(nodes[0]).not.toHaveProperty("defaultChecked");
1085
+
1086
+ const checkedFirst = structuredClone(base);
1087
+ checkedFirst.elements[0]!.initial_options = [
1088
+ { text: { type: "plain_text", text: "First" }, value: "first" },
1089
+ ];
1090
+ const checked = fromSlackBlocks([checkedFirst]);
1091
+ expect(checked.nodes[0]).toMatchObject({ defaultChecked: true });
1092
+ });
1093
+
1094
+ it("clamps inbound radio options to the radio cap", () => {
1095
+ const { nodes, warnings } = fromSlackBlocks([
1096
+ {
1097
+ type: "actions",
1098
+ elements: [
1099
+ {
1100
+ type: "radio_buttons",
1101
+ action_id: "pick",
1102
+ options: Array.from({ length: 30 }, (_, i) => ({
1103
+ text: { type: "plain_text", text: `O${i}` },
1104
+ value: `v${i}`,
1105
+ })),
1106
+ },
1107
+ ],
1108
+ },
1109
+ ]);
1110
+ const radio = nodes[0] as { options: unknown[] };
1111
+ expect(radio.options).toHaveLength(10);
1112
+ expect(
1113
+ warnings.some(
1114
+ (warning) =>
1115
+ warning.component === "RadioGroup" && warning.code === "clamped",
1116
+ ),
1117
+ ).toBe(true);
1118
+ });
1119
+ });
1120
+
1121
+ it("bounds hostile initial_options arrays when deriving defaultChecked", () => {
1122
+ const hostile = new Proxy([], {
1123
+ get(target, prop) {
1124
+ if (prop === "length") return 2 ** 31;
1125
+ if (prop === "slice") return Array.prototype.slice.bind(target);
1126
+ return Reflect.get(target, prop);
1127
+ },
1128
+ });
1129
+ const { nodes } = fromSlackBlocks([
1130
+ {
1131
+ type: "actions",
1132
+ elements: [
1133
+ {
1134
+ type: "checkboxes",
1135
+ action_id: "toggle",
1136
+ options: [
1137
+ { text: { type: "plain_text", text: "First" }, value: "first" },
1138
+ ],
1139
+ initial_options: hostile,
1140
+ },
1141
+ ],
1142
+ },
1143
+ ]);
1144
+ expect(nodes[0]).not.toHaveProperty("defaultChecked");
1145
+ });