package api
import (
"context"
"errors"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// team.text is a patch: a body naming neither field changes neither, and
// a body naming one changes only that one (quarry#424).
func TestTeamTextChangesOnlyWhatItNames(t *testing.T) {
db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
if err := db.AddAccount("ada", "a-long-enough-password", false); err != nil {
t.Fatal(err)
}
if err := db.AddTeam("crew", "Crew", "the first sentence", "ada"); err != nil {
t.Fatal(err)
}
r := ops.NewRegistry()
if err := RegisterAll(r, Deps{Version: "v", Repos: NewRepos(nil), Accounts: newFakeAccounts(), Teams: db}); err != nil {
t.Fatal(err)
}
ada := ops.WithUser(context.Background(), "ada")
held := func() (string, string) {
tm, ok := db.TeamByName("crew")
if !ok {
t.Fatal("team gone")
}
return tm.Title, tm.Descr
}
str := func(s string) *string { return &s }
if _, err := r.Invoke(ada, "team.text", teamTextIn{Name: "crew"}); !errors.Is(err, ops.ErrInvalid) {
t.Errorf("FAIL: a body naming neither field: %v", err)
}
if title, descr := held(); title != "Crew" || descr != "the first sentence" {
t.Errorf("FAIL: a body naming neither changed %q / %q", title, descr)
}
if _, err := r.Invoke(ada, "team.text", teamTextIn{Name: "crew", Title: str("The Crew")}); err != nil {
t.Fatal(err)
}
if title, descr := held(); title != "The Crew" || descr != "the first sentence" {
t.Errorf("FAIL: naming the title changed %q / %q", title, descr)
}
if _, err := r.Invoke(ada, "team.text", teamTextIn{Name: "crew", Descr: str("")}); err != nil {
t.Fatal(err)
}
if title, descr := held(); title != "The Crew" || descr != "" {
t.Errorf("FAIL: an explicit empty descr did not clear it alone: %q / %q", title, descr)
}
t.Log("ok")
}