package api
import (
"testing"
"craftdesign.group/quarry/internal/oltp"
)
// One Fossil repository has many spellings: a default port, a letter
// case or a slash is not a different place, a different path is.
func TestSameFossilRemoteNamesOnePlace(t *testing.T) {
up := "https://fossil.example.org/quarry"
if !sameFossilRemote("https://fossil.example.org/repo/xfer", "https://FOSSIL.example.org:443/%72epo/") {
t.Error("FAIL: /repo/xfer and /%72epo/ read as different places")
}
for _, same := range []string{
"https://fossil.example.org/quarry/", "https://FOSSIL.example.org/quarry",
"https://fossil.example.org:443/quarry", "http://fossil.example.org/quarry",
"https://fossil.example.org/quarry/xfer", "https://fossil.example.org/quarry/xfer/",
"https://fossil.example.org/%71uarry", "https://fossil.example.org//quarry",
"https://fossil.example.org/a/../quarry",
} {
if !sameFossilRemote(same, up) {
t.Errorf("FAIL: %q reads as a different place from %q", same, up)
}
}
for _, other := range []string{
"https://fossil.example.org/other", "https://fossil.example.org:8443/quarry",
"https://other.example.org/quarry", "https://fossil.example.org/quarry/other", "https://fossil.example.org/xfer", "",
} {
if sameFossilRemote(other, up) {
t.Errorf("FAIL: %q reads as the same place as %q", other, up)
}
}
t.Log("ok")
}
func TestAFossilRemoteBecomesItsWebAddress(t *testing.T) {
for remote, want := range map[string]string{
"https://fossil.craftdesign.group/quarry": "fossil.craftdesign.group quarry",
"https://Fossil.Example.org:8443/a/b/": "fossil.example.org:8443 a/b",
"https://fossil.example.org/": "fossil.example.org ",
"https://[2001:DB8::1]:8443/a": "[2001:db8::1]:8443 a",
"https://[2001:db8::1]/a": "[2001:db8::1] a",
} {
h, p := fossilPlace(remote)
if got := h + " " + p; got != want {
t.Errorf("FAIL: %q -> %q, want %q", remote, got, want)
}
}
t.Log("ok")
}
// The About link keeps the remote's own scheme and the brackets round an
// IPv6 host.
func TestTheAboutLinkKeepsTheRemotesSchemeAndBrackets(t *testing.T) {
for remote, want := range map[string]string{
"https://fossil.example.org/quarry": "https://fossil.example.org/quarry",
"http://127.0.0.1:8080/quarry": "http://127.0.0.1:8080/quarry",
"http://[::1]:8080/q": "http://[::1]:8080/q",
"https://[2001:db8::1]/q": "https://[2001:db8::1]/q",
} {
d := Deps{FossilMirrors: aboutStore{oltp.FossilMirror{Remote: remote, PushedAt: "2026-11-01T00:00:00Z"}}}
if _, link := d.fossilMirrorAbout("r"); link != want {
t.Errorf("FAIL: %s links to %q, want %q", remote, link, want)
}
}
t.Log("ok")
}
type aboutStore struct{ m oltp.FossilMirror }
func (s aboutStore) FossilMirrorOf(string) (oltp.FossilMirror, bool, error) { return s.m, true, nil }
func (aboutStore) SetFossilMirror(_, _, _, _, _, _ string) (oltp.FossilMirror, error) {
return oltp.FossilMirror{}, nil
}
func (aboutStore) DisableFossilMirror(_, _, _ string) (bool, error) { return false, nil }