@@ -0,0 +1,159 @@
1+package server
2+
3+import (
4+ "encoding/json"
5+ "io"
6+ "net/http"
7+ "net/http/httptest"
8+ "os"
9+ "os/exec"
10+ "path/filepath"
11+ "strings"
12+ "testing"
13+
14+ "xonecas.io/site/internal/project"
15+)
16+
17+// gitRepo makes a repo with a README that links a doc and shows an image, and returns its git dir.
18+func gitRepo(t *testing.T) string {
19+ dir := t.TempDir()
20+ files := map[string]string{
21+ "README.md": "#Hello\n\n [docs](docs/) [guide](docs/guide.md#top) [web](https://x.io)\n",
22+ "img/logo.png": "\x89PNG\x00\n",
23+ "docs/guide.md": "guide\n",
24+ }
25+ for f, body := range files {
26+ os.MkdirAll(filepath.Join(dir, filepath.Dir(f)), 0o755)
27+ os.WriteFile(filepath.Join(dir, f), []byte(body), 0o644)
28+ }
29+ for _, args := range [][]string{{"init", "-q"}, {"add", "."}, {"-c", "user.name=xonecas", "-c", "user.email=x@x", "commit", "-qm", "first"}} {
30+ if out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).CombinedOutput(); err != nil {
31+ t.Fatal(string(out))
32+ }
33+ }
34+ return filepath.Join(dir, ".git")
35+}
36+
37+// testServer serves a data file with project hello, from gitRepo, and a hidden fork.
38+func testServer(t *testing.T) *httptest.Server {
39+ src := []project.Source{{URL: "https://github.com/xonecas/hello.git", Dir: gitRepo(t), Branch: "main", Commits: 1}}
40+ projects := []project.Project{
41+ {Name: "hello", Sources: src, Info: project.Info{Readme: "README.md", Commits: 3, First: "2026-10-01", Last: "2026-11-01",
42+ Months: map[string]int{"2026-10": 2, "2026-11": 1}, Description: "says hi", Tags: []string{"pre-AI", "small", "frontend"}, Topics: []string{"go"}}},
43+ {Name: "a-fork", Sources: src, Info: project.Info{Hidden: "fork"}},
44+ }
45+ data := filepath.Join(t.TempDir(), "projects.json")
46+ b, _ := json.Marshal(projects)
47+ if err := os.WriteFile(data, b, 0o644); err != nil {
48+ t.Fatal(err)
49+ }
50+ srv, err := New(data, "")
51+ if err != nil {
52+ t.Fatal(err)
53+ }
54+ ts := httptest.NewServer(srv)
55+ t.Cleanup(ts.Close)
56+ return ts
57+}
58+
59+// get fetches path with headers, as key, value pairs, and returns the response with its body read.
60+func get(t *testing.T, ts *httptest.Server, path string, headers ...string) (*http.Response, string) {
61+ req, _ := http.NewRequest("GET", ts.URL+path, nil)
62+ for i := 0; i+1 < len(headers); i += 2 {
63+ req.Header.Set(headers[i], headers[i+1])
64+ }
65+ res, err := http.DefaultClient.Do(req)
66+ if err != nil {
67+ t.Fatal(err)
68+ }
69+ b, _ := io.ReadAll(res.Body)
70+ res.Body.Close()
71+ return res, string(b)
72+}
73+
74+func TestPages(t *testing.T) {
75+ ts := testServer(t)
76+ for path, wants := range map[string][]string{
77+ "/": {"All 1 projects", "/projects/hello/", "says hi", "Every month since 2026", `src="/egg.js?v=`, `src="/htmx.min.js?v=`, `hx-boost="true"`},
78+ "/projects/": {`<span id="shown">1</span>`, `value="frontend" class="sr-only peer">`, `form="filters"`, `hx-select="#results"`},
79+ "/projects/hello/": {`<h1 id="hello">Hello</h1>`, `src="/projects/hello/raw/img/logo.png"`, `href="/projects/hello/tree/docs/"`, `href="/projects/hello/blob/docs/guide.md/#top"`, `href="https://x.io"`, `git clone https://xonecas.io/hello.git`},
80+ "/activity/": {`href="/projects/hello/commit/`},
81+ "/activity/2026/": {"commits in 2026"},
82+ "/projects/hello/blob/img/logo.png/": {"Binary file, not shown."},
83+ "/projects/hello/blob/docs/guide.md/": {`<span id="L1"><a href="#L1">1</a>`},
84+ "/projects/hello/tree/docs/": {"guide.md"},
85+ "/projects/hello/commits/": {"1 commits", "first"},
86+ "/style/": {"Style"},
87+ "/llms.txt": {"[hello]"},
88+ } {
89+ res, body := get(t, ts, path)
90+ if res.StatusCode != 200 {
91+ t.Errorf("%s: %s", path, res.Status)
92+ }
93+ for _, want := range wants {
94+ if !strings.Contains(body, want) {
95+ t.Errorf("%s is missing %q", path, want)
96+ }
97+ }
98+ if strings.Contains(body, "a-fork") {
99+ t.Errorf("%s lists a hidden project", path)
100+ }
101+ }
102+ for _, path := range []string{"/projects/a-fork/", "/projects/hello/tree/README.md/", "/projects/hello/blob/docs/", "/projects/hello/commit/abc/", "/activity/1999/", "/activity/2026/9/", "/nope", "/a-fork.git/info/refs"} {
103+ if res, _ := get(t, ts, path); res.StatusCode != 404 {
104+ t.Errorf("%s: %s, want 404", path, res.Status)
105+ }
106+ }
107+ if res, body := get(t, ts, "/projects/hello/raw/img/logo.png"); res.Header.Get("Content-Type") != "image/png" || body != "\x89PNG\x00\n" {
108+ t.Errorf("raw logo.png: %s %q", res.Header.Get("Content-Type"), body)
109+ }
110+ if res, _ := get(t, ts, "/projects/hello"); res.Request.URL.Path != "/projects/hello/" {
111+ t.Errorf("/projects/hello went to %s, not /projects/hello/", res.Request.URL.Path)
112+ }
113+}
114+
115+// TestProjects filters /projects on the server, and a change to the filters form replaces the address with the view's.
116+func TestProjects(t *testing.T) {
117+ ts := testServer(t)
118+ for query, shown := range map[string]string{"?tag=backend": "0", "?tag=backend&tag=frontend": "1", "?q=hel": "1", "?q=go": "1", "?q=g": "0", "?from=2026-11": "1", "?to=0&from=0": "1"} {
119+ if _, body := get(t, ts, "/projects/"+query); !strings.Contains(body, `<span id="shown">`+shown+`</span>`) {
120+ t.Errorf("%s does not show %s projects", query, shown)
121+ }
122+ }
123+ res, _ := get(t, ts, "/projects/?tag=frontend&sort=recent&q=&from=0&to=1", "HX-Request", "true", "HX-Trigger", "filters")
124+ if got := res.Header.Get("HX-Replace-Url"); got != "/projects/?tag=frontend" {
125+ t.Errorf("HX-Replace-Url is %q", got)
126+ }
127+ if res, _ := get(t, ts, "/projects/?tag=frontend", "HX-Request", "true"); res.Header.Get("HX-Replace-Url") != "" {
128+ t.Error("a pager link replaced the address, so back skips the page it came from")
129+ }
130+}
131+
132+// TestSuggest is the fragment htmx puts under the header search.
133+func TestSuggest(t *testing.T) {
134+ ts := testServer(t)
135+ if _, body := get(t, ts, "/suggest?q=HE", "HX-Request", "true"); body != `<a href="/projects/hello/"><b>hello</b><span>says hi</span></a>` {
136+ t.Errorf("suggest he: %q", body)
137+ }
138+ if _, body := get(t, ts, "/suggest?q=g"); !strings.Contains(body, `href="/projects/?q=go"`) || strings.Contains(body, "<html") {
139+ t.Errorf("suggest g: %q", body)
140+ }
141+}
142+
143+// TestClone clones a project from the server, as a visitor would, and fails to push to it.
144+func TestClone(t *testing.T) {
145+ ts := testServer(t)
146+ dest := filepath.Join(t.TempDir(), "hello")
147+ if b, err := exec.Command("git", "clone", "--quiet", ts.URL+"/hello.git", dest).CombinedOutput(); err != nil {
148+ t.Fatal(string(b))
149+ }
150+ if _, err := os.Stat(filepath.Join(dest, "docs/guide.md")); err != nil {
151+ t.Error("the clone has no docs/guide.md")
152+ }
153+ if b, err := exec.Command("git", "-C", dest, "push", "--quiet", "origin", "HEAD:main2").CombinedOutput(); err == nil {
154+ t.Errorf("a push to the clone URL worked: %s", b)
155+ }
156+ if res, _ := get(t, ts, "/hello.git/config"); res.StatusCode == 200 {
157+ t.Error("the repo's config is served")
158+ }
159+}