@milaboratories/pl-model-middle-layer 1.32.1 → 1.33.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/dist/folders/commit.cjs +79 -0
- package/dist/folders/commit.cjs.map +1 -0
- package/dist/folders/commit.d.ts +82 -0
- package/dist/folders/commit.d.ts.map +1 -0
- package/dist/folders/commit.js +78 -0
- package/dist/folders/commit.js.map +1 -0
- package/dist/folders/decode.cjs +79 -0
- package/dist/folders/decode.cjs.map +1 -0
- package/dist/folders/decode.d.ts +57 -0
- package/dist/folders/decode.d.ts.map +1 -0
- package/dist/folders/decode.js +77 -0
- package/dist/folders/decode.js.map +1 -0
- package/dist/folders/document.cjs +49 -0
- package/dist/folders/document.cjs.map +1 -0
- package/dist/folders/document.d.ts +56 -0
- package/dist/folders/document.d.ts.map +1 -0
- package/dist/folders/document.js +45 -0
- package/dist/folders/document.js.map +1 -0
- package/dist/folders/index.cjs +37 -0
- package/dist/folders/index.d.ts +10 -0
- package/dist/folders/index.js +10 -0
- package/dist/folders/inheritance.cjs +23 -0
- package/dist/folders/inheritance.cjs.map +1 -0
- package/dist/folders/inheritance.d.ts +19 -0
- package/dist/folders/inheritance.d.ts.map +1 -0
- package/dist/folders/inheritance.js +23 -0
- package/dist/folders/inheritance.js.map +1 -0
- package/dist/folders/invariants.cjs +115 -0
- package/dist/folders/invariants.cjs.map +1 -0
- package/dist/folders/invariants.d.ts +48 -0
- package/dist/folders/invariants.d.ts.map +1 -0
- package/dist/folders/invariants.js +114 -0
- package/dist/folders/invariants.js.map +1 -0
- package/dist/folders/naming.cjs +53 -0
- package/dist/folders/naming.cjs.map +1 -0
- package/dist/folders/naming.d.ts +31 -0
- package/dist/folders/naming.d.ts.map +1 -0
- package/dist/folders/naming.js +51 -0
- package/dist/folders/naming.js.map +1 -0
- package/dist/folders/planner.cjs +187 -0
- package/dist/folders/planner.cjs.map +1 -0
- package/dist/folders/planner.d.ts +100 -0
- package/dist/folders/planner.d.ts.map +1 -0
- package/dist/folders/planner.js +185 -0
- package/dist/folders/planner.js.map +1 -0
- package/dist/folders/removal.cjs +47 -0
- package/dist/folders/removal.cjs.map +1 -0
- package/dist/folders/removal.d.ts +42 -0
- package/dist/folders/removal.d.ts.map +1 -0
- package/dist/folders/removal.js +45 -0
- package/dist/folders/removal.js.map +1 -0
- package/dist/folders/view.cjs +210 -0
- package/dist/folders/view.cjs.map +1 -0
- package/dist/folders/view.d.ts +93 -0
- package/dist/folders/view.d.ts.map +1 -0
- package/dist/folders/view.js +205 -0
- package/dist/folders/view.js.map +1 -0
- package/dist/index.cjs +43 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +12 -1
- package/dist/project.cjs +23 -0
- package/dist/project.cjs.map +1 -0
- package/dist/project.d.ts +13 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +22 -0
- package/dist/project.js.map +1 -0
- package/package.json +7 -4
- package/src/folders/commit.ts +133 -0
- package/src/folders/decode.test.ts +128 -0
- package/src/folders/decode.ts +142 -0
- package/src/folders/document.ts +96 -0
- package/src/folders/index.ts +9 -0
- package/src/folders/inheritance.test.ts +86 -0
- package/src/folders/inheritance.ts +32 -0
- package/src/folders/invariants.test.ts +157 -0
- package/src/folders/invariants.ts +155 -0
- package/src/folders/naming.test.ts +62 -0
- package/src/folders/naming.ts +58 -0
- package/src/folders/planner.test.ts +436 -0
- package/src/folders/planner.ts +276 -0
- package/src/folders/removal.test.ts +142 -0
- package/src/folders/removal.ts +77 -0
- package/src/folders/test_utils.ts +86 -0
- package/src/folders/view.test.ts +422 -0
- package/src/folders/view.ts +324 -0
- package/src/index.ts +1 -0
- package/src/project.ts +21 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import type { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
|
|
2
|
+
import type { FoldersDocument, FolderId } from "./document";
|
|
3
|
+
import { foldersUniqueName } from "./naming";
|
|
4
|
+
import type { FoldersView } from "./view";
|
|
5
|
+
import { foldersDocumentFromView, foldersSiblingNames, foldersSubtree } from "./view";
|
|
6
|
+
|
|
7
|
+
/** A folder, a project or a stored template, as something a move acts on. */
|
|
8
|
+
export type FoldersItem =
|
|
9
|
+
| { readonly kind: "folder"; readonly id: FolderId }
|
|
10
|
+
| { readonly kind: "project"; readonly id: ProjectId }
|
|
11
|
+
| { readonly kind: "template"; readonly id: TemplateId };
|
|
12
|
+
|
|
13
|
+
/** A tree item that is not a folder: a project or a stored template. */
|
|
14
|
+
export type FoldersLeafItem = Exclude<FoldersItem, { readonly kind: "folder" }>;
|
|
15
|
+
|
|
16
|
+
/** What one item is called after the move, and what it was called before. */
|
|
17
|
+
export interface FoldersMoveEntry {
|
|
18
|
+
readonly item: FoldersItem;
|
|
19
|
+
readonly currentName: string;
|
|
20
|
+
/** The name the item carries once the plan is applied. */
|
|
21
|
+
readonly name: string;
|
|
22
|
+
/** True when the destination already held that name and the item is being renamed. */
|
|
23
|
+
readonly renamed: boolean;
|
|
24
|
+
/** Folder the item is coming from; absent when it is at the top level. */
|
|
25
|
+
readonly sourceFolder?: FolderId;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Every item of a move and its resulting name.
|
|
30
|
+
*
|
|
31
|
+
* A plan is computed for the preview and recomputed inside the write, and the write commits only
|
|
32
|
+
* when the two are identical, so a plan confirmed against state that has since changed is refused
|
|
33
|
+
* rather than applied. Entries are in a canonical order for exactly that comparison.
|
|
34
|
+
*/
|
|
35
|
+
export interface FoldersMovePlan {
|
|
36
|
+
/** Folder everything lands in; absent for the top level. */
|
|
37
|
+
readonly destination?: FolderId;
|
|
38
|
+
readonly entries: readonly FoldersMoveEntry[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One reason a move cannot be planned. */
|
|
42
|
+
export type FoldersMoveIssue =
|
|
43
|
+
| { readonly kind: "unknown-destination"; readonly folder: FolderId }
|
|
44
|
+
| { readonly kind: "unknown-item"; readonly item: FoldersItem }
|
|
45
|
+
| {
|
|
46
|
+
readonly kind: "into-own-descendant";
|
|
47
|
+
readonly folder: FolderId;
|
|
48
|
+
readonly destination: FolderId;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** A plan, or everything standing in the way of one. */
|
|
52
|
+
export type FoldersMovePlanResult =
|
|
53
|
+
| { readonly ok: true; readonly plan: FoldersMovePlan }
|
|
54
|
+
| { readonly ok: false; readonly issues: readonly FoldersMoveIssue[] };
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Plan a move of any mix of folders, projects and templates into one destination.
|
|
58
|
+
*
|
|
59
|
+
* The only function that decides what a moved item ends up called. The preview and the write both
|
|
60
|
+
* call it, over the same view, so the two cannot drift apart.
|
|
61
|
+
*
|
|
62
|
+
* A folder moves with everything inside it. When the selection holds a folder together with
|
|
63
|
+
* something inside it, that item travels with the folder and is not planned on its own: moving it
|
|
64
|
+
* separately as well would pull it out of the subtree it belongs to.
|
|
65
|
+
*/
|
|
66
|
+
export function planFoldersMove(
|
|
67
|
+
view: FoldersView,
|
|
68
|
+
items: readonly FoldersItem[],
|
|
69
|
+
destination?: FolderId,
|
|
70
|
+
): FoldersMovePlanResult {
|
|
71
|
+
const issues: FoldersMoveIssue[] = [];
|
|
72
|
+
|
|
73
|
+
const destinationKnown =
|
|
74
|
+
destination === undefined || view.folders.some((folder) => folder.id === destination);
|
|
75
|
+
if (destination !== undefined && !destinationKnown)
|
|
76
|
+
issues.push({ kind: "unknown-destination", folder: destination });
|
|
77
|
+
|
|
78
|
+
const located: Located[] = [];
|
|
79
|
+
const seen = new Set<string>();
|
|
80
|
+
for (const item of items) {
|
|
81
|
+
const key = `${item.kind}:${item.id}`;
|
|
82
|
+
if (seen.has(key)) continue;
|
|
83
|
+
seen.add(key);
|
|
84
|
+
|
|
85
|
+
const known = locate(view, item);
|
|
86
|
+
if (known === undefined) {
|
|
87
|
+
issues.push({ kind: "unknown-item", item });
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
located.push({ item, ...known });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const carried = new Set<FolderId>(
|
|
94
|
+
located.flatMap(({ item }) => (item.kind === "folder" ? foldersSubtree(view, item.id) : [])),
|
|
95
|
+
);
|
|
96
|
+
const moved = located.filter(
|
|
97
|
+
({ sourceFolder }) => sourceFolder === undefined || !carried.has(sourceFolder),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
if (destination !== undefined && destinationKnown)
|
|
101
|
+
for (const { item } of moved) {
|
|
102
|
+
if (item.kind !== "folder") continue;
|
|
103
|
+
if (foldersSubtree(view, item.id).includes(destination))
|
|
104
|
+
issues.push({ kind: "into-own-descendant", folder: item.id, destination });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (issues.length > 0) return { ok: false, issues };
|
|
108
|
+
|
|
109
|
+
const taken = foldersSiblingNames(
|
|
110
|
+
view,
|
|
111
|
+
destination,
|
|
112
|
+
moved.map(({ item }) => item.id),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const entries: FoldersMoveEntry[] = [];
|
|
116
|
+
for (const { item, name, sourceFolder } of moved.sort(byCanonicalOrder)) {
|
|
117
|
+
const resulting = foldersUniqueName(name, taken);
|
|
118
|
+
taken.push(resulting);
|
|
119
|
+
entries.push({
|
|
120
|
+
item,
|
|
121
|
+
currentName: name,
|
|
122
|
+
name: resulting,
|
|
123
|
+
renamed: resulting !== name,
|
|
124
|
+
...(sourceFolder === undefined ? {} : { sourceFolder }),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
ok: true,
|
|
130
|
+
plan: { ...(destination === undefined ? {} : { destination }), entries },
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** True when two plans say exactly the same thing. */
|
|
135
|
+
export function foldersMovePlansEqual(a: FoldersMovePlan, b: FoldersMovePlan): boolean {
|
|
136
|
+
if (a.destination !== b.destination) return false;
|
|
137
|
+
if (a.entries.length !== b.entries.length) return false;
|
|
138
|
+
return a.entries.every((entry, index) => {
|
|
139
|
+
const other = b.entries[index];
|
|
140
|
+
if (other === undefined) return false;
|
|
141
|
+
return (
|
|
142
|
+
entry.item.kind === other.item.kind &&
|
|
143
|
+
entry.item.id === other.item.id &&
|
|
144
|
+
entry.currentName === other.currentName &&
|
|
145
|
+
entry.name === other.name &&
|
|
146
|
+
entry.renamed === other.renamed &&
|
|
147
|
+
entry.sourceFolder === other.sourceFolder
|
|
148
|
+
);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* A project or a template the move gives a new name. Their names live in their own metadata, not
|
|
154
|
+
* in the folder document, so the write path has to carry these out separately; a folder's name is
|
|
155
|
+
* in the document and is renamed there.
|
|
156
|
+
*/
|
|
157
|
+
export interface FoldersLabelRename {
|
|
158
|
+
readonly item: FoldersLeafItem;
|
|
159
|
+
readonly name: string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** What applying a plan changes. */
|
|
163
|
+
export interface FoldersMoveApplication {
|
|
164
|
+
/** The document to persist. */
|
|
165
|
+
readonly document: FoldersDocument;
|
|
166
|
+
/** Projects and templates whose label the move changes. */
|
|
167
|
+
readonly labelRenames: readonly FoldersLabelRename[];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The document a plan produces, plus the project and template label renames it implies.
|
|
172
|
+
*
|
|
173
|
+
* Applying a plan is arithmetic on an already validated plan; whether that plan may be committed
|
|
174
|
+
* is the write path's decision.
|
|
175
|
+
*/
|
|
176
|
+
export function applyFoldersMove(
|
|
177
|
+
view: FoldersView,
|
|
178
|
+
movePlan: FoldersMovePlan,
|
|
179
|
+
): FoldersMoveApplication {
|
|
180
|
+
const folderParents = new Map<FolderId, FolderId | undefined>();
|
|
181
|
+
const folderNames = new Map<FolderId, string>();
|
|
182
|
+
const projectFolders = new Map<ProjectId, FolderId | undefined>();
|
|
183
|
+
const templateFolders = new Map<TemplateId, FolderId | undefined>();
|
|
184
|
+
const labelRenames: FoldersLabelRename[] = [];
|
|
185
|
+
|
|
186
|
+
for (const entry of movePlan.entries) {
|
|
187
|
+
switch (entry.item.kind) {
|
|
188
|
+
case "folder":
|
|
189
|
+
folderParents.set(entry.item.id, movePlan.destination);
|
|
190
|
+
folderNames.set(entry.item.id, entry.name);
|
|
191
|
+
break;
|
|
192
|
+
case "project":
|
|
193
|
+
projectFolders.set(entry.item.id, movePlan.destination);
|
|
194
|
+
if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });
|
|
195
|
+
break;
|
|
196
|
+
case "template":
|
|
197
|
+
templateFolders.set(entry.item.id, movePlan.destination);
|
|
198
|
+
if (entry.renamed) labelRenames.push({ item: entry.item, name: entry.name });
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const folders = view.folders.map((folder) => ({
|
|
204
|
+
...folder,
|
|
205
|
+
parent: folderParents.has(folder.id) ? folderParents.get(folder.id) : folder.parent,
|
|
206
|
+
name: folderNames.get(folder.id) ?? folder.name,
|
|
207
|
+
}));
|
|
208
|
+
|
|
209
|
+
const projects = view.projects.map((project) => ({
|
|
210
|
+
...project,
|
|
211
|
+
folder: projectFolders.has(project.id) ? projectFolders.get(project.id) : project.folder,
|
|
212
|
+
}));
|
|
213
|
+
|
|
214
|
+
const templates = view.templates.map((template) => ({
|
|
215
|
+
...template,
|
|
216
|
+
folder: templateFolders.has(template.id) ? templateFolders.get(template.id) : template.folder,
|
|
217
|
+
}));
|
|
218
|
+
|
|
219
|
+
const document = foldersDocumentFromView({ ...view, folders, projects, templates });
|
|
220
|
+
return { document, labelRenames };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
//
|
|
224
|
+
// Internals
|
|
225
|
+
//
|
|
226
|
+
|
|
227
|
+
/** An item of the selection, with what the view says about it. */
|
|
228
|
+
interface Located {
|
|
229
|
+
readonly item: FoldersItem;
|
|
230
|
+
readonly name: string;
|
|
231
|
+
/** Folder holding the item; absent when it is at the top level. */
|
|
232
|
+
readonly sourceFolder?: FolderId;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The name and containing folder of an item the view holds, whichever kind it is. */
|
|
236
|
+
function locate(
|
|
237
|
+
view: FoldersView,
|
|
238
|
+
item: FoldersItem,
|
|
239
|
+
): { readonly name: string; readonly sourceFolder?: FolderId } | undefined {
|
|
240
|
+
switch (item.kind) {
|
|
241
|
+
case "folder": {
|
|
242
|
+
const folder = view.folders.find((candidate) => candidate.id === item.id);
|
|
243
|
+
return folder === undefined ? undefined : { name: folder.name, sourceFolder: folder.parent };
|
|
244
|
+
}
|
|
245
|
+
case "project": {
|
|
246
|
+
const project = view.projects.find((candidate) => candidate.id === item.id);
|
|
247
|
+
return project === undefined
|
|
248
|
+
? undefined
|
|
249
|
+
: { name: project.name, sourceFolder: project.folder };
|
|
250
|
+
}
|
|
251
|
+
case "template": {
|
|
252
|
+
const template = view.templates.find((candidate) => candidate.id === item.id);
|
|
253
|
+
return template === undefined
|
|
254
|
+
? undefined
|
|
255
|
+
: { name: template.name, sourceFolder: template.folder };
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Folders, then projects, then templates; within a kind by name, then by id. The order of the
|
|
262
|
+
* items a caller hands in must not change the plan, or a preview and its write could differ over
|
|
263
|
+
* nothing.
|
|
264
|
+
*/
|
|
265
|
+
function byCanonicalOrder(a: Located, b: Located): number {
|
|
266
|
+
if (a.item.kind !== b.item.kind) return KIND_ORDER[a.item.kind] - KIND_ORDER[b.item.kind];
|
|
267
|
+
const byName = a.name.toLowerCase().localeCompare(b.name.toLowerCase());
|
|
268
|
+
if (byName !== 0) return byName;
|
|
269
|
+
return String(a.item.id).localeCompare(String(b.item.id));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const KIND_ORDER: Readonly<Record<FoldersItem["kind"], number>> = {
|
|
273
|
+
folder: 0,
|
|
274
|
+
project: 1,
|
|
275
|
+
template: 2,
|
|
276
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { commitFoldersRemoval } from "./commit";
|
|
3
|
+
import { applyFoldersRemoval, foldersRemovalsEqual, planFoldersRemoval } from "./removal";
|
|
4
|
+
import { fid, pid, tid, view } from "./test_utils";
|
|
5
|
+
import type { FoldersView } from "./view";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Parent / Doomed / Deeper, with a project and a template at every level and one of each at the
|
|
9
|
+
* top, so every test can say both what a deletion takes and what it must leave standing.
|
|
10
|
+
*/
|
|
11
|
+
function tree(): FoldersView {
|
|
12
|
+
return view(
|
|
13
|
+
[
|
|
14
|
+
{ id: "p", name: "Parent" },
|
|
15
|
+
{ id: "d", name: "Doomed", parent: "p" },
|
|
16
|
+
{ id: "deep", name: "Deeper", parent: "d" },
|
|
17
|
+
],
|
|
18
|
+
[
|
|
19
|
+
{ id: "inDoomed", name: "In doomed", folder: "d" },
|
|
20
|
+
{ id: "inDeep", name: "In deep", folder: "deep" },
|
|
21
|
+
{ id: "inParent", name: "In parent", folder: "p" },
|
|
22
|
+
{ id: "atTop", name: "At top" },
|
|
23
|
+
],
|
|
24
|
+
[
|
|
25
|
+
{ id: "tplInDoomed", name: "Template in doomed", folder: "d" },
|
|
26
|
+
{ id: "tplInParent", name: "Template in parent", folder: "p" },
|
|
27
|
+
{ id: "tplAtTop", name: "Template at top" },
|
|
28
|
+
],
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function removalOf(state: FoldersView, folder: string) {
|
|
33
|
+
const planned = planFoldersRemoval(state, fid(folder));
|
|
34
|
+
if (!planned.ok) throw new Error("expected a removal");
|
|
35
|
+
return planned.removal;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("planning a folder removal", () => {
|
|
39
|
+
test("takes the folder, every folder beneath it, and every project and template inside", () => {
|
|
40
|
+
const removal = removalOf(tree(), "d");
|
|
41
|
+
|
|
42
|
+
expect([...removal.folders].sort()).toEqual([fid("d"), fid("deep")]);
|
|
43
|
+
expect([...removal.projects].sort()).toEqual([pid("inDeep"), pid("inDoomed")]);
|
|
44
|
+
expect([...removal.templates].sort()).toEqual([tid("tplInDoomed")]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("leaves everything outside the subtree alone", () => {
|
|
48
|
+
const document = applyFoldersRemoval(tree(), removalOf(tree(), "d"));
|
|
49
|
+
|
|
50
|
+
expect(document.folders.map((folder) => folder.id)).toEqual([fid("p")]);
|
|
51
|
+
expect(document.assignments).toEqual({ inParent: fid("p") });
|
|
52
|
+
expect(document.templateAssignments).toEqual({ tplInParent: fid("p") });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("a folder that is not there is refused", () => {
|
|
56
|
+
const planned = planFoldersRemoval(tree(), fid("ghost"));
|
|
57
|
+
|
|
58
|
+
expect(planned.ok).toBe(false);
|
|
59
|
+
if (planned.ok) return;
|
|
60
|
+
expect(planned.issues).toEqual([{ kind: "unknown-folder", folder: fid("ghost") }]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("an empty folder removes itself and nothing else", () => {
|
|
64
|
+
const empty = view([{ id: "e", name: "Empty" }], [{ id: "atTop", name: "At top" }]);
|
|
65
|
+
const removal = removalOf(empty, "e");
|
|
66
|
+
|
|
67
|
+
expect(removal.folders).toEqual([fid("e")]);
|
|
68
|
+
expect(removal.projects).toEqual([]);
|
|
69
|
+
expect(removal.templates).toEqual([]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("the order is canonical, so two reads of one tree compare equal", () => {
|
|
73
|
+
expect(foldersRemovalsEqual(removalOf(tree(), "d"), removalOf(tree(), "d"))).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("committing a folder removal", () => {
|
|
78
|
+
test("nothing is written without a confirmed removal", () => {
|
|
79
|
+
const view = tree();
|
|
80
|
+
const edit = commitFoldersRemoval(view, planFoldersRemoval(view, fid("d")), undefined);
|
|
81
|
+
|
|
82
|
+
expect(edit.document).toBeUndefined();
|
|
83
|
+
expect(edit.deletedProjects).toBeUndefined();
|
|
84
|
+
expect(edit.result.ok).toBe(false);
|
|
85
|
+
if (edit.result.ok) return;
|
|
86
|
+
expect(edit.result.reason).toBe("needs-confirmation");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a project that appeared since the dialog opened aborts the deletion", () => {
|
|
90
|
+
const confirmed = removalOf(tree(), "d");
|
|
91
|
+
|
|
92
|
+
// The same folder, with one more project dropped into it in the meantime.
|
|
93
|
+
const after = view(
|
|
94
|
+
[
|
|
95
|
+
{ id: "p", name: "Parent" },
|
|
96
|
+
{ id: "d", name: "Doomed", parent: "p" },
|
|
97
|
+
{ id: "deep", name: "Deeper", parent: "d" },
|
|
98
|
+
],
|
|
99
|
+
[
|
|
100
|
+
{ id: "inDoomed", name: "In doomed", folder: "d" },
|
|
101
|
+
{ id: "inDeep", name: "In deep", folder: "deep" },
|
|
102
|
+
{ id: "latecomer", name: "Latecomer", folder: "d" },
|
|
103
|
+
],
|
|
104
|
+
[{ id: "tplInDoomed", name: "Template in doomed", folder: "d" }],
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const edit = commitFoldersRemoval(after, planFoldersRemoval(after, fid("d")), confirmed);
|
|
108
|
+
|
|
109
|
+
expect(edit.document).toBeUndefined();
|
|
110
|
+
expect(edit.result.ok).toBe(false);
|
|
111
|
+
if (edit.result.ok || edit.result.reason === "cannot-plan")
|
|
112
|
+
throw new Error("expected the removal to be refused as changed");
|
|
113
|
+
expect(edit.result.reason).toBe("plan-changed");
|
|
114
|
+
// The fresh removal is handed back, and it names the project the dialog never showed.
|
|
115
|
+
expect(edit.result.removal.projects).toContain(pid("latecomer"));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("a confirmed removal writes the document and names the projects to destroy", () => {
|
|
119
|
+
const view = tree();
|
|
120
|
+
const confirmed = removalOf(view, "d");
|
|
121
|
+
const edit = commitFoldersRemoval(view, planFoldersRemoval(view, fid("d")), confirmed);
|
|
122
|
+
|
|
123
|
+
expect(edit.result.ok).toBe(true);
|
|
124
|
+
expect(edit.document?.folders.map((folder) => folder.id)).toEqual([fid("p")]);
|
|
125
|
+
expect([...(edit.deletedProjects ?? [])].sort()).toEqual([pid("inDeep"), pid("inDoomed")]);
|
|
126
|
+
expect([...(edit.deletedTemplates ?? [])].sort()).toEqual([tid("tplInDoomed")]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("a folder that vanished before the write is refused, not guessed at", () => {
|
|
130
|
+
const view = tree();
|
|
131
|
+
const edit = commitFoldersRemoval(view, planFoldersRemoval(view, fid("ghost")), {
|
|
132
|
+
folders: [],
|
|
133
|
+
projects: [],
|
|
134
|
+
templates: [],
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
expect(edit.document).toBeUndefined();
|
|
138
|
+
expect(edit.result.ok).toBe(false);
|
|
139
|
+
if (edit.result.ok) return;
|
|
140
|
+
expect(edit.result.reason).toBe("cannot-plan");
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
|
|
2
|
+
import type { FoldersDocument, FolderId } from "./document";
|
|
3
|
+
import type { FoldersView } from "./view";
|
|
4
|
+
import { foldersDocumentFromView, foldersSubtree } from "./view";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Everything a folder deletion destroys: the folder, every folder beneath it, and every project
|
|
8
|
+
* and template held anywhere inside.
|
|
9
|
+
*
|
|
10
|
+
* Deleting a project or a template cannot be undone, so this is computed for the dialog and
|
|
11
|
+
* recomputed inside the write, which commits only if the two agree. Without that, a project
|
|
12
|
+
* dropped into the folder between the two reads would be destroyed without ever appearing in the
|
|
13
|
+
* dialog that asked. Every list is in a canonical order for exactly that comparison.
|
|
14
|
+
*/
|
|
15
|
+
export interface FoldersRemoval {
|
|
16
|
+
readonly folders: readonly FolderId[];
|
|
17
|
+
readonly projects: readonly ProjectId[];
|
|
18
|
+
readonly templates: readonly TemplateId[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** One reason a folder cannot be deleted. */
|
|
22
|
+
export type FoldersRemovalIssue = {
|
|
23
|
+
readonly kind: "unknown-folder";
|
|
24
|
+
readonly folder: FolderId;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** A removal, or everything standing in the way of one. */
|
|
28
|
+
export type FoldersRemovalPlanResult =
|
|
29
|
+
| { readonly ok: true; readonly removal: FoldersRemoval }
|
|
30
|
+
| { readonly ok: false; readonly issues: readonly FoldersRemovalIssue[] };
|
|
31
|
+
|
|
32
|
+
/** What deleting this folder would destroy. */
|
|
33
|
+
export function planFoldersRemoval(view: FoldersView, folder: FolderId): FoldersRemovalPlanResult {
|
|
34
|
+
if (!view.folders.some((candidate) => candidate.id === folder))
|
|
35
|
+
return { ok: false, issues: [{ kind: "unknown-folder", folder }] };
|
|
36
|
+
|
|
37
|
+
const folders = [...foldersSubtree(view, folder)].sort();
|
|
38
|
+
const doomed = new Set<FolderId>(folders);
|
|
39
|
+
const projects = view.projects
|
|
40
|
+
.filter((project) => project.folder !== undefined && doomed.has(project.folder))
|
|
41
|
+
.map((project) => project.id)
|
|
42
|
+
.sort();
|
|
43
|
+
const templates = view.templates
|
|
44
|
+
.filter((template) => template.folder !== undefined && doomed.has(template.folder))
|
|
45
|
+
.map((template) => template.id)
|
|
46
|
+
.sort();
|
|
47
|
+
|
|
48
|
+
return { ok: true, removal: { folders, projects, templates } };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** True when two removals destroy exactly the same things. */
|
|
52
|
+
export function foldersRemovalsEqual(a: FoldersRemoval, b: FoldersRemoval): boolean {
|
|
53
|
+
return (
|
|
54
|
+
a.folders.length === b.folders.length &&
|
|
55
|
+
a.projects.length === b.projects.length &&
|
|
56
|
+
a.templates.length === b.templates.length &&
|
|
57
|
+
a.folders.every((id, index) => id === b.folders[index]) &&
|
|
58
|
+
a.projects.every((id, index) => id === b.projects[index]) &&
|
|
59
|
+
a.templates.every((id, index) => id === b.templates[index])
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The document left once a removal is applied. The projects and templates themselves are deleted
|
|
65
|
+
* by the write path.
|
|
66
|
+
*/
|
|
67
|
+
export function applyFoldersRemoval(view: FoldersView, removal: FoldersRemoval): FoldersDocument {
|
|
68
|
+
const doomedFolders = new Set<FolderId>(removal.folders);
|
|
69
|
+
const doomedProjects = new Set<ProjectId>(removal.projects);
|
|
70
|
+
const doomedTemplates = new Set<TemplateId>(removal.templates);
|
|
71
|
+
return foldersDocumentFromView({
|
|
72
|
+
...view,
|
|
73
|
+
folders: view.folders.filter((folder) => !doomedFolders.has(folder.id)),
|
|
74
|
+
projects: view.projects.filter((project) => !doomedProjects.has(project.id)),
|
|
75
|
+
templates: view.templates.filter((template) => !doomedTemplates.has(template.id)),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
|
|
2
|
+
import type { FoldersDocument, FolderId } from "./document";
|
|
3
|
+
import { FOLDERS_SCHEMA_VERSION } from "./document";
|
|
4
|
+
import type { FoldersItem } from "./planner";
|
|
5
|
+
import type { FoldersView } from "./view";
|
|
6
|
+
import { foldersViewFromDocument } from "./view";
|
|
7
|
+
|
|
8
|
+
/** A folder as a test writes it: plain string ids. */
|
|
9
|
+
export interface TestFolder {
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly parent?: string;
|
|
13
|
+
readonly description?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A project or a template as a test writes it, with the folder holding it. */
|
|
17
|
+
export interface TestItem {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly folder?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const fid = (id: string) => id as FolderId;
|
|
24
|
+
export const pid = (id: string) => id as ProjectId;
|
|
25
|
+
export const tid = (id: string) => id as TemplateId;
|
|
26
|
+
|
|
27
|
+
export const folder = (id: string): Extract<FoldersItem, { kind: "folder" }> => ({
|
|
28
|
+
kind: "folder",
|
|
29
|
+
id: fid(id),
|
|
30
|
+
});
|
|
31
|
+
export const project = (id: string): Extract<FoldersItem, { kind: "project" }> => ({
|
|
32
|
+
kind: "project",
|
|
33
|
+
id: pid(id),
|
|
34
|
+
});
|
|
35
|
+
export const template = (id: string): Extract<FoldersItem, { kind: "template" }> => ({
|
|
36
|
+
kind: "template",
|
|
37
|
+
id: tid(id),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** A current-schema document holding exactly what it is given. */
|
|
41
|
+
export function document(
|
|
42
|
+
folders: readonly TestFolder[],
|
|
43
|
+
assignments: Readonly<Record<string, string>> = {},
|
|
44
|
+
templateAssignments: Readonly<Record<string, string>> = {},
|
|
45
|
+
): FoldersDocument {
|
|
46
|
+
return {
|
|
47
|
+
schemaVersion: FOLDERS_SCHEMA_VERSION,
|
|
48
|
+
folders: folders.map((entry) => ({
|
|
49
|
+
id: fid(entry.id),
|
|
50
|
+
name: entry.name,
|
|
51
|
+
...(entry.parent === undefined ? {} : { parent: fid(entry.parent) }),
|
|
52
|
+
...(entry.description === undefined ? {} : { description: entry.description }),
|
|
53
|
+
})),
|
|
54
|
+
assignments: branded(assignments),
|
|
55
|
+
templateAssignments: branded(templateAssignments),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The healed view of a tree in which every project and template sits where it says. */
|
|
60
|
+
export function view(
|
|
61
|
+
folders: readonly TestFolder[],
|
|
62
|
+
projects: readonly TestItem[],
|
|
63
|
+
templates: readonly TestItem[] = [],
|
|
64
|
+
): FoldersView {
|
|
65
|
+
return foldersViewFromDocument(
|
|
66
|
+
document(folders, placements(projects), placements(templates)),
|
|
67
|
+
projects.map((entry) => ({ id: pid(entry.id), name: entry.name })),
|
|
68
|
+
templates.map((entry) => ({ id: tid(entry.id), name: entry.name })),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//
|
|
73
|
+
// Internals
|
|
74
|
+
//
|
|
75
|
+
|
|
76
|
+
function placements(items: readonly TestItem[]): Record<string, string> {
|
|
77
|
+
return Object.fromEntries(
|
|
78
|
+
items.flatMap((entry) => (entry.folder === undefined ? [] : [[entry.id, entry.folder]])),
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function branded(assignments: Readonly<Record<string, string>>): Record<string, FolderId> {
|
|
83
|
+
return Object.fromEntries(
|
|
84
|
+
Object.entries(assignments).map(([item, folderId]) => [item, fid(folderId)]),
|
|
85
|
+
);
|
|
86
|
+
}
|