package web import ( "net/http" "testing" "veinsforge/internal/config" "veinsforge/internal/data" ) func TestSplitCodeTrailingNewline(t *testing.T) { lines, binary, truncated := splitCode([]byte("one\ntwo\n")) if binary || truncated { t.Fatalf("binary=%v truncated=%v", binary, truncated) } if len(lines) != 2 || codeText(lines) != "one\ntwo" { t.Fatalf("lines = %+v, text = %q", lines, codeText(lines)) } lines, _, _ = splitCode([]byte("one\ntwo\n\n")) if len(lines) != 3 || codeText(lines) != "one\ntwo\n" { t.Fatalf("double newline: lines = %+v, text = %q", lines, codeText(lines)) } } func TestNativeAddressPrefersPublicHost(t *testing.T) { configured := &Server{cfg: config.Config{ NativeHost: "forge.example.com", NativeListen: "0.0.0.0:7419", }} req, _ := http.NewRequest(http.MethodGet, "http://internal.example.net/", nil) if got := configured.nativeAddress(req); got != "forge.example.com:7419" { t.Fatalf("configured host: got %q", got) } } func TestNativeAddressUsesRequestHostOverLoopback(t *testing.T) { s := &Server{cfg: config.Config{ NativeHost: "127.0.0.1", NativeListen: "127.0.0.1:7419", }} req, _ := http.NewRequest(http.MethodGet, "http://forge.example.com:8080/", nil) if got := s.nativeAddress(req); got != "forge.example.com:7419" { t.Fatalf("loopback fallback: got %q", got) } req, _ = http.NewRequest(http.MethodGet, "http://127.0.0.1:8080/", nil) if got := s.nativeAddress(req); got != "127.0.0.1:7419" { t.Fatalf("loopback request: got %q", got) } } func TestNativeCloneURL(t *testing.T) { s := &Server{cfg: config.Config{ NativeHost: "127.0.0.1", NativeListen: "127.0.0.1:7419", }} req, _ := http.NewRequest(http.MethodGet, "http://forge.example.com/", nil) if got := s.nativeCloneURL(req, "alice", "demo"); got != "veins://forge.example.com:7419/alice/demo" { t.Fatalf("clone URL = %q", got) } } func TestArchiveURL(t *testing.T) { repo := &data.Repo{OwnerName: "alice", Name: "demo"} if got, want := archiveURL(repo, "main"), "/alice/demo/archive/main.zip"; got != want { t.Fatalf("archive URL = %q, want %q", got, want) } if got, want := archiveBase(repo, "feature/foo"), "demo-feature-foo"; got != want { t.Fatalf("archive base = %q, want %q", got, want) } } func TestBuildTree(t *testing.T) { files := []string{"README.md", "src/main.go", "src/util/a.go", "src/util/b.go", "z.txt"} tree := buildTree(files, "") if len(tree) != 3 || !tree[0].IsDir || tree[0].Name != "src" { t.Fatalf("root tree = %+v", tree) } sub := buildTree(files, "src") if len(sub) != 2 || sub[0].Name != "util" || sub[1].Name != "main.go" { t.Fatalf("src tree = %+v", sub) } deep := buildTree(files, "src/util") if len(deep) != 2 || deep[0].IsDir { t.Fatalf("util tree = %+v", deep) } } func TestSplitDiffClasses(t *testing.T) { lines := splitDiff("--- a/x\n+++ b/x\n@@ -1 +1 @@\n-old\n+new\n context\n") want := []string{"diff-file", "diff-file", "diff-hunk", "diff-del", "diff-add", ""} if len(lines) != len(want) { t.Fatalf("got %d lines", len(lines)) } for i := range want { if lines[i].Class != want[i] { t.Fatalf("line %d class = %q, want %q", i, lines[i].Class, want[i]) } } } func TestRevPathKeepsSlashesInsideOneSegment(t *testing.T) { for _, rev := range []string{"main", "feature/slash", "a?b", "a%b"} { token := revPath(rev) if token == "" { t.Fatalf("empty token for %q", rev) } if containsSlash(token) { t.Fatalf("token %q still contains a slash", token) } if got := decodeRevPath(token); got != rev { t.Fatalf("decode(%q) = %q, want %q", token, got, rev) } } } func containsSlash(s string) bool { for _, r := range s { if r == '/' { return true } } return false }