e2e/design_test.go

b4684c088d2b99f0f42c4e95231c4c0edc122323
gitbay/e2e/design_test.go history · blame · raw

227 lines · 9960 bytes

  1package e2e
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"net/http"
  7	"net/url"
  8	"os"
  9	"path/filepath"
 10	"strings"
 11	"testing"
 12)
 13
 14func TestReadmeRelativeLinks(t *testing.T) {
 15	inst := startInstance(t)
 16	aliceKey := inst.newKey(t, "alice")
 17	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
 18	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/site"); code != 0 {
 19		t.Fatal("repo create failed")
 20	}
 21	work := t.TempDir()
 22	env := inst.gitEnv(aliceKey)
 23	mustGit(t, work, env, "clone", inst.sshURL("alice/site"), "w")
 24	dir := filepath.Join(work, "w")
 25	os.MkdirAll(filepath.Join(dir, "docs"), 0o755)
 26	os.MkdirAll(filepath.Join(dir, "img"), 0o755)
 27	os.WriteFile(filepath.Join(dir, "README.md"), []byte(
 28		"# site\n\n[guide](docs/guide.md) and [export](docs/paper.html) and "+
 29			"[abs](https://example.org/x) here\n\n![logo](img/logo.png)\n"+
 30			"![ext](https://example.org/pic.png)\n\n"+
 31			"| flag | effect |\n|------|--------|\n| `-v` | verbose |\n\n"+
 32			"```go\nfunc main() {}\n```\n"), 0o644)
 33	os.WriteFile(filepath.Join(dir, "docs", "guide.md"), []byte("# guide\n"), 0o644)
 34	os.WriteFile(filepath.Join(dir, "docs", "paper.org"), []byte("* paper\n"), 0o644)
 35	os.WriteFile(filepath.Join(dir, "img", "logo.png"), []byte{0x89, 0x50}, 0o644)
 36	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 37	mustGit(t, dir, env, "add", ".")
 38	mustGit(t, dir, env, "commit", "-q", "-m", "base")
 39	mustGit(t, dir, env, "push", "-q", "origin", "main")
 40
 41	status, body := inst.get(t, "/alice/site")
 42	if status != 200 {
 43		t.Fatalf("tree: %d", status)
 44	}
 45	for _, want := range []string{
 46		`href="/alice/site/blob/main/docs/guide.md"`,   // relative link
 47		`href="/alice/site/blob/main/docs/paper.org"`,  // .html mapped to .org source
 48		`src="/alice/site/raw/main/img/logo.png"`,      // relative image via raw
 49		`src="https://example.org/pic.png"`,            // remote image untouched
 50		`href="https://example.org/x"`,                 // absolute untouched
 51		"<table>", "<td>verbose</td>",                  // GFM table renders
 52		`<span class="kd">func</span>`, // fenced code highlighted via classes
 53		`href="/alice/site/blob/main/README.md">README.md</a>`, // clickable card header
 54		`<th>name</th>`, // file table column headers
 55	} {
 56		if !strings.Contains(body, want) {
 57			t.Errorf("missing %q", want)
 58		}
 59	}
 60	// Branch dropdown lists branches.
 61	if !strings.Contains(body, `class="refmenu"`) || !strings.Contains(body, ">all refs") {
 62		t.Error("branch dropdown missing")
 63	}
 64	// Per-file history: ?path= filters the log; blob pages link to it.
 65	os.WriteFile(filepath.Join(dir, "docs", "notes.txt"), []byte("n\n"), 0o644)
 66	mustGit(t, dir, env, "add", ".")
 67	mustGit(t, dir, env, "commit", "-q", "-m", "touch only the notes")
 68	mustGit(t, dir, env, "push", "-q", "origin", "main")
 69	if _, body := inst.get(t, "/alice/site/log?path=docs/notes.txt"); !strings.Contains(body, "touch only the notes") ||
 70		strings.Contains(body, ">base<") || !strings.Contains(body, "history of") {
 71		t.Fatalf("per-file log wrong:\n%s", body)
 72	}
 73	if _, body := inst.get(t, "/alice/site/log?path=no/such/file"); !strings.Contains(body, "nothing touches") {
 74		t.Fatalf("empty per-file log:\n%s", body)
 75	}
 76	if _, body := inst.get(t, "/alice/site/blob/main/docs/guide.md"); !strings.Contains(body, `log/main?path=docs%2fguide.md">history</a>`) {
 77		t.Fatalf("blob history link missing:\n%s", body)
 78	}
 79	// CLI parity: repo log --path.
 80	logOut, _, _ := inst.ssh(t, aliceKey, "", "repo", "log", "alice/site", "--path", "docs/notes.txt", "--json")
 81	if !strings.Contains(logOut, "touch only the notes") || strings.Contains(logOut, `"subject":"base"`) {
 82		t.Fatalf("repo log --path wrong:\n%s", logOut)
 83	}
 84	// Raw serves images with their real type (nosniff otherwise blocks
 85	// <img>); everything else stays inert text/plain.
 86	resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/raw/main/img/logo.png", inst.httpPort))
 87	if err != nil {
 88		t.Fatal(err)
 89	}
 90	resp.Body.Close()
 91	if ct := resp.Header.Get("Content-Type"); ct != "image/png" {
 92		t.Errorf("raw png content-type = %q", ct)
 93	}
 94	resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/raw/main/README.md", inst.httpPort))
 95	if err != nil {
 96		t.Fatal(err)
 97	}
 98	resp.Body.Close()
 99	if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") {
100		t.Errorf("raw md content-type = %q", ct)
101	}
102	// Blob pages preview images inline.
103	if _, body := inst.get(t, "/alice/site/blob/main/img/logo.png"); !strings.Contains(body, `<img src="/alice/site/raw/main/img/logo.png"`) {
104		t.Errorf("blob image preview missing:\n%s", body)
105	}
106	// Highlighting is class-based so the palette follows the color scheme:
107	// no inline colors on code, and the stylesheet carries both palettes.
108	if _, body := inst.get(t, "/alice/site/blob/main/README.md"); !strings.Contains(body, `class="chroma"`) ||
109		strings.Contains(body, "style=\"color") {
110		t.Errorf("highlighting not class-based:\n%.2000s", body)
111	}
112	if _, css := inst.get(t, "/static/style.css"); strings.Count(css, "/* Background */") < 2 ||
113		!strings.Contains(css, ".chroma, .bg { background: transparent") {
114		t.Error("stylesheet missing dual syntax palettes")
115	}
116	// Explore rows carry topics, license, and updated date.
117	inst.ssh(t, aliceKey, "", "repo", "topics", "add", "alice/site", "web")
118	// Bare 0BSD grant (no notice-retention clause), wrapped mid-sentence.
119	os.WriteFile(filepath.Join(dir, "LICENSE"), []byte("Permission to use, copy, modify,\nand/or distribute this software for any\npurpose with or without fee is hereby granted.\n"), 0o644)
120	mustGit(t, dir, env, "add", ".")
121	mustGit(t, dir, env, "commit", "-q", "-m", "license")
122	mustGit(t, dir, env, "push", "-q", "origin", "main")
123	_, body = inst.get(t, "/explore")
124	for _, want := range []string{`href="/explore?q=web"`, "0BSD", "updated 20"} {
125		if !strings.Contains(body, want) {
126			t.Errorf("explore row missing %q", want)
127		}
128	}
129}
130
131func TestWebInteractions(t *testing.T) {
132	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
133	aliceKey := inst.newKey(t, "alice")
134	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
135	if _, _, code := inst.ssh(t, aliceKey, "", "org", "create", "theorg"); code != 0 {
136		t.Fatal("org create failed")
137	}
138
139	out, _, code := inst.ssh(t, aliceKey, "", "web", "login", "--json")
140	if code != 0 {
141		t.Fatal("web login failed")
142	}
143	var env2 struct {
144		Data struct {
145			URL string `json:"url"`
146		} `json:"data"`
147	}
148	json.Unmarshal([]byte(out), &env2)
149	browser := newBrowser(t)
150	browserGet(t, browser, inst.base()+env2.Data.URL[strings.Index(env2.Data.URL, "/login"):])
151
152	// Create a repo under the org through the web form.
153	if status, _ := browserPost(t, browser, inst.base()+"/new",
154		url.Values{"owner": {"theorg"}, "name": {"webborn"}, "visibility": {"public"}}); status != 200 {
155		t.Fatalf("org repo via web: %d", status)
156	}
157	if out, _, code := inst.ssh(t, aliceKey, "", "repo", "show", "theorg/webborn"); code != 0 {
158		t.Fatalf("org repo missing: %s", out)
159	}
160
161	// Pin from the web; the repo header reflects it and the dashboard
162	// lists it; a second toggle unpins.
163	if status, _ := browserPost(t, browser, inst.base()+"/theorg/webborn/pin", url.Values{}); status != 200 {
164		t.Fatal("pin toggle failed")
165	}
166	_, body := browserGet(t, browser, inst.base()+"/theorg/webborn")
167	if !strings.Contains(body, "★ pinned") {
168		t.Fatal("repo header not pinned")
169	}
170	if _, body = browserGet(t, browser, inst.base()+"/"); !strings.Contains(body, "theorg<span") {
171		t.Fatal("dashboard missing pinned repo")
172	}
173	browserPost(t, browser, inst.base()+"/theorg/webborn/pin", url.Values{})
174	if _, body = browserGet(t, browser, inst.base()+"/theorg/webborn"); !strings.Contains(body, "☆ pin") {
175		t.Fatal("unpin failed")
176	}
177
178	// New issue with labels through the form; label chips filter.
179	if status, _ := browserPost(t, browser, inst.base()+"/theorg/webborn/issues/new",
180		url.Values{"title": {"styled"}, "body": {"b"}, "labels": {"bug ui"}}); status != 200 {
181		t.Fatal("issue via web failed")
182	}
183	if status, _ := browserPost(t, browser, inst.base()+"/theorg/webborn/issues/new",
184		url.Values{"title": {"plain"}}); status != 200 {
185		t.Fatal("second issue failed")
186	}
187	_, body = browserGet(t, browser, inst.base()+"/theorg/webborn/issues?label=bug")
188	if !strings.Contains(body, "styled") || strings.Contains(body, ">plain<") {
189		t.Fatalf("label filter wrong:\n%s", body)
190	}
191	_, body = browserGet(t, browser, inst.base()+"/theorg/webborn/issues/1")
192	if !strings.Contains(body, `href="/theorg/webborn/issues?label=bug"`) ||
193		!strings.Contains(body, `href="/alice">alice</a>`) {
194		t.Fatal("issue page chips/author not linked")
195	}
196}
197
198func TestCommitParentLinks(t *testing.T) {
199	inst := startInstance(t)
200	aliceKey := inst.newKey(t, "alice")
201	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
202	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
203		t.Fatal("repo create failed")
204	}
205	work := t.TempDir()
206	env := inst.gitEnv(aliceKey)
207	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
208	dir := filepath.Join(work, "w")
209	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
210	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
211	mustGit(t, dir, env, "add", ".")
212	mustGit(t, dir, env, "commit", "-q", "-m", "first")
213	first := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
214	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("b\n"), 0o644)
215	mustGit(t, dir, env, "commit", "-qam", "second")
216	head := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
217	mustGit(t, dir, env, "push", "-q", "origin", "main")
218
219	_, body := inst.get(t, "/alice/app/commit/"+head)
220	if !strings.Contains(body, `href="/alice/app/commit/`+first+`"`) {
221		t.Fatal("parent commit not linked")
222	}
223	_, body = inst.get(t, "/alice/app/commit/"+first)
224	if strings.Contains(body, ">parent") {
225		t.Fatal("root commit shows a parent")
226	}
227}