170 lines
5.0 KiB
Go
170 lines
5.0 KiB
Go
package publish
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/distributor/internal/config"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/state"
|
|
)
|
|
|
|
func TestOutputURLUsesURLPathSemantics(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
baseURL string
|
|
destinationBundlePath string
|
|
outputPath string
|
|
want string
|
|
}{
|
|
{
|
|
name: "nested non index",
|
|
baseURL: "https://reports.example.com/archive",
|
|
destinationBundlePath: "daily/brentwood",
|
|
outputPath: "report.html",
|
|
want: "https://reports.example.com/archive/daily/brentwood/report.html",
|
|
},
|
|
{
|
|
name: "nested index",
|
|
baseURL: "https://reports.example.com/archive",
|
|
destinationBundlePath: "daily/brentwood",
|
|
outputPath: "index.html",
|
|
want: "https://reports.example.com/archive/daily/brentwood/",
|
|
},
|
|
{
|
|
name: "fixed index",
|
|
baseURL: "https://reports.example.com/latest",
|
|
destinationBundlePath: "",
|
|
outputPath: "index.html",
|
|
want: "https://reports.example.com/latest/",
|
|
},
|
|
{
|
|
name: "escaped segments",
|
|
baseURL: "https://reports.example.com/archive root",
|
|
destinationBundlePath: "daily reports",
|
|
outputPath: "morning report.html",
|
|
want: "https://reports.example.com/archive%20root/daily%20reports/morning%20report.html",
|
|
},
|
|
{
|
|
name: "nested output index",
|
|
baseURL: "https://reports.example.com",
|
|
destinationBundlePath: "daily",
|
|
outputPath: "site/index.html",
|
|
want: "https://reports.example.com/daily/site/",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := OutputURL(tt.baseURL, tt.destinationBundlePath, tt.outputPath)
|
|
if err != nil {
|
|
t.Fatalf("OutputURL() error = %v", err)
|
|
}
|
|
if got != tt.want {
|
|
t.Fatalf("OutputURL() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPlanLinksSelectsPrimaryURL(t *testing.T) {
|
|
outputs := []Output{
|
|
{
|
|
DestinationPath: "report.md",
|
|
Kind: state.OutputKindSource,
|
|
},
|
|
{
|
|
DestinationPath: "report.html",
|
|
Kind: state.OutputKindGenerated,
|
|
},
|
|
{
|
|
DestinationPath: "index.html",
|
|
Kind: state.OutputKindGenerated,
|
|
},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
primary string
|
|
want string
|
|
}{
|
|
{name: "auto prefers index", primary: config.LinkPrimaryAuto, want: "https://reports.example.com/daily/"},
|
|
{name: "html uses first generated html", primary: config.LinkPrimaryHTML, want: "https://reports.example.com/daily/report.html"},
|
|
{name: "source uses first source", primary: config.LinkPrimarySource, want: "https://reports.example.com/daily/report.md"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
linked, primaryURL, err := PlanLinks(Request{
|
|
DestinationBundlePath: "daily",
|
|
Links: &config.Links{
|
|
BaseURL: "https://reports.example.com",
|
|
Primary: tt.primary,
|
|
},
|
|
}, outputs)
|
|
if err != nil {
|
|
t.Fatalf("PlanLinks() error = %v", err)
|
|
}
|
|
if primaryURL != tt.want {
|
|
t.Fatalf("primary URL = %q, want %q", primaryURL, tt.want)
|
|
}
|
|
for index, output := range linked {
|
|
if output.URL == "" {
|
|
t.Fatalf("linked output %d has empty URL", index)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPlanLinksReturnsNoPrimaryWhenPolicyHasNoMatch(t *testing.T) {
|
|
linked, primaryURL, err := PlanLinks(Request{
|
|
DestinationBundlePath: "daily",
|
|
Links: &config.Links{
|
|
BaseURL: "https://reports.example.com",
|
|
Primary: config.LinkPrimaryHTML,
|
|
},
|
|
}, []Output{{
|
|
DestinationPath: "report.md",
|
|
Kind: state.OutputKindSource,
|
|
}})
|
|
if err != nil {
|
|
t.Fatalf("PlanLinks() error = %v", err)
|
|
}
|
|
if primaryURL != "" {
|
|
t.Fatalf("primary URL = %q, want empty", primaryURL)
|
|
}
|
|
if linked[0].URL != "https://reports.example.com/daily/report.md" {
|
|
t.Fatalf("linked URL = %q", linked[0].URL)
|
|
}
|
|
}
|
|
|
|
func TestPlanLinksLeavesOutputsUnchangedWithoutConfig(t *testing.T) {
|
|
outputs := []Output{{DestinationPath: "report.md", Kind: state.OutputKindSource}}
|
|
linked, primaryURL, err := PlanLinks(Request{}, outputs)
|
|
if err != nil {
|
|
t.Fatalf("PlanLinks() error = %v", err)
|
|
}
|
|
if primaryURL != "" {
|
|
t.Fatalf("primary URL = %q, want empty", primaryURL)
|
|
}
|
|
if linked[0].URL != "" {
|
|
t.Fatalf("output URL = %q, want empty", linked[0].URL)
|
|
}
|
|
}
|
|
|
|
func TestPlanLinksValidatesBaseURLBeforePlanning(t *testing.T) {
|
|
_, _, err := PlanLinks(Request{
|
|
DestinationBundlePath: "daily",
|
|
Links: &config.Links{
|
|
BaseURL: "https://reports.example.com/archive?preview=1",
|
|
Primary: config.LinkPrimaryAuto,
|
|
},
|
|
}, []Output{{
|
|
DestinationPath: "report.md",
|
|
Kind: state.OutputKindSource,
|
|
}})
|
|
if err == nil {
|
|
t.Fatal("PlanLinks() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), "link base URL: must not include a query string") {
|
|
t.Fatalf("PlanLinks() error = %q, want link base URL context", err)
|
|
}
|
|
}
|