|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { MermaidSyntaxError } from "../diagnostics.js" |
| 3 | +import { renderGitGraphDiagram } from "./diagram.js" |
| 4 | +import { drawGitGraphDiagramGrid } from "./drawing.js" |
| 5 | +import { isMermaidGitGraphDiagram, parseMermaidGitGraphDiagram } from "./parser.js" |
| 6 | +import { renderGitGraphGridText } from "./render-grid.js" |
| 7 | +import { resolveGitGraphStyleColors } from "./style.js" |
| 8 | + |
| 9 | +describe("GitGraphDiagram", () => { |
| 10 | + test("detects and parses commits, branches, checkout, tags, types, and merges", () => { |
| 11 | + const diagram = parseMermaidGitGraphDiagram(`gitGraph TB: |
| 12 | + commit id: "init" |
| 13 | + branch feature order: 1 |
| 14 | + commit id: "api" msg: "Add API" tag: "ready" |
| 15 | + checkout main |
| 16 | + commit id: "docs" type: HIGHLIGHT |
| 17 | + merge feature id: "merge-feature"`) |
| 18 | + |
| 19 | + expect(diagram).toEqual({ |
| 20 | + direction: "TB", |
| 21 | + branches: [ |
| 22 | + { name: "main", order: 0, head: "merge-feature" }, |
| 23 | + { name: "feature", order: 1, head: "api" }, |
| 24 | + ], |
| 25 | + commits: [ |
| 26 | + { id: "init", tags: [], type: "NORMAL", branch: "main", parents: [] }, |
| 27 | + { id: "api", message: "Add API", tags: ["ready"], type: "NORMAL", branch: "feature", parents: ["init"] }, |
| 28 | + { id: "docs", tags: [], type: "HIGHLIGHT", branch: "main", parents: ["init"] }, |
| 29 | + { |
| 30 | + id: "merge-feature", |
| 31 | + tags: [], |
| 32 | + type: "NORMAL", |
| 33 | + branch: "main", |
| 34 | + parents: ["docs", "api"], |
| 35 | + }, |
| 36 | + ], |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + test("renders branch and merge transitions beside compact labels", () => { |
| 41 | + const source = `gitGraph |
| 42 | + commit id: "baseline" |
| 43 | + branch refactor |
| 44 | + commit id: "extract-seam" msg: "Extract seam" |
| 45 | + commit id: "add-tests" tag: "ready" |
| 46 | + checkout main |
| 47 | + commit id: "unrelated-fix" |
| 48 | + merge refactor id: "land-refactor" tag: "v2"` |
| 49 | + |
| 50 | + expect(renderGitGraphDiagram(source)).toBe(`● baseline |
| 51 | +├─╮ |
| 52 | +│ ● Extract seam |
| 53 | +│ ● add-tests [refactor] [ready] |
| 54 | +● │ unrelated-fix |
| 55 | +◎─╯ land-refactor [main] [v2]`) |
| 56 | + }) |
| 57 | + |
| 58 | + test("uses deterministic generated ids", () => { |
| 59 | + expect(parseMermaidGitGraphDiagram("gitGraph\n commit\n commit").commits.map((commit) => commit.id)).toEqual([ |
| 60 | + "commit-1", |
| 61 | + "commit-2", |
| 62 | + ]) |
| 63 | + }) |
| 64 | + |
| 65 | + test("supports shorthand messages and preserves branch heads without direct commits", () => { |
| 66 | + const diagram = parseMermaidGitGraphDiagram(`gitGraph |
| 67 | + commit "Initial release" |
| 68 | + branch feature |
| 69 | + checkout main |
| 70 | + commit id: next`) |
| 71 | + |
| 72 | + expect(diagram.commits[0]?.message).toBe("Initial release") |
| 73 | + expect(diagram.branches).toEqual([ |
| 74 | + { name: "main", order: 0, head: "next" }, |
| 75 | + { name: "feature", head: "commit-1" }, |
| 76 | + ]) |
| 77 | + expect( |
| 78 | + renderGitGraphDiagram(`gitGraph |
| 79 | + commit id: base |
| 80 | + branch feature |
| 81 | + checkout main |
| 82 | + commit id: next`), |
| 83 | + ).toContain("base [feature]") |
| 84 | + }) |
| 85 | + |
| 86 | + test("places unordered branches before explicitly ordered branches", () => { |
| 87 | + const diagram = parseMermaidGitGraphDiagram(`gitGraph |
| 88 | + commit id: base |
| 89 | + branch later order: 2 |
| 90 | + checkout main |
| 91 | + branch ordinary |
| 92 | + checkout main |
| 93 | + branch earlier order: 1`) |
| 94 | + |
| 95 | + expect(diagram.branches.map((branch) => branch.name)).toEqual(["main", "ordinary", "earlier", "later"]) |
| 96 | + }) |
| 97 | + |
| 98 | + test("keeps comment markers inside quoted labels", () => { |
| 99 | + expect(parseMermaidGitGraphDiagram('gitGraph\n commit id: "release%%candidate" %% comment').commits[0]?.id).toBe( |
| 100 | + "release%%candidate", |
| 101 | + ) |
| 102 | + }) |
| 103 | + |
| 104 | + test("uses rounded routing for wide lane transitions", () => { |
| 105 | + expect( |
| 106 | + renderGitGraphDiagram(`gitGraph |
| 107 | + commit id: base |
| 108 | + branch one |
| 109 | + branch two |
| 110 | + commit id: work`), |
| 111 | + ).toBe(`● base [main] [one] |
| 112 | +├───╮ |
| 113 | + ● work [two]`) |
| 114 | + }) |
| 115 | + |
| 116 | + test("preserves direction semantics while rendering vertically", () => { |
| 117 | + const source = "gitGraph BT:\n commit id: one" |
| 118 | + const diagram = parseMermaidGitGraphDiagram(source) |
| 119 | + expect(diagram.direction).toBe("BT") |
| 120 | + expect(renderGitGraphGridText(drawGitGraphDiagramGrid(diagram, { direction: "LR" }))).toBe( |
| 121 | + renderGitGraphDiagram(source), |
| 122 | + ) |
| 123 | + }) |
| 124 | + |
| 125 | + test("reports semantic failures with source diagnostics", () => { |
| 126 | + expect(() => parseMermaidGitGraphDiagram("gitGraph\n checkout missing")).toThrow( |
| 127 | + new MermaidSyntaxError("gitGraph", 2, "checkout missing", 'Unknown branch "missing"'), |
| 128 | + ) |
| 129 | + expect(() => parseMermaidGitGraphDiagram("gitGraph\n cherry-pick id: one")).toThrow( |
| 130 | + new MermaidSyntaxError("gitGraph", 2, "cherry-pick id: one", "Cherry-pick is not supported"), |
| 131 | + ) |
| 132 | + expect(() => parseMermaidGitGraphDiagram("gitGraph\n commit id: same\n commit id: same")).toThrow( |
| 133 | + 'Duplicate commit id "same"', |
| 134 | + ) |
| 135 | + expect(() => parseMermaidGitGraphDiagram("gitGraph\n branch feature\n checkout main\n branch feature")).toThrow( |
| 136 | + 'Duplicate branch "feature"', |
| 137 | + ) |
| 138 | + expect(() => |
| 139 | + parseMermaidGitGraphDiagram("gitGraph\n branch feature\n commit id: work\n checkout main\n merge feature"), |
| 140 | + ).toThrow('Branch "main" has no commits') |
| 141 | + }) |
| 142 | + |
| 143 | + test("draws semantic styles for rails, commit types, merges, and labels", () => { |
| 144 | + const grid = drawGitGraphDiagramGrid( |
| 145 | + parseMermaidGitGraphDiagram(`gitGraph |
| 146 | + commit id: base |
| 147 | + branch feature |
| 148 | + commit id: work type: REVERSE |
| 149 | + checkout main |
| 150 | + commit id: checkpoint type: HIGHLIGHT |
| 151 | + merge feature id: done`), |
| 152 | + ) |
| 153 | + const styles = new Set(grid.rows.flatMap((row) => row.map((cell) => cell.style).filter(Boolean))) |
| 154 | + |
| 155 | + expect(styles).toEqual(new Set(["branch0", "branch1", "commit", "reverse", "highlight", "merge", "label"])) |
| 156 | + expect(Object.keys(resolveGitGraphStyleColors()).sort()).toEqual( |
| 157 | + [ |
| 158 | + "branch0", |
| 159 | + "branch1", |
| 160 | + "branch2", |
| 161 | + "branch3", |
| 162 | + "branch4", |
| 163 | + "branch5", |
| 164 | + "branch6", |
| 165 | + "branch7", |
| 166 | + "commit", |
| 167 | + "highlight", |
| 168 | + "label", |
| 169 | + "merge", |
| 170 | + "reverse", |
| 171 | + ].sort(), |
| 172 | + ) |
| 173 | + }) |
| 174 | + |
| 175 | + test("recognizes only GitGraph headers", () => { |
| 176 | + expect(isMermaidGitGraphDiagram("%% comment\ngitGraph LR:\n commit")).toBe(true) |
| 177 | + expect(isMermaidGitGraphDiagram("graph LR\n A --> B")).toBe(false) |
| 178 | + expect(() => parseMermaidGitGraphDiagram("commit id: missing-header")).toThrow("GitGraph header is required") |
| 179 | + expect(() => parseMermaidGitGraphDiagram("gitGraph\n commit\n gitGraph")).toThrow( |
| 180 | + "GitGraph header can only appear once", |
| 181 | + ) |
| 182 | + }) |
| 183 | +}) |
0 commit comments