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