forked from Gitlink/gitlink-cli
387 lines
12 KiB
Go
387 lines
12 KiB
Go
package issue
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/internal/client"
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
func TestIssueClosePreservesCurrentDescription(t *testing.T) {
|
|
var updatePayload map[string]interface{}
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "GET" && r.URL.Path == "/v1/owner/repo/issues/42.json":
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"subject": "Existing title",
|
|
"description": "Existing description",
|
|
})
|
|
case r.Method == "PATCH" && r.URL.Path == "/v1/owner/repo/issues/42.json":
|
|
updatePayload = decodeJSON(t, r)
|
|
writeJSON(t, w, updatePayload)
|
|
default:
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "close", map[string]string{"number": "42"})
|
|
if err != nil {
|
|
t.Fatalf("close shortcut failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, updatePayload["subject"], "Existing title")
|
|
assertEqual(t, updatePayload["description"], "Existing description")
|
|
assertEqual(t, updatePayload["status_id"], float64(5))
|
|
}
|
|
|
|
func TestIssueUpdatePreservesCurrentDescriptionWhenChangingTitleAndState(t *testing.T) {
|
|
var updatePayload map[string]interface{}
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "GET" && r.URL.Path == "/v1/owner/repo/issues/42.json":
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"subject": "Existing title",
|
|
"description": "Existing description",
|
|
})
|
|
case r.Method == "PATCH" && r.URL.Path == "/v1/owner/repo/issues/42.json":
|
|
updatePayload = decodeJSON(t, r)
|
|
writeJSON(t, w, updatePayload)
|
|
default:
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "update", map[string]string{
|
|
"number": "42",
|
|
"title": "New title",
|
|
"state": "closed",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("update shortcut failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, updatePayload["subject"], "New title")
|
|
assertEqual(t, updatePayload["description"], "Existing description")
|
|
assertEqual(t, updatePayload["status_id"], float64(5))
|
|
}
|
|
|
|
func TestIssueUpdatePreservesCurrentSubjectWhenChangingDescription(t *testing.T) {
|
|
var updatePayload map[string]interface{}
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "GET" && r.URL.Path == "/v1/owner/repo/issues/42.json":
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"subject": "Existing title",
|
|
"description": "Existing description",
|
|
})
|
|
case r.Method == "PATCH" && r.URL.Path == "/v1/owner/repo/issues/42.json":
|
|
updatePayload = decodeJSON(t, r)
|
|
writeJSON(t, w, updatePayload)
|
|
default:
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "update", map[string]string{
|
|
"number": "42",
|
|
"body": "New description",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("update shortcut failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, updatePayload["subject"], "Existing title")
|
|
assertEqual(t, updatePayload["description"], "New description")
|
|
}
|
|
|
|
func TestBatchClosePreservesCurrentDescription(t *testing.T) {
|
|
var updatePayload map[string]interface{}
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "GET" && r.URL.Path == "/v1/owner/repo/issues/42.json":
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"subject": "Existing title",
|
|
"description": "Existing description",
|
|
})
|
|
case r.Method == "PATCH" && r.URL.Path == "/v1/owner/repo/issues/42.json":
|
|
updatePayload = decodeJSON(t, r)
|
|
writeJSON(t, w, updatePayload)
|
|
default:
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "batch-close", map[string]string{
|
|
"numbers": "42",
|
|
"dry-run": "false",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("batch-close shortcut failed: %v", err)
|
|
}
|
|
|
|
assertEqual(t, updatePayload["subject"], "Existing title")
|
|
assertEqual(t, updatePayload["description"], "Existing description")
|
|
assertEqual(t, updatePayload["status_id"], float64(5))
|
|
}
|
|
|
|
func runIssueShortcut(t *testing.T, server *httptest.Server, name string, args map[string]string) error {
|
|
t.Helper()
|
|
shortcut := findIssueShortcut(t, name)
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{
|
|
HTTP: server.Client(),
|
|
BaseURL: server.URL,
|
|
},
|
|
Owner: "owner",
|
|
Repo: "repo",
|
|
Format: "json",
|
|
Args: args,
|
|
}
|
|
return shortcut.Run(ctx)
|
|
}
|
|
|
|
func findIssueShortcut(t *testing.T, name string) *common.Shortcut {
|
|
t.Helper()
|
|
for _, shortcut := range Shortcuts() {
|
|
if shortcut.Name == name {
|
|
return shortcut
|
|
}
|
|
}
|
|
t.Fatalf("shortcut %q not found", name)
|
|
return nil
|
|
}
|
|
|
|
func newIssueTestServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
|
|
t.Helper()
|
|
return httptest.NewServer(handler)
|
|
}
|
|
|
|
func decodeJSON(t *testing.T, r *http.Request) map[string]interface{} {
|
|
t.Helper()
|
|
var payload map[string]interface{}
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("failed to decode request body: %v", err)
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func writeJSON(t *testing.T, w http.ResponseWriter, payload interface{}) {
|
|
t.Helper()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
|
t.Fatalf("failed to write response: %v", err)
|
|
}
|
|
}
|
|
|
|
func assertEqual(t *testing.T, got interface{}, want interface{}) {
|
|
t.Helper()
|
|
if got != want {
|
|
t.Fatalf("got %v (%T), want %v (%T)", got, got, want, want)
|
|
}
|
|
}
|
|
|
|
// === 新增命令测试 ===
|
|
|
|
func TestStatusesCallsCorrectEndpoint(t *testing.T) {
|
|
var requestedPath string
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
requestedPath = r.URL.Path
|
|
writeJSON(t, w, map[string]interface{}{
|
|
"total_count": float64(2),
|
|
"statues": []interface{}{},
|
|
})
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "statuses", map[string]string{})
|
|
if err != nil {
|
|
t.Fatalf("statuses failed: %v", err)
|
|
}
|
|
assertPath(t, requestedPath, "/v1/owner/repo/issue_statues.json")
|
|
}
|
|
|
|
func TestAuthorsCallsCorrectEndpoint(t *testing.T) {
|
|
var requestedPath, requestedKeyword string
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
requestedPath = r.URL.Path
|
|
requestedKeyword = r.URL.Query().Get("keyword")
|
|
writeJSON(t, w, map[string]interface{}{"authors": []interface{}{}})
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "authors", map[string]string{"keyword": "zhang"})
|
|
if err != nil {
|
|
t.Fatalf("authors failed: %v", err)
|
|
}
|
|
assertPath(t, requestedPath, "/v1/owner/repo/issue_authors.json")
|
|
assertEqual(t, requestedKeyword, "zhang")
|
|
}
|
|
|
|
func TestAssignersCallsCorrectEndpoint(t *testing.T) {
|
|
var requestedPath string
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
requestedPath = r.URL.Path
|
|
writeJSON(t, w, map[string]interface{}{"assigners": []interface{}{}})
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "assigners", map[string]string{})
|
|
if err != nil {
|
|
t.Fatalf("assigners failed: %v", err)
|
|
}
|
|
assertPath(t, requestedPath, "/v1/owner/repo/issue_assigners.json")
|
|
}
|
|
|
|
func TestPrioritiesCallsCorrectEndpoint(t *testing.T) {
|
|
var requestedPath string
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
requestedPath = r.URL.Path
|
|
writeJSON(t, w, map[string]interface{}{"priorities": []interface{}{}})
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "priorities", map[string]string{})
|
|
if err != nil {
|
|
t.Fatalf("priorities failed: %v", err)
|
|
}
|
|
assertPath(t, requestedPath, "/v1/owner/repo/issue_priorities.json")
|
|
}
|
|
|
|
func TestCommentEditSendsNotesAndAttachmentIDs(t *testing.T) {
|
|
var payload map[string]interface{}
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
payload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{"id": float64(1)})
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "comment-edit", map[string]string{
|
|
"number": "42",
|
|
"comment-id": "100",
|
|
"body": "updated comment",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("comment-edit failed: %v", err)
|
|
}
|
|
assertEqual(t, payload["notes"], "updated comment")
|
|
}
|
|
|
|
func TestCommentEditRequiresNumber(t *testing.T) {
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "comment-edit", map[string]string{
|
|
"comment-id": "100", "body": "test",
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error for missing --number")
|
|
}
|
|
}
|
|
|
|
func TestCommentDeleteCallsCorrectEndpoint(t *testing.T) {
|
|
var requestedPath, requestedMethod string
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
requestedPath = r.URL.Path
|
|
requestedMethod = r.Method
|
|
writeJSON(t, w, map[string]interface{}{"status": float64(1)})
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "comment-delete", map[string]string{
|
|
"number": "42",
|
|
"comment-id": "100",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("comment-delete failed: %v", err)
|
|
}
|
|
assertPath(t, requestedPath, "/v1/owner/repo/issues/42/journals/100.json")
|
|
assertEqual(t, requestedMethod, "DELETE")
|
|
}
|
|
|
|
func TestBatchDestroyCallsBatchDestroyEndpoint(t *testing.T) {
|
|
var requestedPath, requestedMethod string
|
|
var payload map[string]interface{}
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
requestedPath = r.URL.Path
|
|
requestedMethod = r.Method
|
|
payload = decodeJSON(t, r)
|
|
writeJSON(t, w, map[string]interface{}{"status": float64(0), "message": "success"})
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "batch-destroy", map[string]string{
|
|
"numbers": "1,2,3",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("batch-destroy failed: %v", err)
|
|
}
|
|
assertPath(t, requestedPath, "/v1/owner/repo/issues/batch_destroy.json")
|
|
assertEqual(t, requestedMethod, "DELETE")
|
|
|
|
ids, ok := payload["ids"].([]interface{})
|
|
if !ok {
|
|
t.Fatalf("ids should be array, got %T", payload["ids"])
|
|
}
|
|
assertEqual(t, len(ids), 3)
|
|
assertEqual(t, ids[0], float64(1))
|
|
assertEqual(t, ids[1], float64(2))
|
|
assertEqual(t, ids[2], float64(3))
|
|
}
|
|
|
|
func TestBatchDestroyDryRun(t *testing.T) {
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
t.Fatalf("should not call API in dry-run mode")
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "batch-destroy", map[string]string{
|
|
"numbers": "10,20",
|
|
"dry-run": "true",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("batch-destroy dry-run failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestBatchDestroyRequiresNumbers(t *testing.T) {
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "batch-destroy", map[string]string{})
|
|
if err == nil {
|
|
t.Fatal("expected error for missing --numbers")
|
|
}
|
|
}
|
|
|
|
func TestRepliesCallsChildrenJournals(t *testing.T) {
|
|
var requestedPath string
|
|
server := newIssueTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
requestedPath = r.URL.Path
|
|
writeJSON(t, w, map[string]interface{}{"journals": []interface{}{}})
|
|
})
|
|
defer server.Close()
|
|
|
|
err := runIssueShortcut(t, server, "replies", map[string]string{
|
|
"number": "42",
|
|
"comment-id": "100",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("replies failed: %v", err)
|
|
}
|
|
assertPath(t, requestedPath, "/v1/owner/repo/issues/42/journals/100/children_journals.json")
|
|
}
|
|
|
|
func assertPath(t *testing.T, got, want string) {
|
|
t.Helper()
|
|
if got != want {
|
|
t.Fatalf("path: got %s, want %s", got, want)
|
|
}
|
|
}
|