internal/frontend: fix pageInfo and add tests

pageInfo now returns the last element of the path, if the pageType is
directory or module. If it is a module, a trailing slash is also added
the title.

Tests are added for pageInfo.

Change-Id: I35e0b7bd833a18a553155374f38736cfcc0f16cc
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/259798
Trust: Julie Qiu <julie@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/internal/frontend/unit.go b/internal/frontend/unit.go
index e03150a..ae6e87e 100644
--- a/internal/frontend/unit.go
+++ b/internal/frontend/unit.go
@@ -7,6 +7,7 @@
 import (
 	"context"
 	"net/http"
+	"path"
 	"sort"
 	"strconv"
 	"strings"
@@ -345,7 +346,10 @@
 	if unit.IsPackage() {
 		return unit.Name, pageTypePackage
 	}
-	return unit.Path, pageTypeDirectory
+	if unit.IsModule() {
+		return path.Base(unit.Path), pageTypeModule
+	}
+	return path.Base(unit.Path) + "/", pageTypeDirectory
 }
 
 // displayBreadcrumbs appends additional breadcrumb links for display
diff --git a/internal/frontend/unit_test.go b/internal/frontend/unit_test.go
index 72a744a..68c616b 100644
--- a/internal/frontend/unit_test.go
+++ b/internal/frontend/unit_test.go
@@ -11,9 +11,50 @@
 	"github.com/google/go-cmp/cmp"
 	"golang.org/x/pkgsite/internal"
 	"golang.org/x/pkgsite/internal/postgres"
+	"golang.org/x/pkgsite/internal/stdlib"
 	"golang.org/x/pkgsite/internal/testing/sample"
 )
 
+func TestPageInfo(t *testing.T) {
+	m := sample.LegacyModule("golang.org/x/tools", "v1.0.0", "go/packages", "cmd/godoc")
+
+	type test struct {
+		name      string
+		unit      *internal.Unit
+		wantTitle string
+		wantType  string
+	}
+	var tests []*test
+	for _, u := range m.Units {
+		switch u.Path {
+		case "golang.org/x/tools":
+			tests = append(tests, &test{"module golang.org/x/tools", u, "tools", pageTypeModule})
+		case "golang.org/x/tools/go/packages":
+			tests = append(tests, &test{"package golang.org/x/tools/go/packages", u, "packages", pageTypePackage})
+		case "golang.org/x/tools/go":
+			tests = append(tests, &test{"directory golang.org/x/tools/go", u, "go/", pageTypeDirectory})
+		case "golang.org/x/tools/cmd/godoc":
+			u.Name = "main"
+			tests = append(tests, &test{"package golang.org/x/tools/cmd/godoc", u, "godoc", pageTypeCommand})
+		case "golang.org/x/tools/cmd":
+			tests = append(tests, &test{"directory golang.org/x/tools/cmd", u, "cmd/", pageTypeDirectory})
+		default:
+			t.Fatalf("Unexpected path: %q", u.Path)
+		}
+	}
+	std := sample.LegacyModule(stdlib.ModulePath, "v1.0.0", "")
+	tests = append(tests, &test{"module std", std.Units[0], "Standard library", pageTypeStdLib})
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			gotTitle, gotType := pageInfo(test.unit)
+			if gotTitle != test.wantTitle || gotType != test.wantType {
+				t.Errorf("pageInfo(%q): %q, %q; want = %q, %q", test.unit.Path, gotTitle, gotType, test.wantTitle, test.wantType)
+			}
+		})
+	}
+}
+
 func TestGetNestedModules(t *testing.T) {
 	ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
 	defer cancel()