package main
import (
"strings"
"testing"
)
// notify accepts lowercase letters, digits, hyphens and underscores in a
// topic key and REJECTS the whole trigger otherwise. quarry's internal
// convention is "repo:<name>" and its repo names allow dots, dashes,
// underscores and uppercase, so the boundary has to translate — running
// the real companion is what surfaced this: a colon key was refused with
// unprocessable_entity and every activity event went nowhere.
func TestRepoTopicKeyIsAcceptableToNotify(t *testing.T) {
for in, want := range map[string]string{
"synthetic": "repo-synthetic",
"libfossil": "repo-libfossil",
"My.Repo": "repo-my-repo",
"a_b-c": "repo-a_b-c",
"UPPER": "repo-upper",
"dots.and.dashes-x": "repo-dots-and-dashes-x",
} {
if got := repoTopicKey(in); got != want {
t.Errorf("repoTopicKey(%q) = %q, want %q", in, got, want)
}
}
// Whatever the input, the result must satisfy notify's rule.
for _, in := range []string{"Weird!!Name", "with spaces", "tr'ailing..."} {
got := repoTopicKey(in)
for _, c := range got {
if !(c >= 'a' && c <= 'z') && !(c >= '0' && c <= '9') && c != '-' && c != '_' {
t.Errorf("repoTopicKey(%q) = %q contains %q, which notify refuses", in, got, c)
}
}
}
}
// The derived-data directory must never be chosen by accident. One
// repository has an obvious home (.quarry beside it); a fleet does not,
// and the old rule — "the first --repo", which for a Go map is a coin
// flip per boot — planted indexes, NATS state and the account database
// inside a source corpus. A fleet without --data is refused, and the
// refusal names the repositories so the operator knows what happened.
func TestDataDirRefusesArbitraryChoice(t *testing.T) {
if d, err := dataDir("/x", map[string]string{"a": "/a", "b": "/b"}); err != nil || d != "/x" {
t.Fatalf("explicit --data not honoured: %q %v", d, err)
}
if d, err := dataDir("", map[string]string{"solo": "/repos/solo"}); err != nil || d != "/repos/solo/.quarry" {
t.Fatalf("single repo default = %q %v, want /repos/solo/.quarry", d, err)
}
_, err := dataDir("", map[string]string{"a": "/a", "b": "/b"})
if err == nil {
t.Fatal("a fleet without --data was given an arbitrary directory")
}
for _, want := range []string{"--data", "a", "b"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("refusal does not mention %q: %v", want, err)
}
}
}