package site import ( "net/http/httptest" "testing" ) func TestCompareRequest(t *testing.T) { cases := []struct { name string rangeVal string wantRef string wantBase string threeDot bool wantErr bool }{ {name: "two dot", rangeVal: "main..feature", wantRef: "feature", wantBase: "main"}, {name: "three dot", rangeVal: "main...feature", wantRef: "feature", wantBase: "main", threeDot: true}, {name: "refs with dots", rangeVal: "v1.0..v2.0", wantRef: "v2.0", wantBase: "v1.0"}, {name: "missing separator", rangeVal: "main", wantErr: true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { req := httptest.NewRequest("GET", "/demo/compare/"+tc.rangeVal, nil) req.SetPathValue("range", tc.rangeVal) got, err := CompareRequest(req) if tc.wantErr { if err == nil { t.Fatal("expected an error") } return } if err != nil { t.Fatalf("CompareRequest: %v", err) } if got.Ref != tc.wantRef || got.Range == nil || got.Range.Base != tc.wantBase || got.Range.ThreeDot != tc.threeDot { t.Fatalf("CompareRequest = %+v, want ref=%q base=%q threeDot=%v", got, tc.wantRef, tc.wantBase, tc.threeDot) } }) } } // The compare route must use the parser; the paginated routes must declare // their page size. Otherwise buildCtx silently falls back to defaults. func TestRoutesDeclareParsingMetadata(t *testing.T) { routes := Routes() byName := make(map[string]struct { request bool pageSize int }) for _, rt := range routes { byName[rt.Name] = struct { request bool pageSize int }{ request: rt.Request != nil, pageSize: rt.PageSize, } } if !byName["compare"].request { t.Error("compare route has no Request parser") } if byName["log"].pageSize == 0 { t.Error("log route has no PageSize") } if byName["graph"].pageSize == 0 { t.Error("graph route has no PageSize") } }