1import { test } from "node:test";
2import assert from "node:assert/strict";
3import {
4 Stack,
5 Shelf,
6 Switcher,
7 type Composition,
8} from "../documentation/components/source/composition.ts";
9import { chooseMember } from "../documentation/components/source/composition-editing.ts";
10import { outlineLabel } from "../documentation/components/source/outline-panel.ts";
11import {
12 selectionAfter,
13 subjectOf,
14} from "../documentation/components/source/workbench-selection.ts";
15import { editsFor } from "../documentation/components/source/working-copy.ts";
16
17const text = (value: string): Composition => ({ kind: "Text", text: value });
18const [a, b, c] = ["a", "b", "c"].map(text) as [
19 Composition,
20 Composition,
21 Composition,
22];
23const tree = Stack(a, b, Switcher(c, text("d")), Shelf());
24
25test("a plain choice selects that one node, whatever was selected", () => {
26 for (const chosen of [[], ["0"], ["0.0"], ["0.0", "0.1"], ["0.2.0"]])
27 for (const path of ["0", "0.1", "0.2", "0.2.1", "0.3"])
28 assert.deepEqual(selectionAfter(tree, chosen, path, false), [path]);
29 // The lesson's rule would have built on the selection instead.
30 assert.deepEqual(chooseMember(tree, ["0.0"], "0.1"), ["0.0", "0.1"]);
31 // Choosing the selected node again keeps it, so the wording stays editable.
32 assert.deepEqual(selectionAfter(tree, ["0.0"], "0.0", false), ["0.0"]);
33 assert.ok(editsFor(tree, selectionAfter(tree, ["0.0"], "0.1", false)).text);
34});
35
36test("an extended choice builds on the selection as the lesson's rule does", () => {
37 const neighbors = selectionAfter(tree, ["0.0"], "0.1", true);
38 assert.deepEqual(neighbors, ["0.0", "0.1"]);
39 assert.deepEqual(
40 selectionAfter(tree, neighbors, "0.2", true),
41 ["0.0", "0.1", "0.2"],
42 "a group joins as a member",
43 );
44 assert.deepEqual(selectionAfter(tree, neighbors, "0.0", true), ["0.1"]);
45 assert.deepEqual(
46 selectionAfter(tree, ["0.0"], "0.2", true),
47 ["0.2"],
48 "a member that is not a neighbor starts again",
49 );
50 assert.deepEqual(
51 selectionAfter(tree, ["0.0"], "0.2.0", true),
52 ["0.2.0"],
53 "so does a member of another group",
54 );
55 for (const [chosen, path] of [
56 [["0.0"], "0.1"],
57 [["0.0", "0.1"], "0.2"],
58 [["0.0", "0.1"], "0.1"],
59 [["0.1"], "0.3"],
60 ] as const)
61 if (chooseMember(tree, chosen, path).length)
62 assert.deepEqual(
63 selectionAfter(tree, chosen, path, true),
64 chooseMember(tree, chosen, path),
65 );
66});
67
68test("the workbench's selection is never empty", () => {
69 // Leaving out the only selected member would leave nothing to inspect.
70 assert.deepEqual(chooseMember(tree, ["0.1"], "0.1"), []);
71 assert.deepEqual(selectionAfter(tree, ["0.1"], "0.1", true), ["0.1"]);
72 assert.deepEqual(subjectOf([]), ["0"], "the whole composition");
73 assert.deepEqual(subjectOf(["0.2", "0.3"]), ["0.2", "0.3"]);
74 // A choice that names nothing changes nothing.
75 for (const extend of [false, true])
76 assert.deepEqual(selectionAfter(tree, ["0.1"], "0.9", extend), ["0.1"]);
77 // The whole composition can be arranged and added to: a usable subject.
78 const edits = editsFor(tree, subjectOf([]));
79 assert.equal(edits.arrange?.path, "0");
80 assert.equal(edits.inside?.parent, "0");
81});
82
83test("an outline row says in words what each node is", () => {
84 assert.equal(outlineLabel(tree, true), "Whole composition");
85 assert.equal(outlineLabel(Stack(), true), "Whole composition, empty");
86 assert.equal(outlineLabel(Switcher(c, a), false), "2 members");
87 assert.equal(outlineLabel(Stack(a), false), "1 member");
88 assert.equal(outlineLabel(Shelf(), false), "Empty group");
89 assert.equal(outlineLabel(text("Saturday"), false), "Saturday");
90 assert.equal(outlineLabel(text(""), false), "Empty text");
91});