@malloy-publisher/sdk 0.0.382-dev → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) 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-CTopDUWW.cjs.js +1 -0
  5. package/dist/ServerProvider-DUFPY6Rv.es.js +4072 -0
  6. package/dist/client/api.d.ts +3718 -1507
  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 +85 -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 +49 -0
  14. package/dist/components/Dashboard/DashboardTile.d.ts +48 -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-hLahnv0W.cjs.js} +1 -1
  66. package/dist/{core-CjeTkq8O.es.js → core-lb28vQkR.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-DvEanVgc.cjs.js +339 -0
  78. package/dist/{index-DRDu9kIV.es.js → index-txw04ZbN.es.js} +40531 -27964
  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 +25 -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 +30 -2
  104. package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
  105. package/src/components/Connections/EditConnectionDialog.tsx +33 -4
  106. package/src/components/Connections/common.ts +64 -1
  107. package/src/components/DOMElement/DOMElement.tsx +3 -0
  108. package/src/components/DOMElement/index.ts +3 -0
  109. package/src/components/Dashboard/Dashboard.tsx +398 -0
  110. package/src/components/Dashboard/DashboardTile.tsx +153 -0
  111. package/src/components/Dashboard/index.ts +5 -0
  112. package/src/components/DataAppViewer/DataAppViewer.tsx +214 -0
  113. package/src/components/DataAppViewer/index.ts +4 -0
  114. package/src/components/{Project → Environment}/About.tsx +11 -4
  115. package/src/components/Environment/AddPackageDialog.tsx +202 -0
  116. package/src/components/{Project → Environment}/ConnectionExplorer.tsx +17 -14
  117. package/src/components/Environment/Connections.tsx +391 -0
  118. package/src/components/{Project → Environment}/DeletePackageDialog.tsx +8 -4
  119. package/src/components/{Project → Environment}/EditPackageDialog.tsx +21 -13
  120. package/src/components/Environment/Environment.tsx +89 -0
  121. package/src/components/Environment/Packages.tsx +206 -0
  122. package/src/components/Environment/index.ts +5 -0
  123. package/src/components/Home/{AddProjectDialog.tsx → AddEnvironmentDialog.tsx} +25 -19
  124. package/src/components/Home/{DeleteProjectDialog.tsx → DeleteEnvironmentDialog.tsx} +20 -15
  125. package/src/components/Home/{EditProjectDialog.tsx → EditEnvironmentDialog.tsx} +35 -28
  126. package/src/components/Home/Home.tsx +386 -408
  127. package/src/components/Home/index.ts +3 -0
  128. package/src/components/Loading.tsx +3 -0
  129. package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +179 -0
  130. package/src/components/Materializations/CreateMaterializationDialog.tsx +112 -0
  131. package/src/components/Materializations/DeleteMaterializationDialog.tsx +109 -0
  132. package/src/components/Materializations/EnvironmentMaterializations.tsx +267 -0
  133. package/src/components/Materializations/ManifestView.tsx +87 -0
  134. package/src/components/Materializations/MaterializationDetailDialog.tsx +259 -0
  135. package/src/components/Materializations/MaterializationRunsList.tsx +256 -0
  136. package/src/components/Materializations/Materializations.tsx +355 -0
  137. package/src/components/Materializations/ScheduleCard.tsx +231 -0
  138. package/src/components/Materializations/SectionLabel.tsx +30 -0
  139. package/src/components/Materializations/SetScheduleDialog.tsx +162 -0
  140. package/src/components/Materializations/TriggerChip.tsx +44 -0
  141. package/src/components/Materializations/cron.ts +124 -0
  142. package/src/components/Materializations/index.ts +5 -0
  143. package/src/components/Materializations/utils.ts +145 -0
  144. package/src/components/Model/Model.tsx +44 -2
  145. package/src/components/Model/ModelCell.tsx +14 -5
  146. package/src/components/Model/ModelExplorer.tsx +3 -0
  147. package/src/components/Model/ModelExplorerDialog.tsx +4 -1
  148. package/src/components/Model/SourcesExplorer.tsx +9 -6
  149. package/src/components/Model/index.ts +3 -0
  150. package/src/components/Model/useModelData.ts +6 -3
  151. package/src/components/Notebook/Notebook.tsx +475 -339
  152. package/src/components/Notebook/NotebookCell.tsx +264 -25
  153. package/src/components/Notebook/index.ts +3 -0
  154. package/src/components/Notebook/runPlan.spec.ts +155 -0
  155. package/src/components/Notebook/runPlan.ts +67 -0
  156. package/src/components/Notebook/types.ts +14 -0
  157. package/src/components/Package/ContentTypeIcon.spec.ts +91 -0
  158. package/src/components/Package/ContentTypeIcon.tsx +177 -0
  159. package/src/components/Package/FileTreeView.tsx +3 -0
  160. package/src/components/Package/Models.tsx +7 -4
  161. package/src/components/Package/Package.tsx +666 -59
  162. package/src/components/Package/index.ts +3 -0
  163. package/src/components/QueryResult/QueryResult.tsx +17 -8
  164. package/src/components/QueryResult/index.ts +3 -0
  165. package/src/components/RenderedResult/RenderedResult.tsx +685 -67
  166. package/src/components/RenderedResult/ResultContainer.tsx +71 -2
  167. package/src/components/RenderedResult/index.ts +3 -0
  168. package/src/components/RenderedResult/renderLogs.spec.ts +58 -0
  169. package/src/components/RenderedResult/renderLogs.ts +43 -0
  170. package/src/components/ResultsDialog.tsx +13 -1
  171. package/src/components/ServerProvider.tsx +175 -51
  172. package/src/components/Workbook/BrowserWorkbookStorage.ts +3 -0
  173. package/src/components/Workbook/EditableMalloyCell.tsx +3 -0
  174. package/src/components/Workbook/ModelPicker.tsx +7 -4
  175. package/src/components/Workbook/MutableCell.tsx +10 -5
  176. package/src/components/Workbook/Workbook.tsx +8 -5
  177. package/src/components/Workbook/WorkbookList.tsx +3 -0
  178. package/src/components/Workbook/WorkbookManager.ts +3 -0
  179. package/src/components/Workbook/WorkbookStorage.ts +3 -0
  180. package/src/components/Workbook/WorkbookStorageProvider.tsx +3 -0
  181. package/src/components/Workbook/index.ts +3 -0
  182. package/src/components/click_helper.ts +17 -1
  183. package/src/components/drill/index.ts +28 -0
  184. package/src/components/drill/markDrillableCells.dom.spec.ts +313 -0
  185. package/src/components/drill/markDrillableCells.spec.ts +109 -0
  186. package/src/components/drill/markDrillableCells.ts +314 -0
  187. package/src/components/drill/resolveDrill.spec.ts +449 -0
  188. package/src/components/drill/resolveDrill.ts +375 -0
  189. package/src/components/drill/useDrill.tsx +203 -0
  190. package/src/components/filter/DimensionFilter.tsx +3 -0
  191. package/src/components/filter/index.ts +3 -0
  192. package/src/components/filter/utils.ts +16 -5
  193. package/src/components/given/GivenInput.spec.tsx +689 -0
  194. package/src/components/given/GivenInput.tsx +801 -0
  195. package/src/components/given/GivensPanel.spec.tsx +175 -0
  196. package/src/components/given/GivensPanel.tsx +195 -0
  197. package/src/components/given/filterValue.spec.ts +364 -0
  198. package/src/components/given/filterValue.ts +207 -0
  199. package/src/components/given/index.ts +9 -0
  200. package/src/components/given/paramCodec.spec.ts +857 -0
  201. package/src/components/given/paramCodec.ts +483 -0
  202. package/src/components/given/utils.spec.ts +60 -0
  203. package/src/components/given/utils.ts +79 -0
  204. package/src/components/highlighter.ts +22 -5
  205. package/src/components/index.ts +10 -2
  206. package/src/components/styles.ts +82 -33
  207. package/src/constants/docLinks.spec.ts +54 -0
  208. package/src/constants/docLinks.ts +46 -0
  209. package/src/hooks/givenValue.ts +13 -0
  210. package/src/hooks/index.ts +13 -0
  211. package/src/hooks/useDimensionFilters.ts +4 -7
  212. package/src/hooks/useDimensionFiltersFromSpec.ts +8 -5
  213. package/src/hooks/useDimensionFiltersQuery.ts +10 -13
  214. package/src/hooks/useDimensionalFilterRangeData.ts +11 -14
  215. package/src/hooks/useGivensState.spec.ts +449 -0
  216. package/src/hooks/useGivensState.ts +340 -0
  217. package/src/hooks/useModelGivens.ts +29 -0
  218. package/src/hooks/useQueryWithApiError.ts +3 -0
  219. package/src/hooks/useRawQueryData.ts +6 -3
  220. package/src/hooks/useSuggestOptions.spec.ts +112 -0
  221. package/src/hooks/useSuggestOptions.ts +263 -0
  222. package/src/index.ts +10 -1
  223. package/src/react-app-env.d.ts +3 -0
  224. package/src/theme/ThemeContext.tsx +86 -0
  225. package/src/theme/buildMalloyExplicitTheme.ts +61 -0
  226. package/src/theme/buildTableCssVars.spec.ts +67 -0
  227. package/src/theme/buildTableCssVars.ts +67 -0
  228. package/src/theme/buildVegaThemeOverride.spec.ts +56 -0
  229. package/src/theme/buildVegaThemeOverride.ts +47 -0
  230. package/src/theme/defaults.ts +78 -0
  231. package/src/theme/index.ts +11 -0
  232. package/src/theme/keys.ts +32 -0
  233. package/src/theme/readChartAnnotations.spec.ts +58 -0
  234. package/src/theme/readChartAnnotations.ts +69 -0
  235. package/src/theme/resolveTheme.spec.ts +160 -0
  236. package/src/theme/resolveTheme.ts +121 -0
  237. package/src/theme/types.ts +79 -0
  238. package/src/utils/dataAppEmbed.spec.ts +63 -0
  239. package/src/utils/dataAppEmbed.ts +84 -0
  240. package/src/utils/formatting.spec.ts +25 -21
  241. package/src/utils/formatting.ts +11 -7
  242. package/src/utils/malloyString.spec.ts +24 -0
  243. package/src/utils/malloyString.ts +12 -0
  244. package/src/utils/parsing.spec.ts +26 -19
  245. package/src/utils/parsing.ts +11 -6
  246. package/src/utils/queryClient.ts +17 -0
  247. package/test/README.md +141 -0
  248. package/test/setup.ts +29 -0
  249. package/tsconfig.json +12 -1
  250. package/tsconfig.spec.json +22 -0
  251. package/vite.config.ts +3 -0
  252. package/dist/ServerProvider-lOQXDlqB.cjs.js +0 -1
  253. package/dist/ServerProvider-on-8FH5Z.es.js +0 -3060
  254. package/dist/components/Home/AddProjectDialog.d.ts +0 -1
  255. package/dist/components/Home/DeleteProjectDialog.d.ts +0 -5
  256. package/dist/components/Home/EditProjectDialog.d.ts +0 -7
  257. package/dist/components/Package/Config.d.ts +0 -5
  258. package/dist/components/Package/Databases.d.ts +0 -5
  259. package/dist/components/Package/Notebooks.d.ts +0 -6
  260. package/dist/components/Project/Project.d.ts +0 -6
  261. package/dist/components/Project/index.d.ts +0 -2
  262. package/dist/index-BOLBP6_i.cjs.js +0 -233
  263. package/src/components/Package/Config.tsx +0 -97
  264. package/src/components/Package/Connections.tsx +0 -373
  265. package/src/components/Package/Databases.tsx +0 -228
  266. package/src/components/Package/Notebooks.tsx +0 -77
  267. package/src/components/Project/AddPackageDialog.tsx +0 -159
  268. package/src/components/Project/Packages.tsx +0 -191
  269. package/src/components/Project/Project.tsx +0 -52
  270. package/src/components/Project/index.ts +0 -2
  271. /package/dist/components/{Project → Environment}/About.d.ts +0 -0
  272. /package/dist/components/{Project → Environment}/AddPackageDialog.d.ts +0 -0
  273. /package/dist/components/{Project → Environment}/ConnectionExplorer.d.ts +0 -0
  274. /package/dist/components/{Package → Environment}/Connections.d.ts +0 -0
  275. /package/dist/components/{Project → Environment}/DeletePackageDialog.d.ts +0 -0
  276. /package/dist/components/{Project → Environment}/EditPackageDialog.d.ts +0 -0
  277. /package/dist/components/{Project → Environment}/Packages.d.ts +0 -0
@@ -0,0 +1,84 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ // Shared contract for serving and embedding in-package HTML data apps.
5
+ //
6
+ // Both the Publisher SPA host (DataAppViewer.tsx, Package.tsx) and the
7
+ // build-step-free browser runtime (packages/server/src/runtime/publisher.js)
8
+ // speak this protocol. publisher.js cannot import from here — it ships as
9
+ // standalone vanilla JS — so it carries a cross-reference comment pointing at
10
+ // this file as the single source of truth. Keep the two in sync.
11
+
12
+ /**
13
+ * postMessage `type` an embedded data app emits to its host frame as its
14
+ * content height changes, so the host can resize the iframe to avoid nested
15
+ * scrollbars. Payload shape is {@link PublisherResizeMessage}.
16
+ */
17
+ export const PUBLISHER_RESIZE_MESSAGE_TYPE = "publisher:resize";
18
+
19
+ /** Resize message posted by an embedded data app's publisher.js runtime. */
20
+ export interface PublisherResizeMessage {
21
+ type: typeof PUBLISHER_RESIZE_MESSAGE_TYPE;
22
+ /** Content height in CSS pixels. */
23
+ height: number;
24
+ }
25
+
26
+ /** Type guard for a {@link PublisherResizeMessage} arriving via postMessage. */
27
+ export function isPublisherResizeMessage(
28
+ data: unknown,
29
+ ): data is PublisherResizeMessage {
30
+ return (
31
+ typeof data === "object" &&
32
+ data !== null &&
33
+ (data as { type?: unknown }).type === PUBLISHER_RESIZE_MESSAGE_TYPE &&
34
+ typeof (data as { height?: unknown }).height === "number"
35
+ );
36
+ }
37
+
38
+ /**
39
+ * Derive the Publisher data origin (where static package files are served)
40
+ * from the configured API base URL by stripping the trailing `/api/v0`.
41
+ *
42
+ * The static-file routes live off the server root, not under the API prefix,
43
+ * and the data origin can differ from the SPA origin in multi-host
44
+ * deployments — so a data app's standalone URL is `serverBaseUrl(server)`
45
+ * joined with its root-relative `resource`.
46
+ */
47
+ export function serverBaseUrl(server: string): string {
48
+ return server.replace(/\/api\/v0\/?$/, "");
49
+ }
50
+
51
+ /**
52
+ * Absolute URL of a file inside a package, as Publisher serves it:
53
+ * `<data origin>/environments/<env>/packages/<pkg>/<path>`.
54
+ *
55
+ * The one home for this string. Both the data app viewer's iframe and the
56
+ * "this is not a model" page point at it, and hand-building it in each place is
57
+ * how the two drift.
58
+ *
59
+ * `path` is relative to the package's `public/` directory and is inserted as
60
+ * given, since it may contain slashes. A leading `public/` is dropped, because
61
+ * the served path does not include it and a caller passing a path a reader typed
62
+ * would otherwise produce `public/public/<file>`, which 404s. The server's own
63
+ * redirect drops it for the same reason.
64
+ */
65
+ export function packageFileUrl({
66
+ server,
67
+ environmentName,
68
+ packageName,
69
+ path,
70
+ }: {
71
+ server: string;
72
+ environmentName: string;
73
+ packageName: string;
74
+ path: string;
75
+ }): string {
76
+ const relative = path.startsWith("public/")
77
+ ? path.slice("public/".length)
78
+ : path;
79
+ return (
80
+ `${serverBaseUrl(server)}/environments/${encodeURIComponent(
81
+ environmentName,
82
+ )}/packages/${encodeURIComponent(packageName)}/` + relative
83
+ );
84
+ }
@@ -1,12 +1,16 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  import { describe, expect, it } from "bun:test";
2
5
  import { encodeResourceUri, parseResourceUri } from "./formatting";
3
6
 
4
7
  describe("parseResourceUri", () => {
5
8
  it("should parse a package URI", () => {
6
- const resourceUri = "publisher://projects/malloy-samples/packages/names";
9
+ const resourceUri =
10
+ "publisher://environments/malloy-samples/packages/names";
7
11
  const parsedResource = parseResourceUri(resourceUri);
8
12
  expect(parsedResource).toEqual({
9
- projectName: "malloy-samples",
13
+ environmentName: "malloy-samples",
10
14
  packageName: "names",
11
15
  connectionName: undefined,
12
16
  modelPath: undefined,
@@ -15,10 +19,10 @@ describe("parseResourceUri", () => {
15
19
  });
16
20
  it("should parse a connection URI", () => {
17
21
  const resourceUri =
18
- "publisher://projects/malloy-samples/connections/bigquery";
22
+ "publisher://environments/malloy-samples/connections/bigquery";
19
23
  const parsedResource = parseResourceUri(resourceUri);
20
24
  expect(parsedResource).toEqual({
21
- projectName: "malloy-samples",
25
+ environmentName: "malloy-samples",
22
26
  packageName: undefined,
23
27
  connectionName: "bigquery",
24
28
  modelPath: undefined,
@@ -28,10 +32,10 @@ describe("parseResourceUri", () => {
28
32
 
29
33
  it("should parse a modelPath URI", () => {
30
34
  const resourceUri =
31
- "publisher://projects/malloy-samples/packages/names/models/names1.malloynb";
35
+ "publisher://environments/malloy-samples/packages/names/models/names1.malloynb";
32
36
  const parsedResource = parseResourceUri(resourceUri);
33
37
  expect(parsedResource).toEqual({
34
- projectName: "malloy-samples",
38
+ environmentName: "malloy-samples",
35
39
  packageName: "names",
36
40
  connectionName: undefined,
37
41
  modelPath: "names1.malloynb",
@@ -41,13 +45,13 @@ describe("parseResourceUri", () => {
41
45
 
42
46
  it("should throw an error if the resource URI has the wrong protocol", () => {
43
47
  const resourceUri =
44
- "http://projects/malloy-samples/packages/names/models/names1.malloynb";
48
+ "http://environments/malloy-samples/packages/names/models/names1.malloynb";
45
49
  expect(() => parseResourceUri(resourceUri)).toThrow(
46
50
  `Failed to parse resource URI: ${resourceUri}`,
47
51
  );
48
52
  });
49
53
 
50
- it("should throw an error if the resource URI is missing the project name", () => {
54
+ it("should throw an error if the resource URI is missing the environment name", () => {
51
55
  const resourceUri = "publisher://";
52
56
  expect(() => parseResourceUri(resourceUri)).toThrow(
53
57
  /Failed to parse resource URI/,
@@ -56,10 +60,10 @@ describe("parseResourceUri", () => {
56
60
 
57
61
  it("should parse the optional versionId parameter if present", () => {
58
62
  const resourceUri =
59
- "publisher://projects/malloy-samples/packages/names?versionId=1.0.0";
63
+ "publisher://environments/malloy-samples/packages/names?versionId=1.0.0";
60
64
  const parsedResource = parseResourceUri(resourceUri);
61
65
  expect(parsedResource).toEqual({
62
- projectName: "malloy-samples",
66
+ environmentName: "malloy-samples",
63
67
  packageName: "names",
64
68
  connectionName: undefined,
65
69
  modelPath: undefined,
@@ -71,65 +75,65 @@ describe("parseResourceUri", () => {
71
75
  describe("encodeResourceUri", () => {
72
76
  it("should encode a package URI with no versionId", () => {
73
77
  const resourceUri = encodeResourceUri({
74
- projectName: "malloy-samples",
78
+ environmentName: "malloy-samples",
75
79
  packageName: "names",
76
80
  connectionName: undefined,
77
81
  modelPath: undefined,
78
82
  versionId: undefined,
79
83
  });
80
84
  expect(resourceUri).toEqual(
81
- "publisher://projects/malloy-samples/packages/names",
85
+ "publisher://environments/malloy-samples/packages/names",
82
86
  );
83
87
  });
84
88
 
85
89
  it("should encode a package URI with a versionId", () => {
86
90
  const resourceUri = encodeResourceUri({
87
- projectName: "malloy-samples",
91
+ environmentName: "malloy-samples",
88
92
  packageName: "names",
89
93
  versionId: "1.0.0",
90
94
  connectionName: undefined,
91
95
  modelPath: undefined,
92
96
  });
93
97
  expect(resourceUri).toEqual(
94
- "publisher://projects/malloy-samples/packages/names?versionId=1.0.0",
98
+ "publisher://environments/malloy-samples/packages/names?versionId=1.0.0",
95
99
  );
96
100
  });
97
101
 
98
102
  it("should encode a connection URI", () => {
99
103
  const resourceUri = encodeResourceUri({
100
- projectName: "malloy-samples",
104
+ environmentName: "malloy-samples",
101
105
  packageName: undefined,
102
106
  connectionName: "bigquery",
103
107
  modelPath: undefined,
104
108
  versionId: undefined,
105
109
  });
106
110
  expect(resourceUri).toEqual(
107
- "publisher://projects/malloy-samples/connections/bigquery",
111
+ "publisher://environments/malloy-samples/connections/bigquery",
108
112
  );
109
113
  });
110
114
 
111
115
  it("should encode a modelPath URI", () => {
112
116
  const resourceUri = encodeResourceUri({
113
- projectName: "malloy-samples",
117
+ environmentName: "malloy-samples",
114
118
  packageName: "names",
115
119
  connectionName: undefined,
116
120
  modelPath: "names1.malloynb",
117
121
  versionId: undefined,
118
122
  });
119
123
  expect(resourceUri).toEqual(
120
- "publisher://projects/malloy-samples/packages/names/models/names1.malloynb",
124
+ "publisher://environments/malloy-samples/packages/names/models/names1.malloynb",
121
125
  );
122
126
  });
123
127
 
124
- it("should throw an error if the resource URI is missing the project name", () => {
128
+ it("should throw an error if the resource URI is missing the environment name", () => {
125
129
  expect(() =>
126
130
  encodeResourceUri({
127
- projectName: "",
131
+ environmentName: "",
128
132
  packageName: "names",
129
133
  connectionName: undefined,
130
134
  modelPath: undefined,
131
135
  versionId: undefined,
132
136
  }),
133
- ).toThrow(/Failed to encode resource URI, missing project name/);
137
+ ).toThrow(/Failed to encode resource URI, missing environment name/);
134
138
  });
135
139
  });
@@ -1,5 +1,8 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  export type ParsedResource = {
2
- projectName: string;
5
+ environmentName: string;
3
6
  packageName?: string | undefined;
4
7
  connectionName?: string | undefined;
5
8
  versionId?: string | undefined;
@@ -15,8 +18,9 @@ export const parseResourceUri = (resourceUri: string) => {
15
18
  const pathParts = (parsedUri.hostname + parsedUri.pathname).split("/");
16
19
  for (let i = 0; i < pathParts.length; i += 2) {
17
20
  const part = pathParts[i];
18
- if (part === "projects") {
19
- parsedResource.projectName = decodeURI(pathParts[i + 1]) || undefined;
21
+ if (part === "environments") {
22
+ parsedResource.environmentName =
23
+ decodeURI(pathParts[i + 1]) || undefined;
20
24
  } else if (part === "packages") {
21
25
  parsedResource.packageName = decodeURI(pathParts[i + 1]) || undefined;
22
26
  } else if (part === "connections") {
@@ -30,19 +34,19 @@ export const parseResourceUri = (resourceUri: string) => {
30
34
 
31
35
  parsedResource.versionId =
32
36
  parsedUri.searchParams.get("versionId") || undefined;
33
- if (!parsedResource.projectName) {
37
+ if (!parsedResource.environmentName) {
34
38
  throw new Error(`Failed to parse resource URI: ${resourceUri}`);
35
39
  }
36
40
  return parsedResource;
37
41
  };
38
42
 
39
43
  export const encodeResourceUri = (resource: ParsedResource) => {
40
- if (!resource.projectName) {
44
+ if (!resource.environmentName) {
41
45
  throw new Error(
42
- `Failed to encode resource URI, missing project name: ${resource}`,
46
+ `Failed to encode resource URI, missing environment name: ${resource}`,
43
47
  );
44
48
  }
45
- let uri = `publisher://projects/${resource.projectName}`;
49
+ let uri = `publisher://environments/${resource.environmentName}`;
46
50
  if (resource.packageName) {
47
51
  uri += `/packages/${resource.packageName}`;
48
52
  }
@@ -0,0 +1,24 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { escapeMalloyString } from "./malloyString";
6
+
7
+ describe("escapeMalloyString", () => {
8
+ it("escapes a quote", () => {
9
+ expect(escapeMalloyString("O'Brien")).toBe("O\\'Brien");
10
+ });
11
+
12
+ it("escapes a backslash", () => {
13
+ expect(escapeMalloyString("C:\\temp")).toBe("C:\\\\temp");
14
+ });
15
+
16
+ it("escapes backslashes before quotes, so the quote escape is not doubled", () => {
17
+ // Quote-then-backslash order would turn ' into \' and then \\' .
18
+ expect(escapeMalloyString("a'\\b")).toBe("a\\'\\\\b");
19
+ });
20
+
21
+ it("leaves a plain string alone", () => {
22
+ expect(escapeMalloyString("plain value")).toBe("plain value");
23
+ });
24
+ });
@@ -0,0 +1,12 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ /**
5
+ * Escape a value for use inside a single-quoted Malloy string literal.
6
+ *
7
+ * Backslashes are escaped first, then quotes: doing it the other way round
8
+ * would re-escape the backslash that the quote escape introduced.
9
+ */
10
+ export function escapeMalloyString(value: string): string {
11
+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
12
+ }
@@ -1,11 +1,18 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  import { describe, expect, it } from "bun:test";
2
- import { getProjectDescription, generateProjectReadme } from "./parsing";
5
+ import {
6
+ generateEnvironmentReadme,
7
+ getEnvironmentDescription,
8
+ } from "./parsing";
3
9
 
4
- describe("getProjectDescription", () => {
10
+ describe("getEnvironmentDescription", () => {
5
11
  it("should return the first paragraph of the README", () => {
6
- const readme = "# Project Description\nThis is a project description";
7
- expect(getProjectDescription(readme)).toBe(
8
- "Project Description\nThis is a project description",
12
+ const readme =
13
+ "# Environment Description\nThis is an environment description";
14
+ expect(getEnvironmentDescription(readme)).toBe(
15
+ "Environment Description\nThis is an environment description",
9
16
  );
10
17
  });
11
18
 
@@ -14,42 +21,42 @@ describe("getProjectDescription", () => {
14
21
  const readme = `${longDescription}`;
15
22
  // 5 characters per word + space, so 20 words = 120 characters
16
23
  const truncatedDescription = Array(20).fill("abcde").join(" ") + "...";
17
- expect(getProjectDescription(readme)).toBe(truncatedDescription);
24
+ expect(getEnvironmentDescription(readme)).toBe(truncatedDescription);
18
25
  });
19
26
 
20
27
  it("should return a placeholder description if the README is empty", () => {
21
28
  const readme = "";
22
- expect(getProjectDescription(readme)).toBe(
29
+ expect(getEnvironmentDescription(readme)).toBe(
23
30
  "Explore semantic models, run queries, and build dashboards",
24
31
  );
25
32
  });
26
33
  });
27
34
 
28
- describe("generateProjectReadme", () => {
35
+ describe("generateEnvironmentReadme", () => {
29
36
  it("should preserve the existing readme if it exists", () => {
30
- const project = {
31
- name: "Test Project",
37
+ const environment = {
38
+ name: "Test Environment",
32
39
  readme: "# Test Readme",
33
40
  };
34
- expect(generateProjectReadme(project)).toBe("# Test Readme");
41
+ expect(generateEnvironmentReadme(environment)).toBe("# Test Readme");
35
42
  });
36
43
 
37
- it("should generate a project readme with the description if it does not exist", () => {
38
- const project = {
39
- name: "Test Project",
44
+ it("should generate an environment readme with the description if it does not exist", () => {
45
+ const environment = {
46
+ name: "Test Environment",
40
47
  readme: "",
41
48
  };
42
- expect(generateProjectReadme(project, "Test Description")).toBe(
43
- "# Test Project\n\nTest Description",
49
+ expect(generateEnvironmentReadme(environment, "Test Description")).toBe(
50
+ "# Test Environment\n\nTest Description",
44
51
  );
45
52
  });
46
53
 
47
54
  it("should insert the description in the existing readme if both exist", () => {
48
- const project = {
49
- name: "Test Project",
55
+ const environment = {
56
+ name: "Test Environment",
50
57
  readme: "# Test Readme\n\nOld Description\n\nMore stuff",
51
58
  };
52
- expect(generateProjectReadme(project, "New Description")).toBe(
59
+ expect(generateEnvironmentReadme(environment, "New Description")).toBe(
53
60
  "# Test Readme\n\nNew Description\n\nMore stuff",
54
61
  );
55
62
  });
@@ -1,5 +1,10 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  // Helper function to extract a brief description from README content
2
- export const getProjectDescription = (readme: string | undefined): string => {
5
+ export const getEnvironmentDescription = (
6
+ readme: string | undefined,
7
+ ): string => {
3
8
  if (!readme) {
4
9
  return "Explore semantic models, run queries, and build dashboards";
5
10
  }
@@ -28,19 +33,19 @@ export const getProjectDescription = (readme: string | undefined): string => {
28
33
  return truncated.join(" ") + "...";
29
34
  };
30
35
 
31
- export const generateProjectReadme = (
32
- project: {
36
+ export const generateEnvironmentReadme = (
37
+ environment: {
33
38
  name: string;
34
39
  readme?: string;
35
40
  },
36
41
  description?: string,
37
42
  ): string => {
38
- const readmeLines = (project.readme || undefined)?.split("\n") ?? [];
43
+ const readmeLines = (environment.readme || undefined)?.split("\n") ?? [];
39
44
  if (readmeLines.length === 0) {
40
- readmeLines.push(`# ${project.name}`);
45
+ readmeLines.push(`# ${environment.name}`);
41
46
  readmeLines.push("");
42
47
  }
43
- if ((project.readme?.length ?? 0) > 0 && description) {
48
+ if ((environment.readme?.length ?? 0) > 0 && description) {
44
49
  readmeLines.splice(2, 1, description);
45
50
  } else if (description) {
46
51
  readmeLines.push(description);
@@ -1,3 +1,6 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  import { QueryClient } from "@tanstack/react-query";
2
5
 
3
6
  // Global QueryClient instance - isolated to avoid circular dependencies
@@ -13,3 +16,17 @@ export const globalQueryClient = new QueryClient({
13
16
  },
14
17
  },
15
18
  });
19
+
20
+ // Refetch policy for chart/query-RESULT queries. A Malloy query result is a
21
+ // pure function of the query text + filters, which are already in each query
22
+ // key, so re-executing the query on tab refocus or reconnect only repaints the
23
+ // same result and can cause a visible chart flicker. Treat results as fresh
24
+ // for a few minutes and don't auto-refetch on focus/reconnect. Kept scoped to
25
+ // the result queries (spread into their useQuery options) rather than set on
26
+ // the global client, so other SDK queries (metadata lists, status) keep
27
+ // react-query's default freshness behavior.
28
+ export const CHART_RESULT_QUERY_OPTIONS = {
29
+ staleTime: 5 * 60 * 1000,
30
+ refetchOnWindowFocus: false,
31
+ refetchOnReconnect: false,
32
+ } as const;
package/test/README.md ADDED
@@ -0,0 +1,141 @@
1
+ <!--
2
+ Copyright (c) Credible Data Inc.
3
+ SPDX-License-Identifier: MIT
4
+ -->
5
+
6
+ # Testing React components and hooks in the SDK
7
+
8
+ `bun test` in this package runs with a DOM, so components and hooks can be
9
+ rendered and asserted on. Before this existed every SDK spec was a pure
10
+ function, and anything touching React was untestable.
11
+
12
+ Run them the usual way:
13
+
14
+ ```bash
15
+ cd packages/sdk && bun test # whole package
16
+ cd packages/sdk && bun test src/components/given # one directory
17
+ cd packages/sdk && bun test -t "explicit false" # one test by name
18
+ ```
19
+
20
+ Run them from this package, not from the repo root. Bun reads `bunfig.toml`
21
+ from the working directory, so a spec invoked from the root gets no preload and
22
+ fails with `ReferenceError: document is not defined`. `bun run test:sdk` at the
23
+ root is fine; it changes directory first, and so does CI.
24
+
25
+ ## How the DOM gets there
26
+
27
+ `bunfig.toml` preloads `test/setup.ts`, which registers happy-dom onto
28
+ `globalThis` and installs Testing Library's `cleanup` in an `afterEach`. There
29
+ is no per-file setup: write a spec and it has a document.
30
+
31
+ Two details in that file are load-bearing. Testing Library is imported
32
+ dynamically, after the registrator runs, because it binds to `document` at
33
+ module scope and a static import would hoist above the line that creates one.
34
+ And the setup file lives outside `src/` because `vite-plugin-dts` generates
35
+ declarations for everything under `src/`, so a setup file in there would ship a
36
+ stray `.d.ts` in `dist/`.
37
+
38
+ happy-dom rather than jsdom because `@happy-dom/global-registrator` is designed
39
+ to install into an existing runtime, which is exactly what a Bun preload can do.
40
+ jsdom expects to own the test environment and offers no equivalent hook, so
41
+ using it would have meant bringing in a second test runner alongside `bun test`.
42
+
43
+ ## Testing a component
44
+
45
+ ```tsx
46
+ import { describe, expect, it, mock } from "bun:test";
47
+ import { fireEvent, render, screen } from "@testing-library/react";
48
+ import type { GivenValue } from "../../hooks/givenValue";
49
+ import { GivenInput } from "./GivenInput";
50
+
51
+ it("reverts rather than blanking the field", () => {
52
+ const onChange = mock((_next: GivenValue) => {});
53
+ render(<GivenInput given={{ name: "region" }} value="CA" onChange={onChange} />);
54
+
55
+ fireEvent.click(screen.getByLabelText("clear value"));
56
+
57
+ expect(onChange.mock.calls[0][0]).toBeNull();
58
+ });
59
+ ```
60
+
61
+ Type the mock's parameter. `mock(() => {})` is a zero-argument mock, so
62
+ `mock.calls` is an array of empty tuples: reading `[0][0]` does not compile,
63
+ and nothing constrains what the component actually passes you.
64
+
65
+ Query by what a user can perceive (`getByLabelText`, `getByRole`,
66
+ `getByText`) rather than by class name or component internals, so a test
67
+ survives a restyle. Use `queryAllBy*` when asserting something is absent;
68
+ `getBy*` throws instead of returning empty.
69
+
70
+ `src/components/given/GivenInput.spec.tsx` is the worked example.
71
+
72
+ ## Testing a hook
73
+
74
+ No JSX is needed, so a hook spec is a plain `.spec.ts`.
75
+
76
+ ```ts
77
+ import { act, renderHook } from "@testing-library/react";
78
+
79
+ const { result, rerender } = renderHook(
80
+ ({ types }) => useGivensState({ declaredTypes: types, autorun: true }),
81
+ { initialProps: { types: new Map([["REGION", "filter<string>"]]) } },
82
+ );
83
+
84
+ act(() => result.current.setGiven("REGION", "West"));
85
+ expect(result.current.applied.get("REGION")).toBe("West");
86
+
87
+ rerender({ types: new Map() }); // props change, effects flush
88
+ ```
89
+
90
+ `result.current` is re-read after every update, so hold onto the object rather
91
+ than destructuring off it. Wrap anything that sets state in `act()`.
92
+
93
+ `src/hooks/useGivensState.spec.ts` is the worked example, and two things in it
94
+ are worth copying. The test named "settles when its inputs are fresh objects on
95
+ every render" passes deliberately unmemoized arguments to prove the hook settles
96
+ instead of re-rendering forever; that is a real defect class here, and it is
97
+ invisible to a test that only ever passes stable inputs. Named rather than
98
+ pointed at by position, because "its last test" stopped being that one the first
99
+ time a test was appended. And its `renderWithUrlHost` helper feeds the hook's own output back in
100
+ as props, the way the page hosts do through the URL. Several defects in that
101
+ hook only appear on the second half of that loop, so a spec that treats the
102
+ props as constant cannot see them.
103
+
104
+ ## Where specs live, and what not to break
105
+
106
+ Co-locate a spec with what it tests, matching the existing `*.spec.ts` files.
107
+
108
+ `tsconfig.json` excludes test files, because that config drives the declaration
109
+ build and anything left in it emits a stray `.d.ts` into `dist/`, which ships.
110
+ `bun test` discovers four name patterns, and both configs list all four with
111
+ the extension left open, so `.tsx`/`.mts`/`.cts` cannot slip past:
112
+
113
+ ```
114
+ *.spec.* *.test.* *_spec.* *_test.*
115
+ ```
116
+
117
+ Excluded is not unchecked. `tsconfig.spec.json` includes the same four plus
118
+ everything in `test/`, and the root `typecheck:sdk` runs it, so a type error in
119
+ a spec fails the gate. Without it there would be no gate at all: `bun test`
120
+ strips types without checking them, and eslint in this package is not
121
+ type-aware.
122
+
123
+ Keep the two lists in step, and if you add a pattern, put it in both and check
124
+ `dist/` afterwards.
125
+
126
+ ## One thing happy-dom will let you get wrong
127
+
128
+ happy-dom does not implement the HTML value-sanitization algorithm. Set
129
+ `"12abc"` on a raw `<input type="number">` and happy-dom keeps it, where a real
130
+ browser discards it and leaves the field empty (checked against Chromium). So a
131
+ test that hands a number input a non-numeric value and asserts on what it
132
+ displays can pass here and be false in production.
133
+
134
+ Note where that does *not* bite: it only fires if your code passes the bad
135
+ value through to the DOM. `GivenInput` substitutes `""` first, so both engines
136
+ agree there, and its non-numeric test asserts on the revert affordance rather
137
+ than the displayed value.
138
+
139
+ The general form of that trap: happy-dom is a fast approximation, not a
140
+ browser. Behaviour that depends on layout, real CSS, or the finer points of
141
+ form-control semantics belongs in the Playwright suite in `packages/app`.
package/test/setup.ts ADDED
@@ -0,0 +1,29 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ /**
5
+ * DOM environment for `bun test` in this package.
6
+ *
7
+ * Registered via `preload` in bunfig.toml, so it runs once before any spec and
8
+ * every spec in the package gets the same globals. Lives outside `src/` on
9
+ * purpose: `vite-plugin-dts` emits declarations for everything under `src/`, so
10
+ * a setup file in there would ship a stray `.d.ts` into `dist/`.
11
+ *
12
+ * happy-dom rather than jsdom because `@happy-dom/global-registrator` installs
13
+ * onto globalThis, which is what a Bun preload can do; jsdom expects to own the
14
+ * test runner's environment and has no equivalent hook. See test/README.md.
15
+ */
16
+ import { GlobalRegistrator } from "@happy-dom/global-registrator";
17
+ import { afterEach } from "bun:test";
18
+
19
+ GlobalRegistrator.register();
20
+
21
+ // Imported dynamically, AFTER register(). Testing Library binds to `document`
22
+ // at module scope, so a static import would be hoisted above the line that
23
+ // creates it and every query would resolve against an undefined document.
24
+ const { cleanup } = await import("@testing-library/react");
25
+
26
+ // Unmount between tests. Without this each render appends to the same <body>,
27
+ // so `getByLabelText` starts throwing "found multiple elements" on the second
28
+ // test that renders the same widget.
29
+ afterEach(cleanup);
package/tsconfig.json CHANGED
@@ -27,5 +27,16 @@
27
27
  "forceConsistentCasingInFileNames": true
28
28
  },
29
29
  "include": ["src", "k6-tests"], // Specifies the directory to include when searching for TypeScript files.
30
- "exclude": ["src/**/__docs__", "src/**/__test__", "**/*.spec.ts"]
30
+ // Keep test files out of the build; anything left in emits a stray .d.ts.
31
+ // These are the four name patterns `bun test` discovers, each left open on
32
+ // the extension so .tsx/.mts/.cts cannot slip past. tsconfig.spec.json
33
+ // includes the same four, so excluded does not mean unchecked.
34
+ "exclude": [
35
+ "src/**/__docs__",
36
+ "src/**/__test__",
37
+ "**/*.spec.*",
38
+ "**/*.test.*",
39
+ "**/*_spec.*",
40
+ "**/*_test.*"
41
+ ]
31
42
  }