@malloy-publisher/sdk 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/dist/{DocumentStorageProvider-CyOJ0hqf.es.js → DocumentStorage-B04sZ5Ie.es.js} +2307 -2334
- package/dist/{DocumentStorageProvider-DGPQEd_l.cjs.js → DocumentStorage-B_MMJ0Ot.cjs.js} +20 -20
- package/dist/{RenderedResult-BnPzJ9Jf.es.js → RenderedResult-BB7eokcf.es.js} +100 -80
- package/dist/RenderedResult-DVuGhatg.cjs.js +135 -0
- package/dist/ServerProvider-D-jjzfE5.cjs.js +1 -0
- package/dist/{ServerProvider-BWOEKg0O.es.js → ServerProvider-Do-GBvpn.es.js} +492 -481
- package/dist/builder/index.cjs.js +57 -52
- package/dist/builder/index.es.js +4421 -3738
- package/dist/client/api.d.ts +14 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/Dashboard/TileCard.d.ts +7 -3
- package/dist/components/Dashboard/telemetry.d.ts +20 -8
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +16 -4
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +31 -8
- package/dist/components/DashboardBuilder/controls.d.ts +8 -6
- package/dist/components/DashboardBuilder/document.d.ts +19 -5
- package/dist/components/DashboardBuilder/malloyText.d.ts +0 -30
- package/dist/components/DashboardBuilder/malloyTree.d.ts +232 -0
- package/dist/components/DashboardBuilder/preview.d.ts +17 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +23 -28
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +12 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +69 -5
- package/dist/components/DocumentStorage/index.d.ts +1 -0
- package/dist/index.cjs.js +9 -9
- package/dist/index.es.js +508 -506
- package/dist/theme/resolveTheme.d.ts +1 -1
- package/dist/theme/types.d.ts +12 -2
- package/package.json +1 -1
- package/src/components/Dashboard/TileCard.tsx +8 -4
- package/src/components/Dashboard/telemetry.ts +20 -8
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +4 -5
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +21 -3
- package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +706 -0
- package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +356 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +468 -103
- package/src/components/DashboardBuilder/FilterDialog.tsx +19 -6
- package/src/components/DashboardBuilder/TileFrame.tsx +11 -2
- package/src/components/DashboardBuilder/TileMenu.tsx +6 -0
- package/src/components/DashboardBuilder/__test__/inventory.ts +367 -0
- package/src/components/DashboardBuilder/controls.spec.ts +53 -3
- package/src/components/DashboardBuilder/controls.ts +9 -7
- package/src/components/DashboardBuilder/document.ts +17 -5
- package/src/components/DashboardBuilder/invariants.spec.ts +297 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +27 -66
- package/src/components/DashboardBuilder/malloyText.ts +4 -109
- package/src/components/DashboardBuilder/malloyTree.spec.ts +362 -0
- package/src/components/DashboardBuilder/malloyTree.ts +954 -0
- package/src/components/DashboardBuilder/preview.spec.ts +18 -0
- package/src/components/DashboardBuilder/preview.ts +24 -6
- package/src/components/DashboardBuilder/readDocument.spec.ts +312 -21
- package/src/components/DashboardBuilder/readDocument.ts +144 -165
- package/src/components/DashboardBuilder/scannerShapes.spec.ts +550 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +955 -21
- package/src/components/DashboardBuilder/spliceDocument.ts +509 -119
- package/src/components/DashboardBuilder/statementExtent.spec.ts +606 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.ts +7 -6
- package/src/components/DocumentStorage/DocumentStorage.ts +78 -5
- package/src/components/DocumentStorage/index.ts +1 -0
- package/src/components/Package/Package.tsx +12 -7
- package/src/components/RenderedResult/RenderedResult.tsx +21 -1
- package/src/components/given/GivensPanel.tsx +15 -3
- package/src/theme/buildTableCssVars.spec.ts +3 -0
- package/src/theme/buildTableCssVars.ts +6 -0
- package/src/theme/defaults.ts +16 -12
- package/src/theme/resolveTheme.spec.ts +14 -7
- package/src/theme/resolveTheme.ts +14 -8
- package/src/theme/types.ts +12 -2
- package/tsconfig.spec.json +5 -1
- package/dist/RenderedResult-Dp22kZsQ.cjs.js +0 -115
- package/dist/ServerProvider-Cc489A3f.cjs.js +0 -1
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
act,
|
|
6
|
+
cleanup,
|
|
7
|
+
fireEvent,
|
|
8
|
+
render,
|
|
9
|
+
screen,
|
|
10
|
+
waitFor,
|
|
11
|
+
} from "@testing-library/react";
|
|
12
|
+
import { beforeEach, describe, expect, it, mock } from "bun:test";
|
|
13
|
+
import {
|
|
14
|
+
clearCache,
|
|
15
|
+
mockServerProvider,
|
|
16
|
+
pending,
|
|
17
|
+
serverWrapper,
|
|
18
|
+
} from "../../../test/serverProvider";
|
|
19
|
+
import { globalQueryClient } from "../../utils/queryClient";
|
|
20
|
+
import { sha256Hex } from "../../utils/sha256";
|
|
21
|
+
import {
|
|
22
|
+
DocumentNotFoundError,
|
|
23
|
+
type DocumentLocator,
|
|
24
|
+
type DocumentStorage,
|
|
25
|
+
type DocumentType,
|
|
26
|
+
type Workspace,
|
|
27
|
+
} from "../DocumentStorage";
|
|
28
|
+
import { BrowserDocumentStorage } from "../DocumentStorage/BrowserDocumentStorage";
|
|
29
|
+
import { DocumentStorageProvider } from "../DocumentStorage/DocumentStorageProvider";
|
|
30
|
+
import type { DashboardEvent } from "../Dashboard/telemetry";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The editor against a host whose store is the record, and against the same
|
|
34
|
+
* store when it is not: what it opens, what it hands back as `expectedHash`,
|
|
35
|
+
* what it does with a read or a delete the backend could not answer, and what
|
|
36
|
+
* happens to a reader's edits when a new version of the file arrives.
|
|
37
|
+
*
|
|
38
|
+
* The gate is proved by the SAME fixture with and without `authoritative`,
|
|
39
|
+
* because the specs the Console already has cannot see it: `BrowserDocumentStorage`
|
|
40
|
+
* never declares itself the record, so they exercise one side of every branch.
|
|
41
|
+
*/
|
|
42
|
+
const PACKAGE_FILE = `## artifact { title="Storefront" tiles=["a -> by_cat"] } dashboard { columns=12 }
|
|
43
|
+
import { scoped_orders } from "../data_app.malloy"
|
|
44
|
+
|
|
45
|
+
source: a is scoped_orders extend {
|
|
46
|
+
# colspan=6
|
|
47
|
+
# label="By category"
|
|
48
|
+
view: by_cat is by_category
|
|
49
|
+
}`;
|
|
50
|
+
const withTitle = (title: string) =>
|
|
51
|
+
PACKAGE_FILE.replace('title="Storefront"', `title="${title}"`);
|
|
52
|
+
|
|
53
|
+
/** The file as the server has it, so a save is a real compare-and-swap. */
|
|
54
|
+
let serverText = PACKAGE_FILE;
|
|
55
|
+
|
|
56
|
+
const getModel = mock(async (_env: string, _pkg: string, path: string) => {
|
|
57
|
+
// Not instant, because the defect these specs are about is a local parse
|
|
58
|
+
// of stale text winning the race against the refetch that would correct it.
|
|
59
|
+
// A mock that resolves in the same tick removes the race and the proof.
|
|
60
|
+
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
61
|
+
return {
|
|
62
|
+
data:
|
|
63
|
+
path === "dashboards/overview.malloy"
|
|
64
|
+
? { modelPath: path, sourceText: serverText }
|
|
65
|
+
: { modelPath: path, sources: [], sourceInfos: [] },
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
const updateModelSource = mock(
|
|
69
|
+
async (
|
|
70
|
+
_env: string,
|
|
71
|
+
_pkg: string,
|
|
72
|
+
path: string,
|
|
73
|
+
body: { source: string; expectedHash?: string },
|
|
74
|
+
) => {
|
|
75
|
+
if (body.expectedHash !== (await sha256Hex(serverText)))
|
|
76
|
+
throw {
|
|
77
|
+
response: {
|
|
78
|
+
data: {
|
|
79
|
+
message:
|
|
80
|
+
"`dashboards/overview.malloy` changed in the package since you opened it.",
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
serverText = body.source;
|
|
85
|
+
return {
|
|
86
|
+
data: {
|
|
87
|
+
path,
|
|
88
|
+
contentHash: await sha256Hex(serverText),
|
|
89
|
+
created: false,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
// Spread into `useServer()` on every render, so a test can change what the
|
|
96
|
+
// server says about itself between mounts.
|
|
97
|
+
const serverContext = { mutable: false };
|
|
98
|
+
mockServerProvider(
|
|
99
|
+
{
|
|
100
|
+
models: {
|
|
101
|
+
getModel,
|
|
102
|
+
executeQueryModel: mock(() => pending()),
|
|
103
|
+
updateModelSource,
|
|
104
|
+
},
|
|
105
|
+
dashboards: {
|
|
106
|
+
getDashboard: mock(() =>
|
|
107
|
+
Promise.resolve({
|
|
108
|
+
data: {
|
|
109
|
+
path: "dashboards/overview.malloy",
|
|
110
|
+
givens: [],
|
|
111
|
+
tiles: [],
|
|
112
|
+
},
|
|
113
|
+
}),
|
|
114
|
+
),
|
|
115
|
+
listDashboards: mock(() =>
|
|
116
|
+
Promise.resolve({ data: [{ name: "overview" }] }),
|
|
117
|
+
),
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
serverContext,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const { DashboardEditor } = await import("./DashboardEditor");
|
|
124
|
+
|
|
125
|
+
const PATH = "env/pkg/dashboards/overview.malloy";
|
|
126
|
+
const RECORD: Workspace = {
|
|
127
|
+
name: "Branch",
|
|
128
|
+
writeable: true,
|
|
129
|
+
description: "Saved to the draft branch",
|
|
130
|
+
authoritative: true,
|
|
131
|
+
};
|
|
132
|
+
const READ_ONLY_RECORD: Workspace = {
|
|
133
|
+
name: "Branch",
|
|
134
|
+
writeable: false,
|
|
135
|
+
description: "Saved to the draft branch",
|
|
136
|
+
authoritative: true,
|
|
137
|
+
};
|
|
138
|
+
const BESIDE: Workspace = {
|
|
139
|
+
name: "Branch",
|
|
140
|
+
writeable: true,
|
|
141
|
+
description: "Saved to the draft branch",
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A host store the spec can break on purpose: a read or a delete that rejects
|
|
146
|
+
* with something other than absence is the case the editor used to swallow.
|
|
147
|
+
*/
|
|
148
|
+
class FakeStorage implements DocumentStorage {
|
|
149
|
+
readonly documents = new Map<string, string>();
|
|
150
|
+
readFailure: unknown;
|
|
151
|
+
deleteFailure: unknown;
|
|
152
|
+
/** Slower than the model fetch, so the package answer arrives first. */
|
|
153
|
+
slow = false;
|
|
154
|
+
constructor(private readonly workspace: Workspace) {}
|
|
155
|
+
|
|
156
|
+
async listWorkspaces(writeableOnly: boolean): Promise<Workspace[]> {
|
|
157
|
+
if (this.slow) await new Promise((resolve) => setTimeout(resolve, 150));
|
|
158
|
+
// Honoured, not ignored: a fixture that hands back a read-only
|
|
159
|
+
// workspace to a caller asking for writeable ones cannot tell whether
|
|
160
|
+
// the editor asked the right question.
|
|
161
|
+
return writeableOnly && !this.workspace.writeable ? [] : [this.workspace];
|
|
162
|
+
}
|
|
163
|
+
async listDocuments(
|
|
164
|
+
_workspace: Workspace,
|
|
165
|
+
_type?: DocumentType,
|
|
166
|
+
): Promise<DocumentLocator[]> {
|
|
167
|
+
return [...this.documents.keys()].map((path) => ({
|
|
168
|
+
workspace: this.workspace.name,
|
|
169
|
+
type: "dashboard" as const,
|
|
170
|
+
path,
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
async getDocument(locator: DocumentLocator): Promise<string> {
|
|
174
|
+
if (this.readFailure !== undefined) throw this.readFailure;
|
|
175
|
+
const content = this.documents.get(locator.path);
|
|
176
|
+
if (content === undefined)
|
|
177
|
+
throw new DocumentNotFoundError(`No dashboard at ${locator.path}`);
|
|
178
|
+
return content;
|
|
179
|
+
}
|
|
180
|
+
async saveDocument(
|
|
181
|
+
locator: DocumentLocator,
|
|
182
|
+
content: string,
|
|
183
|
+
): Promise<void> {
|
|
184
|
+
this.documents.set(locator.path, content);
|
|
185
|
+
}
|
|
186
|
+
async deleteDocument(locator: DocumentLocator): Promise<void> {
|
|
187
|
+
if (this.deleteFailure !== undefined) throw this.deleteFailure;
|
|
188
|
+
if (!this.documents.has(locator.path))
|
|
189
|
+
throw new DocumentNotFoundError(`No dashboard at ${locator.path}`);
|
|
190
|
+
this.documents.delete(locator.path);
|
|
191
|
+
}
|
|
192
|
+
async moveDocument(): Promise<void> {
|
|
193
|
+
throw new Error("not exercised");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const mount = (
|
|
198
|
+
storage: DocumentStorage,
|
|
199
|
+
onDirtyChange?: (dirty: boolean) => void,
|
|
200
|
+
onEvent?: (event: DashboardEvent) => void,
|
|
201
|
+
) =>
|
|
202
|
+
render(
|
|
203
|
+
<DocumentStorageProvider documentStorage={storage}>
|
|
204
|
+
<DashboardEditor
|
|
205
|
+
environmentName="env"
|
|
206
|
+
packageName="pkg"
|
|
207
|
+
dashboardName="overview"
|
|
208
|
+
{...(onDirtyChange ? { onDirtyChange } : {})}
|
|
209
|
+
{...(onEvent ? { onEvent } : {})}
|
|
210
|
+
/>
|
|
211
|
+
</DocumentStorageProvider>,
|
|
212
|
+
{ wrapper: serverWrapper },
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
const button = (name: string | RegExp) =>
|
|
216
|
+
screen.getByRole("button", { name, hidden: true });
|
|
217
|
+
|
|
218
|
+
/** Rename the one tile, which is a non-structural edit and saves directly. */
|
|
219
|
+
const renameTile = (to: string, from = "By category") => {
|
|
220
|
+
fireEvent.click(screen.getByLabelText(`Settings for ${from}`));
|
|
221
|
+
fireEvent.change(screen.getByLabelText("Tile title"), {
|
|
222
|
+
target: { value: to },
|
|
223
|
+
});
|
|
224
|
+
fireEvent.keyDown(screen.getByLabelText("Tile title"), { key: "Escape" });
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Let everything the last interaction started finish. A re-open the editor
|
|
229
|
+
* should not have started parses its text and remounts the builder a tick
|
|
230
|
+
* later, so asserting the instant a save reports "Saved" asserts too early to
|
|
231
|
+
* see it.
|
|
232
|
+
*/
|
|
233
|
+
const settle = async () => {
|
|
234
|
+
await act(async () => {
|
|
235
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* A version of the file arriving from somewhere other than this editor: a real
|
|
241
|
+
* refetch of the query the editor reads, driven to completion rather than
|
|
242
|
+
* waited on, so what follows sees the render it caused.
|
|
243
|
+
*/
|
|
244
|
+
const packageChangedTo = async (text: string) => {
|
|
245
|
+
serverText = text;
|
|
246
|
+
await act(async () => {
|
|
247
|
+
await globalQueryClient.refetchQueries({
|
|
248
|
+
queryKey: ["dashboard-editor-model"],
|
|
249
|
+
});
|
|
250
|
+
// react-query hands the new data to its subscribers on a scheduled
|
|
251
|
+
// batch, so the refetch resolving is not yet the render it causes.
|
|
252
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
beforeEach(() => {
|
|
257
|
+
// Explicitly, because nothing registers testing-library's auto-cleanup
|
|
258
|
+
// here: every test in this file mounts the same dashboard, so a container
|
|
259
|
+
// left behind is a second answer to every query.
|
|
260
|
+
cleanup();
|
|
261
|
+
clearCache();
|
|
262
|
+
localStorage.clear();
|
|
263
|
+
serverText = PACKAGE_FILE;
|
|
264
|
+
serverContext.mutable = false;
|
|
265
|
+
updateModelSource.mockClear();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
describe("DashboardEditor, when the host's store is the record", () => {
|
|
269
|
+
it("opens the store's copy rather than the package file, and never offers to resume it", async () => {
|
|
270
|
+
const storage = new FakeStorage(RECORD);
|
|
271
|
+
storage.documents.set(PATH, withTitle("Recorded"));
|
|
272
|
+
const onEvent = mock((_event: DashboardEvent) => {});
|
|
273
|
+
mount(storage, undefined, onEvent);
|
|
274
|
+
|
|
275
|
+
expect(await screen.findByText("Recorded")).toBeDefined();
|
|
276
|
+
expect(screen.queryByText("Storefront")).toBeNull();
|
|
277
|
+
// Nothing is pending, so there is nothing to resume.
|
|
278
|
+
expect(screen.queryByText(/edits to this dashboard/)).toBeNull();
|
|
279
|
+
expect(onEvent.mock.calls[0][0]).toMatchObject({
|
|
280
|
+
type: "dashboard.opened",
|
|
281
|
+
from: "record",
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("leaves that same copy merely offered when the workspace is not the record", async () => {
|
|
286
|
+
const storage = new FakeStorage(BESIDE);
|
|
287
|
+
storage.documents.set(PATH, withTitle("Recorded"));
|
|
288
|
+
mount(storage);
|
|
289
|
+
|
|
290
|
+
expect(await screen.findByText("Storefront")).toBeDefined();
|
|
291
|
+
expect(await screen.findByText(/edits to this dashboard/)).toBeDefined();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("opens the package file when the record has no copy yet, and the first save keeps the history", async () => {
|
|
295
|
+
const storage = new FakeStorage(RECORD);
|
|
296
|
+
mount(storage);
|
|
297
|
+
|
|
298
|
+
expect(await screen.findByText("Storefront")).toBeDefined();
|
|
299
|
+
renameTile("Categories");
|
|
300
|
+
fireEvent.click(button("Save changes"));
|
|
301
|
+
|
|
302
|
+
await waitFor(() =>
|
|
303
|
+
expect(storage.documents.get(PATH)).toContain('# label="Categories"'),
|
|
304
|
+
);
|
|
305
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
306
|
+
await settle();
|
|
307
|
+
// The save wrote the text the builder is holding, so the builder was not
|
|
308
|
+
// remounted onto it: its undo history is still there.
|
|
309
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("writes to the record even when the server says it takes writes", async () => {
|
|
313
|
+
// The precedence that matters in production: a host whose store is the
|
|
314
|
+
// record commonly sits on a server that reports itself writable, and
|
|
315
|
+
// writing the package there edits a deploy of the record, not the record.
|
|
316
|
+
serverContext.mutable = true;
|
|
317
|
+
const storage = new FakeStorage(RECORD);
|
|
318
|
+
storage.documents.set(PATH, withTitle("Recorded"));
|
|
319
|
+
mount(storage);
|
|
320
|
+
|
|
321
|
+
expect(await screen.findByText("Recorded")).toBeDefined();
|
|
322
|
+
renameTile("Categories");
|
|
323
|
+
fireEvent.click(button("Save changes"));
|
|
324
|
+
|
|
325
|
+
await waitFor(() =>
|
|
326
|
+
expect(storage.documents.get(PATH)).toContain('# label="Categories"'),
|
|
327
|
+
);
|
|
328
|
+
expect(updateModelSource).not.toHaveBeenCalled();
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("shows the record to a reader who cannot write to it, and offers no Save", async () => {
|
|
332
|
+
// Hiding it would fall back to the package, which on a server that takes
|
|
333
|
+
// writes means publishing a deploy of the record over the record.
|
|
334
|
+
serverContext.mutable = true;
|
|
335
|
+
const storage = new FakeStorage(READ_ONLY_RECORD);
|
|
336
|
+
storage.documents.set(PATH, withTitle("Recorded"));
|
|
337
|
+
mount(storage);
|
|
338
|
+
|
|
339
|
+
expect(await screen.findByText("Recorded")).toBeDefined();
|
|
340
|
+
expect(screen.queryByText("Storefront")).toBeNull();
|
|
341
|
+
expect(
|
|
342
|
+
screen.queryByRole("button", { name: "Save changes", hidden: true }),
|
|
343
|
+
).toBeNull();
|
|
344
|
+
expect(screen.getByText(/you cannot save into it/)).toBeDefined();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("does not open the package first when the storage answer is slower", async () => {
|
|
348
|
+
// Whether the host keeps the record is not known until storage answers.
|
|
349
|
+
// Opening the package meanwhile puts a reader on a deploy of the record
|
|
350
|
+
// and reports an open of it.
|
|
351
|
+
const storage = new FakeStorage(RECORD);
|
|
352
|
+
storage.slow = true;
|
|
353
|
+
storage.documents.set(PATH, withTitle("Recorded"));
|
|
354
|
+
const onEvent = mock((_event: DashboardEvent) => {});
|
|
355
|
+
mount(storage, undefined, onEvent);
|
|
356
|
+
|
|
357
|
+
expect(await screen.findByText("Recorded")).toBeDefined();
|
|
358
|
+
await settle();
|
|
359
|
+
const opens = onEvent.mock.calls
|
|
360
|
+
.map((call) => call[0])
|
|
361
|
+
.filter((event) => event.type === "dashboard.opened");
|
|
362
|
+
expect(opens).toHaveLength(1);
|
|
363
|
+
expect(opens[0]).toMatchObject({ from: "record" });
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it("says where the record is, in the workspace's own words", async () => {
|
|
367
|
+
const storage = new FakeStorage(RECORD);
|
|
368
|
+
storage.documents.set(PATH, PACKAGE_FILE);
|
|
369
|
+
mount(storage);
|
|
370
|
+
expect(
|
|
371
|
+
await screen.findByText("Saved to the draft branch"),
|
|
372
|
+
).toBeDefined();
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it("keeps a reader out when the record could not be read", async () => {
|
|
376
|
+
const storage = new FakeStorage(RECORD);
|
|
377
|
+
storage.readFailure = new Error("the branch could not be reached");
|
|
378
|
+
mount(storage);
|
|
379
|
+
|
|
380
|
+
expect(
|
|
381
|
+
await screen.findByText(/the branch could not be reached/),
|
|
382
|
+
).toBeDefined();
|
|
383
|
+
// Opening the package and arming Save here would publish a deploy of the
|
|
384
|
+
// record over the record.
|
|
385
|
+
expect(screen.queryByText("Storefront")).toBeNull();
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
describe("DashboardEditor, when the copy is kept beside the package", () => {
|
|
390
|
+
it("opens the package but withholds Save when the saved copy could not be read", async () => {
|
|
391
|
+
const storage = new FakeStorage(BESIDE);
|
|
392
|
+
storage.readFailure = new Error("the branch could not be reached");
|
|
393
|
+
mount(storage);
|
|
394
|
+
|
|
395
|
+
expect(await screen.findByText("Storefront")).toBeDefined();
|
|
396
|
+
expect(
|
|
397
|
+
screen.getByText(/saved copy could not be read, so Save is off/),
|
|
398
|
+
).toBeDefined();
|
|
399
|
+
renameTile("Categories");
|
|
400
|
+
expect(
|
|
401
|
+
screen.queryByRole("button", { name: "Save changes", hidden: true }),
|
|
402
|
+
).toBeNull();
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it("says where the Console's copy goes, in that store's own words", async () => {
|
|
406
|
+
mount(new BrowserDocumentStorage());
|
|
407
|
+
expect(
|
|
408
|
+
await screen.findByText(/Stored in this browser only/),
|
|
409
|
+
).toBeDefined();
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it("keeps the copy offered when superseding it fails for a reason other than absence", async () => {
|
|
413
|
+
serverContext.mutable = true;
|
|
414
|
+
const storage = new FakeStorage(BESIDE);
|
|
415
|
+
storage.documents.set(PATH, withTitle("Drafted"));
|
|
416
|
+
storage.deleteFailure = new Error("the branch could not be reached");
|
|
417
|
+
mount(storage);
|
|
418
|
+
|
|
419
|
+
await screen.findByText("Storefront");
|
|
420
|
+
renameTile("Categories");
|
|
421
|
+
fireEvent.click(button("Save changes"));
|
|
422
|
+
|
|
423
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
424
|
+
expect(
|
|
425
|
+
await screen.findByText(/copy kept beside it could not be cleared/),
|
|
426
|
+
).toBeDefined();
|
|
427
|
+
// The backend's own reason, not a swallowed failure.
|
|
428
|
+
expect(screen.getByText(/the branch could not be reached/)).toBeDefined();
|
|
429
|
+
// The save stands, and the editor stays on it rather than on the copy.
|
|
430
|
+
expect(screen.getByText("Storefront")).toBeDefined();
|
|
431
|
+
await settle();
|
|
432
|
+
expect(screen.getByLabelText("Settings for Categories")).toBeDefined();
|
|
433
|
+
|
|
434
|
+
// The copy really is still there: a fresh visit is offered it again.
|
|
435
|
+
cleanup();
|
|
436
|
+
clearCache();
|
|
437
|
+
serverContext.mutable = true;
|
|
438
|
+
mount(storage);
|
|
439
|
+
expect(await screen.findByText(/edits to this dashboard/)).toBeDefined();
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it("keeps a resumed copy on screen after saving it into the package, and splices the next save onto it", async () => {
|
|
443
|
+
serverContext.mutable = true;
|
|
444
|
+
const storage = new FakeStorage(BESIDE);
|
|
445
|
+
storage.documents.set(PATH, withTitle("Drafted"));
|
|
446
|
+
mount(storage);
|
|
447
|
+
|
|
448
|
+
await screen.findByText("Storefront");
|
|
449
|
+
fireEvent.click(button("Resume"));
|
|
450
|
+
expect(await screen.findByText("Drafted")).toBeDefined();
|
|
451
|
+
|
|
452
|
+
renameTile("Categories");
|
|
453
|
+
fireEvent.click(button("Save changes"));
|
|
454
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
455
|
+
await settle();
|
|
456
|
+
expect(updateModelSource).toHaveBeenCalledTimes(1);
|
|
457
|
+
// The save's own text, not the package file it replaced.
|
|
458
|
+
expect(screen.getByText("Drafted")).toBeDefined();
|
|
459
|
+
expect(screen.queryByText("Storefront")).toBeNull();
|
|
460
|
+
// And on the same mount, so the reader's undo history survived it.
|
|
461
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
462
|
+
|
|
463
|
+
renameTile("Regions", "Categories");
|
|
464
|
+
fireEvent.click(button("Save changes"));
|
|
465
|
+
// On "Saved" rather than on the call count, which rises before the write
|
|
466
|
+
// it started has finished and would leave it running into the next test.
|
|
467
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
468
|
+
await settle();
|
|
469
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
470
|
+
expect(updateModelSource).toHaveBeenCalledTimes(2);
|
|
471
|
+
const second = updateModelSource.mock.calls[1][3].source;
|
|
472
|
+
expect(second).toContain('title="Drafted"');
|
|
473
|
+
expect(second).toContain('# label="Regions"');
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* A save leaves the builder holding text the fetches behind it have not caught
|
|
479
|
+
* up with. Everything here is about that window: what the editor believes is
|
|
480
|
+
* open in it, and what it does with a version that lands during or after it.
|
|
481
|
+
*/
|
|
482
|
+
describe("DashboardEditor, after a save", () => {
|
|
483
|
+
it("keeps the next edit, on the package path", async () => {
|
|
484
|
+
serverContext.mutable = true;
|
|
485
|
+
mount(new FakeStorage(BESIDE));
|
|
486
|
+
|
|
487
|
+
await screen.findByText("Storefront");
|
|
488
|
+
renameTile("Categories");
|
|
489
|
+
fireEvent.click(button("Save changes"));
|
|
490
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
491
|
+
await settle();
|
|
492
|
+
// The save alone must not have remounted the builder: its own write
|
|
493
|
+
// coming back is not a new version of the file.
|
|
494
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
495
|
+
|
|
496
|
+
renameTile("Regions", "Categories");
|
|
497
|
+
await settle();
|
|
498
|
+
// The save's own text coming back is not a version to open, so the
|
|
499
|
+
// builder is not remounted out from under the edit that follows it, and
|
|
500
|
+
// the reader is not offered their own save back as someone else's.
|
|
501
|
+
expect(screen.getByLabelText("Settings for Regions")).toBeDefined();
|
|
502
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
503
|
+
expect(screen.queryByText(/changed since you opened it/)).toBeNull();
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("keeps the next edit, on the record", async () => {
|
|
507
|
+
const storage = new FakeStorage(RECORD);
|
|
508
|
+
storage.documents.set(PATH, PACKAGE_FILE);
|
|
509
|
+
mount(storage);
|
|
510
|
+
|
|
511
|
+
await screen.findByText("Storefront");
|
|
512
|
+
renameTile("Categories");
|
|
513
|
+
fireEvent.click(button("Save changes"));
|
|
514
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
515
|
+
await settle();
|
|
516
|
+
// The save alone must not have remounted the builder: its own write
|
|
517
|
+
// coming back is not a new version of the file.
|
|
518
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
519
|
+
|
|
520
|
+
renameTile("Regions", "Categories");
|
|
521
|
+
await settle();
|
|
522
|
+
expect(screen.getByLabelText("Settings for Regions")).toBeDefined();
|
|
523
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
524
|
+
expect(screen.queryByText(/changed since you opened it/)).toBeNull();
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
it("keeps the next edit, on the Console's copy beside the package", async () => {
|
|
528
|
+
const storage = new FakeStorage(BESIDE);
|
|
529
|
+
mount(storage);
|
|
530
|
+
|
|
531
|
+
await screen.findByText("Storefront");
|
|
532
|
+
renameTile("Categories");
|
|
533
|
+
fireEvent.click(button("Save changes"));
|
|
534
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
535
|
+
await settle();
|
|
536
|
+
// The save alone must not have remounted the builder: its own write
|
|
537
|
+
// coming back is not a new version of the file.
|
|
538
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
539
|
+
|
|
540
|
+
renameTile("Regions", "Categories");
|
|
541
|
+
await settle();
|
|
542
|
+
expect(screen.getByLabelText("Settings for Regions")).toBeDefined();
|
|
543
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
544
|
+
expect(screen.queryByText(/changed since you opened it/)).toBeNull();
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
it("keeps the next edit after re-saving a copy the reader resumed", async () => {
|
|
548
|
+
// The resumed copy IS what is open, so saving it again moves that
|
|
549
|
+
// channel; reading the write back as news offers the reader their own
|
|
550
|
+
// save as someone else's.
|
|
551
|
+
const storage = new FakeStorage(BESIDE);
|
|
552
|
+
storage.documents.set(PATH, withTitle("Drafted"));
|
|
553
|
+
mount(storage);
|
|
554
|
+
|
|
555
|
+
await screen.findByText("Storefront");
|
|
556
|
+
fireEvent.click(button("Resume"));
|
|
557
|
+
await screen.findByText("Drafted");
|
|
558
|
+
|
|
559
|
+
renameTile("Categories");
|
|
560
|
+
fireEvent.click(button("Save changes"));
|
|
561
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
562
|
+
await settle();
|
|
563
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(false);
|
|
564
|
+
|
|
565
|
+
renameTile("Regions", "Categories");
|
|
566
|
+
await settle();
|
|
567
|
+
expect(screen.getByLabelText("Settings for Regions")).toBeDefined();
|
|
568
|
+
expect(screen.queryByText(/changed since you opened it/)).toBeNull();
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it("offers a version another writer landed while the save was in flight", async () => {
|
|
572
|
+
// The case compare-and-swap exists for: this editor's write never comes
|
|
573
|
+
// back, because someone else's landed after it. Reading "the fetch does
|
|
574
|
+
// not match what I wrote" as "not news" would hide it for good.
|
|
575
|
+
serverContext.mutable = true;
|
|
576
|
+
mount(new FakeStorage(BESIDE));
|
|
577
|
+
|
|
578
|
+
await screen.findByText("Storefront");
|
|
579
|
+
renameTile("Categories");
|
|
580
|
+
fireEvent.click(button("Save changes"));
|
|
581
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
582
|
+
await settle();
|
|
583
|
+
|
|
584
|
+
await packageChangedTo(withTitle("Elsewhere"));
|
|
585
|
+
expect(await screen.findByText("Elsewhere")).toBeDefined();
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
it("still sees the package move after a copy is saved beside it", async () => {
|
|
589
|
+
// The copy going into storage leaves the package where it was, so the
|
|
590
|
+
// package's own next move is news even though the builder is ahead of it.
|
|
591
|
+
const storage = new FakeStorage(BESIDE);
|
|
592
|
+
mount(storage);
|
|
593
|
+
|
|
594
|
+
await screen.findByText("Storefront");
|
|
595
|
+
renameTile("Categories");
|
|
596
|
+
fireEvent.click(button("Save changes"));
|
|
597
|
+
await waitFor(() => expect(button("Saved")).toBeDefined());
|
|
598
|
+
await settle();
|
|
599
|
+
|
|
600
|
+
await packageChangedTo(withTitle("Elsewhere"));
|
|
601
|
+
expect(await screen.findByText("Elsewhere")).toBeDefined();
|
|
602
|
+
});
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
describe("DashboardEditor, when a new version of the file arrives", () => {
|
|
606
|
+
it("holds it back rather than replacing unsaved edits, and loads it when the reader says so", async () => {
|
|
607
|
+
serverContext.mutable = true;
|
|
608
|
+
mount(new FakeStorage(BESIDE));
|
|
609
|
+
|
|
610
|
+
await screen.findByText("Storefront");
|
|
611
|
+
renameTile("Categories");
|
|
612
|
+
await packageChangedTo(withTitle("Elsewhere"));
|
|
613
|
+
|
|
614
|
+
expect(screen.getByText(/changed since you opened it/)).toBeDefined();
|
|
615
|
+
// The edit is still here, and so is the text it was made against.
|
|
616
|
+
expect(screen.getByText("Storefront")).toBeDefined();
|
|
617
|
+
expect(screen.getByLabelText("Settings for Categories")).toBeDefined();
|
|
618
|
+
|
|
619
|
+
fireEvent.click(button("Load it"));
|
|
620
|
+
expect(await screen.findByText("Elsewhere")).toBeDefined();
|
|
621
|
+
await waitFor(() =>
|
|
622
|
+
expect(screen.queryByText(/changed since you opened it/)).toBeNull(),
|
|
623
|
+
);
|
|
624
|
+
expect(button("Undo").hasAttribute("disabled")).toBe(true);
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it("still hands back the hash of the text it opened, so the stale save is refused", async () => {
|
|
628
|
+
serverContext.mutable = true;
|
|
629
|
+
mount(new FakeStorage(BESIDE));
|
|
630
|
+
|
|
631
|
+
await screen.findByText("Storefront");
|
|
632
|
+
renameTile("Categories");
|
|
633
|
+
await packageChangedTo(withTitle("Elsewhere"));
|
|
634
|
+
expect(screen.getByText(/changed since you opened it/)).toBeDefined();
|
|
635
|
+
|
|
636
|
+
fireEvent.click(button("Save changes"));
|
|
637
|
+
await waitFor(() => expect(updateModelSource).toHaveBeenCalledTimes(1));
|
|
638
|
+
expect(updateModelSource.mock.calls[0][3].expectedHash).toBe(
|
|
639
|
+
await sha256Hex(PACKAGE_FILE),
|
|
640
|
+
);
|
|
641
|
+
await waitFor(() =>
|
|
642
|
+
expect(
|
|
643
|
+
screen
|
|
644
|
+
.getAllByRole("alert")
|
|
645
|
+
.some((alert) =>
|
|
646
|
+
alert.textContent?.includes(
|
|
647
|
+
"changed in the package since you opened it",
|
|
648
|
+
),
|
|
649
|
+
),
|
|
650
|
+
).toBe(true),
|
|
651
|
+
);
|
|
652
|
+
// Refused, so the file the other writer left is intact.
|
|
653
|
+
expect(serverText).toBe(withTitle("Elsewhere"));
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
it("fetches the version behind a refused save, so the reader can take it", async () => {
|
|
657
|
+
// The conflict this editor never saw coming: the file moved with no
|
|
658
|
+
// refetch, so the refusal is the first news of it. Without going and
|
|
659
|
+
// getting that version, every further save is refused the same way and
|
|
660
|
+
// there is nothing on screen to load.
|
|
661
|
+
serverContext.mutable = true;
|
|
662
|
+
mount(new FakeStorage(BESIDE));
|
|
663
|
+
|
|
664
|
+
await screen.findByText("Storefront");
|
|
665
|
+
renameTile("Categories");
|
|
666
|
+
// Moved underneath, with nothing telling the editor.
|
|
667
|
+
serverText = withTitle("Elsewhere");
|
|
668
|
+
|
|
669
|
+
fireEvent.click(button("Save changes"));
|
|
670
|
+
await waitFor(() =>
|
|
671
|
+
expect(
|
|
672
|
+
screen
|
|
673
|
+
.getAllByRole("alert")
|
|
674
|
+
.some((alert) =>
|
|
675
|
+
alert.textContent?.includes(
|
|
676
|
+
"changed in the package since you opened it",
|
|
677
|
+
),
|
|
678
|
+
),
|
|
679
|
+
).toBe(true),
|
|
680
|
+
);
|
|
681
|
+
expect(
|
|
682
|
+
await screen.findByText(/changed since you opened it/),
|
|
683
|
+
).toBeDefined();
|
|
684
|
+
// And the reader's edits are still theirs to keep or drop.
|
|
685
|
+
expect(screen.getByLabelText("Settings for Categories")).toBeDefined();
|
|
686
|
+
fireEvent.click(button("Load it"));
|
|
687
|
+
expect(await screen.findByText("Elsewhere")).toBeDefined();
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
it("tells the host when there are edits the record does not have", async () => {
|
|
691
|
+
serverContext.mutable = true;
|
|
692
|
+
const seen: boolean[] = [];
|
|
693
|
+
mount(new FakeStorage(BESIDE), (value) => seen.push(value));
|
|
694
|
+
|
|
695
|
+
await screen.findByText("Storefront");
|
|
696
|
+
await waitFor(() => expect(seen).toEqual([false]));
|
|
697
|
+
renameTile("Categories");
|
|
698
|
+
await waitFor(() => expect(seen.at(-1)).toBe(true));
|
|
699
|
+
fireEvent.click(button("Save changes"));
|
|
700
|
+
await waitFor(() => expect(seen.at(-1)).toBe(false));
|
|
701
|
+
await settle();
|
|
702
|
+
// Exactly those three: the builder fires this from an effect, so a
|
|
703
|
+
// handler whose identity changed each render would repeat its answer.
|
|
704
|
+
expect(seen).toEqual([false, true, false]);
|
|
705
|
+
});
|
|
706
|
+
});
|