package server import ( "database/sql" "net/http" "net/http/httptest" "strings" "testing" "github.com/git-pkgs/proxy/internal/database" ) func TestTemplatesRenderAllPages(t *testing.T) { templates := &Templates{} tests := []struct { page string data any }{ {"dashboard", DashboardData{ Stats: DashboardStats{ CachedArtifacts: 42, TotalSize: "1.5 GB", TotalPackages: 10, TotalVersions: 25, }, EnrichmentStats: EnrichmentStatsView{ EnrichedPackages: 5, TotalVulnerabilities: 3, CriticalVulns: 1, HasVulns: true, }, PopularPackages: []PackageInfo{ {Ecosystem: "npm", Name: "lodash", Hits: 100, Size: "500 KB", License: "MIT", LicenseCategory: "permissive"}, }, RecentPackages: []PackageInfo{ {Ecosystem: "cargo", Name: "serde", Version: "1.0.0", Size: "200 KB", CachedAt: "1 hour ago"}, }, }}, {"install", struct{ Registries []RegistryConfig }{ Registries: getRegistryConfigs("http://localhost:8080"), }}, {"search", SearchPageData{ Query: "lodash", Ecosystem: "npm", Results: []SearchResultItem{{Ecosystem: "npm", Name: "lodash", LatestVersion: "4.17.21", Hits: 50, SizeFormatted: "1 MB"}}, Count: 1, Page: 1, PerPage: 50, TotalPages: 1, }}, {"search", SearchPageData{ Query: "nothing", Results: []SearchResultItem{}, Count: 0, Page: 1, PerPage: 50, TotalPages: 0, }}, {"packages_list", PackagesListPageData{ Ecosystem: "", SortBy: defaultSortBy, Results: []SearchResultItem{{Ecosystem: "npm", Name: "express", Hits: 200, SizeFormatted: "2 MB"}}, Count: 1, Page: 1, PerPage: 50, TotalPages: 1, }}, {"package_show", PackageShowData{ Package: &database.Package{ PURL: "pkg:npm/lodash", Ecosystem: "npm", Name: "lodash", LatestVersion: sql.NullString{String: "4.17.21", Valid: true}, License: sql.NullString{String: "MIT", Valid: true}, }, Versions: []database.Version{ {PURL: "pkg:npm/lodash@4.17.21", PackagePURL: "pkg:npm/lodash"}, }, Vulnerabilities: []database.Vulnerability{}, LicenseCategory: "permissive", }}, {"package_show", PackageShowData{ Package: &database.Package{ PURL: "pkg:npm/minimal", Ecosystem: "npm", Name: "minimal", }, Versions: []database.Version{}, Vulnerabilities: []database.Vulnerability{}, LicenseCategory: "unknown", }}, {"version_show", VersionShowData{ Package: &database.Package{ PURL: "pkg:npm/lodash", Ecosystem: "npm", Name: "lodash", LatestVersion: sql.NullString{String: "4.17.21", Valid: true}, License: sql.NullString{String: "MIT", Valid: true}, }, Version: &database.Version{ PURL: "pkg:npm/lodash@4.17.20", PackagePURL: "pkg:npm/lodash", }, Artifacts: []database.Artifact{ { VersionPURL: "pkg:npm/lodash@4.17.20", Filename: "lodash-4.17.20.tgz", StoragePath: sql.NullString{String: "npm/lodash/4.17.20/lodash-4.17.20.tgz", Valid: true}, Size: sql.NullInt64{Int64: 1024, Valid: true}, HitCount: 42, }, }, Vulnerabilities: []database.Vulnerability{}, IsOutdated: true, LicenseCategory: "permissive", HasCachedArtifact: true, }}, {"browse_source", BrowseSourceData{ Ecosystem: "npm", PackageName: "lodash", Version: "4.17.21", }}, {"compare_versions", ComparePageData{ Ecosystem: "npm", PackageName: "lodash", FromVersion: "4.17.20", ToVersion: "4.17.21", }}, } for _, tt := range tests { t.Run(tt.page, func(t *testing.T) { w := httptest.NewRecorder() err := templates.Render(w, tt.page, tt.data) if err != nil { t.Fatalf("Render(%q) failed: %v", tt.page, err) } if w.Code != http.StatusOK { t.Errorf("status = %d, want %d", w.Code, http.StatusOK) } if w.Header().Get("Content-Type") != "text/html; charset=utf-8" { t.Errorf("Content-Type = %q, want text/html", w.Header().Get("Content-Type")) } body := w.Body.String() if body == "" { t.Error("rendered page is empty") } if !strings.Contains(body, "") && !strings.Contains(body, "