gitlink-cli/shortcuts/issue/batch_update_test.go

81 lines
2.1 KiB
Go

package issue
import (
"net/http"
"reflect"
"testing"
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
)
func TestParseCommaInts(t *testing.T) {
got, err := parseCommaInts("1, 2,2, 3")
if err != nil {
t.Fatalf("parseCommaInts returned error: %v", err)
}
want := []int{1, 2, 3}
if !reflect.DeepEqual(got, want) {
t.Fatalf("parseCommaInts() = %#v, want %#v", got, want)
}
}
func TestParseCommaIntsRejectsInvalid(t *testing.T) {
if _, err := parseCommaInts("1,abc"); err == nil {
t.Fatal("parseCommaInts() expected error for non-integer")
}
}
func TestParseCommaIntsEmpty(t *testing.T) {
if _, err := parseCommaInts(""); err == nil {
t.Fatal("parseCommaInts() expected error for empty input")
}
}
func TestBatchDeleteRequiresConfirm(t *testing.T) {
server := common.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("no API calls expected without confirm, got %s %s", r.Method, r.URL.Path)
})
defer server.Close()
ctx := common.NewTestContext(t, server, "owner", "repo", map[string]string{
"ids": "1,2",
})
err := common.RunShortcut(t, Shortcuts(), "batch-delete", ctx)
if err != nil {
t.Fatalf("batch-delete without confirm failed: %v", err)
}
}
func TestBatchDeleteWithConfirm(t *testing.T) {
var deletePayload map[string]interface{}
server := common.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" && r.URL.Path == "/v1/owner/repo/issues/batch_destroy.json" {
deletePayload = common.DecodeJSON(t, r)
common.WriteJSON(t, w, map[string]interface{}{
"status": 0,
"message": "success",
})
} else {
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
})
defer server.Close()
ctx := common.NewTestContext(t, server, "owner", "repo", map[string]string{
"ids": "10,20,30",
"confirm": "true",
})
err := common.RunShortcut(t, Shortcuts(), "batch-delete", ctx)
if err != nil {
t.Fatalf("batch-delete with confirm failed: %v", err)
}
ids, ok := deletePayload["ids"].([]interface{})
if !ok {
t.Fatalf("ids not a slice: %T", deletePayload["ids"])
}
if len(ids) != 3 {
t.Fatalf("expected 3 ids, got %d", len(ids))
}
}