GitRoot
craft your forge, build your project, grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5package main
6
7import (
8 "testing"
9 "time"
10
11 "gitroot.dev/libs/golang/plugin/model"
12)
13
14func TestBranchOrderCommit(t *testing.T) {
15 lc := &linkedCommits{}
16 lc.Insert(model.Commit{Branch: "test", Hash: "bbbb", Message: "first", Date: time.Unix(0, 0), Committer: "moi", ParentHash: "aaaa"})
17 lc.InsertLine(`<li data-hash="aaaa" data-parent-hash="0000000000000000000000000000000000000000"></li>`)
18 if lc.commit.hash != "bbbb" {
19 t.Fatalf("hash should be bbbb but was %s", lc.commit.hash)
20 }
21 if lc.commit.next.hash != "aaaa" {
22 t.Fatalf("hash should be aaaa but was %s", lc.commit.next.hash)
23 }
24}
25
26func TestBranchOrderCommitMissing(t *testing.T) {
27 lc := &linkedCommits{}
28 lc.InsertLine(`<li data-hash="cccc" data-parent-hash="bbbb"></li>`)
29 lc.InsertLine(`<li data-hash="eeee" data-parent-hash="dddd"></li>`)
30 lc.Insert(model.Commit{Branch: "test", Hash: "aaaa", Message: "first", Date: time.Unix(0, 0), Committer: "moi", ParentHash: NullCommitHash})
31 lc.Insert(model.Commit{Branch: "test", Hash: "bbbb", Message: "first", Date: time.Unix(0, 0), Committer: "moi", ParentHash: "aaaa"})
32 lc.Insert(model.Commit{Branch: "test", Hash: "dddd", Message: "first", Date: time.Unix(0, 0), Committer: "moi", ParentHash: "cccc"})
33
34 if lc.commit.hash != "eeee" {
35 t.Fatalf("hash should be eeee but was %s", lc.commit.hash)
36 }
37 if lc.commit.next.hash != "dddd" {
38 t.Fatalf("hash should be dddd but was %s", lc.commit.next.hash)
39 }
40 if lc.commit.next.next.hash != "cccc" {
41 t.Fatalf("hash should be cccc but was %s", lc.commit.next.next.hash)
42 }
43 if lc.commit.next.next.next.hash != "bbbb" {
44 t.Fatalf("hash should be bbbb but was %s", lc.commit.next.next.next.hash)
45 }
46 if lc.commit.next.next.next.next.hash != "aaaa" {
47 t.Fatalf("hash should be aaaa but was %s", lc.commit.next.next.next.next.hash)
48 }
49 if lc.commit.next.next.next.next.next != nil {
50 t.Fatalf("parent of aaaa should be nil")
51 }
52}
53
54func TestBranchForcePush(t *testing.T) {
55 lc := &linkedCommits{}
56 lc.InsertLine(`<li data-hash="cccc" data-parent-hash="bbbb"></li>`)
57 lc.InsertLine(`<li data-hash="eeee" data-parent-hash="dddd"></li>`)
58 lc.Insert(model.Commit{Branch: "test", Hash: "aaaa", Message: "first", Date: time.Unix(0, 0), Committer: "moi", ParentHash: NullCommitHash})
59 lc.Insert(model.Commit{Branch: "test", Hash: "bbbb", Message: "first", Date: time.Unix(0, 0), Committer: "moi", ParentHash: "aaaa"})
60 lc.Insert(model.Commit{Branch: "test", Hash: "dddd", Message: "first", Date: time.Unix(0, 0), Committer: "moi", ParentHash: "cccc"})
61
62 if lc.commit.hash != "eeee" {
63 t.Fatalf("hash should be eeee but was %s", lc.commit.hash)
64 }
65
66 lc.Insert(model.Commit{Branch: "test", Hash: "force", Message: "first", Date: time.Unix(0, 0), Committer: "moi", ParentHash: "aaaa"})
67 if lc.commit.hash != "force" {
68 t.Fatalf("hash should be force but was %s", lc.commit.hash)
69 }
70 if lc.commit.next.hash != "aaaa" {
71 t.Fatalf("hash should be aaaa but was %s", lc.commit.next.hash)
72 }
73}