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	"strings"
  9	"testing"
 10	"time"
 11)
 12
 13type finalizeCall struct {
 14	fp                 string
 15	htmlContent        string
 16	htmlShouldContains []string
 17}
 18
 19func TestRender(t *testing.T) {
 20	w := worktree{lines: []gitWorktreeLine{{
 21		FullPath:       "file",
 22		LastEdited:     time.Now(),
 23		Message:        "Mon joli file",
 24		hasBeenUpdated: true,
 25	}, {
 26		FullPath:       "dir/file2",
 27		LastEdited:     time.Now(),
 28		Message:        "Mon joli file2",
 29		hasBeenUpdated: true,
 30	}, {
 31		FullPath:       "dir/dir2/dir3/file3",
 32		LastEdited:     time.Now(),
 33		Message:        "Mon joli file3",
 34		hasBeenUpdated: true,
 35	}, {
 36		FullPath:       "dir4/file4",
 37		LastEdited:     time.Now(),
 38		Message:        "Mon joli file4",
 39		hasBeenUpdated: true,
 40	}}}
 41	expected := []finalizeCall{{
 42		fp: "worktree/file.html",
 43		htmlShouldContains: []string{
 44			"<pre>Binary</pre>",
 45		},
 46	}, {
 47		fp: "worktree/dir/file2.html",
 48		htmlShouldContains: []string{
 49			"<pre>Binary</pre>",
 50		},
 51	}, {
 52		fp: "worktree/dir/dir2/dir3/file3.html",
 53		htmlShouldContains: []string{
 54			"<pre>Binary</pre>",
 55		},
 56	}, {
 57		fp: "worktree/dir/dir2/dir3/index.html",
 58		htmlShouldContains: []string{
 59			"<a href=\"../../../\">worktree</a>", "<a href=\"../../\">dir</a>", "<a href=\"../\">dir2</a>", "dir3",
 60			"<a href=\"file3.html\">file3</a>",
 61		},
 62	}, {
 63		fp: "worktree/dir/dir2/index.html",
 64		htmlShouldContains: []string{
 65			"<a href=\"../../\">worktree</a>", "<a href=\"../\">dir</a>", "dir2",
 66			"<a href=\"dir3/index.html\">dir3</a>",
 67		},
 68	}, {
 69		fp: "worktree/dir/index.html",
 70		htmlShouldContains: []string{
 71			"<a href=\"../\">worktree</a>", "dir",
 72			"<a href=\"dir2/index.html\">dir2</a>", "<a href=\"file2.html\">file2</a>",
 73		},
 74	}, {
 75		fp: "worktree/dir4/file4.html",
 76		htmlShouldContains: []string{
 77			"<pre>Binary</pre>",
 78		},
 79	}, {
 80		fp: "worktree/dir4/index.html",
 81		htmlShouldContains: []string{
 82			"<a href=\"../\">worktree</a>", "dir4",
 83			"<a href=\"file4.html\">file4</a>",
 84		},
 85	}, {
 86		fp: "worktree/index.html",
 87		htmlShouldContains: []string{
 88			"worktree",
 89			"<a href=\"dir/index.html\">dir</a>", "<a href=\"dir4/index.html\">dir4</a>", "<a href=\"file.html\">file</a>",
 90		},
 91	}}
 92	called := make([]finalizeCall, 0)
 93	w.renderHtml("", "worktree", func(fp, htmlContent string) {
 94		called = append(called, finalizeCall{fp: fp, htmlContent: htmlContent})
 95	})
 96	// if len(called) != len(expected) {
 97	// 	t.Errorf("Should be called %d times but was %d\n%#v", len(expected), len(called), called)
 98	// }
 99	for i, exp := range expected {
100		if called[i].fp != exp.fp {
101			t.Errorf("called[%d].fp should be %s but was %s", i, exp.fp, called[i].fp)
102		}
103		for _, shouldContains := range exp.htmlShouldContains {
104			if _, after, found := strings.Cut(called[i].htmlContent, shouldContains); found {
105				called[i].htmlContent = after
106			} else {
107				t.Errorf("%s should contains %s but was %s", exp.fp, shouldContains, called[i].htmlContent)
108			}
109		}
110	}
111}
112
113func TestIcon(t *testing.T) {
114	if ok := getIcon(".yml"); ok == "📄" {
115		t.Fatalf("icon markdown not found")
116	}
117}
118
119func TestEmoji(t *testing.T) {
120	loadEmojis()
121	if res := replaceEmoji(".yml"); res != ".yml" {
122		t.Fatalf("bad emoji for '.yml'")
123	}
124	if res := replaceEmoji(":fire:"); res != "🔥" {
125		t.Fatalf("bad emoji for ':fire:' %s", res)
126	}
127	if res := replaceEmoji("New release :rocket: :tada:"); res != "New release 🚀 🎉" {
128		t.Fatalf("bad emoji for 'New release :rocket: :tada:' %s", res)
129	}
130}