@malloy-publisher/sdk 0.3.0 → 0.3.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 +8 -0
- package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
- package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
- package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
- package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
- package/dist/ServerProvider-CH71GsoL.cjs.js +1 -0
- package/dist/{ServerProvider-DSK9rfSY.es.js → ServerProvider-X2Ma0HRs.es.js} +486 -521
- package/dist/builder/index.cjs.js +105 -0
- package/dist/builder/index.d.ts +2 -0
- package/dist/builder/index.es.js +9561 -0
- package/dist/builder-entry.d.ts +8 -0
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/Dashboard/Dashboard.d.ts +19 -14
- package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
- package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
- package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
- package/dist/components/Dashboard/TileCard.d.ts +44 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/Dashboard/promoteMeasureRow.d.ts +7 -3
- package/dist/components/Dashboard/telemetry.d.ts +43 -0
- package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
- package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
- package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
- package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
- package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
- package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
- package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
- package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
- package/dist/components/DashboardBuilder/controls.d.ts +157 -0
- package/dist/components/DashboardBuilder/diff.d.ts +35 -0
- package/dist/components/DashboardBuilder/document.d.ts +218 -0
- package/dist/components/DashboardBuilder/layout.d.ts +71 -0
- package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
- package/dist/components/DashboardBuilder/preview.d.ts +60 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
- package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
- package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
- package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
- package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
- package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
- package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
- package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
- package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
- package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +7 -0
- package/dist/components/DocumentStorage/index.d.ts +1 -1
- package/dist/components/FloatingIconButton.d.ts +16 -0
- package/dist/components/Prose.d.ts +46 -0
- package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
- package/dist/components/RenderedResult/index.d.ts +1 -0
- package/dist/components/drill/index.d.ts +1 -1
- package/dist/components/drill/resolveDrill.d.ts +22 -0
- package/dist/components/drill/useDrill.d.ts +11 -2
- package/dist/components/index.d.ts +1 -0
- package/dist/components/styles.d.ts +0 -4
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/useDocumentControls.d.ts +70 -0
- package/dist/hooks/useQueryResult.d.ts +40 -0
- package/dist/index.cjs.js +23 -270
- package/dist/index.es.js +16977 -41293
- package/dist/utils/malloyLiteral.d.ts +11 -0
- package/package.json +24 -9
- package/src/builder-entry.ts +55 -0
- package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
- package/src/components/Dashboard/Dashboard.tsx +165 -175
- package/src/components/Dashboard/DashboardGrid.tsx +124 -0
- package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
- package/src/components/Dashboard/DashboardTile.tsx +66 -123
- package/src/components/Dashboard/ExploreDialog.tsx +84 -0
- package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
- package/src/components/Dashboard/RowsDialog.tsx +234 -0
- package/src/components/Dashboard/TileCard.tsx +120 -0
- package/src/components/Dashboard/index.ts +2 -0
- package/src/components/Dashboard/promoteMeasureRow.spec.ts +70 -0
- package/src/components/Dashboard/promoteMeasureRow.ts +33 -2
- package/src/components/Dashboard/telemetry.ts +44 -0
- package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
- package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
- package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
- package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
- package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
- package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
- package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
- package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
- package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
- package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
- package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
- package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
- package/src/components/DashboardBuilder/catalog.ts +225 -0
- package/src/components/DashboardBuilder/controls.spec.ts +243 -0
- package/src/components/DashboardBuilder/controls.ts +398 -0
- package/src/components/DashboardBuilder/diff.spec.ts +44 -0
- package/src/components/DashboardBuilder/diff.ts +93 -0
- package/src/components/DashboardBuilder/document.ts +209 -0
- package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
- package/src/components/DashboardBuilder/layout.ts +143 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
- package/src/components/DashboardBuilder/malloyText.ts +151 -0
- package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
- package/src/components/DashboardBuilder/preview.spec.ts +149 -0
- package/src/components/DashboardBuilder/preview.ts +172 -0
- package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
- package/src/components/DashboardBuilder/readDocument.ts +467 -0
- package/src/components/DashboardBuilder/sortable.tsx +151 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
- package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
- package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
- package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
- package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
- package/src/components/DashboardBuilder/useDraft.ts +43 -0
- package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
- package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
- package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
- package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
- package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
- package/src/components/DashboardBuilder/useTileResize.ts +110 -0
- package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
- package/src/components/DocumentStorage/DocumentStorageProvider.tsx +12 -0
- package/src/components/DocumentStorage/index.ts +1 -0
- package/src/components/Environment/About.tsx +4 -36
- package/src/components/FloatingIconButton.tsx +41 -0
- package/src/components/Model/Model.tsx +6 -23
- package/src/components/Model/ModelCell.tsx +33 -72
- package/src/components/Notebook/Notebook.tsx +25 -88
- package/src/components/Notebook/NotebookCell.tsx +35 -183
- package/src/components/Prose.spec.tsx +85 -0
- package/src/components/Prose.tsx +179 -0
- package/src/components/QueryResult/QueryResult.tsx +15 -43
- package/src/components/RenderedResult/ResultContainer.tsx +4 -12
- package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
- package/src/components/RenderedResult/ResultPanel.tsx +73 -0
- package/src/components/RenderedResult/index.ts +1 -0
- package/src/components/drill/index.ts +1 -0
- package/src/components/drill/resolveDrill.ts +22 -1
- package/src/components/drill/useDrill.spec.tsx +94 -0
- package/src/components/drill/useDrill.tsx +52 -12
- package/src/components/given/GivensPanel.tsx +1 -1
- package/src/components/index.ts +6 -0
- package/src/components/styles.ts +0 -37
- package/src/hooks/index.ts +13 -0
- package/src/hooks/useDocumentControls.ts +170 -0
- package/src/hooks/useQueryResult.ts +82 -0
- package/src/utils/malloyLiteral.spec.ts +43 -0
- package/src/utils/malloyLiteral.ts +76 -0
- package/vite.config.ts +5 -0
- package/dist/ServerProvider-CGnLjzag.cjs.js +0 -1
|
@@ -0,0 +1,1108 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import * as fs from "fs";
|
|
6
|
+
import * as path from "path";
|
|
7
|
+
import type { DashboardDocument } from "./document";
|
|
8
|
+
import { givenDeclarations } from "./malloyText";
|
|
9
|
+
import { blockAbove, readDashboardDocument, readFailed } from "./readDocument";
|
|
10
|
+
import { spliceDashboardDocument, spliceFailed } from "./spliceDocument";
|
|
11
|
+
import { openDocument, refused, splice, spliced } from "./testing/fixtures";
|
|
12
|
+
|
|
13
|
+
const REPO = path.resolve(import.meta.dir, "../../../../..");
|
|
14
|
+
|
|
15
|
+
const SOURCE = `##! experimental.givens
|
|
16
|
+
|
|
17
|
+
##" A narrative header.
|
|
18
|
+
## artifact { title="Probe" tiles=["a -> by_cat", "a -> by_brand"] } dashboard { columns=12 }
|
|
19
|
+
import "../data_app.malloy"
|
|
20
|
+
|
|
21
|
+
source: a is scoped_orders extend {
|
|
22
|
+
// Why this tile leads: revenue is the number people ask about first.
|
|
23
|
+
# colspan=6
|
|
24
|
+
# break
|
|
25
|
+
# label="By category"
|
|
26
|
+
view: by_cat is by_category
|
|
27
|
+
|
|
28
|
+
# colspan=6
|
|
29
|
+
view: by_brand is by_brand_view
|
|
30
|
+
}`;
|
|
31
|
+
|
|
32
|
+
describe("spliceDashboardDocument: what it preserves", () => {
|
|
33
|
+
// The whole reason for splicing rather than regenerating. A generated file
|
|
34
|
+
// could not carry this comment, and the previous design's answer was to
|
|
35
|
+
// refuse to edit any file that had one.
|
|
36
|
+
it("leaves a comment in the tag block exactly where it was", async () => {
|
|
37
|
+
const out = await spliced(SOURCE, (d) => {
|
|
38
|
+
d.tiles[0].colspan = 4;
|
|
39
|
+
});
|
|
40
|
+
expect(out).toContain(
|
|
41
|
+
" // Why this tile leads: revenue is the number people ask about first.",
|
|
42
|
+
);
|
|
43
|
+
expect(out).toContain(" # colspan=4");
|
|
44
|
+
expect(out).not.toContain(
|
|
45
|
+
'# colspan=6\n # break\n # label="By category"\n view: by_cat',
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// A render tag on the tile — `# big_value`, `# currency` — is the renderer's,
|
|
50
|
+
// not the document's. Measured on the storefront overview before this was
|
|
51
|
+
// pinned: unticking one filter on the KPI strip deleted its `# big_value`,
|
|
52
|
+
// and the strip came back as a one-row table.
|
|
53
|
+
it("keeps the tags it does not model when it rewrites the ones it does", async () => {
|
|
54
|
+
const source = SOURCE.replace(
|
|
55
|
+
' # label="By category"\n',
|
|
56
|
+
' # label="By category"\n # big_value\n # currency\n',
|
|
57
|
+
);
|
|
58
|
+
const out = await spliced(source, (d) => {
|
|
59
|
+
d.tiles[0].label = "Categories";
|
|
60
|
+
d.tiles[0].filters = [{ field: "cat", given: "CATEGORY" }];
|
|
61
|
+
});
|
|
62
|
+
expect(out).toContain(
|
|
63
|
+
' # label="Categories"\n # big_value\n # currency\n',
|
|
64
|
+
);
|
|
65
|
+
expect(out).toContain(
|
|
66
|
+
"view: by_cat is by_category + { where: cat ~ $CATEGORY }",
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Byte-minimal: one property changed, one line different.
|
|
71
|
+
it("changes only the line it had to", async () => {
|
|
72
|
+
const out = await spliced(SOURCE, (d) => {
|
|
73
|
+
d.tiles[0].label = "Categories";
|
|
74
|
+
});
|
|
75
|
+
const before = SOURCE.split("\n");
|
|
76
|
+
const after = out.split("\n");
|
|
77
|
+
const differing = after.filter((line, i) => line !== before[i]);
|
|
78
|
+
expect(differing).toEqual([' # label="Categories"']);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("leaves the file untouched when nothing changed", async () => {
|
|
82
|
+
const out = await spliced(SOURCE, () => {});
|
|
83
|
+
expect(out).toBe(SOURCE);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("spliceDashboardDocument: what it writes", () => {
|
|
88
|
+
it("adds a tag that was not there", async () => {
|
|
89
|
+
const out = await spliced(SOURCE, (d) => {
|
|
90
|
+
d.tiles[1].label = "By brand";
|
|
91
|
+
});
|
|
92
|
+
expect(out).toContain(' # label="By brand"\n view: by_brand');
|
|
93
|
+
expect(await openDocument(out)).toMatchObject({
|
|
94
|
+
tiles: [{ name: "by_cat" }, { name: "by_brand", label: "By brand" }],
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("removes a tag that is no longer wanted", async () => {
|
|
99
|
+
const out = await spliced(SOURCE, (d) => {
|
|
100
|
+
delete d.tiles[0].break;
|
|
101
|
+
});
|
|
102
|
+
expect(out).not.toContain("# break");
|
|
103
|
+
expect((await openDocument(out)).tiles[0].break).toBeUndefined();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// The filter binding lives in the declaration, as a refinement.
|
|
107
|
+
it("writes and clears a filter binding", async () => {
|
|
108
|
+
const bound = await spliced(SOURCE, (d) => {
|
|
109
|
+
d.tiles[0].filters = [{ field: "products.brand", given: "BRAND" }];
|
|
110
|
+
});
|
|
111
|
+
expect(bound).toContain(
|
|
112
|
+
"view: by_cat is by_category + { where: products.brand ~ $BRAND }",
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const cleared = await spliced(bound, (d) => {
|
|
116
|
+
delete d.tiles[0].filters;
|
|
117
|
+
});
|
|
118
|
+
expect(cleared).toContain("view: by_cat is by_category\n");
|
|
119
|
+
expect(cleared).not.toContain("where:");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("spliceDashboardDocument: reordering", () => {
|
|
124
|
+
// Order lives in the `tiles=[…]` array, not in where a view is declared, so
|
|
125
|
+
// a reorder rewrites that array and moves nothing else.
|
|
126
|
+
it("reorders by rewriting the artifact tag's tiles array", async () => {
|
|
127
|
+
const r = await splice(SOURCE, (d) => {
|
|
128
|
+
d.tiles.reverse();
|
|
129
|
+
});
|
|
130
|
+
expect(r.ok).toBe(true);
|
|
131
|
+
if (!spliceFailed(r)) {
|
|
132
|
+
expect(r.source).toContain('tiles=["a -> by_brand", "a -> by_cat"]');
|
|
133
|
+
// The declarations stayed exactly where they were.
|
|
134
|
+
expect(r.source.indexOf("view: by_cat")).toBeLessThan(
|
|
135
|
+
r.source.indexOf("view: by_brand"),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("leaves a tile's comment with the tile it was written above", async () => {
|
|
141
|
+
const r = await splice(SOURCE, (d) => {
|
|
142
|
+
d.tiles.reverse();
|
|
143
|
+
});
|
|
144
|
+
expect(r.ok).toBe(true);
|
|
145
|
+
if (!spliceFailed(r)) {
|
|
146
|
+
// The comment is the reason reordering used to be refused. It never
|
|
147
|
+
// moves, because no declaration does.
|
|
148
|
+
expect(r.source).toContain(
|
|
149
|
+
"// Why this tile leads: revenue is the number people ask about first.\n # colspan=6",
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("keeps every tile's own tags through a reorder", async () => {
|
|
155
|
+
const r = await splice(SOURCE, (d) => {
|
|
156
|
+
d.tiles.reverse();
|
|
157
|
+
});
|
|
158
|
+
expect(r.ok).toBe(true);
|
|
159
|
+
if (!spliceFailed(r)) {
|
|
160
|
+
const back = await openDocument(r.source);
|
|
161
|
+
expect(back.tiles.map((t) => t.name)).toEqual(["by_brand", "by_cat"]);
|
|
162
|
+
// The writer persists the flags the DOCUMENT carries and takes no
|
|
163
|
+
// view of what they mean: this document still has `break` on
|
|
164
|
+
// `by_cat`, so the file does too. Deciding that a row start stays
|
|
165
|
+
// with the POSITION rather than the tile is the builder's rule
|
|
166
|
+
// (`keepRowStructure`), applied before a document reaches here.
|
|
167
|
+
expect(back.tiles.find((t) => t.name === "by_cat")?.break).toBe(true);
|
|
168
|
+
expect(back.tiles.find((t) => t.name === "by_brand")?.break).toBe(
|
|
169
|
+
undefined,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("reorders and re-tags in one write", async () => {
|
|
175
|
+
const r = await splice(SOURCE, (d) => {
|
|
176
|
+
d.tiles.reverse();
|
|
177
|
+
// Matched by identity, not position: this is the tile now FIRST.
|
|
178
|
+
d.tiles[0].label = "Brands";
|
|
179
|
+
});
|
|
180
|
+
expect(r.ok).toBe(true);
|
|
181
|
+
if (!spliceFailed(r)) {
|
|
182
|
+
const back = await openDocument(r.source);
|
|
183
|
+
expect(back.tiles.map((t) => t.name)).toEqual(["by_brand", "by_cat"]);
|
|
184
|
+
expect(back.tiles[0].label).toBe("Brands");
|
|
185
|
+
expect(back.tiles[1].label).toBe("By category");
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("spliceDashboardDocument: bindings", () => {
|
|
191
|
+
it("writes a binding with the comparison it was given", async () => {
|
|
192
|
+
const out = await spliced(SOURCE, (d) => {
|
|
193
|
+
d.tiles[0].filters = [
|
|
194
|
+
{ field: "created_at", given: "SINCE", op: ">=" },
|
|
195
|
+
];
|
|
196
|
+
});
|
|
197
|
+
expect(out).toContain(
|
|
198
|
+
"view: by_cat is by_category + { where: created_at >= $SINCE }",
|
|
199
|
+
);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// The refinement is not all ours. What the reader does not model stays,
|
|
203
|
+
// ahead of the bindings, exactly as written.
|
|
204
|
+
it("keeps an unmodelled clause in the refinement", async () => {
|
|
205
|
+
const source = SOURCE.replace(
|
|
206
|
+
"view: by_cat is by_category",
|
|
207
|
+
"view: by_cat is by_category + { limit: 5, where: category ~ $CATEGORY }",
|
|
208
|
+
);
|
|
209
|
+
const out = await spliced(source, (d) => {
|
|
210
|
+
d.tiles[0].filters = [
|
|
211
|
+
{ field: "category", given: "CATEGORY" },
|
|
212
|
+
{ field: "brand", given: "BRAND" },
|
|
213
|
+
];
|
|
214
|
+
});
|
|
215
|
+
expect(out).toContain(
|
|
216
|
+
"view: by_cat is by_category + { limit: 5, where: category ~ $CATEGORY, where: brand ~ $BRAND }",
|
|
217
|
+
);
|
|
218
|
+
// And clearing the bindings leaves the clause that was never ours.
|
|
219
|
+
const cleared = await spliced(source, (d) => {
|
|
220
|
+
delete d.tiles[0].filters;
|
|
221
|
+
});
|
|
222
|
+
expect(cleared).toContain("view: by_cat is by_category + { limit: 5 }");
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("spliceDashboardDocument: the dashboard's own givens", () => {
|
|
227
|
+
// The convention: a filter the builder adds is a declaration in THIS file.
|
|
228
|
+
// A file that had none gets the experiment switch too, since a `given:`
|
|
229
|
+
// without it fails the package load.
|
|
230
|
+
it("adds a first given after the imports, switching the experiment on", async () => {
|
|
231
|
+
const source = `## artifact { title="T" tiles=["a -> x"] }
|
|
232
|
+
import { one, products } from "../m.malloy"
|
|
233
|
+
|
|
234
|
+
source: a is one extend {
|
|
235
|
+
view: x is vx
|
|
236
|
+
}`;
|
|
237
|
+
const out = await spliced(source, (d) => {
|
|
238
|
+
d.localGivens = [
|
|
239
|
+
{
|
|
240
|
+
name: "CATEGORY",
|
|
241
|
+
type: "filter<string>",
|
|
242
|
+
default: "f''",
|
|
243
|
+
label: "Category",
|
|
244
|
+
control: "select",
|
|
245
|
+
suggest: { source: "products", dimension: "category" },
|
|
246
|
+
},
|
|
247
|
+
];
|
|
248
|
+
d.tiles[0].filters = [{ field: "category", given: "CATEGORY" }];
|
|
249
|
+
});
|
|
250
|
+
expect(out.startsWith("##! experimental.givens\n")).toBe(true);
|
|
251
|
+
expect(out).toContain(
|
|
252
|
+
`import { one, products } from "../m.malloy"
|
|
253
|
+
|
|
254
|
+
# label="Category" control=select suggest { source=products dimension=category }
|
|
255
|
+
given: CATEGORY :: filter<string> is f''
|
|
256
|
+
`,
|
|
257
|
+
);
|
|
258
|
+
expect(out).toContain("view: x is vx + { where: category ~ $CATEGORY }");
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("adds a given after the ones already declared", async () => {
|
|
262
|
+
const source = `##! experimental.givens
|
|
263
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
264
|
+
import "../m.malloy"
|
|
265
|
+
|
|
266
|
+
# label="Category"
|
|
267
|
+
given: CATEGORY :: filter<string> is f''
|
|
268
|
+
|
|
269
|
+
source: a is one extend {
|
|
270
|
+
view: x is vx
|
|
271
|
+
}`;
|
|
272
|
+
const out = await spliced(source, (d) => {
|
|
273
|
+
d.localGivens = [
|
|
274
|
+
...(d.localGivens ?? []),
|
|
275
|
+
{
|
|
276
|
+
name: "SINCE",
|
|
277
|
+
type: "date",
|
|
278
|
+
default: "@2023-01-01",
|
|
279
|
+
label: "Since",
|
|
280
|
+
},
|
|
281
|
+
];
|
|
282
|
+
});
|
|
283
|
+
expect(out).toContain(`given: CATEGORY :: filter<string> is f''
|
|
284
|
+
|
|
285
|
+
# label="Since"
|
|
286
|
+
given: SINCE :: date is @2023-01-01
|
|
287
|
+
`);
|
|
288
|
+
// Exactly one switch, not a second copy.
|
|
289
|
+
expect(out.match(/##! experimental\.givens/g)).toHaveLength(1);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// Its tags are its control contract, so they go with it; a comment does not.
|
|
293
|
+
it("removes a given with its tags and leaves the comment above", async () => {
|
|
294
|
+
const source = `##! experimental.givens
|
|
295
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
296
|
+
import "../m.malloy"
|
|
297
|
+
|
|
298
|
+
// Why the page filters by category at all.
|
|
299
|
+
# label="Category" control=select
|
|
300
|
+
given: CATEGORY :: filter<string> is f''
|
|
301
|
+
# label="Since"
|
|
302
|
+
given: SINCE :: date is @2023-01-01
|
|
303
|
+
|
|
304
|
+
source: a is one extend {
|
|
305
|
+
view: x is vx
|
|
306
|
+
}`;
|
|
307
|
+
const out = await spliced(source, (d) => {
|
|
308
|
+
d.localGivens = (d.localGivens ?? []).filter(
|
|
309
|
+
(g) => g.name !== "CATEGORY",
|
|
310
|
+
);
|
|
311
|
+
});
|
|
312
|
+
expect(out).toContain(`// Why the page filters by category at all.
|
|
313
|
+
# label="Since"
|
|
314
|
+
given: SINCE :: date is @2023-01-01`);
|
|
315
|
+
expect(out).not.toContain("CATEGORY");
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("removes a block's header when its last declaration goes", async () => {
|
|
319
|
+
const source = `##! experimental.givens
|
|
320
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
321
|
+
import "../m.malloy"
|
|
322
|
+
|
|
323
|
+
given:
|
|
324
|
+
# label="Local"
|
|
325
|
+
LOCAL_X :: filter<string> is f'Jeans'
|
|
326
|
+
|
|
327
|
+
source: a is one extend {
|
|
328
|
+
view: x is vx
|
|
329
|
+
}`;
|
|
330
|
+
const out = await spliced(source, (d) => {
|
|
331
|
+
delete d.localGivens;
|
|
332
|
+
});
|
|
333
|
+
expect(out).not.toContain("given:");
|
|
334
|
+
expect(out).not.toContain("LOCAL_X");
|
|
335
|
+
expect(out).toContain(`import "../m.malloy"
|
|
336
|
+
|
|
337
|
+
source: a is one extend {`);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("retags a given in place, keeping the declaration's own line", async () => {
|
|
341
|
+
const source = `##! experimental.givens
|
|
342
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
343
|
+
import "../m.malloy"
|
|
344
|
+
|
|
345
|
+
# label="Category"
|
|
346
|
+
given: CATEGORY :: filter<string> is f''
|
|
347
|
+
|
|
348
|
+
source: a is one extend {
|
|
349
|
+
view: x is vx
|
|
350
|
+
}`;
|
|
351
|
+
const out = await spliced(source, (d) => {
|
|
352
|
+
d.localGivens![0].label = "Product category";
|
|
353
|
+
d.localGivens![0].control = "multiselect";
|
|
354
|
+
});
|
|
355
|
+
expect(out).toContain(`# label="Product category" control=multiselect
|
|
356
|
+
given: CATEGORY :: filter<string> is f''`);
|
|
357
|
+
expect(out).not.toContain(`label="Category"`);
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
describe("spliceDashboardDocument: drills", () => {
|
|
362
|
+
const WITH_DIMENSION = `## artifact { title="T" tiles=["a -> x"] }
|
|
363
|
+
import "../m.malloy"
|
|
364
|
+
|
|
365
|
+
source: a is one extend {
|
|
366
|
+
// The dimension a view groups by, declared here so it can be tagged here.
|
|
367
|
+
# label="Category"
|
|
368
|
+
dimension: cat is products.category
|
|
369
|
+
|
|
370
|
+
# drill { to=self given=BRAND }
|
|
371
|
+
dimension: brand is products.brand
|
|
372
|
+
|
|
373
|
+
view: x is vx
|
|
374
|
+
}`;
|
|
375
|
+
|
|
376
|
+
it("puts a drill tag on a dimension this file declares, under its other tags", async () => {
|
|
377
|
+
const out = await spliced(WITH_DIMENSION, (d) => {
|
|
378
|
+
d.drills = [
|
|
379
|
+
...(d.drills ?? []),
|
|
380
|
+
{
|
|
381
|
+
source: "a",
|
|
382
|
+
name: "cat",
|
|
383
|
+
expression: "products.category",
|
|
384
|
+
to: ["self", "regions"],
|
|
385
|
+
given: "CATEGORY",
|
|
386
|
+
},
|
|
387
|
+
];
|
|
388
|
+
});
|
|
389
|
+
expect(out).toContain(
|
|
390
|
+
` // The dimension a view groups by, declared here so it can be tagged here.
|
|
391
|
+
# label="Category"
|
|
392
|
+
# drill { to=["self", "regions"] given=CATEGORY }
|
|
393
|
+
dimension: cat is products.category`,
|
|
394
|
+
);
|
|
395
|
+
// And it reads back as written.
|
|
396
|
+
const back = await splice(out, () => {});
|
|
397
|
+
if (spliceFailed(back)) throw new Error(back.reason);
|
|
398
|
+
const reread = await readDashboardDocument(out);
|
|
399
|
+
if (readFailed(reread)) throw new Error(reread.reason);
|
|
400
|
+
expect(reread.document.drills?.map((d) => d.name)).toEqual([
|
|
401
|
+
"cat",
|
|
402
|
+
"brand",
|
|
403
|
+
]);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("rewrites and removes a drill tag, leaving the dimension", async () => {
|
|
407
|
+
const retargeted = await spliced(WITH_DIMENSION, (d) => {
|
|
408
|
+
d.drills = [{ ...(d.drills ?? [])[0], to: ["brands"] }];
|
|
409
|
+
});
|
|
410
|
+
expect(retargeted).toContain(
|
|
411
|
+
" # drill { to=brands given=BRAND }\n dimension: brand is products.brand",
|
|
412
|
+
);
|
|
413
|
+
const removed = await spliced(WITH_DIMENSION, (d) => {
|
|
414
|
+
delete d.drills;
|
|
415
|
+
});
|
|
416
|
+
expect(removed).not.toContain("# drill");
|
|
417
|
+
expect(removed).toContain(" dimension: brand is products.brand");
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it("refuses a drill on a dimension the model declares", async () => {
|
|
421
|
+
const r = await splice(WITH_DIMENSION, (d) => {
|
|
422
|
+
d.drills = [
|
|
423
|
+
...(d.drills ?? []),
|
|
424
|
+
{ source: "a", name: "category", expression: "", to: ["self"] },
|
|
425
|
+
];
|
|
426
|
+
});
|
|
427
|
+
expect(spliceFailed(r) && r.reason).toContain("not a dimension");
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
describe("spliceDashboardDocument: the page's own settings", () => {
|
|
432
|
+
it("retitles the page on its one-line tag", async () => {
|
|
433
|
+
const out = await spliced(SOURCE, (d) => {
|
|
434
|
+
d.title = "Renamed";
|
|
435
|
+
});
|
|
436
|
+
expect(out).toContain(
|
|
437
|
+
'## artifact { title="Renamed" tiles=["a -> by_cat", "a -> by_brand"] } dashboard { columns=12 }',
|
|
438
|
+
);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it("changes the grid width, and can take it away", async () => {
|
|
442
|
+
const wider = await spliced(SOURCE, (d) => {
|
|
443
|
+
d.columns = 24;
|
|
444
|
+
});
|
|
445
|
+
expect(wider).toContain("] } dashboard { columns=24 }");
|
|
446
|
+
const flowed = await spliced(SOURCE, (d) => {
|
|
447
|
+
delete d.columns;
|
|
448
|
+
});
|
|
449
|
+
expect(flowed).toContain('tiles=["a -> by_cat", "a -> by_brand"] }\n');
|
|
450
|
+
expect(flowed).not.toContain("dashboard {");
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it("adds and removes autorun and starting values inside the tag", async () => {
|
|
454
|
+
const out = await spliced(SOURCE, (d) => {
|
|
455
|
+
d.autorun = false;
|
|
456
|
+
d.startingGivens = { CATEGORY: "Jeans" };
|
|
457
|
+
});
|
|
458
|
+
expect(out).toContain(
|
|
459
|
+
'## artifact { title="Probe" tiles=["a -> by_cat", "a -> by_brand"] autorun=false givens { CATEGORY="Jeans" } } dashboard { columns=12 }',
|
|
460
|
+
);
|
|
461
|
+
const back = await spliced(out, (d) => {
|
|
462
|
+
delete d.autorun;
|
|
463
|
+
delete d.startingGivens;
|
|
464
|
+
});
|
|
465
|
+
expect(back).toContain(
|
|
466
|
+
'## artifact { title="Probe" tiles=["a -> by_cat", "a -> by_brand"] } dashboard { columns=12 }',
|
|
467
|
+
);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
// The description is the run of `##"` lines; a blank paragraph is a bare one.
|
|
471
|
+
it("rewrites the description in place, paragraphs and all", async () => {
|
|
472
|
+
const out = await spliced(SOURCE, (d) => {
|
|
473
|
+
d.description = "First line.\n\nSecond paragraph, **bold**.";
|
|
474
|
+
});
|
|
475
|
+
expect(out).toContain(
|
|
476
|
+
'##! experimental.givens\n\n##" First line.\n##"\n##" Second paragraph, **bold**.\n## artifact {',
|
|
477
|
+
);
|
|
478
|
+
const gone = await spliced(SOURCE, (d) => {
|
|
479
|
+
delete d.description;
|
|
480
|
+
});
|
|
481
|
+
expect(gone).toContain("##! experimental.givens\n\n## artifact {");
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
it("adds a description to a page that had none", async () => {
|
|
485
|
+
const source = `## artifact { title="T" tiles=["a -> x"] }\nimport "../m.malloy"\n\nsource: a is one extend {\n view: x is vx\n}`;
|
|
486
|
+
const out = await spliced(source, (d) => {
|
|
487
|
+
d.description = "Now with prose.";
|
|
488
|
+
});
|
|
489
|
+
expect(
|
|
490
|
+
out.startsWith('##" Now with prose.\n## artifact { title="T"'),
|
|
491
|
+
).toBe(true);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("retitles and reorders in one write, on the same line", async () => {
|
|
495
|
+
const out = await spliced(SOURCE, (d) => {
|
|
496
|
+
d.title = "Renamed";
|
|
497
|
+
d.tiles.reverse();
|
|
498
|
+
});
|
|
499
|
+
expect(out).toContain(
|
|
500
|
+
'## artifact { title="Renamed" tiles=["a -> by_brand", "a -> by_cat"] } dashboard { columns=12 }',
|
|
501
|
+
);
|
|
502
|
+
});
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
describe("spliceDashboardDocument: tiles added and removed", () => {
|
|
506
|
+
// A removed tile takes its declaration and its `#` tags. The `//` comment
|
|
507
|
+
// above it stays: the file cannot say whose it was, and a comment left is a
|
|
508
|
+
// smaller wrong than one destroyed — the builder shows this diff first.
|
|
509
|
+
it("removes a tile, its tags and its entry, and leaves the comment", async () => {
|
|
510
|
+
const out = await spliced(SOURCE, (d) => {
|
|
511
|
+
d.tiles.splice(0, 1);
|
|
512
|
+
});
|
|
513
|
+
expect(out).toContain('tiles=["a -> by_brand"]');
|
|
514
|
+
expect(out).not.toContain("view: by_cat");
|
|
515
|
+
expect(out).not.toContain('# label="By category"');
|
|
516
|
+
expect(out).not.toContain("# break");
|
|
517
|
+
// The comment stays where it was, and the blank line after the removed
|
|
518
|
+
// tile stays with it: closing that gap would hand the comment to the
|
|
519
|
+
// next tile, which is the guess the writer refuses to make.
|
|
520
|
+
expect(out).toContain(
|
|
521
|
+
" // Why this tile leads: revenue is the number people ask about first.\n\n # colspan=6\n view: by_brand is by_brand_view",
|
|
522
|
+
);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it("removes an inline tile with its whole body", async () => {
|
|
526
|
+
const source = `## artifact { title="T" tiles=["a -> kpis", "a -> x"] }
|
|
527
|
+
import { one } from "../m.malloy"
|
|
528
|
+
|
|
529
|
+
source: a is one extend {
|
|
530
|
+
# colspan=12
|
|
531
|
+
view: kpis is {
|
|
532
|
+
aggregate:
|
|
533
|
+
total_sales
|
|
534
|
+
order_count
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
view: x is vx
|
|
538
|
+
}`;
|
|
539
|
+
const out = await spliced(source, (d) => {
|
|
540
|
+
d.tiles.splice(0, 1);
|
|
541
|
+
});
|
|
542
|
+
expect(out).toBe(`## artifact { title="T" tiles=["a -> x"] }
|
|
543
|
+
import { one } from "../m.malloy"
|
|
544
|
+
|
|
545
|
+
source: a is one extend {
|
|
546
|
+
view: x is vx
|
|
547
|
+
}`);
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it("adds a tile inside the extension of the source it reads", async () => {
|
|
551
|
+
const out = await spliced(SOURCE, (d) => {
|
|
552
|
+
d.tiles.push({
|
|
553
|
+
name: "by_state_tile",
|
|
554
|
+
source: "a",
|
|
555
|
+
declaration: { kind: "reference", from: "sales_by_state" },
|
|
556
|
+
colspan: 6,
|
|
557
|
+
label: "By state",
|
|
558
|
+
filters: [{ field: "category", given: "CATEGORY" }],
|
|
559
|
+
});
|
|
560
|
+
});
|
|
561
|
+
expect(out).toContain(
|
|
562
|
+
'tiles=["a -> by_cat", "a -> by_brand", "a -> by_state_tile"]',
|
|
563
|
+
);
|
|
564
|
+
expect(out).toContain(` # colspan=6
|
|
565
|
+
view: by_brand is by_brand_view
|
|
566
|
+
|
|
567
|
+
# colspan=6
|
|
568
|
+
# label="By state"
|
|
569
|
+
view: by_state_tile is sales_by_state + { where: category ~ $CATEGORY }
|
|
570
|
+
}`);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
// A source the file imports BY NAME can take a new extension; the builder
|
|
574
|
+
// never adds an import, so that is the only kind that can.
|
|
575
|
+
it("adds a tile on a named import by declaring a new extension", async () => {
|
|
576
|
+
const source = `## artifact { title="T" tiles=["a -> x"] }
|
|
577
|
+
import { one, products } from "../m.malloy"
|
|
578
|
+
|
|
579
|
+
source: a is one extend {
|
|
580
|
+
view: x is vx
|
|
581
|
+
}`;
|
|
582
|
+
const out = await spliced(source, (d) => {
|
|
583
|
+
d.sources.push({ name: "products_tiles", base: "products" });
|
|
584
|
+
d.tiles.push({
|
|
585
|
+
name: "by_brand_tile",
|
|
586
|
+
source: "products_tiles",
|
|
587
|
+
declaration: { kind: "reference", from: "by_brand" },
|
|
588
|
+
colspan: 4,
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
expect(out)
|
|
592
|
+
.toBe(`## artifact { title="T" tiles=["a -> x", "products_tiles -> by_brand_tile"] }
|
|
593
|
+
import { one, products } from "../m.malloy"
|
|
594
|
+
|
|
595
|
+
source: a is one extend {
|
|
596
|
+
view: x is vx
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
source: products_tiles is products extend {
|
|
600
|
+
# colspan=4
|
|
601
|
+
view: by_brand_tile is by_brand
|
|
602
|
+
}
|
|
603
|
+
`);
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
it("refuses a tile on a source the file does not import by name", async () => {
|
|
607
|
+
const r = await splice(SOURCE, (d) => {
|
|
608
|
+
d.sources.push({ name: "orders_tiles", base: "order_items" });
|
|
609
|
+
d.tiles.push({
|
|
610
|
+
name: "t",
|
|
611
|
+
source: "orders_tiles",
|
|
612
|
+
declaration: { kind: "reference", from: "by_category" },
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
expect(r.ok).toBe(false);
|
|
616
|
+
if (spliceFailed(r)) expect(r.reason).toContain("not imported by name");
|
|
617
|
+
});
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
describe("spliceDashboardDocument: what it refuses", () => {
|
|
621
|
+
// Adding or removing a tile inserts or deletes a declaration, which carries
|
|
622
|
+
// the comment block above it, and no file says who that comment belongs to.
|
|
623
|
+
it("refuses to change the page's imports", async () => {
|
|
624
|
+
const r = await splice(SOURCE, (d) => {
|
|
625
|
+
d.imports.push({ kind: "all", from: "../more.malloy" });
|
|
626
|
+
});
|
|
627
|
+
expect(r.ok).toBe(false);
|
|
628
|
+
if (spliceFailed(r)) expect(r.reason).toContain("imports");
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
// An inherited tile's tags live on the model's view, and the builder never
|
|
632
|
+
// writes model files. Saying so beats writing a tag onto the wrong object.
|
|
633
|
+
it("refuses to restyle a tile declared on its source", async () => {
|
|
634
|
+
const source = `## artifact { title="T" tiles=["orders -> by_brand"] }\nimport { orders } from '../orders.malloy'`;
|
|
635
|
+
const r = await splice(source, (d) => {
|
|
636
|
+
d.tiles[0].colspan = 6;
|
|
637
|
+
});
|
|
638
|
+
expect(r.ok).toBe(false);
|
|
639
|
+
if (spliceFailed(r)) expect(r.reason).toContain("declared on its source");
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
it("refuses a new tile that is not a reference to a view", async () => {
|
|
643
|
+
expect(
|
|
644
|
+
await refused(SOURCE, (d) => {
|
|
645
|
+
d.tiles.push({
|
|
646
|
+
name: "adhoc",
|
|
647
|
+
source: "a",
|
|
648
|
+
declaration: { kind: "inline" },
|
|
649
|
+
});
|
|
650
|
+
}),
|
|
651
|
+
).toContain("inline query");
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it("refuses to change or remove an extension", async () => {
|
|
655
|
+
expect(
|
|
656
|
+
await refused(SOURCE, (d) => {
|
|
657
|
+
d.sources[0].base = "other_orders";
|
|
658
|
+
}),
|
|
659
|
+
).toContain("cannot be changed or removed");
|
|
660
|
+
expect(
|
|
661
|
+
await refused(SOURCE, (d) => {
|
|
662
|
+
d.sources = [];
|
|
663
|
+
}),
|
|
664
|
+
).toContain("cannot be changed or removed");
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
it("refuses a new extension that no tile reads", async () => {
|
|
668
|
+
expect(
|
|
669
|
+
await refused(SOURCE, (d) => {
|
|
670
|
+
d.sources.push({ name: "b", base: "scoped_orders" });
|
|
671
|
+
}),
|
|
672
|
+
).toContain("needs a tile on it");
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
it("refuses a drill that names no destination", async () => {
|
|
676
|
+
const withDimension = SOURCE.replace(
|
|
677
|
+
"source: a is scoped_orders extend {",
|
|
678
|
+
"source: a is scoped_orders extend {\n dimension: cat is products.category",
|
|
679
|
+
);
|
|
680
|
+
expect(
|
|
681
|
+
await refused(withDimension, (d) => {
|
|
682
|
+
d.drills = [
|
|
683
|
+
{
|
|
684
|
+
source: "a",
|
|
685
|
+
name: "cat",
|
|
686
|
+
expression: "products.category",
|
|
687
|
+
to: [],
|
|
688
|
+
},
|
|
689
|
+
];
|
|
690
|
+
}),
|
|
691
|
+
).toContain("names no destination");
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
it("refuses to edit a file that will not open", async () => {
|
|
695
|
+
const r = await spliceDashboardDocument("not malloy at all", {
|
|
696
|
+
title: "x",
|
|
697
|
+
imports: [],
|
|
698
|
+
sources: [],
|
|
699
|
+
tiles: [],
|
|
700
|
+
});
|
|
701
|
+
expect(r.ok).toBe(false);
|
|
702
|
+
if (spliceFailed(r)) expect(r.reason).toContain("will not open");
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* The round-trip gate, exercised against the real thing. Every composite
|
|
708
|
+
* dashboard in the repository is opened, has one property changed, is spliced,
|
|
709
|
+
* and is read back — which is the property the writer promises and the reason a
|
|
710
|
+
* commented file no longer has to be read-only.
|
|
711
|
+
*/
|
|
712
|
+
/** The tag keys the writer models, across tiles, givens and drills. */
|
|
713
|
+
const MODELLED_TAG =
|
|
714
|
+
/^#\s*(colspan|break|borderless|label|subtitle|description|control|suggest|range_min|range_max|drill)\b/;
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* Every `#` line the document does not model, keyed by the declaration it sits
|
|
718
|
+
* above. Comparing this before and after an edit catches what a file-wide
|
|
719
|
+
* count cannot: a tag that survived but moved onto a different declaration,
|
|
720
|
+
* which in Malloy is a change of meaning rather than a change of layout.
|
|
721
|
+
*/
|
|
722
|
+
function unmodelledTagsByDeclaration(text: string): Record<string, string[]> {
|
|
723
|
+
const lines = text.split("\n");
|
|
724
|
+
const out: Record<string, string[]> = {};
|
|
725
|
+
const record = (key: string, line: number) => {
|
|
726
|
+
const tags = blockAbove(lines, line)
|
|
727
|
+
.tags.map((t) => t.text)
|
|
728
|
+
.filter((t) => !MODELLED_TAG.test(t));
|
|
729
|
+
if (tags.length > 0) out[key] = tags;
|
|
730
|
+
};
|
|
731
|
+
// Keyed by the enclosing source as well as the name: `view:x` repeats
|
|
732
|
+
// across sources, and a colliding key would let a tag move from one to
|
|
733
|
+
// another without the comparison noticing.
|
|
734
|
+
let scope = "";
|
|
735
|
+
lines.forEach((line, i) => {
|
|
736
|
+
const m =
|
|
737
|
+
/^\s*(source|view|dimension|measure):\s*([A-Za-z_][A-Za-z0-9_]*)\s+is\b/.exec(
|
|
738
|
+
line,
|
|
739
|
+
);
|
|
740
|
+
if (!m) return;
|
|
741
|
+
if (m[1] === "source") scope = m[2];
|
|
742
|
+
record(`${scope}/${m[1]}:${m[2]}`, i);
|
|
743
|
+
});
|
|
744
|
+
for (const [name, at] of givenDeclarations(lines))
|
|
745
|
+
record(`given:${name}`, at.line);
|
|
746
|
+
return out;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
describe("every composite dashboard survives an edit", () => {
|
|
750
|
+
const found: string[] = [];
|
|
751
|
+
const walk = (dir: string) => {
|
|
752
|
+
if (!fs.existsSync(dir)) return;
|
|
753
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
754
|
+
const full = path.join(dir, entry.name);
|
|
755
|
+
if (entry.isDirectory()) walk(full);
|
|
756
|
+
else if (entry.name.endsWith(".malloy") && full.includes("dashboards"))
|
|
757
|
+
found.push(full);
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
walk(path.join(REPO, "examples"));
|
|
761
|
+
walk(path.join(REPO, "packages/server/tests/fixtures"));
|
|
762
|
+
|
|
763
|
+
const editable = found.filter((f) => {
|
|
764
|
+
const text = fs.readFileSync(f, "utf8");
|
|
765
|
+
return (
|
|
766
|
+
/##\s*artifact[\s\S]*tiles\s*=/.test(text) &&
|
|
767
|
+
!f.includes("dashboards-lint")
|
|
768
|
+
);
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
for (const file of editable) {
|
|
772
|
+
const name = path.relative(REPO, file);
|
|
773
|
+
it(`opens ${name}, and writes it back byte for byte when nothing changed`, async () => {
|
|
774
|
+
const source = fs.readFileSync(file, "utf8");
|
|
775
|
+
const doc = await openDocument(source);
|
|
776
|
+
const result = await spliceDashboardDocument(source, doc);
|
|
777
|
+
if (spliceFailed(result)) throw new Error(result.reason);
|
|
778
|
+
expect(result.source).toBe(source);
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
it(`round-trips a colspan change in ${name}`, async () => {
|
|
782
|
+
const source = fs.readFileSync(file, "utf8");
|
|
783
|
+
const doc = await readDashboardDocument(source);
|
|
784
|
+
if (readFailed(doc)) throw new Error(doc.reason);
|
|
785
|
+
const target = doc.document.tiles.findIndex(
|
|
786
|
+
(t) => t.declaration.kind !== "inherited",
|
|
787
|
+
);
|
|
788
|
+
// A file whose tiles are all declared on their sources has nothing
|
|
789
|
+
// here to patch, which is a real shape rather than a gap.
|
|
790
|
+
if (target < 0) return;
|
|
791
|
+
|
|
792
|
+
const next = structuredClone(doc.document);
|
|
793
|
+
next.tiles[target].colspan = 3;
|
|
794
|
+
const result = await spliceDashboardDocument(source, next);
|
|
795
|
+
if (spliceFailed(result)) throw new Error(result.reason);
|
|
796
|
+
|
|
797
|
+
const reread = await readDashboardDocument(result.source);
|
|
798
|
+
if (readFailed(reread)) throw new Error(reread.reason);
|
|
799
|
+
expect(reread.document.tiles[target].colspan).toBe(3);
|
|
800
|
+
// Everything the file said that the builder does not model is still
|
|
801
|
+
// there: the comment count is the cheapest proof.
|
|
802
|
+
const comments = (text: string) =>
|
|
803
|
+
text.split("\n").filter((l) => l.trim().startsWith("//")).length;
|
|
804
|
+
expect(comments(result.source)).toBe(comments(source));
|
|
805
|
+
// And every `#` tag the builder does not model is still there, on the
|
|
806
|
+
// same declaration. A file-wide count cannot see a tag that moved to
|
|
807
|
+
// the declaration below, which is how one silently changes meaning.
|
|
808
|
+
expect(unmodelledTagsByDeclaration(result.source)).toEqual(
|
|
809
|
+
unmodelledTagsByDeclaration(source),
|
|
810
|
+
);
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
it(`retags a control in ${name} and leaves every other tag where it was`, async () => {
|
|
814
|
+
const source = fs.readFileSync(file, "utf8");
|
|
815
|
+
const doc = await readDashboardDocument(source);
|
|
816
|
+
if (readFailed(doc)) throw new Error(doc.reason);
|
|
817
|
+
const given = doc.document.localGivens?.[0];
|
|
818
|
+
// Most dashboards declare no givens of their own; that is a real
|
|
819
|
+
// shape, not a gap.
|
|
820
|
+
if (!given) return;
|
|
821
|
+
|
|
822
|
+
const next = structuredClone(doc.document);
|
|
823
|
+
next.localGivens![0].label = `${given.label ?? given.name} (edited)`;
|
|
824
|
+
const result = await spliceDashboardDocument(source, next);
|
|
825
|
+
if (spliceFailed(result)) throw new Error(result.reason);
|
|
826
|
+
expect(unmodelledTagsByDeclaration(result.source)).toEqual(
|
|
827
|
+
unmodelledTagsByDeclaration(source),
|
|
828
|
+
);
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* A given carries two kinds of `#` line: the control contract the builder
|
|
835
|
+
* writes, and annotations routed elsewhere -- `#(secure)` marks a given as an
|
|
836
|
+
* access control, and a host may add its own. Only the first kind is the
|
|
837
|
+
* writer's. The projection never holds the second, so the gate cannot notice
|
|
838
|
+
* it lost one, which is why these assert on the text.
|
|
839
|
+
*/
|
|
840
|
+
describe("spliceDashboardDocument: a given's annotations it does not own", () => {
|
|
841
|
+
const FILE = (givens: string) => `##! experimental.givens
|
|
842
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
843
|
+
import "../m.malloy"
|
|
844
|
+
|
|
845
|
+
${givens}
|
|
846
|
+
|
|
847
|
+
source: a is one extend {
|
|
848
|
+
view: x is vx
|
|
849
|
+
}`;
|
|
850
|
+
|
|
851
|
+
const SECURE_FIRST = FILE(`#(secure)
|
|
852
|
+
# label="Category"
|
|
853
|
+
given: CATEGORY :: filter<string> is f''`);
|
|
854
|
+
|
|
855
|
+
it("keeps a routed marker above the control line, still on the declaration", async () => {
|
|
856
|
+
const result = await spliced(SECURE_FIRST, (d) => {
|
|
857
|
+
d.localGivens![0].label = "Product category";
|
|
858
|
+
});
|
|
859
|
+
expect(result).toContain(
|
|
860
|
+
`#(secure)\n# label="Product category"\ngiven: CATEGORY :: filter<string> is f''`,
|
|
861
|
+
);
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
it("rewrites only the control line", async () => {
|
|
865
|
+
const result = await spliced(SECURE_FIRST, (d) => {
|
|
866
|
+
d.localGivens![0].label = "Product category";
|
|
867
|
+
});
|
|
868
|
+
const changed = result
|
|
869
|
+
.split("\n")
|
|
870
|
+
.filter((line, i) => line !== SECURE_FIRST.split("\n")[i]);
|
|
871
|
+
expect(changed).toEqual([`# label="Product category"`]);
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
it("keeps a routed marker written below the control line", async () => {
|
|
875
|
+
const source = FILE(`# label="Category"
|
|
876
|
+
#(secure)
|
|
877
|
+
given: CATEGORY :: filter<string> is f''`);
|
|
878
|
+
const result = await spliced(source, (d) => {
|
|
879
|
+
d.localGivens![0].label = "Product category";
|
|
880
|
+
});
|
|
881
|
+
// The rebuilt contract lands immediately above the declaration, so the
|
|
882
|
+
// marker ends up above it -- still one contiguous block, and Malloy
|
|
883
|
+
// reads a declaration's annotations without regard to their order.
|
|
884
|
+
expect(result).toContain(
|
|
885
|
+
`#(secure)\n# label="Product category"\ngiven: CATEGORY :: filter<string> is f''`,
|
|
886
|
+
);
|
|
887
|
+
expect(result).not.toContain(`label="Category"`);
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
it("keeps a keyed tag it does not model", async () => {
|
|
891
|
+
const source = FILE(`# audit_scope=finance
|
|
892
|
+
# label="Category"
|
|
893
|
+
given: CATEGORY :: filter<string> is f''`);
|
|
894
|
+
const result = await spliced(source, (d) => {
|
|
895
|
+
d.localGivens![0].control = "multiselect";
|
|
896
|
+
});
|
|
897
|
+
expect(result).toContain(
|
|
898
|
+
`# audit_scope=finance\n# label="Category" control=multiselect\ngiven: CATEGORY`,
|
|
899
|
+
);
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
it("keeps the marker when the contract spans two lines under a comment", async () => {
|
|
903
|
+
const source = FILE(`// Why this is scoped: one category at a time.
|
|
904
|
+
#(secure)
|
|
905
|
+
# description="Narrow to one category"
|
|
906
|
+
# label="Category" control=select
|
|
907
|
+
given: CATEGORY :: filter<string> is f''`);
|
|
908
|
+
const result = await spliced(source, (d) => {
|
|
909
|
+
d.localGivens![0].label = "Product category";
|
|
910
|
+
});
|
|
911
|
+
expect(result).toContain(
|
|
912
|
+
`// Why this is scoped: one category at a time.\n#(secure)\n` +
|
|
913
|
+
`# label="Product category" description="Narrow to one category" control=select\n` +
|
|
914
|
+
`given: CATEGORY :: filter<string> is f''`,
|
|
915
|
+
);
|
|
916
|
+
});
|
|
917
|
+
|
|
918
|
+
it("keeps the marker through a redeclaration", async () => {
|
|
919
|
+
const result = await spliced(SECURE_FIRST, (d) => {
|
|
920
|
+
d.localGivens![0].default = "f'Jeans'";
|
|
921
|
+
});
|
|
922
|
+
expect(result).toContain(
|
|
923
|
+
`#(secure)\n# label="Category"\ngiven: CATEGORY :: filter<string> is f'Jeans'`,
|
|
924
|
+
);
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
it("keeps the marker on a declaration inside a given: block", async () => {
|
|
928
|
+
const source = FILE(`given:
|
|
929
|
+
#(secure)
|
|
930
|
+
# label="Local"
|
|
931
|
+
LOCAL_X :: filter<string> is f'Jeans'`);
|
|
932
|
+
const result = await spliced(source, (d) => {
|
|
933
|
+
d.localGivens![0].label = "Local X";
|
|
934
|
+
});
|
|
935
|
+
expect(result).toContain(
|
|
936
|
+
` #(secure)\n # label="Local X"\n LOCAL_X :: filter<string> is f'Jeans'`,
|
|
937
|
+
);
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
/**
|
|
941
|
+
* The exception, and the reason the filter is on the retag path only: a `#`
|
|
942
|
+
* line whose declaration goes does not lapse, it attaches to whatever is
|
|
943
|
+
* declared next. Leaving `#(secure)` behind would silently mark a
|
|
944
|
+
* different given as an access control.
|
|
945
|
+
*/
|
|
946
|
+
it("takes a routed marker with the declaration it annotates", async () => {
|
|
947
|
+
const source = FILE(`#(secure)
|
|
948
|
+
# label="Category"
|
|
949
|
+
given: CATEGORY :: filter<string> is f''
|
|
950
|
+
|
|
951
|
+
# label="Since"
|
|
952
|
+
given: SINCE :: date is @2023-01-01`);
|
|
953
|
+
const result = await spliced(source, (d) => {
|
|
954
|
+
d.localGivens = d.localGivens!.filter((g) => g.name !== "CATEGORY");
|
|
955
|
+
});
|
|
956
|
+
expect(result).not.toContain("#(secure)");
|
|
957
|
+
expect(result).toContain(
|
|
958
|
+
`import "../m.malloy"\n\n# label="Since"\ngiven: SINCE :: date is @2023-01-01`,
|
|
959
|
+
);
|
|
960
|
+
const reread = await openDocument(result);
|
|
961
|
+
expect(reread.localGivens?.map((g) => g.name)).toEqual(["SINCE"]);
|
|
962
|
+
});
|
|
963
|
+
});
|
|
964
|
+
|
|
965
|
+
describe("spliceDashboardDocument: an ask no planner could place", () => {
|
|
966
|
+
it("refuses rather than reporting a write that never happened", async () => {
|
|
967
|
+
const source = `##! experimental.givens
|
|
968
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
969
|
+
import "../m.malloy"
|
|
970
|
+
|
|
971
|
+
source: a is one extend {
|
|
972
|
+
# label="Category" drill { to=self given=BRAND }
|
|
973
|
+
dimension: cat is category
|
|
974
|
+
|
|
975
|
+
view: x is vx
|
|
976
|
+
}`;
|
|
977
|
+
const reason = await refused(source, (d) => {
|
|
978
|
+
delete d.drills;
|
|
979
|
+
});
|
|
980
|
+
expect(reason).toContain(
|
|
981
|
+
"did not produce the dashboard that was asked for",
|
|
982
|
+
);
|
|
983
|
+
});
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
/**
|
|
987
|
+
* A label is free text a person types, and it is written into a double-quoted
|
|
988
|
+
* tag value. Unescaped, a quote or a trailing backslash closed the value early
|
|
989
|
+
* and the rest of the line parsed as tag syntax -- which the gate caught, so
|
|
990
|
+
* the symptom was not a corrupt file but a document that could never be saved
|
|
991
|
+
* again. The title and a starting given's value already escape; these are the
|
|
992
|
+
* remaining tag values the writer composes.
|
|
993
|
+
*/
|
|
994
|
+
describe("spliceDashboardDocument: labels are free text", () => {
|
|
995
|
+
const FILE = `##! experimental.givens
|
|
996
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
997
|
+
import "../m.malloy"
|
|
998
|
+
|
|
999
|
+
# label="Cat"
|
|
1000
|
+
given: CATEGORY :: filter<string> is f''
|
|
1001
|
+
|
|
1002
|
+
source: a is one extend {
|
|
1003
|
+
# label="X"
|
|
1004
|
+
view: x is vx
|
|
1005
|
+
}`;
|
|
1006
|
+
|
|
1007
|
+
it("keeps a quote in a tile label, and reads it back", async () => {
|
|
1008
|
+
const label = 'Revenue (the "good" kind)';
|
|
1009
|
+
const result = await spliced(FILE, (d) => {
|
|
1010
|
+
d.tiles[0].label = label;
|
|
1011
|
+
});
|
|
1012
|
+
const reread = await openDocument(result);
|
|
1013
|
+
expect(reread.tiles[0].label).toBe(label);
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
it("keeps a trailing backslash in a given's label", async () => {
|
|
1017
|
+
const label = "Category\\";
|
|
1018
|
+
const result = await spliced(FILE, (d) => {
|
|
1019
|
+
d.localGivens![0].label = label;
|
|
1020
|
+
});
|
|
1021
|
+
const reread = await openDocument(result);
|
|
1022
|
+
expect(reread.localGivens?.[0].label).toBe(label);
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
it("keeps a quote in a given's description", async () => {
|
|
1026
|
+
const description = 'Only the "current" quarter';
|
|
1027
|
+
const result = await spliced(FILE, (d) => {
|
|
1028
|
+
d.localGivens![0].description = description;
|
|
1029
|
+
});
|
|
1030
|
+
const reread = await openDocument(result);
|
|
1031
|
+
expect(reread.localGivens?.[0].description).toBe(description);
|
|
1032
|
+
});
|
|
1033
|
+
|
|
1034
|
+
it("keeps a quote in a tile subtitle", async () => {
|
|
1035
|
+
const subtitle = 'by "region"';
|
|
1036
|
+
const result = await spliced(FILE, (d) => {
|
|
1037
|
+
d.tiles[0].subtitle = subtitle;
|
|
1038
|
+
});
|
|
1039
|
+
const reread = await openDocument(result);
|
|
1040
|
+
expect(reread.tiles[0].subtitle).toBe(subtitle);
|
|
1041
|
+
});
|
|
1042
|
+
});
|
|
1043
|
+
|
|
1044
|
+
/**
|
|
1045
|
+
* Two behaviours this writer has that are worth pinning rather than
|
|
1046
|
+
* discovering: what the no-edit gate treats as the same document, and the one
|
|
1047
|
+
* place an unmodelled tag is still lost.
|
|
1048
|
+
*/
|
|
1049
|
+
describe("spliceDashboardDocument: the shapes of no change", () => {
|
|
1050
|
+
const FILE = `##! experimental.givens
|
|
1051
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
1052
|
+
import "../m.malloy"
|
|
1053
|
+
|
|
1054
|
+
# label="Cat" audit_scope=finance
|
|
1055
|
+
given: CATEGORY :: filter<string> is f''
|
|
1056
|
+
|
|
1057
|
+
source: a is one extend {
|
|
1058
|
+
# label="X"
|
|
1059
|
+
view: x is vx
|
|
1060
|
+
}`;
|
|
1061
|
+
|
|
1062
|
+
it("reads an omitted collection and an empty one as the same document", async () => {
|
|
1063
|
+
// A host that normalises its shape asks for no change, and must not be
|
|
1064
|
+
// told its save did not produce what it asked for.
|
|
1065
|
+
for (const materialise of [
|
|
1066
|
+
(d: DashboardDocument) => {
|
|
1067
|
+
d.drills = [];
|
|
1068
|
+
},
|
|
1069
|
+
(d: DashboardDocument) => {
|
|
1070
|
+
d.localGivens = d.localGivens ?? [];
|
|
1071
|
+
d.tiles[0].filters = [];
|
|
1072
|
+
},
|
|
1073
|
+
]) {
|
|
1074
|
+
expect(await spliced(FILE, materialise)).toBe(FILE);
|
|
1075
|
+
}
|
|
1076
|
+
});
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
* KNOWN, and the same rule the tile path has had since `MODELLED_TAG_KEYS`:
|
|
1080
|
+
* ownership is decided by the FIRST key on a line, and the line is replaced
|
|
1081
|
+
* whole. An unmodelled key sharing a line with a modelled one goes with it.
|
|
1082
|
+
*
|
|
1083
|
+
* A routed annotation cannot be caught this way -- `#(secure)` has no key
|
|
1084
|
+
* for `tagKey` to match, so it is never the first key on a shared line --
|
|
1085
|
+
* which is why this is a wart rather than a hole in what the fix is for.
|
|
1086
|
+
*/
|
|
1087
|
+
it("loses an unmodelled key that shares a line with a modelled one", async () => {
|
|
1088
|
+
const result = await spliced(FILE, (d) => {
|
|
1089
|
+
d.localGivens![0].label = "Cat2";
|
|
1090
|
+
});
|
|
1091
|
+
expect(result).toContain(`# label="Cat2"`);
|
|
1092
|
+
expect(result).not.toContain("audit_scope");
|
|
1093
|
+
});
|
|
1094
|
+
|
|
1095
|
+
it("keeps a routed marker that shares its block with a mixed line", async () => {
|
|
1096
|
+
const source = FILE.replace(
|
|
1097
|
+
`# label="Cat" audit_scope=finance`,
|
|
1098
|
+
`#(secure)
|
|
1099
|
+
# label="Cat" audit_scope=finance`,
|
|
1100
|
+
);
|
|
1101
|
+
const result = await spliced(source, (d) => {
|
|
1102
|
+
d.localGivens![0].label = "Cat2";
|
|
1103
|
+
});
|
|
1104
|
+
expect(result).toContain(
|
|
1105
|
+
`#(secure)\n# label="Cat2"\ngiven: CATEGORY :: filter<string> is f''`,
|
|
1106
|
+
);
|
|
1107
|
+
});
|
|
1108
|
+
});
|