@rsconcept/domain 1.0.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 (224) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +55 -0
  3. package/dist/cctext/index.d.ts +1 -0
  4. package/dist/cctext/index.js +42 -0
  5. package/dist/cctext/index.js.map +1 -0
  6. package/dist/cctext/language-api.d.ts +43 -0
  7. package/dist/cctext/language-api.js +252 -0
  8. package/dist/cctext/language-api.js.map +1 -0
  9. package/dist/cctext/language.d.ts +58 -0
  10. package/dist/cctext/language.js +44 -0
  11. package/dist/cctext/language.js.map +1 -0
  12. package/dist/graph/graph.d.ts +62 -0
  13. package/dist/graph/graph.js +385 -0
  14. package/dist/graph/graph.js.map +1 -0
  15. package/dist/graph/index.d.ts +1 -0
  16. package/dist/graph/index.js +384 -0
  17. package/dist/graph/index.js.map +1 -0
  18. package/dist/index.d.ts +28 -0
  19. package/dist/index.js +5851 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/library/folder-tree.d.ts +32 -0
  22. package/dist/library/folder-tree.js +136 -0
  23. package/dist/library/folder-tree.js.map +1 -0
  24. package/dist/library/index.d.ts +17 -0
  25. package/dist/library/index.js +2800 -0
  26. package/dist/library/index.js.map +1 -0
  27. package/dist/library/library-api.d.ts +6 -0
  28. package/dist/library/library-api.js +13 -0
  29. package/dist/library/library-api.js.map +1 -0
  30. package/dist/library/library.d.ts +56 -0
  31. package/dist/library/library.js +23 -0
  32. package/dist/library/library.js.map +1 -0
  33. package/dist/library/oss-api.d.ts +47 -0
  34. package/dist/library/oss-api.js +1105 -0
  35. package/dist/library/oss-api.js.map +1 -0
  36. package/dist/library/oss-layout-api.d.ts +36 -0
  37. package/dist/library/oss-layout-api.js +330 -0
  38. package/dist/library/oss-layout-api.js.map +1 -0
  39. package/dist/library/oss-layout.d.ts +25 -0
  40. package/dist/library/oss-layout.js +1 -0
  41. package/dist/library/oss-layout.js.map +1 -0
  42. package/dist/library/oss.d.ts +136 -0
  43. package/dist/library/oss.js +30 -0
  44. package/dist/library/oss.js.map +1 -0
  45. package/dist/library/rsengine.d.ts +116 -0
  46. package/dist/library/rsengine.js +2604 -0
  47. package/dist/library/rsengine.js.map +1 -0
  48. package/dist/library/rsform-api.d.ts +74 -0
  49. package/dist/library/rsform-api.js +879 -0
  50. package/dist/library/rsform-api.js.map +1 -0
  51. package/dist/library/rsform.d.ts +206 -0
  52. package/dist/library/rsform.js +32 -0
  53. package/dist/library/rsform.js.map +1 -0
  54. package/dist/library/rsmodel-api.d.ts +43 -0
  55. package/dist/library/rsmodel-api.js +836 -0
  56. package/dist/library/rsmodel-api.js.map +1 -0
  57. package/dist/library/rsmodel.d.ts +52 -0
  58. package/dist/library/rsmodel.js +25 -0
  59. package/dist/library/rsmodel.js.map +1 -0
  60. package/dist/library/structure-planner.d.ts +33 -0
  61. package/dist/library/structure-planner.js +481 -0
  62. package/dist/library/structure-planner.js.map +1 -0
  63. package/dist/parsing/ast.d.ts +49 -0
  64. package/dist/parsing/ast.js +93 -0
  65. package/dist/parsing/ast.js.map +1 -0
  66. package/dist/parsing/index.d.ts +3 -0
  67. package/dist/parsing/index.js +141 -0
  68. package/dist/parsing/index.js.map +1 -0
  69. package/dist/parsing/lezer-tree.d.ts +13 -0
  70. package/dist/parsing/lezer-tree.js +50 -0
  71. package/dist/parsing/lezer-tree.js.map +1 -0
  72. package/dist/rslang/api.d.ts +53 -0
  73. package/dist/rslang/api.js +846 -0
  74. package/dist/rslang/api.js.map +1 -0
  75. package/dist/rslang/ast-annotations.d.ts +18 -0
  76. package/dist/rslang/ast-annotations.js +56 -0
  77. package/dist/rslang/ast-annotations.js.map +1 -0
  78. package/dist/rslang/error.d.ts +85 -0
  79. package/dist/rslang/error.js +159 -0
  80. package/dist/rslang/error.js.map +1 -0
  81. package/dist/rslang/eval/calculator.d.ts +43 -0
  82. package/dist/rslang/eval/calculator.js +1639 -0
  83. package/dist/rslang/eval/calculator.js.map +1 -0
  84. package/dist/rslang/eval/evaluation-cache.d.ts +36 -0
  85. package/dist/rslang/eval/evaluation-cache.js +310 -0
  86. package/dist/rslang/eval/evaluation-cache.js.map +1 -0
  87. package/dist/rslang/eval/evaluator.d.ts +70 -0
  88. package/dist/rslang/eval/evaluator.js +1514 -0
  89. package/dist/rslang/eval/evaluator.js.map +1 -0
  90. package/dist/rslang/eval/value-api.d.ts +48 -0
  91. package/dist/rslang/eval/value-api.js +490 -0
  92. package/dist/rslang/eval/value-api.js.map +1 -0
  93. package/dist/rslang/eval/value.d.ts +36 -0
  94. package/dist/rslang/eval/value.js +118 -0
  95. package/dist/rslang/eval/value.js.map +1 -0
  96. package/dist/rslang/index.d.ts +17 -0
  97. package/dist/rslang/index.js +4314 -0
  98. package/dist/rslang/index.js.map +1 -0
  99. package/dist/rslang/labels.d.ts +16 -0
  100. package/dist/rslang/labels.js +315 -0
  101. package/dist/rslang/labels.js.map +1 -0
  102. package/dist/rslang/parser/expression-generator.d.ts +10 -0
  103. package/dist/rslang/parser/expression-generator.js +451 -0
  104. package/dist/rslang/parser/expression-generator.js.map +1 -0
  105. package/dist/rslang/parser/normalize.d.ts +11 -0
  106. package/dist/rslang/parser/normalize.js +507 -0
  107. package/dist/rslang/parser/normalize.js.map +1 -0
  108. package/dist/rslang/parser/parser.d.ts +5 -0
  109. package/dist/rslang/parser/parser.js +24 -0
  110. package/dist/rslang/parser/parser.js.map +1 -0
  111. package/dist/rslang/parser/parser.terms.d.ts +42 -0
  112. package/dist/rslang/parser/parser.terms.js +84 -0
  113. package/dist/rslang/parser/parser.terms.js.map +1 -0
  114. package/dist/rslang/parser/syntax-errors.d.ts +11 -0
  115. package/dist/rslang/parser/syntax-errors.js +403 -0
  116. package/dist/rslang/parser/syntax-errors.js.map +1 -0
  117. package/dist/rslang/parser/token.d.ts +79 -0
  118. package/dist/rslang/parser/token.js +95 -0
  119. package/dist/rslang/parser/token.js.map +1 -0
  120. package/dist/rslang/semantic/analyzer.d.ts +39 -0
  121. package/dist/rslang/semantic/analyzer.js +2604 -0
  122. package/dist/rslang/semantic/analyzer.js.map +1 -0
  123. package/dist/rslang/semantic/arguments-extractor.d.ts +42 -0
  124. package/dist/rslang/semantic/arguments-extractor.js +366 -0
  125. package/dist/rslang/semantic/arguments-extractor.js.map +1 -0
  126. package/dist/rslang/semantic/type-auditor.d.ts +73 -0
  127. package/dist/rslang/semantic/type-auditor.js +1570 -0
  128. package/dist/rslang/semantic/type-auditor.js.map +1 -0
  129. package/dist/rslang/semantic/typification-api.d.ts +27 -0
  130. package/dist/rslang/semantic/typification-api.js +320 -0
  131. package/dist/rslang/semantic/typification-api.js.map +1 -0
  132. package/dist/rslang/semantic/typification-parser.d.ts +12 -0
  133. package/dist/rslang/semantic/typification-parser.js +226 -0
  134. package/dist/rslang/semantic/typification-parser.js.map +1 -0
  135. package/dist/rslang/semantic/typification.d.ts +119 -0
  136. package/dist/rslang/semantic/typification.js +74 -0
  137. package/dist/rslang/semantic/typification.js.map +1 -0
  138. package/dist/rslang/semantic/value-auditor.d.ts +43 -0
  139. package/dist/rslang/semantic/value-auditor.js +523 -0
  140. package/dist/rslang/semantic/value-auditor.js.map +1 -0
  141. package/dist/rslang/semantic/value-class.d.ts +10 -0
  142. package/dist/rslang/semantic/value-class.js +9 -0
  143. package/dist/rslang/semantic/value-class.js.map +1 -0
  144. package/dist/rslang/typification-graph.d.ts +33 -0
  145. package/dist/rslang/typification-graph.js +311 -0
  146. package/dist/rslang/typification-graph.js.map +1 -0
  147. package/dist/shared/branded.d.ts +7 -0
  148. package/dist/shared/branded.js +1 -0
  149. package/dist/shared/branded.js.map +1 -0
  150. package/dist/shared/hash.d.ts +6 -0
  151. package/dist/shared/hash.js +18 -0
  152. package/dist/shared/hash.js.map +1 -0
  153. package/dist/shared/index.d.ts +2 -0
  154. package/dist/shared/index.js +18 -0
  155. package/dist/shared/index.js.map +1 -0
  156. package/package.json +184 -0
  157. package/src/cctext/index.ts +9 -0
  158. package/src/cctext/language-api.test.ts +149 -0
  159. package/src/cctext/language-api.ts +285 -0
  160. package/src/cctext/language.ts +80 -0
  161. package/src/graph/graph.test.ts +392 -0
  162. package/src/graph/graph.ts +433 -0
  163. package/src/graph/index.ts +1 -0
  164. package/src/index.ts +96 -0
  165. package/src/library/folder-tree.test.ts +47 -0
  166. package/src/library/folder-tree.ts +156 -0
  167. package/src/library/index.ts +46 -0
  168. package/src/library/library-api.test.ts +32 -0
  169. package/src/library/library-api.ts +11 -0
  170. package/src/library/library.ts +61 -0
  171. package/src/library/oss-api.ts +449 -0
  172. package/src/library/oss-layout-api.ts +377 -0
  173. package/src/library/oss-layout.ts +27 -0
  174. package/src/library/oss.ts +150 -0
  175. package/src/library/rsengine.ts +593 -0
  176. package/src/library/rsform-api.ts +533 -0
  177. package/src/library/rsform.ts +228 -0
  178. package/src/library/rsmodel-api.ts +340 -0
  179. package/src/library/rsmodel.ts +50 -0
  180. package/src/library/structure-planner.ts +143 -0
  181. package/src/parsing/ast.ts +136 -0
  182. package/src/parsing/index.ts +15 -0
  183. package/src/parsing/lezer-tree.ts +69 -0
  184. package/src/rslang/api.test.ts +116 -0
  185. package/src/rslang/api.ts +183 -0
  186. package/src/rslang/ast-annotations.ts +70 -0
  187. package/src/rslang/error.ts +129 -0
  188. package/src/rslang/eval/calculator.test.ts +124 -0
  189. package/src/rslang/eval/calculator.ts +121 -0
  190. package/src/rslang/eval/evaluation-cache.ts +257 -0
  191. package/src/rslang/eval/evaluator.test.ts +352 -0
  192. package/src/rslang/eval/evaluator.ts +935 -0
  193. package/src/rslang/eval/value-api.test.ts +105 -0
  194. package/src/rslang/eval/value-api.ts +444 -0
  195. package/src/rslang/eval/value.ts +102 -0
  196. package/src/rslang/index.ts +23 -0
  197. package/src/rslang/labels.ts +191 -0
  198. package/src/rslang/parser/expression-generator.test.ts +100 -0
  199. package/src/rslang/parser/expression-generator.ts +466 -0
  200. package/src/rslang/parser/normalize.test.ts +99 -0
  201. package/src/rslang/parser/normalize.ts +462 -0
  202. package/src/rslang/parser/parser.terms.ts +42 -0
  203. package/src/rslang/parser/parser.test.ts +153 -0
  204. package/src/rslang/parser/parser.ts +20 -0
  205. package/src/rslang/parser/rslang.grammar +251 -0
  206. package/src/rslang/parser/syntax-errors.ts +209 -0
  207. package/src/rslang/parser/token.ts +106 -0
  208. package/src/rslang/semantic/analyzer.test.ts +59 -0
  209. package/src/rslang/semantic/analyzer.ts +179 -0
  210. package/src/rslang/semantic/arguments-extractor.ts +327 -0
  211. package/src/rslang/semantic/type-auditor.test.ts +326 -0
  212. package/src/rslang/semantic/type-auditor.ts +1049 -0
  213. package/src/rslang/semantic/typification-api.test.ts +46 -0
  214. package/src/rslang/semantic/typification-api.ts +321 -0
  215. package/src/rslang/semantic/typification-parser.test.ts +50 -0
  216. package/src/rslang/semantic/typification-parser.ts +220 -0
  217. package/src/rslang/semantic/typification.ts +180 -0
  218. package/src/rslang/semantic/value-auditor.test.ts +206 -0
  219. package/src/rslang/semantic/value-auditor.ts +332 -0
  220. package/src/rslang/semantic/value-class.ts +11 -0
  221. package/src/rslang/typification-graph.ts +155 -0
  222. package/src/shared/branded.ts +6 -0
  223. package/src/shared/hash.ts +17 -0
  224. package/src/shared/index.ts +2 -0
@@ -0,0 +1,392 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { Graph } from './graph';
4
+
5
+ describe('Testing Graph construction', () => {
6
+ it('empty Graph should be empty', () => {
7
+ const graph = new Graph();
8
+ expect(graph.nodes.size).toBe(0);
9
+ });
10
+
11
+ it('adding edges should create nodes', () => {
12
+ const graph = new Graph();
13
+ graph.addEdge(13, 37);
14
+ expect([...graph.nodes.keys()]).toStrictEqual([13, 37]);
15
+
16
+ graph.addEdge(13, 38);
17
+ expect([...graph.nodes.keys()]).toStrictEqual([13, 37, 38]);
18
+ });
19
+
20
+ it('creating from array', () => {
21
+ const graph = new Graph([[1, 2], [3], [4, 1]]);
22
+ expect([...graph.nodes.keys()]).toStrictEqual([1, 2, 3, 4]);
23
+ expect([...graph.nodes.get(1)!.outputs]).toStrictEqual([2]);
24
+ });
25
+
26
+ it('cloning', () => {
27
+ const graph = new Graph([[1, 2], [3], [4, 1]]);
28
+ const clone = graph.clone();
29
+ expect([...graph.nodes.keys()]).toStrictEqual([...clone.nodes.keys()]);
30
+ expect([...graph.nodes.values()]).toStrictEqual([...clone.nodes.values()]);
31
+
32
+ clone.removeNode(3);
33
+ expect(clone.nodes.get(3)).toBeUndefined();
34
+ expect(graph.nodes.get(3)).not.toBeUndefined();
35
+ });
36
+ });
37
+
38
+ describe('Testing Graph editing', () => {
39
+ it('removing edges should not remove nodes', () => {
40
+ const graph = new Graph([[1, 2], [3], [4, 1]]);
41
+ expect(graph.hasEdge(4, 1)).toBeTruthy();
42
+
43
+ graph.removeEdge(5, 0);
44
+ graph.removeEdge(4, 1);
45
+
46
+ expect([...graph.nodes.keys()]).toStrictEqual([1, 2, 3, 4]);
47
+ expect(graph.hasEdge(4, 1)).toBeFalsy();
48
+ });
49
+
50
+ it('folding node redirects edges', () => {
51
+ const graph = new Graph([
52
+ [1, 3],
53
+ [2, 3],
54
+ [3, 4],
55
+ [3, 5],
56
+ [3, 3]
57
+ ]);
58
+ graph.foldNode(3);
59
+ expect(graph.hasNode(3)).toBeFalsy();
60
+ expect(graph.hasEdge(1, 4)).toBeTruthy();
61
+ expect(graph.hasEdge(1, 5)).toBeTruthy();
62
+ expect(graph.hasEdge(2, 4)).toBeTruthy();
63
+ expect(graph.hasEdge(2, 5)).toBeTruthy();
64
+ });
65
+
66
+ it('folding a non-existent node should not change the graph', () => {
67
+ const graph = new Graph([
68
+ [1, 2],
69
+ [2, 3]
70
+ ]);
71
+ const clone = graph.clone();
72
+ graph.foldNode(99); // Node 99 does not exist
73
+ expect(graph.nodes.size).toBe(clone.nodes.size);
74
+ expect([...graph.nodes.keys()]).toEqual([...clone.nodes.keys()]);
75
+ expect(graph.hasEdge(1, 2)).toBeTruthy();
76
+ expect(graph.hasEdge(2, 3)).toBeTruthy();
77
+ });
78
+
79
+ it('folding a node with no inputs', () => {
80
+ const graph = new Graph([
81
+ [1, 2],
82
+ [1, 3]
83
+ ]);
84
+ graph.foldNode(1);
85
+ expect(graph.hasNode(1)).toBeFalsy();
86
+ expect(graph.nodes.size).toBe(2); // Nodes 2 and 3 remain
87
+ expect(graph.hasNode(2)).toBeTruthy();
88
+ expect(graph.hasNode(3)).toBeTruthy();
89
+ });
90
+
91
+ it('folding a node with no outputs', () => {
92
+ const graph = new Graph([
93
+ [1, 3],
94
+ [2, 3]
95
+ ]);
96
+ graph.foldNode(3);
97
+ expect(graph.hasNode(3)).toBeFalsy();
98
+ expect(graph.nodes.size).toBe(2); // Nodes 1 and 2 remain
99
+ expect(graph.hasNode(1)).toBeTruthy();
100
+ expect(graph.hasNode(2)).toBeTruthy();
101
+ expect(graph.nodes.get(1)!.outputs.length).toBe(0);
102
+ expect(graph.nodes.get(2)!.outputs.length).toBe(0);
103
+ });
104
+
105
+ it('removing isolated nodes', () => {
106
+ const graph = new Graph([[9, 1], [9, 2], [2, 1], [4, 3], [5, 9], [7], [8]]);
107
+ graph.removeIsolated();
108
+ expect([...graph.nodes.keys()]).toStrictEqual([9, 1, 2, 4, 3, 5]);
109
+ });
110
+
111
+ it('transitive reduction', () => {
112
+ const graph = new Graph([
113
+ [1, 3],
114
+ [1, 2],
115
+ [2, 3]
116
+ ]);
117
+ graph.transitiveReduction();
118
+ expect(graph.hasEdge(1, 2)).toBeTruthy();
119
+ expect(graph.hasEdge(2, 3)).toBeTruthy();
120
+ expect(graph.hasEdge(1, 3)).toBeFalsy();
121
+ });
122
+
123
+ it('transitive reduction - empty graph', () => {
124
+ const graph = new Graph();
125
+ expect(() => graph.transitiveReduction()).not.toThrow();
126
+ expect(graph.nodes.size).toBe(0);
127
+ });
128
+
129
+ it('transitive reduction - single node', () => {
130
+ const graph = new Graph([[1]]);
131
+ graph.transitiveReduction();
132
+ expect(graph.hasNode(1)).toBeTruthy();
133
+ expect(graph.nodes.get(1)!.outputs.length).toBe(0);
134
+ expect(graph.nodes.get(1)!.inputs.length).toBe(0);
135
+ });
136
+
137
+ it('transitive reduction - linear chain', () => {
138
+ const graph = new Graph([
139
+ [1, 2],
140
+ [2, 3],
141
+ [3, 4],
142
+ [4, 5]
143
+ ]);
144
+ graph.transitiveReduction();
145
+ expect(graph.hasEdge(1, 2)).toBeTruthy();
146
+ expect(graph.hasEdge(2, 3)).toBeTruthy();
147
+ expect(graph.hasEdge(3, 4)).toBeTruthy();
148
+ expect(graph.hasEdge(4, 5)).toBeTruthy();
149
+ expect(graph.hasEdge(1, 3)).toBeFalsy();
150
+ expect(graph.hasEdge(1, 4)).toBeFalsy();
151
+ expect(graph.hasEdge(1, 5)).toBeFalsy();
152
+ expect(graph.hasEdge(2, 4)).toBeFalsy();
153
+ expect(graph.hasEdge(2, 5)).toBeFalsy();
154
+ expect(graph.hasEdge(3, 5)).toBeFalsy();
155
+ });
156
+
157
+ it('transitive reduction - diamond pattern', () => {
158
+ const graph = new Graph([
159
+ [1, 2],
160
+ [1, 3],
161
+ [2, 4],
162
+ [3, 4]
163
+ ]);
164
+ graph.transitiveReduction();
165
+ expect(graph.hasEdge(1, 2)).toBeTruthy();
166
+ expect(graph.hasEdge(1, 3)).toBeTruthy();
167
+ expect(graph.hasEdge(2, 4)).toBeTruthy();
168
+ expect(graph.hasEdge(3, 4)).toBeTruthy();
169
+ expect(graph.hasEdge(1, 4)).toBeFalsy();
170
+ });
171
+
172
+ it('transitive reduction - complex transitive relationships', () => {
173
+ const graph = new Graph([
174
+ [6, 7],
175
+ [5, 7],
176
+ [4, 6],
177
+ [3, 5],
178
+ [2, 5],
179
+ [1, 4],
180
+ [1, 3],
181
+ [1, 2]
182
+ ]);
183
+ graph.transitiveReduction();
184
+ expect(graph.hasEdge(1, 2)).toBeTruthy();
185
+ expect(graph.hasEdge(1, 3)).toBeTruthy();
186
+ expect(graph.hasEdge(1, 4)).toBeTruthy();
187
+ expect(graph.hasEdge(2, 5)).toBeTruthy();
188
+ expect(graph.hasEdge(3, 5)).toBeTruthy();
189
+ expect(graph.hasEdge(4, 6)).toBeTruthy();
190
+ expect(graph.hasEdge(5, 7)).toBeTruthy();
191
+ expect(graph.hasEdge(6, 7)).toBeTruthy();
192
+
193
+ expect(graph.hasEdge(1, 5)).toBeFalsy();
194
+ expect(graph.hasEdge(1, 7)).toBeFalsy();
195
+ expect(graph.hasEdge(2, 7)).toBeFalsy();
196
+ expect(graph.hasEdge(3, 7)).toBeFalsy();
197
+ expect(graph.hasEdge(4, 7)).toBeFalsy();
198
+ });
199
+
200
+ it('transitive reduction - disconnected components', () => {
201
+ const graph = new Graph([
202
+ [1, 2],
203
+ [2, 3],
204
+ [4, 5],
205
+ [5, 6],
206
+ [4, 6] // This should be removed
207
+ ]);
208
+ graph.transitiveReduction();
209
+ expect(graph.hasEdge(1, 2)).toBeTruthy();
210
+ expect(graph.hasEdge(2, 3)).toBeTruthy();
211
+ expect(graph.hasEdge(4, 5)).toBeTruthy();
212
+ expect(graph.hasEdge(5, 6)).toBeTruthy();
213
+ expect(graph.hasEdge(4, 6)).toBeFalsy();
214
+ });
215
+
216
+ it('transitive reduction - multiple paths same length', () => {
217
+ const graph = new Graph([
218
+ [3, 4],
219
+ [2, 4],
220
+ [1, 3],
221
+ [1, 2]
222
+ ]);
223
+ graph.transitiveReduction();
224
+ expect(graph.hasEdge(1, 2)).toBeTruthy();
225
+ expect(graph.hasEdge(1, 3)).toBeTruthy();
226
+ expect(graph.hasEdge(2, 4)).toBeTruthy();
227
+ expect(graph.hasEdge(3, 4)).toBeTruthy();
228
+ });
229
+
230
+ it('transitive reduction - already reduced graph', () => {
231
+ const graph = new Graph([
232
+ [1, 2],
233
+ [2, 3]
234
+ ]);
235
+ const originalEdgeCount = 2;
236
+ graph.transitiveReduction();
237
+ expect(graph.hasEdge(1, 2)).toBeTruthy();
238
+ expect(graph.hasEdge(2, 3)).toBeTruthy();
239
+ expect(graph.hasEdge(1, 3)).toBeFalsy();
240
+ // Should still have 2 edges
241
+ let edgeCount = 0;
242
+ graph.nodes.forEach(node => (edgeCount += node.outputs.length));
243
+ expect(edgeCount).toBe(originalEdgeCount);
244
+ });
245
+
246
+ it('transitive reduction - preserves all nodes', () => {
247
+ const graph = new Graph([
248
+ [1, 2],
249
+ [1, 3],
250
+ [2, 3]
251
+ ]);
252
+ const originalNodeCount = graph.nodes.size;
253
+ graph.transitiveReduction();
254
+ expect(graph.nodes.size).toBe(originalNodeCount);
255
+ expect(graph.hasNode(1)).toBeTruthy();
256
+ expect(graph.hasNode(2)).toBeTruthy();
257
+ expect(graph.hasNode(3)).toBeTruthy();
258
+ });
259
+ });
260
+
261
+ describe('Testing Graph sort', () => {
262
+ it('topological order', () => {
263
+ const graph = new Graph([
264
+ [9, 1],
265
+ [9, 2],
266
+ [2, 1],
267
+ [4, 3],
268
+ [5, 9]
269
+ ]);
270
+ expect(graph.topologicalOrder()).toStrictEqual([5, 4, 3, 9, 2, 1]);
271
+ });
272
+ });
273
+
274
+ describe('Testing Graph queries', () => {
275
+ it('expand outputs', () => {
276
+ const graph = new Graph([
277
+ [1, 2], //
278
+ [2, 3],
279
+ [2, 5],
280
+ [5, 6],
281
+ [6, 1],
282
+ [7]
283
+ ]);
284
+ expect(graph.expandOutputs([])).toStrictEqual([]);
285
+ expect(graph.expandAllOutputs([])).toStrictEqual([]);
286
+ expect(graph.expandOutputs([3])).toStrictEqual([]);
287
+ expect(graph.expandAllOutputs([3])).toStrictEqual([]);
288
+ expect(graph.expandOutputs([7])).toStrictEqual([]);
289
+ expect(graph.expandAllOutputs([7])).toStrictEqual([]);
290
+ expect(graph.expandOutputs([2, 5])).toStrictEqual([3, 6]);
291
+ expect(graph.expandAllOutputs([2, 5])).toStrictEqual([3, 6, 1]);
292
+ });
293
+
294
+ it('expand into unique array', () => {
295
+ const graph = new Graph([
296
+ [1, 2],
297
+ [1, 3],
298
+ [2, 5],
299
+ [3, 5]
300
+ ]);
301
+ expect(graph.expandAllOutputs([1])).toStrictEqual([2, 3, 5]);
302
+ });
303
+
304
+ it('expand inputs', () => {
305
+ const graph = new Graph([
306
+ [1, 2], //
307
+ [2, 3],
308
+ [2, 5],
309
+ [5, 6],
310
+ [6, 1],
311
+ [7]
312
+ ]);
313
+ expect(graph.expandInputs([])).toStrictEqual([]);
314
+ expect(graph.expandAllInputs([])).toStrictEqual([]);
315
+ expect(graph.expandInputs([7])).toStrictEqual([]);
316
+ expect(graph.expandAllInputs([7])).toStrictEqual([]);
317
+ expect(graph.expandInputs([6])).toStrictEqual([5]);
318
+ expect(graph.expandAllInputs([6])).toStrictEqual([5, 2, 1]);
319
+ });
320
+
321
+ it('maximize part', () => {
322
+ const graph = new Graph([
323
+ [1, 7], //
324
+ [1, 3],
325
+ [2, 3],
326
+ [2, 4],
327
+ [3, 5],
328
+ [3, 6],
329
+ [3, 4],
330
+ [7, 5],
331
+ [8]
332
+ ]);
333
+ expect(graph.maximizePart([])).toStrictEqual([]);
334
+ expect(graph.maximizePart([8])).toStrictEqual([8]);
335
+ expect(graph.maximizePart([5])).toStrictEqual([5]);
336
+ expect(graph.maximizePart([3])).toStrictEqual([3, 6]);
337
+ expect(graph.maximizePart([3, 2])).toStrictEqual([3, 2, 6, 4]);
338
+ expect(graph.maximizePart([3, 1])).toStrictEqual([3, 1, 7, 5, 6]);
339
+ });
340
+
341
+ it('find elementary cycle', () => {
342
+ const graph = new Graph([
343
+ [1, 1] //
344
+ ]);
345
+ expect(graph.findCycle()).toStrictEqual([1, 1]);
346
+ });
347
+
348
+ it('find cycle acyclic', () => {
349
+ const graph = new Graph([
350
+ [1, 2], //
351
+ [2]
352
+ ]);
353
+ expect(graph.findCycle()).toStrictEqual(null);
354
+ });
355
+
356
+ it('find cycle typical', () => {
357
+ const graph = new Graph([
358
+ [1, 2], //
359
+ [1, 4],
360
+ [2, 3],
361
+ [3, 1],
362
+ [3, 4],
363
+ [4]
364
+ ]);
365
+ expect(graph.findCycle()).toStrictEqual([1, 2, 3, 1]);
366
+ });
367
+
368
+ it('find cycle acyclic 2 components', () => {
369
+ const graph = new Graph([
370
+ [0, 1], //
371
+ [2, 3],
372
+ [3, 0]
373
+ ]);
374
+ expect(graph.findCycle()).toStrictEqual(null);
375
+ });
376
+
377
+ it('sortStable should stably reorder node ids according to topological order', () => {
378
+ const graph = new Graph([
379
+ [1, 2],
380
+ [2, 3],
381
+ [1, 4],
382
+ [4, 3],
383
+ [3, 5]
384
+ ]);
385
+ expect(graph.sortStable([5, 4, 3, 2, 1])).toStrictEqual([5, 3, 4, 2, 1]);
386
+ expect(graph.sortStable([1, 2, 4, 3, 5])).toStrictEqual([1, 2, 4, 3, 5]);
387
+ expect(graph.sortStable([4, 3, 5])).toStrictEqual([4, 3, 5]);
388
+ expect(graph.sortStable([2, 3])).toStrictEqual([2, 3]);
389
+ expect(graph.sortStable([3])).toStrictEqual([3]);
390
+ expect(graph.sortStable([])).toStrictEqual([]);
391
+ });
392
+ });