|
1 |
| -import { buildAdjacencyMap, buildCompressedAdjacencyMap } from './dfs'; |
| 1 | +import type { IGraphItem } from './dfs'; |
| 2 | +import { |
| 3 | + buildAdjacencyMap, |
| 4 | + buildCompressedAdjacencyMap, |
| 5 | + hasCycle, |
| 6 | + pruneGraph, |
| 7 | + topoOrderWithDepends, |
| 8 | + topoOrderWithStart, |
| 9 | + topologicalSort, |
| 10 | +} from './dfs'; |
2 | 11 |
|
3 | 12 | describe('Graph Processing Functions', () => {
|
4 | 13 | describe('buildAdjacencyMap', () => {
|
@@ -82,4 +91,188 @@ describe('Graph Processing Functions', () => {
|
82 | 91 | expect(buildCompressedAdjacencyMap(graph, linkIdSet)).toEqual(expected);
|
83 | 92 | });
|
84 | 93 | });
|
| 94 | + |
| 95 | + describe('topologicalSort', () => { |
| 96 | + it('should perform a basic topological sort', () => { |
| 97 | + const graph: IGraphItem[] = [ |
| 98 | + { fromFieldId: 'a', toFieldId: 'b' }, |
| 99 | + { fromFieldId: 'b', toFieldId: 'c' }, |
| 100 | + ]; |
| 101 | + expect(topologicalSort(graph)).toEqual(['a', 'b', 'c']); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should perform a branched topological sort', () => { |
| 105 | + const graph: IGraphItem[] = [ |
| 106 | + { fromFieldId: 'a', toFieldId: 'b' }, |
| 107 | + { fromFieldId: 'a', toFieldId: 'c' }, |
| 108 | + { fromFieldId: 'b', toFieldId: 'c' }, |
| 109 | + { fromFieldId: 'b', toFieldId: 'd' }, |
| 110 | + ]; |
| 111 | + expect(topologicalSort(graph)).toEqual(['a', 'b', 'd', 'c']); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should handle an empty graph', () => { |
| 115 | + const graph: IGraphItem[] = []; |
| 116 | + expect(topologicalSort(graph)).toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should handle a graph with a single circular node', () => { |
| 120 | + const graph: IGraphItem[] = [{ fromFieldId: 'a', toFieldId: 'a' }]; |
| 121 | + expect(() => topologicalSort(graph)).toThrowError(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should handle graphs with circular dependencies', () => { |
| 125 | + const graph: IGraphItem[] = [ |
| 126 | + { fromFieldId: 'a', toFieldId: 'b' }, |
| 127 | + { fromFieldId: 'b', toFieldId: 'a' }, |
| 128 | + ]; |
| 129 | + expect(() => topologicalSort(graph)).toThrowError(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('topoOrderWithDepends', () => { |
| 134 | + it('should return an empty array for an empty graph', () => { |
| 135 | + const result = topoOrderWithDepends('anyNodeId', []); |
| 136 | + expect(result).toEqual([ |
| 137 | + { |
| 138 | + id: 'anyNodeId', |
| 139 | + dependencies: [], |
| 140 | + }, |
| 141 | + ]); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle circular single node graph correctly', () => { |
| 145 | + const graph: IGraphItem[] = [{ fromFieldId: '1', toFieldId: '1' }]; |
| 146 | + expect(() => topoOrderWithDepends('1', graph)).toThrowError(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should handle circular node graph correctly', () => { |
| 150 | + const graph: IGraphItem[] = [ |
| 151 | + { fromFieldId: '1', toFieldId: '2' }, |
| 152 | + { fromFieldId: '2', toFieldId: '1' }, |
| 153 | + ]; |
| 154 | + expect(() => topoOrderWithDepends('1', graph)).toThrowError(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should return correct order for a normal DAG', () => { |
| 158 | + const graph: IGraphItem[] = [ |
| 159 | + { fromFieldId: '1', toFieldId: '2' }, |
| 160 | + { fromFieldId: '2', toFieldId: '3' }, |
| 161 | + ]; |
| 162 | + const result = topoOrderWithDepends('1', graph); |
| 163 | + expect(result).toEqual([ |
| 164 | + { id: '1', dependencies: [] }, |
| 165 | + { id: '2', dependencies: ['1'] }, |
| 166 | + { id: '3', dependencies: ['2'] }, |
| 167 | + ]); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should return correct order for a complex DAG', () => { |
| 171 | + const graph: IGraphItem[] = [ |
| 172 | + { fromFieldId: '1', toFieldId: '2' }, |
| 173 | + { fromFieldId: '2', toFieldId: '3' }, |
| 174 | + { fromFieldId: '1', toFieldId: '3' }, |
| 175 | + { fromFieldId: '3', toFieldId: '4' }, |
| 176 | + ]; |
| 177 | + const result = topoOrderWithDepends('1', graph); |
| 178 | + expect(result).toEqual([ |
| 179 | + { id: '1', dependencies: [] }, |
| 180 | + { id: '2', dependencies: ['1'] }, |
| 181 | + { id: '3', dependencies: ['2', '1'] }, |
| 182 | + { id: '4', dependencies: ['3'] }, |
| 183 | + ]); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe('hasCycle', () => { |
| 188 | + it('should return false for an empty graph', () => { |
| 189 | + expect(hasCycle([])).toBe(false); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should return true for a single node graph link to self', () => { |
| 193 | + const graph = [{ fromFieldId: '1', toFieldId: '1' }]; |
| 194 | + expect(hasCycle(graph)).toBe(true); |
| 195 | + }); |
| 196 | + |
| 197 | + it('should return false for a normal DAG without cycles', () => { |
| 198 | + const graph = [ |
| 199 | + { fromFieldId: '1', toFieldId: '2' }, |
| 200 | + { fromFieldId: '2', toFieldId: '3' }, |
| 201 | + ]; |
| 202 | + expect(hasCycle(graph)).toBe(false); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should return true for a graph with a cycle', () => { |
| 206 | + const graph = [ |
| 207 | + { fromFieldId: '1', toFieldId: '2' }, |
| 208 | + { fromFieldId: '2', toFieldId: '3' }, |
| 209 | + { fromFieldId: '3', toFieldId: '1' }, // creates a cycle |
| 210 | + ]; |
| 211 | + expect(hasCycle(graph)).toBe(true); |
| 212 | + }); |
| 213 | + }); |
| 214 | + |
| 215 | + describe('topoOrderWithStart', () => { |
| 216 | + it('should return correct order for a normal DAG', () => { |
| 217 | + const graph: IGraphItem[] = [ |
| 218 | + { fromFieldId: '1', toFieldId: '2' }, |
| 219 | + { fromFieldId: '2', toFieldId: '3' }, |
| 220 | + ]; |
| 221 | + const result = topoOrderWithStart('1', graph); |
| 222 | + expect(result).toEqual(['1', '2', '3']); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should return correct order for a complex DAG', () => { |
| 226 | + const graph: IGraphItem[] = [ |
| 227 | + { fromFieldId: '1', toFieldId: '2' }, |
| 228 | + { fromFieldId: '2', toFieldId: '3' }, |
| 229 | + { fromFieldId: '1', toFieldId: '3' }, |
| 230 | + { fromFieldId: '3', toFieldId: '4' }, |
| 231 | + ]; |
| 232 | + const result = topoOrderWithStart('1', graph); |
| 233 | + expect(result).toEqual(['1', '2', '3', '4']); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
| 237 | + describe('pruneGraph', () => { |
| 238 | + test('returns an empty array for an empty graph', () => { |
| 239 | + expect(pruneGraph('A', [])).toEqual([]); |
| 240 | + }); |
| 241 | + |
| 242 | + test('returns correct graph for a single-node graph', () => { |
| 243 | + const graph: IGraphItem[] = [{ fromFieldId: 'A', toFieldId: 'B' }]; |
| 244 | + expect(pruneGraph('A', graph)).toEqual(graph); |
| 245 | + }); |
| 246 | + |
| 247 | + test('returns correct graph for a tow-node graph', () => { |
| 248 | + const graph: IGraphItem[] = [ |
| 249 | + { fromFieldId: 'A', toFieldId: 'C' }, |
| 250 | + { fromFieldId: 'B', toFieldId: 'C' }, |
| 251 | + ]; |
| 252 | + expect(pruneGraph('C', graph)).toEqual(graph); |
| 253 | + }); |
| 254 | + |
| 255 | + test('returns correct graph for a multi-node graph', () => { |
| 256 | + const graph: IGraphItem[] = [ |
| 257 | + { fromFieldId: 'A', toFieldId: 'B' }, |
| 258 | + { fromFieldId: 'B', toFieldId: 'C' }, |
| 259 | + { fromFieldId: 'C', toFieldId: 'D' }, |
| 260 | + { fromFieldId: 'E', toFieldId: 'F' }, |
| 261 | + ]; |
| 262 | + const expectedResult: IGraphItem[] = [ |
| 263 | + { fromFieldId: 'A', toFieldId: 'B' }, |
| 264 | + { fromFieldId: 'B', toFieldId: 'C' }, |
| 265 | + { fromFieldId: 'C', toFieldId: 'D' }, |
| 266 | + ]; |
| 267 | + expect(pruneGraph('A', graph)).toEqual(expectedResult); |
| 268 | + }); |
| 269 | + |
| 270 | + test('returns an empty array for a graph with unrelated node', () => { |
| 271 | + const graph: IGraphItem[] = [ |
| 272 | + { fromFieldId: 'B', toFieldId: 'C' }, |
| 273 | + { fromFieldId: 'C', toFieldId: 'D' }, |
| 274 | + ]; |
| 275 | + expect(pruneGraph('A', graph)).toEqual([]); |
| 276 | + }); |
| 277 | + }); |
85 | 278 | });
|
0 commit comments