@malloy-publisher/sdk 0.0.248 → 0.0.249

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 (98) hide show
  1. package/README.md +32 -17
  2. package/dist/ServerProvider-CcfeBpt3.cjs.js +1 -0
  3. package/dist/{ServerProvider-BxEh3nFI.es.js → ServerProvider-EqBlMMqx.es.js} +1056 -905
  4. package/dist/client/api.d.ts +297 -6
  5. package/dist/client/index.cjs.js +1 -1
  6. package/dist/client/index.es.js +74 -70
  7. package/dist/components/Dashboard/Dashboard.d.ts +49 -0
  8. package/dist/components/Dashboard/DashboardTile.d.ts +48 -0
  9. package/dist/components/Dashboard/index.d.ts +2 -0
  10. package/dist/components/Notebook/Notebook.d.ts +46 -8
  11. package/dist/components/Notebook/NotebookCell.d.ts +37 -3
  12. package/dist/components/Notebook/runPlan.d.ts +29 -0
  13. package/dist/components/Notebook/types.d.ts +10 -0
  14. package/dist/components/Package/ContentTypeIcon.d.ts +17 -1
  15. package/dist/components/Package/Package.d.ts +1 -4
  16. package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
  17. package/dist/components/RenderedResult/ResultContainer.d.ts +3 -1
  18. package/dist/components/ResultsDialog.d.ts +4 -1
  19. package/dist/components/ServerProvider.d.ts +2 -1
  20. package/dist/components/click_helper.d.ts +10 -1
  21. package/dist/components/drill/index.d.ts +3 -0
  22. package/dist/components/drill/markDrillableCells.d.ts +55 -0
  23. package/dist/components/drill/resolveDrill.d.ts +159 -0
  24. package/dist/components/drill/useDrill.d.ts +63 -0
  25. package/dist/components/given/GivenInput.d.ts +27 -3
  26. package/dist/components/given/GivensPanel.d.ts +36 -7
  27. package/dist/components/given/filterValue.d.ts +117 -0
  28. package/dist/components/given/index.d.ts +1 -1
  29. package/dist/components/given/paramCodec.d.ts +60 -0
  30. package/dist/components/index.d.ts +4 -1
  31. package/dist/components/styles.d.ts +23 -0
  32. package/dist/constants/docLinks.d.ts +6 -0
  33. package/dist/{core-BuvlzSIX.es.js → core-DFBwNhvb.es.js} +1 -1
  34. package/dist/{core-C86Rb7tJ.cjs.js → core-DZk7z7ko.cjs.js} +1 -1
  35. package/dist/hooks/givenValue.d.ts +10 -0
  36. package/dist/hooks/index.d.ts +3 -1
  37. package/dist/hooks/useGivensState.d.ts +64 -0
  38. package/dist/hooks/useSuggestOptions.d.ts +78 -0
  39. package/dist/{index-BdtAZ5k6.es.js → index-CLcRrkxd.es.js} +41221 -39894
  40. package/dist/index-CWQXhe0D.cjs.js +338 -0
  41. package/dist/index.cjs.js +1 -1
  42. package/dist/index.es.js +77 -62
  43. package/dist/theme/types.d.ts +9 -0
  44. package/package.json +3 -1
  45. package/src/components/Connections/AddConnectionDialog.tsx +10 -1
  46. package/src/components/Dashboard/Dashboard.tsx +395 -0
  47. package/src/components/Dashboard/DashboardTile.tsx +150 -0
  48. package/src/components/Dashboard/index.ts +2 -0
  49. package/src/components/Home/Home.tsx +114 -23
  50. package/src/components/Notebook/Notebook.tsx +444 -390
  51. package/src/components/Notebook/NotebookCell.tsx +117 -3
  52. package/src/components/Notebook/runPlan.spec.ts +152 -0
  53. package/src/components/Notebook/runPlan.ts +64 -0
  54. package/src/components/Notebook/types.ts +11 -0
  55. package/src/components/Package/ContentTypeIcon.spec.ts +88 -0
  56. package/src/components/Package/ContentTypeIcon.tsx +77 -6
  57. package/src/components/Package/Package.tsx +148 -38
  58. package/src/components/RenderedResult/RenderedResult.tsx +309 -19
  59. package/src/components/RenderedResult/ResultContainer.tsx +28 -1
  60. package/src/components/ResultsDialog.tsx +5 -0
  61. package/src/components/ServerProvider.tsx +2 -0
  62. package/src/components/click_helper.ts +14 -1
  63. package/src/components/drill/index.ts +25 -0
  64. package/src/components/drill/markDrillableCells.dom.spec.ts +310 -0
  65. package/src/components/drill/markDrillableCells.spec.ts +106 -0
  66. package/src/components/drill/markDrillableCells.ts +311 -0
  67. package/src/components/drill/resolveDrill.spec.ts +446 -0
  68. package/src/components/drill/resolveDrill.ts +372 -0
  69. package/src/components/drill/useDrill.tsx +200 -0
  70. package/src/components/given/GivenInput.spec.tsx +495 -15
  71. package/src/components/given/GivenInput.tsx +562 -30
  72. package/src/components/given/GivensPanel.spec.tsx +172 -0
  73. package/src/components/given/GivensPanel.tsx +139 -32
  74. package/src/components/given/filterValue.spec.ts +361 -0
  75. package/src/components/given/filterValue.ts +204 -0
  76. package/src/components/given/index.ts +5 -1
  77. package/src/components/given/paramCodec.spec.ts +854 -0
  78. package/src/components/given/paramCodec.ts +480 -0
  79. package/src/components/index.ts +4 -1
  80. package/src/components/styles.ts +27 -3
  81. package/src/constants/docLinks.spec.ts +51 -0
  82. package/src/constants/docLinks.ts +26 -0
  83. package/src/hooks/givenValue.ts +10 -0
  84. package/src/hooks/index.ts +6 -4
  85. package/src/hooks/useGivensState.spec.ts +446 -0
  86. package/src/hooks/useGivensState.ts +337 -0
  87. package/src/hooks/useSuggestOptions.spec.ts +109 -0
  88. package/src/hooks/useSuggestOptions.ts +260 -0
  89. package/src/theme/buildTableCssVars.ts +4 -0
  90. package/src/theme/resolveTheme.spec.ts +13 -0
  91. package/src/theme/resolveTheme.ts +3 -0
  92. package/src/theme/types.ts +9 -0
  93. package/test/README.md +16 -10
  94. package/dist/ServerProvider-Dg5S1qBU.cjs.js +0 -1
  95. package/dist/hooks/useGivensForm.d.ts +0 -35
  96. package/dist/index-MES4MGpQ.cjs.js +0 -309
  97. package/src/hooks/useGivensForm.spec.ts +0 -99
  98. package/src/hooks/useGivensForm.ts +0 -107
@@ -0,0 +1,446 @@
1
+ import {
2
+ NumberFilterExpression,
3
+ StringFilterExpression,
4
+ } from "@malloydata/malloy-filter";
5
+ import { parseAnnotation } from "@malloydata/malloy-tag";
6
+ import { describe, expect, it } from "bun:test";
7
+ import {
8
+ drillDestinations,
9
+ drillGivenName,
10
+ encodeDrillValue,
11
+ drillValueToFilter,
12
+ humanizeSlug,
13
+ resolveDrill,
14
+ type DrillClickPayload,
15
+ } from "./resolveDrill";
16
+
17
+ /**
18
+ * A click payload whose field carries the given annotation lines, parsed the
19
+ * way the renderer parses them. Going through the real tag parser rather than a
20
+ * hand-built stub is the point: the grammar is the contract with Malloyyo, and a
21
+ * stub would agree with itself no matter what MOTLY actually accepts.
22
+ */
23
+ function click(
24
+ annotations: string[],
25
+ value: unknown,
26
+ options: {
27
+ name?: string;
28
+ isHeader?: boolean;
29
+ wasDimension?: boolean;
30
+ } = {},
31
+ ): DrillClickPayload {
32
+ return {
33
+ field: {
34
+ name: options.name ?? "brand_name",
35
+ tag: parseAnnotation(annotations).tag,
36
+ wasDimension:
37
+ options.wasDimension === undefined
38
+ ? undefined
39
+ : () => options.wasDimension as boolean,
40
+ },
41
+ value,
42
+ isHeader: options.isHeader,
43
+ };
44
+ }
45
+
46
+ const DRILL_TO_TWO = '# drill { to=["overview", "regions"] given=BRAND }\n';
47
+
48
+ describe("resolveDrill", () => {
49
+ it("reads destinations, the given override, and the clicked value", () => {
50
+ expect(resolveDrill(click([DRILL_TO_TWO], "Nike"))).toEqual({
51
+ to: ["overview", "regions"],
52
+ given: "BRAND",
53
+ value: "Nike",
54
+ rawValue: "Nike",
55
+ label: "Nike",
56
+ });
57
+ });
58
+
59
+ it("uses the dimension name verbatim when the tag names no given", () => {
60
+ const intent = resolveDrill(
61
+ click(["# drill { to=overview }\n"], "US", { name: "region_name" }),
62
+ );
63
+ // NOT upper-cased. Given names are matched exactly, so upper-casing here
64
+ // made `to=self` on a lowercase-named given resolve to nothing at all.
65
+ expect(intent?.given).toBe("region_name");
66
+ // A lone destination may be written unbracketed, as Malloyyo allows.
67
+ expect(intent?.to).toEqual(["overview"]);
68
+ });
69
+
70
+ it("passes `self` through as a destination for the host to interpret", () => {
71
+ expect(resolveDrill(click(["# drill { to=self }\n"], "EU"))?.to).toEqual([
72
+ "self",
73
+ ]);
74
+ });
75
+
76
+ it("ignores a click on a field with no drill tag", () => {
77
+ expect(resolveDrill(click([], "Nike"))).toBeUndefined();
78
+ expect(
79
+ resolveDrill(click(["# currency\n"], 10, { name: "amount" })),
80
+ ).toBeUndefined();
81
+ });
82
+
83
+ it("ignores a header click, which names a field rather than a value", () => {
84
+ expect(
85
+ resolveDrill(click([DRILL_TO_TWO], "Nike", { isHeader: true })),
86
+ ).toBeUndefined();
87
+ });
88
+
89
+ it("ignores a drill tag that names no destination", () => {
90
+ // The server lints this as an authoring error; clicking it must not
91
+ // navigate to nowhere in the meantime.
92
+ expect(resolveDrill(click(["# drill\n"], "Nike"))).toBeUndefined();
93
+ expect(
94
+ resolveDrill(click(["# drill { to=[] }\n"], "Nike")),
95
+ ).toBeUndefined();
96
+ });
97
+
98
+ it("ignores clicks with nothing to resolve", () => {
99
+ expect(resolveDrill(undefined)).toBeUndefined();
100
+ expect(resolveDrill({})).toBeUndefined();
101
+ });
102
+
103
+ it("ignores an aggregate that inherited the tag", () => {
104
+ // `# drill` is declared on a dimension, but a measure defined over it can
105
+ // carry the annotation through. Its cell holds a total, not the value the
106
+ // filter would name, so clicking it must not navigate.
107
+ expect(
108
+ resolveDrill(click([DRILL_TO_TWO], "Nike", { wasDimension: false })),
109
+ ).toBeUndefined();
110
+ expect(
111
+ resolveDrill(click([DRILL_TO_TWO], "Nike", { wasDimension: true })),
112
+ ).toBeDefined();
113
+ });
114
+
115
+ it("escapes a clicked value that would otherwise re-parse as two", () => {
116
+ // The whole reason the value goes through the filter encoder: a comma in
117
+ // the data would silently become "either of these" in the filter.
118
+ const intent = resolveDrill(click([DRILL_TO_TWO], "Ben & Jerry, Inc"));
119
+ expect(intent).toEqual({
120
+ to: ["overview", "regions"],
121
+ given: "BRAND",
122
+ value: "Ben\\ &\\ Jerry\\,\\ Inc",
123
+ rawValue: "Ben & Jerry, Inc",
124
+ // The label stays human: it is only ever shown, never parsed.
125
+ label: "Ben & Jerry, Inc",
126
+ });
127
+ // And the encoding is checked against the parser that will read it,
128
+ // rather than against our own idea of what it should look like.
129
+ expect(StringFilterExpression.parse(intent!.value).parsed).toEqual({
130
+ operator: "=",
131
+ values: ["Ben & Jerry, Inc"],
132
+ });
133
+ });
134
+
135
+ it("declines a value it cannot express, rather than filtering by the wrong thing", () => {
136
+ expect(resolveDrill(click([DRILL_TO_TWO], null))).toBeUndefined();
137
+ expect(resolveDrill(click([DRILL_TO_TWO], undefined))).toBeUndefined();
138
+ expect(
139
+ resolveDrill(click([DRILL_TO_TWO], { nested: 1 })),
140
+ ).toBeUndefined();
141
+ });
142
+ });
143
+
144
+ describe("drillDestinations", () => {
145
+ // The affordance reads destinations off field metadata, before any click, so
146
+ // it has to answer the same as the click path: otherwise a cell reads as
147
+ // clickable and then does nothing, or the reverse.
148
+ const field = (annotations: string[], wasDimension?: boolean) => ({
149
+ name: "brand_name",
150
+ tag: parseAnnotation(annotations).tag,
151
+ wasDimension: wasDimension === undefined ? undefined : () => wasDimension,
152
+ });
153
+
154
+ it("agrees with resolveDrill about what drills", () => {
155
+ expect(drillDestinations(field([DRILL_TO_TWO]))).toEqual([
156
+ "overview",
157
+ "regions",
158
+ ]);
159
+ expect(drillDestinations(field(["# drill { to=self }\n"]))).toEqual([
160
+ "self",
161
+ ]);
162
+ expect(drillDestinations(field(["# drill { to=overview }\n"]))).toEqual([
163
+ "overview",
164
+ ]);
165
+ expect(drillDestinations(field([]))).toEqual([]);
166
+ expect(drillDestinations(field(["# drill\n"]))).toEqual([]);
167
+ expect(drillDestinations(field([DRILL_TO_TWO], false))).toEqual([]);
168
+ expect(drillDestinations(undefined)).toEqual([]);
169
+ });
170
+ });
171
+
172
+ describe("humanizeSlug", () => {
173
+ it("reads a slug as a sentence, the way Malloyyo labels the same menu", () => {
174
+ expect(humanizeSlug("category_detail")).toBe("Category detail");
175
+ expect(humanizeSlug("brand-explorer")).toBe("Brand explorer");
176
+ expect(humanizeSlug("overview")).toBe("Overview");
177
+ expect(humanizeSlug("")).toBe("");
178
+ });
179
+ });
180
+
181
+ describe("drillValueToFilter", () => {
182
+ it("encodes a string as a filter list entry", () => {
183
+ expect(drillValueToFilter("Nike")).toBe("Nike");
184
+ expect(drillValueToFilter("a, b")).toBe("a\\,\\ b");
185
+ // An apostrophe is ordinary inside a filter word, so it stays bare.
186
+ expect(drillValueToFilter("Levi's")).toBe("Levi's");
187
+ // The values a hazardous cell will actually match, per the real parser.
188
+ for (const value of ["a, b", "-Nike", "100%", "null"]) {
189
+ expect(
190
+ StringFilterExpression.parse(drillValueToFilter(value)!).parsed,
191
+ ).toEqual({ operator: "=", values: [value] });
192
+ }
193
+ });
194
+
195
+ it("refuses a cell that would encode to the EMPTY filter", () => {
196
+ // The empty filter matches every row, so a drill producing one hands back
197
+ // the whole dataset dressed up as a drilled view. `encodeFilterList`
198
+ // drops any value with no non-whitespace character, and this guard used
199
+ // to test the input against `""` instead of asking the encoder, so it
200
+ // fell out of step the moment that rule grew past the empty string.
201
+ for (const blank of ["", " ", " ", "\t", "\u00a0", "\u3000"]) {
202
+ expect(drillValueToFilter(blank)).toBeUndefined();
203
+ }
204
+ // A value that merely LOOKS blank but does encode is still a drill: the
205
+ // refusal is "encodes to nothing", not "looks empty".
206
+ expect(drillValueToFilter("\u200b")).toBe("\u200b");
207
+ expect(drillValueToFilter(" Nike ")).toBe("\\ Nike\\ ");
208
+ });
209
+
210
+ it("leaves a number bare, which the number grammar reads as the value", () => {
211
+ // Not a negation: checked against the real parser, because `-2.5` looks
212
+ // exactly like the `-` that negates in the *string* grammar.
213
+ for (const value of [100, -2.5, 0]) {
214
+ expect(
215
+ NumberFilterExpression.parse(drillValueToFilter(value)!).parsed,
216
+ ).toEqual({ operator: "=", values: [String(value)] });
217
+ }
218
+ });
219
+
220
+ it("encodes a number as equality and a boolean as itself", () => {
221
+ expect(drillValueToFilter(100)).toBe("100");
222
+ expect(drillValueToFilter(-2.5)).toBe("-2.5");
223
+ expect(drillValueToFilter(0)).toBe("0");
224
+ expect(drillValueToFilter(true)).toBe("true");
225
+ expect(drillValueToFilter(false)).toBe("false");
226
+ });
227
+
228
+ it("truncates a date to the day the cell showed", () => {
229
+ expect(drillValueToFilter(new Date("2024-03-05T13:45:00Z"))).toBe(
230
+ "2024-03-05",
231
+ );
232
+ });
233
+
234
+ it("declines what has no filter spelling", () => {
235
+ expect(drillValueToFilter(null)).toBeUndefined();
236
+ expect(drillValueToFilter(undefined)).toBeUndefined();
237
+ expect(drillValueToFilter(NaN)).toBeUndefined();
238
+ expect(drillValueToFilter(Infinity)).toBeUndefined();
239
+ expect(drillValueToFilter([1, 2])).toBeUndefined();
240
+ });
241
+ });
242
+
243
+ describe("encodeDrillValue", () => {
244
+ it("escapes for a filter given, which is what a filter parses", () => {
245
+ expect(encodeDrillValue("Ben & Jerry, Inc", "filter<string>")).toBe(
246
+ "Ben\\ &\\ Jerry\\,\\ Inc",
247
+ );
248
+ });
249
+
250
+ it("leaves the value alone for a plain string given", () => {
251
+ // The escaping is meaningful only to the filter parser. A plain `string`
252
+ // given is bound as a value, so a seeded `Ben\ &\ Jerry\,\ Inc` would
253
+ // be compared literally, backslashes and all, and match nothing.
254
+ expect(encodeDrillValue("Ben & Jerry, Inc", "string")).toBe(
255
+ "Ben & Jerry, Inc",
256
+ );
257
+ expect(encodeDrillValue("-Outerwear", "string")).toBe("-Outerwear");
258
+ expect(encodeDrillValue("100%", "string")).toBe("100%");
259
+ });
260
+
261
+ it("passes numbers and booleans through in their own type", () => {
262
+ // The STRING forms ("100", "true") would be wrong here, not merely a
263
+ // different spelling: `givensToRequest` converts only `Date`, so a string
264
+ // sent for a `number` given travels as a string and Malloy refuses it.
265
+ expect(encodeDrillValue(100, "number")).toBe(100);
266
+ expect(encodeDrillValue(-2.5, "number")).toBe(-2.5);
267
+ expect(encodeDrillValue(true, "boolean")).toBe(true);
268
+ });
269
+
270
+ it("falls back to the filter encoding when the type is unknown", () => {
271
+ // A cross-dashboard drill: the destination declares its own givens and
272
+ // this side cannot see them, so the filter spelling is the useful guess.
273
+ expect(encodeDrillValue("a, b", undefined)).toBe("a\\,\\ b");
274
+ });
275
+
276
+ it("declines a non-numeric value for a `filter<number>` given", () => {
277
+ // The number branch for PLAIN givens already refused these; the filter
278
+ // arm fell through to string-filter syntax, so a click on a text cell
279
+ // seeded `filter<number>` with `West`, which its own parser refuses, and
280
+ // the server failed every cell in the notebook.
281
+ for (const value of [
282
+ "West",
283
+ true,
284
+ new Date("2024-03-05T00:00:00Z"),
285
+ "",
286
+ ]) {
287
+ expect(encodeDrillValue(value, "filter<number>")).toBeUndefined();
288
+ }
289
+ // A number, or a string spelling one, still passes through bare, which is
290
+ // correct for the NUMBER grammar.
291
+ expect(encodeDrillValue(42, "filter<number>")).toBe("42");
292
+ expect(encodeDrillValue("42", "filter<number>")).toBe("42");
293
+ expect(encodeDrillValue(-5, "filter<number>")).toBe("-5");
294
+ });
295
+
296
+ it("escapes a number drilled into a `filter<string>` given", () => {
297
+ // The two filter grammars disagree about a leading `-`: the NUMBER
298
+ // grammar reads `-5` as the value, the STRING grammar reads it as a
299
+ // negation. Routing a number bare into a `filter<string>` therefore
300
+ // filtered to every row EXCEPT the one clicked, silently.
301
+ const encoded = encodeDrillValue(-5, "filter<string>") as string;
302
+ expect(StringFilterExpression.parse(encoded).parsed).toEqual({
303
+ operator: "=",
304
+ values: ["-5"],
305
+ });
306
+ expect(
307
+ StringFilterExpression.parse(
308
+ encodeDrillValue(-2.5, "filter<string>") as string,
309
+ ).parsed,
310
+ ).toEqual({ operator: "=", values: ["-2.5"] });
311
+ // A `filter<number>` target keeps the bare spelling, which is correct for
312
+ // ITS grammar. That difference is the whole point.
313
+ expect(encodeDrillValue(-5, "filter<number>")).toBe("-5");
314
+ });
315
+
316
+ it("declines a blank cell for a plain `string` given too", () => {
317
+ // The same reason `drillValueToFilter` refuses one: a blank cell is far
318
+ // likelier a misclick than a request for the rows that are blank. This
319
+ // used to test only `""`, so a space or a tab drilled to a value matching
320
+ // essentially nothing and the reader got an empty notebook instead of a
321
+ // declined click.
322
+ for (const blank of ["", " ", "\t", "\u00a0"]) {
323
+ expect(encodeDrillValue(blank, "string")).toBeUndefined();
324
+ }
325
+ // Padding around a real value is not blank, and is kept as clicked.
326
+ expect(encodeDrillValue(" Nike ", "string")).toBe(" Nike ");
327
+ });
328
+
329
+ it("declines a value that is not a number for a `number` given", () => {
330
+ // `Number()` accepts far more than numbers, and every extra it accepted
331
+ // became a WRONG filter rather than a refused one: a clicked Date
332
+ // encoded to its epoch milliseconds and a null cell to 0, so the notebook
333
+ // re-ran filtered to a value nobody clicked and returned nothing.
334
+ expect(
335
+ encodeDrillValue(new Date("2024-03-05T13:45:00Z"), "number"),
336
+ ).toBeUndefined();
337
+ expect(encodeDrillValue(null, "number")).toBeUndefined();
338
+ expect(encodeDrillValue(true, "number")).toBeUndefined();
339
+ expect(encodeDrillValue("", "number")).toBeUndefined();
340
+ expect(encodeDrillValue(" ", "number")).toBeUndefined();
341
+ // A number, and a string that spells one, still pass.
342
+ expect(encodeDrillValue(12, "number")).toBe(12);
343
+ expect(encodeDrillValue("12", "number")).toBe(12);
344
+ });
345
+
346
+ it("hands a temporal given the Date, so it keeps its time of day", () => {
347
+ // Falling through to `drillValueToFilter` truncated to `YYYY-MM-DD`
348
+ // first, which is right for a `date` given and drops the time of day for
349
+ // a `timestamp` one, so the drill filtered to midnight rather than to the
350
+ // instant in the cell. `givensToRequest` spells it per type instead.
351
+ const clicked = new Date("2024-03-05T13:45:00Z");
352
+ for (const type of ["date", "timestamp", "timestamptz"]) {
353
+ expect(encodeDrillValue(clicked, type)).toEqual(clicked);
354
+ }
355
+ // A filter-typed target still takes the day, which is its own rule.
356
+ expect(encodeDrillValue(clicked, "filter<date>")).toBe("2024-03-05");
357
+ });
358
+
359
+ it("declines a value with no spelling", () => {
360
+ expect(encodeDrillValue(null, "string")).toBeUndefined();
361
+ expect(encodeDrillValue(undefined, "filter<string>")).toBeUndefined();
362
+ expect(encodeDrillValue(NaN, "number")).toBeUndefined();
363
+ });
364
+ });
365
+
366
+ describe("a drill on an empty cell", () => {
367
+ it("is refused rather than encoding the match-everything filter", () => {
368
+ // `encodeFilterList` drops an empty value, so encoding one yields the
369
+ // EMPTY filter, which matches every row. A drill that widened instead of
370
+ // narrowing is worse than one that does nothing, and `null` is already
371
+ // refused on exactly that reasoning.
372
+ expect(drillValueToFilter("")).toBeUndefined();
373
+ expect(encodeDrillValue("", "filter<string>")).toBeUndefined();
374
+ expect(encodeDrillValue("", "string")).toBeUndefined();
375
+ expect(encodeDrillValue("", undefined)).toBeUndefined();
376
+ });
377
+
378
+ it("does not over-refuse a falsy value that is a real one", () => {
379
+ // The refusal is about values with no filter spelling, not about values
380
+ // that look empty. A zero and a false are ordinary cells.
381
+ expect(drillValueToFilter(0)).toBe("0");
382
+ expect(drillValueToFilter(false)).toBe("false");
383
+ });
384
+
385
+ it("refuses whitespace too, which it did not always", () => {
386
+ // This case used to assert the opposite, that `" "` still produced a
387
+ // drill, and it passed for the worst possible reason: the value it got
388
+ // back was `""`, the match-everything filter, and `toBeDefined()` is
389
+ // satisfied by it. The assertion was too weak to tell a working drill
390
+ // from the exact catastrophe the block above exists to prevent.
391
+ //
392
+ // The encoder cannot carry a whitespace-only value in this grammar (see
393
+ // filterValue.ts), so refusing is the only faithful option: a drill that
394
+ // widens to every row is worse than one that does nothing.
395
+ expect(drillValueToFilter(" ")).toBeUndefined();
396
+ expect(encodeDrillValue(" ", "filter<string>")).toBeUndefined();
397
+ });
398
+ });
399
+
400
+ describe("encodeDrillValue returns the given's own type", () => {
401
+ it("gives a number given a number, not a string", () => {
402
+ // `givensToRequest` converts only `Date`, so a string sent for a `number`
403
+ // given goes over the wire as a string and Malloy refuses the query.
404
+ expect(encodeDrillValue(100, "number")).toBe(100);
405
+ expect(encodeDrillValue("100", "number")).toBe(100);
406
+ expect(encodeDrillValue(-2.5, "number")).toBe(-2.5);
407
+ expect(encodeDrillValue("not a number", "number")).toBeUndefined();
408
+ });
409
+
410
+ it("gives a boolean given a boolean", () => {
411
+ expect(encodeDrillValue(true, "boolean")).toBe(true);
412
+ expect(encodeDrillValue(false, "boolean")).toBe(false);
413
+ expect(encodeDrillValue("true", "boolean")).toBe(true);
414
+ expect(encodeDrillValue("false", "boolean")).toBe(false);
415
+ expect(encodeDrillValue("yes", "boolean")).toBeUndefined();
416
+ });
417
+
418
+ it("still gives a string given a string and a filter given filter syntax", () => {
419
+ expect(encodeDrillValue("Outerwear", "string")).toBe("Outerwear");
420
+ expect(encodeDrillValue("a, b", "filter<string>")).toBe("a\\,\\ b");
421
+ });
422
+ });
423
+
424
+ describe("drillGivenName", () => {
425
+ it("prefers the tag's given= and otherwise uses the field name as spelled", () => {
426
+ const tagged = parseAnnotation(["# drill { to=self given=BRAND }\n"]);
427
+ expect(drillGivenName({ name: "brand_name", tag: tagged.tag })).toBe(
428
+ "BRAND",
429
+ );
430
+ const untagged = parseAnnotation(["# drill { to=self }\n"]);
431
+ expect(drillGivenName({ name: "brand_name", tag: untagged.tag })).toBe(
432
+ "brand_name",
433
+ );
434
+ });
435
+
436
+ it("matches a lowercase given declaration, which upper-casing did not", () => {
437
+ // The whole point of the change. `declaredTypes` is keyed by the name the
438
+ // model spells and probed with `.has()`, so an upper-cased lookup missed
439
+ // and the click was dropped with no affordance and no error.
440
+ const declared = new Map([["region", "string"]]);
441
+ const untagged = parseAnnotation(["# drill { to=self }\n"]);
442
+ expect(
443
+ declared.has(drillGivenName({ name: "region", tag: untagged.tag })),
444
+ ).toBe(true);
445
+ });
446
+ });