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 "embed"
9 "fmt"
10 "testing"
11
12 "github.com/sergi/go-diff/diffmatchpatch"
13 "gitroot.dev/libs/golang/plugin/model"
14 "gitroot.dev/libs/golang/plugin/test"
15)
16
17//go:embed test_ressources/*
18var fakeFs embed.FS
19
20const want = `<!doctype html>
21<html>
22<head>
23<meta charset="UTF-8">
24<meta name="viewport" content="width=device-width, initial-scale=1.0">
25<link rel="icon" type="image/jpeg" href="/logo.jpeg">
26<base href="test/" />
27
28<title>my title</title>
29<link rel="stylesheet" href="simple.min.css">
30</head>
31<body>
32<header>
33 test/
34 <nav>
35 <ul></ul>
36 </nav>
37</header>
38<h1 id="title">my title <small>baseline</small></h1>
39
40<footer>
41 <small>clone from ssh://myUrl.myTld/test/</small>
42</footer>
43</body>
44</html>`
45
46const wantNotFound = `<!doctype html>
47<html>
48<head>
49<meta charset="UTF-8">
50<meta name="viewport" content="width=device-width, initial-scale=1.0">
51<link rel="icon" type="image/jpeg" href="/logo.jpeg">
52<base href="http://127.0.0.1:4546/test/" />
53
54<title>my title</title>
55<link rel="stylesheet" href="simple.min.css">
56</head>
57<body>
58<header>
59 {{notavar}}http://127.0.0.1:4546/test/
60 <nav>
61 <ul></ul>
62 </nav>
63</header>
64<h1 id="title">my title</h1>
65
66<footer>
67 <small>my page is my title</small>
68</footer>
69</body>
70</html>`
71
72const wantSubRender = `<!doctype html>
73<html>
74<head>
75<meta charset="UTF-8">
76<meta name="viewport" content="width=device-width, initial-scale=1.0">
77<link rel="icon" type="image/jpeg" href="/logo.jpeg">
78<base href="http://127.0.0.1:4546/test/" />
79
80<title>my title</title>
81<link rel="stylesheet" href="simple.min.css">
82</head>
83<body>
84<header>
85 http://127.0.0.1:4546/test/
86 <nav>
87 <ul></ul>
88 </nav>
89</header>
90<h1 id="title">my title <small>baseline</small></h1>
91<section>Hello</section>
92<footer>
93 <small>clone from ssh://127.0.0.1:4545/test/</small>
94</footer>
95</body>
96</html>`
97
98func TestRenderHtml(t *testing.T) {
99 p := Plugin{
100 server: test.NewFakeServerWithOverride(t, test.Override{
101 ForgeConf: func() (model.ForgeConf, error) {
102 return model.ForgeConf{
103 Domain: "myUrl.myTld",
104 ExternalSshAddr: "ssh://myUrl.myTld/",
105 }, nil
106 },
107 }),
108 }
109 p.Init("test", false, `{"favicon":"/logo.jpeg", "header": "{{repo.url}}", "footer":"<small>clone from {{repo.cloneUrl}}</small>"}`)
110 res := p.renderer.render("/help.md", "<h1 id=\"title\">my title <small>baseline</small></h1>\n", map[string]string{})
111 if res != want {
112 t.Errorf("---\n%s\n--- want ---\n%s\n---", res, want)
113 }
114}
115
116func TestRenderHtmlVarNotFound(t *testing.T) {
117 p := Plugin{
118 server: test.NewFakeServer(t),
119 }
120 p.Init("test", false, `{"favicon":"/logo.jpeg", "header": "{{notavar}}{{repo.url}}", "footer":"<small>my page is {{page.title}}</small>"}`)
121 res := p.renderer.render("/help.md", "<h1 id=\"title\">my title</h1>\n", map[string]string{})
122 if res != wantNotFound {
123 t.Errorf("---\n%s\n--- want ---\n%s\n---", res, wantNotFound)
124 }
125}
126
127func TestMenu(t *testing.T) {
128 p := &Plugin{
129 server: test.NewFakeServer(t),
130 }
131 p.Init("repo", false, `{"menu": [
132 {"display": "🏠 Home", "link": "/"},
133 {"display": "📖 Documentation", "link": "/doc/"}
134 ]}`)
135 menu := p.renderer.buildMenu("/doc/index.md")
136 menuForDoc := "<ul><li><a href=\"./index.html\">🏠 Home</a></li><li><a aria-current=\"page\">📖 Documentation</a></li></ul>"
137 if menu != menuForDoc {
138 t.Errorf("l should be %s but was %s", menuForDoc, menu)
139 }
140}
141
142func TestSubRenderInPage(t *testing.T) {
143 s := test.NewFakeServer(t)
144 s.SetOverride(test.Override{
145 Worktree: func() *model.GrFs {
146 return model.NewGrFsWithFs(model.FS_BASE_WORKTREE, s, fakeFs)
147 },
148 })
149 p := Plugin{
150 server: s,
151 }
152 p.Init("test", false, `{"favicon":"/logo.jpeg", "header": "{{repo.url}}", "footer":"<small>clone from {{repo.cloneUrl}}</small>"}`)
153 res := p.renderer.render("/help.md", "<h1 id=\"title\">my title <small>baseline</small></h1>\n", map[string]string{"layout": "test_ressources/layout.html"})
154 if res != wantSubRender {
155 t.Errorf("---\n%s\n--- want ---\n%s\n---", res, wantSubRender)
156 }
157}
158
159func TestSubRenderInConf(t *testing.T) {
160 s := test.NewFakeServer(t)
161 s.SetOverride(test.Override{
162 Worktree: func() *model.GrFs {
163 return model.NewGrFsWithFs(model.FS_BASE_WORKTREE, s, fakeFs)
164 },
165 })
166 p := Plugin{
167 server: s,
168 }
169 p.Init("test", false, `{"favicon":"/logo.jpeg", "header": "{{repo.url}}", "footer":"<small>clone from {{repo.cloneUrl}}</small>", "layout":[{"glob":"**/*.md", "path":"test_ressources/layout.html"}]}`)
170 res := p.renderer.render("/help.md", "<h1 id=\"title\">my title <small>baseline</small></h1>\n", map[string]string{})
171 if res != wantSubRender {
172 t.Errorf("---\n%s\n--- want ---\n%s\n---", res, wantSubRender)
173 }
174}
175
176func TestSubRenderNotExist(t *testing.T) {
177 calledPath := ""
178 server := test.NewFakeServer(t)
179 server.SetOverride(test.Override{
180 ForgeConf: func() (model.ForgeConf, error) {
181 return model.ForgeConf{
182 Domain: "myUrl.myTld",
183 ExternalSshAddr: "ssh://myUrl.myTld/",
184 }, nil
185 },
186 Worktree: func() *model.GrFs {
187 return model.NewGrFsWithFs(model.FS_BASE_WORKTREE, server, fakeFs)
188 },
189 ModifyContent: func(path, content string) {
190 calledPath = path
191 },
192 })
193 p := Plugin{
194 server: server,
195 }
196 p.Init("test", false, `{"favicon":"/logo.jpeg", "header": "{{repo.url}}", "footer":"<small>clone from {{repo.cloneUrl}}</small>", "layout":[{"glob":"**/*.md", "path":"test_ressources/layout2.html"}]}`)
197 res := p.renderer.render("/help.md", "<h1 id=\"title\">my title <small>baseline</small></h1>\n", map[string]string{})
198 if res != want {
199 dmp := diffmatchpatch.New()
200
201 diffs := dmp.DiffMain(res, want, false)
202
203 fmt.Println(dmp.DiffPrettyText(diffs))
204 t.Error(dmp.DiffPrettyText(diffs))
205 }
206 if calledPath != "test_ressources/layout2.html" {
207 t.Fatalf("should be calledPath %s", calledPath)
208 }
209}