@malloy-publisher/sdk 0.12.0 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/README.md +79 -6
  2. package/dist/{RenderedResult-SgDlxzs9.cjs.js → RenderedResult-Cgw4iqev.cjs.js} +1 -1
  3. package/dist/{RenderedResult-D8bCgThO.es.js → RenderedResult-DEjFM1DL.es.js} +2 -2
  4. package/dist/ServerProvider-BRQl_cBo.cjs.js +1 -0
  5. package/dist/{ServerProvider-DBw-thY0.es.js → ServerProvider-RnliA2Ao.es.js} +1084 -723
  6. package/dist/builder/index.cjs.js +1 -1
  7. package/dist/builder/index.es.js +3 -3
  8. package/dist/client/api.d.ts +754 -41
  9. package/dist/client/index.cjs.js +1 -1
  10. package/dist/client/index.es.js +120 -110
  11. package/dist/components/Materializations/MaterializationDetailDialog.d.ts +16 -1
  12. package/dist/components/Model/ExplorerPage.d.ts +9 -0
  13. package/dist/components/Model/Model.d.ts +27 -11
  14. package/dist/components/ModelViewer/AccessChips.d.ts +34 -0
  15. package/dist/components/ModelViewer/EmptyRow.d.ts +23 -0
  16. package/dist/components/ModelViewer/Facts.d.ts +15 -0
  17. package/dist/components/ModelViewer/FieldGroup.d.ts +31 -0
  18. package/dist/components/ModelViewer/FieldRow.d.ts +69 -0
  19. package/dist/components/ModelViewer/GivenFacts.d.ts +21 -0
  20. package/dist/components/ModelViewer/HeaderBar.d.ts +75 -0
  21. package/dist/components/ModelViewer/InlineDoc.d.ts +13 -0
  22. package/dist/components/ModelViewer/ModelGivensSection.d.ts +32 -0
  23. package/dist/components/ModelViewer/ModelSection.d.ts +21 -0
  24. package/dist/components/ModelViewer/ModelViewer.d.ts +91 -0
  25. package/dist/components/ModelViewer/QueriesGroup.d.ts +15 -0
  26. package/dist/components/ModelViewer/RowGlyph.d.ts +27 -0
  27. package/dist/components/ModelViewer/RunResult.d.ts +46 -0
  28. package/dist/components/ModelViewer/SampleValues.d.ts +20 -0
  29. package/dist/components/ModelViewer/SchemaJoin.d.ts +51 -0
  30. package/dist/components/ModelViewer/SchemaView.d.ts +52 -0
  31. package/dist/components/ModelViewer/SegmentIcons.d.ts +70 -0
  32. package/dist/components/ModelViewer/SourcesDropdown.d.ts +53 -0
  33. package/dist/components/ModelViewer/SourcesNav.d.ts +25 -0
  34. package/dist/components/ModelViewer/StatusPill.d.ts +56 -0
  35. package/dist/components/ModelViewer/ValueChip.d.ts +17 -0
  36. package/dist/components/ModelViewer/ViewRow.d.ts +36 -0
  37. package/dist/components/ModelViewer/clickableBox.d.ts +29 -0
  38. package/dist/components/ModelViewer/codeDisplay.d.ts +44 -0
  39. package/dist/components/ModelViewer/docAnnotations.d.ts +90 -0
  40. package/dist/components/ModelViewer/explorerModel.d.ts +12 -0
  41. package/dist/components/ModelViewer/extensions.d.ts +94 -0
  42. package/dist/components/ModelViewer/fieldCode.d.ts +74 -0
  43. package/dist/components/ModelViewer/fieldEntities.d.ts +45 -0
  44. package/dist/components/ModelViewer/fieldExpansion.d.ts +41 -0
  45. package/dist/components/ModelViewer/givenUsage.d.ts +71 -0
  46. package/dist/components/ModelViewer/headerCard.d.ts +31 -0
  47. package/dist/components/ModelViewer/httpStatus.d.ts +4 -0
  48. package/dist/components/ModelViewer/icons.d.ts +31 -0
  49. package/dist/components/ModelViewer/index.d.ts +9 -0
  50. package/dist/components/ModelViewer/joinAccess.d.ts +52 -0
  51. package/dist/components/ModelViewer/malloyRunText.d.ts +22 -0
  52. package/dist/components/ModelViewer/malloyText.d.ts +21 -0
  53. package/dist/components/ModelViewer/modelGivens.d.ts +61 -0
  54. package/dist/components/ModelViewer/modelGivensContext.d.ts +9 -0
  55. package/dist/components/ModelViewer/modelHeaderPresentation.d.ts +28 -0
  56. package/dist/components/ModelViewer/modelTextDeclarations.d.ts +32 -0
  57. package/dist/components/ModelViewer/modelTextParse.d.ts +27 -0
  58. package/dist/components/ModelViewer/namedQueryCode.d.ts +12 -0
  59. package/dist/components/ModelViewer/offSurface.d.ts +54 -0
  60. package/dist/components/ModelViewer/rowLayers.d.ts +30 -0
  61. package/dist/components/ModelViewer/runGate.d.ts +49 -0
  62. package/dist/components/ModelViewer/schemaModel.d.ts +109 -0
  63. package/dist/components/ModelViewer/sectionOpenCommand.d.ts +45 -0
  64. package/dist/components/ModelViewer/sourceAccess.d.ts +67 -0
  65. package/dist/components/ModelViewer/sourceLineage.d.ts +14 -0
  66. package/dist/components/ModelViewer/sourcePicker.d.ts +66 -0
  67. package/dist/components/ModelViewer/surface.d.ts +47 -0
  68. package/dist/components/ModelViewer/text.d.ts +6 -0
  69. package/dist/components/ModelViewer/theme.d.ts +63 -0
  70. package/dist/components/ModelViewer/tokens.d.ts +52 -0
  71. package/dist/components/ModelViewer/ui.d.ts +48 -0
  72. package/dist/components/ModelViewer/useDimensionSamples.d.ts +76 -0
  73. package/dist/components/ModelViewer/useModelSources.d.ts +92 -0
  74. package/dist/components/ModelViewer/useRunQuery.d.ts +115 -0
  75. package/dist/components/ReportBuilder/loadMalloy.d.ts +4 -0
  76. package/dist/components/ServerProvider.d.ts +17 -2
  77. package/dist/components/filter/utils.d.ts +1 -1
  78. package/dist/components/given/GivenInput.d.ts +7 -1
  79. package/dist/components/given/GivensPanel.d.ts +3 -1
  80. package/dist/components/given/filterSummary.d.ts +6 -0
  81. package/dist/components/given/useGivenUrlParams.d.ts +11 -0
  82. package/dist/components/index.d.ts +2 -1
  83. package/dist/components/modelPage.d.ts +36 -0
  84. package/dist/hooks/useDimensionalFilterRangeData.d.ts +9 -0
  85. package/dist/hooks/useSuggestOptions.d.ts +26 -2
  86. package/dist/index.cjs.js +47 -19
  87. package/dist/index.es.js +14137 -9697
  88. package/dist/useCompiledDocument-D88_2cGS.cjs.js +143 -0
  89. package/dist/{useCompiledDocument-CXMPtJkJ.es.js → useCompiledDocument-Dmm6xkom.es.js} +6975 -6893
  90. package/dist/utils/filterCondition.d.ts +14 -0
  91. package/dist/utils/malloyLiteral.d.ts +20 -1
  92. package/package.json +2 -2
  93. package/src/components/Materializations/MaterializationDetailDialog.tsx +33 -7
  94. package/src/components/Materializations/Materializations.tsx +47 -4
  95. package/src/components/Materializations/Materializations.versions.spec.tsx +140 -1
  96. package/src/components/Model/ExplorerPage.tsx +269 -0
  97. package/src/components/Model/Model.tsx +66 -250
  98. package/src/components/Model/ModelExplorer.spec.tsx +64 -1
  99. package/src/components/ModelViewer/AccessChips.tsx +149 -0
  100. package/src/components/ModelViewer/EmptyRow.tsx +71 -0
  101. package/src/components/ModelViewer/Facts.tsx +46 -0
  102. package/src/components/ModelViewer/FieldGroup.tsx +137 -0
  103. package/src/components/ModelViewer/FieldRow.tsx +566 -0
  104. package/src/components/ModelViewer/GivenFacts.tsx +89 -0
  105. package/src/components/ModelViewer/HeaderBar.tsx +362 -0
  106. package/src/components/ModelViewer/InlineDoc.tsx +50 -0
  107. package/src/components/ModelViewer/ModelGivensSection.tsx +142 -0
  108. package/src/components/ModelViewer/ModelSection.tsx +98 -0
  109. package/src/components/ModelViewer/ModelViewer.spec.tsx +937 -0
  110. package/src/components/ModelViewer/ModelViewer.tsx +777 -0
  111. package/src/components/ModelViewer/QueriesGroup.tsx +107 -0
  112. package/src/components/ModelViewer/RowGlyph.tsx +138 -0
  113. package/src/components/ModelViewer/RunResult.tsx +298 -0
  114. package/src/components/ModelViewer/SampleValues.tsx +97 -0
  115. package/src/components/ModelViewer/SchemaJoin.tsx +258 -0
  116. package/src/components/ModelViewer/SchemaView.tsx +222 -0
  117. package/src/components/ModelViewer/SegmentIcons.tsx +129 -0
  118. package/src/components/ModelViewer/SourcesDropdown.tsx +265 -0
  119. package/src/components/ModelViewer/SourcesNav.tsx +75 -0
  120. package/src/components/ModelViewer/StatusPill.tsx +145 -0
  121. package/src/components/ModelViewer/ValueChip.tsx +39 -0
  122. package/src/components/ModelViewer/ViewRow.tsx +274 -0
  123. package/src/components/ModelViewer/clickableBox.ts +59 -0
  124. package/src/components/ModelViewer/codeDisplay.spec.ts +176 -0
  125. package/src/components/ModelViewer/codeDisplay.ts +118 -0
  126. package/src/components/ModelViewer/docAnnotations.spec.ts +181 -0
  127. package/src/components/ModelViewer/docAnnotations.ts +244 -0
  128. package/src/components/ModelViewer/docAnnotations.typeDetail.spec.ts +114 -0
  129. package/src/components/ModelViewer/explorerModel.spec.ts +43 -0
  130. package/src/components/ModelViewer/explorerModel.ts +31 -0
  131. package/src/components/ModelViewer/extensions.tsx +112 -0
  132. package/src/components/ModelViewer/fieldCode.spec.ts +168 -0
  133. package/src/components/ModelViewer/fieldCode.ts +118 -0
  134. package/src/components/ModelViewer/fieldEntities.ts +63 -0
  135. package/src/components/ModelViewer/fieldExpansion.spec.ts +116 -0
  136. package/src/components/ModelViewer/fieldExpansion.ts +95 -0
  137. package/src/components/ModelViewer/givenUsage.spec.ts +304 -0
  138. package/src/components/ModelViewer/givenUsage.ts +325 -0
  139. package/src/components/ModelViewer/headerCard.tsx +102 -0
  140. package/src/components/ModelViewer/httpStatus.ts +15 -0
  141. package/src/components/ModelViewer/icons.tsx +67 -0
  142. package/src/components/ModelViewer/index.ts +26 -0
  143. package/src/components/ModelViewer/joinAccess.spec.ts +95 -0
  144. package/src/components/ModelViewer/joinAccess.ts +54 -0
  145. package/src/components/ModelViewer/malloyRunText.spec.ts +48 -0
  146. package/src/components/ModelViewer/malloyRunText.ts +51 -0
  147. package/src/components/ModelViewer/malloyText.spec.ts +47 -0
  148. package/src/components/ModelViewer/malloyText.ts +77 -0
  149. package/src/components/ModelViewer/modelGivens.ts +228 -0
  150. package/src/components/ModelViewer/modelGivensContext.ts +13 -0
  151. package/src/components/ModelViewer/modelHeaderPresentation.spec.ts +31 -0
  152. package/src/components/ModelViewer/modelHeaderPresentation.ts +41 -0
  153. package/src/components/ModelViewer/modelTextDeclarations.spec.ts +336 -0
  154. package/src/components/ModelViewer/modelTextDeclarations.ts +228 -0
  155. package/src/components/ModelViewer/modelTextParse.ts +112 -0
  156. package/src/components/ModelViewer/namedQueryCode.spec.ts +58 -0
  157. package/src/components/ModelViewer/namedQueryCode.ts +26 -0
  158. package/src/components/ModelViewer/offSurface.tsx +124 -0
  159. package/src/components/ModelViewer/rowLayers.tsx +104 -0
  160. package/src/components/ModelViewer/runGate.spec.ts +219 -0
  161. package/src/components/ModelViewer/runGate.ts +145 -0
  162. package/src/components/ModelViewer/schemaModel.defaultSource.spec.ts +126 -0
  163. package/src/components/ModelViewer/schemaModel.queries.spec.ts +37 -0
  164. package/src/components/ModelViewer/schemaModel.ts +300 -0
  165. package/src/components/ModelViewer/sectionOpenCommand.tsx +125 -0
  166. package/src/components/ModelViewer/sourceAccess.spec.ts +130 -0
  167. package/src/components/ModelViewer/sourceAccess.ts +194 -0
  168. package/src/components/ModelViewer/sourceLineage.spec.ts +73 -0
  169. package/src/components/ModelViewer/sourceLineage.ts +38 -0
  170. package/src/components/ModelViewer/sourcePicker.spec.ts +294 -0
  171. package/src/components/ModelViewer/sourcePicker.ts +162 -0
  172. package/src/components/ModelViewer/surface.ts +51 -0
  173. package/src/components/ModelViewer/text.tsx +33 -0
  174. package/src/components/ModelViewer/theme.spec.ts +38 -0
  175. package/src/components/ModelViewer/theme.ts +58 -0
  176. package/src/components/ModelViewer/tokens.ts +87 -0
  177. package/src/components/ModelViewer/ui.tsx +201 -0
  178. package/src/components/ModelViewer/useDimensionSamples.spec.tsx +139 -0
  179. package/src/components/ModelViewer/useDimensionSamples.ts +220 -0
  180. package/src/components/ModelViewer/useModelSources.spec.ts +110 -0
  181. package/src/components/ModelViewer/useModelSources.ts +238 -0
  182. package/src/components/ModelViewer/useRunQuery.spec.ts +50 -0
  183. package/src/components/ModelViewer/useRunQuery.ts +223 -0
  184. package/src/components/Package/Package.agents.spec.tsx +5 -4
  185. package/src/components/Package/Package.tsx +8 -1
  186. package/src/components/ReportBuilder/emptyStart.spec.ts +8 -0
  187. package/src/components/ReportBuilder/loadMalloy.ts +8 -1
  188. package/src/components/ServerProvider.tsx +29 -3
  189. package/src/components/filter/utils.ts +4 -60
  190. package/src/components/given/FilterButton.tsx +4 -1
  191. package/src/components/given/GivenInput.tsx +8 -0
  192. package/src/components/given/GivensPanel.tsx +6 -1
  193. package/src/components/given/filterSummary.spec.ts +31 -1
  194. package/src/components/given/filterSummary.ts +41 -19
  195. package/src/components/given/useGivenUrlParams.spec.tsx +32 -0
  196. package/src/components/given/useGivenUrlParams.ts +49 -14
  197. package/src/components/index.ts +6 -1
  198. package/src/components/legacy/malloynb/Notebook.spec.tsx +38 -4
  199. package/src/components/legacy/malloynb/NotebookCell.tsx +8 -2
  200. package/src/components/modelPage.spec.ts +79 -0
  201. package/src/components/modelPage.ts +48 -0
  202. package/src/hooks/useDimensionFilters.ts +3 -58
  203. package/src/hooks/useDimensionFiltersQuery.ts +3 -58
  204. package/src/hooks/useDimensionalFilterRangeData.spec.ts +57 -0
  205. package/src/hooks/useDimensionalFilterRangeData.ts +8 -63
  206. package/src/hooks/useSuggestOptions.dom.spec.ts +54 -1
  207. package/src/hooks/useSuggestOptions.spec.ts +71 -0
  208. package/src/hooks/useSuggestOptions.ts +102 -12
  209. package/src/utils/filterCondition.spec.ts +87 -0
  210. package/src/utils/filterCondition.ts +66 -0
  211. package/src/utils/malloyLiteral.spec.ts +53 -1
  212. package/src/utils/malloyLiteral.ts +47 -12
  213. package/dist/ServerProvider-BHfg1Ed6.cjs.js +0 -1
  214. package/dist/useCompiledDocument-D7X_K6YU.cjs.js +0 -142
@@ -0,0 +1,219 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { extractCompiledModelGivens } from "./modelGivens";
6
+ import { runGate } from "./runGate";
7
+
8
+ const given = (name: string, dflt?: string) => ({
9
+ name,
10
+ type: "string",
11
+ modelFile: "m.malloy",
12
+ default: dflt,
13
+ });
14
+
15
+ describe("runGate", () => {
16
+ it("runs plainly when the model declares nothing the explorer can't supply", () => {
17
+ expect(runGate(undefined, undefined)).toEqual({ kind: "ok" });
18
+ expect(runGate([], [])).toEqual({ kind: "ok" });
19
+ });
20
+
21
+ it("runs at the defaults, and names them", () => {
22
+ expect(
23
+ runGate([given("ROLE", "'analyst'"), given("REGION", "'us'")], []),
24
+ ).toEqual({
25
+ kind: "noted",
26
+ note: "Ran with the defaults ROLE: analyst, REGION: us.",
27
+ });
28
+ });
29
+
30
+ it("a value the reader set is neither missing nor a default", () => {
31
+ expect(runGate([given("ROLE")], [], { ROLE: "'admin'" })).toEqual({
32
+ kind: "ok",
33
+ });
34
+ expect(
35
+ runGate([given("ROLE", "'analyst'"), given("REGION", "'us'")], [], {
36
+ ROLE: "'admin'",
37
+ }),
38
+ ).toEqual({
39
+ kind: "noted",
40
+ note: "Ran with the default REGION: us.",
41
+ });
42
+ // Absent is "use the default"; an empty string is a value, as on a
43
+ // dashboard (the control's × is what unsets it).
44
+ expect(runGate([given("ROLE")], [], {}).kind).toBe("blocked");
45
+ expect(runGate([given("ROLE")], [], { ROLE: "" }).kind).toBe("ok");
46
+ });
47
+
48
+ it("refuses when a given has no default or a filter is required, and says what is missing", () => {
49
+ expect(runGate([given("ROLE")], [])).toEqual({
50
+ kind: "blocked",
51
+ canRunAnyway: false,
52
+ reason:
53
+ "This needs a value for the given ROLE. Set it in the Givens section above.",
54
+ });
55
+ expect(runGate([], ["Customer ID"])).toEqual({
56
+ kind: "blocked",
57
+ canRunAnyway: false,
58
+ reason:
59
+ "This needs the required filter Customer ID, which this page can't set; open the source in a dashboard or notebook.",
60
+ });
61
+ expect(runGate([given("A"), given("B", "1")], ["F1", "F2"])).toEqual({
62
+ kind: "blocked",
63
+ canRunAnyway: false,
64
+ reason:
65
+ "This needs a value for the given A (set it in the Givens section above) and the required filters F1, F2, which this page can't set; open the source in a dashboard or notebook.",
66
+ });
67
+ });
68
+
69
+ // The storefront's overview givens: each named as its control words it.
70
+ const category = {
71
+ ...given("CATEGORY", "f''"),
72
+ type: "filter<string>",
73
+ label: "Category",
74
+ };
75
+ const since = {
76
+ ...given("SINCE", "@2023-01-01"),
77
+ type: "date",
78
+ label: "Ordered since",
79
+ };
80
+ const minSale = {
81
+ ...given("MIN_SALE", "f''"),
82
+ type: "filter<number>",
83
+ label: "Minimum line total",
84
+ };
85
+ const region = { ...given("REGION"), label: "Region" };
86
+
87
+ it("names a default the way its control does, never as a Malloy literal", () => {
88
+ expect(runGate([category, since, minSale], [])).toEqual({
89
+ kind: "noted",
90
+ note: "Ran with the defaults Category: All, Ordered since: Jan 1, 2023, Minimum line total: Any.",
91
+ });
92
+ expect(runGate([region], [])).toEqual({
93
+ kind: "blocked",
94
+ canRunAnyway: false,
95
+ reason:
96
+ "This needs a value for the given Region. Set it in the Givens section above.",
97
+ });
98
+ });
99
+
100
+ it("says it can't tell which givens a source reads, and what the run had for each", () => {
101
+ const all = new Set(["CATEGORY", "REGION", "SINCE"]);
102
+ expect(
103
+ runGate([category, region, since], [], { REGION: "West" }, all),
104
+ ).toEqual({
105
+ kind: "noted",
106
+ note: "This page can't tell which givens this source reads. Where it reads one, it ran with Region: West and the defaults Category: All, Ordered since: Jan 1, 2023.",
107
+ });
108
+ expect(runGate([region], [], { REGION: "West" }, all)).toEqual({
109
+ kind: "noted",
110
+ note: "This page can't tell which givens this source reads. Where it reads one, it ran with Region: West.",
111
+ });
112
+ // A given with no value still holds the run: if the source reads it, a
113
+ // run without it fails with no control to fix it.
114
+ expect(runGate([region], [], undefined, all)).toEqual({
115
+ kind: "blocked",
116
+ canRunAnyway: true,
117
+ reason:
118
+ "This needs a value for the given Region. Set it in the Givens section above. This page can't tell whether this source reads it.",
119
+ });
120
+ expect(runGate([], [], undefined, all)).toEqual({ kind: "ok" });
121
+ });
122
+
123
+ it("names apart only the givens it can't tie to the source", () => {
124
+ // The source reads REGION in its own text and is built on an import that
125
+ // may read CATEGORY and SINCE.
126
+ const maybe = new Set(["CATEGORY", "SINCE"]);
127
+ expect(
128
+ runGate([category, region, since], [], { REGION: "West" }, maybe),
129
+ ).toEqual({
130
+ kind: "noted",
131
+ note: "This page can't tell whether this source reads Category or Ordered since. Where it does, it ran with the defaults Category: All, Ordered since: Jan 1, 2023.",
132
+ });
133
+ expect(runGate([category, region, since], [], undefined, maybe)).toEqual({
134
+ kind: "blocked",
135
+ canRunAnyway: false,
136
+ reason:
137
+ "This needs a value for the given Region. Set it in the Givens section above.",
138
+ });
139
+ expect(
140
+ runGate([category, { ...minSale, default: undefined }], [], {}, maybe),
141
+ ).toEqual({
142
+ kind: "blocked",
143
+ canRunAnyway: false,
144
+ reason:
145
+ "This needs a value for the given Minimum line total. Set it in the Givens section above.",
146
+ });
147
+ expect(runGate([category, region], [], {}, new Set(["REGION"]))).toEqual({
148
+ kind: "blocked",
149
+ canRunAnyway: true,
150
+ reason:
151
+ "This needs a value for the given Region. Set it in the Givens section above. This page can't tell whether this source reads it.",
152
+ });
153
+ });
154
+
155
+ it("runs anyway, at the reader's word, without givens it can't tie to the source", () => {
156
+ const maybe = new Set(["REGION", "CATEGORY"]);
157
+ expect(runGate([region, category], [], {}, maybe, true)).toEqual({
158
+ kind: "noted",
159
+ note: "Ran without a value for the given Region, which this page can't tell this source reads. This page can't tell which givens this source reads. Where it reads one, it ran with the default Category: All.",
160
+ });
161
+ // Never for a given the source is known to read, nor past a required
162
+ // filter: those runs fail, with nothing the reader can do.
163
+ expect(runGate([region], [], {}, new Set(), true)).toMatchObject({
164
+ kind: "blocked",
165
+ canRunAnyway: false,
166
+ });
167
+ expect(
168
+ runGate([region], ["Customer ID"], {}, new Set(["REGION"]), true),
169
+ ).toMatchObject({ kind: "blocked", canRunAnyway: false });
170
+ });
171
+
172
+ it("names a named query with no source as the query, not a source", () => {
173
+ expect(
174
+ runGate(
175
+ [region],
176
+ [],
177
+ { REGION: "West" },
178
+ new Set(["REGION"]),
179
+ false,
180
+ "query",
181
+ ),
182
+ ).toEqual({
183
+ kind: "noted",
184
+ note: "This page can't tell which givens this query reads. Where it reads one, it ran with Region: West.",
185
+ });
186
+ });
187
+
188
+ it("names every value a run had, never cut to a button's width", () => {
189
+ const picked = "Outerwear,Footwear,Dresses,Swimwear";
190
+ expect(
191
+ runGate(
192
+ [{ ...category, control: "multiselect" as const }],
193
+ [],
194
+ { CATEGORY: picked },
195
+ new Set(["CATEGORY"]),
196
+ ),
197
+ ).toEqual({
198
+ kind: "noted",
199
+ note: "This page can't tell which givens this source reads. Where it reads one, it ran with Category: Outerwear, Footwear, Dresses, Swimwear.",
200
+ });
201
+ });
202
+ });
203
+
204
+ describe("givens the host fills", () => {
205
+ it("are left out of the model's givens, so a run never blocks on one", () => {
206
+ // A gate's GROUPS is declared with no default (a gate may not read a
207
+ // defaulted given), and the host supplies it on every query.
208
+ const givens = extractCompiledModelGivens(
209
+ "m.malloy",
210
+ { givens: [given("GROUPS"), given("MIN", "100")] },
211
+ new Set(["GROUPS"]),
212
+ );
213
+ expect(givens.map((g) => g.name)).toEqual(["MIN"]);
214
+ expect(runGate(givens, [])).toEqual({
215
+ kind: "noted",
216
+ note: "Ran with the default MIN: 100.",
217
+ });
218
+ });
219
+ });
@@ -0,0 +1,145 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describeGivenValue } from "../given/filterSummary";
5
+ import type { GivenInfo, GivenRequest, GivenRequestValue } from "./modelGivens";
6
+
7
+ /**
8
+ * Whether a run from the explorer can be honest about its inputs.
9
+ *
10
+ * A run sends the given values the reader set in the page's Givens section
11
+ * (the dashboard's own controls). A given the reader left unset falls back
12
+ * to its declared default, and the card names every default it ran at, the
13
+ * way a report's panel shows the values that produced its cells. An unset
14
+ * given with no default, or a `#(filter … required)` on the source (the
15
+ * explorer has no filter controls), cannot run at all, so the card says what
16
+ * is missing instead of showing a bare 400.
17
+ *
18
+ * Each given is named as its control on the page words it, `Ordered since:
19
+ * Jan 1, 2023`, never as a Malloy literal. A given the page can't tie to the
20
+ * source either way (`unknown`) is named apart: the card says it can't tell
21
+ * whether the source reads it, and what the run had for it where it does,
22
+ * rather than calling its default one the run ran at. Values are named whole
23
+ * (`describeGivenValue`), never cut to a button's width: the note is the record
24
+ * of what produced the result.
25
+ *
26
+ * A run held only for givens the page can't tie to the source can run anyway
27
+ * (`canRunAnyway`), at the reader's word: if the source does read one, the run
28
+ * fails, and the card has already said why.
29
+ */
30
+ export type RunGate =
31
+ | { kind: "ok" }
32
+ | { kind: "noted"; note: string }
33
+ | { kind: "blocked"; reason: string; canRunAnyway: boolean };
34
+
35
+ /** `A`, `A or B`, `A, B or C`. */
36
+ export function wordList(
37
+ words: readonly string[],
38
+ conjunction: string,
39
+ ): string {
40
+ if (words.length <= 1) return words.join("");
41
+ return `${words.slice(0, -1).join(", ")} ${conjunction} ${words.at(-1)}`;
42
+ }
43
+
44
+ /** A given and its value as its control words them, whole: `Region: West`, `Category: All`. */
45
+ function givenWords(given: GivenInfo, value?: GivenRequestValue): string {
46
+ return `${given.label ?? given.name}: ${describeGivenValue(given, value)}`;
47
+ }
48
+
49
+ const labelOf = (given: GivenInfo) => given.label ?? given.name;
50
+
51
+ export function runGate(
52
+ givens: ReadonlyArray<GivenInfo> | undefined,
53
+ requiredFilters: ReadonlyArray<string> | undefined,
54
+ /** Values the reader set, as a run sends them; an absent entry means "use the
55
+ * default". An empty string is a value, as on a dashboard (for a
56
+ * `filter<…>`, "All"). */
57
+ values: GivenRequest | undefined = undefined,
58
+ /** Names of the givens the page can't tie to the source either way
59
+ * (scanGivensForSource). */
60
+ unknown: ReadonlySet<string> | undefined = undefined,
61
+ /** The reader chose to run without the missing givens the page can't tie
62
+ * to the source; honored only when those are all that holds the run. */
63
+ runAnyway = false,
64
+ /** What the run reads from: its source, or a named query with none. */
65
+ subject: "source" | "query" = "source",
66
+ ): RunGate {
67
+ // A given the host fills is never in `givens`: extractCompiledModelGivens
68
+ // leaves it out.
69
+ const all = givens ?? [];
70
+ const isSet = (g: GivenInfo) => values?.[g.name] !== undefined;
71
+ const isUnknown = (g: GivenInfo) => unknown?.has(g.name) === true;
72
+ const missing = all.filter((g) => !isSet(g) && g.default === undefined);
73
+ const required = requiredFilters ?? [];
74
+ const canRunAnyway =
75
+ required.length === 0 &&
76
+ missing.length > 0 &&
77
+ missing.every((g) => isUnknown(g));
78
+ const skipped = runAnyway && canRunAnyway ? missing : [];
79
+ if (skipped.length === 0 && (missing.length > 0 || required.length > 0)) {
80
+ // Two different remedies, said separately: a given can be set in the
81
+ // panel above this card; a required filter has no control here.
82
+ const givenPart =
83
+ missing.length > 0
84
+ ? `a value for ${missing.length === 1 ? "the given" : "the givens"} ${missing.map(labelOf).join(", ")}`
85
+ : "";
86
+ const filterPart =
87
+ required.length > 0
88
+ ? `${required.length === 1 ? "the required filter" : "the required filters"} ${required.join(", ")}`
89
+ : "";
90
+ // A run that may not read the given still waits for it: sent without one
91
+ // it does read, it fails with no control to fix it.
92
+ const maybeMissing = missing.filter(isUnknown);
93
+ const maybe =
94
+ maybeMissing.length === 0
95
+ ? ""
96
+ : maybeMissing.length === missing.length
97
+ ? ` This page can't tell whether this ${subject} reads ${missing.length === 1 ? "it" : "them"}.`
98
+ : ` This page can't tell whether this ${subject} reads ${wordList(maybeMissing.map(labelOf), "or")}.`;
99
+ let reason: string;
100
+ if (filterPart === "") {
101
+ reason = `This needs ${givenPart}. Set ${missing.length === 1 ? "it" : "them"} in the Givens section above.${maybe}`;
102
+ } else if (givenPart === "") {
103
+ reason = `This needs ${filterPart}, which this page can't set; open the source in a dashboard or notebook.`;
104
+ } else {
105
+ reason = `This needs ${givenPart} (set ${missing.length === 1 ? "it" : "them"} in the Givens section above) and ${filterPart}, which this page can't set; open the source in a dashboard or notebook.${maybe}`;
106
+ }
107
+ return { kind: "blocked", reason, canRunAnyway };
108
+ }
109
+ const defaultsOf = (list: GivenInfo[]) =>
110
+ list.length > 0
111
+ ? `${list.length === 1 ? "the default" : "the defaults"} ${list
112
+ .map((g) => givenWords(g))
113
+ .join(", ")}`
114
+ : "";
115
+ const notes: string[] = [];
116
+ if (skipped.length > 0) {
117
+ notes.push(
118
+ `Ran without a value for ${skipped.length === 1 ? "the given" : "the givens"} ${wordList(skipped.map(labelOf), "and")}, which this page can't tell this ${subject} reads.`,
119
+ );
120
+ }
121
+ const ran = all.filter((g) => !skipped.includes(g));
122
+ const known = ran.filter((g) => !isUnknown(g));
123
+ const knownDefaults = defaultsOf(known.filter((g) => !isSet(g)));
124
+ if (knownDefaults !== "") notes.push(`Ran with ${knownDefaults}.`);
125
+ const maybe = ran.filter(isUnknown);
126
+ if (maybe.length > 0) {
127
+ const had = [
128
+ maybe
129
+ .filter(isSet)
130
+ .map((g) => givenWords(g, values?.[g.name]))
131
+ .join(", "),
132
+ defaultsOf(maybe.filter((g) => !isSet(g))),
133
+ ]
134
+ .filter((part) => part !== "")
135
+ .join(" and ");
136
+ notes.push(
137
+ maybe.length === ran.length
138
+ ? `This page can't tell which givens this ${subject} reads. Where it reads one, it ran with ${had}.`
139
+ : `This page can't tell whether this ${subject} reads ${wordList(maybe.map(labelOf), "or")}. Where it does, it ran with ${had}.`,
140
+ );
141
+ }
142
+ return notes.length > 0
143
+ ? { kind: "noted", note: notes.join(" ") }
144
+ : { kind: "ok" };
145
+ }
@@ -0,0 +1,126 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { pickDefaultSource } from "./schemaModel";
6
+ import type { ExplorerSource } from "./schemaModel";
7
+
8
+ /**
9
+ * A source with `views` many views. View NAMES are namespaced per source, so
10
+ * two unrelated sources never look like one extends the other — which is what
11
+ * a shared name would mean (see pickDefaultSource). `extendsOf` opts a fixture
12
+ * into that relationship explicitly by inheriting the base's names.
13
+ */
14
+ function src(
15
+ name: string,
16
+ views: number,
17
+ extendsOf?: ExplorerSource,
18
+ ): ExplorerSource {
19
+ const inherited = extendsOf?.views.map((v) => v.name) ?? [];
20
+ const own = Array.from(
21
+ { length: views - inherited.length },
22
+ (_, i) => `${name}_v${i}`,
23
+ );
24
+ return {
25
+ name,
26
+ accent: "steel",
27
+ calculations: [],
28
+ dimensions: [],
29
+ measures: [],
30
+ views: [...inherited, ...own].map((n) => ({
31
+ name: n,
32
+ kind: "view" as const,
33
+ })),
34
+ joins: [],
35
+ };
36
+ }
37
+
38
+ describe("pickDefaultSource", () => {
39
+ // The two shapes that motivated the change, taken from the shipped samples:
40
+ // both declare a lookup table first and their subject last.
41
+ it("opens flights.malloy on flights, not the carriers lookup it declares first", () => {
42
+ const sources = [
43
+ src("carriers", 0),
44
+ src("airports", 1),
45
+ src("aircraft", 0),
46
+ src("flights", 6),
47
+ ];
48
+ expect(pickDefaultSource(sources, "faa/flights.malloy")?.name).toBe(
49
+ "flights",
50
+ );
51
+ });
52
+
53
+ it("opens imdb.malloy on movies — the file has no namesake source at all", () => {
54
+ const sources = [
55
+ src("people", 1),
56
+ src("principals", 0),
57
+ src("movies", 5),
58
+ ];
59
+ expect(pickDefaultSource(sources, "imdb/imdb.malloy")?.name).toBe(
60
+ "movies",
61
+ );
62
+ });
63
+
64
+ it("breaks a views tie toward the file's namesake", () => {
65
+ const sources = [src("carriers", 2), src("flights", 2)];
66
+ expect(pickDefaultSource(sources, "flights.malloy")?.name).toBe(
67
+ "flights",
68
+ );
69
+ });
70
+
71
+ it("falls back to declaration order when nothing distinguishes the sources", () => {
72
+ const sources = [src("alpha", 0), src("beta", 0)];
73
+ expect(pickDefaultSource(sources, "gamma.malloy")?.name).toBe("alpha");
74
+ });
75
+
76
+ it("keeps the most-viewed source even when another matches the filename", () => {
77
+ // Namesake is the TIEBREAK, not the rule: a namesake with fewer views
78
+ // does not beat the source the model is actually written around.
79
+ const sources = [src("orders", 9), src("sales", 1)];
80
+ expect(pickDefaultSource(sources, "sales.malloy")?.name).toBe("orders");
81
+ });
82
+
83
+ it("prefers the base over a source extended from it", () => {
84
+ // ga4.malloy: `purchase_events is events extend { … }` carries events'
85
+ // views PLUS its own, so on a raw count the narrow slice outranks the
86
+ // subject of the file.
87
+ const events = src("events", 1);
88
+ const purchase = src("purchase_events", 3, events);
89
+ expect(
90
+ pickDefaultSource([events, purchase], "ga4/ga4.malloy")?.name,
91
+ ).toBe("events");
92
+ });
93
+
94
+ it("picks the earliest base in a chain, not the widest leaf", () => {
95
+ // a <- b <- c, all three listed: only `a` extends nothing, so it wins
96
+ // despite having the fewest views. The file is about its base.
97
+ const a = src("a", 1);
98
+ const b = src("b", 2, a);
99
+ const c = src("c", 3, b);
100
+ expect(pickDefaultSource([a, b, c], "x.malloy")?.name).toBe("a");
101
+ });
102
+
103
+ it("falls back to the count when every listed source extends another", () => {
104
+ // Two siblings off the same hidden base (imported, or not exported):
105
+ // each contains the other's inherited view, so neither is a base and the
106
+ // pool is the whole list.
107
+ const hidden = src("hidden", 1);
108
+ const b = src("b", 2, hidden);
109
+ const c = src("c", 2, hidden);
110
+ // c gains a view b lacks, so it is the wider of two equals.
111
+ c.views.push({ name: "c_extra", kind: "view" });
112
+ expect(pickDefaultSource([b, c], "x.malloy")?.name).toBe("c");
113
+ });
114
+
115
+ it("treats - and _ as the same character in a namesake tie", () => {
116
+ const sources = [src("other", 2), src("purchase_events", 2)];
117
+ expect(pickDefaultSource(sources, "purchase-events.malloy")?.name).toBe(
118
+ "purchase_events",
119
+ );
120
+ });
121
+
122
+ it("survives an unknown path and an empty list", () => {
123
+ expect(pickDefaultSource([src("only", 0)], undefined)?.name).toBe("only");
124
+ expect(pickDefaultSource([], "x.malloy")).toBeNull();
125
+ });
126
+ });
@@ -0,0 +1,37 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { toExplorerQueries } from "./schemaModel";
6
+
7
+ describe("toExplorerQueries", () => {
8
+ it("maps a named query's doc and renderer tags from the publisher's raw annotation lines", () => {
9
+ const [q] = toExplorerQueries([
10
+ {
11
+ name: "genre_movie_map",
12
+ sourceName: undefined,
13
+ annotations: [
14
+ "#(doc) Movies by genre, as a map.\n",
15
+ "# bar_chart\n",
16
+ ],
17
+ },
18
+ ]);
19
+ expect(q).toEqual({
20
+ name: "genre_movie_map",
21
+ doc: "Movies by genre, as a map.",
22
+ tags: ["bar_chart"],
23
+ });
24
+ });
25
+
26
+ it("drops entries the publisher left unnamed and tolerates a missing list", () => {
27
+ expect(toExplorerQueries([{ name: undefined }])).toEqual([]);
28
+ expect(toExplorerQueries(undefined)).toEqual([]);
29
+ });
30
+
31
+ it("yields no doc and no tags for an unannotated query", () => {
32
+ const [q] = toExplorerQueries([{ name: "plain" }]);
33
+ expect(q.name).toBe("plain");
34
+ expect(q.doc).toBeUndefined();
35
+ expect(q.tags ?? []).toEqual([]);
36
+ });
37
+ });