gitGraphLayout.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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(
  43. layout.edges.find((edge) => edge.fromHash === "m" && edge.toHash === "right")?.toLaneIndex,
  44. ).toBe(1);
  45. });
  46. it("reuses active parent lanes across multiple side branches", () => {
  47. const layout = layoutGitGraph([
  48. commit("m", ["left", "right", "docs"]),
  49. commit("left", ["base"]),
  50. commit("right", ["base"]),
  51. commit("docs", ["base"]),
  52. commit("base"),
  53. ]);
  54. expect(layout.laneCount).toBe(3);
  55. expect(layout.rows.map((row) => [row.commit.hash, row.laneIndex])).toEqual([
  56. ["m", 0],
  57. ["left", 0],
  58. ["right", 1],
  59. ["docs", 2],
  60. ["base", 0],
  61. ]);
  62. });
  63. it("keeps a shared parent on the lower first-parent lane after side branches", () => {
  64. const layout = layoutGitGraph([
  65. commit("merge", ["main1", "feature2"]),
  66. commit("feature2", ["feature1"]),
  67. commit("feature1", ["root"]),
  68. commit("main1", ["root"]),
  69. commit("root"),
  70. ]);
  71. expect(layout.rows.map((row) => [row.commit.hash, row.laneIndex])).toEqual([
  72. ["merge", 0],
  73. ["feature2", 1],
  74. ["feature1", 1],
  75. ["main1", 0],
  76. ["root", 0],
  77. ]);
  78. expect(
  79. layout.edges.find((edge) => edge.fromHash === "feature1" && edge.toHash === "root")
  80. ?.toLaneIndex,
  81. ).toBe(0);
  82. expect(
  83. layout.edges.find((edge) => edge.fromHash === "main1" && edge.toHash === "root")?.toLaneIndex,
  84. ).toBe(0);
  85. expect(
  86. layout.laneSegments.some((segment) => segment.hash === "root" && segment.laneIndex === 0),
  87. ).toBe(true);
  88. expect(
  89. layout.paths.some(
  90. (path) =>
  91. path.relatedHashes.includes("feature1") &&
  92. path.relatedHashes.includes("root") &&
  93. path.path.includes("C"),
  94. ),
  95. ).toBe(true);
  96. });
  97. it("marks parent edges as truncated when the parent is outside the window", () => {
  98. const layout = layoutGitGraph([commit("head", ["outside", "outside-side"])]);
  99. expect(layout.edges).toHaveLength(2);
  100. expect(layout.edges[0]?.truncated).toBe(true);
  101. expect(layout.edges[0]?.toHash).toBe("outside");
  102. expect(layout.edges[1]?.truncated).toBe(true);
  103. expect(layout.edges[1]?.toLaneIndex).toBe(1);
  104. expect(layout.laneSegments).toHaveLength(0);
  105. expect(layout.paths).toHaveLength(0);
  106. expect(layout.laneCount).toBe(1);
  107. });
  108. it("keeps branch paths when a paged-in parent appears later", () => {
  109. const layout = layoutGitGraph([
  110. commit("merge", ["left", "right"]),
  111. commit("left", ["base"]),
  112. commit("right", ["base"]),
  113. commit("base"),
  114. ]);
  115. expect(
  116. layout.laneSegments.some((segment) => segment.hash === "right" && segment.laneIndex === 1),
  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. });
  128. describe("GitGraphPane", () => {
  129. it("renders graph rows, refs, and selected state in static markup", () => {
  130. const commits: GitGraphCommit[] = [
  131. {
  132. ...commit("f4d91c7", ["c18e2bb", "9a51d0a"]),
  133. refs: [
  134. { name: "HEAD", kind: "head" },
  135. { name: "feature/git-graph", kind: "branch" },
  136. ],
  137. subject: "Render an intentionally long commit subject that should truncate inside the row",
  138. },
  139. commit("c18e2bb", ["base"]),
  140. commit("9a51d0a", ["base"]),
  141. commit("base"),
  142. ];
  143. const html = renderToStaticMarkup(
  144. createElement(
  145. ZCodeIntlProvider,
  146. { initialLocale: "en-US" },
  147. createElement(GitGraphPane, {
  148. commits,
  149. hasMore: false,
  150. selectedCommitHash: "f4d91c7",
  151. onSelectCommit: vi.fn(),
  152. onRefresh: vi.fn(),
  153. }),
  154. ),
  155. );
  156. expect(html).toContain("Git Graph");
  157. expect(html).toContain("Refresh Git Graph");
  158. expect(html).toContain("feature/git-graph");
  159. expect(html).toContain("Render an intentionally long commit subject");
  160. expect(html).toContain("bg-selected");
  161. expect(html).toContain("<svg");
  162. });
  163. it("keeps the graph column wide enough for the Graph header", () => {
  164. const html = renderToStaticMarkup(
  165. createElement(
  166. ZCodeIntlProvider,
  167. { initialLocale: "en-US" },
  168. createElement(GitGraphPane, {
  169. commits: [commit("head")],
  170. hasMore: false,
  171. selectedCommitHash: "head",
  172. onSelectCommit: vi.fn(),
  173. }),
  174. ),
  175. );
  176. expect(html).toContain("grid-template-columns:minmax(56px, 44px) minmax(0, 1fr)");
  177. expect(html).toContain('width="56"');
  178. });
  179. });