128 lines
4.5 KiB
Go
128 lines
4.5 KiB
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCLIRepoHTTPCommandsOutput(t *testing.T) {
|
|
t.Setenv("GITOOCEAN_CONFIG", filepath.Join(t.TempDir(), "client.json"))
|
|
var auth string
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
auth = r.Header.Get("Authorization")
|
|
switch r.URL.Path {
|
|
case "/api/repos/alice/demo":
|
|
writeJSON(w, http.StatusOK, Repository{Owner: "alice", Name: "demo", Visibility: "public", DefaultBranch: "main", Description: "desc"})
|
|
case "/api/repos/search":
|
|
if r.URL.Query().Get("q") != "demo" || r.URL.Query().Get("scope") != "mine" {
|
|
t.Fatalf("unexpected search query: %s", r.URL.RawQuery)
|
|
}
|
|
writeJSON(w, http.StatusOK, []Repository{{Owner: "alice", Name: "demo", Visibility: "public", Description: "desc"}})
|
|
case "/api/repos/alice/demo/branches":
|
|
writeJSON(w, http.StatusOK, []RefInfo{{Name: "main", Commit: "1234567890abcdef", Date: time.Date(2026, 1, 2, 3, 4, 0, 0, time.UTC), Message: "initial"}})
|
|
default:
|
|
t.Fatalf("unexpected path: %s", r.URL.Path)
|
|
}
|
|
}))
|
|
defer ts.Close()
|
|
if err := saveConfig(Config{Server: ts.URL, Token: "tok", Username: "alice"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
out := captureStdout(t, func() {
|
|
if err := cliRepoView([]string{"alice/demo"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if auth != "Bearer tok" || !strings.Contains(out, "alice/demo") || strings.Contains(strings.TrimSpace(out), "{") {
|
|
t.Fatalf("unexpected repo view output/auth: auth=%q out=%s", auth, out)
|
|
}
|
|
|
|
out = captureStdout(t, func() {
|
|
if err := cliRepoView([]string{"alice/demo", "--json"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if !strings.Contains(out, `"owner": "alice"`) {
|
|
t.Fatalf("expected JSON output, got %s", out)
|
|
}
|
|
|
|
out = captureStdout(t, func() {
|
|
if err := cliRepoSearch([]string{"demo", "--mine"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if !strings.Contains(out, "REPOSITORY") || !strings.Contains(out, "alice/demo") {
|
|
t.Fatalf("unexpected search output: %s", out)
|
|
}
|
|
|
|
out = captureStdout(t, func() {
|
|
if err := cliRepoRefs([]string{"alice/demo"}, "branches"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if !strings.Contains(out, "MAIN") && !strings.Contains(out, "main") {
|
|
t.Fatalf("unexpected refs output: %s", out)
|
|
}
|
|
}
|
|
|
|
func TestCLIPRAndTokenHTTPCommandsOutput(t *testing.T) {
|
|
t.Setenv("GITOOCEAN_CONFIG", filepath.Join(t.TempDir(), "client.json"))
|
|
created := time.Date(2026, 1, 2, 3, 4, 0, 0, time.UTC)
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/api/repos/alice/demo/pulls":
|
|
writeJSON(w, http.StatusOK, []PullRequest{{Number: 2, Status: "open", Title: "Fix", SourceOwner: "alice", SourceRepo: "demo", SourceBranch: "feature", TargetOwner: "alice", TargetRepo: "demo", TargetBranch: "main"}})
|
|
case "/api/repos/alice/demo/pulls/2":
|
|
writeJSON(w, http.StatusOK, PullRequest{Number: 2, Status: "open", Title: "Fix", Author: "alice", SourceOwner: "alice", SourceRepo: "demo", SourceBranch: "feature", TargetOwner: "alice", TargetRepo: "demo", TargetBranch: "main"})
|
|
case "/api/repos/alice/demo/pulls/2/comments":
|
|
writeJSON(w, http.StatusOK, []PRComment{{ID: 1, Author: "alice", Body: "looks good", CreatedAt: created, UpdatedAt: created}})
|
|
case "/api/tokens":
|
|
writeJSON(w, http.StatusOK, []TokenInfo{{ID: 1, Active: true, CreatedAt: created, ExpiresAt: created.Add(tokenTTL)}})
|
|
default:
|
|
t.Fatalf("unexpected path: %s", r.URL.Path)
|
|
}
|
|
}))
|
|
defer ts.Close()
|
|
if err := saveConfig(Config{Server: ts.URL, Token: "tok", Username: "alice"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
out := captureStdout(t, func() {
|
|
if err := cliPRList([]string{"alice/demo"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if !strings.Contains(out, "#2") || strings.Contains(strings.TrimSpace(out), "[") {
|
|
t.Fatalf("unexpected PR list output: %s", out)
|
|
}
|
|
out = captureStdout(t, func() {
|
|
if err := cliPRView([]string{"alice/demo", "2", "--json"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if !strings.Contains(out, `"number": 2`) {
|
|
t.Fatalf("unexpected PR view JSON: %s", out)
|
|
}
|
|
out = captureStdout(t, func() {
|
|
if err := cliPRComments([]string{"alice/demo", "2"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if !strings.Contains(out, "looks good") {
|
|
t.Fatalf("unexpected comments output: %s", out)
|
|
}
|
|
out = captureStdout(t, func() {
|
|
if err := cliToken([]string{"list", "--json"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
if !strings.Contains(out, `"id": 1`) {
|
|
t.Fatalf("unexpected token JSON: %s", out)
|
|
}
|
|
}
|