Simplify KB duplicate API and improve naming/docs coverage.
Drop client-supplied target_id, generate IDs server-side, localize duplicate names from request language, document the endpoint, and expand handler/client tests.
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -9,6 +10,8 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/application/repository"
|
||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/middleware"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
@@ -19,7 +22,7 @@ import (
|
||||
type stubKBCopyService struct {
|
||||
interfaces.KnowledgeBaseService
|
||||
byID func(ctx context.Context, id string) (*types.KnowledgeBase, error)
|
||||
duplicate func(ctx context.Context, sourceID, targetID string) (*types.KnowledgeBase, error)
|
||||
duplicate func(ctx context.Context, sourceID string) (*types.KnowledgeBase, error)
|
||||
}
|
||||
|
||||
func (s *stubKBCopyService) GetKnowledgeBaseByID(ctx context.Context, id string) (*types.KnowledgeBase, error) {
|
||||
@@ -29,9 +32,8 @@ func (s *stubKBCopyService) GetKnowledgeBaseByID(ctx context.Context, id string)
|
||||
func (s *stubKBCopyService) DuplicateKnowledgeBase(
|
||||
ctx context.Context,
|
||||
sourceID string,
|
||||
targetID string,
|
||||
) (*types.KnowledgeBase, error) {
|
||||
return s.duplicate(ctx, sourceID, targetID)
|
||||
return s.duplicate(ctx, sourceID)
|
||||
}
|
||||
|
||||
func newDuplicateRouter(svc interfaces.KnowledgeBaseService) *gin.Engine {
|
||||
@@ -49,7 +51,7 @@ func newDuplicateRouter(svc interfaces.KnowledgeBaseService) *gin.Engine {
|
||||
}
|
||||
|
||||
func TestDuplicateHandler_ReturnsCreatedKnowledgeBase(t *testing.T) {
|
||||
var gotSourceID, gotTargetID string
|
||||
var gotSourceID string
|
||||
svc := &stubKBCopyService{
|
||||
byID: func(_ context.Context, id string) (*types.KnowledgeBase, error) {
|
||||
if id != "src" {
|
||||
@@ -57,12 +59,12 @@ func TestDuplicateHandler_ReturnsCreatedKnowledgeBase(t *testing.T) {
|
||||
}
|
||||
return &types.KnowledgeBase{ID: "src", TenantID: 1, Name: "Source"}, nil
|
||||
},
|
||||
duplicate: func(_ context.Context, sourceID, targetID string) (*types.KnowledgeBase, error) {
|
||||
gotSourceID, gotTargetID = sourceID, targetID
|
||||
duplicate: func(_ context.Context, sourceID string) (*types.KnowledgeBase, error) {
|
||||
gotSourceID = sourceID
|
||||
return &types.KnowledgeBase{
|
||||
ID: "copy-id",
|
||||
TenantID: 1,
|
||||
Name: "Source",
|
||||
Name: "Source Copy",
|
||||
CreatorID: "u-test",
|
||||
}, nil
|
||||
},
|
||||
@@ -70,16 +72,14 @@ func TestDuplicateHandler_ReturnsCreatedKnowledgeBase(t *testing.T) {
|
||||
r := newDuplicateRouter(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/src/duplicate",
|
||||
strings.NewReader(`{"target_id":"copy-id"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/src/duplicate", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("expected 201 for duplicate, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if gotSourceID != "src" || gotTargetID != "copy-id" {
|
||||
t.Fatalf("duplicate service called with source=%q target=%q", gotSourceID, gotTargetID)
|
||||
if gotSourceID != "src" {
|
||||
t.Fatalf("duplicate service called with source=%q", gotSourceID)
|
||||
}
|
||||
body := w.Body.String()
|
||||
for _, want := range []string{`"source_id":"src"`, `"target_id":"copy-id"`, `"knowledge_base"`} {
|
||||
@@ -95,7 +95,7 @@ func TestDuplicateHandler_RejectsCrossTenantSource(t *testing.T) {
|
||||
byID: func(_ context.Context, id string) (*types.KnowledgeBase, error) {
|
||||
return &types.KnowledgeBase{ID: id, TenantID: 2, Name: "Shared"}, nil
|
||||
},
|
||||
duplicate: func(_ context.Context, _, _ string) (*types.KnowledgeBase, error) {
|
||||
duplicate: func(_ context.Context, _ string) (*types.KnowledgeBase, error) {
|
||||
calledDuplicate = true
|
||||
return nil, nil
|
||||
},
|
||||
@@ -103,9 +103,7 @@ func TestDuplicateHandler_RejectsCrossTenantSource(t *testing.T) {
|
||||
r := newDuplicateRouter(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/src/duplicate",
|
||||
strings.NewReader(`{}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/src/duplicate", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusForbidden {
|
||||
@@ -115,3 +113,71 @@ func TestDuplicateHandler_RejectsCrossTenantSource(t *testing.T) {
|
||||
t.Fatal("duplicate service must not be called when source KB is outside the caller tenant")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuplicateHandler_SourceNotFound(t *testing.T) {
|
||||
calledDuplicate := false
|
||||
svc := &stubKBCopyService{
|
||||
byID: func(_ context.Context, _ string) (*types.KnowledgeBase, error) {
|
||||
return nil, repository.ErrKnowledgeBaseNotFound
|
||||
},
|
||||
duplicate: func(_ context.Context, _ string) (*types.KnowledgeBase, error) {
|
||||
calledDuplicate = true
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
r := newDuplicateRouter(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/missing/duplicate", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("expected 404 for missing source, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if calledDuplicate {
|
||||
t.Fatal("duplicate service must not be called when source KB is missing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuplicateHandler_PropagatesServiceAppError(t *testing.T) {
|
||||
svc := &stubKBCopyService{
|
||||
byID: func(_ context.Context, _ string) (*types.KnowledgeBase, error) {
|
||||
return &types.KnowledgeBase{ID: "src", TenantID: 1, Name: "Source"}, nil
|
||||
},
|
||||
duplicate: func(_ context.Context, _ string) (*types.KnowledgeBase, error) {
|
||||
return nil, apperrors.NewBadRequestError("invalid vector store binding")
|
||||
},
|
||||
}
|
||||
r := newDuplicateRouter(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/src/duplicate", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400 for service app error, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "invalid vector store binding") {
|
||||
t.Fatalf("expected service error message in body: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuplicateHandler_ServiceUnexpectedError(t *testing.T) {
|
||||
svc := &stubKBCopyService{
|
||||
byID: func(_ context.Context, _ string) (*types.KnowledgeBase, error) {
|
||||
return &types.KnowledgeBase{ID: "src", TenantID: 1, Name: "Source"}, nil
|
||||
},
|
||||
duplicate: func(_ context.Context, _ string) (*types.KnowledgeBase, error) {
|
||||
return nil, errors.New("database unavailable")
|
||||
},
|
||||
}
|
||||
r := newDuplicateRouter(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/knowledge-bases/src/duplicate", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("expected 500 for unexpected service error, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user