| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- import { createElement } from "react";
- import { renderToStaticMarkup } from "react-dom/server";
- import { describe, expect, it, vi } from "vitest";
- import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
- import { GitGraphPane } from "../src/git-graph/GitGraphPane.js";
- import type { GitGraphCommit } from "../src/git-graph/layout.js";
- import { layoutGitGraph } from "../src/git-graph/layout.js";
- function commit(hash: string, parents: string[] = []): GitGraphCommit {
- return {
- hash,
- parents,
- refs: [],
- subject: `Commit ${hash}`,
- authorName: "ZCode",
- authoredAtMs: 1_766_955_000_000,
- };
- }
- describe("layoutGitGraph", () => {
- it("keeps linear history in a single lane", () => {
- const layout = layoutGitGraph([commit("c3", ["c2"]), commit("c2", ["c1"]), commit("c1")]);
- expect(layout.laneCount).toBe(1);
- expect(layout.rows.map((row) => row.laneIndex)).toEqual([0, 0, 0]);
- expect(layout.edges).toHaveLength(2);
- expect(layout.edges.every((edge) => !edge.truncated)).toBe(true);
- });
- it("allocates a side lane for branch merge parents", () => {
- const layout = layoutGitGraph([
- commit("m", ["left", "right"]),
- commit("left", ["base"]),
- commit("right", ["base"]),
- commit("base", ["root"]),
- commit("root"),
- ]);
- expect(layout.laneCount).toBe(2);
- expect(layout.rows.map((row) => [row.commit.hash, row.laneIndex])).toEqual([
- ["m", 0],
- ["left", 0],
- ["right", 1],
- ["base", 0],
- ["root", 0],
- ]);
- expect(layout.edges.find((edge) => edge.fromHash === "m" && edge.toHash === "right")
- ?.toLaneIndex).toBe(1);
- });
- it("reuses active parent lanes across multiple side branches", () => {
- const layout = layoutGitGraph([
- commit("m", ["left", "right", "docs"]),
- commit("left", ["base"]),
- commit("right", ["base"]),
- commit("docs", ["base"]),
- commit("base"),
- ]);
- expect(layout.laneCount).toBe(3);
- expect(layout.rows.map((row) => [row.commit.hash, row.laneIndex])).toEqual([
- ["m", 0],
- ["left", 0],
- ["right", 1],
- ["docs", 2],
- ["base", 0],
- ]);
- });
- it("keeps a shared parent on the lower first-parent lane after side branches", () => {
- const layout = layoutGitGraph([
- commit("merge", ["main1", "feature2"]),
- commit("feature2", ["feature1"]),
- commit("feature1", ["root"]),
- commit("main1", ["root"]),
- commit("root"),
- ]);
- expect(layout.rows.map((row) => [row.commit.hash, row.laneIndex])).toEqual([
- ["merge", 0],
- ["feature2", 1],
- ["feature1", 1],
- ["main1", 0],
- ["root", 0],
- ]);
- expect(layout.edges.find((edge) => edge.fromHash === "feature1" && edge.toHash === "root")
- ?.toLaneIndex).toBe(0);
- expect(layout.edges.find((edge) => edge.fromHash === "main1" && edge.toHash === "root")
- ?.toLaneIndex).toBe(0);
- expect(
- layout.laneSegments.some(
- (segment) => segment.hash === "root" && segment.laneIndex === 0,
- ),
- ).toBe(true);
- expect(
- layout.paths.some(
- (path) =>
- path.relatedHashes.includes("feature1") &&
- path.relatedHashes.includes("root") &&
- path.path.includes("C"),
- ),
- ).toBe(true);
- });
- it("marks parent edges as truncated when the parent is outside the window", () => {
- const layout = layoutGitGraph([commit("head", ["outside", "outside-side"])]);
- expect(layout.edges).toHaveLength(2);
- expect(layout.edges[0]?.truncated).toBe(true);
- expect(layout.edges[0]?.toHash).toBe("outside");
- expect(layout.edges[1]?.truncated).toBe(true);
- expect(layout.edges[1]?.toLaneIndex).toBe(1);
- expect(layout.laneSegments).toHaveLength(0);
- expect(layout.paths).toHaveLength(0);
- expect(layout.laneCount).toBe(1);
- });
- it("keeps branch paths when a paged-in parent appears later", () => {
- const layout = layoutGitGraph([
- commit("merge", ["left", "right"]),
- commit("left", ["base"]),
- commit("right", ["base"]),
- commit("base"),
- ]);
- expect(
- layout.laneSegments.some(
- (segment) => segment.hash === "right" && segment.laneIndex === 1,
- ),
- ).toBe(true);
- expect(
- layout.paths.some(
- (path) =>
- path.relatedHashes.includes("right") &&
- path.relatedHashes.includes("base") &&
- path.path.includes("C"),
- ),
- ).toBe(true);
- });
- it("keeps refs on the row when multiple names point to one commit", () => {
- const layout = layoutGitGraph([
- {
- ...commit("head"),
- refs: [
- { name: "HEAD", kind: "head" },
- { name: "main", kind: "branch" },
- { name: "v1.0.0", kind: "tag" },
- ],
- },
- ]);
- expect(layout.rows[0]?.commit.refs.map((ref) => ref.name)).toEqual([
- "HEAD",
- "main",
- "v1.0.0",
- ]);
- });
- });
- describe("GitGraphPane", () => {
- it("renders graph rows, refs, and selected state in static markup", () => {
- const commits: GitGraphCommit[] = [
- {
- ...commit("f4d91c7", ["c18e2bb", "9a51d0a"]),
- refs: [
- { name: "HEAD", kind: "head" },
- { name: "feature/git-graph", kind: "branch" },
- ],
- subject:
- "Render an intentionally long commit subject that should truncate inside the row",
- },
- commit("c18e2bb", ["base"]),
- commit("9a51d0a", ["base"]),
- commit("base"),
- ];
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "en-US" },
- createElement(GitGraphPane, {
- commits,
- hasMore: false,
- selectedCommitHash: "f4d91c7",
- onSelectCommit: vi.fn(),
- onRefresh: vi.fn(),
- }),
- ),
- );
- expect(html).toContain("Git Graph");
- expect(html).toContain("Refresh Git Graph");
- expect(html).toContain("feature/git-graph");
- expect(html).toContain("Render an intentionally long commit subject");
- expect(html).toContain("bg-selected");
- expect(html).toContain("<svg");
- });
- it("keeps the graph column wide enough for the Graph header", () => {
- const html = renderToStaticMarkup(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "en-US" },
- createElement(GitGraphPane, {
- commits: [commit("head")],
- hasMore: false,
- selectedCommitHash: "head",
- onSelectCommit: vi.fn(),
- }),
- ),
- );
- expect(html).toContain("grid-template-columns:minmax(56px, 44px) minmax(0, 1fr)");
- expect(html).toContain('width="56"');
- });
- });
|