/** * Building graph rows for tests. * * Typed as the real `GraphRow`, so if the Rust side changes shape and the * bindings are updated, every test that builds a row stops compiling. That is * the point: a fixture that can drift silently is worse than no fixture. */ import type { Edge, GraphRow, RefBadge } from "../../src/lib/bindings"; /** A predictable, obviously-fake object id. */ export function fakeId(index: number): string { return index.toString(16).padStart(4, "0").repeat(10); } export function edge(partial: Partial = {}): Edge { return { fromColumn: 0, toColumn: 0, lane: 0, kind: "through", ...partial }; } export function badge(partial: Partial = {}): RefBadge { return { shorthand: "main", kind: "localBranch", isHead: false, ...partial }; } export function row(index: number, partial: Partial = {}): GraphRow { return { id: fakeId(index), row: index, column: 0, lane: 0, dot: "normal", edges: [edge({ kind: "fromCommit" })], width: 1, summary: `Commit number ${index}`, author: "Fixture", // Fixed rather than relative to now, so a test asserting on a rendered // date does not change its answer overnight. time: 1_700_000_000 - index * 3600, refs: [], ...partial, }; } /** A straight run of commits. */ export function linearHistory(count: number): GraphRow[] { return Array.from({ length: count }, (_, index) => row(index)); } /** * A history with a branch and a merge, so lanes, columns and dots all have * something to say. * * ``` * 0 ◍ merge, column 0, two parents * 1 │ ● side branch, column 1 * 2 ● main, column 0 * 3 ● shared root * ``` */ export function branchedHistory(): GraphRow[] { return [ row(0, { dot: "merge", width: 2, refs: [badge({ shorthand: "main", isHead: true })], edges: [ edge({ kind: "fromCommit", fromColumn: 0, toColumn: 0, lane: 0 }), edge({ kind: "fromCommit", fromColumn: 0, toColumn: 1, lane: 1 }), ], }), row(1, { column: 1, lane: 1, width: 2, refs: [badge({ shorthand: "v1.0.0", kind: "tag" })], edges: [ edge({ kind: "through", fromColumn: 0, toColumn: 0, lane: 0 }), edge({ kind: "toCommit", fromColumn: 1, toColumn: 1, lane: 1 }), edge({ kind: "fromCommit", fromColumn: 1, toColumn: 1, lane: 1 }), ], }), row(2, { width: 2, edges: [ edge({ kind: "toCommit", fromColumn: 0, toColumn: 0, lane: 0 }), edge({ kind: "through", fromColumn: 1, toColumn: 1, lane: 1 }), edge({ kind: "fromCommit", fromColumn: 0, toColumn: 0, lane: 0 }), ], }), row(3, { dot: "root", width: 1, edges: [edge({ kind: "toCommit", fromColumn: 0, toColumn: 0, lane: 0 })], }), ]; } /** A remembered repository. */ export function recent( name: string, partial: Partial = {}, ): import("../../src/lib/bindings").RecentRepository { return { id: `/repositories/${name}/.git`, path: `/repositories/${name}`, name, // Fixed rather than relative to now, so an assertion on a rendered date // does not change its answer overnight. lastOpened: 1_700_000_000, exists: true, ...partial, }; } /** A reference, as `list_references` returns one. */ export function reference( shorthand: string, kind: import("../../src/lib/bindings").RefKind = "localBranch", ): import("../../src/lib/bindings").RefEntry { const namespace = { localBranch: "refs/heads/", remoteBranch: "refs/remotes/", tag: "refs/tags/", other: "refs/", }[kind]; return { name: `${namespace}${shorthand}`, shorthand, kind, target: "0".repeat(40), }; }