package nextcloud

import "testing"

func TestDecodeDAVPath(t *testing.T) {
	got := decodeDAVPath("/Documents/My%20File.docx")
	want := "/Documents/My File.docx"
	if got != want {
		t.Fatalf("decodeDAVPath() = %q, want %q", got, want)
	}
}

func TestClientPathFromDAVHref(t *testing.T) {
	href := "/remote.php/dav/files/user%40example.com/Documents/Hello%20World/"
	got := clientPathFromDAVHref(href)
	want := "/Documents/Hello World"
	if got != want {
		t.Fatalf("clientPathFromDAVHref() = %q, want %q", got, want)
	}
}

func TestFileNameFromDAVPropPrefersDisplayName(t *testing.T) {
	got := fileNameFromDAVProp("My File", "/remote.php/dav/files/u/x%20y")
	if got != "My File" {
		t.Fatalf("got %q", got)
	}
}

func TestFileNameFromDAVPropDecodesHref(t *testing.T) {
	got := fileNameFromDAVProp("", "/remote.php/dav/files/u/Report%20Q1.pdf")
	if got != "Report Q1.pdf" {
		t.Fatalf("got %q", got)
	}
}

func TestSameServerDestinationHeader(t *testing.T) {
	got := SameServerDestinationHeader("/remote.php/dav/files/user/doc.pdf")
	want := "/remote.php/dav/files/user/doc.pdf"
	if got != want {
		t.Fatalf("SameServerDestinationHeader() = %q, want %q", got, want)
	}
	got = SameServerDestinationHeader("remote.php/dav/files/user/doc.pdf")
	if got != want {
		t.Fatalf("SameServerDestinationHeader(relative) = %q, want %q", got, want)
	}
}

func TestWebDAVDestinationUsesPublicURL(t *testing.T) {
	c := (&Client{}).WithPublicURL("http://localhost/cloud")
	got := c.webDAVDestination("/remote.php/dav/files/user/doc.pdf")
	want := "http://localhost/cloud/remote.php/dav/files/user/doc.pdf"
	if got != want {
		t.Fatalf("webDAVDestination() = %q, want %q", got, want)
	}
}

func TestWebDAVDestinationFallbackRelative(t *testing.T) {
	c := &Client{}
	got := c.webDAVDestination("/remote.php/dav/files/user/doc.pdf")
	want := "/remote.php/dav/files/user/doc.pdf"
	if got != want {
		t.Fatalf("webDAVDestination() = %q, want %q", got, want)
	}
}

func TestWebDAVPathEncodesSpaces(t *testing.T) {
	c := &Client{}
	got := c.WebDAVPath("user@example.com", "/Documents/My File")
	want := "/remote.php/dav/files/user@example.com/Documents/My%20File"
	if got != want {
		t.Fatalf("WebDAVPath() = %q, want %q", got, want)
	}
}

func TestResolvePropfindClientPathRelativeHref(t *testing.T) {
	got := ResolvePropfindClientPath("/Documents", "photo.jpg", "photo.jpg")
	want := "/Documents/photo.jpg"
	if got != want {
		t.Fatalf("ResolvePropfindClientPath() = %q, want %q", got, want)
	}
}

func TestNormalizeClientFilePathStripsOCSPrefix(t *testing.T) {
	got := NormalizeClientFilePath("alice", "/alice/files/Photos/vacation.jpg")
	want := "/Photos/vacation.jpg"
	if got != want {
		t.Fatalf("NormalizeClientFilePath() = %q, want %q", got, want)
	}
}

func TestSyncFileDisplayNamePrefersPathBasename(t *testing.T) {
	got := SyncFileDisplayName("/Documents/actual.jpg", "Display Name.jpg")
	want := "actual.jpg"
	if got != want {
		t.Fatalf("SyncFileDisplayName() = %q, want %q", got, want)
	}
}

func TestEnsureClientFilePathJoinsName(t *testing.T) {
	got := EnsureClientFilePath("/Documents", "report.pdf")
	want := "/Documents/report.pdf"
	if got != want {
		t.Fatalf("EnsureClientFilePath() = %q, want %q", got, want)
	}
}
