@terreno/ui 57.9.0 → 57.10.1

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 (162) hide show
  1. package/README.md +2 -0
  2. package/dist/Button.d.ts.map +1 -1
  3. package/dist/Button.js +19 -4
  4. package/dist/Button.js.map +1 -1
  5. package/dist/Common.d.ts +68 -1
  6. package/dist/Common.d.ts.map +1 -1
  7. package/dist/Common.js.map +1 -1
  8. package/dist/DateTimeField.d.ts.map +1 -1
  9. package/dist/DateTimeField.js +90 -17
  10. package/dist/DateTimeField.js.map +1 -1
  11. package/dist/FilePickerButton.d.ts +15 -1
  12. package/dist/FilePickerButton.d.ts.map +1 -1
  13. package/dist/FilePickerButton.js +26 -9
  14. package/dist/FilePickerButton.js.map +1 -1
  15. package/dist/GPTChat.d.ts +57 -1
  16. package/dist/GPTChat.d.ts.map +1 -1
  17. package/dist/GPTChat.js +199 -7
  18. package/dist/GPTChat.js.map +1 -1
  19. package/dist/HtmlFrame.d.ts +11 -0
  20. package/dist/HtmlFrame.d.ts.map +1 -0
  21. package/dist/HtmlFrame.js +29 -0
  22. package/dist/HtmlFrame.js.map +1 -0
  23. package/dist/Text.d.ts +1 -1
  24. package/dist/Text.d.ts.map +1 -1
  25. package/dist/Text.js +2 -2
  26. package/dist/Text.js.map +1 -1
  27. package/dist/asks/AskCard.d.ts +34 -0
  28. package/dist/asks/AskCard.d.ts.map +1 -0
  29. package/dist/asks/AskCard.js +91 -0
  30. package/dist/asks/AskCard.js.map +1 -0
  31. package/dist/asks/AskChoice.d.ts +11 -0
  32. package/dist/asks/AskChoice.d.ts.map +1 -0
  33. package/dist/asks/AskChoice.js +146 -0
  34. package/dist/asks/AskChoice.js.map +1 -0
  35. package/dist/asks/AskConfirm.d.ts +15 -0
  36. package/dist/asks/AskConfirm.d.ts.map +1 -0
  37. package/dist/asks/AskConfirm.js +17 -0
  38. package/dist/asks/AskConfirm.js.map +1 -0
  39. package/dist/asks/AskFiles.d.ts +18 -0
  40. package/dist/asks/AskFiles.d.ts.map +1 -0
  41. package/dist/asks/AskFiles.js +149 -0
  42. package/dist/asks/AskFiles.js.map +1 -0
  43. package/dist/asks/AskForm.d.ts +27 -0
  44. package/dist/asks/AskForm.d.ts.map +1 -0
  45. package/dist/asks/AskForm.js +146 -0
  46. package/dist/asks/AskForm.js.map +1 -0
  47. package/dist/asks/AskMarkdown.d.ts +25 -0
  48. package/dist/asks/AskMarkdown.d.ts.map +1 -0
  49. package/dist/asks/AskMarkdown.js +73 -0
  50. package/dist/asks/AskMarkdown.js.map +1 -0
  51. package/dist/asks/SimpleAskCard.d.ts +23 -0
  52. package/dist/asks/SimpleAskCard.d.ts.map +1 -0
  53. package/dist/asks/SimpleAskCard.js +32 -0
  54. package/dist/asks/SimpleAskCard.js.map +1 -0
  55. package/dist/asks/askControls.d.ts +33 -0
  56. package/dist/asks/askControls.d.ts.map +1 -0
  57. package/dist/asks/askControls.js +30 -0
  58. package/dist/asks/askControls.js.map +1 -0
  59. package/dist/asks/askFileRefs.d.ts +26 -0
  60. package/dist/asks/askFileRefs.d.ts.map +1 -0
  61. package/dist/asks/askFileRefs.js +75 -0
  62. package/dist/asks/askFileRefs.js.map +1 -0
  63. package/dist/asks/askFormDraft.d.ts +60 -0
  64. package/dist/asks/askFormDraft.d.ts.map +1 -0
  65. package/dist/asks/askFormDraft.js +224 -0
  66. package/dist/asks/askFormDraft.js.map +1 -0
  67. package/dist/asks/askSummary.d.ts +10 -0
  68. package/dist/asks/askSummary.d.ts.map +1 -0
  69. package/dist/asks/askSummary.js +138 -0
  70. package/dist/asks/askSummary.js.map +1 -0
  71. package/dist/asks/askTypes.d.ts +21 -0
  72. package/dist/asks/askTypes.d.ts.map +1 -0
  73. package/dist/asks/askTypes.js +2 -0
  74. package/dist/asks/askTypes.js.map +1 -0
  75. package/dist/asks/simpleCardButtonVariants.d.ts +5 -0
  76. package/dist/asks/simpleCardButtonVariants.d.ts.map +1 -0
  77. package/dist/asks/simpleCardButtonVariants.js +8 -0
  78. package/dist/asks/simpleCardButtonVariants.js.map +1 -0
  79. package/dist/blocks/BlocksError.d.ts +8 -0
  80. package/dist/blocks/BlocksError.d.ts.map +1 -0
  81. package/dist/blocks/BlocksError.js +14 -0
  82. package/dist/blocks/BlocksError.js.map +1 -0
  83. package/dist/blocks/BlocksView.d.ts +10 -0
  84. package/dist/blocks/BlocksView.d.ts.map +1 -0
  85. package/dist/blocks/BlocksView.js +90 -0
  86. package/dist/blocks/BlocksView.js.map +1 -0
  87. package/dist/blocks/blockRenderers.d.ts +25 -0
  88. package/dist/blocks/blockRenderers.d.ts.map +1 -0
  89. package/dist/blocks/blockRenderers.js +136 -0
  90. package/dist/blocks/blockRenderers.js.map +1 -0
  91. package/dist/blocks/datasetToPoints.d.ts +22 -0
  92. package/dist/blocks/datasetToPoints.d.ts.map +1 -0
  93. package/dist/blocks/datasetToPoints.js +28 -0
  94. package/dist/blocks/datasetToPoints.js.map +1 -0
  95. package/dist/blocks/useBlockSelections.d.ts +6 -0
  96. package/dist/blocks/useBlockSelections.d.ts.map +1 -0
  97. package/dist/blocks/useBlockSelections.js +10 -0
  98. package/dist/blocks/useBlockSelections.js.map +1 -0
  99. package/dist/blocks/useResolvedDatasets.d.ts +18 -0
  100. package/dist/blocks/useResolvedDatasets.d.ts.map +1 -0
  101. package/dist/blocks/useResolvedDatasets.js +59 -0
  102. package/dist/blocks/useResolvedDatasets.js.map +1 -0
  103. package/dist/blocks/useResolvedImages.d.ts +15 -0
  104. package/dist/blocks/useResolvedImages.d.ts.map +1 -0
  105. package/dist/blocks/useResolvedImages.js +66 -0
  106. package/dist/blocks/useResolvedImages.js.map +1 -0
  107. package/dist/index.d.ts +6 -1
  108. package/dist/index.d.ts.map +1 -1
  109. package/dist/index.js +4 -1
  110. package/dist/index.js.map +1 -1
  111. package/dist/lazyBoundaries/heavyOptionalExports.d.ts +6 -0
  112. package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
  113. package/dist/lazyBoundaries/heavyOptionalExports.js +4 -0
  114. package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
  115. package/package.json +2 -1
  116. package/src/Button.test.tsx +72 -0
  117. package/src/Button.tsx +34 -4
  118. package/src/Common.ts +67 -0
  119. package/src/DateTimeField.test.tsx +185 -2
  120. package/src/DateTimeField.tsx +106 -16
  121. package/src/DonutChart.test.tsx +5 -10
  122. package/src/FilePickerButton.test.tsx +56 -3
  123. package/src/FilePickerButton.tsx +83 -17
  124. package/src/GPTChat.test.tsx +758 -2
  125. package/src/GPTChat.tsx +517 -7
  126. package/src/HtmlFrame.tsx +60 -0
  127. package/src/LineChart.test.tsx +5 -10
  128. package/src/Text.test.tsx +22 -0
  129. package/src/Text.tsx +10 -1
  130. package/src/asks/AskCard.test.tsx +1428 -0
  131. package/src/asks/AskCard.tsx +240 -0
  132. package/src/asks/AskChoice.tsx +368 -0
  133. package/src/asks/AskConfirm.tsx +42 -0
  134. package/src/asks/AskFiles.test.tsx +347 -0
  135. package/src/asks/AskFiles.tsx +246 -0
  136. package/src/asks/AskForm.tsx +334 -0
  137. package/src/asks/AskMarkdown.tsx +176 -0
  138. package/src/asks/SimpleAskCard.test.tsx +253 -0
  139. package/src/asks/SimpleAskCard.tsx +82 -0
  140. package/src/asks/askControls.tsx +98 -0
  141. package/src/asks/askFileRefs.test.ts +155 -0
  142. package/src/asks/askFileRefs.ts +98 -0
  143. package/src/asks/askFormDraft.test.ts +347 -0
  144. package/src/asks/askFormDraft.ts +309 -0
  145. package/src/asks/askSummary.test.ts +255 -0
  146. package/src/asks/askSummary.ts +160 -0
  147. package/src/asks/askTypes.ts +25 -0
  148. package/src/asks/asksUseTerrenoComponents.test.ts +55 -0
  149. package/src/asks/simpleCardButtonVariants.ts +14 -0
  150. package/src/blocks/BlocksError.tsx +28 -0
  151. package/src/blocks/BlocksView.test.tsx +607 -0
  152. package/src/blocks/BlocksView.tsx +125 -0
  153. package/src/blocks/blockRenderers.tsx +288 -0
  154. package/src/blocks/datasetToPoints.test.ts +31 -0
  155. package/src/blocks/datasetToPoints.ts +48 -0
  156. package/src/blocks/useBlockSelections.ts +13 -0
  157. package/src/blocks/useResolvedDatasets.ts +78 -0
  158. package/src/blocks/useResolvedImages.ts +81 -0
  159. package/src/index.tsx +7 -0
  160. package/src/isolated/HtmlFrame.isolated.tsx +54 -0
  161. package/src/lazyBoundaries/heavyOptionalExports.test.tsx +2 -0
  162. package/src/lazyBoundaries/heavyOptionalExports.tsx +14 -0
@@ -0,0 +1,607 @@
1
+ import {describe, expect, it, mock} from "bun:test";
2
+ import {readdirSync, readFileSync} from "node:fs";
3
+ import {join} from "node:path";
4
+ import {act, fireEvent, waitFor} from "@testing-library/react-native";
5
+ import {ActivityIndicator, Image as NativeImage} from "react-native";
6
+
7
+ import {sharedResponsiveBreakpointStore} from "../ResponsiveBreakpoint";
8
+ import {renderWithTheme} from "../test-utils";
9
+ import {BlocksView} from "./BlocksView";
10
+ import {renderBlock} from "./blockRenderers";
11
+
12
+ const ACTIONS = `v: 1
13
+ datasets:
14
+ signups:
15
+ columns:
16
+ - name: month
17
+ type: string
18
+ - name: count
19
+ type: number
20
+ rows:
21
+ - [Jan, 120]
22
+ signups_weekly:
23
+ columns:
24
+ - name: month
25
+ type: string
26
+ - name: count
27
+ type: number
28
+ rows:
29
+ - [W1, 40]
30
+ blocks:
31
+ - type: chart
32
+ id: signups_chart
33
+ kind: bar
34
+ data: signups
35
+ x: month
36
+ y: count
37
+ - type: actions
38
+ id: chart_actions
39
+ elements:
40
+ - type: segmented
41
+ id: grain
42
+ target: signups_chart
43
+ options:
44
+ - label: Month
45
+ data: signups
46
+ - label: Week
47
+ data: signups_weekly
48
+ - type: button
49
+ id: reply_btn
50
+ text: Reply
51
+ action:
52
+ kind: reply
53
+ text: Thanks
54
+ - type: button
55
+ id: open_btn
56
+ text: Open
57
+ action:
58
+ kind: open
59
+ url: https://example.com
60
+ - type: button
61
+ id: run_btn
62
+ text: Run
63
+ action:
64
+ kind: callback
65
+ name: export_csv
66
+ `;
67
+
68
+ const LAYOUT = `v: 1
69
+ blocks:
70
+ - type: heading
71
+ size: lg
72
+ text: Signups this quarter
73
+ - type: text
74
+ markdown: Signups grew 12 percent.
75
+ - type: columns
76
+ children:
77
+ - type: metric
78
+ label: Total
79
+ value: "403"
80
+ delta: "+12%"
81
+ trend: up
82
+ - type: metric
83
+ label: Best month
84
+ value: Feb
85
+ - type: card
86
+ title: Notes
87
+ children:
88
+ - type: badge
89
+ status: info
90
+ text: Draft
91
+ - type: context
92
+ text: February was the strongest month.
93
+ - type: divider
94
+ `;
95
+
96
+ // Button presses await a haptic call before running onClick.
97
+ const press = async (element: Parameters<typeof fireEvent.press>[0]): Promise<void> => {
98
+ await act(async () => {
99
+ fireEvent.press(element);
100
+ await new Promise((resolve) => setTimeout(resolve, 0));
101
+ });
102
+ };
103
+
104
+ describe("BlocksView", () => {
105
+ it("renders leaf and layout blocks with Terreno components", async () => {
106
+ const {getByText, getByTestId} = renderWithTheme(<BlocksView document={LAYOUT} />);
107
+ expect(getByText("Signups this quarter")).toBeTruthy();
108
+ expect(getByText("Total")).toBeTruthy();
109
+ expect(getByText("403")).toBeTruthy();
110
+ expect(getByText("▲ +12%")).toBeTruthy();
111
+ expect(getByText("Draft")).toBeTruthy();
112
+ expect(getByText("February was the strongest month.")).toBeTruthy();
113
+ expect(getByText("Notes")).toBeTruthy();
114
+ expect(getByTestId("blocks-3-2")).toBeTruthy();
115
+ await waitFor(() => {
116
+ expect(getByText("Signups grew 12 percent.")).toBeTruthy();
117
+ });
118
+ });
119
+
120
+ it("stacks columns on sm and lays them out in a row from md", () => {
121
+ const {getByTestId} = renderWithTheme(<BlocksView document={LAYOUT} />);
122
+ act((): void => {
123
+ sharedResponsiveBreakpointStore.updateWidth(320);
124
+ });
125
+ expect(getByTestId("blocks-2").props.style.flexDirection).toBe("column");
126
+ act((): void => {
127
+ sharedResponsiveBreakpointStore.updateWidth(375);
128
+ });
129
+ expect(getByTestId("blocks-2").props.style.flexDirection).toBe("row");
130
+ });
131
+
132
+ it("shows a banner and collapsed raw YAML for an invalid document", () => {
133
+ const raw = "v: 1\nblocks:\n - type: heading\n color: red\n text: Hi\n";
134
+ const {getByText, queryByText, getByTestId} = renderWithTheme(
135
+ <BlocksView document={raw} testID="blocks" />
136
+ );
137
+ expect(getByText(/is not a field of blocks\[0\]/)).toBeTruthy();
138
+ expect(queryByText(raw)).toBeNull();
139
+ fireEvent.press(getByTestId("blocks-raw.toggle"));
140
+ expect(getByText(raw)).toBeTruthy();
141
+ });
142
+
143
+ it("renders a non-document as one text block", async () => {
144
+ const {getByText} = renderWithTheme(<BlocksView document="Hello from the model" />);
145
+ await waitFor(() => {
146
+ expect(getByText("Hello from the model")).toBeTruthy();
147
+ });
148
+ });
149
+
150
+ it("renders a bar chart and a table from an inline dataset, and a donut from points", async () => {
151
+ const document = `v: 1
152
+ datasets:
153
+ signups:
154
+ columns:
155
+ - name: month
156
+ type: string
157
+ - name: count
158
+ type: number
159
+ rows:
160
+ - [Jan, 120]
161
+ - [Feb, 180]
162
+ blocks:
163
+ - type: chart
164
+ kind: bar
165
+ title: Signups by month
166
+ data: signups
167
+ x: month
168
+ y: count
169
+ - type: chart
170
+ kind: donut
171
+ points:
172
+ - label: Web
173
+ value: 10
174
+ - label: Mobile
175
+ value: 4
176
+ - type: table
177
+ data: signups
178
+ columns: [month, count]
179
+ `;
180
+ const {getAllByText, getByText} = renderWithTheme(<BlocksView document={document} />);
181
+ await waitFor(() => {
182
+ expect(getAllByText("Jan").length).toBeGreaterThan(0);
183
+ expect(getByText("Web")).toBeTruthy();
184
+ expect(getAllByText("180").length).toBeGreaterThan(0);
185
+ });
186
+ });
187
+
188
+ it("renders every column when a table does not list them", async () => {
189
+ const document = `v: 1
190
+ datasets:
191
+ signups:
192
+ columns:
193
+ - name: month
194
+ type: string
195
+ rows:
196
+ - [Jan]
197
+ blocks:
198
+ - type: table
199
+ data: signups
200
+ `;
201
+ const {getByText} = renderWithTheme(<BlocksView document={document} />);
202
+ expect(getByText("month")).toBeTruthy();
203
+ expect(getByText("Jan")).toBeTruthy();
204
+ });
205
+
206
+ it("uses the default selection setter when a select button has no host context", async () => {
207
+ const view = renderBlock(
208
+ {
209
+ elements: [
210
+ {
211
+ action: {data: "weekly", kind: "select", target: "signups_chart"},
212
+ id: "weekly",
213
+ text: "Weekly",
214
+ type: "button",
215
+ },
216
+ ],
217
+ id: "row",
218
+ type: "actions",
219
+ },
220
+ "blocks-0"
221
+ );
222
+ const {getByText} = renderWithTheme(view);
223
+ await press(getByText("Weekly"));
224
+ });
225
+
226
+ it("highlights the segment that matches the chart and switches a table", async () => {
227
+ const document = `v: 1
228
+ datasets:
229
+ signups:
230
+ columns:
231
+ - name: month
232
+ type: string
233
+ - name: count
234
+ type: number
235
+ rows:
236
+ - [Jan, 12]
237
+ weekly:
238
+ columns:
239
+ - name: month
240
+ type: string
241
+ - name: count
242
+ type: number
243
+ rows:
244
+ - [W1, 4]
245
+ blocks:
246
+ - type: chart
247
+ id: signups_chart
248
+ kind: bar
249
+ data: weekly
250
+ x: month
251
+ y: count
252
+ - type: table
253
+ id: signups_table
254
+ data: signups
255
+ - type: actions
256
+ id: row
257
+ elements:
258
+ - type: segmented
259
+ id: grain
260
+ target: signups_chart
261
+ options:
262
+ - label: Month
263
+ data: signups
264
+ - label: Week
265
+ data: weekly
266
+ - type: button
267
+ id: show_week
268
+ text: Show week
269
+ action:
270
+ kind: select
271
+ target: signups_table
272
+ data: weekly
273
+ `;
274
+ const {getAllByText, getByText, queryByText} = renderWithTheme(
275
+ <BlocksView document={document} />
276
+ );
277
+ const segmentStyle = (label: string): {backgroundColor?: string} | undefined => {
278
+ let node = getByText(label).parent;
279
+ for (let depth = 0; depth < 4 && node; depth += 1) {
280
+ const style = node.props?.style as {backgroundColor?: string} | undefined;
281
+ if (style && "backgroundColor" in style) {
282
+ return style;
283
+ }
284
+ node = node.parent;
285
+ }
286
+ return undefined;
287
+ };
288
+ expect(segmentStyle("Week")?.backgroundColor).toBeTruthy();
289
+ expect(segmentStyle("Month")?.backgroundColor).toBeUndefined();
290
+ expect(getByText("Jan")).toBeTruthy();
291
+ await press(getByText("Show week"));
292
+ expect(queryByText("Jan")).toBeNull();
293
+ expect(getAllByText("W1").length).toBe(2);
294
+ });
295
+
296
+ it("clears a ref spinner when the host fetch fails", async () => {
297
+ const document = `v: 1
298
+ datasets:
299
+ signups:
300
+ source: ref
301
+ id: ds_signups
302
+ blocks:
303
+ - type: chart
304
+ kind: bar
305
+ data: signups
306
+ x: month
307
+ y: count
308
+ `;
309
+ const resolveDataset = mock(async () => {
310
+ throw new Error("offline");
311
+ });
312
+ const {UNSAFE_queryAllByType} = renderWithTheme(
313
+ <BlocksView document={document} resolveDataset={resolveDataset} />
314
+ );
315
+ await waitFor(() => {
316
+ expect(UNSAFE_queryAllByType(ActivityIndicator)).toHaveLength(0);
317
+ });
318
+ });
319
+
320
+ it("keeps a file ref off Image until resolveImage returns a url", async () => {
321
+ const document = `v: 1
322
+ blocks:
323
+ - type: image
324
+ alt: Receipt
325
+ src: file:6710c2a4f1
326
+ `;
327
+ const unresolved = renderWithTheme(<BlocksView document={document} />);
328
+ expect(unresolved.getByText("Receipt")).toBeTruthy();
329
+ expect(unresolved.UNSAFE_queryAllByType(NativeImage)).toHaveLength(0);
330
+ unresolved.unmount();
331
+
332
+ const resolveImage = mock(async () => "https://cdn.example/receipt.png");
333
+ const loaded = renderWithTheme(<BlocksView document={document} resolveImage={resolveImage} />);
334
+ await waitFor(() => {
335
+ expect(loaded.UNSAFE_getAllByType(NativeImage)[0]?.props.source.uri).toBe(
336
+ "https://cdn.example/receipt.png"
337
+ );
338
+ });
339
+ expect(resolveImage).toHaveBeenCalledWith("6710c2a4f1");
340
+ });
341
+
342
+ it("keeps the alt text when resolveImage fails", async () => {
343
+ const document = `v: 1
344
+ blocks:
345
+ - type: card
346
+ children:
347
+ - type: image
348
+ alt: Receipt
349
+ src: file:6710c2a4f1
350
+ `;
351
+ const resolveImage = mock(async () => {
352
+ throw new Error("offline");
353
+ });
354
+ const view = renderWithTheme(<BlocksView document={document} resolveImage={resolveImage} />);
355
+ await waitFor(() => {
356
+ expect(resolveImage).toHaveBeenCalledWith("6710c2a4f1");
357
+ });
358
+ expect(view.getByText("Receipt")).toBeTruthy();
359
+ expect(view.UNSAFE_queryAllByType(NativeImage)).toHaveLength(0);
360
+ });
361
+
362
+ it("drops a file url that arrives after the view unmounts", async () => {
363
+ const document = `v: 1
364
+ blocks:
365
+ - type: image
366
+ alt: Receipt
367
+ src: file:6710c2a4f1
368
+ `;
369
+ let finish: (url: string) => void = () => {};
370
+ const resolveImage = mock(
371
+ () =>
372
+ new Promise<string>((resolve) => {
373
+ finish = resolve;
374
+ })
375
+ );
376
+ const view = renderWithTheme(<BlocksView document={document} resolveImage={resolveImage} />);
377
+ view.unmount();
378
+ finish("https://cdn.example/late.png");
379
+ await new Promise((resolve) => {
380
+ setTimeout(resolve, 20);
381
+ });
382
+ });
383
+
384
+ it("loads a ref dataset through resolveDataset", async () => {
385
+ const document = `v: 1
386
+ datasets:
387
+ signups:
388
+ source: ref
389
+ id: ds_signups
390
+ blocks:
391
+ - type: chart
392
+ kind: bar
393
+ data: signups
394
+ x: month
395
+ y: count
396
+ `;
397
+ const resolveDataset = mock(async () => ({
398
+ columns: [
399
+ {name: "month", type: "string" as const},
400
+ {name: "count", type: "number" as const},
401
+ ],
402
+ rows: [["Mar", 9]],
403
+ }));
404
+ const {getByText} = renderWithTheme(
405
+ <BlocksView document={document} resolveDataset={resolveDataset} />
406
+ );
407
+ await waitFor(() => {
408
+ expect(getByText("Mar")).toBeTruthy();
409
+ });
410
+ expect(resolveDataset).toHaveBeenCalledWith({id: "ds_signups"});
411
+ });
412
+
413
+ it("switches the chart dataset from a segmented control and reports select", async () => {
414
+ const onAction = mock(() => undefined);
415
+ const {getByText, queryByText} = renderWithTheme(
416
+ <BlocksView document={ACTIONS} onAction={onAction} />
417
+ );
418
+ await waitFor(() => {
419
+ expect(getByText("Jan")).toBeTruthy();
420
+ });
421
+ fireEvent.press(getByText("Week"));
422
+ await waitFor(() => {
423
+ expect(getByText("W1")).toBeTruthy();
424
+ });
425
+ expect(queryByText("Jan")).toBeNull();
426
+ expect(onAction).toHaveBeenCalledWith({
427
+ action: {data: "signups_weekly", kind: "select", target: "signups_chart"},
428
+ blockId: "chart_actions",
429
+ elementId: "grain",
430
+ });
431
+ });
432
+
433
+ it("reports reply and open actions", async () => {
434
+ const onAction = mock(() => undefined);
435
+ const {getByText} = renderWithTheme(<BlocksView document={ACTIONS} onAction={onAction} />);
436
+ await press(getByText("Reply"));
437
+ await press(getByText("Open"));
438
+ expect(onAction).toHaveBeenNthCalledWith(1, {
439
+ action: {kind: "reply", text: "Thanks"},
440
+ blockId: "chart_actions",
441
+ elementId: "reply_btn",
442
+ });
443
+ expect(onAction).toHaveBeenNthCalledWith(2, {
444
+ action: {kind: "open", url: "https://example.com"},
445
+ blockId: "chart_actions",
446
+ elementId: "open_btn",
447
+ });
448
+ });
449
+
450
+ it("disables a callback the host did not register", async () => {
451
+ const onAction = mock(() => undefined);
452
+ const {getByTestId, getByText} = renderWithTheme(
453
+ <BlocksView document={ACTIONS} hostActions={["approve"]} onAction={onAction} />
454
+ );
455
+ expect(getByTestId("blocks-1-run_btn").props.accessibilityState.disabled).toBe(true);
456
+ await press(getByText("Run"));
457
+ expect(onAction).not.toHaveBeenCalled();
458
+ });
459
+
460
+ it("runs a callback the host registered", async () => {
461
+ const onAction = mock(() => undefined);
462
+ const {getByText} = renderWithTheme(
463
+ <BlocksView document={ACTIONS} hostActions={["export_csv"]} onAction={onAction} />
464
+ );
465
+ await press(getByText("Run"));
466
+ expect(onAction).toHaveBeenCalledWith({
467
+ action: {kind: "callback", name: "export_csv"},
468
+ blockId: "chart_actions",
469
+ elementId: "run_btn",
470
+ });
471
+ });
472
+
473
+ it("shows a loading state for a pending element", () => {
474
+ const {getByTestId, UNSAFE_getAllByType} = renderWithTheme(
475
+ <BlocksView document={ACTIONS} pendingElementIds={["run_btn"]} />
476
+ );
477
+ expect(getByTestId("blocks-1-run_btn").props.accessibilityState.disabled).toBe(true);
478
+ expect(UNSAFE_getAllByType(ActivityIndicator).length).toBeGreaterThan(0);
479
+ });
480
+
481
+ it("replaces a block from overrides", () => {
482
+ const document = `v: 1
483
+ blocks:
484
+ - type: heading
485
+ id: title
486
+ text: Original
487
+ `;
488
+ const {getByText, queryByText} = renderWithTheme(
489
+ <BlocksView
490
+ document={document}
491
+ overrides={{title: {id: "title", text: "Replaced", type: "heading"}}}
492
+ />
493
+ );
494
+ expect(getByText("Replaced")).toBeTruthy();
495
+ expect(queryByText("Original")).toBeNull();
496
+ });
497
+
498
+ it("hides a nested layout error while the reply is still streaming", () => {
499
+ const document = `v: 1
500
+ blocks:
501
+ - type: heading
502
+ text: Plans
503
+ - type: text
504
+ markdown: Compare the options.
505
+ - type: columns
506
+ children:
507
+ - type: card
508
+ title: Team
509
+ children:
510
+ - type: text
511
+ markdown: Twenty dollars
512
+ - type: text
513
+ markdown: Starter is free.
514
+ `;
515
+ const streaming = renderWithTheme(<BlocksView document={document} streaming />);
516
+ expect(
517
+ streaming.queryByText(
518
+ "blocks[2].children[0]: A columns or card block is nested inside another columns or card block."
519
+ )
520
+ ).toBeNull();
521
+
522
+ const finished = renderWithTheme(<BlocksView document={document} />);
523
+ expect(
524
+ finished.getByText(
525
+ "blocks[2].children[0]: A columns or card block is nested inside another columns or card block."
526
+ )
527
+ ).toBeTruthy();
528
+ });
529
+
530
+ it("keeps html as a placeholder until the host allows it and the reply finishes", () => {
531
+ const block: Record<string, string> = {};
532
+ block.type = "html";
533
+ block.title = "Invoice preview";
534
+ block.html = "<h1>Invoice #1042</h1>";
535
+ const document: Record<string, unknown> = {};
536
+ document.v = 1;
537
+ document.blocks = [block];
538
+
539
+ const off = renderWithTheme(<BlocksView document={document} />);
540
+ expect(off.getByText("HTML preview is turned off.")).toBeTruthy();
541
+ expect(off.queryByText("Invoice #1042")).toBeNull();
542
+
543
+ const streaming = renderWithTheme(<BlocksView allowHtml document={document} streaming />);
544
+ expect(streaming.getByText("The preview appears when this reply finishes.")).toBeTruthy();
545
+ expect(streaming.queryByText("Invoice #1042")).toBeNull();
546
+ expect(streaming.queryByTestId("html-frame-native")).toBeNull();
547
+
548
+ const on = renderWithTheme(<BlocksView allowHtml document={document} />);
549
+ expect(on.getByText("Agent-generated preview")).toBeTruthy();
550
+ expect(on.getByText("Invoice preview")).toBeTruthy();
551
+ expect(on.getByTestId("html-frame-native")).toBeTruthy();
552
+ expect(off.queryByTestId("html-frame-native")).toBeNull();
553
+ });
554
+
555
+ it("renders a callout, an image, and a details block", () => {
556
+ const callout: Record<string, string> = {};
557
+ callout.type = "callout";
558
+ callout.status = "warning";
559
+ callout.text = "Seats renew on Friday.";
560
+ const picture: Record<string, string> = {};
561
+ picture.type = "image";
562
+ picture.alt = "Receipt";
563
+ picture.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
564
+ const details: Record<string, string> = {};
565
+ details.type = "details";
566
+ details.title = "Invoice notes";
567
+ details.text = "Twelve seats, billed monthly.";
568
+ const document: Record<string, unknown> = {};
569
+ document.v = 1;
570
+ document.blocks = [callout, picture, details];
571
+
572
+ const {getByText} = renderWithTheme(<BlocksView document={document} />);
573
+
574
+ expect(getByText("Seats renew on Friday.")).toBeTruthy();
575
+ expect(getByText("Receipt")).toBeTruthy();
576
+ expect(getByText("Invoice notes")).toBeTruthy();
577
+ expect(getByText("Twelve seats, billed monthly.")).toBeTruthy();
578
+ });
579
+
580
+ it("rejects an https image whose host is not allowed", () => {
581
+ const block: Record<string, string> = {};
582
+ block.type = "image";
583
+ block.alt = "Pixel";
584
+ block.src = "https://evil.test/pixel.png";
585
+ const document: Record<string, unknown> = {};
586
+ document.v = 1;
587
+ document.blocks = [block];
588
+
589
+ const blocked = renderWithTheme(<BlocksView document={document} />);
590
+ expect(blocked.getByText(/IMAGE_HOST_NOT_ALLOWED|allowed host/)).toBeTruthy();
591
+
592
+ const allowed = renderWithTheme(<BlocksView document={document} imageHosts={["evil.test"]} />);
593
+ expect(allowed.getByText("Pixel")).toBeTruthy();
594
+ });
595
+
596
+ it("does not use raw react-native views in the blocks folder", () => {
597
+ const dir = import.meta.dir;
598
+ const files = readdirSync(dir).filter(
599
+ (name) => name.endsWith(".tsx") && !name.endsWith(".test.tsx")
600
+ );
601
+ expect(files.length).toBeGreaterThan(0);
602
+ for (const name of files) {
603
+ const source = readFileSync(join(dir, name), "utf8");
604
+ expect(source.includes('from "react-native"')).toBe(false);
605
+ }
606
+ });
607
+ });