/** * @file Tests for the History commit-graph layout: a straight line, a branch * merged back, converging lanes and a parent beyond the loaded page. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect } from "vitest"; import { commitGraph } from "../commitGraph"; const c = (sha: string, ...parents: string[]) => ({ sha, parents }); describe("commitGraph", () => { it("draws a linear history in one column", () => { const { rows, width } = commitGraph([c("c", "b"), c("b", "a"), c("a")]); expect(width).toBe(1); expect(rows.map((r) => r.col)).toEqual([0, 0, 0]); // The tip has no line from above; the root none below. expect(rows[0]?.segs).toEqual([{ x1: 0, y1: 0.5, x2: 0, y2: 1, color: 0 }]); expect(rows[2]?.segs).toEqual([{ x1: 0, y1: 0, x2: 0, y2: 0.5, color: 0 }]); }); it("opens a lane for a merged branch and closes it at the fork point", () => { // m merge of b2 into a2 // |\\ // a2 b2 // | b1 // |/ // a1 const { rows, width } = commitGraph([ c("m", "a2", "b2"), c("a2", "a1"), c("b2", "b1"), c("b1", "a1"), c("a1"), ]); expect(width).toBe(2); expect(rows.map((r) => r.col)).toEqual([0, 0, 1, 1, 0]); expect(rows[0]?.segs).toContainEqual({ x1: 0, y1: 0.5, x2: 1, y2: 1, color: 1 }); // b1's lane converges into a1's column. expect(rows[4]?.segs).toContainEqual({ x1: 1, y1: 0, x2: 0, y2: 0.5, color: 1 }); expect(rows[1]?.color).toBe(rows[0]?.color); expect(rows[2]?.color).not.toBe(rows[0]?.color); }); it("keeps a lane running for a parent past the page", () => { const { rows } = commitGraph([c("b", "a")]); expect(rows[0]?.segs).toEqual([{ x1: 0, y1: 0.5, x2: 0, y2: 1, color: 0 }]); }); });