1import { test } from "node:test";
2import assert from "node:assert/strict";
3import {
4 example,
5 Stack,
6} from "../documentation/components/source/composition.ts";
7import {
8 atPath,
9 groupingReason,
10 neighboringMembers,
11 groupMembers,
12 ungroupMembers,
13 restoreGrouping,
14} from "../documentation/components/source/grouping.ts";
15
16test("grouping preserves content and order, and ungrouping restores the original tree", () => {
17 const initial = example("flat");
18 const grouped = groupMembers(initial, ["0.1", "0.0"]);
19 assert.equal(atPath(grouped, "0.0")?.kind, "Stack");
20 assert.deepEqual(atPath(grouped, "0.0.0"), atPath(initial, "0.0"));
21 assert.deepEqual(atPath(grouped, "0.1"), atPath(initial, "0.2"));
22 assert.deepEqual(ungroupMembers(grouped, "0.0"), initial);
23 assert.equal(atPath(initial, "0.0")?.kind, "Text");
24});
25test("an authored group composes with a neighboring leaf, then unwraps at either depth", () => {
26 const initial = example("event");
27 const pair = groupMembers(initial, ["0.0", "0.1"]);
28 const larger = groupMembers(pair, ["0.0", "0.1"]);
29 assert.equal(atPath(larger, "0.0.0")?.kind, "Stack");
30 assert.deepEqual(ungroupMembers(larger, "0.0"), pair);
31 const flattened = ungroupMembers(larger, "0.0.0");
32 assert.deepEqual(atPath(flattened, "0.0.2"), atPath(initial, "0.2"));
33});
34test("nonadjacent, repeated, stale, different-level and redundant selections cannot edit", () => {
35 const tree = example("nested");
36 for (const paths of [
37 ["0.0.0"],
38 ["0.0", "0.0"],
39 ["0.0.0", "0.1.0"],
40 ["0.0", "0.0.0"],
41 ["0.5", "0.6"],
42 ["0.0", "0.1"],
43 ]) {
44 assert.ok(groupingReason(tree, paths));
45 assert.throws(() => groupMembers(tree, paths));
46 }
47 assert.ok(groupingReason(example("flat"), ["0.0", "0.2"]));
48 assert.throws(() => ungroupMembers(tree, "0"));
49 assert.throws(() => ungroupMembers(tree, "0.0.0"));
50});
51test("valid saved regroupings restore while changed, reordered or pathological data is ignored", () => {
52 const initial = example("flat");
53 const grouped = groupMembers(initial, ["0.1", "0.2"]);
54 assert.deepEqual(
55 restoreGrouping(JSON.parse(JSON.stringify(grouped)), initial),
56 grouped,
57 );
58 assert.equal(restoreGrouping(example("event"), initial), undefined);
59 if (!("children" in initial)) throw new Error("Expected group");
60 assert.equal(
61 restoreGrouping(Stack(...[...initial.children].reverse()), initial),
62 undefined,
63 );
64 assert.equal(
65 restoreGrouping({ kind: "Stack", children: [initial] }, initial),
66 undefined,
67 );
68 assert.equal(restoreGrouping(null, initial), undefined);
69 assert.equal(atPath(initial, "0.-1"), undefined);
70});
71
72test("a continuous selection can span all siblings without permitting a redundant group", () => {
73 const tree = example("flat");
74 assert.equal(neighboringMembers(tree, ["0.3", "0.2", "0.1", "0.0"]), true);
75 assert.ok(groupingReason(tree, ["0.0", "0.1", "0.2", "0.3"]));
76 assert.equal(neighboringMembers(tree, ["0.0", "0.2"]), false);
77 assert.equal(neighboringMembers(tree, ["0.0", "0.0"]), false);
78 assert.equal(neighboringMembers(tree, ["0"]), false);
79});