@malloy-publisher/sdk 0.0.382-dev → 0.2.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 (284) hide show
  1. package/README.md +115 -95
  2. package/TREE_SHAKING.md +16 -11
  3. package/bunfig.toml +7 -0
  4. package/dist/ServerProvider-Cn_LhKda.cjs.js +1 -0
  5. package/dist/ServerProvider-sfabtriY.es.js +4076 -0
  6. package/dist/client/api.d.ts +3773 -1515
  7. package/dist/client/configuration.d.ts +1 -1
  8. package/dist/client/index.cjs.js +1 -1
  9. package/dist/client/index.d.ts +1 -1
  10. package/dist/client/index.es.js +86 -59
  11. package/dist/components/Connections/EditConnectionDialog.d.ts +2 -1
  12. package/dist/components/Connections/common.d.ts +8 -2
  13. package/dist/components/Dashboard/Dashboard.d.ts +62 -0
  14. package/dist/components/Dashboard/DashboardTile.d.ts +57 -0
  15. package/dist/components/Dashboard/index.d.ts +2 -0
  16. package/dist/components/DataAppViewer/DataAppViewer.d.ts +23 -0
  17. package/dist/components/DataAppViewer/index.d.ts +1 -0
  18. package/dist/components/Environment/Environment.d.ts +6 -0
  19. package/dist/components/Environment/index.d.ts +2 -0
  20. package/dist/components/Home/AddEnvironmentDialog.d.ts +1 -0
  21. package/dist/components/Home/DeleteEnvironmentDialog.d.ts +5 -0
  22. package/dist/components/Home/EditEnvironmentDialog.d.ts +7 -0
  23. package/dist/components/Home/Home.d.ts +2 -2
  24. package/dist/components/Materializations/CreateEnvMaterializationDialog.d.ts +14 -0
  25. package/dist/components/Materializations/CreateMaterializationDialog.d.ts +10 -0
  26. package/dist/components/Materializations/DeleteMaterializationDialog.d.ts +7 -0
  27. package/dist/components/Materializations/EnvironmentMaterializations.d.ts +7 -0
  28. package/dist/components/Materializations/ManifestView.d.ts +13 -0
  29. package/dist/components/Materializations/MaterializationDetailDialog.d.ts +9 -0
  30. package/dist/components/Materializations/MaterializationRunsList.d.ts +15 -0
  31. package/dist/components/Materializations/Materializations.d.ts +7 -0
  32. package/dist/components/Materializations/ScheduleCard.d.ts +20 -0
  33. package/dist/components/Materializations/SectionLabel.d.ts +10 -0
  34. package/dist/components/Materializations/SetScheduleDialog.d.ts +11 -0
  35. package/dist/components/Materializations/TriggerChip.d.ts +10 -0
  36. package/dist/components/Materializations/cron.d.ts +23 -0
  37. package/dist/components/Materializations/index.d.ts +2 -0
  38. package/dist/components/Materializations/utils.d.ts +46 -0
  39. package/dist/components/Notebook/Notebook.d.ts +47 -4
  40. package/dist/components/Notebook/NotebookCell.d.ts +37 -1
  41. package/dist/components/Notebook/runPlan.d.ts +29 -0
  42. package/dist/components/Notebook/types.d.ts +10 -0
  43. package/dist/components/Package/ContentTypeIcon.d.ts +31 -0
  44. package/dist/components/Package/Package.d.ts +1 -4
  45. package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
  46. package/dist/components/RenderedResult/ResultContainer.d.ts +5 -1
  47. package/dist/components/RenderedResult/renderLogs.d.ts +6 -0
  48. package/dist/components/ResultsDialog.d.ts +6 -1
  49. package/dist/components/ServerProvider.d.ts +28 -4
  50. package/dist/components/click_helper.d.ts +10 -1
  51. package/dist/components/drill/index.d.ts +3 -0
  52. package/dist/components/drill/markDrillableCells.d.ts +55 -0
  53. package/dist/components/drill/resolveDrill.d.ts +139 -0
  54. package/dist/components/drill/useDrill.d.ts +63 -0
  55. package/dist/components/given/GivenInput.d.ts +52 -0
  56. package/dist/components/given/GivensPanel.d.ts +47 -0
  57. package/dist/components/given/filterValue.d.ts +97 -0
  58. package/dist/components/given/index.d.ts +2 -0
  59. package/dist/components/given/paramCodec.d.ts +60 -0
  60. package/dist/components/given/utils.d.ts +17 -0
  61. package/dist/components/highlighter.d.ts +6 -1
  62. package/dist/components/index.d.ts +7 -2
  63. package/dist/components/styles.d.ts +47 -0
  64. package/dist/constants/docLinks.d.ts +12 -0
  65. package/dist/{core-YNpOLuB1.cjs.js → core-CqRKhtoy.cjs.js} +1 -1
  66. package/dist/{core-CjeTkq8O.es.js → core-DyU47reh.es.js} +1 -1
  67. package/dist/github-dark-BKL9xNgN.cjs.js +1 -0
  68. package/dist/github-dark-DenFmJkN.es.js +4 -0
  69. package/dist/hooks/givenValue.d.ts +10 -0
  70. package/dist/hooks/index.d.ts +4 -0
  71. package/dist/hooks/useDimensionFiltersFromSpec.d.ts +3 -3
  72. package/dist/hooks/useDimensionFiltersQuery.d.ts +3 -3
  73. package/dist/hooks/useDimensionalFilterRangeData.d.ts +4 -4
  74. package/dist/hooks/useGivensState.d.ts +64 -0
  75. package/dist/hooks/useModelGivens.d.ts +10 -0
  76. package/dist/hooks/useSuggestOptions.d.ts +78 -0
  77. package/dist/{index-DRDu9kIV.es.js → index-CAkFE4Ol.es.js} +41278 -28504
  78. package/dist/index-DD4b_bt0.cjs.js +356 -0
  79. package/dist/index.cjs.js +1 -1
  80. package/dist/index.d.ts +4 -1
  81. package/dist/index.es.js +79 -48
  82. package/dist/malloy-explorer.css +12 -1
  83. package/dist/sdk.css +1 -1
  84. package/dist/theme/ThemeContext.d.ts +45 -0
  85. package/dist/theme/buildMalloyExplicitTheme.d.ts +34 -0
  86. package/dist/theme/buildTableCssVars.d.ts +41 -0
  87. package/dist/theme/buildVegaThemeOverride.d.ts +16 -0
  88. package/dist/theme/defaults.d.ts +7 -0
  89. package/dist/theme/index.d.ts +8 -0
  90. package/dist/theme/keys.d.ts +20 -0
  91. package/dist/theme/readChartAnnotations.d.ts +23 -0
  92. package/dist/theme/resolveTheme.d.ts +22 -0
  93. package/dist/theme/types.d.ts +70 -0
  94. package/dist/utils/dataAppEmbed.d.ts +44 -0
  95. package/dist/utils/formatting.d.ts +1 -1
  96. package/dist/utils/malloyString.d.ts +7 -0
  97. package/dist/utils/parsing.d.ts +2 -2
  98. package/dist/utils/queryClient.d.ts +5 -0
  99. package/package.json +19 -8
  100. package/src/client-entry.ts +3 -0
  101. package/src/components/AnalyzePackageButton.tsx +9 -3
  102. package/src/components/ApiErrorDisplay.tsx +3 -0
  103. package/src/components/Connections/AddConnectionDialog.tsx +132 -25
  104. package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
  105. package/src/components/Connections/EditConnectionDialog.tsx +167 -37
  106. package/src/components/Connections/common.ts +94 -3
  107. package/src/components/DOMElement/DOMElement.tsx +3 -0
  108. package/src/components/DOMElement/index.ts +3 -0
  109. package/src/components/Dashboard/Dashboard.spec.tsx +216 -0
  110. package/src/components/Dashboard/Dashboard.tsx +458 -0
  111. package/src/components/Dashboard/DashboardTile.spec.tsx +81 -0
  112. package/src/components/Dashboard/DashboardTile.tsx +199 -0
  113. package/src/components/Dashboard/index.ts +5 -0
  114. package/src/components/Dashboard/tileGridColumn.spec.ts +35 -0
  115. package/src/components/DataAppViewer/DataAppViewer.tsx +214 -0
  116. package/src/components/DataAppViewer/index.ts +4 -0
  117. package/src/components/{Project → Environment}/About.tsx +11 -4
  118. package/src/components/Environment/AddPackageDialog.tsx +202 -0
  119. package/src/components/{Project → Environment}/ConnectionExplorer.tsx +17 -14
  120. package/src/components/Environment/Connections.tsx +391 -0
  121. package/src/components/{Project → Environment}/DeletePackageDialog.tsx +8 -4
  122. package/src/components/{Project → Environment}/EditPackageDialog.tsx +21 -13
  123. package/src/components/Environment/Environment.tsx +89 -0
  124. package/src/components/Environment/Packages.tsx +206 -0
  125. package/src/components/Environment/index.ts +5 -0
  126. package/src/components/Home/{AddProjectDialog.tsx → AddEnvironmentDialog.tsx} +25 -19
  127. package/src/components/Home/{DeleteProjectDialog.tsx → DeleteEnvironmentDialog.tsx} +20 -15
  128. package/src/components/Home/{EditProjectDialog.tsx → EditEnvironmentDialog.tsx} +35 -28
  129. package/src/components/Home/Home.tsx +386 -408
  130. package/src/components/Home/index.ts +3 -0
  131. package/src/components/Loading.tsx +3 -0
  132. package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +179 -0
  133. package/src/components/Materializations/CreateMaterializationDialog.tsx +112 -0
  134. package/src/components/Materializations/DeleteMaterializationDialog.tsx +109 -0
  135. package/src/components/Materializations/EnvironmentMaterializations.tsx +267 -0
  136. package/src/components/Materializations/ManifestView.tsx +87 -0
  137. package/src/components/Materializations/MaterializationDetailDialog.tsx +259 -0
  138. package/src/components/Materializations/MaterializationRunsList.tsx +256 -0
  139. package/src/components/Materializations/Materializations.tsx +355 -0
  140. package/src/components/Materializations/ScheduleCard.tsx +231 -0
  141. package/src/components/Materializations/SectionLabel.tsx +30 -0
  142. package/src/components/Materializations/SetScheduleDialog.tsx +162 -0
  143. package/src/components/Materializations/TriggerChip.tsx +44 -0
  144. package/src/components/Materializations/cron.ts +124 -0
  145. package/src/components/Materializations/index.ts +5 -0
  146. package/src/components/Materializations/utils.ts +145 -0
  147. package/src/components/Model/Model.tsx +44 -2
  148. package/src/components/Model/ModelCell.tsx +14 -5
  149. package/src/components/Model/ModelExplorer.tsx +3 -0
  150. package/src/components/Model/ModelExplorerDialog.tsx +4 -1
  151. package/src/components/Model/SourcesExplorer.tsx +9 -6
  152. package/src/components/Model/index.ts +3 -0
  153. package/src/components/Model/useModelData.ts +6 -3
  154. package/src/components/Notebook/Notebook.spec.tsx +88 -0
  155. package/src/components/Notebook/Notebook.tsx +476 -339
  156. package/src/components/Notebook/NotebookCell.tsx +264 -25
  157. package/src/components/Notebook/index.ts +3 -0
  158. package/src/components/Notebook/runPlan.spec.ts +155 -0
  159. package/src/components/Notebook/runPlan.ts +67 -0
  160. package/src/components/Notebook/types.ts +14 -0
  161. package/src/components/Package/ContentTypeIcon.spec.ts +91 -0
  162. package/src/components/Package/ContentTypeIcon.tsx +177 -0
  163. package/src/components/Package/FileTreeView.tsx +3 -0
  164. package/src/components/Package/Models.tsx +7 -4
  165. package/src/components/Package/Package.spec.tsx +113 -0
  166. package/src/components/Package/Package.tsx +692 -59
  167. package/src/components/Package/index.ts +3 -0
  168. package/src/components/QueryResult/QueryResult.tsx +17 -8
  169. package/src/components/QueryResult/index.ts +3 -0
  170. package/src/components/RenderedResult/RenderedResult.tsx +725 -67
  171. package/src/components/RenderedResult/ResultContainer.tsx +71 -2
  172. package/src/components/RenderedResult/index.ts +3 -0
  173. package/src/components/RenderedResult/renderLogs.spec.ts +58 -0
  174. package/src/components/RenderedResult/renderLogs.ts +43 -0
  175. package/src/components/ResultsDialog.tsx +13 -1
  176. package/src/components/ServerProvider.tsx +175 -51
  177. package/src/components/Workbook/BrowserWorkbookStorage.ts +3 -0
  178. package/src/components/Workbook/EditableMalloyCell.tsx +3 -0
  179. package/src/components/Workbook/ModelPicker.tsx +7 -4
  180. package/src/components/Workbook/MutableCell.tsx +10 -5
  181. package/src/components/Workbook/Workbook.tsx +8 -5
  182. package/src/components/Workbook/WorkbookList.tsx +3 -0
  183. package/src/components/Workbook/WorkbookManager.ts +3 -0
  184. package/src/components/Workbook/WorkbookStorage.ts +3 -0
  185. package/src/components/Workbook/WorkbookStorageProvider.tsx +3 -0
  186. package/src/components/Workbook/index.ts +3 -0
  187. package/src/components/click_helper.ts +17 -1
  188. package/src/components/drill/index.ts +28 -0
  189. package/src/components/drill/markDrillableCells.dom.spec.ts +313 -0
  190. package/src/components/drill/markDrillableCells.spec.ts +109 -0
  191. package/src/components/drill/markDrillableCells.ts +314 -0
  192. package/src/components/drill/resolveDrill.spec.ts +449 -0
  193. package/src/components/drill/resolveDrill.ts +375 -0
  194. package/src/components/drill/useDrill.tsx +203 -0
  195. package/src/components/filter/DimensionFilter.tsx +3 -0
  196. package/src/components/filter/index.ts +3 -0
  197. package/src/components/filter/utils.ts +16 -5
  198. package/src/components/given/GivenInput.spec.tsx +689 -0
  199. package/src/components/given/GivenInput.tsx +801 -0
  200. package/src/components/given/GivensPanel.spec.tsx +175 -0
  201. package/src/components/given/GivensPanel.tsx +195 -0
  202. package/src/components/given/filterValue.spec.ts +364 -0
  203. package/src/components/given/filterValue.ts +207 -0
  204. package/src/components/given/index.ts +9 -0
  205. package/src/components/given/paramCodec.spec.ts +857 -0
  206. package/src/components/given/paramCodec.ts +483 -0
  207. package/src/components/given/utils.spec.ts +60 -0
  208. package/src/components/given/utils.ts +79 -0
  209. package/src/components/highlighter.ts +22 -5
  210. package/src/components/index.ts +10 -2
  211. package/src/components/styles.ts +82 -33
  212. package/src/constants/docLinks.spec.ts +54 -0
  213. package/src/constants/docLinks.ts +46 -0
  214. package/src/hooks/givenValue.ts +13 -0
  215. package/src/hooks/index.ts +13 -0
  216. package/src/hooks/useDimensionFilters.ts +4 -7
  217. package/src/hooks/useDimensionFiltersFromSpec.ts +8 -5
  218. package/src/hooks/useDimensionFiltersQuery.ts +10 -13
  219. package/src/hooks/useDimensionalFilterRangeData.ts +11 -14
  220. package/src/hooks/useGivensState.spec.ts +449 -0
  221. package/src/hooks/useGivensState.ts +340 -0
  222. package/src/hooks/useModelGivens.ts +29 -0
  223. package/src/hooks/useQueryWithApiError.ts +3 -0
  224. package/src/hooks/useRawQueryData.ts +6 -3
  225. package/src/hooks/useSuggestOptions.dom.spec.ts +91 -0
  226. package/src/hooks/useSuggestOptions.spec.ts +112 -0
  227. package/src/hooks/useSuggestOptions.ts +268 -0
  228. package/src/index.ts +10 -1
  229. package/src/react-app-env.d.ts +3 -0
  230. package/src/theme/ThemeContext.tsx +86 -0
  231. package/src/theme/buildMalloyExplicitTheme.ts +61 -0
  232. package/src/theme/buildTableCssVars.spec.ts +86 -0
  233. package/src/theme/buildTableCssVars.ts +98 -0
  234. package/src/theme/buildVegaThemeOverride.spec.ts +56 -0
  235. package/src/theme/buildVegaThemeOverride.ts +47 -0
  236. package/src/theme/defaults.ts +78 -0
  237. package/src/theme/index.ts +11 -0
  238. package/src/theme/keys.ts +32 -0
  239. package/src/theme/readChartAnnotations.spec.ts +58 -0
  240. package/src/theme/readChartAnnotations.ts +69 -0
  241. package/src/theme/resolveTheme.spec.ts +160 -0
  242. package/src/theme/resolveTheme.ts +121 -0
  243. package/src/theme/types.ts +79 -0
  244. package/src/utils/dataAppEmbed.spec.ts +63 -0
  245. package/src/utils/dataAppEmbed.ts +84 -0
  246. package/src/utils/formatting.spec.ts +25 -21
  247. package/src/utils/formatting.ts +11 -7
  248. package/src/utils/malloyString.spec.ts +24 -0
  249. package/src/utils/malloyString.ts +12 -0
  250. package/src/utils/parsing.spec.ts +26 -19
  251. package/src/utils/parsing.ts +11 -6
  252. package/src/utils/queryClient.ts +17 -0
  253. package/test/README.md +185 -0
  254. package/test/serverProvider.tsx +72 -0
  255. package/test/setup.ts +29 -0
  256. package/tsconfig.json +12 -1
  257. package/tsconfig.spec.json +22 -0
  258. package/vite.config.ts +3 -0
  259. package/dist/ServerProvider-lOQXDlqB.cjs.js +0 -1
  260. package/dist/ServerProvider-on-8FH5Z.es.js +0 -3060
  261. package/dist/components/Home/AddProjectDialog.d.ts +0 -1
  262. package/dist/components/Home/DeleteProjectDialog.d.ts +0 -5
  263. package/dist/components/Home/EditProjectDialog.d.ts +0 -7
  264. package/dist/components/Package/Config.d.ts +0 -5
  265. package/dist/components/Package/Databases.d.ts +0 -5
  266. package/dist/components/Package/Notebooks.d.ts +0 -6
  267. package/dist/components/Project/Project.d.ts +0 -6
  268. package/dist/components/Project/index.d.ts +0 -2
  269. package/dist/index-BOLBP6_i.cjs.js +0 -233
  270. package/src/components/Package/Config.tsx +0 -97
  271. package/src/components/Package/Connections.tsx +0 -373
  272. package/src/components/Package/Databases.tsx +0 -228
  273. package/src/components/Package/Notebooks.tsx +0 -77
  274. package/src/components/Project/AddPackageDialog.tsx +0 -159
  275. package/src/components/Project/Packages.tsx +0 -191
  276. package/src/components/Project/Project.tsx +0 -52
  277. package/src/components/Project/index.ts +0 -2
  278. /package/dist/components/{Project → Environment}/About.d.ts +0 -0
  279. /package/dist/components/{Project → Environment}/AddPackageDialog.d.ts +0 -0
  280. /package/dist/components/{Project → Environment}/ConnectionExplorer.d.ts +0 -0
  281. /package/dist/components/{Package → Environment}/Connections.d.ts +0 -0
  282. /package/dist/components/{Project → Environment}/DeletePackageDialog.d.ts +0 -0
  283. /package/dist/components/{Project → Environment}/EditPackageDialog.d.ts +0 -0
  284. /package/dist/components/{Project → Environment}/Packages.d.ts +0 -0
@@ -0,0 +1,449 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { act, renderHook } from "@testing-library/react";
5
+ import { describe, expect, it } from "bun:test";
6
+ import { useGivensState } from "./useGivensState";
7
+
8
+ const TYPES = new Map<string, string | undefined>([
9
+ ["REGION", "filter<string>"],
10
+ ["MIN_AMOUNT", "number"],
11
+ ]);
12
+
13
+ const entries = (values: Map<string, unknown>) => Object.fromEntries(values);
14
+
15
+ /**
16
+ * A host that behaves like `NotebookPage` and `DashboardPage`: it keeps the
17
+ * applied values in its URL and feeds that URL straight back in as `params`.
18
+ *
19
+ * Testing the hook without that loop misses most of what it does. Every Reset
20
+ * defect below is invisible to a test that treats `params` as a constant,
21
+ * because the second half of each one happens when the values the hook just
22
+ * reported arrive back through the front door.
23
+ */
24
+ function renderWithUrlHost(options: {
25
+ autorun: boolean;
26
+ startingValues?: Record<string, string>;
27
+ initialUrl?: Record<string, string>;
28
+ }) {
29
+ let url: Record<string, string> = options.initialUrl ?? {};
30
+ const urlWrites: Record<string, string>[] = [];
31
+ // Mounted with no starting values, then given them a render later. That is
32
+ // how it really happens: they arrive with the notebook, after the first
33
+ // render, and the hook's report guard is seeded for exactly that order.
34
+ // Handing them over at mount instead makes the first render look like the
35
+ // steady state and hides whether they ever reach the URL.
36
+ const arrival: { starting?: Record<string, string> } = {};
37
+ const view = renderHook(
38
+ ({
39
+ p,
40
+ s,
41
+ }: {
42
+ p: Record<string, string>;
43
+ s: Record<string, string> | undefined;
44
+ }) =>
45
+ useGivensState({
46
+ declaredTypes: TYPES,
47
+ startingValues: s,
48
+ params: p,
49
+ onParamsChange: (next) => {
50
+ url = next;
51
+ urlWrites.push(next);
52
+ },
53
+ autorun: options.autorun,
54
+ }),
55
+ { initialProps: { p: url, s: arrival.starting } },
56
+ );
57
+ // Let the reported values propagate back through the host, repeatedly, so a
58
+ // change that keeps provoking another URL write shows up as more writes
59
+ // rather than as a stable-looking end state.
60
+ const settle = () => {
61
+ for (let i = 0; i < 5; i++)
62
+ act(() => view.rerender({ p: url, s: arrival.starting }));
63
+ };
64
+ settle();
65
+ arrival.starting = options.startingValues;
66
+ settle();
67
+ return {
68
+ get current() {
69
+ return view.result.current;
70
+ },
71
+ settle,
72
+ urlWrites,
73
+ get url() {
74
+ return url;
75
+ },
76
+ };
77
+ }
78
+
79
+ describe("useGivensState: where the controls start", () => {
80
+ it("takes the document's starting values when the URL is empty", () => {
81
+ const host = renderWithUrlHost({
82
+ autorun: true,
83
+ startingValues: { REGION: "West" },
84
+ });
85
+ expect(entries(host.current.applied)).toEqual({ REGION: "West" });
86
+ });
87
+
88
+ it("lets the URL win, so a shared link shows what the sender saw", () => {
89
+ const host = renderWithUrlHost({
90
+ autorun: true,
91
+ startingValues: { REGION: "West" },
92
+ initialUrl: { REGION: "Northeast" },
93
+ });
94
+ expect(entries(host.current.applied)).toEqual({ REGION: "Northeast" });
95
+ });
96
+
97
+ it("reads each value as its declared type", () => {
98
+ const host = renderWithUrlHost({
99
+ autorun: true,
100
+ initialUrl: { REGION: "West", MIN_AMOUNT: "100" },
101
+ });
102
+ expect(host.current.applied.get("REGION")).toBe("West");
103
+ expect(host.current.applied.get("MIN_AMOUNT")).toBe(100);
104
+ });
105
+
106
+ it("ignores a parameter the model does not declare", () => {
107
+ // A tracking tag on the page URL must not reach a query, which would
108
+ // fail outright on an unknown given.
109
+ const host = renderWithUrlHost({
110
+ autorun: true,
111
+ initialUrl: { REGION: "West", utm_source: "slack" },
112
+ });
113
+ expect(entries(host.current.applied)).toEqual({ REGION: "West" });
114
+ });
115
+
116
+ it("reports the starting values outward once, and then stops", () => {
117
+ const host = renderWithUrlHost({
118
+ autorun: true,
119
+ startingValues: { REGION: "West" },
120
+ });
121
+ expect(host.urlWrites).toEqual([{ REGION: "West" }]);
122
+ });
123
+ });
124
+
125
+ describe("useGivensState: editing", () => {
126
+ it("applies immediately under autorun", () => {
127
+ const host = renderWithUrlHost({ autorun: true });
128
+ act(() => host.current.setGiven("REGION", "Northeast"));
129
+ host.settle();
130
+
131
+ expect(entries(host.current.applied)).toEqual({ REGION: "Northeast" });
132
+ expect(host.current.pending).toBe(false);
133
+ expect(host.url).toEqual({ REGION: "Northeast" });
134
+ });
135
+
136
+ it("batches behind Apply when autorun is off", () => {
137
+ const host = renderWithUrlHost({ autorun: false });
138
+ act(() => host.current.setGiven("REGION", "Northeast"));
139
+ act(() => host.current.setGiven("MIN_AMOUNT", 100));
140
+ host.settle();
141
+
142
+ expect(entries(host.current.draft)).toEqual({
143
+ REGION: "Northeast",
144
+ MIN_AMOUNT: 100,
145
+ });
146
+ expect(entries(host.current.applied)).toEqual({});
147
+ expect(host.current.pending).toBe(true);
148
+ // Two edits, and the URL has not moved: nothing has been run yet.
149
+ expect(host.urlWrites).toEqual([]);
150
+
151
+ act(() => host.current.apply());
152
+ host.settle();
153
+
154
+ expect(entries(host.current.applied)).toEqual({
155
+ REGION: "Northeast",
156
+ MIN_AMOUNT: 100,
157
+ });
158
+ expect(host.current.pending).toBe(false);
159
+ // One write for two edits, which is the whole point of batching.
160
+ expect(host.urlWrites).toEqual([
161
+ { REGION: "Northeast", MIN_AMOUNT: "100" },
162
+ ]);
163
+ });
164
+
165
+ it("keeps the other starting values when one control is edited", () => {
166
+ const host = renderWithUrlHost({
167
+ autorun: true,
168
+ startingValues: { REGION: "West", MIN_AMOUNT: "5" },
169
+ });
170
+ act(() => host.current.setGiven("REGION", "Northeast"));
171
+ host.settle();
172
+
173
+ expect(entries(host.current.applied)).toEqual({
174
+ REGION: "Northeast",
175
+ MIN_AMOUNT: 5,
176
+ });
177
+ });
178
+
179
+ it("drops an override on null, back to the model default", () => {
180
+ const host = renderWithUrlHost({
181
+ autorun: true,
182
+ initialUrl: { REGION: "West" },
183
+ });
184
+ act(() => host.current.setGiven("REGION", null));
185
+ host.settle();
186
+
187
+ expect(host.current.applied.has("REGION")).toBe(false);
188
+ expect(host.url).toEqual({});
189
+ });
190
+
191
+ it("drops values for givens the model no longer declares", () => {
192
+ // What a package reload can do while the page is open. Sending a given
193
+ // the model no longer declares fails the query outright.
194
+ let url: Record<string, string> = { REGION: "West", MIN_AMOUNT: "100" };
195
+ const { result, rerender } = renderHook(
196
+ ({ types }: { types: Map<string, string | undefined> }) =>
197
+ useGivensState({
198
+ declaredTypes: types,
199
+ params: url,
200
+ onParamsChange: (next) => {
201
+ url = next;
202
+ },
203
+ autorun: true,
204
+ }),
205
+ { initialProps: { types: TYPES } },
206
+ );
207
+ expect(result.current.applied.size).toBe(2);
208
+
209
+ const reloaded = new Map([["REGION", "filter<string>"]]);
210
+ act(() => rerender({ types: reloaded }));
211
+
212
+ expect(entries(result.current.applied)).toEqual({ REGION: "West" });
213
+ });
214
+ });
215
+
216
+ describe("useGivensState: Reset", () => {
217
+ // Two symptoms were reported against #935's `clearAll`, and they turned out
218
+ // to be one defect: it cleared to an EMPTY map rather than to the starting
219
+ // point, and it only committed under autorun. Each test below fails against
220
+ // that version, in the way named in its comment.
221
+
222
+ it("puts the document's starting values back, and runs once", () => {
223
+ // Was: cleared to empty, the empty state went out to the URL, the
224
+ // starting values then flowed back in from `initial`, and the queries
225
+ // ran a second time. Two URL writes where there should be one.
226
+ const host = renderWithUrlHost({
227
+ autorun: true,
228
+ startingValues: { REGION: "West" },
229
+ });
230
+ act(() => host.current.setGiven("REGION", "Northeast"));
231
+ host.settle();
232
+ const writesBefore = host.urlWrites.length;
233
+
234
+ act(() => host.current.reset());
235
+ host.settle();
236
+
237
+ expect(entries(host.current.applied)).toEqual({ REGION: "West" });
238
+ expect(host.url).toEqual({ REGION: "West" });
239
+ expect(host.urlWrites.length - writesBefore).toBe(1);
240
+ });
241
+
242
+ it("takes effect with autorun off, instead of doing nothing at all", () => {
243
+ // Was: `applied` was left untouched whenever autorun was false, so the
244
+ // URL never changed and no query re-ran. Reset was inert.
245
+ const host = renderWithUrlHost({ autorun: false });
246
+ act(() => host.current.setGiven("REGION", "Northeast"));
247
+ act(() => host.current.apply());
248
+ host.settle();
249
+ expect(host.url).toEqual({ REGION: "Northeast" });
250
+ const writesBefore = host.urlWrites.length;
251
+
252
+ act(() => host.current.reset());
253
+ host.settle();
254
+
255
+ expect(entries(host.current.applied)).toEqual({});
256
+ expect(host.url).toEqual({});
257
+ expect(host.urlWrites.length - writesBefore).toBe(1);
258
+ });
259
+
260
+ it("leaves nothing pending, so Apply does not light up after a Reset", () => {
261
+ const host = renderWithUrlHost({
262
+ autorun: false,
263
+ startingValues: { REGION: "West" },
264
+ });
265
+ act(() => host.current.setGiven("REGION", "Northeast"));
266
+ host.settle();
267
+ expect(host.current.pending).toBe(true);
268
+
269
+ act(() => host.current.reset());
270
+ host.settle();
271
+
272
+ expect(host.current.pending).toBe(false);
273
+ expect(entries(host.current.draft)).toEqual({ REGION: "West" });
274
+ });
275
+
276
+ it("clears a URL-carried value rather than treating it as the start", () => {
277
+ // The URL is the thing being reset away from, so a link's values are not
278
+ // the starting point even though they win on arrival.
279
+ const host = renderWithUrlHost({
280
+ autorun: true,
281
+ startingValues: { REGION: "West" },
282
+ initialUrl: { REGION: "Northeast" },
283
+ });
284
+ expect(entries(host.current.applied)).toEqual({ REGION: "Northeast" });
285
+
286
+ act(() => host.current.reset());
287
+ host.settle();
288
+
289
+ expect(entries(host.current.applied)).toEqual({ REGION: "West" });
290
+ });
291
+ });
292
+
293
+ describe("useGivensState: settling", () => {
294
+ it("stops reporting once the host echoes the values back", () => {
295
+ // The hook reports applied values out for the host to put in the URL,
296
+ // and the host feeds that URL back in. Without the `sameParams` guard
297
+ // that is a loop.
298
+ const host = renderWithUrlHost({
299
+ autorun: true,
300
+ initialUrl: { REGION: "West" },
301
+ });
302
+ host.settle();
303
+ host.settle();
304
+ expect(host.urlWrites).toEqual([]);
305
+ });
306
+
307
+ it("settles when its inputs are fresh objects on every render", () => {
308
+ // Carried over from `useGivensForm.spec.ts`, which this hook replaces.
309
+ // That hook infinite-looped on an unmemoized argument: its effect
310
+ // returned a fresh Map every run, which changed state identity, which
311
+ // re-rendered, until React gave up with "Maximum update depth exceeded"
312
+ // after 645k warnings. This is a published SDK export, so a consumer can
313
+ // reasonably pass an object literal.
314
+ let renders = 0;
315
+ const { result } = renderHook(() => {
316
+ renders++;
317
+ return useGivensState({
318
+ declaredTypes: new Map([["REGION", "filter<string>"]]),
319
+ startingValues: { REGION: "West" },
320
+ params: {},
321
+ onParamsChange: () => {},
322
+ autorun: true,
323
+ });
324
+ });
325
+
326
+ expect(entries(result.current.applied)).toEqual({ REGION: "West" });
327
+ expect(renders).toBeLessThan(5);
328
+ });
329
+ });
330
+
331
+ describe("useGivensState: swapping documents", () => {
332
+ it("drops the previous notebook's applied values on navigation", () => {
333
+ // Navigating between notebooks reuses this hook's component, and the
334
+ // edits used to be keyed by their starting VALUES alone. Two notebooks
335
+ // that both start empty produced the same key, so the first one's
336
+ // applied values stayed active and filtered the second, while
337
+ // `lastReported` (already equal to them) suppressed the report that
338
+ // would have shown them in the URL.
339
+ const reported: Record<string, string>[] = [];
340
+ const { result, rerender } = renderHook(
341
+ (props: { doc: string; params: Record<string, string> }) =>
342
+ useGivensState({
343
+ declaredTypes: new Map([["REGION", "string"]]),
344
+ params: props.params,
345
+ documentKey: props.doc,
346
+ onParamsChange: (next) => reported.push(next),
347
+ autorun: true,
348
+ }),
349
+ { initialProps: { doc: "a.malloynb", params: {} } },
350
+ );
351
+
352
+ act(() => result.current.setGiven("REGION", "West"));
353
+ expect(entries(result.current.applied)).toEqual({ REGION: "West" });
354
+
355
+ // The host writes it to the URL and feeds it straight back in.
356
+ rerender({ doc: "a.malloynb", params: { REGION: "West" } });
357
+ expect(entries(result.current.applied)).toEqual({ REGION: "West" });
358
+
359
+ rerender({ doc: "b.malloynb", params: {} });
360
+ expect(entries(result.current.applied)).toEqual({});
361
+ expect(reported.at(-1)).toEqual({});
362
+ });
363
+
364
+ it("keeps edits to the givens a model reload did not drop", () => {
365
+ // `prune` exists to drop values for givens that no longer exist while
366
+ // keeping the rest. It never got the chance: `initialKey` was built from
367
+ // `initial`, which is already filtered by `declaredTypes`, so removing
368
+ // one given re-keyed everything and discarded the reader's edits to the
369
+ // givens that survived. A declaration change is not a new starting point.
370
+ const { result, rerender } = renderHook(
371
+ (props: { types: Map<string, string> }) =>
372
+ useGivensState({
373
+ declaredTypes: props.types,
374
+ startingValues: { STATUS: "open" },
375
+ documentKey: "orders.malloynb",
376
+ autorun: true,
377
+ }),
378
+ {
379
+ initialProps: {
380
+ types: new Map([
381
+ ["REGION", "string"],
382
+ ["STATUS", "string"],
383
+ ]),
384
+ },
385
+ },
386
+ );
387
+
388
+ act(() => result.current.setGiven("REGION", "east"));
389
+ expect(entries(result.current.applied)).toEqual({
390
+ STATUS: "open",
391
+ REGION: "east",
392
+ });
393
+
394
+ // The model author removes STATUS and the package reloads.
395
+ rerender({ types: new Map([["REGION", "string"]]) });
396
+ expect(entries(result.current.applied)).toEqual({ REGION: "east" });
397
+ });
398
+
399
+ it("is not re-keyed by the ORDER a host lists its parameters in", () => {
400
+ // `URLSearchParams` iterates in the query string's own order, so a host
401
+ // rewriting `?A=1&B=2` as `?B=2&A=1` would otherwise look like a new
402
+ // starting point and throw the reader's edits away for nothing.
403
+ const { result, rerender } = renderHook(
404
+ (props: { params: Record<string, string> }) =>
405
+ useGivensState({
406
+ declaredTypes: new Map([
407
+ ["A", "string"],
408
+ ["B", "string"],
409
+ ]),
410
+ params: props.params,
411
+ documentKey: "orders.malloynb",
412
+ autorun: true,
413
+ }),
414
+ { initialProps: { params: { A: "1", B: "2" } } },
415
+ );
416
+
417
+ act(() => result.current.setGiven("A", "edited"));
418
+ rerender({ params: { B: "2", A: "1" } });
419
+ expect(entries(result.current.applied)).toEqual({ A: "edited", B: "2" });
420
+ });
421
+
422
+ it("does not resurrect an abandoned edit when the key returns", () => {
423
+ // The edits were only IGNORED on a key mismatch, never discarded, and the
424
+ // key space is small enough to revisit: notebook a -> b -> a from a clean
425
+ // link matched the abandoned edits again and re-applied a filter the URL
426
+ // did not carry, to a reader who had just opened it fresh. Browser Back
427
+ // and a drill pushing an earlier URL do the same.
428
+ const { result, rerender } = renderHook(
429
+ (props: { doc: string; params: Record<string, string> }) =>
430
+ useGivensState({
431
+ declaredTypes: new Map([["REGION", "string"]]),
432
+ params: props.params,
433
+ documentKey: props.doc,
434
+ autorun: true,
435
+ }),
436
+ { initialProps: { doc: "a.malloynb", params: {} } },
437
+ );
438
+
439
+ act(() => result.current.setGiven("REGION", "East"));
440
+ // The host echoes it into the URL, then the reader navigates away.
441
+ rerender({ doc: "a.malloynb", params: { REGION: "East" } });
442
+ rerender({ doc: "b.malloynb", params: {} });
443
+ expect(entries(result.current.applied)).toEqual({});
444
+
445
+ // Back to the first notebook from a link carrying no query string.
446
+ rerender({ doc: "a.malloynb", params: {} });
447
+ expect(entries(result.current.applied)).toEqual({});
448
+ });
449
+ });