|
1 | | -import { describe, expect, it } from 'vitest'; |
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
2 | 2 | import { Graph } from '../../Graph.js'; |
3 | 3 | import { serializeGraph } from '../../utils/serializeGraph.js'; |
4 | 4 | import { shortestPath } from './shortestPath.js'; |
5 | 5 | import { shortestPaths } from './shortestPaths.js'; |
| 6 | +import { addWeightFunction } from './getPath.js'; |
| 7 | +import { NextWeightFnParams } from '../../types.js'; |
6 | 8 |
|
7 | 9 | describe("Dijkstra's Shortest Path Algorithm", function () { |
8 | 10 | it('Should compute shortest path on a single edge.', function () { |
@@ -104,3 +106,75 @@ describe("Dijkstra's Shortest Path Algorithm", function () { |
104 | 106 | expect(postSerializedGraph.links).toContainEqual({ source: 'f', target: 'c' }); |
105 | 107 | }); |
106 | 108 | }); |
| 109 | + |
| 110 | +describe('addWeightFunction', () => { |
| 111 | + it('should return edgeWeight if currentPathWeight is undefined', () => { |
| 112 | + const params = { edgeWeight: 5, currentPathWeight: undefined, hop: 1 }; |
| 113 | + expect(addWeightFunction(params)).toBe(5); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return the sum of edgeWeight and currentPathWeight', () => { |
| 117 | + const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1 }; |
| 118 | + expect(addWeightFunction(params)).toBe(15); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('shortestPath with custom weight functions', () => { |
| 123 | + it('should compute shortest path with default weight function (sum of weights)', () => { |
| 124 | + const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2); |
| 125 | + expect(shortestPath(graph, 'a', 'c')).toEqual({ |
| 126 | + nodes: ['a', 'b', 'c'], |
| 127 | + weight: 3, |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should compute shortest path with a custom weight function', () => { |
| 132 | + const customWeightFn = ({ edgeWeight, currentPathWeight, hop }: NextWeightFnParams) => { |
| 133 | + if (currentPathWeight === undefined) { |
| 134 | + return edgeWeight; |
| 135 | + } |
| 136 | + return currentPathWeight + edgeWeight ** hop; |
| 137 | + }; |
| 138 | + |
| 139 | + const graph = new Graph().addEdge('a', 'b', 2).addEdge('b', 'c', 3); |
| 140 | + expect(shortestPath(graph, 'a', 'c', customWeightFn)).toEqual({ |
| 141 | + nodes: ['a', 'b', 'c'], |
| 142 | + weight: 7, |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should pass correct parameters to custom weight function for a path with 3 nodes', () => { |
| 147 | + const customWeightFn = vi.fn(({ edgeWeight, currentPathWeight, hop }: NextWeightFnParams) => { |
| 148 | + if (currentPathWeight === undefined) { |
| 149 | + return edgeWeight; |
| 150 | + } |
| 151 | + return currentPathWeight + edgeWeight ** hop; |
| 152 | + }); |
| 153 | + |
| 154 | + const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2); |
| 155 | + shortestPath(graph, 'a', 'c', customWeightFn); |
| 156 | + |
| 157 | + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1 }); |
| 158 | + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2 }); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should compute shortest path with a custom weight function in a graph with multiple paths', () => { |
| 162 | + const customWeightFn = ({ edgeWeight, currentPathWeight }: NextWeightFnParams) => { |
| 163 | + if (currentPathWeight === undefined) { |
| 164 | + return edgeWeight; |
| 165 | + } |
| 166 | + return edgeWeight + currentPathWeight; |
| 167 | + }; |
| 168 | + |
| 169 | + const graph = new Graph() |
| 170 | + .addEdge('a', 'b', 1) |
| 171 | + .addEdge('b', 'c', 2) |
| 172 | + .addEdge('a', 'd', 1) |
| 173 | + .addEdge('d', 'c', 1); |
| 174 | + |
| 175 | + expect(shortestPath(graph, 'a', 'c', customWeightFn)).toEqual({ |
| 176 | + nodes: ['a', 'd', 'c'], |
| 177 | + weight: 2, |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments