@dxos/app-graph 0.4.6 → 0.4.7-main.06facb4

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/src/testing.ts DELETED
@@ -1,152 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import { type Action } from './action';
6
- import { type Graph } from './graph';
7
- import { type NodeBuilder, type Node } from './node';
8
-
9
- /**
10
- * Create a test node builder that always adds nodes and actions to the specified depth.
11
- *
12
- * @param id The id of the node builder, used to identify nodes created by this builder.
13
- * @param depth The depth at which to add nodes and actions.
14
- * @default 1
15
- *
16
- * @returns A test node builder
17
- */
18
- // TODO(burdon): Change to TestNodeBuilder class (see other builder/generator patterns in client/echo).
19
- export const createTestNodeBuilder = (id: string, depth = 1) => {
20
- const nodes = new Map<string, Node>();
21
- const nodeBuilder: NodeBuilder = (parent) => {
22
- if (checkDepth(parent) >= depth) {
23
- return;
24
- }
25
-
26
- const [child] = parent.addNode(id, {
27
- id: `${parent.id}-${id}`,
28
- label: `${parent.id}-${id}`,
29
- data: null,
30
- parent,
31
- });
32
-
33
- parent.addAction({
34
- id: `${parent.id}-${id}`,
35
- label: `${parent.id}-${id}`,
36
- invoke: () => {},
37
- });
38
-
39
- nodes.set(parent.id, parent);
40
- nodes.set(child.id, child);
41
- };
42
-
43
- const addNode = (parentId: string, node: Pick<Node, 'id' | 'label'> & Partial<Node>) => {
44
- const parent = nodes.get(parentId);
45
- if (!parent) {
46
- return;
47
- }
48
-
49
- const [child] = parent.addNode(id, node);
50
- nodes.set(child.id, child);
51
- return child;
52
- };
53
-
54
- const removeNode = (parentId: string, id: string) => {
55
- const parent = nodes.get(parentId);
56
- if (!parent) {
57
- return;
58
- }
59
-
60
- return parent.removeNode(id);
61
- };
62
-
63
- const addAction = (parentId: string, action: Pick<Action, 'id' | 'label' | 'invoke'> & Partial<Action>) => {
64
- const parent = nodes.get(parentId);
65
- if (!parent) {
66
- return;
67
- }
68
-
69
- return parent.addAction(action);
70
- };
71
-
72
- const removeAction = (parentId: string, id: string) => {
73
- const parent = nodes.get(parentId);
74
- if (!parent) {
75
- return;
76
- }
77
-
78
- return parent.removeAction(id);
79
- };
80
-
81
- const addProperty = (parentId: string, key: string, value: any) => {
82
- const parent = nodes.get(parentId);
83
- if (!parent) {
84
- return;
85
- }
86
-
87
- return parent.addProperty(key, value);
88
- };
89
-
90
- const removeProperty = (parentId: string, key: string) => {
91
- const parent = nodes.get(parentId);
92
- if (!parent) {
93
- return;
94
- }
95
-
96
- return parent.removeProperty(key);
97
- };
98
-
99
- return { nodeBuilder, addNode, removeNode, addAction, removeAction, addProperty, removeProperty };
100
- };
101
-
102
- /**
103
- * Build a graph from a nested list of nodes.
104
- *
105
- * @param graph Graph to add nodes to.
106
- * @param nodes Nodes to add to the
107
- *
108
- * @example
109
- * const graph = new GraphStore();
110
- * buildGraph(graph, [
111
- * {
112
- * id: 'test1',
113
- * label: 'test1',
114
- * children: [
115
- * {
116
- * id: 'test1.1',
117
- * label: 'test1.1',
118
- * },
119
- * {
120
- * id: 'test1.2',
121
- * label: 'test1.2',
122
- * },
123
- * ],
124
- * },
125
- * {
126
- * id: 'test2',
127
- * label: 'test2',
128
- * },
129
- * ]);
130
- */
131
-
132
- // TODO(wittjosiah): Type nodes.
133
- export const buildGraph = (graph: Graph, id: string, nodes: any[]) => {
134
- addNodes(graph.root, id, nodes);
135
- return graph;
136
- };
137
-
138
- const addNodes = (root: Node, id: string, nodes: any[]) => {
139
- nodes.forEach((node) => {
140
- const [child] = root.addNode(id, node);
141
- addNodes(child, id, node.children || []);
142
- node.actions?.forEach((action: any) => child.addAction(action));
143
- });
144
- };
145
-
146
- const checkDepth = (node: Node, depth = 0): number => {
147
- if (!node.parent) {
148
- return depth;
149
- }
150
-
151
- return checkDepth(node.parent, depth + 1);
152
- };