From f5acc1c08c700011c69088425d23425652894a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Mon, 12 Apr 2021 12:41:15 +0200 Subject: [PATCH] fixup! Add experimental history graph across branches --- src/frontend/draw-graph.ts | 6 +- src/history-graph-common.ts | 2 +- src/test/suite/history-graph.test.ts | 122 +++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 4 deletions(-) diff --git a/src/frontend/draw-graph.ts b/src/frontend/draw-graph.ts index c37a54f..93673fd 100644 --- a/src/frontend/draw-graph.ts +++ b/src/frontend/draw-graph.ts @@ -22,7 +22,7 @@ import * as GitgraphJS from "@gitgraph/js"; import { commitKeyToBranchName, - CommitWithChildrenFromJson, + commitWithChildrenFromJson, getBranchName, MessageType, ReceivedHistoryReceivedMsg, @@ -180,11 +180,11 @@ function convertMessagePayload(msg: any): SendHistoryReceivedMsg | undefined { return { ...rest, parentlessCommits: parentlessCommits.map((c) => - CommitWithChildrenFromJson(c) + commitWithChildrenFromJson(c) ), commitMapInitializer: commitMapInitializer.map(([k, commit]) => [ k, - CommitWithChildrenFromJson(commit) + commitWithChildrenFromJson(commit) ]) }; } diff --git a/src/history-graph-common.ts b/src/history-graph-common.ts index 9685d9b..9b5dc7e 100644 --- a/src/history-graph-common.ts +++ b/src/history-graph-common.ts @@ -107,7 +107,7 @@ export interface JsonDumpedCommitWithChildren * Converts a JSON serialized [[CommitWithChildren]] into a * [[CommitWithChildren]]. */ -export function CommitWithChildrenFromJson( +export function commitWithChildrenFromJson( commit: JsonDumpedCommitWithChildren ): CommitWithChildren { const { commitTime, ...rest } = commit; diff --git a/src/test/suite/history-graph.test.ts b/src/test/suite/history-graph.test.ts index c7463c5..7a514f1 100644 --- a/src/test/suite/history-graph.test.ts +++ b/src/test/suite/history-graph.test.ts @@ -21,9 +21,21 @@ import { expect } from "chai"; import { afterEach, beforeEach, Context, describe, it } from "mocha"; +import { Commit } from "open-build-service-api"; import { join } from "path"; import { createSandbox } from "sinon"; import { HistoryGraph } from "../../history-graph"; +import { + commitKeyToBranchName, + commitToCommitWithHashes, + CommitWithChildren, + commitWithChildrenFromJson, + CommitWithHashes, + getBranchName, + getCommitKey, + isCommitWithChildren, + isCommitWithHashes +} from "../../history-graph-common"; import { AccountMapInitializer, createFakeWebviewView, @@ -126,3 +138,113 @@ describe("HistoryGraph", () => { ); }); }); + +describe("CommitWithHashes", () => { + const commitCommon = { + projectName: "bar", + packageName: "foo", + files: [], + revisionHash: "uiaeasdf", + revision: 1 + }; + const cmtWithChildren: CommitWithChildren = { + ...commitCommon, + parentCommits: ["baz"], + childCommits: ["foo"], + commitTime: new Date(1000) + }; + const cmtWithHashes: CommitWithHashes = { + ...commitCommon, + parentCommits: ["bar"], + commitTime: new Date(100) + }; + const cmt: Commit = { + ...commitCommon, + commitTime: new Date(1337), + parentCommits: undefined + }; + const cmtWithParent: Commit = { + ...commitCommon, + commitTime: new Date(2674), + parentCommits: [cmt] + }; + + describe("#commitWithChildrenFromJson", () => { + it("reproduces a json dumped commit", () => { + commitWithChildrenFromJson( + JSON.parse(JSON.stringify(cmtWithChildren)) + ).should.deep.equal(cmtWithChildren); + }); + }); + + describe("#isCommitWithHashes", () => { + it("correctly identifies a CommitWithHashes", () => { + isCommitWithHashes(cmtWithHashes).should.equal(true); + }); + + it("correctly identifies a CommitWithChildren", () => { + isCommitWithHashes(cmtWithChildren).should.equal(false); + }); + + it("correctly identifies a Commit", () => { + isCommitWithHashes(cmt).should.equal(false); + }); + }); + + describe("#isCommitWithChildren", () => { + it("correctly identifies a CommitWithHashes", () => { + isCommitWithChildren(cmtWithHashes).should.equal(false); + }); + + it("correctly identifies a CommitWithChildren", () => { + isCommitWithChildren(cmtWithChildren).should.equal(true); + }); + + it("correctly identifies a Commit", () => { + isCommitWithChildren(cmt).should.equal(false); + }); + }); + + describe("#commitToCommitWithHashes", () => { + it("leaves a CommitWithHashes untouched", () => { + commitToCommitWithHashes(cmtWithHashes).should.equal(cmtWithHashes); + }); + + it("leaves a CommitWithChildren untouched", () => { + commitToCommitWithHashes(cmtWithChildren).should.equal(cmtWithChildren); + }); + + it("converts a Commit", () => { + commitToCommitWithHashes(cmt).should.deep.equal({ + ...commitCommon, + commitTime: new Date(1337), + parentCommits: [] + }); + commitToCommitWithHashes(cmtWithParent).should.deep.equal({ + ...commitCommon, + commitTime: new Date(2674), + parentCommits: [getCommitKey(cmt)] + }); + }); + }); + + describe("#getCommitKey", () => { + it("creates a unique commit key", () => { + getCommitKey(cmt).should.deep.equal("bar/foo@uiaeasdf"); + }); + }); + + describe("#getBranchName", () => { + it("creates a unique branch name", () => { + getBranchName(cmt).should.deep.equal("bar/foo"); + }); + }); + + describe("commitKeyToBranchName", () => { + it("reconstructs the branch name from a commit key", () => { + commitKeyToBranchName(getCommitKey(cmt)).should.deep.equal( + getBranchName(cmt) + ); + }); + }); +});