@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.
- package/README.md +115 -95
- package/TREE_SHAKING.md +16 -11
- package/bunfig.toml +7 -0
- package/dist/ServerProvider-Cn_LhKda.cjs.js +1 -0
- package/dist/ServerProvider-sfabtriY.es.js +4076 -0
- package/dist/client/api.d.ts +3773 -1515
- 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 +86 -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 +62 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +57 -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-CqRKhtoy.cjs.js} +1 -1
- package/dist/{core-CjeTkq8O.es.js → core-DyU47reh.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-DRDu9kIV.es.js → index-CAkFE4Ol.es.js} +41278 -28504
- package/dist/index-DD4b_bt0.cjs.js +356 -0
- 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 +41 -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 +132 -25
- package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
- package/src/components/Connections/EditConnectionDialog.tsx +167 -37
- package/src/components/Connections/common.ts +94 -3
- package/src/components/DOMElement/DOMElement.tsx +3 -0
- package/src/components/DOMElement/index.ts +3 -0
- package/src/components/Dashboard/Dashboard.spec.tsx +216 -0
- package/src/components/Dashboard/Dashboard.tsx +458 -0
- package/src/components/Dashboard/DashboardTile.spec.tsx +81 -0
- package/src/components/Dashboard/DashboardTile.tsx +199 -0
- package/src/components/Dashboard/index.ts +5 -0
- package/src/components/Dashboard/tileGridColumn.spec.ts +35 -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.spec.tsx +88 -0
- package/src/components/Notebook/Notebook.tsx +476 -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.spec.tsx +113 -0
- package/src/components/Package/Package.tsx +692 -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 +725 -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.dom.spec.ts +91 -0
- package/src/hooks/useSuggestOptions.spec.ts +112 -0
- package/src/hooks/useSuggestOptions.ts +268 -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 +86 -0
- package/src/theme/buildTableCssVars.ts +98 -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 +185 -0
- package/test/serverProvider.tsx +72 -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,857 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import dayjs from "dayjs";
|
|
6
|
+
import utc from "dayjs/plugin/utc";
|
|
7
|
+
import type { GivenValue } from "../../hooks/givenValue";
|
|
8
|
+
import {
|
|
9
|
+
givenToParam,
|
|
10
|
+
givensToParams,
|
|
11
|
+
givensToRequest,
|
|
12
|
+
paramToGiven,
|
|
13
|
+
paramsToGivens,
|
|
14
|
+
pickedDayToUtc,
|
|
15
|
+
} from "./paramCodec";
|
|
16
|
+
|
|
17
|
+
dayjs.extend(utc);
|
|
18
|
+
|
|
19
|
+
describe("givenToParam", () => {
|
|
20
|
+
it("omits what the server should fill from the model default", () => {
|
|
21
|
+
expect(givenToParam(undefined, "string")).toBe(undefined);
|
|
22
|
+
expect(givenToParam(null, "string")).toBe(undefined);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("keeps an empty string, which is a real value", () => {
|
|
26
|
+
// For a filter this is the empty filter: "All", and is meaningfully
|
|
27
|
+
// different from leaving the declaration's default in place.
|
|
28
|
+
expect(givenToParam("", "filter<string>")).toBe("");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("renders each value type as one parameter", () => {
|
|
32
|
+
expect(givenToParam("Nike", "string")).toBe("Nike");
|
|
33
|
+
expect(givenToParam(42, "number")).toBe("42");
|
|
34
|
+
expect(givenToParam(false, "boolean")).toBe("false");
|
|
35
|
+
expect(givenToParam(new Date("2024-03-01T00:00:00Z"), "date")).toBe(
|
|
36
|
+
"2024-03-01",
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("refuses an array rather than joining it into one parameter", () => {
|
|
41
|
+
// `GivenValue` used to admit `string[]`, and this joined on `,` with no
|
|
42
|
+
// escaping, which the codec cannot undo: `paramToGiven` never returns an
|
|
43
|
+
// array for ANY type, so the list came back as a single string. Sha-Bang's
|
|
44
|
+
// case is the one that shows it is not merely lossy but unrecoverable,
|
|
45
|
+
// because the value itself contains the separator: `["Ben & Jerry, Inc",
|
|
46
|
+
// "Nike"]` joined to `Ben & Jerry, Inc,Nike`, which splits back to three
|
|
47
|
+
// values, not two.
|
|
48
|
+
//
|
|
49
|
+
// The type no longer promises arrays. Cast here because this pins the
|
|
50
|
+
// runtime guard for a JavaScript caller the types cannot reach.
|
|
51
|
+
const asGiven = (value: unknown) => value as never;
|
|
52
|
+
expect(givenToParam(asGiven(["a", "b"]), "string")).toBeUndefined();
|
|
53
|
+
expect(
|
|
54
|
+
givenToParam(asGiven(["Ben & Jerry, Inc", "Nike"]), "string"),
|
|
55
|
+
).toBeUndefined();
|
|
56
|
+
expect(givenToParam(asGiven([1, 2]), "number")).toBeUndefined();
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("pickedDayToUtc", () => {
|
|
61
|
+
// dayjs in LOCAL mode, which is what MUI's adapter actually hands the
|
|
62
|
+
// control: the clicked day carrying the reference time-of-day.
|
|
63
|
+
const clicked = (iso: string, hour: number) =>
|
|
64
|
+
dayjs(iso).hour(hour).minute(0).second(0);
|
|
65
|
+
|
|
66
|
+
it("carries the previous value's time of day onto the new day", () => {
|
|
67
|
+
// A DatePicker offers no way to re-enter a time, so zeroing it silently
|
|
68
|
+
// moved a `timestamp` given's boundary to midnight on every day change.
|
|
69
|
+
const previous = new Date("2024-01-01T14:45:30.250Z");
|
|
70
|
+
const day = { year: () => 2024, month: () => 5, date: () => 9 };
|
|
71
|
+
expect(pickedDayToUtc(day, previous).toISOString()).toBe(
|
|
72
|
+
"2024-06-09T14:45:30.250Z",
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("uses midnight when there is no previous time to carry", () => {
|
|
77
|
+
const day = { year: () => 2024, month: () => 5, date: () => 9 };
|
|
78
|
+
expect(pickedDayToUtc(day).toISOString()).toBe(
|
|
79
|
+
"2024-06-09T00:00:00.000Z",
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("reads the day's own fields back under UTC, in any zone", () => {
|
|
84
|
+
// The zone-carrying tests below only bite where the local offset actually
|
|
85
|
+
// crosses midnight, so at UTC they are vacuous and CI runs at UTC. This
|
|
86
|
+
// one holds the invariant everywhere: the fields that went in are the UTC
|
|
87
|
+
// fields that come out.
|
|
88
|
+
//
|
|
89
|
+
// Measured, rather than assumed, by reverting `pickedDayToUtc` to the
|
|
90
|
+
// `toDate()` it replaced and counting failures in this file: UTC 3,
|
|
91
|
+
// Asia/Kolkata 3, America/Los_Angeles 4, Australia/Sydney 5. So the zone
|
|
92
|
+
// tests do discriminate (Sydney catches two that a UTC run cannot), and a
|
|
93
|
+
// half-hour offset adds nothing over UTC at these hours: the useful pair
|
|
94
|
+
// is one zone ahead of UTC and one behind.
|
|
95
|
+
//
|
|
96
|
+
// The variable is offset DIRECTION, not zone count. Adding zones for
|
|
97
|
+
// symmetry buys nothing: Kolkata scores identically to UTC despite being
|
|
98
|
+
// the most exotic-looking of the four. If only one non-UTC zone can be
|
|
99
|
+
// afforded, pick a far-ahead one.
|
|
100
|
+
const day = { year: () => 2024, month: () => 0, date: () => 5 };
|
|
101
|
+
const stored = pickedDayToUtc(day);
|
|
102
|
+
expect([
|
|
103
|
+
stored.getUTCFullYear(),
|
|
104
|
+
stored.getUTCMonth(),
|
|
105
|
+
stored.getUTCDate(),
|
|
106
|
+
]).toEqual([2024, 0, 5]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("stores the day that was clicked, whatever the reader's offset", () => {
|
|
110
|
+
const day = clicked("2024-01-05", 8);
|
|
111
|
+
expect(givenToParam(pickedDayToUtc(day), "date")).toBe("2024-01-05");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("survives an evening pick, which shifts the other way behind UTC", () => {
|
|
115
|
+
const day = clicked("2024-01-05", 22);
|
|
116
|
+
expect(givenToParam(pickedDayToUtc(day), "date")).toBe("2024-01-05");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("is what `toDate()` is not", () => {
|
|
120
|
+
// Pins the defect itself: in an ahead-of-UTC zone the old expression
|
|
121
|
+
// really does produce a different day, so this test is meaningful there
|
|
122
|
+
// and vacuous at UTC. Asserted as a round-trip either way.
|
|
123
|
+
const day = clicked("2024-01-05", 8);
|
|
124
|
+
expect(givenToParam(day.toDate(), "date")).toBe(
|
|
125
|
+
dayjs(day.toDate()).utc().format("YYYY-MM-DD"),
|
|
126
|
+
);
|
|
127
|
+
expect(givenToParam(pickedDayToUtc(day), "date")).toBe("2024-01-05");
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
/** Every spelling the grammar admits, with the instant it must resolve to. */
|
|
132
|
+
const TEMPORAL_CASES: ReadonlyArray<readonly [string, string]> = [
|
|
133
|
+
["2024-01-05T10:30:00Z", "2024-01-05T10:30:00.000Z"],
|
|
134
|
+
["2024-01-05 10:30:00 UTC", "2024-01-05T10:30:00.000Z"],
|
|
135
|
+
["2024-01-05 10:30:00 GMT", "2024-01-05T10:30:00.000Z"],
|
|
136
|
+
["2024-01-05 10:30:00", "2024-01-05T10:30:00.000Z"],
|
|
137
|
+
["2024-01-05", "2024-01-05T00:00:00.000Z"],
|
|
138
|
+
["2024-01-05T10:30:00+05:30", "2024-01-05T05:00:00.000Z"],
|
|
139
|
+
["2024-01-05T10:30:00+0530", "2024-01-05T05:00:00.000Z"],
|
|
140
|
+
["2026-08-10 12:00:00.123+00", "2026-08-10T12:00:00.123Z"],
|
|
141
|
+
["2026-08-10 12:00:00-05", "2026-08-10T17:00:00.000Z"],
|
|
142
|
+
["2024-01-05T10:30:00.123456", "2024-01-05T10:30:00.123Z"],
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
describe("parseTemporal zone gate", () => {
|
|
146
|
+
it("does not mistake a meridiem for a zone", () => {
|
|
147
|
+
// The gate first read `\s[A-Z]{2,5}$` as "carries a zone abbreviation",
|
|
148
|
+
// which matches ` AM` and ` PM`, so this spelling reached `new Date` and
|
|
149
|
+
// was read as LOCAL time: five hours apart between two readers, which is
|
|
150
|
+
// the exact defect the gate exists to prevent.
|
|
151
|
+
for (const raw of [
|
|
152
|
+
"Dec 25 2024 10:30 AM",
|
|
153
|
+
"Dec 25 2024 10:30 PM",
|
|
154
|
+
"1/5/2024 8:00 AM",
|
|
155
|
+
]) {
|
|
156
|
+
expect(paramToGiven("timestamp", raw)).toBe(raw);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("resolves every accepted spelling without asking the engine", () => {
|
|
161
|
+
// The reason this module parses the zone itself. Outside strict ISO,
|
|
162
|
+
// `new Date` is implementation-defined: `new Date("2024-01-05 10:30:00
|
|
163
|
+
// UTC")` is the right instant in V8 and Bun and **Invalid Date in
|
|
164
|
+
// Safari's JavaScriptCore**. A suite running under Bun cannot see that,
|
|
165
|
+
// so this asserts the value we compute, never what an engine would.
|
|
166
|
+
expect(TEMPORAL_CASES.length).toBeGreaterThan(0);
|
|
167
|
+
for (const [raw, iso] of TEMPORAL_CASES) {
|
|
168
|
+
const v = paramToGiven("timestamp", raw);
|
|
169
|
+
expect(v instanceof Date && v.toISOString()).toBe(iso);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("reads the two unambiguous zone abbreviations", () => {
|
|
174
|
+
// `2024-01-05 10:30:00 UTC` is what BigQuery prints for a TIMESTAMP, so
|
|
175
|
+
// it is what a user pastes. Removing the abbreviation branch wholesale to
|
|
176
|
+
// kill the ` AM` match broke it: the fix has to be an allow-list, not an
|
|
177
|
+
// absence.
|
|
178
|
+
for (const raw of [
|
|
179
|
+
"2024-01-05 10:30:00 UTC",
|
|
180
|
+
"2024-01-05 10:30:00 GMT",
|
|
181
|
+
]) {
|
|
182
|
+
const v = paramToGiven("timestamp", raw);
|
|
183
|
+
expect(v instanceof Date && v.toISOString()).toBe(
|
|
184
|
+
"2024-01-05T10:30:00.000Z",
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("reads the two-digit offset Postgres and DuckDB print", () => {
|
|
190
|
+
// `SELECT now()` prints `2026-08-10 12:00:00.123+00` in both, and a gate
|
|
191
|
+
// demanding four offset digits rejected the default output of the two
|
|
192
|
+
// databases Publisher ships with.
|
|
193
|
+
const a = paramToGiven("timestamp", "2026-08-10 12:00:00.123+00");
|
|
194
|
+
expect(a instanceof Date && a.toISOString()).toBe(
|
|
195
|
+
"2026-08-10T12:00:00.123Z",
|
|
196
|
+
);
|
|
197
|
+
const b = paramToGiven("timestamp", "2026-08-10 12:00:00-05");
|
|
198
|
+
expect(b instanceof Date && b.toISOString()).toBe(
|
|
199
|
+
"2026-08-10T17:00:00.000Z",
|
|
200
|
+
);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("reads a half-hour offset in both spellings", () => {
|
|
204
|
+
for (const raw of [
|
|
205
|
+
"2024-01-05T10:30:00+05:30",
|
|
206
|
+
"2024-01-05T10:30:00+0530",
|
|
207
|
+
]) {
|
|
208
|
+
const v = paramToGiven("timestamp", raw);
|
|
209
|
+
expect(v instanceof Date && v.toISOString()).toBe(
|
|
210
|
+
"2024-01-05T05:00:00.000Z",
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("refuses an offset that is not a real one", () => {
|
|
216
|
+
// `+99:99` was read as 99 hours 99 minutes, resolving four days off with
|
|
217
|
+
// nothing to say so. Same principle as the parts check: refuse rather
|
|
218
|
+
// than silently produce a different instant.
|
|
219
|
+
for (const raw of [
|
|
220
|
+
"2024-01-05T10:30:00+99:99",
|
|
221
|
+
"2024-01-05T10:30:00+1599",
|
|
222
|
+
"2024-01-05T10:30:00+15",
|
|
223
|
+
]) {
|
|
224
|
+
expect(paramToGiven("timestamp", raw)).toBe(raw);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("accepts the real extremes of the offset range", () => {
|
|
229
|
+
// Kiribati is +14:00 and Baker Island is -12:00.
|
|
230
|
+
const a = paramToGiven("timestamp", "2024-01-05T10:30:00+1400");
|
|
231
|
+
expect(a instanceof Date && a.toISOString()).toBe(
|
|
232
|
+
"2024-01-04T20:30:00.000Z",
|
|
233
|
+
);
|
|
234
|
+
const b = paramToGiven("timestamp", "2024-01-05T10:30:00-1200");
|
|
235
|
+
expect(b instanceof Date && b.toISOString()).toBe(
|
|
236
|
+
"2024-01-05T22:30:00.000Z",
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("refuses a zone on a bare date, which cannot mean anything", () => {
|
|
241
|
+
// `2024-01-05+05` otherwise resolved to the 4th in UTC, so a `date` given
|
|
242
|
+
// reported back a day earlier than what was written.
|
|
243
|
+
expect(paramToGiven("date", "2024-01-05+05")).toBe("2024-01-05+05");
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("applies an offset across a month boundary", () => {
|
|
247
|
+
const v = paramToGiven("timestamp", "2024-02-29T23:30:00-05");
|
|
248
|
+
expect(v instanceof Date && v.toISOString()).toBe(
|
|
249
|
+
"2024-03-01T04:30:00.000Z",
|
|
250
|
+
);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("reads the print form of every warehouse Publisher ships with", () => {
|
|
254
|
+
for (const [raw, iso] of [
|
|
255
|
+
["2024-01-05 10:30:00 UTC", "2024-01-05T10:30:00.000Z"],
|
|
256
|
+
["2024-01-05 10:30:00.123456 UTC", "2024-01-05T10:30:00.123Z"],
|
|
257
|
+
["2026-08-10 12:00:00.123+00", "2026-08-10T12:00:00.123Z"],
|
|
258
|
+
["2026-08-10 12:00:00.123456+05:30", "2026-08-10T06:30:00.123Z"],
|
|
259
|
+
["2024-01-05 10:30:00.123 +0000", "2024-01-05T10:30:00.123Z"],
|
|
260
|
+
["2024-01-05 10:30:00", "2024-01-05T10:30:00.000Z"],
|
|
261
|
+
] as const) {
|
|
262
|
+
const v = paramToGiven("timestamp", raw);
|
|
263
|
+
expect(v instanceof Date && v.toISOString()).toBe(iso);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("does not treat a named regional zone as unambiguous", () => {
|
|
268
|
+
// `CST` alone is three different offsets, so it is passed through and the
|
|
269
|
+
// server names the given rather than the UI guessing.
|
|
270
|
+
expect(paramToGiven("timestamp", "2024-01-05 10:30:00 CST")).toBe(
|
|
271
|
+
"2024-01-05 10:30:00 CST",
|
|
272
|
+
);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("still reads a value carrying a real zone", () => {
|
|
276
|
+
for (const [raw, iso] of [
|
|
277
|
+
["2024-01-05T10:30:00Z", "2024-01-05T10:30:00.000Z"],
|
|
278
|
+
["2024-01-05T10:30:00+05:00", "2024-01-05T05:30:00.000Z"],
|
|
279
|
+
] as const) {
|
|
280
|
+
const v = paramToGiven("timestamp", raw);
|
|
281
|
+
expect(v instanceof Date && v.toISOString()).toBe(iso);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("reads warehouse microsecond precision, truncating to milliseconds", () => {
|
|
286
|
+
// Capping the pattern at three fractional digits made an ordinary
|
|
287
|
+
// `...:00.123456` fail to parse at all.
|
|
288
|
+
for (const raw of [
|
|
289
|
+
"2024-01-05T10:30:00.123456",
|
|
290
|
+
"2024-01-05 10:30:00.123456",
|
|
291
|
+
"2024-01-05T10:30:00.123456789",
|
|
292
|
+
]) {
|
|
293
|
+
const v = paramToGiven("timestamp", raw);
|
|
294
|
+
expect(v instanceof Date && v.toISOString()).toBe(
|
|
295
|
+
"2024-01-05T10:30:00.123Z",
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
describe("paramToGiven temporal edges", () => {
|
|
302
|
+
it("passes through a date whose parts do not exist rather than rolling it over", () => {
|
|
303
|
+
// Date.UTC normalises instead of refusing, so without the parts check
|
|
304
|
+
// this arrives as 2025-02-14 AND is reported back out, rewriting the
|
|
305
|
+
// reader's URL to a date they never typed.
|
|
306
|
+
for (const raw of ["2024-13-45", "2024-99-99", "2024-01-01 99:99"]) {
|
|
307
|
+
expect(paramToGiven("date", raw)).toBe(raw);
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("passes through a zone-less spelling it cannot read as UTC", () => {
|
|
312
|
+
// `new Date("12/25/2024")` is LOCAL midnight, so this resolved to a
|
|
313
|
+
// different instant for every reader.
|
|
314
|
+
expect(paramToGiven("timestamp", "12/25/2024")).toBe("12/25/2024");
|
|
315
|
+
expect(paramToGiven("timestamp", "Dec 25 2024")).toBe("Dec 25 2024");
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("still reads a value that states its own zone", () => {
|
|
319
|
+
const v = paramToGiven("timestamp", "2024-12-25T00:00:00Z");
|
|
320
|
+
expect(v instanceof Date && v.toISOString()).toBe(
|
|
321
|
+
"2024-12-25T00:00:00.000Z",
|
|
322
|
+
);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("accepts the boundaries the check must not reject", () => {
|
|
326
|
+
for (const raw of ["2024-02-29", "2024-12-31 23:59:59", "2024-01-01"]) {
|
|
327
|
+
expect(paramToGiven("date", raw)).toBeInstanceOf(Date);
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
describe("paramToGiven", () => {
|
|
333
|
+
it("leaves a filter as the filter syntax it is", () => {
|
|
334
|
+
// `filter<number>` values are filter expressions, not numbers, so the
|
|
335
|
+
// inner type must not pull them through a numeric coercion.
|
|
336
|
+
expect(paramToGiven("filter<number>", ">= 100")).toBe(">= 100");
|
|
337
|
+
expect(paramToGiven("filter<string>", "us-east, us-west")).toBe(
|
|
338
|
+
"us-east, us-west",
|
|
339
|
+
);
|
|
340
|
+
expect(paramToGiven("filter<date>", "2024-03-01")).toBe("2024-03-01");
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("reads plain types back as their type", () => {
|
|
344
|
+
expect(paramToGiven("number", "42")).toBe(42);
|
|
345
|
+
expect(paramToGiven("boolean", "true")).toBe(true);
|
|
346
|
+
expect(paramToGiven("boolean", "false")).toBe(false);
|
|
347
|
+
expect(paramToGiven("string", "Nike")).toBe("Nike");
|
|
348
|
+
expect(paramToGiven("date", "2024-03-01")).toBeInstanceOf(Date);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("leaves an unrecognized type as the raw string", () => {
|
|
352
|
+
// Including `array<…>`, which no server can emit: `malloyGivenToApi`
|
|
353
|
+
// renders a non-filter given as the bare `type.type`. Splitting on `,`
|
|
354
|
+
// here would corrupt any value containing one, for no reachable gain.
|
|
355
|
+
expect(paramToGiven("array<string>", "a,b")).toBe("a,b");
|
|
356
|
+
expect(paramToGiven("record", "x")).toBe("x");
|
|
357
|
+
expect(paramToGiven(undefined, "x")).toBe("x");
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("passes a nonsense value through for the server to reject", () => {
|
|
361
|
+
// A URL is hand-editable. A NaN or an Invalid Date would reach the server
|
|
362
|
+
// as null and read as "unset"; the raw text gets an error naming the
|
|
363
|
+
// given instead, which is the more useful failure.
|
|
364
|
+
expect(paramToGiven("number", "abc")).toBe("abc");
|
|
365
|
+
// `Number("")` is 0, not NaN, so an empty parameter would otherwise
|
|
366
|
+
// arrive as a deliberate zero and filter on it. It now reads as UNSET,
|
|
367
|
+
// which serves that purpose better: `""` was still being sent.
|
|
368
|
+
expect(paramToGiven("number", "")).toBeNull();
|
|
369
|
+
expect(paramToGiven("number", " ")).toBe(" ");
|
|
370
|
+
expect(paramToGiven("date", "not-a-date")).toBe("not-a-date");
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
describe("paramsToGivens", () => {
|
|
375
|
+
const declared = new Map<string, string | undefined>([
|
|
376
|
+
["REGION", "filter<string>"],
|
|
377
|
+
["LIMIT", "number"],
|
|
378
|
+
]);
|
|
379
|
+
|
|
380
|
+
it("reads the declared givens out of a URL", () => {
|
|
381
|
+
expect(
|
|
382
|
+
paramsToGivens({ REGION: "us-east", LIMIT: "10" }, declared),
|
|
383
|
+
).toEqual(
|
|
384
|
+
new Map<string, GivenValue>([
|
|
385
|
+
["REGION", "us-east"],
|
|
386
|
+
["LIMIT", 10],
|
|
387
|
+
]),
|
|
388
|
+
);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("ignores a parameter the dashboard does not declare", () => {
|
|
392
|
+
// Binding an undeclared given fails the query, and an unrelated query
|
|
393
|
+
// parameter on the page URL must not be able to break the dashboard.
|
|
394
|
+
expect(
|
|
395
|
+
paramsToGivens({ REGION: "us-east", utm_source: "email" }, declared),
|
|
396
|
+
).toEqual(new Map<string, GivenValue>([["REGION", "us-east"]]));
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
describe("givensToParams", () => {
|
|
401
|
+
it("drops unset givens so a default view has a clean URL", () => {
|
|
402
|
+
const values = new Map<string, GivenValue>([
|
|
403
|
+
["REGION", "us-east"],
|
|
404
|
+
["LIMIT", null],
|
|
405
|
+
]);
|
|
406
|
+
expect(
|
|
407
|
+
givensToParams(
|
|
408
|
+
values,
|
|
409
|
+
new Map([
|
|
410
|
+
["REGION", "filter<string>"],
|
|
411
|
+
["LIMIT", "number"],
|
|
412
|
+
]),
|
|
413
|
+
),
|
|
414
|
+
).toEqual({ REGION: "us-east" });
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
describe("givensToRequest", () => {
|
|
419
|
+
const values = new Map<string, GivenValue>([
|
|
420
|
+
["REGION", "us-east"],
|
|
421
|
+
["LIMIT", 10],
|
|
422
|
+
["SINCE", new Date("2024-03-01T04:05:06.007Z")],
|
|
423
|
+
]);
|
|
424
|
+
const types = new Map<string, string | undefined>([
|
|
425
|
+
["REGION", "filter<string>"],
|
|
426
|
+
["LIMIT", "number"],
|
|
427
|
+
["SINCE", "date"],
|
|
428
|
+
]);
|
|
429
|
+
|
|
430
|
+
it("keeps types, unlike the URL form", () => {
|
|
431
|
+
expect(givensToRequest(values, types)).toEqual({
|
|
432
|
+
REGION: "us-east",
|
|
433
|
+
LIMIT: 10,
|
|
434
|
+
SINCE: "2024-03-01",
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it("narrows to a tile's own givens", () => {
|
|
439
|
+
// A composite tile must be run with only the givens it references.
|
|
440
|
+
expect(givensToRequest(values, types, ["REGION"])).toEqual({
|
|
441
|
+
REGION: "us-east",
|
|
442
|
+
});
|
|
443
|
+
expect(givensToRequest(values, types, [])).toEqual({});
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
// The server takes a different spelling for each of the three time types and
|
|
447
|
+
// rejects the other two, so one blanket ISO string fails two of them.
|
|
448
|
+
it.each([
|
|
449
|
+
["date", "2024-03-01"],
|
|
450
|
+
["timestamp", "2024-03-01T04:05:06.007"],
|
|
451
|
+
["timestamptz", "2024-03-01T04:05:06.007Z"],
|
|
452
|
+
])("encodes a Date for a %s given", (type, expected) => {
|
|
453
|
+
const request = givensToRequest(
|
|
454
|
+
new Map<string, GivenValue>([
|
|
455
|
+
["SINCE", new Date("2024-03-01T04:05:06.007Z")],
|
|
456
|
+
]),
|
|
457
|
+
new Map([["SINCE", type]]),
|
|
458
|
+
);
|
|
459
|
+
expect(request).toEqual({ SINCE: expected });
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
describe("a Date in the URL keeps the precision its type carries", () => {
|
|
464
|
+
it("truncates a `date` given to the day, which is its whole spelling", () => {
|
|
465
|
+
expect(givenToParam(new Date("2024-03-05T14:30:00Z"), "date")).toBe(
|
|
466
|
+
"2024-03-05",
|
|
467
|
+
);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("keeps the time of day for timestamp and timestamptz", () => {
|
|
471
|
+
// Encoding every Date as `YYYY-MM-DD` truncated a timestamp given the
|
|
472
|
+
// moment it round-tripped through the address bar, so the query re-ran at
|
|
473
|
+
// midnight against a different instant than the reader had chosen.
|
|
474
|
+
const at1430 = new Date("2024-03-05T14:30:00Z");
|
|
475
|
+
expect(givenToParam(at1430, "timestamp")).toBe("2024-03-05T14:30:00.000");
|
|
476
|
+
expect(givenToParam(at1430, "timestamptz")).toBe(
|
|
477
|
+
"2024-03-05T14:30:00.000Z",
|
|
478
|
+
);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it("round-trips a timestamp through the URL without moving it", () => {
|
|
482
|
+
const at1430 = new Date("2024-03-05T14:30:00Z");
|
|
483
|
+
const param = givenToParam(at1430, "timestamp") as string;
|
|
484
|
+
const back = paramToGiven("timestamp", param) as Date;
|
|
485
|
+
expect(back.toISOString()).toBe(at1430.toISOString());
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it("uses each given's own declared type across the whole row", () => {
|
|
489
|
+
const at1430 = new Date("2024-03-05T14:30:00Z");
|
|
490
|
+
const params = givensToParams(
|
|
491
|
+
new Map([
|
|
492
|
+
["DAY", at1430],
|
|
493
|
+
["MOMENT", at1430],
|
|
494
|
+
]),
|
|
495
|
+
new Map([
|
|
496
|
+
["DAY", "date"],
|
|
497
|
+
["MOMENT", "timestamptz"],
|
|
498
|
+
]),
|
|
499
|
+
);
|
|
500
|
+
expect(params).toEqual({
|
|
501
|
+
DAY: "2024-03-05",
|
|
502
|
+
MOMENT: "2024-03-05T14:30:00.000Z",
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
describe("a naive timestamp is read as UTC, in any timezone", () => {
|
|
508
|
+
// A LIMITATION worth knowing: under TZ=UTC the correct and the broken
|
|
509
|
+
// implementations are indistinguishable, because local time IS UTC there. So
|
|
510
|
+
// these assertions only bite when the suite runs outside UTC, and a UTC CI
|
|
511
|
+
// cannot catch a regression here. Verified by hand across UTC,
|
|
512
|
+
// America/New_York, Asia/Kolkata and Pacific/Auckland; to exercise it:
|
|
513
|
+
// TZ=America/New_York bun test src/components/given/paramCodec.spec.ts
|
|
514
|
+
// Running one suite under a non-UTC zone in CI would close that gap.
|
|
515
|
+
//
|
|
516
|
+
// Each assertion below names its expected value outright rather than
|
|
517
|
+
// round-tripping, so it fails on its own terms rather than agreeing with
|
|
518
|
+
// whatever the writer did. `parseTemporal`'s own doc carries the reason the
|
|
519
|
+
// parser is built this way; this block only pins that it is.
|
|
520
|
+
it("reads the naive spelling dateToRequest emits as the same instant", () => {
|
|
521
|
+
const back = paramToGiven("timestamp", "2024-03-05T14:30:00.000") as Date;
|
|
522
|
+
expect(back.toISOString()).toBe("2024-03-05T14:30:00.000Z");
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it("still honours a zone marker when one is present", () => {
|
|
526
|
+
const utc = paramToGiven(
|
|
527
|
+
"timestamptz",
|
|
528
|
+
"2024-03-05T14:30:00.000Z",
|
|
529
|
+
) as Date;
|
|
530
|
+
expect(utc.toISOString()).toBe("2024-03-05T14:30:00.000Z");
|
|
531
|
+
const offset = paramToGiven(
|
|
532
|
+
"timestamptz",
|
|
533
|
+
"2024-03-05T14:30:00+02:00",
|
|
534
|
+
) as Date;
|
|
535
|
+
expect(offset.toISOString()).toBe("2024-03-05T12:30:00.000Z");
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it("reads a date-only value as UTC midnight", () => {
|
|
539
|
+
const day = paramToGiven("date", "2024-03-05") as Date;
|
|
540
|
+
expect(day.toISOString()).toBe("2024-03-05T00:00:00.000Z");
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it("still passes nonsense through for the server to reject", () => {
|
|
544
|
+
expect(paramToGiven("timestamp", "not-a-date")).toBe("not-a-date");
|
|
545
|
+
});
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
describe("a boolean param that is neither true nor false", () => {
|
|
549
|
+
it("passes through instead of being coerced to false", () => {
|
|
550
|
+
// A URL is hand-editable. Coercing `?ACTIVE=yes` to `false` sends the
|
|
551
|
+
// OPPOSITE of what was typed, with no complaint; the raw text gets an
|
|
552
|
+
// error naming the given, which is how the number and date cases behave.
|
|
553
|
+
expect(paramToGiven("boolean", "yes")).toBe("yes");
|
|
554
|
+
expect(paramToGiven("boolean", "1")).toBe("1");
|
|
555
|
+
expect(paramToGiven("boolean", "TRUE")).toBe("TRUE");
|
|
556
|
+
// `""` is the exception: an empty parameter means UNSET for a boolean,
|
|
557
|
+
// so it is dropped rather than passed through. Sending `{"FLAG": ""}` put
|
|
558
|
+
// a value on the wire that no boolean can be, while the checkbox showed
|
|
559
|
+
// the model default.
|
|
560
|
+
expect(paramToGiven("boolean", "")).toBeNull();
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
describe("a datetime with no zone marker, however it is spelled", () => {
|
|
565
|
+
it("reads a space-separated datetime as UTC, like the T-separated one", () => {
|
|
566
|
+
// The spelling a reader copies out of a Malloy literal or a rendered
|
|
567
|
+
// cell. Keying the detection on the literal `T` let this one through as
|
|
568
|
+
// local time, so the same shared link resolved to a different instant for
|
|
569
|
+
// every reader and the URL was rewritten to the shifted value.
|
|
570
|
+
const spaced = paramToGiven("timestamp", "2024-01-15 14:30") as Date;
|
|
571
|
+
const tee = paramToGiven("timestamp", "2024-01-15T14:30") as Date;
|
|
572
|
+
expect(spaced.toISOString()).toBe("2024-01-15T14:30:00.000Z");
|
|
573
|
+
expect(tee.toISOString()).toBe(spaced.toISOString());
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
it("still honours an explicit offset or Z", () => {
|
|
577
|
+
expect(
|
|
578
|
+
(
|
|
579
|
+
paramToGiven("timestamptz", "2024-01-15T14:30:00+02:00") as Date
|
|
580
|
+
).toISOString(),
|
|
581
|
+
).toBe("2024-01-15T12:30:00.000Z");
|
|
582
|
+
expect(
|
|
583
|
+
(
|
|
584
|
+
paramToGiven("timestamptz", "2024-01-15T14:30:00Z") as Date
|
|
585
|
+
).toISOString(),
|
|
586
|
+
).toBe("2024-01-15T14:30:00.000Z");
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
describe("an empty query parameter", () => {
|
|
591
|
+
// `?FLAG=` was reaching the request as `{"FLAG": ""}`, which no boolean,
|
|
592
|
+
// number or date can be, while the control showed the model default or an
|
|
593
|
+
// empty box. The screen and the request disagreed and nothing said so.
|
|
594
|
+
// Decided here, once, for every type: what `?X=` MEANS is a property of the
|
|
595
|
+
// type, not of the widget. The controls keep their own `value !== ""`
|
|
596
|
+
// guards, which is not duplication, because `""` still reaches them by two
|
|
597
|
+
// routes: a host passing it in directly, and this codec itself for `string`
|
|
598
|
+
// and `filter<…>`, where the empty string is a value a user can mean.
|
|
599
|
+
it("means unset for a type that has no empty value", () => {
|
|
600
|
+
for (const type of ["boolean", "number", "date", "timestamp"]) {
|
|
601
|
+
expect(paramToGiven(type, "")).toBeNull();
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
it("is dropped from the request rather than sent", () => {
|
|
606
|
+
const values = new Map([["X", paramToGiven("boolean", "")]]);
|
|
607
|
+
expect(givensToRequest(values, new Map([["X", "boolean"]]))).toEqual({});
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it("stays a real value for a string, where the user can mean it", () => {
|
|
611
|
+
expect(paramToGiven("string", "")).toBe("");
|
|
612
|
+
expect(paramToGiven("filter<string>", "")).toBe("");
|
|
613
|
+
expect(
|
|
614
|
+
givensToRequest(new Map([["X", ""]]), new Map([["X", "string"]])),
|
|
615
|
+
).toEqual({ X: "" });
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
describe("values that survive a real URL, not just a string literal", () => {
|
|
620
|
+
/** What `useSearchParams` actually hands the page for a pasted value. */
|
|
621
|
+
const throughUrl = (raw: string) =>
|
|
622
|
+
new URLSearchParams(`T=${raw}`).get("T") as string;
|
|
623
|
+
|
|
624
|
+
it("reads an eastern offset, which the URL turns into a space", () => {
|
|
625
|
+
// `+` in a query string IS a space, so a hand-pasted `+05:00` arrives as
|
|
626
|
+
// ` 05:00`. Refusing it made the parser work for the western half of the
|
|
627
|
+
// world only: `-05:00` survived the address bar and `+05:00` did not.
|
|
628
|
+
const v = paramToGiven(
|
|
629
|
+
"timestamp",
|
|
630
|
+
throughUrl("2024-01-05T10:30:00+05:00"),
|
|
631
|
+
);
|
|
632
|
+
expect(v instanceof Date && v.toISOString()).toBe(
|
|
633
|
+
"2024-01-05T05:30:00.000Z",
|
|
634
|
+
);
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
it("agrees with itself east and west", () => {
|
|
638
|
+
const east = paramToGiven(
|
|
639
|
+
"timestamp",
|
|
640
|
+
throughUrl("2024-01-05T10:30:00+05"),
|
|
641
|
+
);
|
|
642
|
+
const west = paramToGiven(
|
|
643
|
+
"timestamp",
|
|
644
|
+
throughUrl("2024-01-05T10:30:00-05"),
|
|
645
|
+
);
|
|
646
|
+
expect(east instanceof Date && east.toISOString()).toBe(
|
|
647
|
+
"2024-01-05T05:30:00.000Z",
|
|
648
|
+
);
|
|
649
|
+
expect(west instanceof Date && west.toISOString()).toBe(
|
|
650
|
+
"2024-01-05T15:30:00.000Z",
|
|
651
|
+
);
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it("does not read a stray trailing number as an offset", () => {
|
|
655
|
+
// The rewrite only fires when a FULL datetime precedes the group.
|
|
656
|
+
expect(paramToGiven("timestamp", "2024-01-05 10")).toBe("2024-01-05 10");
|
|
657
|
+
});
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
describe("offsets that exist, and offsets that do not", () => {
|
|
661
|
+
it("refuses a westward offset past -12:00", () => {
|
|
662
|
+
// The range is not symmetric: -12:00 (Baker Island) to +14:00 (Kiribati).
|
|
663
|
+
for (const raw of [
|
|
664
|
+
"2024-01-05T10:30:00-1300",
|
|
665
|
+
"2024-01-05T10:30:00-1400",
|
|
666
|
+
]) {
|
|
667
|
+
expect(paramToGiven("timestamp", raw)).toBe(raw);
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
it("still accepts both real extremes", () => {
|
|
672
|
+
const east = paramToGiven("timestamp", "2024-01-05T10:30:00+1400");
|
|
673
|
+
const west = paramToGiven("timestamp", "2024-01-05T10:30:00-1200");
|
|
674
|
+
expect(east instanceof Date && east.toISOString()).toBe(
|
|
675
|
+
"2024-01-04T20:30:00.000Z",
|
|
676
|
+
);
|
|
677
|
+
expect(west instanceof Date && west.toISOString()).toBe(
|
|
678
|
+
"2024-01-05T22:30:00.000Z",
|
|
679
|
+
);
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
describe("a `date` given and the zone rules that apply to it", () => {
|
|
684
|
+
it("refuses one that shifts the calendar day", () => {
|
|
685
|
+
// Resolving it lands on the 4th, and because the value is reported back
|
|
686
|
+
// outward the reader's own URL was rewritten to a day they never wrote.
|
|
687
|
+
for (const raw of [
|
|
688
|
+
"2024-01-05T00:30:00+05:30",
|
|
689
|
+
"2024-01-05T23:30:00-05:00",
|
|
690
|
+
"2024-01-05+05",
|
|
691
|
+
]) {
|
|
692
|
+
expect(paramToGiven("date", raw)).toBe(raw);
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
it("accepts one that leaves the day alone", () => {
|
|
697
|
+
// Refusing EVERY zone was the previous attempt and it was far too broad:
|
|
698
|
+
// it rejected `2024-01-05T00:00:00.000Z`, which is exactly what
|
|
699
|
+
// `Date#toISOString()` emits, so any host or link generator serializing a
|
|
700
|
+
// JS date produced a value the control painted red.
|
|
701
|
+
for (const raw of [
|
|
702
|
+
"2024-01-05T00:00:00.000Z",
|
|
703
|
+
"2024-01-05T00:00:00Z",
|
|
704
|
+
"2024-01-05 10:00:00 UTC",
|
|
705
|
+
"2024-01-05T10:00:00+00:00",
|
|
706
|
+
"2024-01-05T10:00:00+05:00",
|
|
707
|
+
]) {
|
|
708
|
+
const v = paramToGiven("date", raw);
|
|
709
|
+
expect(v instanceof Date && v.toISOString().slice(0, 10)).toBe(
|
|
710
|
+
"2024-01-05",
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
it("resolves a raw `+` on a bare date as a time of day, not an offset", () => {
|
|
716
|
+
// The docs are written in query-string notation, so this is the form a
|
|
717
|
+
// reader actually types. Verifying the rule with a string LITERAL missed
|
|
718
|
+
// it: `?SINCE=2024-01-05+00:00` is delivered as `2024-01-05 00:00`, a
|
|
719
|
+
// bare date plus a valid time, and resolves rather than being refused.
|
|
720
|
+
// Same class as the defect the `+` repair exists for: a value behaves
|
|
721
|
+
// differently as a string literal than it does through a URL.
|
|
722
|
+
const delivered = new URLSearchParams("T=2024-01-05+00:00").get(
|
|
723
|
+
"T",
|
|
724
|
+
) as string;
|
|
725
|
+
expect(delivered).toBe("2024-01-05 00:00");
|
|
726
|
+
const v = paramToGiven("date", delivered);
|
|
727
|
+
expect(v instanceof Date && v.toISOString()).toBe(
|
|
728
|
+
"2024-01-05T00:00:00.000Z",
|
|
729
|
+
);
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
it("refuses any zone on a BARE date, day-preserving or not", () => {
|
|
733
|
+
// A separate rule from the day-shift one, and easy to state backwards: an
|
|
734
|
+
// offset qualifies a time of day, and a bare date has none, so `Z` and
|
|
735
|
+
// `+00:00` are refused here too.
|
|
736
|
+
for (const raw of [
|
|
737
|
+
"2024-01-05Z",
|
|
738
|
+
"2024-01-05 UTC",
|
|
739
|
+
"2024-01-05+00:00",
|
|
740
|
+
"2024-01-05-00:00",
|
|
741
|
+
]) {
|
|
742
|
+
expect(paramToGiven("date", raw)).toBe(raw);
|
|
743
|
+
}
|
|
744
|
+
// With a time on it, a day-preserving zone reads normally.
|
|
745
|
+
expect(paramToGiven("date", "2024-01-05T00:00:00Z")).toBeInstanceOf(Date);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it("reports a refused value back the way it was typed", () => {
|
|
749
|
+
// Not in the mangled space form the URL delivered, or the reader's own
|
|
750
|
+
// address bar loses the `+` on the next report.
|
|
751
|
+
const delivered = new URLSearchParams("T=2024-01-05T00:30:00+05:30").get(
|
|
752
|
+
"T",
|
|
753
|
+
) as string;
|
|
754
|
+
expect(paramToGiven("date", delivered)).toBe("2024-01-05T00:30:00+05:30");
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
it("still reads the spellings a date can actually have", () => {
|
|
758
|
+
for (const raw of ["2024-01-05", "2024-01-05 10:30:00"]) {
|
|
759
|
+
expect(paramToGiven("date", raw)).toBeInstanceOf(Date);
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
it("leaves timestamp types resolving their zone as before", () => {
|
|
764
|
+
for (const type of ["timestamp", "timestamptz"]) {
|
|
765
|
+
const v = paramToGiven(type, "2024-01-05T00:30:00+05:30");
|
|
766
|
+
expect(v instanceof Date && v.toISOString()).toBe(
|
|
767
|
+
"2024-01-04T19:00:00.000Z",
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
});
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
describe("the empty-parameter rule agrees with the type switch", () => {
|
|
774
|
+
it('keeps `""` for any type the switch passes through untouched', () => {
|
|
775
|
+
// As an allowlist this named three types and treated everything else as
|
|
776
|
+
// unset, while the switch's `default` arm passes an unrecognised type
|
|
777
|
+
// through: a deliberate `""` on such a type did not survive the round trip.
|
|
778
|
+
for (const type of ["json", "unknown_future_type"]) {
|
|
779
|
+
expect(paramToGiven(type, "")).toBe("");
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
it("still reads it as unset for the types with no empty value", () => {
|
|
784
|
+
for (const type of [
|
|
785
|
+
"boolean",
|
|
786
|
+
"number",
|
|
787
|
+
"date",
|
|
788
|
+
"timestamp",
|
|
789
|
+
"timestamptz",
|
|
790
|
+
]) {
|
|
791
|
+
expect(paramToGiven(type, "")).toBeNull();
|
|
792
|
+
}
|
|
793
|
+
});
|
|
794
|
+
});
|
|
795
|
+
|
|
796
|
+
describe("a bare date with a zone, which is a rule of its own", () => {
|
|
797
|
+
it("is refused for every temporal type, not just `date`", () => {
|
|
798
|
+
// The guard lives in `parseTemporal`, above the type-specific day check,
|
|
799
|
+
// so it is not a `date` rule. Kept in its own describe for that reason: a
|
|
800
|
+
// refactor of the `date` block must not carry away the only coverage of a
|
|
801
|
+
// guard that is type-independent.
|
|
802
|
+
for (const type of ["date", "timestamp", "timestamptz"]) {
|
|
803
|
+
expect(paramToGiven(type, "2024-01-05Z")).toBe("2024-01-05Z");
|
|
804
|
+
expect(paramToGiven(type, "2024-01-05+00:00")).toBe(
|
|
805
|
+
"2024-01-05+00:00",
|
|
806
|
+
);
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
it("does not apply once the value carries a time", () => {
|
|
811
|
+
// The distinction the docs and the `case "date"` comment both dropped:
|
|
812
|
+
// an offset needs a time of day to qualify, so the same zone reads
|
|
813
|
+
// normally as soon as there is one.
|
|
814
|
+
for (const type of ["date", "timestamp", "timestamptz"]) {
|
|
815
|
+
expect(paramToGiven(type, "2024-01-05T00:00:00Z")).toBeInstanceOf(
|
|
816
|
+
Date,
|
|
817
|
+
);
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
describe("whitespace, which is not the same as empty", () => {
|
|
823
|
+
it("does not collapse a whitespace-only temporal parameter to empty", () => {
|
|
824
|
+
// `restoreUrlPlus` trims to line its pattern up, and returning the
|
|
825
|
+
// trimmed text turned ` ` into `""` AFTER the empty-means-unset check had
|
|
826
|
+
// already let it past. The given was then sent as `""`: an empty date the
|
|
827
|
+
// control showed as unset, so the screen and the query disagreed.
|
|
828
|
+
for (const type of ["date", "timestamp", "timestamptz"]) {
|
|
829
|
+
expect(paramToGiven(type, " ")).toBe(" ");
|
|
830
|
+
expect(paramToGiven(type, "\t ")).toBe("\t ");
|
|
831
|
+
expect(paramToGiven(type, "")).toBeNull();
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
it("reads leading whitespace the same way whether or not a `+` was eaten", () => {
|
|
836
|
+
// These two differ only in whether the URL ate an offset, and they used
|
|
837
|
+
// to disagree: the restoring path trimmed and the non-restoring path did
|
|
838
|
+
// not, so a leading space was fatal to one and harmless to the other.
|
|
839
|
+
expect(paramToGiven("timestamp", " 2024-01-05T10:30:00Z")).toEqual(
|
|
840
|
+
new Date("2024-01-05T10:30:00.000Z"),
|
|
841
|
+
);
|
|
842
|
+
expect(paramToGiven("timestamp", " 2024-01-05T10:30:00 05:00")).toEqual(
|
|
843
|
+
new Date("2024-01-05T05:30:00.000Z"),
|
|
844
|
+
);
|
|
845
|
+
// Trailing was always fine, and stays fine.
|
|
846
|
+
expect(paramToGiven("date", "2024-01-05 ")).toEqual(
|
|
847
|
+
new Date("2024-01-05T00:00:00.000Z"),
|
|
848
|
+
);
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
it("still puts back a `+` the address bar ate", () => {
|
|
852
|
+
// The trim above exists for this, so it has to keep working.
|
|
853
|
+
expect(paramToGiven("timestamptz", "2024-03-01 12:30 05:30")).toEqual(
|
|
854
|
+
new Date("2024-03-01T07:00:00.000Z"),
|
|
855
|
+
);
|
|
856
|
+
});
|
|
857
|
+
});
|