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	"bytes"
  9	"fmt"
 10	"io/fs"
 11	"testing"
 12	"time"
 13
 14	"gitroot.dev/libs/golang/plugin/model"
 15	gitroot "gitroot.dev/libs/golang/plugin/model"
 16	"gitroot.dev/libs/golang/plugin/test"
 17)
 18
 19var firstRender = `<li data-hash="aaaa" data-parent-hash="0000000000000000000000000000000000000000"><code title="aaaa">aaaa</code> - <span title="first">first</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>`
 20var secondRender = `<li data-hash="bbbb" data-parent-hash="aaaa"><code title="bbbb">bbbb</code> - <span title="second">second</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>
 21<li data-hash="aaaa" data-parent-hash="0000000000000000000000000000000000000000"><code title="aaaa">aaaa</code> - <span title="first">first</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>`
 22var thirdRender = `<li data-hash="dddd" data-parent-hash="cccc"><code title="dddd">dddd</code> - <span title="four">four</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>
 23<li data-hash="cccc"><code title="cccc">cccc</code> - <i>commit not found</i></li>
 24<li data-hash="bbbb" data-parent-hash="aaaa"><code title="bbbb">bbbb</code> - <span title="second">second</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>
 25<li data-hash="aaaa" data-parent-hash="0000000000000000000000000000000000000000"><code title="aaaa">aaaa</code> - <span title="first">first</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>`
 26var fourRender = `<li data-hash="dddd" data-parent-hash="cccc"><code title="dddd">dddd</code> - <span title="four">four</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>
 27<li data-hash="cccc" data-parent-hash="bbbb"><code title="cccc">cccc</code> - <span title="third">third</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>
 28<li data-hash="bbbb" data-parent-hash="aaaa"><code title="bbbb">bbbb</code> - <span title="second">second</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>
 29<li data-hash="aaaa" data-parent-hash="0000000000000000000000000000000000000000"><code title="aaaa">aaaa</code> - <span title="first">first</span> <i style="font-size:small;float:right;">(Wed Feb 12 14:33:00 1986) &lt;moi&gt;</i></li>`
 30
 31func TestBranchRender(t *testing.T) {
 32	p := &Plugin{
 33		server: test.NewFakeServer(t),
 34	}
 35	p.Init("repo", model.InitKindDiff, `{"branchesDir": "branches"}`)
 36
 37	p.AddIfNotExist(gitroot.Commit{Branch: "test", Hash: "aaaa", Message: "first", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: NullCommitHash})
 38	p.RenderBranches()
 39	all, err := fs.ReadFile(p.server.Webcontent(), "branches/index.html")
 40	if err != nil {
 41		t.Fatal(err)
 42	}
 43	if !bytes.Contains(all, []byte("<a href=\"branches/test.html\">test</a>")) {
 44		t.Fatalf("test not found")
 45	}
 46	branch, err := fs.ReadFile(p.server.Webcontent(), "branches/test.html")
 47	if err != nil {
 48		t.Fatal(err)
 49	}
 50	if !bytes.Contains(branch, []byte(firstRender)) {
 51		t.Fatalf("init commit \n---\n%s", branch)
 52	}
 53
 54	p.branchCommits = make([]*branchCommits, 0)
 55	p.AddIfNotExist(gitroot.Commit{Branch: "test", Hash: "bbbb", Message: "second", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: "aaaa"})
 56	p.RenderBranches()
 57	branch2, err := fs.ReadFile(p.server.Webcontent(), "branches/test.html")
 58	if err != nil {
 59		t.Fatal(err)
 60	}
 61	if !bytes.Contains(branch2, []byte(secondRender)) {
 62		t.Fatalf("second commit \n---\n%s", branch2)
 63	}
 64
 65	p.branchCommits = make([]*branchCommits, 0)
 66	p.AddIfNotExist(gitroot.Commit{Branch: "test", Hash: "dddd", Message: "four", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: "cccc"})
 67	p.RenderBranches()
 68	branch3, err := fs.ReadFile(p.server.Webcontent(), "branches/test.html")
 69	if err != nil {
 70		t.Fatal(err)
 71	}
 72	if !bytes.Contains(branch3, []byte(thirdRender)) {
 73		t.Fatalf("third commit \n---\n%s", branch3)
 74	}
 75
 76	p.branchCommits = make([]*branchCommits, 0)
 77	p.AddIfNotExist(gitroot.Commit{Branch: "test", Hash: "cccc", Message: "third", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: "bbbb"})
 78	p.RenderBranches()
 79	branch4, err := fs.ReadFile(p.server.Webcontent(), "branches/test.html")
 80	if err != nil {
 81		t.Fatal(err)
 82	}
 83	if !bytes.Contains(branch4, []byte(fourRender)) {
 84		t.Fatalf("four commit \n---\n%s", branch4)
 85	}
 86
 87	p.branchCommits = make([]*branchCommits, 0)
 88	p.AddIfNotExist(gitroot.Commit{Branch: "test", Hash: "bbbb", Message: "second", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: "aaaa"})
 89	p.RenderBranches()
 90	branch4, err = fs.ReadFile(p.server.Webcontent(), "branches/test.html")
 91	if err != nil {
 92		t.Fatal(err)
 93	}
 94	if !bytes.Contains(branch4, []byte(fourRender)) {
 95		t.Fatalf("five commit \n---\n%s", branch4)
 96	}
 97}
 98
 99func TestFillCommits(t *testing.T) {
100	fromCalled := ""
101	toCalled := ""
102	p := &Plugin{
103		server: test.NewFakeServerWithOverride(t, test.Override{
104			Commits: func(from, to string) ([]gitroot.Commit, error) {
105				fromCalled = from
106				toCalled = to
107				return []gitroot.Commit{
108					{Branch: "fillCommits", Hash: "dddd", Message: "four", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: "cccc"},
109					{Branch: "fillCommits", Hash: "cccc", Message: "third", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: "bbbb"},
110				}, nil
111			},
112		}),
113	}
114	p.Init("repo", model.InitKindDiff, `{"branchesDir": "branches"}`)
115
116	p.AddIfNotExist(gitroot.Commit{Branch: "fillCommits", Hash: "aaaa", Message: "first", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: NullCommitHash})
117	p.AddIfNotExist(gitroot.Commit{Branch: "fillCommits", Hash: "bbbb", Message: "second", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: "aaaa"})
118	p.AddIfNotExist(gitroot.Commit{Branch: "fillCommits", Hash: "eeee", Message: "five", Date: time.Date(1986, time.February, 12, 14, 33, 0, 0, time.UTC), CommitterName: "moi", ParentHash: "dddd"})
119	p.RenderBranches()
120
121	if fromCalled != "dddd" {
122		t.Fatal(fmt.Errorf("from not dddd but %s", fromCalled))
123	}
124	if toCalled != "bbbb" {
125		t.Fatal(fmt.Errorf("to not bbbb but %s", toCalled))
126	}
127
128	branch, err := fs.ReadFile(p.server.Webcontent(), "branches/fillCommits.html")
129	if err != nil {
130		t.Fatal(err)
131	}
132	if !bytes.Contains(branch, []byte(fourRender)) {
133		t.Fatalf("init commit \n---\n%s", branch)
134	}
135}