Jacky Zhao
2024-02-23 be9b6b3a1e2038ea0a985ebd37fb18a661bddd04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import test, { describe } from "node:test"
import DepGraph from "./depgraph"
import assert from "node:assert"
 
describe("DepGraph", () => {
  test("getLeafNodes", () => {
    const graph = new DepGraph<string>()
    graph.addEdge("A", "B")
    graph.addEdge("B", "C")
    graph.addEdge("D", "C")
    assert.deepStrictEqual(graph.getLeafNodes("A"), new Set(["C"]))
    assert.deepStrictEqual(graph.getLeafNodes("B"), new Set(["C"]))
    assert.deepStrictEqual(graph.getLeafNodes("C"), new Set(["C"]))
    assert.deepStrictEqual(graph.getLeafNodes("D"), new Set(["C"]))
  })
 
  describe("getLeafNodeAncestors", () => {
    test("gets correct ancestors in a graph without cycles", () => {
      const graph = new DepGraph<string>()
      graph.addEdge("A", "B")
      graph.addEdge("B", "C")
      graph.addEdge("D", "B")
      assert.deepStrictEqual(graph.getLeafNodeAncestors("A"), new Set(["A", "B", "D"]))
      assert.deepStrictEqual(graph.getLeafNodeAncestors("B"), new Set(["A", "B", "D"]))
      assert.deepStrictEqual(graph.getLeafNodeAncestors("C"), new Set(["A", "B", "D"]))
      assert.deepStrictEqual(graph.getLeafNodeAncestors("D"), new Set(["A", "B", "D"]))
    })
 
    test("gets correct ancestors in a graph with cycles", () => {
      const graph = new DepGraph<string>()
      graph.addEdge("A", "B")
      graph.addEdge("B", "C")
      graph.addEdge("C", "A")
      graph.addEdge("C", "D")
      assert.deepStrictEqual(graph.getLeafNodeAncestors("A"), new Set(["A", "B", "C"]))
      assert.deepStrictEqual(graph.getLeafNodeAncestors("B"), new Set(["A", "B", "C"]))
      assert.deepStrictEqual(graph.getLeafNodeAncestors("C"), new Set(["A", "B", "C"]))
      assert.deepStrictEqual(graph.getLeafNodeAncestors("D"), new Set(["A", "B", "C"]))
    })
  })
 
  describe("updateIncomingEdgesForNode", () => {
    test("merges when node exists", () => {
      // A.md -> B.md -> B.html
      const graph = new DepGraph<string>()
      graph.addEdge("A.md", "B.md")
      graph.addEdge("B.md", "B.html")
 
      // B.md is edited so it removes the A.md transclusion
      // and adds C.md transclusion
      // C.md -> B.md
      const other = new DepGraph<string>()
      other.addEdge("C.md", "B.md")
      other.addEdge("B.md", "B.html")
 
      // A.md -> B.md removed, C.md -> B.md added
      // C.md -> B.md -> B.html
      graph.updateIncomingEdgesForNode(other, "B.md")
 
      const expected = {
        nodes: ["A.md", "B.md", "B.html", "C.md"],
        edges: [
          ["B.md", "B.html"],
          ["C.md", "B.md"],
        ],
      }
 
      assert.deepStrictEqual(graph.export(), expected)
    })
 
    test("adds node if it does not exist", () => {
      // A.md -> B.md
      const graph = new DepGraph<string>()
      graph.addEdge("A.md", "B.md")
 
      // Add a new file C.md that transcludes B.md
      // B.md -> C.md
      const other = new DepGraph<string>()
      other.addEdge("B.md", "C.md")
 
      // B.md -> C.md added
      // A.md -> B.md -> C.md
      graph.updateIncomingEdgesForNode(other, "C.md")
 
      const expected = {
        nodes: ["A.md", "B.md", "C.md"],
        edges: [
          ["A.md", "B.md"],
          ["B.md", "C.md"],
        ],
      }
 
      assert.deepStrictEqual(graph.export(), expected)
    })
  })
})