@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,62 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { foldersNameBlank, foldersNameTaken, foldersUniqueName } from "./naming";
|
|
3
|
+
|
|
4
|
+
describe("foldersNameTaken", () => {
|
|
5
|
+
test("a free name is not taken", () => {
|
|
6
|
+
expect(foldersNameTaken("Samples", ["Runs", "Analysis"])).toBe(false);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test("case does not matter", () => {
|
|
10
|
+
expect(foldersNameTaken("Samples", ["samples"])).toBe(true);
|
|
11
|
+
expect(foldersNameTaken("SAMPLES", ["Samples"])).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("surrounding whitespace does not matter", () => {
|
|
15
|
+
expect(foldersNameTaken(" Samples ", ["Samples"])).toBe(true);
|
|
16
|
+
expect(foldersNameTaken("Samples", ["samples "])).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("foldersNameBlank", () => {
|
|
21
|
+
test("an empty name and one of whitespace only are blank", () => {
|
|
22
|
+
expect(foldersNameBlank("")).toBe(true);
|
|
23
|
+
expect(foldersNameBlank(" \t ")).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("a name with anything in it is not blank", () => {
|
|
27
|
+
expect(foldersNameBlank(" a ")).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("foldersUniqueName", () => {
|
|
32
|
+
test("a free name is returned untouched", () => {
|
|
33
|
+
expect(foldersUniqueName("Samples", ["Runs"])).toBe("Samples");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("a taken name gains the copy suffix", () => {
|
|
37
|
+
expect(foldersUniqueName("Samples", ["Samples"])).toBe("Samples (Copy)");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("the suffix counts up while names are taken", () => {
|
|
41
|
+
expect(foldersUniqueName("Samples", ["Samples", "Samples (Copy)"])).toBe("Samples (Copy 2)");
|
|
42
|
+
expect(foldersUniqueName("Samples", ["Samples", "Samples (Copy)", "Samples (Copy 2)"])).toBe(
|
|
43
|
+
"Samples (Copy 3)",
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("an existing copy suffix is replaced rather than stacked", () => {
|
|
48
|
+
expect(foldersUniqueName("Samples (Copy)", ["Samples (Copy)"])).toBe("Samples (Copy 2)");
|
|
49
|
+
expect(foldersUniqueName("Samples (Copy 2)", ["Samples (Copy 2)", "Samples (Copy)"])).toBe(
|
|
50
|
+
"Samples (Copy 3)",
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("collision is decided without regard to case", () => {
|
|
55
|
+
expect(foldersUniqueName("Samples", ["samples"])).toBe("Samples (Copy)");
|
|
56
|
+
expect(foldersUniqueName("Samples", ["samples", "SAMPLES (COPY)"])).toBe("Samples (Copy 2)");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("a name that merely contains another is not a collision", () => {
|
|
60
|
+
expect(foldersUniqueName("Samples", ["Samples 2024"])).toBe("Samples");
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one naming rule for folders, projects and templates.
|
|
3
|
+
*
|
|
4
|
+
* Scope is always the children of one parent — a folder, or the top level, which is simply the
|
|
5
|
+
* folder with no parent. Folders, projects and templates share that one namespace, so no two of
|
|
6
|
+
* them inside the same parent can carry the same name. Comparison ignores case and surrounding
|
|
7
|
+
* whitespace: `Samples`, `samples` and ` Samples ` are the same thing to the person looking at
|
|
8
|
+
* them.
|
|
9
|
+
*
|
|
10
|
+
* The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
|
|
11
|
+
* human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
|
|
12
|
+
* an error instead: a text field that quietly disagrees with what was typed is worse than one
|
|
13
|
+
* that says no.
|
|
14
|
+
*
|
|
15
|
+
* Names already stored are never rewritten. Accounts predating folders may hold collisions,
|
|
16
|
+
* because nothing enforced uniqueness before.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** True when a sibling already carries this name, compared as the rule above compares names. */
|
|
20
|
+
export function foldersNameTaken(name: string, siblingNames: Iterable<string>): boolean {
|
|
21
|
+
const wanted = normalizeName(name);
|
|
22
|
+
for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** True when a name has nothing in it but whitespace; a folder cannot be called that. */
|
|
27
|
+
export function foldersNameBlank(name: string): boolean {
|
|
28
|
+
return name.trim() === "";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The name to use for an item landing beside `siblingNames`.
|
|
33
|
+
*
|
|
34
|
+
* A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and
|
|
35
|
+
* an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.
|
|
36
|
+
*/
|
|
37
|
+
export function foldersUniqueName(desiredName: string, siblingNames: Iterable<string>): string {
|
|
38
|
+
const taken = new Set<string>();
|
|
39
|
+
for (const sibling of siblingNames) taken.add(normalizeName(sibling));
|
|
40
|
+
|
|
41
|
+
if (!taken.has(normalizeName(desiredName))) return desiredName;
|
|
42
|
+
|
|
43
|
+
const base = desiredName.replace(COPY_SUFFIX, "");
|
|
44
|
+
let candidate = `${base} (Copy)`;
|
|
45
|
+
for (let index = 2; taken.has(normalizeName(candidate)); index++)
|
|
46
|
+
candidate = `${base} (Copy ${index})`;
|
|
47
|
+
return candidate;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//
|
|
51
|
+
// Internals
|
|
52
|
+
//
|
|
53
|
+
|
|
54
|
+
const COPY_SUFFIX = / \(Copy(?: \d+)?\)$/;
|
|
55
|
+
|
|
56
|
+
function normalizeName(name: string): string {
|
|
57
|
+
return name.trim().toLowerCase();
|
|
58
|
+
}
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import type { FoldersMovePlanResult } from "./planner";
|
|
3
|
+
import { applyFoldersMove, planFoldersMove, foldersMovePlansEqual } from "./planner";
|
|
4
|
+
import { fid, folder, pid, project, template, view } from "./test_utils";
|
|
5
|
+
import { foldersViewFromDocument } from "./view";
|
|
6
|
+
|
|
7
|
+
function planned(result: FoldersMovePlanResult) {
|
|
8
|
+
if (!result.ok) throw new Error(`expected a plan, got ${JSON.stringify(result.issues)}`);
|
|
9
|
+
return result.plan;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function issues(result: FoldersMovePlanResult) {
|
|
13
|
+
if (result.ok) throw new Error("expected the plan to be refused");
|
|
14
|
+
return result.issues;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("planFoldersMove", () => {
|
|
18
|
+
test("a move into free names renames nothing", () => {
|
|
19
|
+
const tree = view(
|
|
20
|
+
[
|
|
21
|
+
{ id: "a", name: "A" },
|
|
22
|
+
{ id: "b", name: "B" },
|
|
23
|
+
],
|
|
24
|
+
[{ id: "p1", name: "Run", folder: "a" }],
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const plan = planned(planFoldersMove(tree, [project("p1")], fid("b")));
|
|
28
|
+
expect(plan.destination).toBe("b");
|
|
29
|
+
expect(plan.entries).toEqual([
|
|
30
|
+
{
|
|
31
|
+
item: { kind: "project", id: "p1" },
|
|
32
|
+
currentName: "Run",
|
|
33
|
+
name: "Run",
|
|
34
|
+
renamed: false,
|
|
35
|
+
sourceFolder: "a",
|
|
36
|
+
},
|
|
37
|
+
]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("a name the destination already holds is suffixed", () => {
|
|
41
|
+
const tree = view(
|
|
42
|
+
[
|
|
43
|
+
{ id: "a", name: "A" },
|
|
44
|
+
{ id: "b", name: "B" },
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
{ id: "p1", name: "Run", folder: "a" },
|
|
48
|
+
{ id: "p2", name: "Run", folder: "b" },
|
|
49
|
+
],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const plan = planned(planFoldersMove(tree, [project("p1")], fid("b")));
|
|
53
|
+
expect(plan.entries[0]).toMatchObject({ name: "Run (Copy)", renamed: true });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("folders and projects share one namespace inside a parent", () => {
|
|
57
|
+
const tree = view(
|
|
58
|
+
[
|
|
59
|
+
{ id: "a", name: "A" },
|
|
60
|
+
{ id: "b", name: "Run", parent: "a" },
|
|
61
|
+
],
|
|
62
|
+
[{ id: "p1", name: "Run" }],
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const plan = planned(planFoldersMove(tree, [project("p1")], fid("a")));
|
|
66
|
+
expect(plan.entries[0]).toMatchObject({ name: "Run (Copy)", renamed: true });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("several moved items with the same name are given distinct ones", () => {
|
|
70
|
+
const tree = view(
|
|
71
|
+
[
|
|
72
|
+
{ id: "a", name: "A" },
|
|
73
|
+
{ id: "b", name: "B" },
|
|
74
|
+
{ id: "c", name: "C" },
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
{ id: "p1", name: "Run", folder: "a" },
|
|
78
|
+
{ id: "p2", name: "Run", folder: "b" },
|
|
79
|
+
],
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const plan = planned(planFoldersMove(tree, [project("p1"), project("p2")], fid("c")));
|
|
83
|
+
expect(plan.entries.map((entry) => entry.name)).toEqual(["Run", "Run (Copy)"]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("an item already in the destination keeps its own name", () => {
|
|
87
|
+
const tree = view([{ id: "a", name: "A" }], [{ id: "p1", name: "Run", folder: "a" }]);
|
|
88
|
+
const plan = planned(planFoldersMove(tree, [project("p1")], fid("a")));
|
|
89
|
+
expect(plan.entries[0]).toMatchObject({ name: "Run", renamed: false });
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("the order the caller lists items in does not change the plan", () => {
|
|
93
|
+
const tree = view(
|
|
94
|
+
[
|
|
95
|
+
{ id: "a", name: "A" },
|
|
96
|
+
{ id: "dest", name: "Dest" },
|
|
97
|
+
],
|
|
98
|
+
[
|
|
99
|
+
{ id: "p1", name: "Run", folder: "a" },
|
|
100
|
+
{ id: "p2", name: "Run", folder: "a" },
|
|
101
|
+
],
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const first = planned(
|
|
105
|
+
planFoldersMove(tree, [folder("a"), project("p1"), project("p2")], fid("dest")),
|
|
106
|
+
);
|
|
107
|
+
const second = planned(
|
|
108
|
+
planFoldersMove(tree, [project("p2"), project("p1"), folder("a")], fid("dest")),
|
|
109
|
+
);
|
|
110
|
+
expect(foldersMovePlansEqual(first, second)).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("a move to the top level is planned against the top level's names", () => {
|
|
114
|
+
const tree = view(
|
|
115
|
+
[{ id: "a", name: "A" }],
|
|
116
|
+
[
|
|
117
|
+
{ id: "p1", name: "Run" },
|
|
118
|
+
{ id: "p2", name: "Run", folder: "a" },
|
|
119
|
+
],
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const plan = planned(planFoldersMove(tree, [project("p2")]));
|
|
123
|
+
expect(plan.destination).toBeUndefined();
|
|
124
|
+
expect(plan.entries[0]).toMatchObject({ name: "Run (Copy)", renamed: true });
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("a move of nothing plans nothing", () => {
|
|
128
|
+
const tree = view([{ id: "f", name: "F" }], [{ id: "p", name: "P" }]);
|
|
129
|
+
expect(planned(planFoldersMove(tree, [], fid("f"))).entries).toEqual([]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("the same item named twice in one selection is planned once", () => {
|
|
133
|
+
const tree = view([{ id: "d", name: "D" }], [{ id: "p", name: "P" }]);
|
|
134
|
+
expect(
|
|
135
|
+
planned(planFoldersMove(tree, [project("p"), project("p")], fid("d"))).entries,
|
|
136
|
+
).toHaveLength(1);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("an unknown destination is refused", () => {
|
|
140
|
+
const tree = view([], [{ id: "p1", name: "Run" }]);
|
|
141
|
+
expect(issues(planFoldersMove(tree, [project("p1")], fid("nope")))).toEqual([
|
|
142
|
+
{ kind: "unknown-destination", folder: "nope" },
|
|
143
|
+
]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("an unknown item is refused", () => {
|
|
147
|
+
const tree = view([{ id: "a", name: "A" }], []);
|
|
148
|
+
expect(issues(planFoldersMove(tree, [project("ghost")], fid("a")))).toEqual([
|
|
149
|
+
{ kind: "unknown-item", item: { kind: "project", id: "ghost" } },
|
|
150
|
+
]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("a folder cannot be moved into itself or into its own descendant", () => {
|
|
154
|
+
const tree = view(
|
|
155
|
+
[
|
|
156
|
+
{ id: "a", name: "A" },
|
|
157
|
+
{ id: "b", name: "B", parent: "a" },
|
|
158
|
+
],
|
|
159
|
+
[],
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
expect(issues(planFoldersMove(tree, [folder("a")], fid("a")))[0]?.kind).toBe(
|
|
163
|
+
"into-own-descendant",
|
|
164
|
+
);
|
|
165
|
+
expect(issues(planFoldersMove(tree, [folder("a")], fid("b")))[0]?.kind).toBe(
|
|
166
|
+
"into-own-descendant",
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("a move that nests deeply is allowed: there is no depth limit", () => {
|
|
171
|
+
const tree = view(
|
|
172
|
+
[
|
|
173
|
+
{ id: "a", name: "A" },
|
|
174
|
+
{ id: "b", name: "B", parent: "a" },
|
|
175
|
+
{ id: "x", name: "X" },
|
|
176
|
+
{ id: "y", name: "Y", parent: "x" },
|
|
177
|
+
],
|
|
178
|
+
[],
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
expect(planFoldersMove(tree, [folder("x")], fid("b")).ok).toBe(true);
|
|
182
|
+
expect(planFoldersMove(tree, [folder("x")], fid("a")).ok).toBe(true);
|
|
183
|
+
// The one nesting rule that remains: a folder may not be moved inside itself.
|
|
184
|
+
expect(issues(planFoldersMove(tree, [folder("x")], fid("y")))).toEqual([
|
|
185
|
+
{ kind: "into-own-descendant", folder: "x", destination: "y" },
|
|
186
|
+
]);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("a project may be moved into a nested folder", () => {
|
|
190
|
+
const tree = view(
|
|
191
|
+
[
|
|
192
|
+
{ id: "a", name: "A" },
|
|
193
|
+
{ id: "b", name: "B", parent: "a" },
|
|
194
|
+
{ id: "c", name: "C", parent: "b" },
|
|
195
|
+
],
|
|
196
|
+
[{ id: "p1", name: "Run" }],
|
|
197
|
+
);
|
|
198
|
+
expect(planFoldersMove(tree, [project("p1")], fid("c")).ok).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe("a selection holding a folder and something inside it", () => {
|
|
203
|
+
const tree = view(
|
|
204
|
+
[
|
|
205
|
+
{ id: "a", name: "A" },
|
|
206
|
+
{ id: "b", name: "B", parent: "a" },
|
|
207
|
+
{ id: "c", name: "C", parent: "b" },
|
|
208
|
+
{ id: "dest", name: "Dest" },
|
|
209
|
+
],
|
|
210
|
+
[
|
|
211
|
+
{ id: "p1", name: "Run", folder: "a" },
|
|
212
|
+
{ id: "p2", name: "Deep run", folder: "c" },
|
|
213
|
+
{ id: "p3", name: "Elsewhere" },
|
|
214
|
+
],
|
|
215
|
+
[{ id: "t1", name: "Snapshot", folder: "b" }],
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
test("moves the folder alone, carrying its contents along", () => {
|
|
219
|
+
const plan = planned(
|
|
220
|
+
planFoldersMove(tree, [folder("a"), folder("b"), project("p1")], fid("dest")),
|
|
221
|
+
);
|
|
222
|
+
expect(plan.entries.map((entry) => entry.item)).toEqual([folder("a")]);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("keeps the moved subtree intact", () => {
|
|
226
|
+
const selection = [folder("a"), folder("b"), folder("c"), project("p1"), project("p2")];
|
|
227
|
+
const plan = planned(planFoldersMove(tree, [...selection, template("t1")], fid("dest")));
|
|
228
|
+
const moved = foldersViewFromDocument(
|
|
229
|
+
applyFoldersMove(tree, plan).document,
|
|
230
|
+
tree.projects,
|
|
231
|
+
tree.templates,
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
expect(moved.folders.find((entry) => entry.id === "a")?.parent).toBe("dest");
|
|
235
|
+
expect(moved.folders.find((entry) => entry.id === "b")?.parent).toBe("a");
|
|
236
|
+
expect(moved.folders.find((entry) => entry.id === "c")?.path).toEqual(["Dest", "A", "B", "C"]);
|
|
237
|
+
expect(moved.projects.find((entry) => entry.id === "p1")?.folder).toBe("a");
|
|
238
|
+
expect(moved.projects.find((entry) => entry.id === "p2")?.folder).toBe("c");
|
|
239
|
+
expect(moved.templates.find((entry) => entry.id === "t1")?.folder).toBe("b");
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("still moves what the folder does not hold", () => {
|
|
243
|
+
const plan = planned(
|
|
244
|
+
planFoldersMove(tree, [project("p1"), folder("b"), project("p3")], fid("dest")),
|
|
245
|
+
);
|
|
246
|
+
expect(plan.entries.map((entry) => entry.item)).toEqual([
|
|
247
|
+
folder("b"),
|
|
248
|
+
project("p3"),
|
|
249
|
+
project("p1"),
|
|
250
|
+
]);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("is refused when the destination lies inside the folder", () => {
|
|
254
|
+
expect(issues(planFoldersMove(tree, [folder("a"), folder("b")], fid("c")))).toEqual([
|
|
255
|
+
{ kind: "into-own-descendant", folder: "a", destination: "c" },
|
|
256
|
+
]);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
describe("moving a template", () => {
|
|
261
|
+
test("a template arriving where a project carries its name is renamed", () => {
|
|
262
|
+
const tree = view(
|
|
263
|
+
[
|
|
264
|
+
{ id: "a", name: "A" },
|
|
265
|
+
{ id: "b", name: "B" },
|
|
266
|
+
],
|
|
267
|
+
[{ id: "p1", name: "Run", folder: "b" }],
|
|
268
|
+
[{ id: "t1", name: "Run", folder: "a" }],
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
const plan = planned(planFoldersMove(tree, [template("t1")], fid("b")));
|
|
272
|
+
expect(plan.entries[0]).toMatchObject({
|
|
273
|
+
name: "Run (Copy)",
|
|
274
|
+
renamed: true,
|
|
275
|
+
sourceFolder: "a",
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test("a template arriving where another template carries its name is renamed", () => {
|
|
280
|
+
const tree = view(
|
|
281
|
+
[
|
|
282
|
+
{ id: "a", name: "A" },
|
|
283
|
+
{ id: "b", name: "B" },
|
|
284
|
+
],
|
|
285
|
+
[],
|
|
286
|
+
[
|
|
287
|
+
{ id: "t1", name: "Snapshot", folder: "a" },
|
|
288
|
+
{ id: "t2", name: "Snapshot", folder: "b" },
|
|
289
|
+
],
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
const plan = planned(planFoldersMove(tree, [template("t1")], fid("b")));
|
|
293
|
+
expect(plan.entries[0]).toMatchObject({ name: "Snapshot (Copy)", renamed: true });
|
|
294
|
+
expect(applyFoldersMove(tree, plan).labelRenames).toEqual([
|
|
295
|
+
{ item: { kind: "template", id: "t1" }, name: "Snapshot (Copy)" },
|
|
296
|
+
]);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test("a project arriving where a template carries its name is renamed", () => {
|
|
300
|
+
const tree = view(
|
|
301
|
+
[
|
|
302
|
+
{ id: "a", name: "A" },
|
|
303
|
+
{ id: "b", name: "B" },
|
|
304
|
+
],
|
|
305
|
+
[{ id: "p1", name: "Run", folder: "a" }],
|
|
306
|
+
[{ id: "t1", name: "Run", folder: "b" }],
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const plan = planned(planFoldersMove(tree, [project("p1")], fid("b")));
|
|
310
|
+
expect(plan.entries[0]).toMatchObject({ name: "Run (Copy)", renamed: true });
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test("applying the plan re-parents the template and touches nothing else", () => {
|
|
314
|
+
const tree = view(
|
|
315
|
+
[
|
|
316
|
+
{ id: "a", name: "A" },
|
|
317
|
+
{ id: "b", name: "B" },
|
|
318
|
+
],
|
|
319
|
+
[{ id: "p1", name: "Run", folder: "a" }],
|
|
320
|
+
[{ id: "t1", name: "Snapshot", folder: "a" }],
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
const plan = planned(planFoldersMove(tree, [template("t1")], fid("b")));
|
|
324
|
+
const applied = applyFoldersMove(tree, plan);
|
|
325
|
+
|
|
326
|
+
expect(applied.document.templateAssignments).toEqual({ t1: "b" });
|
|
327
|
+
expect(applied.document.assignments).toEqual({ p1: "a" });
|
|
328
|
+
expect(applied.labelRenames).toEqual([]);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test("folders come first, then projects, then templates, whatever order is handed in", () => {
|
|
332
|
+
const tree = view(
|
|
333
|
+
[
|
|
334
|
+
{ id: "a", name: "A" },
|
|
335
|
+
{ id: "dest", name: "Dest" },
|
|
336
|
+
],
|
|
337
|
+
[{ id: "p1", name: "Run" }],
|
|
338
|
+
[{ id: "t1", name: "Snapshot" }],
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
const plan = planned(
|
|
342
|
+
planFoldersMove(tree, [template("t1"), project("p1"), folder("a")], fid("dest")),
|
|
343
|
+
);
|
|
344
|
+
expect(plan.entries.map((entry) => entry.item.kind)).toEqual(["folder", "project", "template"]);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
test("a template that is not there is refused", () => {
|
|
348
|
+
const tree = view([{ id: "a", name: "A" }], []);
|
|
349
|
+
expect(issues(planFoldersMove(tree, [template("ghost")], fid("a")))).toEqual([
|
|
350
|
+
{ kind: "unknown-item", item: { kind: "template", id: "ghost" } },
|
|
351
|
+
]);
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
describe("foldersMovePlansEqual", () => {
|
|
356
|
+
const tree = view(
|
|
357
|
+
[
|
|
358
|
+
{ id: "a", name: "A" },
|
|
359
|
+
{ id: "b", name: "B" },
|
|
360
|
+
],
|
|
361
|
+
[{ id: "p1", name: "Run", folder: "a" }],
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
test("a plan equals itself recomputed over unchanged state", () => {
|
|
365
|
+
const first = planned(planFoldersMove(tree, [project("p1")], fid("b")));
|
|
366
|
+
const second = planned(planFoldersMove(tree, [project("p1")], fid("b")));
|
|
367
|
+
expect(foldersMovePlansEqual(first, second)).toBe(true);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
test("a plan stops matching once the destination has gained a colliding name", () => {
|
|
371
|
+
const confirmed = planned(planFoldersMove(tree, [project("p1")], fid("b")));
|
|
372
|
+
|
|
373
|
+
const moved = view(
|
|
374
|
+
[
|
|
375
|
+
{ id: "a", name: "A" },
|
|
376
|
+
{ id: "b", name: "B" },
|
|
377
|
+
],
|
|
378
|
+
[
|
|
379
|
+
{ id: "p1", name: "Run", folder: "a" },
|
|
380
|
+
{ id: "p2", name: "Run", folder: "b" },
|
|
381
|
+
],
|
|
382
|
+
);
|
|
383
|
+
const fresh = planned(planFoldersMove(moved, [project("p1")], fid("b")));
|
|
384
|
+
|
|
385
|
+
expect(foldersMovePlansEqual(confirmed, fresh)).toBe(false);
|
|
386
|
+
expect(fresh.entries[0]).toMatchObject({ name: "Run (Copy)" });
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
test("plans into different destinations are not equal", () => {
|
|
390
|
+
const toB = planned(planFoldersMove(tree, [project("p1")], fid("b")));
|
|
391
|
+
const toRoot = planned(planFoldersMove(tree, [project("p1")]));
|
|
392
|
+
expect(foldersMovePlansEqual(toB, toRoot)).toBe(false);
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
describe("applyFoldersMove", () => {
|
|
397
|
+
test("a move re-parents the items and names the renames it implies", () => {
|
|
398
|
+
const tree = view(
|
|
399
|
+
[
|
|
400
|
+
{ id: "a", name: "A" },
|
|
401
|
+
{ id: "b", name: "B" },
|
|
402
|
+
],
|
|
403
|
+
[
|
|
404
|
+
{ id: "p1", name: "Run", folder: "a" },
|
|
405
|
+
{ id: "p2", name: "Run", folder: "b" },
|
|
406
|
+
],
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
const plan = planned(planFoldersMove(tree, [project("p1")], fid("b")));
|
|
410
|
+
const applied = applyFoldersMove(tree, plan);
|
|
411
|
+
|
|
412
|
+
expect(applied.document.assignments).toEqual({ p1: "b", p2: "b" });
|
|
413
|
+
expect(applied.labelRenames).toEqual([
|
|
414
|
+
{ item: { kind: "project", id: "p1" }, name: "Run (Copy)" },
|
|
415
|
+
]);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
test("a moved folder keeps its own subtree", () => {
|
|
419
|
+
const tree = view(
|
|
420
|
+
[
|
|
421
|
+
{ id: "a", name: "A" },
|
|
422
|
+
{ id: "b", name: "B", parent: "a" },
|
|
423
|
+
{ id: "dest", name: "Dest" },
|
|
424
|
+
],
|
|
425
|
+
[{ id: "p1", name: "Run", folder: "b" }],
|
|
426
|
+
);
|
|
427
|
+
|
|
428
|
+
const plan = planned(planFoldersMove(tree, [folder("a")], fid("dest")));
|
|
429
|
+
const applied = applyFoldersMove(tree, plan);
|
|
430
|
+
|
|
431
|
+
const moved = foldersViewFromDocument(applied.document, [{ id: pid("p1"), name: "Run" }]);
|
|
432
|
+
expect(moved.folders.find((entry) => entry.id === "a")?.parent).toBe("dest");
|
|
433
|
+
expect(moved.folders.find((entry) => entry.id === "b")?.path).toEqual(["Dest", "A", "B"]);
|
|
434
|
+
expect(moved.projects[0]?.folder).toBe("b");
|
|
435
|
+
});
|
|
436
|
+
});
|