@milaboratories/pl-model-middle-layer 1.32.2 → 1.34.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.
Files changed (87) hide show
  1. package/dist/folders/commit.cjs +79 -0
  2. package/dist/folders/commit.cjs.map +1 -0
  3. package/dist/folders/commit.d.ts +82 -0
  4. package/dist/folders/commit.d.ts.map +1 -0
  5. package/dist/folders/commit.js +78 -0
  6. package/dist/folders/commit.js.map +1 -0
  7. package/dist/folders/decode.cjs +79 -0
  8. package/dist/folders/decode.cjs.map +1 -0
  9. package/dist/folders/decode.d.ts +57 -0
  10. package/dist/folders/decode.d.ts.map +1 -0
  11. package/dist/folders/decode.js +77 -0
  12. package/dist/folders/decode.js.map +1 -0
  13. package/dist/folders/document.cjs +49 -0
  14. package/dist/folders/document.cjs.map +1 -0
  15. package/dist/folders/document.d.ts +56 -0
  16. package/dist/folders/document.d.ts.map +1 -0
  17. package/dist/folders/document.js +45 -0
  18. package/dist/folders/document.js.map +1 -0
  19. package/dist/folders/index.cjs +37 -0
  20. package/dist/folders/index.d.ts +10 -0
  21. package/dist/folders/index.js +10 -0
  22. package/dist/folders/inheritance.cjs +23 -0
  23. package/dist/folders/inheritance.cjs.map +1 -0
  24. package/dist/folders/inheritance.d.ts +19 -0
  25. package/dist/folders/inheritance.d.ts.map +1 -0
  26. package/dist/folders/inheritance.js +23 -0
  27. package/dist/folders/inheritance.js.map +1 -0
  28. package/dist/folders/invariants.cjs +115 -0
  29. package/dist/folders/invariants.cjs.map +1 -0
  30. package/dist/folders/invariants.d.ts +50 -0
  31. package/dist/folders/invariants.d.ts.map +1 -0
  32. package/dist/folders/invariants.js +114 -0
  33. package/dist/folders/invariants.js.map +1 -0
  34. package/dist/folders/naming.cjs +54 -0
  35. package/dist/folders/naming.cjs.map +1 -0
  36. package/dist/folders/naming.d.ts +32 -0
  37. package/dist/folders/naming.d.ts.map +1 -0
  38. package/dist/folders/naming.js +52 -0
  39. package/dist/folders/naming.js.map +1 -0
  40. package/dist/folders/planner.cjs +192 -0
  41. package/dist/folders/planner.cjs.map +1 -0
  42. package/dist/folders/planner.d.ts +100 -0
  43. package/dist/folders/planner.d.ts.map +1 -0
  44. package/dist/folders/planner.js +190 -0
  45. package/dist/folders/planner.js.map +1 -0
  46. package/dist/folders/removal.cjs +47 -0
  47. package/dist/folders/removal.cjs.map +1 -0
  48. package/dist/folders/removal.d.ts +42 -0
  49. package/dist/folders/removal.d.ts.map +1 -0
  50. package/dist/folders/removal.js +45 -0
  51. package/dist/folders/removal.js.map +1 -0
  52. package/dist/folders/view.cjs +212 -0
  53. package/dist/folders/view.cjs.map +1 -0
  54. package/dist/folders/view.d.ts +98 -0
  55. package/dist/folders/view.d.ts.map +1 -0
  56. package/dist/folders/view.js +207 -0
  57. package/dist/folders/view.js.map +1 -0
  58. package/dist/index.cjs +43 -2
  59. package/dist/index.d.ts +12 -2
  60. package/dist/index.js +12 -1
  61. package/dist/project.cjs +23 -0
  62. package/dist/project.cjs.map +1 -0
  63. package/dist/project.d.ts +13 -0
  64. package/dist/project.d.ts.map +1 -1
  65. package/dist/project.js +22 -0
  66. package/dist/project.js.map +1 -0
  67. package/package.json +6 -3
  68. package/src/folders/commit.ts +133 -0
  69. package/src/folders/decode.test.ts +128 -0
  70. package/src/folders/decode.ts +142 -0
  71. package/src/folders/document.ts +96 -0
  72. package/src/folders/index.ts +9 -0
  73. package/src/folders/inheritance.test.ts +91 -0
  74. package/src/folders/inheritance.ts +33 -0
  75. package/src/folders/invariants.test.ts +166 -0
  76. package/src/folders/invariants.ts +159 -0
  77. package/src/folders/naming.test.ts +62 -0
  78. package/src/folders/naming.ts +59 -0
  79. package/src/folders/planner.test.ts +450 -0
  80. package/src/folders/planner.ts +279 -0
  81. package/src/folders/removal.test.ts +142 -0
  82. package/src/folders/removal.ts +77 -0
  83. package/src/folders/test_utils.ts +86 -0
  84. package/src/folders/view.test.ts +433 -0
  85. package/src/folders/view.ts +334 -0
  86. package/src/index.ts +1 -0
  87. package/src/project.ts +21 -0
@@ -0,0 +1,166 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import type { FoldersDocument } from "./document";
3
+ import { formatFoldersViolation, validateFoldersDocument } from "./invariants";
4
+ import { document, pid, tid } from "./test_utils";
5
+ import type { FoldersProjectInput } from "./view";
6
+
7
+ const kinds = (value: FoldersDocument, projects?: FoldersProjectInput[]) =>
8
+ validateFoldersDocument(value, projects).map((violation) => violation.kind);
9
+
10
+ describe("validateFoldersDocument", () => {
11
+ test("a project assigned to a nested folder is valid", () => {
12
+ expect(
13
+ validateFoldersDocument(
14
+ document(
15
+ [
16
+ { id: "a", name: "A" },
17
+ { id: "b", name: "B", parent: "a" },
18
+ { id: "c", name: "C", parent: "b" },
19
+ ],
20
+ { p1: "c" },
21
+ ),
22
+ [{ id: pid("p1"), name: "Run" }],
23
+ ),
24
+ ).toEqual([]);
25
+ });
26
+
27
+ test("nesting has no limit: a long chain is a valid document", () => {
28
+ const chain = Array.from({ length: 12 }, (_, index) => ({
29
+ id: `f${index}`,
30
+ name: `F${index}`,
31
+ ...(index === 0 ? {} : { parent: `f${index - 1}` }),
32
+ }));
33
+
34
+ expect(kinds(document(chain))).toEqual([]);
35
+ });
36
+
37
+ test("a cycle is reported for every folder in it", () => {
38
+ expect(
39
+ kinds(
40
+ document([
41
+ { id: "a", name: "A", parent: "b" },
42
+ { id: "b", name: "B", parent: "a" },
43
+ ]),
44
+ ),
45
+ ).toEqual(["cycle", "cycle"]);
46
+ });
47
+
48
+ test("a folder hanging below a cycle is not reported as sitting in it", () => {
49
+ const violations = validateFoldersDocument(
50
+ document([
51
+ { id: "a", name: "A", parent: "b" },
52
+ { id: "b", name: "B", parent: "a" },
53
+ { id: "c", name: "C", parent: "a" },
54
+ ]),
55
+ );
56
+ expect(violations).toEqual([
57
+ { kind: "cycle", folder: "a" },
58
+ { kind: "cycle", folder: "b" },
59
+ ]);
60
+ });
61
+
62
+ test("a folder named with whitespace only is reported", () => {
63
+ expect(kinds(document([{ id: "a", name: " " }]))).toEqual(["empty-folder-name"]);
64
+ });
65
+
66
+ test("a parent that is not there is reported", () => {
67
+ expect(kinds(document([{ id: "a", name: "A", parent: "gone" }]))).toEqual(["missing-parent"]);
68
+ });
69
+
70
+ test("a folder id used twice is reported", () => {
71
+ expect(
72
+ kinds(
73
+ document([
74
+ { id: "a", name: "A" },
75
+ { id: "a", name: "B" },
76
+ ]),
77
+ ),
78
+ ).toEqual(["duplicate-folder-id"]);
79
+ });
80
+
81
+ test("names must be unique within one parent, ignoring case", () => {
82
+ expect(
83
+ kinds(
84
+ document([
85
+ { id: "a", name: "Samples" },
86
+ { id: "b", name: "samples" },
87
+ ]),
88
+ ),
89
+ ).toEqual(["duplicate-name"]);
90
+ });
91
+
92
+ test("the same name under different parents is fine", () => {
93
+ expect(
94
+ kinds(
95
+ document([
96
+ { id: "a", name: "A" },
97
+ { id: "b", name: "B" },
98
+ { id: "a1", name: "Runs", parent: "a" },
99
+ { id: "b1", name: "Runs", parent: "b" },
100
+ ]),
101
+ ),
102
+ ).toEqual([]);
103
+ });
104
+
105
+ test("a folder and a project inside the same parent may share a name", () => {
106
+ expect(kinds(document([{ id: "a", name: "Runs" }], { p1: "gone-nowhere" }), [])).toEqual([]);
107
+
108
+ expect(kinds(document([{ id: "a", name: "Runs" }]), [{ id: pid("p1"), name: "runs" }])).toEqual(
109
+ [],
110
+ );
111
+ });
112
+
113
+ test("two projects inside the same parent may not", () => {
114
+ expect(
115
+ kinds(document([]), [
116
+ { id: pid("p1"), name: "Runs" },
117
+ { id: pid("p2"), name: "runs" },
118
+ ]),
119
+ ).toEqual(["duplicate-name"]);
120
+ });
121
+
122
+ test("a template shares its name with nothing but other templates", () => {
123
+ const withTemplates = (templates: { id: string; name: string }[]) =>
124
+ validateFoldersDocument(
125
+ document([{ id: "a", name: "Runs" }], { p1: "a" }, { t1: "a", t2: "a" }),
126
+ [{ id: pid("p1"), name: "Pilot" }],
127
+ templates.map((template) => ({ id: tid(template.id), name: template.name })),
128
+ );
129
+
130
+ expect(withTemplates([{ id: "t1", name: "Snapshot" }])).toEqual([]);
131
+ expect(withTemplates([{ id: "t1", name: "pilot" }])).toEqual([]);
132
+ expect(
133
+ withTemplates([
134
+ { id: "t1", name: "Snapshot" },
135
+ { id: "t2", name: "Snapshot" },
136
+ ]),
137
+ ).toEqual([{ kind: "duplicate-name", item: "template", parent: "a", name: "Snapshot" }]);
138
+ });
139
+
140
+ test("an assignment naming a folder that is not there is reported", () => {
141
+ expect(kinds(document([], { p1: "gone" }), [{ id: pid("p1"), name: "Run" }])).toEqual([
142
+ "unknown-assignment-folder",
143
+ ]);
144
+ });
145
+
146
+ test("an assignment naming a project that is gone is not the document's problem", () => {
147
+ expect(kinds(document([{ id: "a", name: "A" }], { ghost: "gone" }), [])).toEqual([]);
148
+ });
149
+
150
+ test("every violation formats to a line naming what is wrong", () => {
151
+ const violations = validateFoldersDocument(
152
+ document(
153
+ [
154
+ { id: "a", name: "A", parent: "gone" },
155
+ { id: "b", name: "B" },
156
+ { id: "b", name: "B" },
157
+ ],
158
+ { p1: "missing" },
159
+ ),
160
+ [{ id: pid("p1"), name: "Run" }],
161
+ );
162
+
163
+ expect(violations.length).toBeGreaterThan(0);
164
+ for (const violation of violations) expect(formatFoldersViolation(violation)).not.toBe("");
165
+ });
166
+ });
@@ -0,0 +1,159 @@
1
+ import type { ProjectId, TemplateId } from "@milaboratories/pl-model-common";
2
+ import { asProjectId, asTemplateId } from "@milaboratories/pl-model-common";
3
+ import type { FoldersDocument, FolderId } from "./document";
4
+ import { foldersNameBlank, foldersNameTaken } from "./naming";
5
+ import type { FoldersItem } from "./planner";
6
+ import type { FoldersProjectInput, FoldersTemplateInput } from "./view";
7
+
8
+ /** One way a document breaks the rules the write path enforces. */
9
+ export type FoldersViolation =
10
+ | { readonly kind: "duplicate-folder-id"; readonly folder: FolderId }
11
+ | { readonly kind: "empty-folder-name"; readonly folder: FolderId }
12
+ | {
13
+ readonly kind: "missing-parent";
14
+ readonly folder: FolderId;
15
+ readonly parent: FolderId;
16
+ }
17
+ | { readonly kind: "cycle"; readonly folder: FolderId }
18
+ | {
19
+ readonly kind: "duplicate-name";
20
+ /** Which kind of item the name is used twice among. */
21
+ readonly item: FoldersItem["kind"];
22
+ readonly parent?: FolderId;
23
+ readonly name: string;
24
+ }
25
+ | {
26
+ readonly kind: "unknown-assignment-folder";
27
+ readonly project: ProjectId;
28
+ readonly folder: FolderId;
29
+ }
30
+ | {
31
+ readonly kind: "unknown-template-assignment-folder";
32
+ readonly template: TemplateId;
33
+ readonly folder: FolderId;
34
+ };
35
+
36
+ /**
37
+ * Everything wrong with a document, in one pass.
38
+ *
39
+ * This is the write path's check: a read heals the same problems instead of reporting them, but a
40
+ * write that would persist any of them is refused, because a document no reader can render as
41
+ * stored is a document someone has to repair by hand.
42
+ *
43
+ * Project and template names are optional. Without them only folder names can be checked for
44
+ * duplicates, so a write path that can supply them should. The template list also tells an assignment for a template that is gone — which a read
45
+ * prunes — from one naming a folder that does not exist, which is a broken document.
46
+ */
47
+ export function validateFoldersDocument(
48
+ document: FoldersDocument,
49
+ projects?: readonly FoldersProjectInput[],
50
+ templates?: readonly FoldersTemplateInput[],
51
+ ): readonly FoldersViolation[] {
52
+ const violations: FoldersViolation[] = [];
53
+
54
+ const byId = new Map<FolderId, { name: string; parent?: FolderId }>();
55
+ for (const folder of document.folders) {
56
+ if (byId.has(folder.id)) {
57
+ violations.push({ kind: "duplicate-folder-id", folder: folder.id });
58
+ continue;
59
+ }
60
+ if (foldersNameBlank(folder.name))
61
+ violations.push({ kind: "empty-folder-name", folder: folder.id });
62
+ byId.set(folder.id, { name: folder.name, parent: folder.parent });
63
+ }
64
+
65
+ for (const [id, folder] of byId) {
66
+ if (folder.parent !== undefined && !byId.has(folder.parent)) {
67
+ violations.push({ kind: "missing-parent", folder: id, parent: folder.parent });
68
+ continue;
69
+ }
70
+
71
+ if (onCycle(id, byId)) violations.push({ kind: "cycle", folder: id });
72
+ }
73
+
74
+ // The comparison is the naming rule's, not a second one written here: a document is invalid in
75
+ // exactly the cases the rule would have refused, or the write path and the checker would drift.
76
+ // Each kind of item has a namespace of its own inside a parent.
77
+ const namesByParent = new Map<string, string[]>();
78
+ const claim = (item: FoldersItem["kind"], parent: FolderId | undefined, name: string) => {
79
+ const key = `${item}:${parent ?? ""}`;
80
+ const names = namesByParent.get(key) ?? [];
81
+ namesByParent.set(key, names);
82
+ if (foldersNameTaken(name, names))
83
+ violations.push({ kind: "duplicate-name", item, parent, name });
84
+ names.push(name);
85
+ };
86
+
87
+ for (const folder of byId.values()) claim("folder", folder.parent, folder.name);
88
+
89
+ const knownProjects = new Set<string>((projects ?? []).map((project) => project.id));
90
+ for (const project of projects ?? []) {
91
+ const parent = document.assignments[project.id];
92
+ if (parent === undefined || byId.has(parent)) claim("project", parent, project.name);
93
+ }
94
+
95
+ for (const [project, folder] of Object.entries(document.assignments))
96
+ if (!byId.has(folder) && (projects === undefined || knownProjects.has(project)))
97
+ violations.push({
98
+ kind: "unknown-assignment-folder",
99
+ project: asProjectId(project),
100
+ folder,
101
+ });
102
+
103
+ for (const template of templates ?? []) {
104
+ const parent = document.templateAssignments[template.id];
105
+ if (parent === undefined || byId.has(parent)) claim("template", parent, template.name);
106
+ }
107
+
108
+ const knownTemplates = new Set<string>((templates ?? []).map((template) => template.id));
109
+ for (const [template, folder] of Object.entries(document.templateAssignments))
110
+ if (!byId.has(folder) && (templates === undefined || knownTemplates.has(template)))
111
+ violations.push({
112
+ kind: "unknown-template-assignment-folder",
113
+ template: asTemplateId(template),
114
+ folder,
115
+ });
116
+
117
+ return violations;
118
+ }
119
+
120
+ /** A one-line description of a violation, for an error a user or a log will read. */
121
+ export function formatFoldersViolation(violation: FoldersViolation): string {
122
+ switch (violation.kind) {
123
+ case "duplicate-folder-id":
124
+ return `folder id ${violation.folder} appears more than once`;
125
+ case "empty-folder-name":
126
+ return `folder ${violation.folder} has an empty name`;
127
+ case "missing-parent":
128
+ return `folder ${violation.folder} names a parent that does not exist (${violation.parent})`;
129
+ case "cycle":
130
+ return `folder ${violation.folder} sits in a cycle of parents`;
131
+ case "duplicate-name":
132
+ return violation.parent === undefined
133
+ ? `the ${violation.item} name "${violation.name}" is used more than once at the top level`
134
+ : `the ${violation.item} name "${violation.name}" is used more than once inside folder ${violation.parent}`;
135
+ case "unknown-assignment-folder":
136
+ return `project ${violation.project} is assigned to a folder that does not exist (${violation.folder})`;
137
+ case "unknown-template-assignment-folder":
138
+ return `template ${violation.template} is assigned to a folder that does not exist (${violation.folder})`;
139
+ }
140
+ }
141
+
142
+ //
143
+ // Internals
144
+ //
145
+
146
+ /**
147
+ * True when following parents from this folder leads back to it. A folder that merely hangs below
148
+ * a cycle is not on it.
149
+ */
150
+ function onCycle(id: FolderId, byId: ReadonlyMap<FolderId, { parent?: FolderId }>): boolean {
151
+ const seen = new Set<FolderId>();
152
+ let current = byId.get(id)?.parent;
153
+ while (current !== undefined && !seen.has(current)) {
154
+ if (current === id) return true;
155
+ seen.add(current);
156
+ current = byId.get(current)?.parent;
157
+ }
158
+ return false;
159
+ }
@@ -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,59 @@
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 — and one kind of item. No two folders, no two projects and no two
6
+ * templates inside the same parent can carry the same name, while a folder, a project and a
7
+ * template beside each other may: the list shows each kind under its own heading, so they are
8
+ * never mistaken for one another. Comparison ignores case and surrounding whitespace: `Samples`,
9
+ * `samples` and ` Samples ` are the same thing to the person looking at them.
10
+ *
11
+ * The rule auto-renames only where a machine chose the name — a move and a duplicate. Where a
12
+ * human typed the name, a caller uses {@link foldersNameTaken} and rejects the name with
13
+ * an error instead: a text field that quietly disagrees with what was typed is worse than one
14
+ * that says no.
15
+ *
16
+ * Names already stored are never rewritten. Accounts predating folders may hold collisions,
17
+ * because nothing enforced uniqueness before.
18
+ */
19
+
20
+ /** True when a sibling already carries this name, compared as the rule above compares names. */
21
+ export function foldersNameTaken(name: string, siblingNames: Iterable<string>): boolean {
22
+ const wanted = normalizeName(name);
23
+ for (const sibling of siblingNames) if (normalizeName(sibling) === wanted) return true;
24
+ return false;
25
+ }
26
+
27
+ /** True when a name has nothing in it but whitespace; a folder cannot be called that. */
28
+ export function foldersNameBlank(name: string): boolean {
29
+ return name.trim() === "";
30
+ }
31
+
32
+ /**
33
+ * The name to use for an item landing beside `siblingNames`.
34
+ *
35
+ * A free name is returned untouched. A taken one gains an `X (Copy)`, `X (Copy 2)` … suffix, and
36
+ * an existing copy suffix is replaced rather than stacked, so repeated copies stay readable.
37
+ */
38
+ export function foldersUniqueName(desiredName: string, siblingNames: Iterable<string>): string {
39
+ const taken = new Set<string>();
40
+ for (const sibling of siblingNames) taken.add(normalizeName(sibling));
41
+
42
+ if (!taken.has(normalizeName(desiredName))) return desiredName;
43
+
44
+ const base = desiredName.replace(COPY_SUFFIX, "");
45
+ let candidate = `${base} (Copy)`;
46
+ for (let index = 2; taken.has(normalizeName(candidate)); index++)
47
+ candidate = `${base} (Copy ${index})`;
48
+ return candidate;
49
+ }
50
+
51
+ //
52
+ // Internals
53
+ //
54
+
55
+ const COPY_SUFFIX = / \(Copy(?: \d+)?\)$/;
56
+
57
+ function normalizeName(name: string): string {
58
+ return name.trim().toLowerCase();
59
+ }