package quarrycli
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func run(t *testing.T, args ...string) (int, string, string) {
t.Helper()
var out, errw bytes.Buffer
e := &Env{Stdout: &out, Stderr: &errw, Version: "9.9.9-test"}
e.Pickup.ConfigDir = t.TempDir()
code := Run(context.Background(), args, e)
return code, out.String(), errw.String()
}
// Bare quarry lists the verbs the table marks as working and none it
// marks not yet implemented; it grows as verbs ship.
func TestBareQuarryListsOnlyImplementedVerbs(t *testing.T) {
code, out, _ := run(t)
if code != 0 {
t.Errorf("FAIL: bare quarry exit %d", code)
}
front := FrontDoor()
if len(front) == 0 {
t.Errorf("FAIL: the bare screen has no verbs")
}
for _, c := range front {
if c.Planned {
t.Errorf("FAIL: front-door verb %q is not implemented", c.Name)
}
if !strings.Contains(out, "quarry "+c.Name) {
t.Errorf("FAIL: bare quarry does not show %q", c.Name)
}
if got, _ := Resolve(strings.Fields(c.Name)); got != c {
t.Errorf("FAIL: %q does not resolve to itself", c.Name)
}
}
for _, c := range Commands() {
if c.Planned && strings.Contains(out, "quarry "+c.Name) {
t.Errorf("FAIL: bare quarry shows %q, which is not implemented", c.Name)
}
}
if !t.Failed() {
t.Log("ok")
}
}
func TestAliasesResolveToTheSameCommands(t *testing.T) {
for alias, full := range map[string]string{"login": "auth login", "whoami": "auth status"} {
a, _ := Resolve([]string{alias, "x"})
b, rest := Resolve(append(strings.Fields(full), "x"))
if a == nil || a != b {
t.Errorf("FAIL: %q and %q are different commands", alias, full)
}
if len(rest) != 1 || rest[0] != "x" {
t.Errorf("FAIL: %q left %v for the verb", full, rest)
}
}
if !t.Failed() {
t.Log("ok")
}
}
func TestHelpListsEveryVerbAndJSONDescribesThem(t *testing.T) {
_, out, _ := run(t, "help")
code, js, _ := run(t, "help", "--json")
if code != 0 {
t.Errorf("FAIL: help --json exit %d", code)
}
var doc struct {
Version string `json:"version"`
Commands []struct {
Name string `json:"name"`
Aliases []string `json:"aliases"`
Usage string `json:"usage"`
Summary string `json:"summary"`
Planned bool `json:"planned"`
} `json:"commands"`
}
if err := json.Unmarshal([]byte(js), &doc); err != nil {
t.Fatalf("FAIL: help --json is not one JSON object: %v", err)
}
if len(doc.Commands) != len(Commands()) {
t.Errorf("FAIL: json lists %d verbs, table has %d", len(doc.Commands), len(Commands()))
}
for _, c := range Commands() {
if !strings.Contains(out, c.Name) {
t.Errorf("FAIL: help does not list %q", c.Name)
}
}
for _, c := range doc.Commands {
if c.Name == "" || c.Usage == "" {
t.Errorf("FAIL: json verb %+v lacks a name or usage", c)
}
}
if doc.Version != "9.9.9-test" {
t.Errorf("FAIL: json version %q", doc.Version)
}
if !t.Failed() {
t.Log("ok")
}
}
func TestUnknownVerbExits2(t *testing.T) {
if code, out, _ := run(t, "frobnicate"); code != 2 || out != "" {
t.Errorf("FAIL: unknown verb exit %d, stdout %q", code, out)
}
if !t.Failed() {
t.Log("ok")
}
}
func TestVersionNamesTheClientAndTheForgeWhenReachable(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/version" {
w.WriteHeader(404)
return
}
w.Write([]byte(`{"version":"4.5.6-forge"}`))
}))
defer srv.Close()
code, out, _ := run(t, "version", srv.URL)
if code != 0 || !strings.Contains(out, "9.9.9-test") || !strings.Contains(out, "4.5.6-forge") {
t.Errorf("FAIL: version exit %d output %q", code, out)
}
// An unreachable forge does not fail the verb.
code, out, _ = run(t, "version", "http://127.0.0.1:1")
if code != 0 || !strings.Contains(out, "9.9.9-test") || strings.Contains(out, "forge") && strings.Contains(out, "4.5") {
t.Errorf("FAIL: unreachable forge: exit %d output %q", code, out)
}
if !t.Failed() {
t.Log("ok")
}
}
// logout is a working verb that the bare screen does not list.
func TestLogoutIsImplementedAndOffTheBareScreen(t *testing.T) {
c, rest := Resolve([]string{"auth", "logout", "x"})
if c == nil || c.Name != "auth logout" || len(rest) != 1 {
t.Fatalf("FAIL: auth logout resolves to %v, rest %v", c, rest)
}
if c.Planned || c.Front || c.Hidden {
t.Errorf("FAIL: logout flags planned=%v front=%v hidden=%v", c.Planned, c.Front, c.Hidden)
}
_, bare, _ := run(t)
if strings.Contains(bare, "logout") {
t.Errorf("FAIL: the bare screen lists logout")
}
_, help, _ := run(t, "help")
if !strings.Contains(help, "auth logout") {
t.Errorf("FAIL: help does not list logout")
}
if !t.Failed() {
t.Log("ok")
}
}
// Help's exit line names every status the verbs use, 4 included.
func TestHelpNamesExitStatus4(t *testing.T) {
_, help, _ := run(t, "help")
var exit string
for _, l := range strings.Split(help, "\n") {
if strings.HasPrefix(l, "Exit:") {
exit = l
}
}
if !strings.Contains(exit, "4") {
t.Errorf("FAIL: the exit line is %q", exit)
}
if !t.Failed() {
t.Log("ok")
}
}
// Through the program: a verb that needs a token, run with none, exits 4
// and --json carries the next command.
func TestVerbsNeedingATokenExit4Through(t *testing.T) {
for _, verb := range [][]string{{"auth", "status"}, {"whoami"}, {"auth", "logout"}} {
code, out, _ := run(t, append(append([]string{}, verb...), "--json", "http://127.0.0.1:1")...)
var m struct {
OK bool `json:"ok"`
Code string `json:"code"`
Next struct {
Cmd string `json:"cmd"`
} `json:"next"`
}
if err := json.Unmarshal([]byte(out), &m); err != nil || code != 4 || m.OK || m.Code != "no_login" || m.Next.Cmd != "quarry auth login" {
t.Errorf("FAIL: %v: exit %d out %q", verb, code, out)
}
}
if !t.Failed() {
t.Log("ok")
}
}