gitGraphLayout.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import { GitGraphPane } from "../src/git-graph/GitGraphPane.js";
  6. import type { GitGraphCommit } from "../src/git-graph/layout.js";
  7. import { layoutGitGraph } from "../src/git-graph/layout.js";
  8. function commit(hash: string, parents: string[] = []): GitGraphCommit {
  9. return {
  10. hash,
  11. parents,
  12. refs: [],
  13. subject: `Commit ${hash}`,
  14. authorName: "ZCode",
  15. authoredAtMs: 1_766_955_000_000,
  16. };
  17. }
  18. describe("layoutGitGraph", () => {
  19. it("keeps linear history in a single lane", () => {
  20. const layout = layoutGitGraph([commit("c3", ["c2"]), commit("c2", ["c1"]), commit("c1")]);
  21. expect(layout.laneCount).toBe(1);
  22. expect(layout.rows.map((row) => row.laneIndex)).toEqual([0, 0, 0]);
  23. expect(layout.edges).toHaveLength(2);
  24. expect(layout.edges.every((edge) => !edge.truncated)).toBe(true);
  25. });
  26. it("allocates a side lane for branch merge parents", () => {
  27. const layout = layoutGitGraph([
  28. commit("m", ["left", "right"]),
  29. commit("left", ["base"]),
  30. commit("right", ["base"]),
  31. commit("base", ["root"]),
  32. commit("root"),
  33. ]);
  34. expect(layout.laneCount).toBe(2);
  35. expect(layout.rows.map((row) => [row.commit.hash, row.laneIndex])).toEqual([
  36. ["m", 0],
  37. ["left", 0],
  38. ["right", 1],
  39. ["base", 0],
  40. ["root", 0],
  41. ]);
  42. expect(layout.edges.find((edge) => edge.fromHash === "m" && edge.toHash === "right")
  43. ?.toLaneIndex).toBe(1);
  44. });
  45. it("reuses active parent lanes across multiple side branches", () => {
  46. const layout = layoutGitGraph([
  47. commit("m", ["left", "right", "docs"]),
  48. commit("left", ["base"]),
  49. commit("right", ["base"]),
  50. commit("docs", ["base"]),
  51. commit("base"),
  52. ]);
  53. expect(layout.laneCount).toBe(3);
  54. expect(layout.rows.map((row) => [row.commit.hash, row.laneIndex])).toEqual([
  55. ["m", 0],
  56. ["left", 0],
  57. ["right", 1],
  58. ["docs", 2],
  59. ["base", 0],
  60. ]);
  61. });
  62. it("keeps a shared parent on the lower first-parent lane after side branches", () => {
  63. const layout = layoutGitGraph([
  64. commit("merge", ["main1", "feature2"]),
  65. commit("feature2", ["feature1"]),
  66. commit("feature1", ["root"]),
  67. commit("main1", ["root"]),
  68. commit("root"),
  69. ]);
  70. expect(layout.rows.map((row) => [row.commit.hash, row.laneIndex])).toEqual([
  71. ["merge", 0],
  72. ["feature2", 1],
  73. ["feature1", 1],
  74. ["main1", 0],
  75. ["root", 0],
  76. ]);
  77. expect(layout.edges.find((edge) => edge.fromHash === "feature1" && edge.toHash === "root")
  78. ?.toLaneIndex).toBe(0);
  79. expect(layout.edges.find((edge) => edge.fromHash === "main1" && edge.toHash === "root")
  80. ?.toLaneIndex).toBe(0);
  81. expect(
  82. layout.laneSegments.some(
  83. (segment) => segment.hash === "root" && segment.laneIndex === 0,
  84. ),
  85. ).toBe(true);
  86. expect(
  87. layout.paths.some(
  88. (path) =>
  89. path.relatedHashes.includes("feature1") &&
  90. path.relatedHashes.includes("root") &&
  91. path.path.includes("C"),
  92. ),
  93. ).toBe(true);
  94. });
  95. it("marks parent edges as truncated when the parent is outside the window", () => {
  96. const layout = layoutGitGraph([commit("head", ["outside", "outside-side"])]);
  97. expect(layout.edges).toHaveLength(2);
  98. expect(layout.edges[0]?.truncated).toBe(true);
  99. expect(layout.edges[0]?.toHash).toBe("outside");
  100. expect(layout.edges[1]?.truncated).toBe(true);
  101. expect(layout.edges[1]?.toLaneIndex).toBe(1);
  102. expect(layout.laneSegments).toHaveLength(0);
  103. expect(layout.paths).toHaveLength(0);
  104. expect(layout.laneCount).toBe(1);
  105. });
  106. it("keeps branch paths when a paged-in parent appears later", () => {
  107. const layout = layoutGitGraph([
  108. commit("merge", ["left", "right"]),
  109. commit("left", ["base"]),
  110. commit("right", ["base"]),
  111. commit("base"),
  112. ]);
  113. expect(
  114. layout.laneSegments.some(
  115. (segment) => segment.hash === "right" && segment.laneIndex === 1,
  116. ),
  117. ).toBe(true);
  118. expect(
  119. layout.paths.some(
  120. (path) =>
  121. path.relatedHashes.includes("right") &&
  122. path.relatedHashes.includes("base") &&
  123. path.path.includes("C"),
  124. ),
  125. ).toBe(true);
  126. });
  127. it("keeps refs on the row when multiple names point to one commit", () => {
  128. const layout = layoutGitGraph([
  129. {
  130. ...commit("head"),
  131. refs: [
  132. { name: "HEAD", kind: "head" },
  133. { name: "main", kind: "branch" },
  134. { name: "v1.0.0", kind: "tag" },
  135. ],
  136. },
  137. ]);
  138. expect(layout.rows[0]?.commit.refs.map((ref) => ref.name)).toEqual([
  139. "HEAD",
  140. "main",
  141. "v1.0.0",
  142. ]);
  143. });
  144. });
  145. describe("GitGraphPane", () => {
  146. it("renders graph rows, refs, and selected state in static markup", () => {
  147. const commits: GitGraphCommit[] = [
  148. {
  149. ...commit("f4d91c7", ["c18e2bb", "9a51d0a"]),
  150. refs: [
  151. { name: "HEAD", kind: "head" },
  152. { name: "feature/git-graph", kind: "branch" },
  153. ],
  154. subject:
  155. "Render an intentionally long commit subject that should truncate inside the row",
  156. },
  157. commit("c18e2bb", ["base"]),
  158. commit("9a51d0a", ["base"]),
  159. commit("base"),
  160. ];
  161. const html = renderToStaticMarkup(
  162. createElement(
  163. ZCodeIntlProvider,
  164. { initialLocale: "en-US" },
  165. createElement(GitGraphPane, {
  166. commits,
  167. hasMore: false,
  168. selectedCommitHash: "f4d91c7",
  169. onSelectCommit: vi.fn(),
  170. onRefresh: vi.fn(),
  171. }),
  172. ),
  173. );
  174. expect(html).toContain("Git Graph");
  175. expect(html).toContain("Refresh Git Graph");
  176. expect(html).toContain("feature/git-graph");
  177. expect(html).toContain("Render an intentionally long commit subject");
  178. expect(html).toContain("bg-selected");
  179. expect(html).toContain("<svg");
  180. });
  181. it("keeps the graph column wide enough for the Graph header", () => {
  182. const html = renderToStaticMarkup(
  183. createElement(
  184. ZCodeIntlProvider,
  185. { initialLocale: "en-US" },
  186. createElement(GitGraphPane, {
  187. commits: [commit("head")],
  188. hasMore: false,
  189. selectedCommitHash: "head",
  190. onSelectCommit: vi.fn(),
  191. }),
  192. ),
  193. );
  194. expect(html).toContain("grid-template-columns:minmax(56px, 44px) minmax(0, 1fr)");
  195. expect(html).toContain('width="56"');
  196. });
  197. });