@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.
- package/README.md +115 -95
- package/TREE_SHAKING.md +16 -11
- package/bunfig.toml +7 -0
- package/dist/ServerProvider-CTopDUWW.cjs.js +1 -0
- package/dist/ServerProvider-DUFPY6Rv.es.js +4072 -0
- package/dist/client/api.d.ts +3718 -1507
- package/dist/client/configuration.d.ts +1 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.es.js +85 -59
- package/dist/components/Connections/EditConnectionDialog.d.ts +2 -1
- package/dist/components/Connections/common.d.ts +8 -2
- package/dist/components/Dashboard/Dashboard.d.ts +49 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +48 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/DataAppViewer/DataAppViewer.d.ts +23 -0
- package/dist/components/DataAppViewer/index.d.ts +1 -0
- package/dist/components/Environment/Environment.d.ts +6 -0
- package/dist/components/Environment/index.d.ts +2 -0
- package/dist/components/Home/AddEnvironmentDialog.d.ts +1 -0
- package/dist/components/Home/DeleteEnvironmentDialog.d.ts +5 -0
- package/dist/components/Home/EditEnvironmentDialog.d.ts +7 -0
- package/dist/components/Home/Home.d.ts +2 -2
- package/dist/components/Materializations/CreateEnvMaterializationDialog.d.ts +14 -0
- package/dist/components/Materializations/CreateMaterializationDialog.d.ts +10 -0
- package/dist/components/Materializations/DeleteMaterializationDialog.d.ts +7 -0
- package/dist/components/Materializations/EnvironmentMaterializations.d.ts +7 -0
- package/dist/components/Materializations/ManifestView.d.ts +13 -0
- package/dist/components/Materializations/MaterializationDetailDialog.d.ts +9 -0
- package/dist/components/Materializations/MaterializationRunsList.d.ts +15 -0
- package/dist/components/Materializations/Materializations.d.ts +7 -0
- package/dist/components/Materializations/ScheduleCard.d.ts +20 -0
- package/dist/components/Materializations/SectionLabel.d.ts +10 -0
- package/dist/components/Materializations/SetScheduleDialog.d.ts +11 -0
- package/dist/components/Materializations/TriggerChip.d.ts +10 -0
- package/dist/components/Materializations/cron.d.ts +23 -0
- package/dist/components/Materializations/index.d.ts +2 -0
- package/dist/components/Materializations/utils.d.ts +46 -0
- package/dist/components/Notebook/Notebook.d.ts +47 -4
- package/dist/components/Notebook/NotebookCell.d.ts +37 -1
- package/dist/components/Notebook/runPlan.d.ts +29 -0
- package/dist/components/Notebook/types.d.ts +10 -0
- package/dist/components/Package/ContentTypeIcon.d.ts +31 -0
- package/dist/components/Package/Package.d.ts +1 -4
- package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
- package/dist/components/RenderedResult/ResultContainer.d.ts +5 -1
- package/dist/components/RenderedResult/renderLogs.d.ts +6 -0
- package/dist/components/ResultsDialog.d.ts +6 -1
- package/dist/components/ServerProvider.d.ts +28 -4
- package/dist/components/click_helper.d.ts +10 -1
- package/dist/components/drill/index.d.ts +3 -0
- package/dist/components/drill/markDrillableCells.d.ts +55 -0
- package/dist/components/drill/resolveDrill.d.ts +139 -0
- package/dist/components/drill/useDrill.d.ts +63 -0
- package/dist/components/given/GivenInput.d.ts +52 -0
- package/dist/components/given/GivensPanel.d.ts +47 -0
- package/dist/components/given/filterValue.d.ts +97 -0
- package/dist/components/given/index.d.ts +2 -0
- package/dist/components/given/paramCodec.d.ts +60 -0
- package/dist/components/given/utils.d.ts +17 -0
- package/dist/components/highlighter.d.ts +6 -1
- package/dist/components/index.d.ts +7 -2
- package/dist/components/styles.d.ts +47 -0
- package/dist/constants/docLinks.d.ts +12 -0
- package/dist/{core-YNpOLuB1.cjs.js → core-hLahnv0W.cjs.js} +1 -1
- package/dist/{core-CjeTkq8O.es.js → core-lb28vQkR.es.js} +1 -1
- package/dist/github-dark-BKL9xNgN.cjs.js +1 -0
- package/dist/github-dark-DenFmJkN.es.js +4 -0
- package/dist/hooks/givenValue.d.ts +10 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/useDimensionFiltersFromSpec.d.ts +3 -3
- package/dist/hooks/useDimensionFiltersQuery.d.ts +3 -3
- package/dist/hooks/useDimensionalFilterRangeData.d.ts +4 -4
- package/dist/hooks/useGivensState.d.ts +64 -0
- package/dist/hooks/useModelGivens.d.ts +10 -0
- package/dist/hooks/useSuggestOptions.d.ts +78 -0
- package/dist/index-DvEanVgc.cjs.js +339 -0
- package/dist/{index-DRDu9kIV.es.js → index-txw04ZbN.es.js} +40531 -27964
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.es.js +79 -48
- package/dist/malloy-explorer.css +12 -1
- package/dist/sdk.css +1 -1
- package/dist/theme/ThemeContext.d.ts +45 -0
- package/dist/theme/buildMalloyExplicitTheme.d.ts +34 -0
- package/dist/theme/buildTableCssVars.d.ts +25 -0
- package/dist/theme/buildVegaThemeOverride.d.ts +16 -0
- package/dist/theme/defaults.d.ts +7 -0
- package/dist/theme/index.d.ts +8 -0
- package/dist/theme/keys.d.ts +20 -0
- package/dist/theme/readChartAnnotations.d.ts +23 -0
- package/dist/theme/resolveTheme.d.ts +22 -0
- package/dist/theme/types.d.ts +70 -0
- package/dist/utils/dataAppEmbed.d.ts +44 -0
- package/dist/utils/formatting.d.ts +1 -1
- package/dist/utils/malloyString.d.ts +7 -0
- package/dist/utils/parsing.d.ts +2 -2
- package/dist/utils/queryClient.d.ts +5 -0
- package/package.json +19 -8
- package/src/client-entry.ts +3 -0
- package/src/components/AnalyzePackageButton.tsx +9 -3
- package/src/components/ApiErrorDisplay.tsx +3 -0
- package/src/components/Connections/AddConnectionDialog.tsx +30 -2
- package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
- package/src/components/Connections/EditConnectionDialog.tsx +33 -4
- package/src/components/Connections/common.ts +64 -1
- package/src/components/DOMElement/DOMElement.tsx +3 -0
- package/src/components/DOMElement/index.ts +3 -0
- package/src/components/Dashboard/Dashboard.tsx +398 -0
- package/src/components/Dashboard/DashboardTile.tsx +153 -0
- package/src/components/Dashboard/index.ts +5 -0
- package/src/components/DataAppViewer/DataAppViewer.tsx +214 -0
- package/src/components/DataAppViewer/index.ts +4 -0
- package/src/components/{Project → Environment}/About.tsx +11 -4
- package/src/components/Environment/AddPackageDialog.tsx +202 -0
- package/src/components/{Project → Environment}/ConnectionExplorer.tsx +17 -14
- package/src/components/Environment/Connections.tsx +391 -0
- package/src/components/{Project → Environment}/DeletePackageDialog.tsx +8 -4
- package/src/components/{Project → Environment}/EditPackageDialog.tsx +21 -13
- package/src/components/Environment/Environment.tsx +89 -0
- package/src/components/Environment/Packages.tsx +206 -0
- package/src/components/Environment/index.ts +5 -0
- package/src/components/Home/{AddProjectDialog.tsx → AddEnvironmentDialog.tsx} +25 -19
- package/src/components/Home/{DeleteProjectDialog.tsx → DeleteEnvironmentDialog.tsx} +20 -15
- package/src/components/Home/{EditProjectDialog.tsx → EditEnvironmentDialog.tsx} +35 -28
- package/src/components/Home/Home.tsx +386 -408
- package/src/components/Home/index.ts +3 -0
- package/src/components/Loading.tsx +3 -0
- package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +179 -0
- package/src/components/Materializations/CreateMaterializationDialog.tsx +112 -0
- package/src/components/Materializations/DeleteMaterializationDialog.tsx +109 -0
- package/src/components/Materializations/EnvironmentMaterializations.tsx +267 -0
- package/src/components/Materializations/ManifestView.tsx +87 -0
- package/src/components/Materializations/MaterializationDetailDialog.tsx +259 -0
- package/src/components/Materializations/MaterializationRunsList.tsx +256 -0
- package/src/components/Materializations/Materializations.tsx +355 -0
- package/src/components/Materializations/ScheduleCard.tsx +231 -0
- package/src/components/Materializations/SectionLabel.tsx +30 -0
- package/src/components/Materializations/SetScheduleDialog.tsx +162 -0
- package/src/components/Materializations/TriggerChip.tsx +44 -0
- package/src/components/Materializations/cron.ts +124 -0
- package/src/components/Materializations/index.ts +5 -0
- package/src/components/Materializations/utils.ts +145 -0
- package/src/components/Model/Model.tsx +44 -2
- package/src/components/Model/ModelCell.tsx +14 -5
- package/src/components/Model/ModelExplorer.tsx +3 -0
- package/src/components/Model/ModelExplorerDialog.tsx +4 -1
- package/src/components/Model/SourcesExplorer.tsx +9 -6
- package/src/components/Model/index.ts +3 -0
- package/src/components/Model/useModelData.ts +6 -3
- package/src/components/Notebook/Notebook.tsx +475 -339
- package/src/components/Notebook/NotebookCell.tsx +264 -25
- package/src/components/Notebook/index.ts +3 -0
- package/src/components/Notebook/runPlan.spec.ts +155 -0
- package/src/components/Notebook/runPlan.ts +67 -0
- package/src/components/Notebook/types.ts +14 -0
- package/src/components/Package/ContentTypeIcon.spec.ts +91 -0
- package/src/components/Package/ContentTypeIcon.tsx +177 -0
- package/src/components/Package/FileTreeView.tsx +3 -0
- package/src/components/Package/Models.tsx +7 -4
- package/src/components/Package/Package.tsx +666 -59
- package/src/components/Package/index.ts +3 -0
- package/src/components/QueryResult/QueryResult.tsx +17 -8
- package/src/components/QueryResult/index.ts +3 -0
- package/src/components/RenderedResult/RenderedResult.tsx +685 -67
- package/src/components/RenderedResult/ResultContainer.tsx +71 -2
- package/src/components/RenderedResult/index.ts +3 -0
- package/src/components/RenderedResult/renderLogs.spec.ts +58 -0
- package/src/components/RenderedResult/renderLogs.ts +43 -0
- package/src/components/ResultsDialog.tsx +13 -1
- package/src/components/ServerProvider.tsx +175 -51
- package/src/components/Workbook/BrowserWorkbookStorage.ts +3 -0
- package/src/components/Workbook/EditableMalloyCell.tsx +3 -0
- package/src/components/Workbook/ModelPicker.tsx +7 -4
- package/src/components/Workbook/MutableCell.tsx +10 -5
- package/src/components/Workbook/Workbook.tsx +8 -5
- package/src/components/Workbook/WorkbookList.tsx +3 -0
- package/src/components/Workbook/WorkbookManager.ts +3 -0
- package/src/components/Workbook/WorkbookStorage.ts +3 -0
- package/src/components/Workbook/WorkbookStorageProvider.tsx +3 -0
- package/src/components/Workbook/index.ts +3 -0
- package/src/components/click_helper.ts +17 -1
- package/src/components/drill/index.ts +28 -0
- package/src/components/drill/markDrillableCells.dom.spec.ts +313 -0
- package/src/components/drill/markDrillableCells.spec.ts +109 -0
- package/src/components/drill/markDrillableCells.ts +314 -0
- package/src/components/drill/resolveDrill.spec.ts +449 -0
- package/src/components/drill/resolveDrill.ts +375 -0
- package/src/components/drill/useDrill.tsx +203 -0
- package/src/components/filter/DimensionFilter.tsx +3 -0
- package/src/components/filter/index.ts +3 -0
- package/src/components/filter/utils.ts +16 -5
- package/src/components/given/GivenInput.spec.tsx +689 -0
- package/src/components/given/GivenInput.tsx +801 -0
- package/src/components/given/GivensPanel.spec.tsx +175 -0
- package/src/components/given/GivensPanel.tsx +195 -0
- package/src/components/given/filterValue.spec.ts +364 -0
- package/src/components/given/filterValue.ts +207 -0
- package/src/components/given/index.ts +9 -0
- package/src/components/given/paramCodec.spec.ts +857 -0
- package/src/components/given/paramCodec.ts +483 -0
- package/src/components/given/utils.spec.ts +60 -0
- package/src/components/given/utils.ts +79 -0
- package/src/components/highlighter.ts +22 -5
- package/src/components/index.ts +10 -2
- package/src/components/styles.ts +82 -33
- package/src/constants/docLinks.spec.ts +54 -0
- package/src/constants/docLinks.ts +46 -0
- package/src/hooks/givenValue.ts +13 -0
- package/src/hooks/index.ts +13 -0
- package/src/hooks/useDimensionFilters.ts +4 -7
- package/src/hooks/useDimensionFiltersFromSpec.ts +8 -5
- package/src/hooks/useDimensionFiltersQuery.ts +10 -13
- package/src/hooks/useDimensionalFilterRangeData.ts +11 -14
- package/src/hooks/useGivensState.spec.ts +449 -0
- package/src/hooks/useGivensState.ts +340 -0
- package/src/hooks/useModelGivens.ts +29 -0
- package/src/hooks/useQueryWithApiError.ts +3 -0
- package/src/hooks/useRawQueryData.ts +6 -3
- package/src/hooks/useSuggestOptions.spec.ts +112 -0
- package/src/hooks/useSuggestOptions.ts +263 -0
- package/src/index.ts +10 -1
- package/src/react-app-env.d.ts +3 -0
- package/src/theme/ThemeContext.tsx +86 -0
- package/src/theme/buildMalloyExplicitTheme.ts +61 -0
- package/src/theme/buildTableCssVars.spec.ts +67 -0
- package/src/theme/buildTableCssVars.ts +67 -0
- package/src/theme/buildVegaThemeOverride.spec.ts +56 -0
- package/src/theme/buildVegaThemeOverride.ts +47 -0
- package/src/theme/defaults.ts +78 -0
- package/src/theme/index.ts +11 -0
- package/src/theme/keys.ts +32 -0
- package/src/theme/readChartAnnotations.spec.ts +58 -0
- package/src/theme/readChartAnnotations.ts +69 -0
- package/src/theme/resolveTheme.spec.ts +160 -0
- package/src/theme/resolveTheme.ts +121 -0
- package/src/theme/types.ts +79 -0
- package/src/utils/dataAppEmbed.spec.ts +63 -0
- package/src/utils/dataAppEmbed.ts +84 -0
- package/src/utils/formatting.spec.ts +25 -21
- package/src/utils/formatting.ts +11 -7
- package/src/utils/malloyString.spec.ts +24 -0
- package/src/utils/malloyString.ts +12 -0
- package/src/utils/parsing.spec.ts +26 -19
- package/src/utils/parsing.ts +11 -6
- package/src/utils/queryClient.ts +17 -0
- package/test/README.md +141 -0
- package/test/setup.ts +29 -0
- package/tsconfig.json +12 -1
- package/tsconfig.spec.json +22 -0
- package/vite.config.ts +3 -0
- package/dist/ServerProvider-lOQXDlqB.cjs.js +0 -1
- package/dist/ServerProvider-on-8FH5Z.es.js +0 -3060
- package/dist/components/Home/AddProjectDialog.d.ts +0 -1
- package/dist/components/Home/DeleteProjectDialog.d.ts +0 -5
- package/dist/components/Home/EditProjectDialog.d.ts +0 -7
- package/dist/components/Package/Config.d.ts +0 -5
- package/dist/components/Package/Databases.d.ts +0 -5
- package/dist/components/Package/Notebooks.d.ts +0 -6
- package/dist/components/Project/Project.d.ts +0 -6
- package/dist/components/Project/index.d.ts +0 -2
- package/dist/index-BOLBP6_i.cjs.js +0 -233
- package/src/components/Package/Config.tsx +0 -97
- package/src/components/Package/Connections.tsx +0 -373
- package/src/components/Package/Databases.tsx +0 -228
- package/src/components/Package/Notebooks.tsx +0 -77
- package/src/components/Project/AddPackageDialog.tsx +0 -159
- package/src/components/Project/Packages.tsx +0 -191
- package/src/components/Project/Project.tsx +0 -52
- package/src/components/Project/index.ts +0 -2
- /package/dist/components/{Project → Environment}/About.d.ts +0 -0
- /package/dist/components/{Project → Environment}/AddPackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/ConnectionExplorer.d.ts +0 -0
- /package/dist/components/{Package → Environment}/Connections.d.ts +0 -0
- /package/dist/components/{Project → Environment}/DeletePackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/EditPackageDialog.d.ts +0 -0
- /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 =
|
|
9
|
+
const resourceUri =
|
|
10
|
+
"publisher://environments/malloy-samples/packages/names";
|
|
7
11
|
const parsedResource = parseResourceUri(resourceUri);
|
|
8
12
|
expect(parsedResource).toEqual({
|
|
9
|
-
|
|
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://
|
|
22
|
+
"publisher://environments/malloy-samples/connections/bigquery";
|
|
19
23
|
const parsedResource = parseResourceUri(resourceUri);
|
|
20
24
|
expect(parsedResource).toEqual({
|
|
21
|
-
|
|
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://
|
|
35
|
+
"publisher://environments/malloy-samples/packages/names/models/names1.malloynb";
|
|
32
36
|
const parsedResource = parseResourceUri(resourceUri);
|
|
33
37
|
expect(parsedResource).toEqual({
|
|
34
|
-
|
|
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://
|
|
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
|
|
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://
|
|
63
|
+
"publisher://environments/malloy-samples/packages/names?versionId=1.0.0";
|
|
60
64
|
const parsedResource = parseResourceUri(resourceUri);
|
|
61
65
|
expect(parsedResource).toEqual({
|
|
62
|
-
|
|
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
|
-
|
|
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://
|
|
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
|
-
|
|
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://
|
|
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
|
-
|
|
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://
|
|
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
|
-
|
|
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://
|
|
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
|
|
128
|
+
it("should throw an error if the resource URI is missing the environment name", () => {
|
|
125
129
|
expect(() =>
|
|
126
130
|
encodeResourceUri({
|
|
127
|
-
|
|
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
|
|
137
|
+
).toThrow(/Failed to encode resource URI, missing environment name/);
|
|
134
138
|
});
|
|
135
139
|
});
|
package/src/utils/formatting.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
1
4
|
export type ParsedResource = {
|
|
2
|
-
|
|
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 === "
|
|
19
|
-
parsedResource.
|
|
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.
|
|
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.
|
|
44
|
+
if (!resource.environmentName) {
|
|
41
45
|
throw new Error(
|
|
42
|
-
`Failed to encode resource URI, missing
|
|
46
|
+
`Failed to encode resource URI, missing environment name: ${resource}`,
|
|
43
47
|
);
|
|
44
48
|
}
|
|
45
|
-
let uri = `publisher://
|
|
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 {
|
|
5
|
+
import {
|
|
6
|
+
generateEnvironmentReadme,
|
|
7
|
+
getEnvironmentDescription,
|
|
8
|
+
} from "./parsing";
|
|
3
9
|
|
|
4
|
-
describe("
|
|
10
|
+
describe("getEnvironmentDescription", () => {
|
|
5
11
|
it("should return the first paragraph of the README", () => {
|
|
6
|
-
const readme =
|
|
7
|
-
|
|
8
|
-
|
|
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(
|
|
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(
|
|
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("
|
|
35
|
+
describe("generateEnvironmentReadme", () => {
|
|
29
36
|
it("should preserve the existing readme if it exists", () => {
|
|
30
|
-
const
|
|
31
|
-
name: "Test
|
|
37
|
+
const environment = {
|
|
38
|
+
name: "Test Environment",
|
|
32
39
|
readme: "# Test Readme",
|
|
33
40
|
};
|
|
34
|
-
expect(
|
|
41
|
+
expect(generateEnvironmentReadme(environment)).toBe("# Test Readme");
|
|
35
42
|
});
|
|
36
43
|
|
|
37
|
-
it("should generate
|
|
38
|
-
const
|
|
39
|
-
name: "Test
|
|
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(
|
|
43
|
-
"# Test
|
|
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
|
|
49
|
-
name: "Test
|
|
55
|
+
const environment = {
|
|
56
|
+
name: "Test Environment",
|
|
50
57
|
readme: "# Test Readme\n\nOld Description\n\nMore stuff",
|
|
51
58
|
};
|
|
52
|
-
expect(
|
|
59
|
+
expect(generateEnvironmentReadme(environment, "New Description")).toBe(
|
|
53
60
|
"# Test Readme\n\nNew Description\n\nMore stuff",
|
|
54
61
|
);
|
|
55
62
|
});
|
package/src/utils/parsing.ts
CHANGED
|
@@ -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
|
|
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
|
|
32
|
-
|
|
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 = (
|
|
43
|
+
const readmeLines = (environment.readme || undefined)?.split("\n") ?? [];
|
|
39
44
|
if (readmeLines.length === 0) {
|
|
40
|
-
readmeLines.push(`# ${
|
|
45
|
+
readmeLines.push(`# ${environment.name}`);
|
|
41
46
|
readmeLines.push("");
|
|
42
47
|
}
|
|
43
|
-
if ((
|
|
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);
|
package/src/utils/queryClient.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
}
|