forked from Gitlink/gitlink-cli
237 lines
5.8 KiB
Go
237 lines
5.8 KiB
Go
package wiki
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/internal/client"
|
|
"github.com/gitlink-org/gitlink-cli/internal/output"
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
func resetProjectIDCache() {
|
|
projectIDCache = sync.Map{}
|
|
}
|
|
|
|
func newMockServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
|
|
t.Helper()
|
|
return httptest.NewServer(handler)
|
|
}
|
|
|
|
func writeJSON(t *testing.T, w http.ResponseWriter, v interface{}) {
|
|
t.Helper()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
t.Fatalf("writeJSON: %v", err)
|
|
}
|
|
}
|
|
|
|
// ---- unwrapGatewayResponse tests ----
|
|
|
|
func TestUnwrapGatewayResponse_Success(t *testing.T) {
|
|
env := output.SuccessEnvelope(map[string]interface{}{
|
|
"code": float64(200),
|
|
"data": map[string]interface{}{"id": float64(1)},
|
|
}, nil)
|
|
|
|
result, err := unwrapGatewayResponse(env)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
data, ok := result.Data.(map[string]interface{})
|
|
if !ok {
|
|
t.Fatal("expected map data")
|
|
}
|
|
if data["id"] != float64(1) {
|
|
t.Fatalf("got %v, want 1", data["id"])
|
|
}
|
|
}
|
|
|
|
func TestUnwrapGatewayResponse_Code201(t *testing.T) {
|
|
env := output.SuccessEnvelope(map[string]interface{}{
|
|
"code": float64(201),
|
|
"data": "ok",
|
|
}, nil)
|
|
|
|
result, err := unwrapGatewayResponse(env)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if result.Data != "ok" {
|
|
t.Fatalf("got %v, want ok", result.Data)
|
|
}
|
|
}
|
|
|
|
func TestUnwrapGatewayResponse_BusinessError(t *testing.T) {
|
|
env := output.SuccessEnvelope(map[string]interface{}{
|
|
"code": float64(500),
|
|
"msg": "内部错误",
|
|
}, nil)
|
|
|
|
_, err := unwrapGatewayResponse(env)
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if err.Error() != "[500] 内部错误" {
|
|
t.Fatalf("got %q, want %q", err.Error(), "[500] 内部错误")
|
|
}
|
|
}
|
|
|
|
func TestUnwrapGatewayResponse_NoDataField(t *testing.T) {
|
|
env := output.SuccessEnvelope(map[string]interface{}{
|
|
"code": float64(200),
|
|
}, nil)
|
|
|
|
result, err := unwrapGatewayResponse(env)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
// Should return original envelope since no "data" field to extract
|
|
if result != env {
|
|
t.Fatal("expected original envelope when no data field")
|
|
}
|
|
}
|
|
|
|
func TestUnwrapGatewayResponse_NonMapData(t *testing.T) {
|
|
env := output.SuccessEnvelope("plain text", nil)
|
|
|
|
result, err := unwrapGatewayResponse(env)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if result.Data != "plain text" {
|
|
t.Fatalf("got %v, want plain text", result.Data)
|
|
}
|
|
}
|
|
|
|
// ---- resolveProjectID tests ----
|
|
|
|
func TestResolveProjectID_Success(t *testing.T) {
|
|
resetProjectIDCache()
|
|
server := newMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/owner1/repo1/detail.json" {
|
|
writeJSON(t, w, map[string]interface{}{"project_id": float64(123)})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
})
|
|
defer server.Close()
|
|
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL},
|
|
Owner: "owner1",
|
|
Repo: "repo1",
|
|
}
|
|
pid, err := resolveProjectID(ctx)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if pid != "123" {
|
|
t.Fatalf("got %q, want %q", pid, "123")
|
|
}
|
|
}
|
|
|
|
func TestResolveProjectID_Float64(t *testing.T) {
|
|
resetProjectIDCache()
|
|
server := newMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/owner2/repo2/detail.json" {
|
|
writeJSON(t, w, map[string]interface{}{"project_id": float64(456)})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
})
|
|
defer server.Close()
|
|
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL},
|
|
Owner: "owner2",
|
|
Repo: "repo2",
|
|
}
|
|
pid, err := resolveProjectID(ctx)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if pid != "456" {
|
|
t.Fatalf("got %q, want %q", pid, "456")
|
|
}
|
|
}
|
|
|
|
func TestResolveProjectID_CacheHit(t *testing.T) {
|
|
resetProjectIDCache()
|
|
callCount := 0
|
|
server := newMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
callCount++
|
|
if r.URL.Path == "/owner3/repo3/detail.json" {
|
|
writeJSON(t, w, map[string]interface{}{"project_id": float64(789)})
|
|
return
|
|
}
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
})
|
|
defer server.Close()
|
|
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL},
|
|
Owner: "owner3",
|
|
Repo: "repo3",
|
|
}
|
|
|
|
pid1, err := resolveProjectID(ctx)
|
|
if err != nil {
|
|
t.Fatalf("first call: %v", err)
|
|
}
|
|
if pid1 != "789" {
|
|
t.Fatalf("first call: got %q, want %q", pid1, "789")
|
|
}
|
|
|
|
pid2, err := resolveProjectID(ctx)
|
|
if err != nil {
|
|
t.Fatalf("second call: %v", err)
|
|
}
|
|
if pid2 != "789" {
|
|
t.Fatalf("second call: got %q, want %q", pid2, "789")
|
|
}
|
|
|
|
if callCount != 1 {
|
|
t.Fatalf("API called %d times, want 1 (cache miss)", callCount)
|
|
}
|
|
}
|
|
|
|
func TestResolveProjectID_MissingProjectID(t *testing.T) {
|
|
resetProjectIDCache()
|
|
server := newMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(t, w, map[string]interface{}{"name": "no-project-id"})
|
|
})
|
|
defer server.Close()
|
|
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL},
|
|
Owner: "owner4",
|
|
Repo: "repo4",
|
|
}
|
|
_, err := resolveProjectID(ctx)
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
}
|
|
|
|
func TestResolveProjectID_APIError(t *testing.T) {
|
|
resetProjectIDCache()
|
|
server := newMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
})
|
|
defer server.Close()
|
|
|
|
ctx := &common.RuntimeContext{
|
|
Client: &client.Client{HTTP: server.Client(), BaseURL: server.URL},
|
|
Owner: "owner5",
|
|
Repo: "repo5",
|
|
}
|
|
_, err := resolveProjectID(ctx)
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
}
|