From 2c4bb78225236ecbf785457c8d6716fedaccb0b2 Mon Sep 17 00:00:00 2001 From: "yiling.ji" Date: Wed, 18 Dec 2024 03:11:14 +0000 Subject: [PATCH 1/5] Merge branch 'feature/handler_tests' into 'main' Add repo handler tests See merge request product/starhub/starhub-server!731 --- api/handler/helper_test.go | 128 ++++ api/handler/repo.go | 12 +- api/handler/repo_test.go | 1172 ++++++++++++++++++++++++++++++++++-- api/httpbase/user.go | 4 +- 4 files changed, 1244 insertions(+), 72 deletions(-) create mode 100644 api/handler/helper_test.go diff --git a/api/handler/helper_test.go b/api/handler/helper_test.go new file mode 100644 index 00000000..4c62d995 --- /dev/null +++ b/api/handler/helper_test.go @@ -0,0 +1,128 @@ +package handler + +import ( + "bytes" + "encoding/json" + "io" + "mime/multipart" + "net/http" + "net/http/httptest" + "net/url" + "testing" + + "github.com/gin-gonic/gin" + "github.com/spf13/cast" + "github.com/stretchr/testify/require" + "opencsg.com/csghub-server/api/httpbase" + "opencsg.com/csghub-server/component" +) + +type GinTester struct { + ginHandler gin.HandlerFunc + ctx *gin.Context + response *httptest.ResponseRecorder + OKText string // text of httpbase.OK +} + +func NewGinTester() *GinTester { + response := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(response) + ctx.Request = &http.Request{ + URL: &url.URL{}, + } + + return &GinTester{ + ginHandler: nil, + ctx: ctx, + response: response, + OKText: "OK", + } +} + +func (g *GinTester) Handler(handler gin.HandlerFunc) { + g.ginHandler = handler +} + +func (g *GinTester) Execute() { + g.ginHandler(g.ctx) +} +func (g *GinTester) WithUser() *GinTester { + g.ctx.Set(httpbase.CurrentUserCtxVar, "u") + return g +} + +func (g *GinTester) WithParam(key string, value string) *GinTester { + params := g.ctx.Params + for i, param := range params { + if param.Key == key { + params[i].Value = value + return g + } + } + g.ctx.AddParam(key, value) + return g +} + +func (g *GinTester) WithKV(key string, value any) *GinTester { + g.ctx.Set(key, value) + return g +} + +func (g *GinTester) WithBody(t *testing.T, body any) *GinTester { + b, err := json.Marshal(body) + require.Nil(t, err) + g.ctx.Request.Body = io.NopCloser(bytes.NewBuffer(b)) + return g +} + +func (g *GinTester) WithMultipartForm(mf *multipart.Form) *GinTester { + g.ctx.Request.MultipartForm = mf + return g +} + +func (g *GinTester) WithQuery(key, value string) *GinTester { + q := g.ctx.Request.URL.Query() + q.Add(key, value) + g.ctx.Request.URL.RawQuery = q.Encode() + return g +} + +func (g *GinTester) AddPagination(page int, per int) *GinTester { + g.WithQuery("page", cast.ToString(page)) + g.WithQuery("per", cast.ToString(per)) + return g +} + +func (g *GinTester) ResponseEq(t *testing.T, code int, msg string, expected any) { + var r = struct { + Msg string `json:"msg"` + Data any `json:"data,omitempty"` + }{ + Msg: msg, + Data: expected, + } + b, err := json.Marshal(r) + require.NoError(t, err) + require.Equal(t, code, g.response.Code, g.response.Body.String()) + require.JSONEq(t, string(b), g.response.Body.String()) + +} + +func (g *GinTester) ResponseEqSimple(t *testing.T, code int, expected any) { + b, err := json.Marshal(expected) + require.NoError(t, err) + require.Equal(t, code, g.response.Code) + require.JSONEq(t, string(b), g.response.Body.String()) + +} + +func (g *GinTester) RequireUser(t *testing.T) { + // use a tmp ctx to test no user case + tmp := NewGinTester() + tmp.ctx.Params = g.ctx.Params + g.ginHandler(tmp.ctx) + tmp.ResponseEq(t, http.StatusUnauthorized, component.ErrUserNotFound.Error(), nil) + // add user to original test ctx now + _ = g.WithUser() + +} diff --git a/api/handler/repo.go b/api/handler/repo.go index 075673a7..b763ad83 100644 --- a/api/handler/repo.go +++ b/api/handler/repo.go @@ -29,12 +29,14 @@ func NewRepoHandler(config *config.Config) (*RepoHandler, error) { return nil, err } return &RepoHandler{ - c: uc, + c: uc, + deployStatusCheckInterval: 5 * time.Second, }, nil } type RepoHandler struct { - c component.RepoComponent + c component.RepoComponent + deployStatusCheckInterval time.Duration } // CreateRepoFile godoc @@ -521,7 +523,7 @@ func (h *RepoHandler) Tags(ctx *gin.Context) { func (h *RepoHandler) UpdateTags(ctx *gin.Context) { currentUser := httpbase.GetCurrentUser(ctx) if currentUser == "" { - httpbase.UnauthorizedError(ctx, httpbase.ErrorNeedLogin) + httpbase.UnauthorizedError(ctx, component.ErrUserNotFound) return } namespace, name, err := common.GetNamespaceAndNameFromContext(ctx) @@ -1695,7 +1697,7 @@ func (h *RepoHandler) DeployStatus(ctx *gin.Context) { slog.Info("deploy handler status request context done", slog.Any("error", ctx.Request.Context().Err())) return default: - time.Sleep(time.Second * 5) + time.Sleep(h.deployStatusCheckInterval) // user http request context instead of gin context, so that server knows the life cycle of the request _, status, instances, err := h.c.DeployStatus(ctx.Request.Context(), repoType, namespace, name, deployID) if err != nil { @@ -2136,7 +2138,7 @@ func (h *RepoHandler) ServerlessStatus(ctx *gin.Context) { slog.Info("deploy handler status request context done", slog.Any("error", ctx.Request.Context().Err())) return default: - time.Sleep(time.Second * 5) + time.Sleep(h.deployStatusCheckInterval) // user http request context instead of gin context, so that server knows the life cycle of the request _, status, instances, err := h.c.DeployStatus(ctx.Request.Context(), types.ModelRepo, namespace, name, deployID) if err != nil { diff --git a/api/handler/repo_test.go b/api/handler/repo_test.go index 6d252166..e935995e 100644 --- a/api/handler/repo_test.go +++ b/api/handler/repo_test.go @@ -1,132 +1,1172 @@ package handler import ( + "bytes" + "context" "encoding/json" "errors" + "io" + "mime/multipart" "net/http" "net/http/httptest" + "strings" "testing" + "time" + "github.com/alibabacloud-go/tea/tea" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" mockcomponent "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/component" + "opencsg.com/csghub-server/builder/deploy" + "opencsg.com/csghub-server/builder/store/database" "opencsg.com/csghub-server/common/types" "opencsg.com/csghub-server/component" ) +type RepoTester struct { + *GinTester + handler *RepoHandler + mocks struct { + repo *mockcomponent.MockRepoComponent + } +} + +func NewRepoTester(t *testing.T) *RepoTester { + tester := &RepoTester{GinTester: NewGinTester()} + tester.mocks.repo = mockcomponent.NewMockRepoComponent(t) + tester.handler = &RepoHandler{tester.mocks.repo, 0} + tester.WithParam("name", "r") + tester.WithParam("namespace", "u") + return tester + +} + +func (rt *RepoTester) WithHandleFunc(fn func(rp *RepoHandler) gin.HandlerFunc) *RepoTester { + rt.ginHandler = fn(rt.handler) + return rt +} + +func TestRepoHandler_CreateFile(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.CreateFile + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().CreateFile(tester.ctx, &types.CreateFileReq{ + Message: "foo", + Branch: "main", + Content: "bar", + Username: "u", + Namespace: "u", + Name: "r", + CurrentUser: "u", + FilePath: "foo", + }).Return(&types.CreateFileResp{}, nil) + tester.WithParam("file_path", "foo") + req := &types.CreateFileReq{ + Message: "foo", + Branch: "main", + Content: "bar", + } + tester.WithBody(t, req) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, &types.CreateFileResp{}) + +} + +func TestRepoHandler_UpdateFile(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.UpdateFile + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().UpdateFile(tester.ctx, &types.UpdateFileReq{ + Message: "foo", + Branch: "main", + Content: "bar", + Username: "u", + Namespace: "u", + Name: "r", + CurrentUser: "u", + FilePath: "foo", + }).Return(&types.UpdateFileResp{}, nil) + tester.WithParam("file_path", "foo") + req := &types.CreateFileReq{ + Message: "foo", + Branch: "main", + Content: "bar", + } + tester.WithBody(t, req) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, &types.UpdateFileResp{}) + +} + +func TestRepoHandler_Commits(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.Commits + }) + + tester.WithUser() + tester.mocks.repo.EXPECT().Commits(tester.ctx, &types.GetCommitsReq{ + Namespace: "u", + Name: "r", + Ref: "main", + Page: 1, + Per: 10, + RepoType: types.ModelRepo, + CurrentUser: "u", + }).Return([]types.Commit{{ID: "c1"}}, &types.RepoPageOpts{Total: 100, PageCount: 1}, nil) + tester.WithParam("file_path", "foo") + tester.WithQuery("ref", "main") + tester.AddPagination(1, 10) + tester.WithKV("repo_type", types.ModelRepo) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, gin.H{ + "commits": []types.Commit{{ID: "c1"}}, + "total": 100, + "page_count": 1, + }) + +} + func TestRepoHandler_LastCommit(t *testing.T) { t.Run("forbidden", func(t *testing.T) { - comp := mockcomponent.NewMockRepoComponent(t) - h := &RepoHandler{comp} - - response := httptest.NewRecorder() - ginc, _ := gin.CreateTestContext(response) - ginc.AddParam("namespace", "user_name_1") - ginc.AddParam("name", "repo_name_1") + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.LastCommit + }) //user does not have permission to access repo - comp.EXPECT().LastCommit(mock.Anything, mock.Anything).Return(nil, component.ErrForbidden).Once() - h.LastCommit(ginc) - require.Equal(t, http.StatusForbidden, response.Code) + tester.mocks.repo.EXPECT().LastCommit(mock.Anything, mock.Anything).Return(nil, component.ErrForbidden).Once() + tester.Execute() + require.Equal(t, http.StatusForbidden, tester.response.Code) }) t.Run("server error", func(t *testing.T) { - comp := mockcomponent.NewMockRepoComponent(t) - h := &RepoHandler{comp} - - response := httptest.NewRecorder() - ginc, _ := gin.CreateTestContext(response) - ginc.AddParam("namespace", "user_name_1") - ginc.AddParam("name", "repo_name_1") - + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.LastCommit + }) commit := &types.Commit{} - comp.EXPECT().LastCommit(mock.Anything, mock.Anything).Return(commit, errors.New("custome error")).Once() - h.LastCommit(ginc) - require.Equal(t, http.StatusInternalServerError, response.Code) + tester.mocks.repo.EXPECT().LastCommit(mock.Anything, mock.Anything).Return(commit, errors.New("custome error")).Once() + tester.Execute() + require.Equal(t, http.StatusInternalServerError, tester.response.Code) }) t.Run("success", func(t *testing.T) { - comp := mockcomponent.NewMockRepoComponent(t) - h := &RepoHandler{comp} - - response := httptest.NewRecorder() - ginc, _ := gin.CreateTestContext(response) - ginc.AddParam("namespace", "user_name_1") - ginc.AddParam("name", "repo_name_1") + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.LastCommit + }) commit := &types.Commit{} - commit.AuthorName = "user_name_1" + commit.AuthorName = "u" commit.ID = uuid.New().String() - comp.EXPECT().LastCommit(mock.Anything, mock.Anything).Return(commit, nil).Once() - h.LastCommit(ginc) - require.Equal(t, http.StatusOK, response.Code) + tester.mocks.repo.EXPECT().LastCommit(mock.Anything, mock.Anything).Return(commit, nil).Once() + tester.Execute() + require.Equal(t, http.StatusOK, tester.response.Code) var r = struct { Code int `json:"code,omitempty"` Msg string `json:"msg"` Data *types.Commit `json:"data,omitempty"` }{} - err := json.Unmarshal(response.Body.Bytes(), &r) + err := json.Unmarshal(tester.response.Body.Bytes(), &r) require.Empty(t, err) require.Equal(t, commit.ID, r.Data.ID) }) } -func TestRepoHandler_Tree(t *testing.T) { - t.Run("forbidden", func(t *testing.T) { - comp := mockcomponent.NewMockRepoComponent(t) - h := &RepoHandler{comp} +func TestRepoHandler_FileRaw(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.FileRaw + }) - response := httptest.NewRecorder() - ginc, _ := gin.CreateTestContext(response) - ginc.AddParam("namespace", "user_name_1") - ginc.AddParam("name", "repo_name_1") + tester.WithUser() + tester.mocks.repo.EXPECT().FileRaw(tester.ctx, &types.GetFileReq{ + Namespace: "u", + Name: "r", + Ref: "main", + RepoType: types.ModelRepo, + CurrentUser: "u", + Path: "foo", + }).Return("data", nil) + tester.WithParam("file_path", "foo") + tester.WithQuery("ref", "main") + tester.WithKV("repo_type", types.ModelRepo) - //user does not have permission to access repo - comp.EXPECT().Tree(mock.Anything, mock.Anything).Return(nil, component.ErrForbidden).Once() - h.Tree(ginc) - require.Equal(t, http.StatusForbidden, response.Code) + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, "data") + +} + +func TestRepoHandler_FileInfo(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.FileInfo }) - t.Run("server error", func(t *testing.T) { - comp := mockcomponent.NewMockRepoComponent(t) - h := &RepoHandler{comp} + tester.WithUser() + tester.mocks.repo.EXPECT().FileInfo(tester.ctx, &types.GetFileReq{ + Namespace: "u", + Name: "r", + Ref: "main", + RepoType: types.ModelRepo, + Path: "foo", + CurrentUser: "u", + }).Return(&types.File{Name: "foo.go"}, nil) + tester.WithParam("file_path", "foo") + tester.WithQuery("ref", "main") + tester.WithKV("repo_type", types.ModelRepo) - response := httptest.NewRecorder() - ginc, _ := gin.CreateTestContext(response) - ginc.AddParam("namespace", "user_name_1") - ginc.AddParam("name", "repo_name_1") + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, &types.File{Name: "foo.go"}) - comp.EXPECT().Tree(mock.Anything, mock.Anything).Return(nil, errors.New("custome error")).Once() - h.Tree(ginc) - require.Equal(t, http.StatusInternalServerError, response.Code) +} + +func TestRepoHandler_DownloadFile(t *testing.T) { + + t.Run("lfs file", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DownloadFile + }) + + tester.WithUser() + tester.mocks.repo.EXPECT().DownloadFile(tester.ctx, &types.GetFileReq{ + Namespace: "u", + Name: "r", + Ref: "main", + RepoType: types.ModelRepo, + CurrentUser: "u", + Path: "foo", + Lfs: true, + }, "u").Return(nil, 100, "foo", nil) + tester.WithParam("file_path", "foo") + tester.WithQuery("ref", "main") + tester.WithQuery("lfs", "true") + tester.WithKV("repo_type", types.ModelRepo) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, "foo") }) - t.Run("success", func(t *testing.T) { - comp := mockcomponent.NewMockRepoComponent(t) - h := &RepoHandler{comp} + t.Run("normal file", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DownloadFile + }) + + tester.WithUser() + tester.mocks.repo.EXPECT().DownloadFile(tester.ctx, &types.GetFileReq{ + Namespace: "u", + Name: "r", + Ref: "main", + RepoType: types.ModelRepo, + CurrentUser: "u", + Path: "foo", + }, "u").Return( + io.NopCloser(bytes.NewBuffer([]byte("bar"))), 100, "foo", nil, + ) + tester.WithParam("file_path", "foo") + tester.WithQuery("ref", "main") + tester.WithKV("repo_type", types.ModelRepo) + + tester.Execute() + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "application/octet-stream", headers.Get("Content-Type")) + require.Equal(t, `attachment; filename="foo"`, headers.Get("Content-Disposition")) + require.Equal(t, "100", headers.Get("Content-Length")) + r := tester.response.Body.String() + require.Equal(t, "bar", r) + }) + +} + +func TestRepoHandler_Branches(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.Branches + }) + + tester.WithUser() + tester.mocks.repo.EXPECT().Branches(tester.ctx, &types.GetBranchesReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + Per: 10, + Page: 1, + }).Return([]types.Branch{{Name: "main"}}, nil) + tester.WithKV("repo_type", types.ModelRepo) + tester.AddPagination(1, 10) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, []types.Branch{{Name: "main"}}) + +} + +func TestRepoHandler_Tags(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.Tags + }) + + tester.WithUser() + tester.mocks.repo.EXPECT().Tags(tester.ctx, &types.GetTagsReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + }).Return([]database.Tag{{Name: "main"}}, nil) + tester.WithKV("repo_type", types.ModelRepo) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, []database.Tag{{Name: "main"}}) + +} - response := httptest.NewRecorder() - ginc, _ := gin.CreateTestContext(response) - ginc.AddParam("namespace", "user_name_1") - ginc.AddParam("name", "repo_name_1") +func TestRepoHandler_UpdateTags(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.UpdateTags + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().UpdateTags( + tester.ctx, "u", "r", types.ModelRepo, + "cat", "u", []string{"foo", "bar"}, + ).Return(nil) + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("category", "cat") + tester.WithBody(t, []string{"foo", "bar"}) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, nil) + +} + +func TestRepoHandler_Tree(t *testing.T) { + t.Run("forbidden", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.Tree + }) + //user does not have permission to access repo + tester.mocks.repo.EXPECT().Tree(mock.Anything, mock.Anything).Return(nil, component.ErrForbidden).Once() + tester.Execute() + require.Equal(t, http.StatusForbidden, tester.response.Code) + }) + + t.Run("server error", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.Tree + }) + tester.mocks.repo.EXPECT().Tree(mock.Anything, mock.Anything).Return(nil, errors.New("custome error")).Once() + tester.Execute() + require.Equal(t, http.StatusInternalServerError, tester.response.Code) + }) + + t.Run("success", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.Tree + }) var tree []*types.File - comp.EXPECT().Tree(mock.Anything, mock.Anything).Return(tree, nil).Once() - h.Tree(ginc) - require.Equal(t, http.StatusOK, response.Code) + tester.mocks.repo.EXPECT().Tree(mock.Anything, mock.Anything).Return(tree, nil).Once() + tester.Execute() + require.Equal(t, http.StatusOK, tester.response.Code) var r = struct { Code int `json:"code,omitempty"` Msg string `json:"msg"` Data []*types.File `json:"data,omitempty"` }{} - err := json.Unmarshal(response.Body.Bytes(), &r) + err := json.Unmarshal(tester.response.Body.Bytes(), &r) require.Empty(t, err) require.Equal(t, tree, r.Data) }) } + +func TestRepoHandler_UpdateDownloads(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.UpdateDownloads + }) + + tester.WithUser() + tester.mocks.repo.EXPECT().UpdateDownloads( + tester.ctx, &types.UpdateDownloadsReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + ReqDate: "2002-02-01", + Date: time.Date(2002, 2, 1, 0, 0, 0, 0, time.UTC), + }, + ).Return(nil) + tester.WithKV("repo_type", types.ModelRepo) + tester.WithBody(t, &types.UpdateDownloadsReq{ReqDate: "2002-02-01"}) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, nil) + +} + +func TestRepoHandler_IncrDownloads(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.IncrDownloads + }) + + tester.WithUser() + tester.mocks.repo.EXPECT().IncrDownloads( + tester.ctx, types.ModelRepo, "u", "r", + ).Return(nil) + tester.WithKV("repo_type", types.ModelRepo) + tester.WithBody(t, &types.UpdateDownloadsReq{ReqDate: "2002-02-01"}) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, nil) + +} + +func TestRepoHandler_UploadFile(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.UploadFile + }) + tester.RequireUser(t) + + bodyBuffer := new(bytes.Buffer) + mw := multipart.NewWriter(bodyBuffer) + err := mw.WriteField("file_path", "foo") + require.NoError(t, err) + err = mw.WriteField("message", "msg") + require.NoError(t, err) + err = mw.WriteField("branch", "main") + require.NoError(t, err) + part, err := mw.CreateFormFile("file", "file") + if err != nil { + t.Fatal(err) + } + file := strings.NewReader(`data`) + _, err = io.Copy(part, file) + require.NoError(t, err) + mw.Close() + req := httptest.NewRequest(http.MethodPost, "/", bodyBuffer) + req.Header.Set("Content-Type", mw.FormDataContentType()) + err = req.ParseMultipartForm(20) + require.NoError(t, err) + tester.WithMultipartForm(req.MultipartForm) + + tester.mocks.repo.EXPECT().UploadFile( + tester.ctx, &types.CreateFileReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + Content: "ZGF0YQ==", + OriginalContent: []byte("data"), + CurrentUser: "u", + Message: "msg", + Branch: "main", + FilePath: "foo", + Username: "u", + }, + ).Return(nil) + tester.WithKV("repo_type", types.ModelRepo) + + tester.Execute() + tester.ResponseEq(t, http.StatusOK, tester.OKText, nil) + +} + +func TestRepoHandler_SDKListFiles(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.SDKListFiles + }) + + tester.WithUser() + tester.WithParam("ref", "main") + tester.WithParam("branch_mapped", "main_main") + tester.WithKV("repo_type", types.ModelRepo) + tester.mocks.repo.EXPECT().SDKListFiles( + tester.ctx, types.ModelRepo, "u", "r", "main_main", "u", + ).Return(&types.SDKFiles{ID: "f1"}, nil) + + tester.Execute() + tester.ResponseEqSimple(t, http.StatusOK, &types.SDKFiles{ID: "f1"}) +} + +func TestRepoHandler_HandleDownload(t *testing.T) { + + t.Run("lfs file", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.SDKDownload + }) + + tester.WithUser() + tester.WithParam("ref", "main") + tester.WithParam("branch_mapped", "main_main") + tester.WithParam("file_path", "foo") + tester.WithKV("repo_type", types.ModelRepo) + req := &types.GetFileReq{ + Namespace: "u", + Name: "r", + Path: "foo", + Ref: "main_main", + Lfs: false, + SaveAs: "foo", + RepoType: types.ModelRepo, + } + tester.mocks.repo.EXPECT().IsLfs(tester.ctx, req).Return(true, 100, nil) + reqnew := *req + reqnew.Lfs = true + tester.mocks.repo.EXPECT().SDKDownloadFile(tester.ctx, &reqnew, "u").Return( + nil, 100, "url", nil, + ) + + tester.Execute() + + // redirected + require.Equal(t, http.StatusOK, tester.response.Code) + resp := tester.response.Result() + defer resp.Body.Close() + lc, err := resp.Location() + require.NoError(t, err) + require.Equal(t, "/url", lc.String()) + }) + + t.Run("normal file", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.SDKDownload + }) + + tester.WithUser() + tester.WithParam("ref", "main") + tester.WithParam("branch_mapped", "main_main") + tester.WithParam("file_path", "foo") + tester.WithKV("repo_type", types.ModelRepo) + req := &types.GetFileReq{ + Namespace: "u", + Name: "r", + Path: "foo", + Ref: "main_main", + Lfs: false, + SaveAs: "foo", + RepoType: types.ModelRepo, + } + tester.mocks.repo.EXPECT().IsLfs(tester.ctx, req).Return(false, 100, nil) + tester.mocks.repo.EXPECT().SDKDownloadFile(tester.ctx, req, "u").Return( + io.NopCloser(bytes.NewBuffer([]byte("bar"))), 100, "url", nil, + ) + + tester.Execute() + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "application/octet-stream", headers.Get("Content-Type")) + require.Equal(t, `attachment; filename="foo"`, headers.Get("Content-Disposition")) + require.Equal(t, "100", headers.Get("Content-Length")) + r := tester.response.Body.String() + require.Equal(t, "bar", r) + }) +} + +func TestRepoHandler_HeadSDKDownload(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.HeadSDKDownload + }) + + tester.WithUser() + tester.WithParam("file_path", "foo") + tester.WithParam("branch", "main") + tester.WithKV("repo_type", types.ModelRepo) + tester.mocks.repo.EXPECT().HeadDownloadFile( + tester.ctx, &types.GetFileReq{ + Namespace: "u", + Name: "r", + Path: "foo", + Ref: "main", + SaveAs: "foo", + RepoType: types.ModelRepo, + }, "u", + ).Return(&types.File{Size: 100, SHA: "def"}, &types.Commit{ID: "abc"}, nil) + + tester.Execute() + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "100", headers.Get("Content-Length")) + require.Equal(t, "abc", headers.Get("X-Repo-Commit")) + require.Equal(t, "def", headers.Get("ETag")) +} + +func TestRepoHandler_CommitWithDiff(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.CommitWithDiff + }) + + tester.WithUser() + tester.WithParam("commit_id", "foo") + tester.WithKV("repo_type", types.ModelRepo) + tester.mocks.repo.EXPECT().GetCommitWithDiff( + tester.ctx, &types.GetCommitsReq{ + Namespace: "u", + Name: "r", + Ref: "foo", + RepoType: types.ModelRepo, + CurrentUser: "u", + }, + ).Return(&types.CommitResponse{Commit: &types.Commit{ID: "foo"}}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, &types.CommitResponse{Commit: &types.Commit{ID: "foo"}}) +} + +func TestRepoHandler_CreateMirror(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.CreateMirror + }) + + tester.RequireUser(t) + tester.WithKV("repo_type", types.ModelRepo) + tester.WithBody(t, &types.CreateMirrorReq{ + SourceUrl: "https://foo.com", + MirrorSourceID: 12, + }) + tester.mocks.repo.EXPECT().CreateMirror( + tester.ctx, types.CreateMirrorReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + SourceUrl: "https://foo.com", + MirrorSourceID: 12, + }, + ).Return(&database.Mirror{ID: 123}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, &database.Mirror{ID: 123}) +} + +func TestRepoHandler_MirrorFromSaas(t *testing.T) { + t.Run("valid", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.MirrorFromSaas + }) + tester.RequireUser(t) + + tester.WithParam("namespace", types.OpenCSGPrefix+"repo") + tester.WithKV("repo_type", types.ModelRepo) + tester.mocks.repo.EXPECT().MirrorFromSaas( + tester.ctx, "CSG_repo", "r", "u", types.ModelRepo, + ).Return(nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) + }) + + t.Run("invalid", func(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.MirrorFromSaas + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.Execute() + tester.ResponseEq(t, 400, "Repo could not be mirrored", nil) + }) +} + +func TestRepoHandler_GetMirror(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.GetMirror + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.mocks.repo.EXPECT().GetMirror( + tester.ctx, types.GetMirrorReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + }, + ).Return(&database.Mirror{ID: 11}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, &database.Mirror{ID: 11}) +} + +func TestRepoHandler_UpdateMirror(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.UpdateMirror + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithBody(t, &types.UpdateMirrorReq{ + MirrorSourceID: 123, + SourceUrl: "foo", + }) + tester.mocks.repo.EXPECT().UpdateMirror( + tester.ctx, types.UpdateMirrorReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + MirrorSourceID: 123, + SourceUrl: "foo", + SourceRepoPath: "foo", + }, + ).Return(&database.Mirror{ID: 11}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, &database.Mirror{ID: 11}) +} + +func TestRepoHandler_DeleteMirror(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DeleteMirror + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.mocks.repo.EXPECT().DeleteMirror( + tester.ctx, types.DeleteMirrorReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + }, + ).Return(nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestRepoHandler_RuntimeFrameworkList(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.RuntimeFrameworkList + }) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithQuery("deploy_type", "1") + tester.mocks.repo.EXPECT().ListRuntimeFramework( + tester.ctx, types.ModelRepo, "u", "r", 1, + ).Return([]types.RuntimeFramework{{FrameName: "f1"}}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, []types.RuntimeFramework{{FrameName: "f1"}}) +} + +func TestRepoHandler_RuntimeFrameworkCreate(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.RuntimeFrameworkCreate + }) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithBody(t, &types.RuntimeFrameworkReq{ + FrameName: "f1", + }) + tester.mocks.repo.EXPECT().CreateRuntimeFramework( + tester.ctx, &types.RuntimeFrameworkReq{FrameName: "f1"}, + ).Return(&types.RuntimeFramework{FrameName: "f1"}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, &types.RuntimeFramework{FrameName: "f1"}) +} + +func TestRepoHandler_RuntimeFrameworkUpdate(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.RuntimeFrameworkUpdate + }) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithBody(t, &types.RuntimeFrameworkReq{ + FrameName: "f1", + }) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().UpdateRuntimeFramework( + tester.ctx, int64(1), &types.RuntimeFrameworkReq{FrameName: "f1"}, + ).Return(&types.RuntimeFramework{FrameName: "f1"}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, &types.RuntimeFramework{FrameName: "f1"}) +} + +func TestRepoHandler_RuntimeFrameworkDelete(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.RuntimeFrameworkDelete + }) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().DeleteRuntimeFramework( + tester.ctx, int64(1), + ).Return(nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestRepoHandler_DeployList(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DeployList + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.mocks.repo.EXPECT().ListDeploy( + tester.ctx, types.ModelRepo, "u", "r", "u", + ).Return([]types.DeployRepo{{DeployName: "n"}}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, []types.DeployRepo{{DeployName: "n"}}) +} + +func TestRepoHandler_DeployDetail(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DeployDetail + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().DeployDetail( + tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.InferenceType, + }, + ).Return(&types.DeployRepo{DeployName: "n"}, nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, &types.DeployRepo{DeployName: "n"}) +} + +func TestRepoHandler_DeployInstanceLogs(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DeployInstanceLogs + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.WithParam("instance", "ii") + runlogChan := make(chan string) + tester.mocks.repo.EXPECT().DeployInstanceLogs( + mock.Anything, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.InferenceType, + InstanceName: "ii", + }, + ).Return(deploy.NewMultiLogReader(nil, runlogChan), nil) + cc, cancel := context.WithCancel(tester.ctx.Request.Context()) + tester.ctx.Request = tester.ctx.Request.WithContext(cc) + go func() { + runlogChan <- "foo" + runlogChan <- "bar" + close(runlogChan) + cancel() + }() + + tester.Execute() + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "text/event-stream", headers.Get("Content-Type")) + require.Equal(t, "no-cache", headers.Get("Cache-Control")) + require.Equal(t, "keep-alive", headers.Get("Connection")) + require.Equal(t, "chunked", headers.Get("Transfer-Encoding")) + require.Equal( + t, "event:Container\ndata:foo\n\nevent:Container\ndata:bar\n\n", + tester.response.Body.String(), + ) + +} + +func TestRepoHandler_DeployStatus(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DeployStatus + }) + tester.handler.deployStatusCheckInterval = 0 + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().AllowAccessDeploy( + tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.InferenceType, + }, + ).Return(true, nil) + cc, cancel := context.WithCancel(tester.ctx.Request.Context()) + tester.ctx.Request = tester.ctx.Request.WithContext(cc) + tester.mocks.repo.EXPECT().DeployStatus( + mock.Anything, types.ModelRepo, "u", "r", int64(1), + ).Return("", "s1", []types.Instance{{Name: "i1"}}, nil).Once() + tester.mocks.repo.EXPECT().DeployStatus( + mock.Anything, types.ModelRepo, "u", "r", int64(1), + ).RunAndReturn(func(ctx context.Context, rt types.RepositoryType, s1, s2 string, i int64) (string, string, []types.Instance, error) { + cancel() + return "", "s3", nil, nil + }).Once() + + tester.Execute() + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "text/event-stream", headers.Get("Content-Type")) + require.Equal(t, "no-cache", headers.Get("Cache-Control")) + require.Equal(t, "keep-alive", headers.Get("Connection")) + require.Equal(t, "chunked", headers.Get("Transfer-Encoding")) + require.Equal( + t, "event:status\ndata:{\"status\":\"s1\",\"details\":[{\"name\":\"i1\",\"status\":\"\"}]}\n\nevent:status\ndata:{\"status\":\"s3\",\"details\":null}\n\n", + tester.response.Body.String(), + ) + +} + +func TestRepoHandler_SyncMirror(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.SyncMirror + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().SyncMirror( + tester.ctx, types.ModelRepo, "u", "r", "u", + ).Return(nil) + + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestRepoHandler_MirrorProgress(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.MirrorProgress + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().MirrorProgress( + tester.ctx, types.ModelRepo, "u", "r", "u", + ).Return([]types.LFSSyncProgressResp{{Oid: "o1"}}, nil) + + tester.Execute() + tester.ResponseEq( + t, 200, tester.OKText, []types.LFSSyncProgressResp{{Oid: "o1"}}, + ) +} + +func TestRepoHandler_DeployUpdate(t *testing.T) { + t.Run("not admin", func(t *testing.T) { + + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DeployUpdate + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().AllowAdminAccess(tester.ctx, types.ModelRepo, "u", "r", "u").Return(false, nil) + tester.Execute() + tester.ResponseEq( + t, 401, "user not allowed to update deploy", nil, + ) + }) + + t.Run("admin", func(t *testing.T) { + + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.DeployUpdate + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().AllowAdminAccess(tester.ctx, types.ModelRepo, "u", "r", "u").Return(true, nil) + tester.WithBody(t, &types.DeployUpdateReq{ + MinReplica: tea.Int(1), + MaxReplica: tea.Int(5), + }) + tester.mocks.repo.EXPECT().DeployUpdate(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.InferenceType, + }, &types.DeployUpdateReq{ + MinReplica: tea.Int(1), + MaxReplica: tea.Int(5), + }).Return(nil) + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) + }) +} + +func TestRepoHandler_RuntimeFrameworkListWithType(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.RuntimeFrameworkListWithType + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.mocks.repo.EXPECT().ListRuntimeFrameworkWithType( + tester.ctx, types.InferenceType, + ).Return([]types.RuntimeFramework{{FrameName: "f1"}}, nil) + + tester.Execute() + tester.ResponseEq( + t, 200, tester.OKText, []types.RuntimeFramework{{FrameName: "f1"}}, + ) +} + +func TestRepoHandler_ServerlessDetail(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.ServerlessDetail + }) + tester.RequireUser(t) + + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().DeployDetail( + tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.ServerlessType, + }, + ).Return(&types.DeployRepo{Name: "r"}, nil) + + tester.Execute() + tester.ResponseEq( + t, 200, tester.OKText, &types.DeployRepo{Name: "r"}, + ) +} + +func TestRepoHandler_ServerlessLogs(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.ServerlessLogs + }) + + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.WithParam("instance", "ii") + runlogChan := make(chan string) + tester.mocks.repo.EXPECT().DeployInstanceLogs( + mock.Anything, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.ServerlessType, + InstanceName: "ii", + }, + ).Return(deploy.NewMultiLogReader(nil, runlogChan), nil) + cc, cancel := context.WithCancel(tester.ctx.Request.Context()) + tester.ctx.Request = tester.ctx.Request.WithContext(cc) + go func() { + runlogChan <- "foo" + runlogChan <- "bar" + close(runlogChan) + cancel() + }() + + tester.Execute() + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "text/event-stream", headers.Get("Content-Type")) + require.Equal(t, "no-cache", headers.Get("Cache-Control")) + require.Equal(t, "keep-alive", headers.Get("Connection")) + require.Equal(t, "chunked", headers.Get("Transfer-Encoding")) + require.Equal( + t, "event:Container\ndata:foo\n\nevent:Container\ndata:bar\n\n", + tester.response.Body.String(), + ) + +} + +func TestRepoHandler_ServerlessStatus(t *testing.T) { + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.ServerlessStatus + }) + tester.handler.deployStatusCheckInterval = 0 + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.mocks.repo.EXPECT().AllowAccessDeploy( + tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.ServerlessType, + }, + ).Return(true, nil) + cc, cancel := context.WithCancel(tester.ctx.Request.Context()) + tester.ctx.Request = tester.ctx.Request.WithContext(cc) + tester.mocks.repo.EXPECT().DeployStatus( + mock.Anything, types.ModelRepo, "u", "r", int64(1), + ).Return("", "s1", []types.Instance{{Name: "i1"}}, nil).Once() + tester.mocks.repo.EXPECT().DeployStatus( + mock.Anything, types.ModelRepo, "u", "r", int64(1), + ).RunAndReturn(func(ctx context.Context, rt types.RepositoryType, s1, s2 string, i int64) (string, string, []types.Instance, error) { + cancel() + return "", "s3", nil, nil + }).Once() + + tester.Execute() + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "text/event-stream", headers.Get("Content-Type")) + require.Equal(t, "no-cache", headers.Get("Cache-Control")) + require.Equal(t, "keep-alive", headers.Get("Connection")) + require.Equal(t, "chunked", headers.Get("Transfer-Encoding")) + require.Equal( + t, "event:status\ndata:{\"status\":\"s1\",\"details\":[{\"name\":\"i1\",\"status\":\"\"}]}\n\nevent:status\ndata:{\"status\":\"s3\",\"details\":null}\n\n", + tester.response.Body.String(), + ) + +} + +func TestRepoHandler_ServelessUpdate(t *testing.T) { + + tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { + return rp.ServerlessUpdate + }) + tester.RequireUser(t) + + tester.WithKV("repo_type", types.ModelRepo) + tester.WithParam("id", "1") + tester.WithBody(t, &types.DeployUpdateReq{ + MinReplica: tea.Int(1), + MaxReplica: tea.Int(5), + }) + tester.mocks.repo.EXPECT().DeployUpdate(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.ServerlessType, + }, &types.DeployUpdateReq{ + MinReplica: tea.Int(1), + MaxReplica: tea.Int(5), + }).Return(nil) + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} diff --git a/api/httpbase/user.go b/api/httpbase/user.go index 8ef33fce..f94de028 100644 --- a/api/httpbase/user.go +++ b/api/httpbase/user.go @@ -1,6 +1,8 @@ package httpbase -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" +) const ( CurrentUserCtxVar = "currentUser" From 48b0bc2f90a22b60b35a3b91e6c374d6f6c25144 Mon Sep 17 00:00:00 2001 From: yiling Date: Tue, 24 Dec 2024 17:30:10 +0800 Subject: [PATCH 2/5] fix repo handler test --- api/handler/repo_test.go | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/api/handler/repo_test.go b/api/handler/repo_test.go index e935995e..557e694d 100644 --- a/api/handler/repo_test.go +++ b/api/handler/repo_test.go @@ -585,14 +585,13 @@ func TestRepoHandler_HeadSDKDownload(t *testing.T) { SaveAs: "foo", RepoType: types.ModelRepo, }, "u", - ).Return(&types.File{Size: 100, SHA: "def"}, &types.Commit{ID: "abc"}, nil) + ).Return(&types.File{Size: 100, SHA: "def"}, nil) tester.Execute() require.Equal(t, 200, tester.response.Code) headers := tester.response.Header() require.Equal(t, "100", headers.Get("Content-Length")) - require.Equal(t, "abc", headers.Get("X-Repo-Commit")) - require.Equal(t, "def", headers.Get("ETag")) + require.Equal(t, "def", headers.Get("X-Repo-Commit")) } func TestRepoHandler_CommitWithDiff(t *testing.T) { @@ -947,24 +946,6 @@ func TestRepoHandler_SyncMirror(t *testing.T) { tester.ResponseEq(t, 200, tester.OKText, nil) } -func TestRepoHandler_MirrorProgress(t *testing.T) { - tester := NewRepoTester(t).WithHandleFunc(func(rp *RepoHandler) gin.HandlerFunc { - return rp.MirrorProgress - }) - tester.RequireUser(t) - - tester.WithKV("repo_type", types.ModelRepo) - tester.WithParam("id", "1") - tester.mocks.repo.EXPECT().MirrorProgress( - tester.ctx, types.ModelRepo, "u", "r", "u", - ).Return([]types.LFSSyncProgressResp{{Oid: "o1"}}, nil) - - tester.Execute() - tester.ResponseEq( - t, 200, tester.OKText, []types.LFSSyncProgressResp{{Oid: "o1"}}, - ) -} - func TestRepoHandler_DeployUpdate(t *testing.T) { t.Run("not admin", func(t *testing.T) { From 26d0ab73482ed83a8bf62e4cff5275d060529d62 Mon Sep 17 00:00:00 2001 From: "yiling.ji" Date: Thu, 19 Dec 2024 07:52:23 +0000 Subject: [PATCH 3/5] Merge branch 'feature/handler_tests' into 'main' Add code handler tests and fix prompt component cycle import See merge request product/starhub/starhub-server!741 --- .mockery.yaml | 2 + .../component/mock_CodeComponent.go | 460 ++++++ .../component/mock_PromptComponent.go | 1444 +++++++++++++++++ api/handler/code.go | 24 +- api/handler/code_test.go | 149 ++ api/handler/prompt.go | 4 +- builder/sensitive/aliyun_green.go | 16 +- common/types/prompt.go | 56 +- component/prompt.go | 87 +- component/prompt_test.go | 16 +- 10 files changed, 2149 insertions(+), 109 deletions(-) create mode 100644 _mocks/opencsg.com/csghub-server/component/mock_CodeComponent.go create mode 100644 _mocks/opencsg.com/csghub-server/component/mock_PromptComponent.go create mode 100644 api/handler/code_test.go diff --git a/.mockery.yaml b/.mockery.yaml index cbda4ba7..64fb75b7 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -17,6 +17,8 @@ packages: SpaceComponent: RuntimeArchitectureComponent: SensitiveComponent: + CodeComponent: + PromptComponent: opencsg.com/csghub-server/user/component: config: interfaces: diff --git a/_mocks/opencsg.com/csghub-server/component/mock_CodeComponent.go b/_mocks/opencsg.com/csghub-server/component/mock_CodeComponent.go new file mode 100644 index 00000000..536c106c --- /dev/null +++ b/_mocks/opencsg.com/csghub-server/component/mock_CodeComponent.go @@ -0,0 +1,460 @@ +// Code generated by mockery v2.49.1. DO NOT EDIT. + +package component + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" + types "opencsg.com/csghub-server/common/types" +) + +// MockCodeComponent is an autogenerated mock type for the CodeComponent type +type MockCodeComponent struct { + mock.Mock +} + +type MockCodeComponent_Expecter struct { + mock *mock.Mock +} + +func (_m *MockCodeComponent) EXPECT() *MockCodeComponent_Expecter { + return &MockCodeComponent_Expecter{mock: &_m.Mock} +} + +// Create provides a mock function with given fields: ctx, req +func (_m *MockCodeComponent) Create(ctx context.Context, req *types.CreateCodeReq) (*types.Code, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Create") + } + + var r0 *types.Code + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *types.CreateCodeReq) (*types.Code, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.CreateCodeReq) *types.Code); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Code) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.CreateCodeReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockCodeComponent_Create_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Create' +type MockCodeComponent_Create_Call struct { + *mock.Call +} + +// Create is a helper method to define mock.On call +// - ctx context.Context +// - req *types.CreateCodeReq +func (_e *MockCodeComponent_Expecter) Create(ctx interface{}, req interface{}) *MockCodeComponent_Create_Call { + return &MockCodeComponent_Create_Call{Call: _e.mock.On("Create", ctx, req)} +} + +func (_c *MockCodeComponent_Create_Call) Run(run func(ctx context.Context, req *types.CreateCodeReq)) *MockCodeComponent_Create_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.CreateCodeReq)) + }) + return _c +} + +func (_c *MockCodeComponent_Create_Call) Return(_a0 *types.Code, _a1 error) *MockCodeComponent_Create_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockCodeComponent_Create_Call) RunAndReturn(run func(context.Context, *types.CreateCodeReq) (*types.Code, error)) *MockCodeComponent_Create_Call { + _c.Call.Return(run) + return _c +} + +// Delete provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockCodeComponent) Delete(ctx context.Context, namespace string, name string, currentUser string) error { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for Delete") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) error); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockCodeComponent_Delete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Delete' +type MockCodeComponent_Delete_Call struct { + *mock.Call +} + +// Delete is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockCodeComponent_Expecter) Delete(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockCodeComponent_Delete_Call { + return &MockCodeComponent_Delete_Call{Call: _e.mock.On("Delete", ctx, namespace, name, currentUser)} +} + +func (_c *MockCodeComponent_Delete_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockCodeComponent_Delete_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockCodeComponent_Delete_Call) Return(_a0 error) *MockCodeComponent_Delete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockCodeComponent_Delete_Call) RunAndReturn(run func(context.Context, string, string, string) error) *MockCodeComponent_Delete_Call { + _c.Call.Return(run) + return _c +} + +// Index provides a mock function with given fields: ctx, filter, per, page +func (_m *MockCodeComponent) Index(ctx context.Context, filter *types.RepoFilter, per int, page int) ([]types.Code, int, error) { + ret := _m.Called(ctx, filter, per, page) + + if len(ret) == 0 { + panic("no return value specified for Index") + } + + var r0 []types.Code + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.RepoFilter, int, int) ([]types.Code, int, error)); ok { + return rf(ctx, filter, per, page) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.RepoFilter, int, int) []types.Code); ok { + r0 = rf(ctx, filter, per, page) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Code) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.RepoFilter, int, int) int); ok { + r1 = rf(ctx, filter, per, page) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.RepoFilter, int, int) error); ok { + r2 = rf(ctx, filter, per, page) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockCodeComponent_Index_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Index' +type MockCodeComponent_Index_Call struct { + *mock.Call +} + +// Index is a helper method to define mock.On call +// - ctx context.Context +// - filter *types.RepoFilter +// - per int +// - page int +func (_e *MockCodeComponent_Expecter) Index(ctx interface{}, filter interface{}, per interface{}, page interface{}) *MockCodeComponent_Index_Call { + return &MockCodeComponent_Index_Call{Call: _e.mock.On("Index", ctx, filter, per, page)} +} + +func (_c *MockCodeComponent_Index_Call) Run(run func(ctx context.Context, filter *types.RepoFilter, per int, page int)) *MockCodeComponent_Index_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.RepoFilter), args[2].(int), args[3].(int)) + }) + return _c +} + +func (_c *MockCodeComponent_Index_Call) Return(_a0 []types.Code, _a1 int, _a2 error) *MockCodeComponent_Index_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockCodeComponent_Index_Call) RunAndReturn(run func(context.Context, *types.RepoFilter, int, int) ([]types.Code, int, error)) *MockCodeComponent_Index_Call { + _c.Call.Return(run) + return _c +} + +// OrgCodes provides a mock function with given fields: ctx, req +func (_m *MockCodeComponent) OrgCodes(ctx context.Context, req *types.OrgCodesReq) ([]types.Code, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for OrgCodes") + } + + var r0 []types.Code + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.OrgCodesReq) ([]types.Code, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.OrgCodesReq) []types.Code); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Code) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.OrgCodesReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.OrgCodesReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockCodeComponent_OrgCodes_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'OrgCodes' +type MockCodeComponent_OrgCodes_Call struct { + *mock.Call +} + +// OrgCodes is a helper method to define mock.On call +// - ctx context.Context +// - req *types.OrgCodesReq +func (_e *MockCodeComponent_Expecter) OrgCodes(ctx interface{}, req interface{}) *MockCodeComponent_OrgCodes_Call { + return &MockCodeComponent_OrgCodes_Call{Call: _e.mock.On("OrgCodes", ctx, req)} +} + +func (_c *MockCodeComponent_OrgCodes_Call) Run(run func(ctx context.Context, req *types.OrgCodesReq)) *MockCodeComponent_OrgCodes_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.OrgCodesReq)) + }) + return _c +} + +func (_c *MockCodeComponent_OrgCodes_Call) Return(_a0 []types.Code, _a1 int, _a2 error) *MockCodeComponent_OrgCodes_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockCodeComponent_OrgCodes_Call) RunAndReturn(run func(context.Context, *types.OrgCodesReq) ([]types.Code, int, error)) *MockCodeComponent_OrgCodes_Call { + _c.Call.Return(run) + return _c +} + +// Relations provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockCodeComponent) Relations(ctx context.Context, namespace string, name string, currentUser string) (*types.Relations, error) { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for Relations") + } + + var r0 *types.Relations + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) (*types.Relations, error)); ok { + return rf(ctx, namespace, name, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) *types.Relations); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Relations) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string) error); ok { + r1 = rf(ctx, namespace, name, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockCodeComponent_Relations_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Relations' +type MockCodeComponent_Relations_Call struct { + *mock.Call +} + +// Relations is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockCodeComponent_Expecter) Relations(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockCodeComponent_Relations_Call { + return &MockCodeComponent_Relations_Call{Call: _e.mock.On("Relations", ctx, namespace, name, currentUser)} +} + +func (_c *MockCodeComponent_Relations_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockCodeComponent_Relations_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockCodeComponent_Relations_Call) Return(_a0 *types.Relations, _a1 error) *MockCodeComponent_Relations_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockCodeComponent_Relations_Call) RunAndReturn(run func(context.Context, string, string, string) (*types.Relations, error)) *MockCodeComponent_Relations_Call { + _c.Call.Return(run) + return _c +} + +// Show provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockCodeComponent) Show(ctx context.Context, namespace string, name string, currentUser string) (*types.Code, error) { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for Show") + } + + var r0 *types.Code + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) (*types.Code, error)); ok { + return rf(ctx, namespace, name, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) *types.Code); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Code) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string) error); ok { + r1 = rf(ctx, namespace, name, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockCodeComponent_Show_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Show' +type MockCodeComponent_Show_Call struct { + *mock.Call +} + +// Show is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockCodeComponent_Expecter) Show(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockCodeComponent_Show_Call { + return &MockCodeComponent_Show_Call{Call: _e.mock.On("Show", ctx, namespace, name, currentUser)} +} + +func (_c *MockCodeComponent_Show_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockCodeComponent_Show_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockCodeComponent_Show_Call) Return(_a0 *types.Code, _a1 error) *MockCodeComponent_Show_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockCodeComponent_Show_Call) RunAndReturn(run func(context.Context, string, string, string) (*types.Code, error)) *MockCodeComponent_Show_Call { + _c.Call.Return(run) + return _c +} + +// Update provides a mock function with given fields: ctx, req +func (_m *MockCodeComponent) Update(ctx context.Context, req *types.UpdateCodeReq) (*types.Code, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Update") + } + + var r0 *types.Code + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UpdateCodeReq) (*types.Code, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UpdateCodeReq) *types.Code); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Code) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UpdateCodeReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockCodeComponent_Update_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Update' +type MockCodeComponent_Update_Call struct { + *mock.Call +} + +// Update is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UpdateCodeReq +func (_e *MockCodeComponent_Expecter) Update(ctx interface{}, req interface{}) *MockCodeComponent_Update_Call { + return &MockCodeComponent_Update_Call{Call: _e.mock.On("Update", ctx, req)} +} + +func (_c *MockCodeComponent_Update_Call) Run(run func(ctx context.Context, req *types.UpdateCodeReq)) *MockCodeComponent_Update_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UpdateCodeReq)) + }) + return _c +} + +func (_c *MockCodeComponent_Update_Call) Return(_a0 *types.Code, _a1 error) *MockCodeComponent_Update_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockCodeComponent_Update_Call) RunAndReturn(run func(context.Context, *types.UpdateCodeReq) (*types.Code, error)) *MockCodeComponent_Update_Call { + _c.Call.Return(run) + return _c +} + +// NewMockCodeComponent creates a new instance of MockCodeComponent. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockCodeComponent(t interface { + mock.TestingT + Cleanup(func()) +}) *MockCodeComponent { + mock := &MockCodeComponent{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/_mocks/opencsg.com/csghub-server/component/mock_PromptComponent.go b/_mocks/opencsg.com/csghub-server/component/mock_PromptComponent.go new file mode 100644 index 00000000..a6e65a19 --- /dev/null +++ b/_mocks/opencsg.com/csghub-server/component/mock_PromptComponent.go @@ -0,0 +1,1444 @@ +// Code generated by mockery v2.49.1. DO NOT EDIT. + +package component + +import ( + context "context" + + gitserver "opencsg.com/csghub-server/builder/git/gitserver" + database "opencsg.com/csghub-server/builder/store/database" + + mock "github.com/stretchr/testify/mock" + + types "opencsg.com/csghub-server/common/types" +) + +// MockPromptComponent is an autogenerated mock type for the PromptComponent type +type MockPromptComponent struct { + mock.Mock +} + +type MockPromptComponent_Expecter struct { + mock *mock.Mock +} + +func (_m *MockPromptComponent) EXPECT() *MockPromptComponent_Expecter { + return &MockPromptComponent_Expecter{mock: &_m.Mock} +} + +// AddRelationModel provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) AddRelationModel(ctx context.Context, req types.RelationModel) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for AddRelationModel") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.RelationModel) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockPromptComponent_AddRelationModel_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AddRelationModel' +type MockPromptComponent_AddRelationModel_Call struct { + *mock.Call +} + +// AddRelationModel is a helper method to define mock.On call +// - ctx context.Context +// - req types.RelationModel +func (_e *MockPromptComponent_Expecter) AddRelationModel(ctx interface{}, req interface{}) *MockPromptComponent_AddRelationModel_Call { + return &MockPromptComponent_AddRelationModel_Call{Call: _e.mock.On("AddRelationModel", ctx, req)} +} + +func (_c *MockPromptComponent_AddRelationModel_Call) Run(run func(ctx context.Context, req types.RelationModel)) *MockPromptComponent_AddRelationModel_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.RelationModel)) + }) + return _c +} + +func (_c *MockPromptComponent_AddRelationModel_Call) Return(_a0 error) *MockPromptComponent_AddRelationModel_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockPromptComponent_AddRelationModel_Call) RunAndReturn(run func(context.Context, types.RelationModel) error) *MockPromptComponent_AddRelationModel_Call { + _c.Call.Return(run) + return _c +} + +// CreatePrompt provides a mock function with given fields: ctx, req, body +func (_m *MockPromptComponent) CreatePrompt(ctx context.Context, req types.PromptReq, body *types.CreatePromptReq) (*types.Prompt, error) { + ret := _m.Called(ctx, req, body) + + if len(ret) == 0 { + panic("no return value specified for CreatePrompt") + } + + var r0 *types.Prompt + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq, *types.CreatePromptReq) (*types.Prompt, error)); ok { + return rf(ctx, req, body) + } + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq, *types.CreatePromptReq) *types.Prompt); ok { + r0 = rf(ctx, req, body) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Prompt) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.PromptReq, *types.CreatePromptReq) error); ok { + r1 = rf(ctx, req, body) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_CreatePrompt_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CreatePrompt' +type MockPromptComponent_CreatePrompt_Call struct { + *mock.Call +} + +// CreatePrompt is a helper method to define mock.On call +// - ctx context.Context +// - req types.PromptReq +// - body *types.CreatePromptReq +func (_e *MockPromptComponent_Expecter) CreatePrompt(ctx interface{}, req interface{}, body interface{}) *MockPromptComponent_CreatePrompt_Call { + return &MockPromptComponent_CreatePrompt_Call{Call: _e.mock.On("CreatePrompt", ctx, req, body)} +} + +func (_c *MockPromptComponent_CreatePrompt_Call) Run(run func(ctx context.Context, req types.PromptReq, body *types.CreatePromptReq)) *MockPromptComponent_CreatePrompt_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.PromptReq), args[2].(*types.CreatePromptReq)) + }) + return _c +} + +func (_c *MockPromptComponent_CreatePrompt_Call) Return(_a0 *types.Prompt, _a1 error) *MockPromptComponent_CreatePrompt_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_CreatePrompt_Call) RunAndReturn(run func(context.Context, types.PromptReq, *types.CreatePromptReq) (*types.Prompt, error)) *MockPromptComponent_CreatePrompt_Call { + _c.Call.Return(run) + return _c +} + +// CreatePromptRepo provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) CreatePromptRepo(ctx context.Context, req *types.CreatePromptRepoReq) (*types.PromptRes, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for CreatePromptRepo") + } + + var r0 *types.PromptRes + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *types.CreatePromptRepoReq) (*types.PromptRes, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.CreatePromptRepoReq) *types.PromptRes); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.PromptRes) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.CreatePromptRepoReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_CreatePromptRepo_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CreatePromptRepo' +type MockPromptComponent_CreatePromptRepo_Call struct { + *mock.Call +} + +// CreatePromptRepo is a helper method to define mock.On call +// - ctx context.Context +// - req *types.CreatePromptRepoReq +func (_e *MockPromptComponent_Expecter) CreatePromptRepo(ctx interface{}, req interface{}) *MockPromptComponent_CreatePromptRepo_Call { + return &MockPromptComponent_CreatePromptRepo_Call{Call: _e.mock.On("CreatePromptRepo", ctx, req)} +} + +func (_c *MockPromptComponent_CreatePromptRepo_Call) Run(run func(ctx context.Context, req *types.CreatePromptRepoReq)) *MockPromptComponent_CreatePromptRepo_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.CreatePromptRepoReq)) + }) + return _c +} + +func (_c *MockPromptComponent_CreatePromptRepo_Call) Return(_a0 *types.PromptRes, _a1 error) *MockPromptComponent_CreatePromptRepo_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_CreatePromptRepo_Call) RunAndReturn(run func(context.Context, *types.CreatePromptRepoReq) (*types.PromptRes, error)) *MockPromptComponent_CreatePromptRepo_Call { + _c.Call.Return(run) + return _c +} + +// DelRelationModel provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) DelRelationModel(ctx context.Context, req types.RelationModel) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for DelRelationModel") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.RelationModel) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockPromptComponent_DelRelationModel_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DelRelationModel' +type MockPromptComponent_DelRelationModel_Call struct { + *mock.Call +} + +// DelRelationModel is a helper method to define mock.On call +// - ctx context.Context +// - req types.RelationModel +func (_e *MockPromptComponent_Expecter) DelRelationModel(ctx interface{}, req interface{}) *MockPromptComponent_DelRelationModel_Call { + return &MockPromptComponent_DelRelationModel_Call{Call: _e.mock.On("DelRelationModel", ctx, req)} +} + +func (_c *MockPromptComponent_DelRelationModel_Call) Run(run func(ctx context.Context, req types.RelationModel)) *MockPromptComponent_DelRelationModel_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.RelationModel)) + }) + return _c +} + +func (_c *MockPromptComponent_DelRelationModel_Call) Return(_a0 error) *MockPromptComponent_DelRelationModel_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockPromptComponent_DelRelationModel_Call) RunAndReturn(run func(context.Context, types.RelationModel) error) *MockPromptComponent_DelRelationModel_Call { + _c.Call.Return(run) + return _c +} + +// DeletePrompt provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) DeletePrompt(ctx context.Context, req types.PromptReq) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for DeletePrompt") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockPromptComponent_DeletePrompt_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeletePrompt' +type MockPromptComponent_DeletePrompt_Call struct { + *mock.Call +} + +// DeletePrompt is a helper method to define mock.On call +// - ctx context.Context +// - req types.PromptReq +func (_e *MockPromptComponent_Expecter) DeletePrompt(ctx interface{}, req interface{}) *MockPromptComponent_DeletePrompt_Call { + return &MockPromptComponent_DeletePrompt_Call{Call: _e.mock.On("DeletePrompt", ctx, req)} +} + +func (_c *MockPromptComponent_DeletePrompt_Call) Run(run func(ctx context.Context, req types.PromptReq)) *MockPromptComponent_DeletePrompt_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.PromptReq)) + }) + return _c +} + +func (_c *MockPromptComponent_DeletePrompt_Call) Return(_a0 error) *MockPromptComponent_DeletePrompt_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockPromptComponent_DeletePrompt_Call) RunAndReturn(run func(context.Context, types.PromptReq) error) *MockPromptComponent_DeletePrompt_Call { + _c.Call.Return(run) + return _c +} + +// GetConversation provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) GetConversation(ctx context.Context, req types.ConversationReq) (*database.PromptConversation, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for GetConversation") + } + + var r0 *database.PromptConversation + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationReq) (*database.PromptConversation, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationReq) *database.PromptConversation); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*database.PromptConversation) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.ConversationReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_GetConversation_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetConversation' +type MockPromptComponent_GetConversation_Call struct { + *mock.Call +} + +// GetConversation is a helper method to define mock.On call +// - ctx context.Context +// - req types.ConversationReq +func (_e *MockPromptComponent_Expecter) GetConversation(ctx interface{}, req interface{}) *MockPromptComponent_GetConversation_Call { + return &MockPromptComponent_GetConversation_Call{Call: _e.mock.On("GetConversation", ctx, req)} +} + +func (_c *MockPromptComponent_GetConversation_Call) Run(run func(ctx context.Context, req types.ConversationReq)) *MockPromptComponent_GetConversation_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.ConversationReq)) + }) + return _c +} + +func (_c *MockPromptComponent_GetConversation_Call) Return(_a0 *database.PromptConversation, _a1 error) *MockPromptComponent_GetConversation_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_GetConversation_Call) RunAndReturn(run func(context.Context, types.ConversationReq) (*database.PromptConversation, error)) *MockPromptComponent_GetConversation_Call { + _c.Call.Return(run) + return _c +} + +// GetPrompt provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) GetPrompt(ctx context.Context, req types.PromptReq) (*types.PromptOutput, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for GetPrompt") + } + + var r0 *types.PromptOutput + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq) (*types.PromptOutput, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq) *types.PromptOutput); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.PromptOutput) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.PromptReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_GetPrompt_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetPrompt' +type MockPromptComponent_GetPrompt_Call struct { + *mock.Call +} + +// GetPrompt is a helper method to define mock.On call +// - ctx context.Context +// - req types.PromptReq +func (_e *MockPromptComponent_Expecter) GetPrompt(ctx interface{}, req interface{}) *MockPromptComponent_GetPrompt_Call { + return &MockPromptComponent_GetPrompt_Call{Call: _e.mock.On("GetPrompt", ctx, req)} +} + +func (_c *MockPromptComponent_GetPrompt_Call) Run(run func(ctx context.Context, req types.PromptReq)) *MockPromptComponent_GetPrompt_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.PromptReq)) + }) + return _c +} + +func (_c *MockPromptComponent_GetPrompt_Call) Return(_a0 *types.PromptOutput, _a1 error) *MockPromptComponent_GetPrompt_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_GetPrompt_Call) RunAndReturn(run func(context.Context, types.PromptReq) (*types.PromptOutput, error)) *MockPromptComponent_GetPrompt_Call { + _c.Call.Return(run) + return _c +} + +// HateConversationMessage provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) HateConversationMessage(ctx context.Context, req types.ConversationMessageReq) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for HateConversationMessage") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationMessageReq) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockPromptComponent_HateConversationMessage_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HateConversationMessage' +type MockPromptComponent_HateConversationMessage_Call struct { + *mock.Call +} + +// HateConversationMessage is a helper method to define mock.On call +// - ctx context.Context +// - req types.ConversationMessageReq +func (_e *MockPromptComponent_Expecter) HateConversationMessage(ctx interface{}, req interface{}) *MockPromptComponent_HateConversationMessage_Call { + return &MockPromptComponent_HateConversationMessage_Call{Call: _e.mock.On("HateConversationMessage", ctx, req)} +} + +func (_c *MockPromptComponent_HateConversationMessage_Call) Run(run func(ctx context.Context, req types.ConversationMessageReq)) *MockPromptComponent_HateConversationMessage_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.ConversationMessageReq)) + }) + return _c +} + +func (_c *MockPromptComponent_HateConversationMessage_Call) Return(_a0 error) *MockPromptComponent_HateConversationMessage_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockPromptComponent_HateConversationMessage_Call) RunAndReturn(run func(context.Context, types.ConversationMessageReq) error) *MockPromptComponent_HateConversationMessage_Call { + _c.Call.Return(run) + return _c +} + +// IndexPromptRepo provides a mock function with given fields: ctx, filter, per, page +func (_m *MockPromptComponent) IndexPromptRepo(ctx context.Context, filter *types.RepoFilter, per int, page int) ([]types.PromptRes, int, error) { + ret := _m.Called(ctx, filter, per, page) + + if len(ret) == 0 { + panic("no return value specified for IndexPromptRepo") + } + + var r0 []types.PromptRes + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.RepoFilter, int, int) ([]types.PromptRes, int, error)); ok { + return rf(ctx, filter, per, page) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.RepoFilter, int, int) []types.PromptRes); ok { + r0 = rf(ctx, filter, per, page) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.PromptRes) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.RepoFilter, int, int) int); ok { + r1 = rf(ctx, filter, per, page) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.RepoFilter, int, int) error); ok { + r2 = rf(ctx, filter, per, page) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockPromptComponent_IndexPromptRepo_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IndexPromptRepo' +type MockPromptComponent_IndexPromptRepo_Call struct { + *mock.Call +} + +// IndexPromptRepo is a helper method to define mock.On call +// - ctx context.Context +// - filter *types.RepoFilter +// - per int +// - page int +func (_e *MockPromptComponent_Expecter) IndexPromptRepo(ctx interface{}, filter interface{}, per interface{}, page interface{}) *MockPromptComponent_IndexPromptRepo_Call { + return &MockPromptComponent_IndexPromptRepo_Call{Call: _e.mock.On("IndexPromptRepo", ctx, filter, per, page)} +} + +func (_c *MockPromptComponent_IndexPromptRepo_Call) Run(run func(ctx context.Context, filter *types.RepoFilter, per int, page int)) *MockPromptComponent_IndexPromptRepo_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.RepoFilter), args[2].(int), args[3].(int)) + }) + return _c +} + +func (_c *MockPromptComponent_IndexPromptRepo_Call) Return(_a0 []types.PromptRes, _a1 int, _a2 error) *MockPromptComponent_IndexPromptRepo_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockPromptComponent_IndexPromptRepo_Call) RunAndReturn(run func(context.Context, *types.RepoFilter, int, int) ([]types.PromptRes, int, error)) *MockPromptComponent_IndexPromptRepo_Call { + _c.Call.Return(run) + return _c +} + +// LikeConversationMessage provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) LikeConversationMessage(ctx context.Context, req types.ConversationMessageReq) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for LikeConversationMessage") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationMessageReq) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockPromptComponent_LikeConversationMessage_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LikeConversationMessage' +type MockPromptComponent_LikeConversationMessage_Call struct { + *mock.Call +} + +// LikeConversationMessage is a helper method to define mock.On call +// - ctx context.Context +// - req types.ConversationMessageReq +func (_e *MockPromptComponent_Expecter) LikeConversationMessage(ctx interface{}, req interface{}) *MockPromptComponent_LikeConversationMessage_Call { + return &MockPromptComponent_LikeConversationMessage_Call{Call: _e.mock.On("LikeConversationMessage", ctx, req)} +} + +func (_c *MockPromptComponent_LikeConversationMessage_Call) Run(run func(ctx context.Context, req types.ConversationMessageReq)) *MockPromptComponent_LikeConversationMessage_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.ConversationMessageReq)) + }) + return _c +} + +func (_c *MockPromptComponent_LikeConversationMessage_Call) Return(_a0 error) *MockPromptComponent_LikeConversationMessage_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockPromptComponent_LikeConversationMessage_Call) RunAndReturn(run func(context.Context, types.ConversationMessageReq) error) *MockPromptComponent_LikeConversationMessage_Call { + _c.Call.Return(run) + return _c +} + +// ListConversationsByUserID provides a mock function with given fields: ctx, currentUser +func (_m *MockPromptComponent) ListConversationsByUserID(ctx context.Context, currentUser string) ([]database.PromptConversation, error) { + ret := _m.Called(ctx, currentUser) + + if len(ret) == 0 { + panic("no return value specified for ListConversationsByUserID") + } + + var r0 []database.PromptConversation + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string) ([]database.PromptConversation, error)); ok { + return rf(ctx, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string) []database.PromptConversation); ok { + r0 = rf(ctx, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]database.PromptConversation) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_ListConversationsByUserID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListConversationsByUserID' +type MockPromptComponent_ListConversationsByUserID_Call struct { + *mock.Call +} + +// ListConversationsByUserID is a helper method to define mock.On call +// - ctx context.Context +// - currentUser string +func (_e *MockPromptComponent_Expecter) ListConversationsByUserID(ctx interface{}, currentUser interface{}) *MockPromptComponent_ListConversationsByUserID_Call { + return &MockPromptComponent_ListConversationsByUserID_Call{Call: _e.mock.On("ListConversationsByUserID", ctx, currentUser)} +} + +func (_c *MockPromptComponent_ListConversationsByUserID_Call) Run(run func(ctx context.Context, currentUser string)) *MockPromptComponent_ListConversationsByUserID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *MockPromptComponent_ListConversationsByUserID_Call) Return(_a0 []database.PromptConversation, _a1 error) *MockPromptComponent_ListConversationsByUserID_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_ListConversationsByUserID_Call) RunAndReturn(run func(context.Context, string) ([]database.PromptConversation, error)) *MockPromptComponent_ListConversationsByUserID_Call { + _c.Call.Return(run) + return _c +} + +// ListPrompt provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) ListPrompt(ctx context.Context, req types.PromptReq) ([]types.PromptOutput, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for ListPrompt") + } + + var r0 []types.PromptOutput + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq) ([]types.PromptOutput, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq) []types.PromptOutput); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.PromptOutput) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.PromptReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_ListPrompt_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListPrompt' +type MockPromptComponent_ListPrompt_Call struct { + *mock.Call +} + +// ListPrompt is a helper method to define mock.On call +// - ctx context.Context +// - req types.PromptReq +func (_e *MockPromptComponent_Expecter) ListPrompt(ctx interface{}, req interface{}) *MockPromptComponent_ListPrompt_Call { + return &MockPromptComponent_ListPrompt_Call{Call: _e.mock.On("ListPrompt", ctx, req)} +} + +func (_c *MockPromptComponent_ListPrompt_Call) Run(run func(ctx context.Context, req types.PromptReq)) *MockPromptComponent_ListPrompt_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.PromptReq)) + }) + return _c +} + +func (_c *MockPromptComponent_ListPrompt_Call) Return(_a0 []types.PromptOutput, _a1 error) *MockPromptComponent_ListPrompt_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_ListPrompt_Call) RunAndReturn(run func(context.Context, types.PromptReq) ([]types.PromptOutput, error)) *MockPromptComponent_ListPrompt_Call { + _c.Call.Return(run) + return _c +} + +// NewConversation provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) NewConversation(ctx context.Context, req types.ConversationTitleReq) (*database.PromptConversation, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for NewConversation") + } + + var r0 *database.PromptConversation + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationTitleReq) (*database.PromptConversation, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationTitleReq) *database.PromptConversation); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*database.PromptConversation) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.ConversationTitleReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_NewConversation_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NewConversation' +type MockPromptComponent_NewConversation_Call struct { + *mock.Call +} + +// NewConversation is a helper method to define mock.On call +// - ctx context.Context +// - req types.ConversationTitleReq +func (_e *MockPromptComponent_Expecter) NewConversation(ctx interface{}, req interface{}) *MockPromptComponent_NewConversation_Call { + return &MockPromptComponent_NewConversation_Call{Call: _e.mock.On("NewConversation", ctx, req)} +} + +func (_c *MockPromptComponent_NewConversation_Call) Run(run func(ctx context.Context, req types.ConversationTitleReq)) *MockPromptComponent_NewConversation_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.ConversationTitleReq)) + }) + return _c +} + +func (_c *MockPromptComponent_NewConversation_Call) Return(_a0 *database.PromptConversation, _a1 error) *MockPromptComponent_NewConversation_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_NewConversation_Call) RunAndReturn(run func(context.Context, types.ConversationTitleReq) (*database.PromptConversation, error)) *MockPromptComponent_NewConversation_Call { + _c.Call.Return(run) + return _c +} + +// OrgPrompts provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) OrgPrompts(ctx context.Context, req *types.OrgPromptsReq) ([]types.PromptRes, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for OrgPrompts") + } + + var r0 []types.PromptRes + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.OrgPromptsReq) ([]types.PromptRes, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.OrgPromptsReq) []types.PromptRes); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.PromptRes) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.OrgPromptsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.OrgPromptsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockPromptComponent_OrgPrompts_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'OrgPrompts' +type MockPromptComponent_OrgPrompts_Call struct { + *mock.Call +} + +// OrgPrompts is a helper method to define mock.On call +// - ctx context.Context +// - req *types.OrgPromptsReq +func (_e *MockPromptComponent_Expecter) OrgPrompts(ctx interface{}, req interface{}) *MockPromptComponent_OrgPrompts_Call { + return &MockPromptComponent_OrgPrompts_Call{Call: _e.mock.On("OrgPrompts", ctx, req)} +} + +func (_c *MockPromptComponent_OrgPrompts_Call) Run(run func(ctx context.Context, req *types.OrgPromptsReq)) *MockPromptComponent_OrgPrompts_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.OrgPromptsReq)) + }) + return _c +} + +func (_c *MockPromptComponent_OrgPrompts_Call) Return(_a0 []types.PromptRes, _a1 int, _a2 error) *MockPromptComponent_OrgPrompts_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockPromptComponent_OrgPrompts_Call) RunAndReturn(run func(context.Context, *types.OrgPromptsReq) ([]types.PromptRes, int, error)) *MockPromptComponent_OrgPrompts_Call { + _c.Call.Return(run) + return _c +} + +// ParseJsonFile provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) ParseJsonFile(ctx context.Context, req gitserver.GetRepoInfoByPathReq) (*types.PromptOutput, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for ParseJsonFile") + } + + var r0 *types.PromptOutput + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, gitserver.GetRepoInfoByPathReq) (*types.PromptOutput, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, gitserver.GetRepoInfoByPathReq) *types.PromptOutput); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.PromptOutput) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, gitserver.GetRepoInfoByPathReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_ParseJsonFile_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ParseJsonFile' +type MockPromptComponent_ParseJsonFile_Call struct { + *mock.Call +} + +// ParseJsonFile is a helper method to define mock.On call +// - ctx context.Context +// - req gitserver.GetRepoInfoByPathReq +func (_e *MockPromptComponent_Expecter) ParseJsonFile(ctx interface{}, req interface{}) *MockPromptComponent_ParseJsonFile_Call { + return &MockPromptComponent_ParseJsonFile_Call{Call: _e.mock.On("ParseJsonFile", ctx, req)} +} + +func (_c *MockPromptComponent_ParseJsonFile_Call) Run(run func(ctx context.Context, req gitserver.GetRepoInfoByPathReq)) *MockPromptComponent_ParseJsonFile_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(gitserver.GetRepoInfoByPathReq)) + }) + return _c +} + +func (_c *MockPromptComponent_ParseJsonFile_Call) Return(_a0 *types.PromptOutput, _a1 error) *MockPromptComponent_ParseJsonFile_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_ParseJsonFile_Call) RunAndReturn(run func(context.Context, gitserver.GetRepoInfoByPathReq) (*types.PromptOutput, error)) *MockPromptComponent_ParseJsonFile_Call { + _c.Call.Return(run) + return _c +} + +// Relations provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockPromptComponent) Relations(ctx context.Context, namespace string, name string, currentUser string) (*types.Relations, error) { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for Relations") + } + + var r0 *types.Relations + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) (*types.Relations, error)); ok { + return rf(ctx, namespace, name, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) *types.Relations); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Relations) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string) error); ok { + r1 = rf(ctx, namespace, name, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_Relations_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Relations' +type MockPromptComponent_Relations_Call struct { + *mock.Call +} + +// Relations is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockPromptComponent_Expecter) Relations(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockPromptComponent_Relations_Call { + return &MockPromptComponent_Relations_Call{Call: _e.mock.On("Relations", ctx, namespace, name, currentUser)} +} + +func (_c *MockPromptComponent_Relations_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockPromptComponent_Relations_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockPromptComponent_Relations_Call) Return(_a0 *types.Relations, _a1 error) *MockPromptComponent_Relations_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_Relations_Call) RunAndReturn(run func(context.Context, string, string, string) (*types.Relations, error)) *MockPromptComponent_Relations_Call { + _c.Call.Return(run) + return _c +} + +// RemoveConversation provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) RemoveConversation(ctx context.Context, req types.ConversationReq) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for RemoveConversation") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationReq) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockPromptComponent_RemoveConversation_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RemoveConversation' +type MockPromptComponent_RemoveConversation_Call struct { + *mock.Call +} + +// RemoveConversation is a helper method to define mock.On call +// - ctx context.Context +// - req types.ConversationReq +func (_e *MockPromptComponent_Expecter) RemoveConversation(ctx interface{}, req interface{}) *MockPromptComponent_RemoveConversation_Call { + return &MockPromptComponent_RemoveConversation_Call{Call: _e.mock.On("RemoveConversation", ctx, req)} +} + +func (_c *MockPromptComponent_RemoveConversation_Call) Run(run func(ctx context.Context, req types.ConversationReq)) *MockPromptComponent_RemoveConversation_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.ConversationReq)) + }) + return _c +} + +func (_c *MockPromptComponent_RemoveConversation_Call) Return(_a0 error) *MockPromptComponent_RemoveConversation_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockPromptComponent_RemoveConversation_Call) RunAndReturn(run func(context.Context, types.ConversationReq) error) *MockPromptComponent_RemoveConversation_Call { + _c.Call.Return(run) + return _c +} + +// RemoveRepo provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockPromptComponent) RemoveRepo(ctx context.Context, namespace string, name string, currentUser string) error { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for RemoveRepo") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) error); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockPromptComponent_RemoveRepo_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RemoveRepo' +type MockPromptComponent_RemoveRepo_Call struct { + *mock.Call +} + +// RemoveRepo is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockPromptComponent_Expecter) RemoveRepo(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockPromptComponent_RemoveRepo_Call { + return &MockPromptComponent_RemoveRepo_Call{Call: _e.mock.On("RemoveRepo", ctx, namespace, name, currentUser)} +} + +func (_c *MockPromptComponent_RemoveRepo_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockPromptComponent_RemoveRepo_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockPromptComponent_RemoveRepo_Call) Return(_a0 error) *MockPromptComponent_RemoveRepo_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockPromptComponent_RemoveRepo_Call) RunAndReturn(run func(context.Context, string, string, string) error) *MockPromptComponent_RemoveRepo_Call { + _c.Call.Return(run) + return _c +} + +// SaveGeneratedText provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) SaveGeneratedText(ctx context.Context, req types.Conversation) (*database.PromptConversationMessage, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for SaveGeneratedText") + } + + var r0 *database.PromptConversationMessage + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.Conversation) (*database.PromptConversationMessage, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.Conversation) *database.PromptConversationMessage); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*database.PromptConversationMessage) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.Conversation) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_SaveGeneratedText_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SaveGeneratedText' +type MockPromptComponent_SaveGeneratedText_Call struct { + *mock.Call +} + +// SaveGeneratedText is a helper method to define mock.On call +// - ctx context.Context +// - req types.Conversation +func (_e *MockPromptComponent_Expecter) SaveGeneratedText(ctx interface{}, req interface{}) *MockPromptComponent_SaveGeneratedText_Call { + return &MockPromptComponent_SaveGeneratedText_Call{Call: _e.mock.On("SaveGeneratedText", ctx, req)} +} + +func (_c *MockPromptComponent_SaveGeneratedText_Call) Run(run func(ctx context.Context, req types.Conversation)) *MockPromptComponent_SaveGeneratedText_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.Conversation)) + }) + return _c +} + +func (_c *MockPromptComponent_SaveGeneratedText_Call) Return(_a0 *database.PromptConversationMessage, _a1 error) *MockPromptComponent_SaveGeneratedText_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_SaveGeneratedText_Call) RunAndReturn(run func(context.Context, types.Conversation) (*database.PromptConversationMessage, error)) *MockPromptComponent_SaveGeneratedText_Call { + _c.Call.Return(run) + return _c +} + +// SetRelationModels provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) SetRelationModels(ctx context.Context, req types.RelationModels) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for SetRelationModels") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.RelationModels) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockPromptComponent_SetRelationModels_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SetRelationModels' +type MockPromptComponent_SetRelationModels_Call struct { + *mock.Call +} + +// SetRelationModels is a helper method to define mock.On call +// - ctx context.Context +// - req types.RelationModels +func (_e *MockPromptComponent_Expecter) SetRelationModels(ctx interface{}, req interface{}) *MockPromptComponent_SetRelationModels_Call { + return &MockPromptComponent_SetRelationModels_Call{Call: _e.mock.On("SetRelationModels", ctx, req)} +} + +func (_c *MockPromptComponent_SetRelationModels_Call) Run(run func(ctx context.Context, req types.RelationModels)) *MockPromptComponent_SetRelationModels_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.RelationModels)) + }) + return _c +} + +func (_c *MockPromptComponent_SetRelationModels_Call) Return(_a0 error) *MockPromptComponent_SetRelationModels_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockPromptComponent_SetRelationModels_Call) RunAndReturn(run func(context.Context, types.RelationModels) error) *MockPromptComponent_SetRelationModels_Call { + _c.Call.Return(run) + return _c +} + +// Show provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockPromptComponent) Show(ctx context.Context, namespace string, name string, currentUser string) (*types.PromptRes, error) { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for Show") + } + + var r0 *types.PromptRes + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) (*types.PromptRes, error)); ok { + return rf(ctx, namespace, name, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) *types.PromptRes); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.PromptRes) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string) error); ok { + r1 = rf(ctx, namespace, name, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_Show_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Show' +type MockPromptComponent_Show_Call struct { + *mock.Call +} + +// Show is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockPromptComponent_Expecter) Show(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockPromptComponent_Show_Call { + return &MockPromptComponent_Show_Call{Call: _e.mock.On("Show", ctx, namespace, name, currentUser)} +} + +func (_c *MockPromptComponent_Show_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockPromptComponent_Show_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockPromptComponent_Show_Call) Return(_a0 *types.PromptRes, _a1 error) *MockPromptComponent_Show_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_Show_Call) RunAndReturn(run func(context.Context, string, string, string) (*types.PromptRes, error)) *MockPromptComponent_Show_Call { + _c.Call.Return(run) + return _c +} + +// SubmitMessage provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) SubmitMessage(ctx context.Context, req types.ConversationReq) (<-chan string, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for SubmitMessage") + } + + var r0 <-chan string + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationReq) (<-chan string, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationReq) <-chan string); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(<-chan string) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.ConversationReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_SubmitMessage_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SubmitMessage' +type MockPromptComponent_SubmitMessage_Call struct { + *mock.Call +} + +// SubmitMessage is a helper method to define mock.On call +// - ctx context.Context +// - req types.ConversationReq +func (_e *MockPromptComponent_Expecter) SubmitMessage(ctx interface{}, req interface{}) *MockPromptComponent_SubmitMessage_Call { + return &MockPromptComponent_SubmitMessage_Call{Call: _e.mock.On("SubmitMessage", ctx, req)} +} + +func (_c *MockPromptComponent_SubmitMessage_Call) Run(run func(ctx context.Context, req types.ConversationReq)) *MockPromptComponent_SubmitMessage_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.ConversationReq)) + }) + return _c +} + +func (_c *MockPromptComponent_SubmitMessage_Call) Return(_a0 <-chan string, _a1 error) *MockPromptComponent_SubmitMessage_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_SubmitMessage_Call) RunAndReturn(run func(context.Context, types.ConversationReq) (<-chan string, error)) *MockPromptComponent_SubmitMessage_Call { + _c.Call.Return(run) + return _c +} + +// UpdateConversation provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) UpdateConversation(ctx context.Context, req types.ConversationTitleReq) (*database.PromptConversation, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for UpdateConversation") + } + + var r0 *database.PromptConversation + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationTitleReq) (*database.PromptConversation, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.ConversationTitleReq) *database.PromptConversation); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*database.PromptConversation) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.ConversationTitleReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_UpdateConversation_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdateConversation' +type MockPromptComponent_UpdateConversation_Call struct { + *mock.Call +} + +// UpdateConversation is a helper method to define mock.On call +// - ctx context.Context +// - req types.ConversationTitleReq +func (_e *MockPromptComponent_Expecter) UpdateConversation(ctx interface{}, req interface{}) *MockPromptComponent_UpdateConversation_Call { + return &MockPromptComponent_UpdateConversation_Call{Call: _e.mock.On("UpdateConversation", ctx, req)} +} + +func (_c *MockPromptComponent_UpdateConversation_Call) Run(run func(ctx context.Context, req types.ConversationTitleReq)) *MockPromptComponent_UpdateConversation_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.ConversationTitleReq)) + }) + return _c +} + +func (_c *MockPromptComponent_UpdateConversation_Call) Return(_a0 *database.PromptConversation, _a1 error) *MockPromptComponent_UpdateConversation_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_UpdateConversation_Call) RunAndReturn(run func(context.Context, types.ConversationTitleReq) (*database.PromptConversation, error)) *MockPromptComponent_UpdateConversation_Call { + _c.Call.Return(run) + return _c +} + +// UpdatePrompt provides a mock function with given fields: ctx, req, body +func (_m *MockPromptComponent) UpdatePrompt(ctx context.Context, req types.PromptReq, body *types.UpdatePromptReq) (*types.Prompt, error) { + ret := _m.Called(ctx, req, body) + + if len(ret) == 0 { + panic("no return value specified for UpdatePrompt") + } + + var r0 *types.Prompt + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq, *types.UpdatePromptReq) (*types.Prompt, error)); ok { + return rf(ctx, req, body) + } + if rf, ok := ret.Get(0).(func(context.Context, types.PromptReq, *types.UpdatePromptReq) *types.Prompt); ok { + r0 = rf(ctx, req, body) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Prompt) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.PromptReq, *types.UpdatePromptReq) error); ok { + r1 = rf(ctx, req, body) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_UpdatePrompt_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdatePrompt' +type MockPromptComponent_UpdatePrompt_Call struct { + *mock.Call +} + +// UpdatePrompt is a helper method to define mock.On call +// - ctx context.Context +// - req types.PromptReq +// - body *types.UpdatePromptReq +func (_e *MockPromptComponent_Expecter) UpdatePrompt(ctx interface{}, req interface{}, body interface{}) *MockPromptComponent_UpdatePrompt_Call { + return &MockPromptComponent_UpdatePrompt_Call{Call: _e.mock.On("UpdatePrompt", ctx, req, body)} +} + +func (_c *MockPromptComponent_UpdatePrompt_Call) Run(run func(ctx context.Context, req types.PromptReq, body *types.UpdatePromptReq)) *MockPromptComponent_UpdatePrompt_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.PromptReq), args[2].(*types.UpdatePromptReq)) + }) + return _c +} + +func (_c *MockPromptComponent_UpdatePrompt_Call) Return(_a0 *types.Prompt, _a1 error) *MockPromptComponent_UpdatePrompt_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_UpdatePrompt_Call) RunAndReturn(run func(context.Context, types.PromptReq, *types.UpdatePromptReq) (*types.Prompt, error)) *MockPromptComponent_UpdatePrompt_Call { + _c.Call.Return(run) + return _c +} + +// UpdatePromptRepo provides a mock function with given fields: ctx, req +func (_m *MockPromptComponent) UpdatePromptRepo(ctx context.Context, req *types.UpdatePromptRepoReq) (*types.PromptRes, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for UpdatePromptRepo") + } + + var r0 *types.PromptRes + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UpdatePromptRepoReq) (*types.PromptRes, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UpdatePromptRepoReq) *types.PromptRes); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.PromptRes) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UpdatePromptRepoReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockPromptComponent_UpdatePromptRepo_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdatePromptRepo' +type MockPromptComponent_UpdatePromptRepo_Call struct { + *mock.Call +} + +// UpdatePromptRepo is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UpdatePromptRepoReq +func (_e *MockPromptComponent_Expecter) UpdatePromptRepo(ctx interface{}, req interface{}) *MockPromptComponent_UpdatePromptRepo_Call { + return &MockPromptComponent_UpdatePromptRepo_Call{Call: _e.mock.On("UpdatePromptRepo", ctx, req)} +} + +func (_c *MockPromptComponent_UpdatePromptRepo_Call) Run(run func(ctx context.Context, req *types.UpdatePromptRepoReq)) *MockPromptComponent_UpdatePromptRepo_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UpdatePromptRepoReq)) + }) + return _c +} + +func (_c *MockPromptComponent_UpdatePromptRepo_Call) Return(_a0 *types.PromptRes, _a1 error) *MockPromptComponent_UpdatePromptRepo_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockPromptComponent_UpdatePromptRepo_Call) RunAndReturn(run func(context.Context, *types.UpdatePromptRepoReq) (*types.PromptRes, error)) *MockPromptComponent_UpdatePromptRepo_Call { + _c.Call.Return(run) + return _c +} + +// NewMockPromptComponent creates a new instance of MockPromptComponent. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockPromptComponent(t interface { + mock.TestingT + Cleanup(func()) +}) *MockPromptComponent { + mock := &MockPromptComponent{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/api/handler/code.go b/api/handler/code.go index 5792c0c0..bca06df0 100644 --- a/api/handler/code.go +++ b/api/handler/code.go @@ -25,14 +25,14 @@ func NewCodeHandler(config *config.Config) (*CodeHandler, error) { return nil, fmt.Errorf("error creating sensitive component:%w", err) } return &CodeHandler{ - c: tc, - sc: sc, + code: tc, + sensitive: sc, }, nil } type CodeHandler struct { - c component.CodeComponent - sc component.SensitiveComponent + code component.CodeComponent + sensitive component.SensitiveComponent } // CreateCode godoc @@ -61,7 +61,7 @@ func (h *CodeHandler) Create(ctx *gin.Context) { return } - _, err := h.sc.CheckRequestV2(ctx, req) + _, err := h.sensitive.CheckRequestV2(ctx, req) if err != nil { slog.Error("failed to check sensitive request", slog.Any("error", err)) httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error()) @@ -69,7 +69,7 @@ func (h *CodeHandler) Create(ctx *gin.Context) { } req.Username = currentUser - code, err := h.c.Create(ctx, req) + code, err := h.code.Create(ctx, req) if err != nil { slog.Error("Failed to create code", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -128,7 +128,7 @@ func (h *CodeHandler) Index(ctx *gin.Context) { return } - codes, total, err := h.c.Index(ctx, filter, per, page) + codes, total, err := h.code.Index(ctx, filter, per, page) if err != nil { slog.Error("Failed to get codes", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -170,7 +170,7 @@ func (h *CodeHandler) Update(ctx *gin.Context) { return } - _, err := h.sc.CheckRequestV2(ctx, req) + _, err := h.sensitive.CheckRequestV2(ctx, req) if err != nil { slog.Error("failed to check sensitive request", slog.Any("error", err)) httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error()) @@ -187,7 +187,7 @@ func (h *CodeHandler) Update(ctx *gin.Context) { req.Namespace = namespace req.Name = name - code, err := h.c.Update(ctx, req) + code, err := h.code.Update(ctx, req) if err != nil { slog.Error("Failed to update code", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -224,7 +224,7 @@ func (h *CodeHandler) Delete(ctx *gin.Context) { httpbase.BadRequest(ctx, err.Error()) return } - err = h.c.Delete(ctx, namespace, name, currentUser) + err = h.code.Delete(ctx, namespace, name, currentUser) if err != nil { slog.Error("Failed to delete code", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -256,7 +256,7 @@ func (h *CodeHandler) Show(ctx *gin.Context) { return } currentUser := httpbase.GetCurrentUser(ctx) - detail, err := h.c.Show(ctx, namespace, name, currentUser) + detail, err := h.code.Show(ctx, namespace, name, currentUser) if err != nil { if errors.Is(err, component.ErrUnauthorized) { httpbase.UnauthorizedError(ctx, err) @@ -291,7 +291,7 @@ func (h *CodeHandler) Relations(ctx *gin.Context) { return } currentUser := httpbase.GetCurrentUser(ctx) - detail, err := h.c.Relations(ctx, namespace, name, currentUser) + detail, err := h.code.Relations(ctx, namespace, name, currentUser) if err != nil { if errors.Is(err, component.ErrUnauthorized) { httpbase.UnauthorizedError(ctx, err) diff --git a/api/handler/code_test.go b/api/handler/code_test.go new file mode 100644 index 00000000..09bbe917 --- /dev/null +++ b/api/handler/code_test.go @@ -0,0 +1,149 @@ +package handler + +import ( + "fmt" + "testing" + + "github.com/alibabacloud-go/tea/tea" + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/require" + mockcomponent "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/component" + "opencsg.com/csghub-server/common/types" +) + +type CodeTester struct { + *GinTester + handler *CodeHandler + mocks struct { + code *mockcomponent.MockCodeComponent + sensitive *mockcomponent.MockSensitiveComponent + } +} + +func NewCodeTester(t *testing.T) *CodeTester { + tester := &CodeTester{GinTester: NewGinTester()} + tester.mocks.code = mockcomponent.NewMockCodeComponent(t) + tester.mocks.sensitive = mockcomponent.NewMockSensitiveComponent(t) + tester.handler = &CodeHandler{code: tester.mocks.code, sensitive: tester.mocks.sensitive} + tester.WithParam("name", "r") + tester.WithParam("namespace", "u") + return tester + +} + +func (ct *CodeTester) WithHandleFunc(fn func(cp *CodeHandler) gin.HandlerFunc) *CodeTester { + ct.ginHandler = fn(ct.handler) + return ct +} + +func TestCodeHandler_Create(t *testing.T) { + tester := NewCodeTester(t).WithHandleFunc(func(cp *CodeHandler) gin.HandlerFunc { + return cp.Create + }) + tester.RequireUser(t) + + req := &types.CreateCodeReq{CreateRepoReq: types.CreateRepoReq{Name: "c"}} + tester.mocks.sensitive.EXPECT().CheckRequestV2(tester.ctx, req).Return(true, nil) + reqn := *req + reqn.Username = "u" + tester.mocks.code.EXPECT().Create(tester.ctx, &reqn).Return(&types.Code{Name: "c"}, nil) + tester.WithBody(t, req).Execute() + + tester.ResponseEqSimple(t, 200, gin.H{"data": &types.Code{Name: "c"}}) + +} + +func TestCodeHandler_Index(t *testing.T) { + + cases := []struct { + sort string + source string + error bool + }{ + {"most_download", "local", false}, + {"foo", "local", true}, + {"most_download", "bar", true}, + } + + for _, c := range cases { + t.Run(fmt.Sprintf("%+v", c), func(t *testing.T) { + + tester := NewCodeTester(t).WithHandleFunc(func(cp *CodeHandler) gin.HandlerFunc { + return cp.Index + }) + + if !c.error { + tester.mocks.code.EXPECT().Index(tester.ctx, &types.RepoFilter{ + Search: "foo", + Sort: c.sort, + Source: c.source, + }, 10, 1).Return([]types.Code{ + {Name: "cc"}, + }, 100, nil) + } + + tester.AddPagination(1, 10).WithQuery("search", "foo"). + WithQuery("sort", c.sort). + WithQuery("source", c.source).Execute() + + if c.error { + require.Equal(t, 400, tester.response.Code) + } else { + tester.ResponseEqSimple(t, 200, gin.H{ + "data": []types.Code{{Name: "cc"}}, + "total": 100, + }) + } + }) + } +} + +func TestCodeHandler_Update(t *testing.T) { + tester := NewCodeTester(t).WithHandleFunc(func(cp *CodeHandler) gin.HandlerFunc { + return cp.Update + }) + tester.RequireUser(t) + + req := &types.UpdateCodeReq{UpdateRepoReq: types.UpdateRepoReq{Nickname: tea.String("nc")}} + tester.mocks.sensitive.EXPECT().CheckRequestV2(tester.ctx, req).Return(true, nil) + reqn := *req + reqn.Username = "u" + reqn.Name = "r" + reqn.Namespace = "u" + tester.mocks.code.EXPECT().Update(tester.ctx, &reqn).Return(&types.Code{Name: "c"}, nil) + tester.WithBody(t, req).Execute() + + tester.ResponseEq(t, 200, tester.OKText, &types.Code{Name: "c"}) + +} + +func TestCodeHandler_Delete(t *testing.T) { + tester := NewCodeTester(t).WithHandleFunc(func(cp *CodeHandler) gin.HandlerFunc { + return cp.Delete + }) + tester.RequireUser(t) + + tester.mocks.code.EXPECT().Delete(tester.ctx, "u", "r", "u").Return(nil) + tester.Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestCodeHandler_Show(t *testing.T) { + tester := NewCodeTester(t).WithHandleFunc(func(cp *CodeHandler) gin.HandlerFunc { + return cp.Show + }) + + tester.mocks.code.EXPECT().Show(tester.ctx, "u", "r", "u").Return(&types.Code{Name: "c"}, nil) + tester.WithUser().Execute() + tester.ResponseEq(t, 200, tester.OKText, &types.Code{Name: "c"}) +} + +func TestCodeHandler_Relations(t *testing.T) { + tester := NewCodeTester(t).WithHandleFunc(func(cp *CodeHandler) gin.HandlerFunc { + return cp.Relations + }) + + tester.mocks.code.EXPECT().Relations(tester.ctx, "u", "r", "u").Return(&types.Relations{}, nil) + tester.WithUser().Execute() + tester.ResponseEq(t, 200, tester.OKText, &types.Relations{}) +} diff --git a/api/handler/prompt.go b/api/handler/prompt.go index 0eecbdee..6354c638 100644 --- a/api/handler/prompt.go +++ b/api/handler/prompt.go @@ -229,7 +229,7 @@ func (h *PromptHandler) CreatePrompt(ctx *gin.Context) { return } - var body *component.CreatePromptReq + var body *types.CreatePromptReq if err := ctx.ShouldBindJSON(&body); err != nil { slog.Error("Bad request prompt format", "error", err) httpbase.BadRequest(ctx, err.Error()) @@ -291,7 +291,7 @@ func (h *PromptHandler) UpdatePrompt(ctx *gin.Context) { return } - var body *component.UpdatePromptReq + var body *types.UpdatePromptReq if err := ctx.ShouldBindJSON(&body); err != nil { slog.Error("Bad request prompt format", "error", err) httpbase.BadRequest(ctx, err.Error()) diff --git a/builder/sensitive/aliyun_green.go b/builder/sensitive/aliyun_green.go index 7c06a953..b6041127 100644 --- a/builder/sensitive/aliyun_green.go +++ b/builder/sensitive/aliyun_green.go @@ -15,9 +15,21 @@ import ( "github.com/alibabacloud-go/tea/tea" "github.com/aliyun/alibaba-cloud-sdk-go/services/green" "opencsg.com/csghub-server/common/config" - "opencsg.com/csghub-server/common/utils/common" ) +// copy from common/utils/common to avoid cycle import +func truncString(s string, limit int) string { + if len(s) <= limit { + return s + } + + s1 := []byte(s[:limit]) + s1[limit-1] = '.' + s1[limit-2] = '.' + s1[limit-3] = '.' + return string(s1) +} + type GreenClient interface { TextScan(request *green.TextScanRequest) (response *TextScanResponse, err error) } @@ -149,7 +161,7 @@ func (c *AliyunGreenChecker) PassLargeTextCheck(ctx context.Context, text string } if result.Suggestion == "block" { - slog.Info("block content", slog.String("content", common.TruncString(data.Content, 128)), slog.String("taskId", data.TaskId), + slog.Info("block content", slog.String("content", truncString(data.Content, 128)), slog.String("taskId", data.TaskId), slog.String("aliyun_request_id", resp.RequestID)) return &CheckResult{IsSensitive: true, Reason: result.Label}, nil diff --git a/common/types/prompt.go b/common/types/prompt.go index 7ad62e90..c6a6f641 100644 --- a/common/types/prompt.go +++ b/common/types/prompt.go @@ -2,6 +2,8 @@ package types import ( "time" + + "opencsg.com/csghub-server/builder/sensitive" ) type PromptReq struct { @@ -104,19 +106,6 @@ type PromptRes struct { Namespace *Namespace `json:"namespace"` } -type Prompt struct { - Title string `json:"title" binding:"required"` - Content string `json:"content" binding:"required"` - Language string `json:"language" binding:"required"` - Tags []string `json:"tags"` - Type string `json:"type"` // "text|image|video|audio" - Source string `json:"source"` - Author string `json:"author"` - Time string `json:"time"` - Copyright string `json:"copyright"` - Feedback []string `json:"feedback"` -} - type PromptOutput struct { Prompt FilePath string `json:"file_path"` @@ -131,3 +120,44 @@ type CreatePromptReq struct { type UpdatePromptReq struct { Prompt } + +type Prompt struct { + Title string `json:"title" binding:"required"` + Content string `json:"content" binding:"required"` + Language string `json:"language" binding:"required"` + Tags []string `json:"tags"` + Type string `json:"type"` // "text|image|video|audio" + Source string `json:"source"` + Author string `json:"author"` + Time string `json:"time"` + Copyright string `json:"copyright"` + Feedback []string `json:"feedback"` +} + +func (req *Prompt) GetSensitiveFields() []SensitiveField { + var fields []SensitiveField + fields = append(fields, SensitiveField{ + Name: "title", + Value: func() string { + return req.Title + }, + Scenario: string(sensitive.ScenarioCommentDetection), + }) + fields = append(fields, SensitiveField{ + Name: "content", + Value: func() string { + return req.Content + }, + Scenario: string(sensitive.ScenarioCommentDetection), + }) + if len(req.Source) > 0 { + fields = append(fields, SensitiveField{ + Name: "source", + Value: func() string { + return req.Source + }, + Scenario: string(sensitive.ScenarioCommentDetection), + }) + } + return fields +} diff --git a/component/prompt.go b/component/prompt.go index 6fdadcb2..dbe41c1b 100644 --- a/component/prompt.go +++ b/component/prompt.go @@ -17,7 +17,6 @@ import ( "opencsg.com/csghub-server/builder/git/membership" "opencsg.com/csghub-server/builder/llm" "opencsg.com/csghub-server/builder/rpc" - "opencsg.com/csghub-server/builder/sensitive" "opencsg.com/csghub-server/builder/store/database" "opencsg.com/csghub-server/common/config" "opencsg.com/csghub-server/common/types" @@ -48,11 +47,11 @@ type promptComponentImpl struct { } type PromptComponent interface { - ListPrompt(ctx context.Context, req types.PromptReq) ([]PromptOutput, error) - GetPrompt(ctx context.Context, req types.PromptReq) (*PromptOutput, error) - ParseJsonFile(ctx context.Context, req gitserver.GetRepoInfoByPathReq) (*PromptOutput, error) - CreatePrompt(ctx context.Context, req types.PromptReq, body *CreatePromptReq) (*Prompt, error) - UpdatePrompt(ctx context.Context, req types.PromptReq, body *UpdatePromptReq) (*Prompt, error) + ListPrompt(ctx context.Context, req types.PromptReq) ([]types.PromptOutput, error) + GetPrompt(ctx context.Context, req types.PromptReq) (*types.PromptOutput, error) + ParseJsonFile(ctx context.Context, req gitserver.GetRepoInfoByPathReq) (*types.PromptOutput, error) + CreatePrompt(ctx context.Context, req types.PromptReq, body *types.CreatePromptReq) (*types.Prompt, error) + UpdatePrompt(ctx context.Context, req types.PromptReq, body *types.UpdatePromptReq) (*types.Prompt, error) DeletePrompt(ctx context.Context, req types.PromptReq) error NewConversation(ctx context.Context, req types.ConversationTitleReq) (*database.PromptConversation, error) ListConversationsByUserID(ctx context.Context, currentUser string) ([]database.PromptConversation, error) @@ -104,7 +103,7 @@ func NewPromptComponent(cfg *config.Config) (PromptComponent, error) { }, nil } -func (c *promptComponentImpl) ListPrompt(ctx context.Context, req types.PromptReq) ([]PromptOutput, error) { +func (c *promptComponentImpl) ListPrompt(ctx context.Context, req types.PromptReq) ([]types.PromptOutput, error) { r, err := c.repoStore.FindByPath(ctx, types.PromptRepo, req.Namespace, req.Name) if err != nil { return nil, fmt.Errorf("failed to find prompt set, error: %w", err) @@ -129,9 +128,9 @@ func (c *promptComponentImpl) ListPrompt(ctx context.Context, req types.PromptRe if tree == nil { return nil, fmt.Errorf("failed to find any files") } - var prompts []PromptOutput + var prompts []types.PromptOutput wg := &sync.WaitGroup{} - chPrompts := make(chan *PromptOutput, len(tree)) + chPrompts := make(chan *types.PromptOutput, len(tree)) done := make(chan struct{}, 1) go func() { @@ -177,7 +176,7 @@ func (c *promptComponentImpl) ListPrompt(ctx context.Context, req types.PromptRe return prompts, nil } -func (c *promptComponentImpl) GetPrompt(ctx context.Context, req types.PromptReq) (*PromptOutput, error) { +func (c *promptComponentImpl) GetPrompt(ctx context.Context, req types.PromptReq) (*types.PromptOutput, error) { r, err := c.repoStore.FindByPath(ctx, types.PromptRepo, req.Namespace, req.Name) if err != nil { return nil, fmt.Errorf("failed to find prompt repo, error: %w", err) @@ -207,7 +206,7 @@ func (c *promptComponentImpl) GetPrompt(ctx context.Context, req types.PromptReq return p, nil } -func (c *promptComponentImpl) ParseJsonFile(ctx context.Context, req gitserver.GetRepoInfoByPathReq) (*PromptOutput, error) { +func (c *promptComponentImpl) ParseJsonFile(ctx context.Context, req gitserver.GetRepoInfoByPathReq) (*types.PromptOutput, error) { f, err := c.gitServer.GetRepoFileContents(ctx, req) if err != nil { return nil, fmt.Errorf("failed to get %s contents, cause:%w", req.Path, err) @@ -216,7 +215,7 @@ func (c *promptComponentImpl) ParseJsonFile(ctx context.Context, req gitserver.G if err != nil { return nil, fmt.Errorf("failed to base64 decode %s contents, cause:%w", req.Path, err) } - var prompt Prompt + var prompt types.Prompt err = yaml.Unmarshal(decodedContent, &prompt) if err != nil { return nil, fmt.Errorf("failed to Unmarshal %s contents, cause: %w, decodedContent: %v", req.Path, err, string(decodedContent)) @@ -224,14 +223,14 @@ func (c *promptComponentImpl) ParseJsonFile(ctx context.Context, req gitserver.G if len(prompt.Title) < 1 { prompt.Title = f.Name } - po := PromptOutput{ + po := types.PromptOutput{ Prompt: prompt, FilePath: req.Path, } return &po, nil } -func (c *promptComponentImpl) CreatePrompt(ctx context.Context, req types.PromptReq, body *CreatePromptReq) (*Prompt, error) { +func (c *promptComponentImpl) CreatePrompt(ctx context.Context, req types.PromptReq, body *types.CreatePromptReq) (*types.Prompt, error) { u, err := c.checkPromptRepoPermission(ctx, req) if err != nil { return nil, fmt.Errorf("user do not allowed create prompt") @@ -267,7 +266,7 @@ func (c *promptComponentImpl) CreatePrompt(ctx context.Context, req types.Prompt return &body.Prompt, nil } -func (c *promptComponentImpl) UpdatePrompt(ctx context.Context, req types.PromptReq, body *UpdatePromptReq) (*Prompt, error) { +func (c *promptComponentImpl) UpdatePrompt(ctx context.Context, req types.PromptReq, body *types.UpdatePromptReq) (*types.Prompt, error) { u, err := c.checkPromptRepoPermission(ctx, req) if err != nil { return nil, fmt.Errorf("user do not allowed update prompt") @@ -1161,63 +1160,7 @@ func (c *promptComponentImpl) getRelations(ctx context.Context, repoID int64, cu return rels, nil } -type Prompt struct { - Title string `json:"title" binding:"required"` - Content string `json:"content" binding:"required"` - Language string `json:"language" binding:"required"` - Tags []string `json:"tags"` - Type string `json:"type"` // "text|image|video|audio" - Source string `json:"source"` - Author string `json:"author"` - Time string `json:"time"` - Copyright string `json:"copyright"` - Feedback []string `json:"feedback"` -} - -type PromptOutput struct { - Prompt - FilePath string `json:"file_path"` - CanWrite bool `json:"can_write"` - CanManage bool `json:"can_manage"` -} - -type CreatePromptReq struct { - Prompt -} - -type UpdatePromptReq struct { - Prompt -} - -var _ types.SensitiveRequestV2 = (*Prompt)(nil) - -func (req *Prompt) GetSensitiveFields() []types.SensitiveField { - var fields []types.SensitiveField - fields = append(fields, types.SensitiveField{ - Name: "title", - Value: func() string { - return req.Title - }, - Scenario: string(sensitive.ScenarioCommentDetection), - }) - fields = append(fields, types.SensitiveField{ - Name: "content", - Value: func() string { - return req.Content - }, - Scenario: string(sensitive.ScenarioCommentDetection), - }) - if len(req.Source) > 0 { - fields = append(fields, types.SensitiveField{ - Name: "source", - Value: func() string { - return req.Source - }, - Scenario: string(sensitive.ScenarioCommentDetection), - }) - } - return fields -} +var _ types.SensitiveRequestV2 = (*types.Prompt)(nil) func (c *promptComponentImpl) OrgPrompts(ctx context.Context, req *types.OrgPromptsReq) ([]types.PromptRes, int, error) { var resPrompts []types.PromptRes diff --git a/component/prompt_test.go b/component/prompt_test.go index 30ce73ce..5704c66c 100644 --- a/component/prompt_test.go +++ b/component/prompt_test.go @@ -133,8 +133,8 @@ func TestPromptComponent_CreatePrompt(t *testing.T) { Name: "n", CurrentUser: "foo", Path: "p", - }, &CreatePromptReq{ - Prompt: Prompt{Title: "TEST", Content: "test"}, + }, &types.CreatePromptReq{ + Prompt: types.Prompt{Title: "TEST", Content: "test"}, }) require.NotNil(t, err) return @@ -166,8 +166,8 @@ func TestPromptComponent_CreatePrompt(t *testing.T) { Name: "n", CurrentUser: "foo", Path: "p", - }, &CreatePromptReq{ - Prompt: Prompt{Title: "TEST", Content: "test"}, + }, &types.CreatePromptReq{ + Prompt: types.Prompt{Title: "TEST", Content: "test"}, }) require.Nil(t, err) @@ -204,8 +204,8 @@ func TestPromptComponent_UpdatePrompt(t *testing.T) { Name: "n", CurrentUser: "foo", Path: "TEST.jsonl", - }, &UpdatePromptReq{ - Prompt: Prompt{Title: "TEST.jsonl", Content: "test"}, + }, &types.UpdatePromptReq{ + Prompt: types.Prompt{Title: "TEST.jsonl", Content: "test"}, }) require.NotNil(t, err) return @@ -237,8 +237,8 @@ func TestPromptComponent_UpdatePrompt(t *testing.T) { Name: "n", CurrentUser: "foo", Path: "TEST.jsonl", - }, &UpdatePromptReq{ - Prompt: Prompt{Title: "TEST", Content: "test"}, + }, &types.UpdatePromptReq{ + Prompt: types.Prompt{Title: "TEST", Content: "test"}, }) require.Nil(t, err) From e8f0d00e62f8956e2ba5a94f49d2877a7f6566e9 Mon Sep 17 00:00:00 2001 From: "yiling.ji" Date: Mon, 23 Dec 2024 05:50:30 +0000 Subject: [PATCH 4/5] Merge branch 'feature/handler_tests' into 'main' Add model/user/git-http handler tests See merge request product/starhub/starhub-server!751 --- .mockery.yaml | 3 + .../component/mock_GitHTTPComponent.go | 647 +++++++++ .../component/mock_ModelComponent.go | 1108 ++++++++++++++ .../component/mock_UserComponent.go | 1277 +++++++++++++++++ api/handler/git_http.go | 36 +- api/handler/git_http_test.go | 315 ++++ api/handler/helper_test.go | 26 +- api/handler/model.go | 58 +- api/handler/model_test.go | 561 ++++++++ api/handler/user.go | 52 +- api/handler/user_test.go | 439 ++++++ common/types/user.go | 8 +- component/user.go | 12 +- component/user_test.go | 32 +- 14 files changed, 4470 insertions(+), 104 deletions(-) create mode 100644 _mocks/opencsg.com/csghub-server/component/mock_GitHTTPComponent.go create mode 100644 _mocks/opencsg.com/csghub-server/component/mock_ModelComponent.go create mode 100644 _mocks/opencsg.com/csghub-server/component/mock_UserComponent.go create mode 100644 api/handler/git_http_test.go create mode 100644 api/handler/model_test.go create mode 100644 api/handler/user_test.go diff --git a/.mockery.yaml b/.mockery.yaml index 64fb75b7..04274577 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -19,6 +19,9 @@ packages: SensitiveComponent: CodeComponent: PromptComponent: + ModelComponent: + UserComponent: + GitHTTPComponent: opencsg.com/csghub-server/user/component: config: interfaces: diff --git a/_mocks/opencsg.com/csghub-server/component/mock_GitHTTPComponent.go b/_mocks/opencsg.com/csghub-server/component/mock_GitHTTPComponent.go new file mode 100644 index 00000000..1b10efa4 --- /dev/null +++ b/_mocks/opencsg.com/csghub-server/component/mock_GitHTTPComponent.go @@ -0,0 +1,647 @@ +// Code generated by mockery v2.49.1. DO NOT EDIT. + +package component + +import ( + context "context" + io "io" + + database "opencsg.com/csghub-server/builder/store/database" + + mock "github.com/stretchr/testify/mock" + + types "opencsg.com/csghub-server/common/types" + + url "net/url" +) + +// MockGitHTTPComponent is an autogenerated mock type for the GitHTTPComponent type +type MockGitHTTPComponent struct { + mock.Mock +} + +type MockGitHTTPComponent_Expecter struct { + mock *mock.Mock +} + +func (_m *MockGitHTTPComponent) EXPECT() *MockGitHTTPComponent_Expecter { + return &MockGitHTTPComponent_Expecter{mock: &_m.Mock} +} + +// BuildObjectResponse provides a mock function with given fields: ctx, req, isUpload +func (_m *MockGitHTTPComponent) BuildObjectResponse(ctx context.Context, req types.BatchRequest, isUpload bool) (*types.BatchResponse, error) { + ret := _m.Called(ctx, req, isUpload) + + if len(ret) == 0 { + panic("no return value specified for BuildObjectResponse") + } + + var r0 *types.BatchResponse + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.BatchRequest, bool) (*types.BatchResponse, error)); ok { + return rf(ctx, req, isUpload) + } + if rf, ok := ret.Get(0).(func(context.Context, types.BatchRequest, bool) *types.BatchResponse); ok { + r0 = rf(ctx, req, isUpload) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.BatchResponse) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.BatchRequest, bool) error); ok { + r1 = rf(ctx, req, isUpload) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockGitHTTPComponent_BuildObjectResponse_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BuildObjectResponse' +type MockGitHTTPComponent_BuildObjectResponse_Call struct { + *mock.Call +} + +// BuildObjectResponse is a helper method to define mock.On call +// - ctx context.Context +// - req types.BatchRequest +// - isUpload bool +func (_e *MockGitHTTPComponent_Expecter) BuildObjectResponse(ctx interface{}, req interface{}, isUpload interface{}) *MockGitHTTPComponent_BuildObjectResponse_Call { + return &MockGitHTTPComponent_BuildObjectResponse_Call{Call: _e.mock.On("BuildObjectResponse", ctx, req, isUpload)} +} + +func (_c *MockGitHTTPComponent_BuildObjectResponse_Call) Run(run func(ctx context.Context, req types.BatchRequest, isUpload bool)) *MockGitHTTPComponent_BuildObjectResponse_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.BatchRequest), args[2].(bool)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_BuildObjectResponse_Call) Return(_a0 *types.BatchResponse, _a1 error) *MockGitHTTPComponent_BuildObjectResponse_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockGitHTTPComponent_BuildObjectResponse_Call) RunAndReturn(run func(context.Context, types.BatchRequest, bool) (*types.BatchResponse, error)) *MockGitHTTPComponent_BuildObjectResponse_Call { + _c.Call.Return(run) + return _c +} + +// CreateLock provides a mock function with given fields: ctx, req +func (_m *MockGitHTTPComponent) CreateLock(ctx context.Context, req types.LfsLockReq) (*database.LfsLock, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for CreateLock") + } + + var r0 *database.LfsLock + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.LfsLockReq) (*database.LfsLock, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.LfsLockReq) *database.LfsLock); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*database.LfsLock) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.LfsLockReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockGitHTTPComponent_CreateLock_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CreateLock' +type MockGitHTTPComponent_CreateLock_Call struct { + *mock.Call +} + +// CreateLock is a helper method to define mock.On call +// - ctx context.Context +// - req types.LfsLockReq +func (_e *MockGitHTTPComponent_Expecter) CreateLock(ctx interface{}, req interface{}) *MockGitHTTPComponent_CreateLock_Call { + return &MockGitHTTPComponent_CreateLock_Call{Call: _e.mock.On("CreateLock", ctx, req)} +} + +func (_c *MockGitHTTPComponent_CreateLock_Call) Run(run func(ctx context.Context, req types.LfsLockReq)) *MockGitHTTPComponent_CreateLock_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.LfsLockReq)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_CreateLock_Call) Return(_a0 *database.LfsLock, _a1 error) *MockGitHTTPComponent_CreateLock_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockGitHTTPComponent_CreateLock_Call) RunAndReturn(run func(context.Context, types.LfsLockReq) (*database.LfsLock, error)) *MockGitHTTPComponent_CreateLock_Call { + _c.Call.Return(run) + return _c +} + +// GitReceivePack provides a mock function with given fields: ctx, req +func (_m *MockGitHTTPComponent) GitReceivePack(ctx context.Context, req types.GitReceivePackReq) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for GitReceivePack") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.GitReceivePackReq) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockGitHTTPComponent_GitReceivePack_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GitReceivePack' +type MockGitHTTPComponent_GitReceivePack_Call struct { + *mock.Call +} + +// GitReceivePack is a helper method to define mock.On call +// - ctx context.Context +// - req types.GitReceivePackReq +func (_e *MockGitHTTPComponent_Expecter) GitReceivePack(ctx interface{}, req interface{}) *MockGitHTTPComponent_GitReceivePack_Call { + return &MockGitHTTPComponent_GitReceivePack_Call{Call: _e.mock.On("GitReceivePack", ctx, req)} +} + +func (_c *MockGitHTTPComponent_GitReceivePack_Call) Run(run func(ctx context.Context, req types.GitReceivePackReq)) *MockGitHTTPComponent_GitReceivePack_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.GitReceivePackReq)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_GitReceivePack_Call) Return(_a0 error) *MockGitHTTPComponent_GitReceivePack_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockGitHTTPComponent_GitReceivePack_Call) RunAndReturn(run func(context.Context, types.GitReceivePackReq) error) *MockGitHTTPComponent_GitReceivePack_Call { + _c.Call.Return(run) + return _c +} + +// GitUploadPack provides a mock function with given fields: ctx, req +func (_m *MockGitHTTPComponent) GitUploadPack(ctx context.Context, req types.GitUploadPackReq) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for GitUploadPack") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.GitUploadPackReq) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockGitHTTPComponent_GitUploadPack_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GitUploadPack' +type MockGitHTTPComponent_GitUploadPack_Call struct { + *mock.Call +} + +// GitUploadPack is a helper method to define mock.On call +// - ctx context.Context +// - req types.GitUploadPackReq +func (_e *MockGitHTTPComponent_Expecter) GitUploadPack(ctx interface{}, req interface{}) *MockGitHTTPComponent_GitUploadPack_Call { + return &MockGitHTTPComponent_GitUploadPack_Call{Call: _e.mock.On("GitUploadPack", ctx, req)} +} + +func (_c *MockGitHTTPComponent_GitUploadPack_Call) Run(run func(ctx context.Context, req types.GitUploadPackReq)) *MockGitHTTPComponent_GitUploadPack_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.GitUploadPackReq)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_GitUploadPack_Call) Return(_a0 error) *MockGitHTTPComponent_GitUploadPack_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockGitHTTPComponent_GitUploadPack_Call) RunAndReturn(run func(context.Context, types.GitUploadPackReq) error) *MockGitHTTPComponent_GitUploadPack_Call { + _c.Call.Return(run) + return _c +} + +// InfoRefs provides a mock function with given fields: ctx, req +func (_m *MockGitHTTPComponent) InfoRefs(ctx context.Context, req types.InfoRefsReq) (io.Reader, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for InfoRefs") + } + + var r0 io.Reader + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.InfoRefsReq) (io.Reader, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.InfoRefsReq) io.Reader); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(io.Reader) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.InfoRefsReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockGitHTTPComponent_InfoRefs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'InfoRefs' +type MockGitHTTPComponent_InfoRefs_Call struct { + *mock.Call +} + +// InfoRefs is a helper method to define mock.On call +// - ctx context.Context +// - req types.InfoRefsReq +func (_e *MockGitHTTPComponent_Expecter) InfoRefs(ctx interface{}, req interface{}) *MockGitHTTPComponent_InfoRefs_Call { + return &MockGitHTTPComponent_InfoRefs_Call{Call: _e.mock.On("InfoRefs", ctx, req)} +} + +func (_c *MockGitHTTPComponent_InfoRefs_Call) Run(run func(ctx context.Context, req types.InfoRefsReq)) *MockGitHTTPComponent_InfoRefs_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.InfoRefsReq)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_InfoRefs_Call) Return(_a0 io.Reader, _a1 error) *MockGitHTTPComponent_InfoRefs_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockGitHTTPComponent_InfoRefs_Call) RunAndReturn(run func(context.Context, types.InfoRefsReq) (io.Reader, error)) *MockGitHTTPComponent_InfoRefs_Call { + _c.Call.Return(run) + return _c +} + +// LfsDownload provides a mock function with given fields: ctx, req +func (_m *MockGitHTTPComponent) LfsDownload(ctx context.Context, req types.DownloadRequest) (*url.URL, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for LfsDownload") + } + + var r0 *url.URL + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.DownloadRequest) (*url.URL, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.DownloadRequest) *url.URL); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*url.URL) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.DownloadRequest) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockGitHTTPComponent_LfsDownload_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LfsDownload' +type MockGitHTTPComponent_LfsDownload_Call struct { + *mock.Call +} + +// LfsDownload is a helper method to define mock.On call +// - ctx context.Context +// - req types.DownloadRequest +func (_e *MockGitHTTPComponent_Expecter) LfsDownload(ctx interface{}, req interface{}) *MockGitHTTPComponent_LfsDownload_Call { + return &MockGitHTTPComponent_LfsDownload_Call{Call: _e.mock.On("LfsDownload", ctx, req)} +} + +func (_c *MockGitHTTPComponent_LfsDownload_Call) Run(run func(ctx context.Context, req types.DownloadRequest)) *MockGitHTTPComponent_LfsDownload_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.DownloadRequest)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_LfsDownload_Call) Return(_a0 *url.URL, _a1 error) *MockGitHTTPComponent_LfsDownload_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockGitHTTPComponent_LfsDownload_Call) RunAndReturn(run func(context.Context, types.DownloadRequest) (*url.URL, error)) *MockGitHTTPComponent_LfsDownload_Call { + _c.Call.Return(run) + return _c +} + +// LfsUpload provides a mock function with given fields: ctx, body, req +func (_m *MockGitHTTPComponent) LfsUpload(ctx context.Context, body io.ReadCloser, req types.UploadRequest) error { + ret := _m.Called(ctx, body, req) + + if len(ret) == 0 { + panic("no return value specified for LfsUpload") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, io.ReadCloser, types.UploadRequest) error); ok { + r0 = rf(ctx, body, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockGitHTTPComponent_LfsUpload_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LfsUpload' +type MockGitHTTPComponent_LfsUpload_Call struct { + *mock.Call +} + +// LfsUpload is a helper method to define mock.On call +// - ctx context.Context +// - body io.ReadCloser +// - req types.UploadRequest +func (_e *MockGitHTTPComponent_Expecter) LfsUpload(ctx interface{}, body interface{}, req interface{}) *MockGitHTTPComponent_LfsUpload_Call { + return &MockGitHTTPComponent_LfsUpload_Call{Call: _e.mock.On("LfsUpload", ctx, body, req)} +} + +func (_c *MockGitHTTPComponent_LfsUpload_Call) Run(run func(ctx context.Context, body io.ReadCloser, req types.UploadRequest)) *MockGitHTTPComponent_LfsUpload_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(io.ReadCloser), args[2].(types.UploadRequest)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_LfsUpload_Call) Return(_a0 error) *MockGitHTTPComponent_LfsUpload_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockGitHTTPComponent_LfsUpload_Call) RunAndReturn(run func(context.Context, io.ReadCloser, types.UploadRequest) error) *MockGitHTTPComponent_LfsUpload_Call { + _c.Call.Return(run) + return _c +} + +// LfsVerify provides a mock function with given fields: ctx, req, p +func (_m *MockGitHTTPComponent) LfsVerify(ctx context.Context, req types.VerifyRequest, p types.Pointer) error { + ret := _m.Called(ctx, req, p) + + if len(ret) == 0 { + panic("no return value specified for LfsVerify") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.VerifyRequest, types.Pointer) error); ok { + r0 = rf(ctx, req, p) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockGitHTTPComponent_LfsVerify_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LfsVerify' +type MockGitHTTPComponent_LfsVerify_Call struct { + *mock.Call +} + +// LfsVerify is a helper method to define mock.On call +// - ctx context.Context +// - req types.VerifyRequest +// - p types.Pointer +func (_e *MockGitHTTPComponent_Expecter) LfsVerify(ctx interface{}, req interface{}, p interface{}) *MockGitHTTPComponent_LfsVerify_Call { + return &MockGitHTTPComponent_LfsVerify_Call{Call: _e.mock.On("LfsVerify", ctx, req, p)} +} + +func (_c *MockGitHTTPComponent_LfsVerify_Call) Run(run func(ctx context.Context, req types.VerifyRequest, p types.Pointer)) *MockGitHTTPComponent_LfsVerify_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.VerifyRequest), args[2].(types.Pointer)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_LfsVerify_Call) Return(_a0 error) *MockGitHTTPComponent_LfsVerify_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockGitHTTPComponent_LfsVerify_Call) RunAndReturn(run func(context.Context, types.VerifyRequest, types.Pointer) error) *MockGitHTTPComponent_LfsVerify_Call { + _c.Call.Return(run) + return _c +} + +// ListLocks provides a mock function with given fields: ctx, req +func (_m *MockGitHTTPComponent) ListLocks(ctx context.Context, req types.ListLFSLockReq) (*types.LFSLockList, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for ListLocks") + } + + var r0 *types.LFSLockList + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.ListLFSLockReq) (*types.LFSLockList, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.ListLFSLockReq) *types.LFSLockList); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.LFSLockList) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.ListLFSLockReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockGitHTTPComponent_ListLocks_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListLocks' +type MockGitHTTPComponent_ListLocks_Call struct { + *mock.Call +} + +// ListLocks is a helper method to define mock.On call +// - ctx context.Context +// - req types.ListLFSLockReq +func (_e *MockGitHTTPComponent_Expecter) ListLocks(ctx interface{}, req interface{}) *MockGitHTTPComponent_ListLocks_Call { + return &MockGitHTTPComponent_ListLocks_Call{Call: _e.mock.On("ListLocks", ctx, req)} +} + +func (_c *MockGitHTTPComponent_ListLocks_Call) Run(run func(ctx context.Context, req types.ListLFSLockReq)) *MockGitHTTPComponent_ListLocks_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.ListLFSLockReq)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_ListLocks_Call) Return(_a0 *types.LFSLockList, _a1 error) *MockGitHTTPComponent_ListLocks_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockGitHTTPComponent_ListLocks_Call) RunAndReturn(run func(context.Context, types.ListLFSLockReq) (*types.LFSLockList, error)) *MockGitHTTPComponent_ListLocks_Call { + _c.Call.Return(run) + return _c +} + +// UnLock provides a mock function with given fields: ctx, req +func (_m *MockGitHTTPComponent) UnLock(ctx context.Context, req types.UnlockLFSReq) (*database.LfsLock, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for UnLock") + } + + var r0 *database.LfsLock + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.UnlockLFSReq) (*database.LfsLock, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.UnlockLFSReq) *database.LfsLock); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*database.LfsLock) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.UnlockLFSReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockGitHTTPComponent_UnLock_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UnLock' +type MockGitHTTPComponent_UnLock_Call struct { + *mock.Call +} + +// UnLock is a helper method to define mock.On call +// - ctx context.Context +// - req types.UnlockLFSReq +func (_e *MockGitHTTPComponent_Expecter) UnLock(ctx interface{}, req interface{}) *MockGitHTTPComponent_UnLock_Call { + return &MockGitHTTPComponent_UnLock_Call{Call: _e.mock.On("UnLock", ctx, req)} +} + +func (_c *MockGitHTTPComponent_UnLock_Call) Run(run func(ctx context.Context, req types.UnlockLFSReq)) *MockGitHTTPComponent_UnLock_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.UnlockLFSReq)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_UnLock_Call) Return(_a0 *database.LfsLock, _a1 error) *MockGitHTTPComponent_UnLock_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockGitHTTPComponent_UnLock_Call) RunAndReturn(run func(context.Context, types.UnlockLFSReq) (*database.LfsLock, error)) *MockGitHTTPComponent_UnLock_Call { + _c.Call.Return(run) + return _c +} + +// VerifyLock provides a mock function with given fields: ctx, req +func (_m *MockGitHTTPComponent) VerifyLock(ctx context.Context, req types.VerifyLFSLockReq) (*types.LFSLockListVerify, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for VerifyLock") + } + + var r0 *types.LFSLockListVerify + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.VerifyLFSLockReq) (*types.LFSLockListVerify, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.VerifyLFSLockReq) *types.LFSLockListVerify); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.LFSLockListVerify) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.VerifyLFSLockReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockGitHTTPComponent_VerifyLock_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'VerifyLock' +type MockGitHTTPComponent_VerifyLock_Call struct { + *mock.Call +} + +// VerifyLock is a helper method to define mock.On call +// - ctx context.Context +// - req types.VerifyLFSLockReq +func (_e *MockGitHTTPComponent_Expecter) VerifyLock(ctx interface{}, req interface{}) *MockGitHTTPComponent_VerifyLock_Call { + return &MockGitHTTPComponent_VerifyLock_Call{Call: _e.mock.On("VerifyLock", ctx, req)} +} + +func (_c *MockGitHTTPComponent_VerifyLock_Call) Run(run func(ctx context.Context, req types.VerifyLFSLockReq)) *MockGitHTTPComponent_VerifyLock_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.VerifyLFSLockReq)) + }) + return _c +} + +func (_c *MockGitHTTPComponent_VerifyLock_Call) Return(_a0 *types.LFSLockListVerify, _a1 error) *MockGitHTTPComponent_VerifyLock_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockGitHTTPComponent_VerifyLock_Call) RunAndReturn(run func(context.Context, types.VerifyLFSLockReq) (*types.LFSLockListVerify, error)) *MockGitHTTPComponent_VerifyLock_Call { + _c.Call.Return(run) + return _c +} + +// NewMockGitHTTPComponent creates a new instance of MockGitHTTPComponent. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockGitHTTPComponent(t interface { + mock.TestingT + Cleanup(func()) +}) *MockGitHTTPComponent { + mock := &MockGitHTTPComponent{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/_mocks/opencsg.com/csghub-server/component/mock_ModelComponent.go b/_mocks/opencsg.com/csghub-server/component/mock_ModelComponent.go new file mode 100644 index 00000000..fc5bc4d2 --- /dev/null +++ b/_mocks/opencsg.com/csghub-server/component/mock_ModelComponent.go @@ -0,0 +1,1108 @@ +// Code generated by mockery v2.49.1. DO NOT EDIT. + +package component + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" + database "opencsg.com/csghub-server/builder/store/database" + + types "opencsg.com/csghub-server/common/types" +) + +// MockModelComponent is an autogenerated mock type for the ModelComponent type +type MockModelComponent struct { + mock.Mock +} + +type MockModelComponent_Expecter struct { + mock *mock.Mock +} + +func (_m *MockModelComponent) EXPECT() *MockModelComponent_Expecter { + return &MockModelComponent_Expecter{mock: &_m.Mock} +} + +// AddRelationDataset provides a mock function with given fields: ctx, req +func (_m *MockModelComponent) AddRelationDataset(ctx context.Context, req types.RelationDataset) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for AddRelationDataset") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.RelationDataset) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockModelComponent_AddRelationDataset_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AddRelationDataset' +type MockModelComponent_AddRelationDataset_Call struct { + *mock.Call +} + +// AddRelationDataset is a helper method to define mock.On call +// - ctx context.Context +// - req types.RelationDataset +func (_e *MockModelComponent_Expecter) AddRelationDataset(ctx interface{}, req interface{}) *MockModelComponent_AddRelationDataset_Call { + return &MockModelComponent_AddRelationDataset_Call{Call: _e.mock.On("AddRelationDataset", ctx, req)} +} + +func (_c *MockModelComponent_AddRelationDataset_Call) Run(run func(ctx context.Context, req types.RelationDataset)) *MockModelComponent_AddRelationDataset_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.RelationDataset)) + }) + return _c +} + +func (_c *MockModelComponent_AddRelationDataset_Call) Return(_a0 error) *MockModelComponent_AddRelationDataset_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockModelComponent_AddRelationDataset_Call) RunAndReturn(run func(context.Context, types.RelationDataset) error) *MockModelComponent_AddRelationDataset_Call { + _c.Call.Return(run) + return _c +} + +// Create provides a mock function with given fields: ctx, req +func (_m *MockModelComponent) Create(ctx context.Context, req *types.CreateModelReq) (*types.Model, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Create") + } + + var r0 *types.Model + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *types.CreateModelReq) (*types.Model, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.CreateModelReq) *types.Model); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.CreateModelReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_Create_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Create' +type MockModelComponent_Create_Call struct { + *mock.Call +} + +// Create is a helper method to define mock.On call +// - ctx context.Context +// - req *types.CreateModelReq +func (_e *MockModelComponent_Expecter) Create(ctx interface{}, req interface{}) *MockModelComponent_Create_Call { + return &MockModelComponent_Create_Call{Call: _e.mock.On("Create", ctx, req)} +} + +func (_c *MockModelComponent_Create_Call) Run(run func(ctx context.Context, req *types.CreateModelReq)) *MockModelComponent_Create_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.CreateModelReq)) + }) + return _c +} + +func (_c *MockModelComponent_Create_Call) Return(_a0 *types.Model, _a1 error) *MockModelComponent_Create_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_Create_Call) RunAndReturn(run func(context.Context, *types.CreateModelReq) (*types.Model, error)) *MockModelComponent_Create_Call { + _c.Call.Return(run) + return _c +} + +// DelRelationDataset provides a mock function with given fields: ctx, req +func (_m *MockModelComponent) DelRelationDataset(ctx context.Context, req types.RelationDataset) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for DelRelationDataset") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.RelationDataset) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockModelComponent_DelRelationDataset_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DelRelationDataset' +type MockModelComponent_DelRelationDataset_Call struct { + *mock.Call +} + +// DelRelationDataset is a helper method to define mock.On call +// - ctx context.Context +// - req types.RelationDataset +func (_e *MockModelComponent_Expecter) DelRelationDataset(ctx interface{}, req interface{}) *MockModelComponent_DelRelationDataset_Call { + return &MockModelComponent_DelRelationDataset_Call{Call: _e.mock.On("DelRelationDataset", ctx, req)} +} + +func (_c *MockModelComponent_DelRelationDataset_Call) Run(run func(ctx context.Context, req types.RelationDataset)) *MockModelComponent_DelRelationDataset_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.RelationDataset)) + }) + return _c +} + +func (_c *MockModelComponent_DelRelationDataset_Call) Return(_a0 error) *MockModelComponent_DelRelationDataset_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockModelComponent_DelRelationDataset_Call) RunAndReturn(run func(context.Context, types.RelationDataset) error) *MockModelComponent_DelRelationDataset_Call { + _c.Call.Return(run) + return _c +} + +// Delete provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockModelComponent) Delete(ctx context.Context, namespace string, name string, currentUser string) error { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for Delete") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) error); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockModelComponent_Delete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Delete' +type MockModelComponent_Delete_Call struct { + *mock.Call +} + +// Delete is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockModelComponent_Expecter) Delete(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockModelComponent_Delete_Call { + return &MockModelComponent_Delete_Call{Call: _e.mock.On("Delete", ctx, namespace, name, currentUser)} +} + +func (_c *MockModelComponent_Delete_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockModelComponent_Delete_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockModelComponent_Delete_Call) Return(_a0 error) *MockModelComponent_Delete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockModelComponent_Delete_Call) RunAndReturn(run func(context.Context, string, string, string) error) *MockModelComponent_Delete_Call { + _c.Call.Return(run) + return _c +} + +// DeleteRuntimeFrameworkModes provides a mock function with given fields: ctx, deployType, id, paths +func (_m *MockModelComponent) DeleteRuntimeFrameworkModes(ctx context.Context, deployType int, id int64, paths []string) ([]string, error) { + ret := _m.Called(ctx, deployType, id, paths) + + if len(ret) == 0 { + panic("no return value specified for DeleteRuntimeFrameworkModes") + } + + var r0 []string + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, int, int64, []string) ([]string, error)); ok { + return rf(ctx, deployType, id, paths) + } + if rf, ok := ret.Get(0).(func(context.Context, int, int64, []string) []string); ok { + r0 = rf(ctx, deployType, id, paths) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, int, int64, []string) error); ok { + r1 = rf(ctx, deployType, id, paths) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_DeleteRuntimeFrameworkModes_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeleteRuntimeFrameworkModes' +type MockModelComponent_DeleteRuntimeFrameworkModes_Call struct { + *mock.Call +} + +// DeleteRuntimeFrameworkModes is a helper method to define mock.On call +// - ctx context.Context +// - deployType int +// - id int64 +// - paths []string +func (_e *MockModelComponent_Expecter) DeleteRuntimeFrameworkModes(ctx interface{}, deployType interface{}, id interface{}, paths interface{}) *MockModelComponent_DeleteRuntimeFrameworkModes_Call { + return &MockModelComponent_DeleteRuntimeFrameworkModes_Call{Call: _e.mock.On("DeleteRuntimeFrameworkModes", ctx, deployType, id, paths)} +} + +func (_c *MockModelComponent_DeleteRuntimeFrameworkModes_Call) Run(run func(ctx context.Context, deployType int, id int64, paths []string)) *MockModelComponent_DeleteRuntimeFrameworkModes_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(int), args[2].(int64), args[3].([]string)) + }) + return _c +} + +func (_c *MockModelComponent_DeleteRuntimeFrameworkModes_Call) Return(_a0 []string, _a1 error) *MockModelComponent_DeleteRuntimeFrameworkModes_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_DeleteRuntimeFrameworkModes_Call) RunAndReturn(run func(context.Context, int, int64, []string) ([]string, error)) *MockModelComponent_DeleteRuntimeFrameworkModes_Call { + _c.Call.Return(run) + return _c +} + +// Deploy provides a mock function with given fields: ctx, deployReq, req +func (_m *MockModelComponent) Deploy(ctx context.Context, deployReq types.DeployActReq, req types.ModelRunReq) (int64, error) { + ret := _m.Called(ctx, deployReq, req) + + if len(ret) == 0 { + panic("no return value specified for Deploy") + } + + var r0 int64 + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.DeployActReq, types.ModelRunReq) (int64, error)); ok { + return rf(ctx, deployReq, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.DeployActReq, types.ModelRunReq) int64); ok { + r0 = rf(ctx, deployReq, req) + } else { + r0 = ret.Get(0).(int64) + } + + if rf, ok := ret.Get(1).(func(context.Context, types.DeployActReq, types.ModelRunReq) error); ok { + r1 = rf(ctx, deployReq, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_Deploy_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Deploy' +type MockModelComponent_Deploy_Call struct { + *mock.Call +} + +// Deploy is a helper method to define mock.On call +// - ctx context.Context +// - deployReq types.DeployActReq +// - req types.ModelRunReq +func (_e *MockModelComponent_Expecter) Deploy(ctx interface{}, deployReq interface{}, req interface{}) *MockModelComponent_Deploy_Call { + return &MockModelComponent_Deploy_Call{Call: _e.mock.On("Deploy", ctx, deployReq, req)} +} + +func (_c *MockModelComponent_Deploy_Call) Run(run func(ctx context.Context, deployReq types.DeployActReq, req types.ModelRunReq)) *MockModelComponent_Deploy_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.DeployActReq), args[2].(types.ModelRunReq)) + }) + return _c +} + +func (_c *MockModelComponent_Deploy_Call) Return(_a0 int64, _a1 error) *MockModelComponent_Deploy_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_Deploy_Call) RunAndReturn(run func(context.Context, types.DeployActReq, types.ModelRunReq) (int64, error)) *MockModelComponent_Deploy_Call { + _c.Call.Return(run) + return _c +} + +// GetServerless provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockModelComponent) GetServerless(ctx context.Context, namespace string, name string, currentUser string) (*types.DeployRepo, error) { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for GetServerless") + } + + var r0 *types.DeployRepo + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) (*types.DeployRepo, error)); ok { + return rf(ctx, namespace, name, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) *types.DeployRepo); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.DeployRepo) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string) error); ok { + r1 = rf(ctx, namespace, name, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_GetServerless_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetServerless' +type MockModelComponent_GetServerless_Call struct { + *mock.Call +} + +// GetServerless is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockModelComponent_Expecter) GetServerless(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockModelComponent_GetServerless_Call { + return &MockModelComponent_GetServerless_Call{Call: _e.mock.On("GetServerless", ctx, namespace, name, currentUser)} +} + +func (_c *MockModelComponent_GetServerless_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockModelComponent_GetServerless_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockModelComponent_GetServerless_Call) Return(_a0 *types.DeployRepo, _a1 error) *MockModelComponent_GetServerless_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_GetServerless_Call) RunAndReturn(run func(context.Context, string, string, string) (*types.DeployRepo, error)) *MockModelComponent_GetServerless_Call { + _c.Call.Return(run) + return _c +} + +// Index provides a mock function with given fields: ctx, filter, per, page, needOpWeight +func (_m *MockModelComponent) Index(ctx context.Context, filter *types.RepoFilter, per int, page int, needOpWeight bool) ([]*types.Model, int, error) { + ret := _m.Called(ctx, filter, per, page, needOpWeight) + + if len(ret) == 0 { + panic("no return value specified for Index") + } + + var r0 []*types.Model + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.RepoFilter, int, int, bool) ([]*types.Model, int, error)); ok { + return rf(ctx, filter, per, page, needOpWeight) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.RepoFilter, int, int, bool) []*types.Model); ok { + r0 = rf(ctx, filter, per, page, needOpWeight) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.RepoFilter, int, int, bool) int); ok { + r1 = rf(ctx, filter, per, page, needOpWeight) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.RepoFilter, int, int, bool) error); ok { + r2 = rf(ctx, filter, per, page, needOpWeight) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockModelComponent_Index_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Index' +type MockModelComponent_Index_Call struct { + *mock.Call +} + +// Index is a helper method to define mock.On call +// - ctx context.Context +// - filter *types.RepoFilter +// - per int +// - page int +// - needOpWeight bool +func (_e *MockModelComponent_Expecter) Index(ctx interface{}, filter interface{}, per interface{}, page interface{}, needOpWeight interface{}) *MockModelComponent_Index_Call { + return &MockModelComponent_Index_Call{Call: _e.mock.On("Index", ctx, filter, per, page, needOpWeight)} +} + +func (_c *MockModelComponent_Index_Call) Run(run func(ctx context.Context, filter *types.RepoFilter, per int, page int, needOpWeight bool)) *MockModelComponent_Index_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.RepoFilter), args[2].(int), args[3].(int), args[4].(bool)) + }) + return _c +} + +func (_c *MockModelComponent_Index_Call) Return(_a0 []*types.Model, _a1 int, _a2 error) *MockModelComponent_Index_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockModelComponent_Index_Call) RunAndReturn(run func(context.Context, *types.RepoFilter, int, int, bool) ([]*types.Model, int, error)) *MockModelComponent_Index_Call { + _c.Call.Return(run) + return _c +} + +// ListAllByRuntimeFramework provides a mock function with given fields: ctx, currentUser +func (_m *MockModelComponent) ListAllByRuntimeFramework(ctx context.Context, currentUser string) ([]database.RuntimeFramework, error) { + ret := _m.Called(ctx, currentUser) + + if len(ret) == 0 { + panic("no return value specified for ListAllByRuntimeFramework") + } + + var r0 []database.RuntimeFramework + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string) ([]database.RuntimeFramework, error)); ok { + return rf(ctx, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string) []database.RuntimeFramework); ok { + r0 = rf(ctx, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]database.RuntimeFramework) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_ListAllByRuntimeFramework_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListAllByRuntimeFramework' +type MockModelComponent_ListAllByRuntimeFramework_Call struct { + *mock.Call +} + +// ListAllByRuntimeFramework is a helper method to define mock.On call +// - ctx context.Context +// - currentUser string +func (_e *MockModelComponent_Expecter) ListAllByRuntimeFramework(ctx interface{}, currentUser interface{}) *MockModelComponent_ListAllByRuntimeFramework_Call { + return &MockModelComponent_ListAllByRuntimeFramework_Call{Call: _e.mock.On("ListAllByRuntimeFramework", ctx, currentUser)} +} + +func (_c *MockModelComponent_ListAllByRuntimeFramework_Call) Run(run func(ctx context.Context, currentUser string)) *MockModelComponent_ListAllByRuntimeFramework_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *MockModelComponent_ListAllByRuntimeFramework_Call) Return(_a0 []database.RuntimeFramework, _a1 error) *MockModelComponent_ListAllByRuntimeFramework_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_ListAllByRuntimeFramework_Call) RunAndReturn(run func(context.Context, string) ([]database.RuntimeFramework, error)) *MockModelComponent_ListAllByRuntimeFramework_Call { + _c.Call.Return(run) + return _c +} + +// ListModelsByRuntimeFrameworkID provides a mock function with given fields: ctx, currentUser, per, page, id, deployType +func (_m *MockModelComponent) ListModelsByRuntimeFrameworkID(ctx context.Context, currentUser string, per int, page int, id int64, deployType int) ([]types.Model, int, error) { + ret := _m.Called(ctx, currentUser, per, page, id, deployType) + + if len(ret) == 0 { + panic("no return value specified for ListModelsByRuntimeFrameworkID") + } + + var r0 []types.Model + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, string, int, int, int64, int) ([]types.Model, int, error)); ok { + return rf(ctx, currentUser, per, page, id, deployType) + } + if rf, ok := ret.Get(0).(func(context.Context, string, int, int, int64, int) []types.Model); ok { + r0 = rf(ctx, currentUser, per, page, id, deployType) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, int, int, int64, int) int); ok { + r1 = rf(ctx, currentUser, per, page, id, deployType) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, string, int, int, int64, int) error); ok { + r2 = rf(ctx, currentUser, per, page, id, deployType) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockModelComponent_ListModelsByRuntimeFrameworkID_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListModelsByRuntimeFrameworkID' +type MockModelComponent_ListModelsByRuntimeFrameworkID_Call struct { + *mock.Call +} + +// ListModelsByRuntimeFrameworkID is a helper method to define mock.On call +// - ctx context.Context +// - currentUser string +// - per int +// - page int +// - id int64 +// - deployType int +func (_e *MockModelComponent_Expecter) ListModelsByRuntimeFrameworkID(ctx interface{}, currentUser interface{}, per interface{}, page interface{}, id interface{}, deployType interface{}) *MockModelComponent_ListModelsByRuntimeFrameworkID_Call { + return &MockModelComponent_ListModelsByRuntimeFrameworkID_Call{Call: _e.mock.On("ListModelsByRuntimeFrameworkID", ctx, currentUser, per, page, id, deployType)} +} + +func (_c *MockModelComponent_ListModelsByRuntimeFrameworkID_Call) Run(run func(ctx context.Context, currentUser string, per int, page int, id int64, deployType int)) *MockModelComponent_ListModelsByRuntimeFrameworkID_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(int), args[3].(int), args[4].(int64), args[5].(int)) + }) + return _c +} + +func (_c *MockModelComponent_ListModelsByRuntimeFrameworkID_Call) Return(_a0 []types.Model, _a1 int, _a2 error) *MockModelComponent_ListModelsByRuntimeFrameworkID_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockModelComponent_ListModelsByRuntimeFrameworkID_Call) RunAndReturn(run func(context.Context, string, int, int, int64, int) ([]types.Model, int, error)) *MockModelComponent_ListModelsByRuntimeFrameworkID_Call { + _c.Call.Return(run) + return _c +} + +// ListModelsOfRuntimeFrameworks provides a mock function with given fields: ctx, currentUser, search, sort, per, page, deployType +func (_m *MockModelComponent) ListModelsOfRuntimeFrameworks(ctx context.Context, currentUser string, search string, sort string, per int, page int, deployType int) ([]types.Model, int, error) { + ret := _m.Called(ctx, currentUser, search, sort, per, page, deployType) + + if len(ret) == 0 { + panic("no return value specified for ListModelsOfRuntimeFrameworks") + } + + var r0 []types.Model + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, int, int, int) ([]types.Model, int, error)); ok { + return rf(ctx, currentUser, search, sort, per, page, deployType) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, int, int, int) []types.Model); ok { + r0 = rf(ctx, currentUser, search, sort, per, page, deployType) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string, int, int, int) int); ok { + r1 = rf(ctx, currentUser, search, sort, per, page, deployType) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, string, string, string, int, int, int) error); ok { + r2 = rf(ctx, currentUser, search, sort, per, page, deployType) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockModelComponent_ListModelsOfRuntimeFrameworks_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListModelsOfRuntimeFrameworks' +type MockModelComponent_ListModelsOfRuntimeFrameworks_Call struct { + *mock.Call +} + +// ListModelsOfRuntimeFrameworks is a helper method to define mock.On call +// - ctx context.Context +// - currentUser string +// - search string +// - sort string +// - per int +// - page int +// - deployType int +func (_e *MockModelComponent_Expecter) ListModelsOfRuntimeFrameworks(ctx interface{}, currentUser interface{}, search interface{}, sort interface{}, per interface{}, page interface{}, deployType interface{}) *MockModelComponent_ListModelsOfRuntimeFrameworks_Call { + return &MockModelComponent_ListModelsOfRuntimeFrameworks_Call{Call: _e.mock.On("ListModelsOfRuntimeFrameworks", ctx, currentUser, search, sort, per, page, deployType)} +} + +func (_c *MockModelComponent_ListModelsOfRuntimeFrameworks_Call) Run(run func(ctx context.Context, currentUser string, search string, sort string, per int, page int, deployType int)) *MockModelComponent_ListModelsOfRuntimeFrameworks_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string), args[4].(int), args[5].(int), args[6].(int)) + }) + return _c +} + +func (_c *MockModelComponent_ListModelsOfRuntimeFrameworks_Call) Return(_a0 []types.Model, _a1 int, _a2 error) *MockModelComponent_ListModelsOfRuntimeFrameworks_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockModelComponent_ListModelsOfRuntimeFrameworks_Call) RunAndReturn(run func(context.Context, string, string, string, int, int, int) ([]types.Model, int, error)) *MockModelComponent_ListModelsOfRuntimeFrameworks_Call { + _c.Call.Return(run) + return _c +} + +// OrgModels provides a mock function with given fields: ctx, req +func (_m *MockModelComponent) OrgModels(ctx context.Context, req *types.OrgModelsReq) ([]types.Model, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for OrgModels") + } + + var r0 []types.Model + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.OrgModelsReq) ([]types.Model, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.OrgModelsReq) []types.Model); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.OrgModelsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.OrgModelsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockModelComponent_OrgModels_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'OrgModels' +type MockModelComponent_OrgModels_Call struct { + *mock.Call +} + +// OrgModels is a helper method to define mock.On call +// - ctx context.Context +// - req *types.OrgModelsReq +func (_e *MockModelComponent_Expecter) OrgModels(ctx interface{}, req interface{}) *MockModelComponent_OrgModels_Call { + return &MockModelComponent_OrgModels_Call{Call: _e.mock.On("OrgModels", ctx, req)} +} + +func (_c *MockModelComponent_OrgModels_Call) Run(run func(ctx context.Context, req *types.OrgModelsReq)) *MockModelComponent_OrgModels_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.OrgModelsReq)) + }) + return _c +} + +func (_c *MockModelComponent_OrgModels_Call) Return(_a0 []types.Model, _a1 int, _a2 error) *MockModelComponent_OrgModels_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockModelComponent_OrgModels_Call) RunAndReturn(run func(context.Context, *types.OrgModelsReq) ([]types.Model, int, error)) *MockModelComponent_OrgModels_Call { + _c.Call.Return(run) + return _c +} + +// Relations provides a mock function with given fields: ctx, namespace, name, currentUser +func (_m *MockModelComponent) Relations(ctx context.Context, namespace string, name string, currentUser string) (*types.Relations, error) { + ret := _m.Called(ctx, namespace, name, currentUser) + + if len(ret) == 0 { + panic("no return value specified for Relations") + } + + var r0 *types.Relations + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) (*types.Relations, error)); ok { + return rf(ctx, namespace, name, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string) *types.Relations); ok { + r0 = rf(ctx, namespace, name, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Relations) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string) error); ok { + r1 = rf(ctx, namespace, name, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_Relations_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Relations' +type MockModelComponent_Relations_Call struct { + *mock.Call +} + +// Relations is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +func (_e *MockModelComponent_Expecter) Relations(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}) *MockModelComponent_Relations_Call { + return &MockModelComponent_Relations_Call{Call: _e.mock.On("Relations", ctx, namespace, name, currentUser)} +} + +func (_c *MockModelComponent_Relations_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string)) *MockModelComponent_Relations_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string)) + }) + return _c +} + +func (_c *MockModelComponent_Relations_Call) Return(_a0 *types.Relations, _a1 error) *MockModelComponent_Relations_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_Relations_Call) RunAndReturn(run func(context.Context, string, string, string) (*types.Relations, error)) *MockModelComponent_Relations_Call { + _c.Call.Return(run) + return _c +} + +// SDKModelInfo provides a mock function with given fields: ctx, namespace, name, ref, currentUser +func (_m *MockModelComponent) SDKModelInfo(ctx context.Context, namespace string, name string, ref string, currentUser string) (*types.SDKModelInfo, error) { + ret := _m.Called(ctx, namespace, name, ref, currentUser) + + if len(ret) == 0 { + panic("no return value specified for SDKModelInfo") + } + + var r0 *types.SDKModelInfo + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string) (*types.SDKModelInfo, error)); ok { + return rf(ctx, namespace, name, ref, currentUser) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string) *types.SDKModelInfo); ok { + r0 = rf(ctx, namespace, name, ref, currentUser) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.SDKModelInfo) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string, string) error); ok { + r1 = rf(ctx, namespace, name, ref, currentUser) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_SDKModelInfo_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SDKModelInfo' +type MockModelComponent_SDKModelInfo_Call struct { + *mock.Call +} + +// SDKModelInfo is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - ref string +// - currentUser string +func (_e *MockModelComponent_Expecter) SDKModelInfo(ctx interface{}, namespace interface{}, name interface{}, ref interface{}, currentUser interface{}) *MockModelComponent_SDKModelInfo_Call { + return &MockModelComponent_SDKModelInfo_Call{Call: _e.mock.On("SDKModelInfo", ctx, namespace, name, ref, currentUser)} +} + +func (_c *MockModelComponent_SDKModelInfo_Call) Run(run func(ctx context.Context, namespace string, name string, ref string, currentUser string)) *MockModelComponent_SDKModelInfo_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string), args[4].(string)) + }) + return _c +} + +func (_c *MockModelComponent_SDKModelInfo_Call) Return(_a0 *types.SDKModelInfo, _a1 error) *MockModelComponent_SDKModelInfo_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_SDKModelInfo_Call) RunAndReturn(run func(context.Context, string, string, string, string) (*types.SDKModelInfo, error)) *MockModelComponent_SDKModelInfo_Call { + _c.Call.Return(run) + return _c +} + +// SetRelationDatasets provides a mock function with given fields: ctx, req +func (_m *MockModelComponent) SetRelationDatasets(ctx context.Context, req types.RelationDatasets) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for SetRelationDatasets") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, types.RelationDatasets) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockModelComponent_SetRelationDatasets_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SetRelationDatasets' +type MockModelComponent_SetRelationDatasets_Call struct { + *mock.Call +} + +// SetRelationDatasets is a helper method to define mock.On call +// - ctx context.Context +// - req types.RelationDatasets +func (_e *MockModelComponent_Expecter) SetRelationDatasets(ctx interface{}, req interface{}) *MockModelComponent_SetRelationDatasets_Call { + return &MockModelComponent_SetRelationDatasets_Call{Call: _e.mock.On("SetRelationDatasets", ctx, req)} +} + +func (_c *MockModelComponent_SetRelationDatasets_Call) Run(run func(ctx context.Context, req types.RelationDatasets)) *MockModelComponent_SetRelationDatasets_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.RelationDatasets)) + }) + return _c +} + +func (_c *MockModelComponent_SetRelationDatasets_Call) Return(_a0 error) *MockModelComponent_SetRelationDatasets_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockModelComponent_SetRelationDatasets_Call) RunAndReturn(run func(context.Context, types.RelationDatasets) error) *MockModelComponent_SetRelationDatasets_Call { + _c.Call.Return(run) + return _c +} + +// SetRuntimeFrameworkModes provides a mock function with given fields: ctx, deployType, id, paths +func (_m *MockModelComponent) SetRuntimeFrameworkModes(ctx context.Context, deployType int, id int64, paths []string) ([]string, error) { + ret := _m.Called(ctx, deployType, id, paths) + + if len(ret) == 0 { + panic("no return value specified for SetRuntimeFrameworkModes") + } + + var r0 []string + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, int, int64, []string) ([]string, error)); ok { + return rf(ctx, deployType, id, paths) + } + if rf, ok := ret.Get(0).(func(context.Context, int, int64, []string) []string); ok { + r0 = rf(ctx, deployType, id, paths) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, int, int64, []string) error); ok { + r1 = rf(ctx, deployType, id, paths) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_SetRuntimeFrameworkModes_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SetRuntimeFrameworkModes' +type MockModelComponent_SetRuntimeFrameworkModes_Call struct { + *mock.Call +} + +// SetRuntimeFrameworkModes is a helper method to define mock.On call +// - ctx context.Context +// - deployType int +// - id int64 +// - paths []string +func (_e *MockModelComponent_Expecter) SetRuntimeFrameworkModes(ctx interface{}, deployType interface{}, id interface{}, paths interface{}) *MockModelComponent_SetRuntimeFrameworkModes_Call { + return &MockModelComponent_SetRuntimeFrameworkModes_Call{Call: _e.mock.On("SetRuntimeFrameworkModes", ctx, deployType, id, paths)} +} + +func (_c *MockModelComponent_SetRuntimeFrameworkModes_Call) Run(run func(ctx context.Context, deployType int, id int64, paths []string)) *MockModelComponent_SetRuntimeFrameworkModes_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(int), args[2].(int64), args[3].([]string)) + }) + return _c +} + +func (_c *MockModelComponent_SetRuntimeFrameworkModes_Call) Return(_a0 []string, _a1 error) *MockModelComponent_SetRuntimeFrameworkModes_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_SetRuntimeFrameworkModes_Call) RunAndReturn(run func(context.Context, int, int64, []string) ([]string, error)) *MockModelComponent_SetRuntimeFrameworkModes_Call { + _c.Call.Return(run) + return _c +} + +// Show provides a mock function with given fields: ctx, namespace, name, currentUser, needOpWeight +func (_m *MockModelComponent) Show(ctx context.Context, namespace string, name string, currentUser string, needOpWeight bool) (*types.Model, error) { + ret := _m.Called(ctx, namespace, name, currentUser, needOpWeight) + + if len(ret) == 0 { + panic("no return value specified for Show") + } + + var r0 *types.Model + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, bool) (*types.Model, error)); ok { + return rf(ctx, namespace, name, currentUser, needOpWeight) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, bool) *types.Model); ok { + r0 = rf(ctx, namespace, name, currentUser, needOpWeight) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string, bool) error); ok { + r1 = rf(ctx, namespace, name, currentUser, needOpWeight) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_Show_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Show' +type MockModelComponent_Show_Call struct { + *mock.Call +} + +// Show is a helper method to define mock.On call +// - ctx context.Context +// - namespace string +// - name string +// - currentUser string +// - needOpWeight bool +func (_e *MockModelComponent_Expecter) Show(ctx interface{}, namespace interface{}, name interface{}, currentUser interface{}, needOpWeight interface{}) *MockModelComponent_Show_Call { + return &MockModelComponent_Show_Call{Call: _e.mock.On("Show", ctx, namespace, name, currentUser, needOpWeight)} +} + +func (_c *MockModelComponent_Show_Call) Run(run func(ctx context.Context, namespace string, name string, currentUser string, needOpWeight bool)) *MockModelComponent_Show_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string), args[4].(bool)) + }) + return _c +} + +func (_c *MockModelComponent_Show_Call) Return(_a0 *types.Model, _a1 error) *MockModelComponent_Show_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_Show_Call) RunAndReturn(run func(context.Context, string, string, string, bool) (*types.Model, error)) *MockModelComponent_Show_Call { + _c.Call.Return(run) + return _c +} + +// Update provides a mock function with given fields: ctx, req +func (_m *MockModelComponent) Update(ctx context.Context, req *types.UpdateModelReq) (*types.Model, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Update") + } + + var r0 *types.Model + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UpdateModelReq) (*types.Model, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UpdateModelReq) *types.Model); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UpdateModelReq) error); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockModelComponent_Update_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Update' +type MockModelComponent_Update_Call struct { + *mock.Call +} + +// Update is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UpdateModelReq +func (_e *MockModelComponent_Expecter) Update(ctx interface{}, req interface{}) *MockModelComponent_Update_Call { + return &MockModelComponent_Update_Call{Call: _e.mock.On("Update", ctx, req)} +} + +func (_c *MockModelComponent_Update_Call) Run(run func(ctx context.Context, req *types.UpdateModelReq)) *MockModelComponent_Update_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UpdateModelReq)) + }) + return _c +} + +func (_c *MockModelComponent_Update_Call) Return(_a0 *types.Model, _a1 error) *MockModelComponent_Update_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockModelComponent_Update_Call) RunAndReturn(run func(context.Context, *types.UpdateModelReq) (*types.Model, error)) *MockModelComponent_Update_Call { + _c.Call.Return(run) + return _c +} + +// NewMockModelComponent creates a new instance of MockModelComponent. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockModelComponent(t interface { + mock.TestingT + Cleanup(func()) +}) *MockModelComponent { + mock := &MockModelComponent{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/_mocks/opencsg.com/csghub-server/component/mock_UserComponent.go b/_mocks/opencsg.com/csghub-server/component/mock_UserComponent.go new file mode 100644 index 00000000..69e029e0 --- /dev/null +++ b/_mocks/opencsg.com/csghub-server/component/mock_UserComponent.go @@ -0,0 +1,1277 @@ +// Code generated by mockery v2.49.1. DO NOT EDIT. + +package component + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" + database "opencsg.com/csghub-server/builder/store/database" + + types "opencsg.com/csghub-server/common/types" +) + +// MockUserComponent is an autogenerated mock type for the UserComponent type +type MockUserComponent struct { + mock.Mock +} + +type MockUserComponent_Expecter struct { + mock *mock.Mock +} + +func (_m *MockUserComponent) EXPECT() *MockUserComponent_Expecter { + return &MockUserComponent_Expecter{mock: &_m.Mock} +} + +// AddLikes provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) AddLikes(ctx context.Context, req *types.UserLikesRequest) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for AddLikes") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserLikesRequest) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockUserComponent_AddLikes_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AddLikes' +type MockUserComponent_AddLikes_Call struct { + *mock.Call +} + +// AddLikes is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserLikesRequest +func (_e *MockUserComponent_Expecter) AddLikes(ctx interface{}, req interface{}) *MockUserComponent_AddLikes_Call { + return &MockUserComponent_AddLikes_Call{Call: _e.mock.On("AddLikes", ctx, req)} +} + +func (_c *MockUserComponent_AddLikes_Call) Run(run func(ctx context.Context, req *types.UserLikesRequest)) *MockUserComponent_AddLikes_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserLikesRequest)) + }) + return _c +} + +func (_c *MockUserComponent_AddLikes_Call) Return(_a0 error) *MockUserComponent_AddLikes_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockUserComponent_AddLikes_Call) RunAndReturn(run func(context.Context, *types.UserLikesRequest) error) *MockUserComponent_AddLikes_Call { + _c.Call.Return(run) + return _c +} + +// Codes provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) Codes(ctx context.Context, req *types.UserModelsReq) ([]types.Code, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Codes") + } + + var r0 []types.Code + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserModelsReq) ([]types.Code, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserModelsReq) []types.Code); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Code) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserModelsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserModelsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_Codes_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Codes' +type MockUserComponent_Codes_Call struct { + *mock.Call +} + +// Codes is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserModelsReq +func (_e *MockUserComponent_Expecter) Codes(ctx interface{}, req interface{}) *MockUserComponent_Codes_Call { + return &MockUserComponent_Codes_Call{Call: _e.mock.On("Codes", ctx, req)} +} + +func (_c *MockUserComponent_Codes_Call) Run(run func(ctx context.Context, req *types.UserModelsReq)) *MockUserComponent_Codes_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserModelsReq)) + }) + return _c +} + +func (_c *MockUserComponent_Codes_Call) Return(_a0 []types.Code, _a1 int, _a2 error) *MockUserComponent_Codes_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_Codes_Call) RunAndReturn(run func(context.Context, *types.UserModelsReq) ([]types.Code, int, error)) *MockUserComponent_Codes_Call { + _c.Call.Return(run) + return _c +} + +// Collections provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) Collections(ctx context.Context, req *types.UserCollectionReq) ([]types.Collection, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Collections") + } + + var r0 []types.Collection + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserCollectionReq) ([]types.Collection, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserCollectionReq) []types.Collection); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Collection) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserCollectionReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserCollectionReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_Collections_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Collections' +type MockUserComponent_Collections_Call struct { + *mock.Call +} + +// Collections is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserCollectionReq +func (_e *MockUserComponent_Expecter) Collections(ctx interface{}, req interface{}) *MockUserComponent_Collections_Call { + return &MockUserComponent_Collections_Call{Call: _e.mock.On("Collections", ctx, req)} +} + +func (_c *MockUserComponent_Collections_Call) Run(run func(ctx context.Context, req *types.UserCollectionReq)) *MockUserComponent_Collections_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserCollectionReq)) + }) + return _c +} + +func (_c *MockUserComponent_Collections_Call) Return(_a0 []types.Collection, _a1 int, _a2 error) *MockUserComponent_Collections_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_Collections_Call) RunAndReturn(run func(context.Context, *types.UserCollectionReq) ([]types.Collection, int, error)) *MockUserComponent_Collections_Call { + _c.Call.Return(run) + return _c +} + +// Datasets provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) Datasets(ctx context.Context, req *types.UserDatasetsReq) ([]types.Dataset, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Datasets") + } + + var r0 []types.Dataset + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserDatasetsReq) ([]types.Dataset, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserDatasetsReq) []types.Dataset); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Dataset) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserDatasetsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserDatasetsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_Datasets_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Datasets' +type MockUserComponent_Datasets_Call struct { + *mock.Call +} + +// Datasets is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserDatasetsReq +func (_e *MockUserComponent_Expecter) Datasets(ctx interface{}, req interface{}) *MockUserComponent_Datasets_Call { + return &MockUserComponent_Datasets_Call{Call: _e.mock.On("Datasets", ctx, req)} +} + +func (_c *MockUserComponent_Datasets_Call) Run(run func(ctx context.Context, req *types.UserDatasetsReq)) *MockUserComponent_Datasets_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserDatasetsReq)) + }) + return _c +} + +func (_c *MockUserComponent_Datasets_Call) Return(_a0 []types.Dataset, _a1 int, _a2 error) *MockUserComponent_Datasets_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_Datasets_Call) RunAndReturn(run func(context.Context, *types.UserDatasetsReq) ([]types.Dataset, int, error)) *MockUserComponent_Datasets_Call { + _c.Call.Return(run) + return _c +} + +// DeleteLikes provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) DeleteLikes(ctx context.Context, req *types.UserLikesRequest) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for DeleteLikes") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserLikesRequest) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockUserComponent_DeleteLikes_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeleteLikes' +type MockUserComponent_DeleteLikes_Call struct { + *mock.Call +} + +// DeleteLikes is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserLikesRequest +func (_e *MockUserComponent_Expecter) DeleteLikes(ctx interface{}, req interface{}) *MockUserComponent_DeleteLikes_Call { + return &MockUserComponent_DeleteLikes_Call{Call: _e.mock.On("DeleteLikes", ctx, req)} +} + +func (_c *MockUserComponent_DeleteLikes_Call) Run(run func(ctx context.Context, req *types.UserLikesRequest)) *MockUserComponent_DeleteLikes_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserLikesRequest)) + }) + return _c +} + +func (_c *MockUserComponent_DeleteLikes_Call) Return(_a0 error) *MockUserComponent_DeleteLikes_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockUserComponent_DeleteLikes_Call) RunAndReturn(run func(context.Context, *types.UserLikesRequest) error) *MockUserComponent_DeleteLikes_Call { + _c.Call.Return(run) + return _c +} + +// Evaluations provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) Evaluations(ctx context.Context, req *types.UserEvaluationReq) ([]types.ArgoWorkFlowRes, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Evaluations") + } + + var r0 []types.ArgoWorkFlowRes + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserEvaluationReq) ([]types.ArgoWorkFlowRes, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserEvaluationReq) []types.ArgoWorkFlowRes); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.ArgoWorkFlowRes) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserEvaluationReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserEvaluationReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_Evaluations_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Evaluations' +type MockUserComponent_Evaluations_Call struct { + *mock.Call +} + +// Evaluations is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserEvaluationReq +func (_e *MockUserComponent_Expecter) Evaluations(ctx interface{}, req interface{}) *MockUserComponent_Evaluations_Call { + return &MockUserComponent_Evaluations_Call{Call: _e.mock.On("Evaluations", ctx, req)} +} + +func (_c *MockUserComponent_Evaluations_Call) Run(run func(ctx context.Context, req *types.UserEvaluationReq)) *MockUserComponent_Evaluations_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserEvaluationReq)) + }) + return _c +} + +func (_c *MockUserComponent_Evaluations_Call) Return(_a0 []types.ArgoWorkFlowRes, _a1 int, _a2 error) *MockUserComponent_Evaluations_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_Evaluations_Call) RunAndReturn(run func(context.Context, *types.UserEvaluationReq) ([]types.ArgoWorkFlowRes, int, error)) *MockUserComponent_Evaluations_Call { + _c.Call.Return(run) + return _c +} + +// GetUserByName provides a mock function with given fields: ctx, userName +func (_m *MockUserComponent) GetUserByName(ctx context.Context, userName string) (*database.User, error) { + ret := _m.Called(ctx, userName) + + if len(ret) == 0 { + panic("no return value specified for GetUserByName") + } + + var r0 *database.User + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string) (*database.User, error)); ok { + return rf(ctx, userName) + } + if rf, ok := ret.Get(0).(func(context.Context, string) *database.User); ok { + r0 = rf(ctx, userName) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*database.User) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, userName) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockUserComponent_GetUserByName_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetUserByName' +type MockUserComponent_GetUserByName_Call struct { + *mock.Call +} + +// GetUserByName is a helper method to define mock.On call +// - ctx context.Context +// - userName string +func (_e *MockUserComponent_Expecter) GetUserByName(ctx interface{}, userName interface{}) *MockUserComponent_GetUserByName_Call { + return &MockUserComponent_GetUserByName_Call{Call: _e.mock.On("GetUserByName", ctx, userName)} +} + +func (_c *MockUserComponent_GetUserByName_Call) Run(run func(ctx context.Context, userName string)) *MockUserComponent_GetUserByName_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string)) + }) + return _c +} + +func (_c *MockUserComponent_GetUserByName_Call) Return(_a0 *database.User, _a1 error) *MockUserComponent_GetUserByName_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockUserComponent_GetUserByName_Call) RunAndReturn(run func(context.Context, string) (*database.User, error)) *MockUserComponent_GetUserByName_Call { + _c.Call.Return(run) + return _c +} + +// LikeCollection provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) LikeCollection(ctx context.Context, req *types.UserLikesRequest) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for LikeCollection") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserLikesRequest) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockUserComponent_LikeCollection_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LikeCollection' +type MockUserComponent_LikeCollection_Call struct { + *mock.Call +} + +// LikeCollection is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserLikesRequest +func (_e *MockUserComponent_Expecter) LikeCollection(ctx interface{}, req interface{}) *MockUserComponent_LikeCollection_Call { + return &MockUserComponent_LikeCollection_Call{Call: _e.mock.On("LikeCollection", ctx, req)} +} + +func (_c *MockUserComponent_LikeCollection_Call) Run(run func(ctx context.Context, req *types.UserLikesRequest)) *MockUserComponent_LikeCollection_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserLikesRequest)) + }) + return _c +} + +func (_c *MockUserComponent_LikeCollection_Call) Return(_a0 error) *MockUserComponent_LikeCollection_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockUserComponent_LikeCollection_Call) RunAndReturn(run func(context.Context, *types.UserLikesRequest) error) *MockUserComponent_LikeCollection_Call { + _c.Call.Return(run) + return _c +} + +// LikesCodes provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) LikesCodes(ctx context.Context, req *types.UserModelsReq) ([]types.Code, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for LikesCodes") + } + + var r0 []types.Code + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserModelsReq) ([]types.Code, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserModelsReq) []types.Code); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Code) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserModelsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserModelsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_LikesCodes_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LikesCodes' +type MockUserComponent_LikesCodes_Call struct { + *mock.Call +} + +// LikesCodes is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserModelsReq +func (_e *MockUserComponent_Expecter) LikesCodes(ctx interface{}, req interface{}) *MockUserComponent_LikesCodes_Call { + return &MockUserComponent_LikesCodes_Call{Call: _e.mock.On("LikesCodes", ctx, req)} +} + +func (_c *MockUserComponent_LikesCodes_Call) Run(run func(ctx context.Context, req *types.UserModelsReq)) *MockUserComponent_LikesCodes_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserModelsReq)) + }) + return _c +} + +func (_c *MockUserComponent_LikesCodes_Call) Return(_a0 []types.Code, _a1 int, _a2 error) *MockUserComponent_LikesCodes_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_LikesCodes_Call) RunAndReturn(run func(context.Context, *types.UserModelsReq) ([]types.Code, int, error)) *MockUserComponent_LikesCodes_Call { + _c.Call.Return(run) + return _c +} + +// LikesCollection provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) LikesCollection(ctx context.Context, req *types.UserSpacesReq) ([]types.Collection, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for LikesCollection") + } + + var r0 []types.Collection + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserSpacesReq) ([]types.Collection, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserSpacesReq) []types.Collection); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Collection) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserSpacesReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserSpacesReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_LikesCollection_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LikesCollection' +type MockUserComponent_LikesCollection_Call struct { + *mock.Call +} + +// LikesCollection is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserSpacesReq +func (_e *MockUserComponent_Expecter) LikesCollection(ctx interface{}, req interface{}) *MockUserComponent_LikesCollection_Call { + return &MockUserComponent_LikesCollection_Call{Call: _e.mock.On("LikesCollection", ctx, req)} +} + +func (_c *MockUserComponent_LikesCollection_Call) Run(run func(ctx context.Context, req *types.UserSpacesReq)) *MockUserComponent_LikesCollection_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserSpacesReq)) + }) + return _c +} + +func (_c *MockUserComponent_LikesCollection_Call) Return(_a0 []types.Collection, _a1 int, _a2 error) *MockUserComponent_LikesCollection_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_LikesCollection_Call) RunAndReturn(run func(context.Context, *types.UserSpacesReq) ([]types.Collection, int, error)) *MockUserComponent_LikesCollection_Call { + _c.Call.Return(run) + return _c +} + +// LikesDatasets provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) LikesDatasets(ctx context.Context, req *types.UserDatasetsReq) ([]types.Dataset, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for LikesDatasets") + } + + var r0 []types.Dataset + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserDatasetsReq) ([]types.Dataset, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserDatasetsReq) []types.Dataset); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Dataset) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserDatasetsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserDatasetsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_LikesDatasets_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LikesDatasets' +type MockUserComponent_LikesDatasets_Call struct { + *mock.Call +} + +// LikesDatasets is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserDatasetsReq +func (_e *MockUserComponent_Expecter) LikesDatasets(ctx interface{}, req interface{}) *MockUserComponent_LikesDatasets_Call { + return &MockUserComponent_LikesDatasets_Call{Call: _e.mock.On("LikesDatasets", ctx, req)} +} + +func (_c *MockUserComponent_LikesDatasets_Call) Run(run func(ctx context.Context, req *types.UserDatasetsReq)) *MockUserComponent_LikesDatasets_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserDatasetsReq)) + }) + return _c +} + +func (_c *MockUserComponent_LikesDatasets_Call) Return(_a0 []types.Dataset, _a1 int, _a2 error) *MockUserComponent_LikesDatasets_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_LikesDatasets_Call) RunAndReturn(run func(context.Context, *types.UserDatasetsReq) ([]types.Dataset, int, error)) *MockUserComponent_LikesDatasets_Call { + _c.Call.Return(run) + return _c +} + +// LikesModels provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) LikesModels(ctx context.Context, req *types.UserModelsReq) ([]types.Model, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for LikesModels") + } + + var r0 []types.Model + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserModelsReq) ([]types.Model, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserModelsReq) []types.Model); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserModelsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserModelsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_LikesModels_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LikesModels' +type MockUserComponent_LikesModels_Call struct { + *mock.Call +} + +// LikesModels is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserModelsReq +func (_e *MockUserComponent_Expecter) LikesModels(ctx interface{}, req interface{}) *MockUserComponent_LikesModels_Call { + return &MockUserComponent_LikesModels_Call{Call: _e.mock.On("LikesModels", ctx, req)} +} + +func (_c *MockUserComponent_LikesModels_Call) Run(run func(ctx context.Context, req *types.UserModelsReq)) *MockUserComponent_LikesModels_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserModelsReq)) + }) + return _c +} + +func (_c *MockUserComponent_LikesModels_Call) Return(_a0 []types.Model, _a1 int, _a2 error) *MockUserComponent_LikesModels_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_LikesModels_Call) RunAndReturn(run func(context.Context, *types.UserModelsReq) ([]types.Model, int, error)) *MockUserComponent_LikesModels_Call { + _c.Call.Return(run) + return _c +} + +// LikesSpaces provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) LikesSpaces(ctx context.Context, req *types.UserSpacesReq) ([]types.Space, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for LikesSpaces") + } + + var r0 []types.Space + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserSpacesReq) ([]types.Space, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserSpacesReq) []types.Space); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Space) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserSpacesReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserSpacesReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_LikesSpaces_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LikesSpaces' +type MockUserComponent_LikesSpaces_Call struct { + *mock.Call +} + +// LikesSpaces is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserSpacesReq +func (_e *MockUserComponent_Expecter) LikesSpaces(ctx interface{}, req interface{}) *MockUserComponent_LikesSpaces_Call { + return &MockUserComponent_LikesSpaces_Call{Call: _e.mock.On("LikesSpaces", ctx, req)} +} + +func (_c *MockUserComponent_LikesSpaces_Call) Run(run func(ctx context.Context, req *types.UserSpacesReq)) *MockUserComponent_LikesSpaces_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserSpacesReq)) + }) + return _c +} + +func (_c *MockUserComponent_LikesSpaces_Call) Return(_a0 []types.Space, _a1 int, _a2 error) *MockUserComponent_LikesSpaces_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_LikesSpaces_Call) RunAndReturn(run func(context.Context, *types.UserSpacesReq) ([]types.Space, int, error)) *MockUserComponent_LikesSpaces_Call { + _c.Call.Return(run) + return _c +} + +// ListDeploys provides a mock function with given fields: ctx, repoType, req +func (_m *MockUserComponent) ListDeploys(ctx context.Context, repoType types.RepositoryType, req *types.DeployReq) ([]types.DeployRepo, int, error) { + ret := _m.Called(ctx, repoType, req) + + if len(ret) == 0 { + panic("no return value specified for ListDeploys") + } + + var r0 []types.DeployRepo + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, types.RepositoryType, *types.DeployReq) ([]types.DeployRepo, int, error)); ok { + return rf(ctx, repoType, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.RepositoryType, *types.DeployReq) []types.DeployRepo); ok { + r0 = rf(ctx, repoType, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.DeployRepo) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.RepositoryType, *types.DeployReq) int); ok { + r1 = rf(ctx, repoType, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, types.RepositoryType, *types.DeployReq) error); ok { + r2 = rf(ctx, repoType, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_ListDeploys_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListDeploys' +type MockUserComponent_ListDeploys_Call struct { + *mock.Call +} + +// ListDeploys is a helper method to define mock.On call +// - ctx context.Context +// - repoType types.RepositoryType +// - req *types.DeployReq +func (_e *MockUserComponent_Expecter) ListDeploys(ctx interface{}, repoType interface{}, req interface{}) *MockUserComponent_ListDeploys_Call { + return &MockUserComponent_ListDeploys_Call{Call: _e.mock.On("ListDeploys", ctx, repoType, req)} +} + +func (_c *MockUserComponent_ListDeploys_Call) Run(run func(ctx context.Context, repoType types.RepositoryType, req *types.DeployReq)) *MockUserComponent_ListDeploys_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.RepositoryType), args[2].(*types.DeployReq)) + }) + return _c +} + +func (_c *MockUserComponent_ListDeploys_Call) Return(_a0 []types.DeployRepo, _a1 int, _a2 error) *MockUserComponent_ListDeploys_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_ListDeploys_Call) RunAndReturn(run func(context.Context, types.RepositoryType, *types.DeployReq) ([]types.DeployRepo, int, error)) *MockUserComponent_ListDeploys_Call { + _c.Call.Return(run) + return _c +} + +// ListInstances provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) ListInstances(ctx context.Context, req *types.UserRepoReq) ([]types.DeployRepo, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for ListInstances") + } + + var r0 []types.DeployRepo + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserRepoReq) ([]types.DeployRepo, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserRepoReq) []types.DeployRepo); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.DeployRepo) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserRepoReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserRepoReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_ListInstances_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListInstances' +type MockUserComponent_ListInstances_Call struct { + *mock.Call +} + +// ListInstances is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserRepoReq +func (_e *MockUserComponent_Expecter) ListInstances(ctx interface{}, req interface{}) *MockUserComponent_ListInstances_Call { + return &MockUserComponent_ListInstances_Call{Call: _e.mock.On("ListInstances", ctx, req)} +} + +func (_c *MockUserComponent_ListInstances_Call) Run(run func(ctx context.Context, req *types.UserRepoReq)) *MockUserComponent_ListInstances_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserRepoReq)) + }) + return _c +} + +func (_c *MockUserComponent_ListInstances_Call) Return(_a0 []types.DeployRepo, _a1 int, _a2 error) *MockUserComponent_ListInstances_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_ListInstances_Call) RunAndReturn(run func(context.Context, *types.UserRepoReq) ([]types.DeployRepo, int, error)) *MockUserComponent_ListInstances_Call { + _c.Call.Return(run) + return _c +} + +// ListServerless provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) ListServerless(ctx context.Context, req types.DeployReq) ([]types.DeployRepo, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for ListServerless") + } + + var r0 []types.DeployRepo + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, types.DeployReq) ([]types.DeployRepo, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, types.DeployReq) []types.DeployRepo); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.DeployRepo) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.DeployReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, types.DeployReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_ListServerless_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListServerless' +type MockUserComponent_ListServerless_Call struct { + *mock.Call +} + +// ListServerless is a helper method to define mock.On call +// - ctx context.Context +// - req types.DeployReq +func (_e *MockUserComponent_Expecter) ListServerless(ctx interface{}, req interface{}) *MockUserComponent_ListServerless_Call { + return &MockUserComponent_ListServerless_Call{Call: _e.mock.On("ListServerless", ctx, req)} +} + +func (_c *MockUserComponent_ListServerless_Call) Run(run func(ctx context.Context, req types.DeployReq)) *MockUserComponent_ListServerless_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(types.DeployReq)) + }) + return _c +} + +func (_c *MockUserComponent_ListServerless_Call) Return(_a0 []types.DeployRepo, _a1 int, _a2 error) *MockUserComponent_ListServerless_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_ListServerless_Call) RunAndReturn(run func(context.Context, types.DeployReq) ([]types.DeployRepo, int, error)) *MockUserComponent_ListServerless_Call { + _c.Call.Return(run) + return _c +} + +// Models provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) Models(ctx context.Context, req *types.UserModelsReq) ([]types.Model, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Models") + } + + var r0 []types.Model + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserModelsReq) ([]types.Model, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserModelsReq) []types.Model); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Model) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserModelsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserModelsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_Models_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Models' +type MockUserComponent_Models_Call struct { + *mock.Call +} + +// Models is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserModelsReq +func (_e *MockUserComponent_Expecter) Models(ctx interface{}, req interface{}) *MockUserComponent_Models_Call { + return &MockUserComponent_Models_Call{Call: _e.mock.On("Models", ctx, req)} +} + +func (_c *MockUserComponent_Models_Call) Run(run func(ctx context.Context, req *types.UserModelsReq)) *MockUserComponent_Models_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserModelsReq)) + }) + return _c +} + +func (_c *MockUserComponent_Models_Call) Return(_a0 []types.Model, _a1 int, _a2 error) *MockUserComponent_Models_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_Models_Call) RunAndReturn(run func(context.Context, *types.UserModelsReq) ([]types.Model, int, error)) *MockUserComponent_Models_Call { + _c.Call.Return(run) + return _c +} + +// Prompts provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) Prompts(ctx context.Context, req *types.UserPromptsReq) ([]types.PromptRes, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Prompts") + } + + var r0 []types.PromptRes + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserPromptsReq) ([]types.PromptRes, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserPromptsReq) []types.PromptRes); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.PromptRes) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserPromptsReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserPromptsReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_Prompts_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Prompts' +type MockUserComponent_Prompts_Call struct { + *mock.Call +} + +// Prompts is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserPromptsReq +func (_e *MockUserComponent_Expecter) Prompts(ctx interface{}, req interface{}) *MockUserComponent_Prompts_Call { + return &MockUserComponent_Prompts_Call{Call: _e.mock.On("Prompts", ctx, req)} +} + +func (_c *MockUserComponent_Prompts_Call) Run(run func(ctx context.Context, req *types.UserPromptsReq)) *MockUserComponent_Prompts_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserPromptsReq)) + }) + return _c +} + +func (_c *MockUserComponent_Prompts_Call) Return(_a0 []types.PromptRes, _a1 int, _a2 error) *MockUserComponent_Prompts_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_Prompts_Call) RunAndReturn(run func(context.Context, *types.UserPromptsReq) ([]types.PromptRes, int, error)) *MockUserComponent_Prompts_Call { + _c.Call.Return(run) + return _c +} + +// Spaces provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) Spaces(ctx context.Context, req *types.UserSpacesReq) ([]types.Space, int, error) { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for Spaces") + } + + var r0 []types.Space + var r1 int + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserSpacesReq) ([]types.Space, int, error)); ok { + return rf(ctx, req) + } + if rf, ok := ret.Get(0).(func(context.Context, *types.UserSpacesReq) []types.Space); ok { + r0 = rf(ctx, req) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Space) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *types.UserSpacesReq) int); ok { + r1 = rf(ctx, req) + } else { + r1 = ret.Get(1).(int) + } + + if rf, ok := ret.Get(2).(func(context.Context, *types.UserSpacesReq) error); ok { + r2 = rf(ctx, req) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// MockUserComponent_Spaces_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Spaces' +type MockUserComponent_Spaces_Call struct { + *mock.Call +} + +// Spaces is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserSpacesReq +func (_e *MockUserComponent_Expecter) Spaces(ctx interface{}, req interface{}) *MockUserComponent_Spaces_Call { + return &MockUserComponent_Spaces_Call{Call: _e.mock.On("Spaces", ctx, req)} +} + +func (_c *MockUserComponent_Spaces_Call) Run(run func(ctx context.Context, req *types.UserSpacesReq)) *MockUserComponent_Spaces_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserSpacesReq)) + }) + return _c +} + +func (_c *MockUserComponent_Spaces_Call) Return(_a0 []types.Space, _a1 int, _a2 error) *MockUserComponent_Spaces_Call { + _c.Call.Return(_a0, _a1, _a2) + return _c +} + +func (_c *MockUserComponent_Spaces_Call) RunAndReturn(run func(context.Context, *types.UserSpacesReq) ([]types.Space, int, error)) *MockUserComponent_Spaces_Call { + _c.Call.Return(run) + return _c +} + +// UnLikeCollection provides a mock function with given fields: ctx, req +func (_m *MockUserComponent) UnLikeCollection(ctx context.Context, req *types.UserLikesRequest) error { + ret := _m.Called(ctx, req) + + if len(ret) == 0 { + panic("no return value specified for UnLikeCollection") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, *types.UserLikesRequest) error); ok { + r0 = rf(ctx, req) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MockUserComponent_UnLikeCollection_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UnLikeCollection' +type MockUserComponent_UnLikeCollection_Call struct { + *mock.Call +} + +// UnLikeCollection is a helper method to define mock.On call +// - ctx context.Context +// - req *types.UserLikesRequest +func (_e *MockUserComponent_Expecter) UnLikeCollection(ctx interface{}, req interface{}) *MockUserComponent_UnLikeCollection_Call { + return &MockUserComponent_UnLikeCollection_Call{Call: _e.mock.On("UnLikeCollection", ctx, req)} +} + +func (_c *MockUserComponent_UnLikeCollection_Call) Run(run func(ctx context.Context, req *types.UserLikesRequest)) *MockUserComponent_UnLikeCollection_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(*types.UserLikesRequest)) + }) + return _c +} + +func (_c *MockUserComponent_UnLikeCollection_Call) Return(_a0 error) *MockUserComponent_UnLikeCollection_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockUserComponent_UnLikeCollection_Call) RunAndReturn(run func(context.Context, *types.UserLikesRequest) error) *MockUserComponent_UnLikeCollection_Call { + _c.Call.Return(run) + return _c +} + +// NewMockUserComponent creates a new instance of MockUserComponent. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockUserComponent(t interface { + mock.TestingT + Cleanup(func()) +}) *MockUserComponent { + mock := &MockUserComponent{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/api/handler/git_http.go b/api/handler/git_http.go index 5047cea1..a9bcb86b 100644 --- a/api/handler/git_http.go +++ b/api/handler/git_http.go @@ -25,12 +25,12 @@ func NewGitHTTPHandler(config *config.Config) (*GitHTTPHandler, error) { return nil, err } return &GitHTTPHandler{ - c: uc, + gitHttp: uc, }, nil } type GitHTTPHandler struct { - c component.GitHTTPComponent + gitHttp component.GitHTTPComponent } func (h *GitHTTPHandler) InfoRefs(ctx *gin.Context) { @@ -53,7 +53,7 @@ func (h *GitHTTPHandler) InfoRefs(ctx *gin.Context) { GitProtocol: gitProtocol, CurrentUser: httpbase.GetCurrentUser(ctx), } - reader, err := h.c.InfoRefs(ctx, req) + reader, err := h.gitHttp.InfoRefs(ctx, req) if err != nil { if err == component.ErrUnauthorized { ctx.Header("WWW-Authenticate", "Basic realm=opencsg-git") @@ -109,7 +109,7 @@ func (h *GitHTTPHandler) GitUploadPack(ctx *gin.Context) { ctx.Header("Content-Type", fmt.Sprintf("application/x-%s-result", action)) ctx.Header("Cache-Control", "no-cache") - err := h.c.GitUploadPack(ctx, req) + err := h.gitHttp.GitUploadPack(ctx, req) if err != nil { httpbase.ServerError(ctx, err) return @@ -132,7 +132,7 @@ func (h *GitHTTPHandler) GitReceivePack(ctx *gin.Context) { ctx.Header("Content-Type", fmt.Sprintf("application/x-%s-result", action)) ctx.Header("Cache-Control", "no-cache") - err := h.c.GitReceivePack(ctx, req) + err := h.gitHttp.GitReceivePack(ctx, req) if err != nil { if err == component.ErrUnauthorized { ctx.Header("WWW-Authenticate", "Basic realm=opencsg-git") @@ -175,12 +175,7 @@ func (h *GitHTTPHandler) LfsBatch(ctx *gin.Context) { return } - s3Internal := ctx.GetHeader("X-OPENCSG-S3-Internal") - if s3Internal == "true" { - ctx.Set("X-OPENCSG-S3-Internal", true) - } - - objectResponse, err := h.c.BuildObjectResponse(ctx, batchRequest, isUpload) + objectResponse, err := h.gitHttp.BuildObjectResponse(ctx, batchRequest, isUpload) if err != nil { if errors.Is(err, component.ErrUnauthorized) { ctx.Header("WWW-Authenticate", "Basic realm=opencsg-git") @@ -216,7 +211,7 @@ func (h *GitHTTPHandler) LfsUpload(ctx *gin.Context) { uploadRequest.RepoType = types.RepositoryType(ctx.GetString("repo_type")) uploadRequest.CurrentUser = httpbase.GetCurrentUser(ctx) - err = h.c.LfsUpload(ctx, ctx.Request.Body, uploadRequest) + err = h.gitHttp.LfsUpload(ctx, ctx.Request.Body, uploadRequest) if err != nil { httpbase.ServerError(ctx, err) return @@ -240,12 +235,7 @@ func (h *GitHTTPHandler) LfsDownload(ctx *gin.Context) { downloadRequest.CurrentUser = httpbase.GetCurrentUser(ctx) downloadRequest.SaveAs = ctx.Query("save_as") - s3Internal := ctx.GetHeader("X-OPENCSG-S3-Internal") - if s3Internal == "true" { - ctx.Set("X-OPENCSG-S3-Internal", true) - } - - url, err := h.c.LfsDownload(ctx, downloadRequest) + url, err := h.gitHttp.LfsDownload(ctx, downloadRequest) if err != nil { httpbase.ServerError(ctx, err) return @@ -269,7 +259,7 @@ func (h *GitHTTPHandler) LfsVerify(ctx *gin.Context) { verifyRequest.RepoType = types.RepositoryType(ctx.GetString("repo_type")) verifyRequest.CurrentUser = httpbase.GetCurrentUser(ctx) - err := h.c.LfsVerify(ctx, verifyRequest, pointer) + err := h.gitHttp.LfsVerify(ctx, verifyRequest, pointer) if err != nil { slog.Error("Bad request format", "error", err) httpbase.BadRequest(ctx, err.Error()) @@ -312,7 +302,7 @@ func (h *GitHTTPHandler) ListLocks(ctx *gin.Context) { } req.Limit = limit - res, err := h.c.ListLocks(ctx, req) + res, err := h.gitHttp.ListLocks(ctx, req) if err != nil { if errors.Is(err, component.ErrUnauthorized) { ctx.Header("WWW-Authenticate", "Basic realm=opencsg-git") @@ -344,7 +334,7 @@ func (h *GitHTTPHandler) CreateLock(ctx *gin.Context) { req.RepoType = types.RepositoryType(ctx.GetString("repo_type")) req.CurrentUser = httpbase.GetCurrentUser(ctx) - lock, err := h.c.CreateLock(ctx, req) + lock, err := h.gitHttp.CreateLock(ctx, req) if err != nil { if errors.Is(err, component.ErrAlreadyExists) { ctx.PureJSON(http.StatusConflict, types.LFSLockError{ @@ -413,7 +403,7 @@ func (h *GitHTTPHandler) VerifyLock(ctx *gin.Context) { } req.Limit = limit - res, err := h.c.VerifyLock(ctx, req) + res, err := h.gitHttp.VerifyLock(ctx, req) if err != nil { slog.Error("Bad request format", "error", err) ctx.PureJSON(http.StatusInternalServerError, types.LFSLockError{ @@ -452,7 +442,7 @@ func (h *GitHTTPHandler) UnLock(ctx *gin.Context) { req.RepoType = types.RepositoryType(ctx.GetString("repo_type")) req.CurrentUser = httpbase.GetCurrentUser(ctx) - lock, err = h.c.UnLock(ctx, req) + lock, err = h.gitHttp.UnLock(ctx, req) if err != nil { if errors.Is(err, component.ErrUnauthorized) { ctx.Header("WWW-Authenticate", "Basic realm=opencsg-git") diff --git a/api/handler/git_http_test.go b/api/handler/git_http_test.go new file mode 100644 index 00000000..e1d844fa --- /dev/null +++ b/api/handler/git_http_test.go @@ -0,0 +1,315 @@ +package handler + +import ( + "bytes" + "compress/gzip" + "io" + "net/url" + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/require" + mockcomponent "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/component" + "opencsg.com/csghub-server/builder/store/database" + "opencsg.com/csghub-server/common/types" +) + +type GitHTTPTester struct { + *GinTester + handler *GitHTTPHandler + mocks struct { + gitHttp *mockcomponent.MockGitHTTPComponent + } +} + +func NewGitHTTPTester(t *testing.T) *GitHTTPTester { + tester := &GitHTTPTester{GinTester: NewGinTester()} + tester.mocks.gitHttp = mockcomponent.NewMockGitHTTPComponent(t) + + tester.handler = &GitHTTPHandler{ + gitHttp: tester.mocks.gitHttp, + } + tester.WithParam("repo", "testRepo") + tester.WithParam("branch", "testBranch") + return tester +} + +func (t *GitHTTPTester) WithHandleFunc(fn func(h *GitHTTPHandler) gin.HandlerFunc) *GitHTTPTester { + t.ginHandler = fn(t.handler) + return t +} + +func TestGitHTTPHandler_InfoRefs(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.InfoRefs + }) + + reader := io.NopCloser(bytes.NewBuffer([]byte("foo"))) + tester.mocks.gitHttp.EXPECT().InfoRefs(tester.ctx, types.InfoRefsReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + Rpc: "git-upload-pack", + GitProtocol: "ssh", + CurrentUser: "u", + }).Return(reader, nil) + tester.WithQuery("service", "git-upload-pack").WithHeader("Git-Protocol", "ssh") + tester.WithKV("namespace", "u").WithKV("name", "r") + tester.WithKV("repo_type", "model").WithUser().WithHeader("Accept-Encoding", "gzip").Execute() + + require.Equal(t, 200, tester.response.Code) + var b bytes.Buffer + gz := gzip.NewWriter(&b) + _, err := gz.Write([]byte("foo")) + require.NoError(t, err) + err = gz.Close() + require.NoError(t, err) + require.Equal(t, b.String(), tester.response.Body.String()) + headers := tester.response.Header() + require.Equal(t, "application/x-git-upload-pack-advertisement", headers.Get("Content-Type")) + require.Equal(t, "no-cache", headers.Get("Cache-Control")) +} + +func TestGitHTTPHandler_GitUploadPack(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.GitUploadPack + }) + + tester.mocks.gitHttp.EXPECT().GitUploadPack(tester.ctx, types.GitUploadPackReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + GitProtocol: "ssh", + Request: tester.ctx.Request, + Writer: tester.ctx.Writer, + CurrentUser: "u", + }).Return(nil) + tester.SetPath("git").WithQuery("service", "git-upload-pack").WithHeader("Git-Protocol", "ssh") + tester.WithKV("namespace", "u").WithKV("name", "r") + tester.WithKV("repo_type", "model").WithUser().WithHeader("Accept-Encoding", "gzip").Execute() + + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "application/x-git-result", headers.Get("Content-Type")) + require.Equal(t, "no-cache", headers.Get("Cache-Control")) +} + +func TestGitHTTPHandler_GitReceivePack(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.GitReceivePack + }) + + tester.mocks.gitHttp.EXPECT().GitReceivePack(tester.ctx, types.GitUploadPackReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + GitProtocol: "ssh", + Request: tester.ctx.Request, + Writer: tester.ctx.Writer, + CurrentUser: "u", + }).Return(nil) + tester.SetPath("git").WithQuery("service", "git-upload-pack").WithHeader("Git-Protocol", "ssh") + tester.WithKV("namespace", "u").WithKV("name", "r") + tester.WithKV("repo_type", "model").WithUser().WithHeader("Accept-Encoding", "gzip").Execute() + + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, "application/x-git-result", headers.Get("Content-Type")) + require.Equal(t, "no-cache", headers.Get("Cache-Control")) +} + +func TestGitHTTPHandler_LfsBatch(t *testing.T) { + + for _, c := range []string{"upload", "download"} { + t.Run("c", func(t *testing.T) { + + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.LfsBatch + }) + + tester.mocks.gitHttp.EXPECT().BuildObjectResponse(tester.ctx, types.BatchRequest{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + Authorization: "auth", + Operation: c, + }, c == "upload").Return(&types.BatchResponse{Transfer: "t"}, nil) + tester.SetPath("git").WithQuery("service", "git-upload-pack").WithHeader("Git-Protocol", "ssh") + tester.WithKV("namespace", "u").WithKV("name", "r").WithHeader("Authorization", "auth") + tester.WithBody(t, &types.BatchRequest{Operation: c}) + tester.WithKV("repo_type", "model").WithUser().WithHeader("Accept-Encoding", "gzip").Execute() + + tester.ResponseEqSimple(t, 200, &types.BatchResponse{Transfer: "t"}) + headers := tester.response.Header() + require.Equal(t, types.LfsMediaType, headers.Get("Content-Type")) + }) + } +} + +func TestGitHTTPHandler_LfsUpload(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.LfsUpload + }) + + tester.mocks.gitHttp.EXPECT().LfsUpload(tester.ctx, tester.ctx.Request.Body, types.UploadRequest{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + Oid: "o", + Size: 100, + }).Return(nil) + tester.SetPath("git").WithParam("oid", "o").WithParam("size", "100") + tester.WithKV("namespace", "u").WithKV("name", "r").WithHeader("Authorization", "auth") + tester.WithKV("repo_type", "model").WithUser().Execute() + + require.Equal(t, 200, tester.response.Code) + headers := tester.response.Header() + require.Equal(t, types.LfsMediaType, headers.Get("Content-Type")) +} + +func TestGitHTTPHandler_LfsDownload(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.LfsDownload + }) + + tester.mocks.gitHttp.EXPECT().LfsDownload(tester.ctx, types.DownloadRequest{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + Oid: "o", + Size: 100, + }).Return(&url.URL{Path: "url"}, nil) + tester.SetPath("git").WithParam("oid", "o").WithParam("size", "100") + tester.WithKV("namespace", "u").WithKV("name", "r").WithHeader("Authorization", "auth") + tester.WithKV("repo_type", "model").WithUser().Execute() + + require.Equal(t, 200, tester.response.Code) + resp := tester.response.Result() + defer resp.Body.Close() + lc, err := resp.Location() + require.NoError(t, err) + require.Equal(t, "url", lc.String()) +} + +func TestGitHTTPHandler_LfsVerify(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.LfsVerify + }) + + tester.mocks.gitHttp.EXPECT().LfsVerify(tester.ctx, types.VerifyRequest{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + }, types.Pointer{Oid: "o"}).Return(nil) + tester.WithKV("namespace", "u").WithKV("name", "r").WithHeader("Authorization", "auth") + tester.WithKV("repo_type", "model").WithUser().WithBody(t, &types.Pointer{ + Oid: "o", + }).Execute() + + tester.ResponseEqSimple(t, 200, nil) +} + +func TestGitHTTPHandler_ListLocks(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.ListLocks + }) + + tester.mocks.gitHttp.EXPECT().ListLocks(tester.ctx, types.ListLFSLockReq{ + ID: 1, + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + Cursor: 12, + Path: "p", + Limit: 5, + }).Return(&types.LFSLockList{Next: "n"}, nil) + tester.WithKV("namespace", "u").WithKV("name", "r").WithQuery("path", "p").WithQuery("id", "1") + tester.WithKV("repo_type", "model").WithUser().WithQuery("cursor", "12").WithQuery("limit", "5").Execute() + + tester.ResponseEqSimple(t, 200, &types.LFSLockList{Next: "n"}) +} + +func TestGitHTTPHandler_CreateLock(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.CreateLock + }) + + tester.mocks.gitHttp.EXPECT().CreateLock(tester.ctx, types.LfsLockReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + }).Return(&database.LfsLock{ + ID: 1, + Path: "p", + User: database.User{Username: "u"}, + }, nil) + tester.WithKV("namespace", "u").WithKV("name", "r") + tester.WithKV("repo_type", "model").WithUser().WithBody(t, types.LfsLockReq{}).Execute() + + tester.ResponseEqSimple(t, 200, &types.LFSLockResponse{ + Lock: &types.LFSLock{ + ID: "1", + Path: "p", + Owner: &types.LFSLockOwner{ + Name: "u", + }, + }, + }) +} + +func TestGitHTTPHandler_VerifyLock(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.VerifyLock + }) + + tester.mocks.gitHttp.EXPECT().VerifyLock(tester.ctx, types.VerifyLFSLockReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + }).Return(&types.LFSLockListVerify{ + Next: "n", + }, nil) + tester.WithKV("namespace", "u").WithKV("name", "r") + tester.WithKV("repo_type", "model").WithUser().WithBody(t, types.VerifyLFSLockReq{}).Execute() + + tester.ResponseEqSimple(t, 200, &types.LFSLockListVerify{ + Next: "n", + }) +} + +func TestGitHTTPHandler_UnLock(t *testing.T) { + tester := NewGitHTTPTester(t).WithHandleFunc(func(h *GitHTTPHandler) gin.HandlerFunc { + return h.UnLock + }) + + tester.mocks.gitHttp.EXPECT().UnLock(tester.ctx, types.UnlockLFSReq{ + ID: 1, + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + }).Return(&database.LfsLock{ + ID: 1, + Path: "p", + User: database.User{Username: "u"}, + }, nil) + tester.WithKV("namespace", "u").WithKV("name", "r").WithParam("lid", "1") + tester.WithKV("repo_type", "model").WithUser().WithBody(t, types.UnlockLFSReq{}).Execute() + + tester.ResponseEqSimple(t, 200, &types.LFSLockResponse{ + Lock: &types.LFSLock{ + ID: "1", + Path: "p", + Owner: &types.LFSLockOwner{ + Name: "u", + }, + }, + }) +} diff --git a/api/handler/helper_test.go b/api/handler/helper_test.go index 4c62d995..7b9bdc38 100644 --- a/api/handler/helper_test.go +++ b/api/handler/helper_test.go @@ -22,6 +22,7 @@ type GinTester struct { ctx *gin.Context response *httptest.ResponseRecorder OKText string // text of httpbase.OK + _executed bool } func NewGinTester() *GinTester { @@ -45,6 +46,7 @@ func (g *GinTester) Handler(handler gin.HandlerFunc) { func (g *GinTester) Execute() { g.ginHandler(g.ctx) + g._executed = true } func (g *GinTester) WithUser() *GinTester { g.ctx.Set(httpbase.CurrentUserCtxVar, "u") @@ -87,6 +89,21 @@ func (g *GinTester) WithQuery(key, value string) *GinTester { return g } +func (g *GinTester) SetPath(path string) *GinTester { + g.ctx.Request.URL.Path = path + return g +} + +func (g *GinTester) WithHeader(key, value string) *GinTester { + h := g.ctx.Request.Header + if h == nil { + h = map[string][]string{} + } + h.Add(key, value) + g.ctx.Request.Header = h + return g +} + func (g *GinTester) AddPagination(page int, per int) *GinTester { g.WithQuery("page", cast.ToString(page)) g.WithQuery("per", cast.ToString(per)) @@ -94,6 +111,9 @@ func (g *GinTester) AddPagination(page int, per int) *GinTester { } func (g *GinTester) ResponseEq(t *testing.T, code int, msg string, expected any) { + if !g._executed { + require.FailNow(t, "call Execute method first") + } var r = struct { Msg string `json:"msg"` Data any `json:"data,omitempty"` @@ -109,9 +129,12 @@ func (g *GinTester) ResponseEq(t *testing.T, code int, msg string, expected any) } func (g *GinTester) ResponseEqSimple(t *testing.T, code int, expected any) { + if !g._executed { + require.FailNow(t, "call Execute method first") + } b, err := json.Marshal(expected) require.NoError(t, err) - require.Equal(t, code, g.response.Code) + require.Equal(t, code, g.response.Code, g.response.Body.String()) require.JSONEq(t, string(b), g.response.Body.String()) } @@ -121,6 +144,7 @@ func (g *GinTester) RequireUser(t *testing.T) { tmp := NewGinTester() tmp.ctx.Params = g.ctx.Params g.ginHandler(tmp.ctx) + tmp._executed = true tmp.ResponseEq(t, http.StatusUnauthorized, component.ErrUserNotFound.Error(), nil) // add user to original test ctx now _ = g.WithUser() diff --git a/api/handler/model.go b/api/handler/model.go index cd0eb829..218c1294 100644 --- a/api/handler/model.go +++ b/api/handler/model.go @@ -32,16 +32,16 @@ func NewModelHandler(config *config.Config) (*ModelHandler, error) { } return &ModelHandler{ - c: uc, - sc: sc, - repo: repo, + model: uc, + sensitive: sc, + repo: repo, }, nil } type ModelHandler struct { - c component.ModelComponent - sc component.SensitiveComponent - repo component.RepoComponent + model component.ModelComponent + repo component.RepoComponent + sensitive component.SensitiveComponent } // GetVisiableModels godoc @@ -92,7 +92,8 @@ func (h *ModelHandler) Index(ctx *gin.Context) { return } - models, total, err := h.c.Index(ctx, filter, per, page, false) + models, total, err := h.model.Index(ctx, filter, per, page, false) + if err != nil { slog.Error("Failed to get models", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -133,14 +134,14 @@ func (h *ModelHandler) Create(ctx *gin.Context) { } req.Username = currentUser - _, err := h.sc.CheckRequestV2(ctx, req) + _, err := h.sensitive.CheckRequestV2(ctx, req) if err != nil { slog.Error("failed to check sensitive request", slog.Any("error", err)) httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error()) return } - model, err := h.c.Create(ctx, req) + model, err := h.model.Create(ctx, req) if err != nil { slog.Error("Failed to create model", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -178,7 +179,7 @@ func (h *ModelHandler) Update(ctx *gin.Context) { return } - _, err := h.sc.CheckRequestV2(ctx, req) + _, err := h.sensitive.CheckRequestV2(ctx, req) if err != nil { slog.Error("failed to check sensitive request", slog.Any("error", err)) httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error()) @@ -195,7 +196,7 @@ func (h *ModelHandler) Update(ctx *gin.Context) { req.Name = name req.Username = currentUser - model, err := h.c.Update(ctx, req) + model, err := h.model.Update(ctx, req) if err != nil { slog.Error("Failed to update model", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -232,7 +233,7 @@ func (h *ModelHandler) Delete(ctx *gin.Context) { httpbase.BadRequest(ctx, err.Error()) return } - err = h.c.Delete(ctx, namespace, name, currentUser) + err = h.model.Delete(ctx, namespace, name, currentUser) if err != nil { slog.Error("Failed to delete model", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -264,7 +265,8 @@ func (h *ModelHandler) Show(ctx *gin.Context) { return } currentUser := httpbase.GetCurrentUser(ctx) - detail, err := h.c.Show(ctx, namespace, name, currentUser, false) + detail, err := h.model.Show(ctx, namespace, name, currentUser, false) + if err != nil { if errors.Is(err, component.ErrUnauthorized) { httpbase.UnauthorizedError(ctx, err) @@ -292,7 +294,7 @@ func (h *ModelHandler) SDKModelInfo(ctx *gin.Context) { ref = mappedBranch } currentUser := httpbase.GetCurrentUser(ctx) - modelInfo, err := h.c.SDKModelInfo(ctx, namespace, name, ref, currentUser) + modelInfo, err := h.model.SDKModelInfo(ctx, namespace, name, ref, currentUser) if err != nil { if errors.Is(err, component.ErrUnauthorized) { httpbase.UnauthorizedError(ctx, err) @@ -327,7 +329,7 @@ func (h *ModelHandler) Relations(ctx *gin.Context) { return } currentUser := httpbase.GetCurrentUser(ctx) - detail, err := h.c.Relations(ctx, namespace, name, currentUser) + detail, err := h.model.Relations(ctx, namespace, name, currentUser) if err != nil { if errors.Is(err, component.ErrUnauthorized) { httpbase.UnauthorizedError(ctx, err) @@ -379,7 +381,7 @@ func (h *ModelHandler) SetRelations(ctx *gin.Context) { req.Name = name req.CurrentUser = currentUser - err = h.c.SetRelationDatasets(ctx, req) + err = h.model.SetRelationDatasets(ctx, req) if err != nil { slog.Error("Failed to set datasets for model", slog.Any("req", req), slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -426,7 +428,7 @@ func (h *ModelHandler) AddDatasetRelation(ctx *gin.Context) { req.Name = name req.CurrentUser = currentUser - err = h.c.AddRelationDataset(ctx, req) + err = h.model.AddRelationDataset(ctx, req) if err != nil { slog.Error("Failed to add dataset for model", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -473,7 +475,7 @@ func (h *ModelHandler) DelDatasetRelation(ctx *gin.Context) { req.Name = name req.CurrentUser = currentUser - err = h.c.DelRelationDataset(ctx, req) + err = h.model.DelRelationDataset(ctx, req) if err != nil { slog.Error("Failed to delete dataset for model", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -598,7 +600,7 @@ func (h *ModelHandler) DeployDedicated(ctx *gin.Context) { return } - _, err = h.sc.CheckRequestV2(ctx, &req) + _, err = h.sensitive.CheckRequestV2(ctx, &req) if err != nil { slog.Error("failed to check sensitive request", slog.Any("error", err)) httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error()) @@ -611,7 +613,7 @@ func (h *ModelHandler) DeployDedicated(ctx *gin.Context) { CurrentUser: currentUser, DeployType: types.InferenceType, } - deployID, err := h.c.Deploy(ctx, epReq, req) + deployID, err := h.model.Deploy(ctx, epReq, req) if err != nil { slog.Error("failed to deploy model as inference", slog.String("namespace", namespace), slog.String("name", name), slog.Any("currentUser", currentUser), slog.Any("req", req), slog.Any("error", err)) @@ -693,7 +695,7 @@ func (h *ModelHandler) FinetuneCreate(ctx *gin.Context) { DeployType: types.FinetuneType, } - deployID, err := h.c.Deploy(ctx, ftReq, *modelReq) + deployID, err := h.model.Deploy(ctx, ftReq, *modelReq) if err != nil { slog.Error("failed to deploy model as notebook instance", slog.String("namespace", namespace), slog.String("name", name), slog.Any("error", err)) @@ -975,7 +977,7 @@ func (h *ModelHandler) ListByRuntimeFrameworkID(ctx *gin.Context) { return } - models, total, err := h.c.ListModelsByRuntimeFrameworkID(ctx, currentUser, per, page, id, deployType) + models, total, err := h.model.ListModelsByRuntimeFrameworkID(ctx, currentUser, per, page, id, deployType) if err != nil { slog.Error("Failed to get models", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -1115,7 +1117,7 @@ func (h *ModelHandler) ListAllRuntimeFramework(ctx *gin.Context) { return } - runtimes, err := h.c.ListAllByRuntimeFramework(ctx, currentUser) + runtimes, err := h.model.ListAllByRuntimeFramework(ctx, currentUser) if err != nil { slog.Error("Failed to get runtime frameworks", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -1171,7 +1173,7 @@ func (h *ModelHandler) UpdateModelRuntimeFrameworks(ctx *gin.Context) { slog.Info("update runtime frameworks models", slog.Any("req", req), slog.Any("runtime framework id", id), slog.Any("deployType", deployType)) - list, err := h.c.SetRuntimeFrameworkModes(ctx, deployType, id, req.Models) + list, err := h.model.SetRuntimeFrameworkModes(ctx, deployType, id, req.Models) if err != nil { slog.Error("Failed to set models runtime framework", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -1224,7 +1226,7 @@ func (h *ModelHandler) DeleteModelRuntimeFrameworks(ctx *gin.Context) { slog.Info("update runtime frameworks models", slog.Any("req", req), slog.Any("runtime framework id", id), slog.Any("deployType", deployType)) - list, err := h.c.DeleteRuntimeFrameworkModes(ctx, deployType, id, req.Models) + list, err := h.model.DeleteRuntimeFrameworkModes(ctx, deployType, id, req.Models) if err != nil { slog.Error("Failed to set models runtime framework", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -1277,7 +1279,7 @@ func (h *ModelHandler) ListModelsOfRuntimeFrameworks(ctx *gin.Context) { return } - models, total, err := h.c.ListModelsOfRuntimeFrameworks(ctx, currentUser, filter.Search, filter.Sort, per, page, deployType) + models, total, err := h.model.ListModelsOfRuntimeFrameworks(ctx, currentUser, filter.Search, filter.Sort, per, page, deployType) if err != nil { slog.Error("fail to get models for all runtime frameworks", slog.Any("deployType", deployType), slog.Any("per", per), slog.Any("page", page), slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -1382,7 +1384,7 @@ func (h *ModelHandler) DeployServerless(ctx *gin.Context) { } req.SecureLevel = 1 // public for serverless - deployID, err := h.c.Deploy(ctx, deployReq, req) + deployID, err := h.model.Deploy(ctx, deployReq, req) if err != nil { slog.Error("failed to deploy model as serverless", slog.String("namespace", namespace), slog.String("name", name), slog.Any("currentUser", currentUser), slog.Any("req", req), slog.Any("error", err)) @@ -1532,7 +1534,7 @@ func (h *ModelHandler) GetDeployServerless(ctx *gin.Context) { return } - response, err := h.c.GetServerless(ctx, namespace, name, currentUser) + response, err := h.model.GetServerless(ctx, namespace, name, currentUser) if err != nil { slog.Error("failed to get model serverless endpoint", slog.String("namespace", namespace), slog.String("name", name), slog.Any("currentUser", currentUser), slog.Any("error", err)) diff --git a/api/handler/model_test.go b/api/handler/model_test.go new file mode 100644 index 00000000..5a082cdc --- /dev/null +++ b/api/handler/model_test.go @@ -0,0 +1,561 @@ +package handler + +import ( + "fmt" + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/require" + mockcomponent "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/component" + "opencsg.com/csghub-server/builder/store/database" + "opencsg.com/csghub-server/common/types" +) + +type ModelTester struct { + *GinTester + handler *ModelHandler + mocks struct { + model *mockcomponent.MockModelComponent + sensitive *mockcomponent.MockSensitiveComponent + repo *mockcomponent.MockRepoComponent + } +} + +func NewModelTester(t *testing.T) *ModelTester { + tester := &ModelTester{GinTester: NewGinTester()} + tester.mocks.model = mockcomponent.NewMockModelComponent(t) + tester.mocks.sensitive = mockcomponent.NewMockSensitiveComponent(t) + tester.mocks.repo = mockcomponent.NewMockRepoComponent(t) + + tester.handler = &ModelHandler{ + model: tester.mocks.model, sensitive: tester.mocks.sensitive, + repo: tester.mocks.repo, + } + tester.WithParam("name", "r") + tester.WithParam("namespace", "u") + return tester + +} + +func (t *ModelTester) WithHandleFunc(fn func(h *ModelHandler) gin.HandlerFunc) *ModelTester { + t.ginHandler = fn(t.handler) + return t + +} + +func TestModelHandler_Index(t *testing.T) { + cases := []struct { + sort string + source string + error bool + }{ + {"most_download", "local", false}, + {"foo", "local", true}, + {"most_download", "bar", true}, + } + + for _, c := range cases { + t.Run(fmt.Sprintf("%+v", c), func(t *testing.T) { + + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.Index + }) + + if !c.error { + tester.mocks.model.EXPECT().Index(tester.ctx, &types.RepoFilter{ + Search: "foo", + Sort: c.sort, + Source: c.source, + }, 10, 1, false).Return([]*types.Model{ + {Name: "cc"}, + }, 100, nil) + } + + tester.AddPagination(1, 10).WithQuery("search", "foo"). + WithQuery("sort", c.sort). + WithQuery("source", c.source).Execute() + + if c.error { + require.Equal(t, 400, tester.response.Code) + } else { + tester.ResponseEqSimple(t, 200, gin.H{ + "data": []*types.Model{{Name: "cc"}}, + "total": 100, + }) + } + }) + } +} + +func TestModelHandler_Create(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.Create + }) + tester.RequireUser(t) + + req := &types.CreateModelReq{CreateRepoReq: types.CreateRepoReq{Username: "u"}} + tester.mocks.sensitive.EXPECT().CheckRequestV2(tester.ctx, req).Return(true, nil) + tester.mocks.model.EXPECT().Create(tester.ctx, req).Return(&types.Model{Name: "m"}, nil) + tester.WithBody(t, req).Execute() + + tester.ResponseEq(t, 200, tester.OKText, &types.Model{Name: "m"}) +} + +func TestModelHandler_Update(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.Update + }) + tester.RequireUser(t) + + req := &types.UpdateModelReq{UpdateRepoReq: types.UpdateRepoReq{}} + tester.mocks.sensitive.EXPECT().CheckRequestV2(tester.ctx, req).Return(true, nil) + tester.mocks.model.EXPECT().Update(tester.ctx, &types.UpdateModelReq{ + UpdateRepoReq: types.UpdateRepoReq{ + Namespace: "u", + Name: "r", + Username: "u", + }, + }).Return(&types.Model{Name: "m"}, nil) + tester.WithBody(t, req).Execute() + + tester.ResponseEq(t, 200, tester.OKText, &types.Model{Name: "m"}) +} + +func TestModelHandler_Delete(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.Delete + }) + tester.RequireUser(t) + + tester.mocks.model.EXPECT().Delete(tester.ctx, "u", "r", "u").Return(nil) + tester.Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_Show(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.Show + }) + + tester.WithUser() + tester.mocks.model.EXPECT().Show(tester.ctx, "u", "r", "u", false).Return(&types.Model{ + Name: "m", + }, nil) + tester.Execute() + + tester.ResponseEq(t, 200, tester.OKText, &types.Model{Name: "m"}) +} + +func TestModelHandler_SDKModelInfo(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.SDKModelInfo + }) + + tester.WithUser() + tester.mocks.model.EXPECT().SDKModelInfo(tester.ctx, "u", "r", "main", "u").Return(&types.SDKModelInfo{ + ID: "m", + }, nil) + tester.WithParam("ref", "main").Execute() + + tester.ResponseEqSimple(t, 200, &types.SDKModelInfo{ID: "m"}) +} + +func TestModelHandler_Relations(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.Relations + }) + + tester.WithUser() + tester.mocks.model.EXPECT().Relations(tester.ctx, "u", "r", "u").Return(&types.Relations{ + Models: []*types.Model{{Name: "m1"}}, + }, nil) + tester.Execute() + + tester.ResponseEq(t, 200, tester.OKText, &types.Relations{ + Models: []*types.Model{{Name: "m1"}}, + }) +} + +func TestModelHandler_SetRelations(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.SetRelations + }) + tester.RequireUser(t) + + req := &types.RelationDatasets{ + Namespace: "u", + Name: "r", + CurrentUser: "u", + } + tester.mocks.model.EXPECT().SetRelationDatasets(tester.ctx, *req).Return(nil) + tester.WithBody(t, &types.RelationDatasets{}).Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_AddDatasetRelation(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.AddDatasetRelation + }) + tester.RequireUser(t) + + req := &types.RelationDataset{ + Namespace: "u", + Name: "r", + CurrentUser: "u", + } + tester.mocks.model.EXPECT().AddRelationDataset(tester.ctx, *req).Return(nil) + tester.WithBody(t, &types.RelationDataset{}).Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_DelDatasetRelation(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.DelDatasetRelation + }) + tester.RequireUser(t) + + req := &types.RelationDataset{ + Namespace: "u", + Name: "r", + CurrentUser: "u", + } + tester.mocks.model.EXPECT().DelRelationDataset(tester.ctx, *req).Return(nil) + tester.WithBody(t, &types.RelationDataset{}).Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_DeployDedicated(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.DeployDedicated + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().AllowReadAccess(tester.ctx, types.ModelRepo, "u", "r", "u").Return(true, nil) + tester.mocks.sensitive.EXPECT().CheckRequestV2(tester.ctx, &types.ModelRunReq{ + Revision: "main", + MinReplica: 1, + MaxReplica: 2, + }).Return(true, nil) + tester.mocks.model.EXPECT().Deploy(tester.ctx, types.DeployActReq{ + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployType: types.InferenceType, + }, types.ModelRunReq{MinReplica: 1, MaxReplica: 2, Revision: "main"}).Return(123, nil) + + tester.WithBody(t, &types.ModelRunReq{MinReplica: 1, MaxReplica: 2}).Execute() + + tester.ResponseEq(t, 200, tester.OKText, types.DeployRepo{DeployID: 123}) +} + +func TestModelHandler_FinetuneCreate(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.FinetuneCreate + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().AllowAdminAccess(tester.ctx, types.ModelRepo, "u", "r", "u").Return(true, nil) + + tester.mocks.model.EXPECT().Deploy(tester.ctx, types.DeployActReq{ + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployType: types.FinetuneType, + }, types.ModelRunReq{MinReplica: 1, MaxReplica: 1, Revision: "main", SecureLevel: 2}).Return(123, nil) + + tester.WithBody(t, &types.ModelRunReq{MinReplica: 1, MaxReplica: 2, Revision: "main"}).Execute() + + tester.ResponseEq(t, 200, tester.OKText, types.DeployRepo{DeployID: 123}) + +} + +func TestModelHandler_DeployDelete(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.DeployDelete + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().DeleteDeploy(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.InferenceType, + }).Return(nil) + + tester.WithParam("id", "1").Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_FinetuneDelete(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.FinetuneDelete + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().DeleteDeploy(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.FinetuneType, + }).Return(nil) + + tester.WithParam("id", "1").Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_DeployStop(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.DeployStop + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().DeployStop(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.InferenceType, + }).Return(nil) + + tester.WithParam("id", "1").Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_DeployStart(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.DeployStart + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().DeployStart(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.InferenceType, + }).Return(nil) + + tester.WithParam("id", "1").Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_FinetuneStop(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.FinetuneStop + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().DeployStop(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.FinetuneType, + }).Return(nil) + + tester.WithParam("id", "1").Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_FinetuneStart(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.FinetuneStart + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().DeployStart(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.FinetuneType, + }).Return(nil) + + tester.WithParam("id", "1").Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_ListByRuntimeFrameworkID(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.ListByRuntimeFrameworkID + }) + tester.RequireUser(t) + + tester.WithQuery("deploy_type", "").AddPagination(1, 10).WithParam("id", "1") + tester.mocks.model.EXPECT().ListModelsByRuntimeFrameworkID( + tester.ctx, "u", 10, 1, int64(1), types.InferenceType, + ).Return([]types.Model{{Name: "foo"}}, 100, nil) + tester.Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "data": []types.Model{{Name: "foo"}}, + "total": 100, + }) +} + +func TestModelHandler_ListAllRuntimeFramework(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.ListAllRuntimeFramework + }) + tester.RequireUser(t) + + tester.mocks.model.EXPECT().ListAllByRuntimeFramework( + tester.ctx, "u", + ).Return([]database.RuntimeFramework{{FrameName: "foo"}}, nil) + tester.Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "data": []database.RuntimeFramework{{FrameName: "foo"}}, + }) +} + +func TestModelHandler_UpdateModelRuntimeFramework(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.UpdateModelRuntimeFrameworks + }) + + tester.WithUser().WithQuery("deploy_type", "").AddPagination(1, 10).WithParam("id", "1") + tester.mocks.model.EXPECT().SetRuntimeFrameworkModes( + tester.ctx, types.InferenceType, int64(1), []string{"foo"}, + ).Return([]string{"bar"}, nil) + tester.WithBody(t, types.RuntimeFrameworkModels{ + Models: []string{"foo"}, + }).Execute() + tester.ResponseEq(t, 200, tester.OKText, []string{"bar"}) +} + +func TestModelHandler_DeleteModelRuntimeFramework(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.DeleteModelRuntimeFrameworks + }) + + tester.WithUser().WithQuery("deploy_type", "").AddPagination(1, 10).WithParam("id", "1") + tester.mocks.model.EXPECT().DeleteRuntimeFrameworkModes( + tester.ctx, types.InferenceType, int64(1), []string{"foo"}, + ).Return([]string{"bar"}, nil) + tester.WithBody(t, types.RuntimeFrameworkModels{ + Models: []string{"foo"}, + }).Execute() + tester.ResponseEq(t, 200, tester.OKText, []string{"bar"}) +} + +func TestModelHandler_ListModelsOfRuntimeFrameworks(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.ListModelsOfRuntimeFrameworks + }) + tester.RequireUser(t) + + tester.WithQuery("deploy_type", "").AddPagination(1, 10).WithParam("id", "1") + tester.mocks.model.EXPECT().ListModelsOfRuntimeFrameworks( + tester.ctx, "u", "foo", "most_downloads", 10, 1, types.InferenceType, + ).Return([]types.Model{{Name: "foo"}}, 100, nil) + tester.WithQuery("search", "foo").WithQuery("sort", "most_downloads").Execute() + + tester.ResponseEqSimple(t, 200, gin.H{ + "data": []types.Model{{Name: "foo"}}, + "total": 100, + }) +} + +func TestModelHandler_AllFiles(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.AllFiles + }) + + tester.mocks.repo.EXPECT().AllFiles(tester.ctx, types.GetAllFilesReq{ + Namespace: "u", + Name: "r", + RepoType: types.ModelRepo, + CurrentUser: "u", + }).Return([]*types.File{{Name: "foo"}}, nil) + tester.WithUser().Execute() + + tester.ResponseEq(t, 200, tester.OKText, []*types.File{{Name: "foo"}}) +} + +func TestModelHandler_DeployServerless(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.DeployServerless + }) + tester.RequireUser(t) + + tester.mocks.model.EXPECT().Deploy(tester.ctx, types.DeployActReq{ + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployType: types.ServerlessType, + }, types.ModelRunReq{MinReplica: 1, MaxReplica: 2, Revision: "main", SecureLevel: 1}).Return(123, nil) + + tester.WithBody(t, &types.ModelRunReq{MinReplica: 1, MaxReplica: 2}).Execute() + + tester.ResponseEq(t, 200, tester.OKText, types.DeployRepo{DeployID: 123}) +} + +func TestModelHandler_ServerlessStop(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.ServerlessStop + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().DeployStop(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.ServerlessType, + }).Return(nil) + + tester.WithParam("id", "1").Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_ServerlessStart(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.ServerlessStart + }) + tester.RequireUser(t) + + tester.mocks.repo.EXPECT().DeployStart(tester.ctx, types.DeployActReq{ + RepoType: types.ModelRepo, + Namespace: "u", + Name: "r", + CurrentUser: "u", + DeployID: 1, + DeployType: types.ServerlessType, + }).Return(nil) + + tester.WithParam("id", "1").Execute() + + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestModelHandler_GetDeployServerless(t *testing.T) { + tester := NewModelTester(t).WithHandleFunc(func(h *ModelHandler) gin.HandlerFunc { + return h.GetDeployServerless + }) + + tester.mocks.model.EXPECT().GetServerless(tester.ctx, "u", "r", "u").Return(&types.DeployRepo{ + DeployID: 1, + }, nil) + tester.WithUser().Execute() + + tester.ResponseEq(t, 200, tester.OKText, &types.DeployRepo{DeployID: 1}) +} diff --git a/api/handler/user.go b/api/handler/user.go index c062710f..41984d76 100644 --- a/api/handler/user.go +++ b/api/handler/user.go @@ -20,12 +20,12 @@ func NewUserHandler(config *config.Config) (*UserHandler, error) { return nil, err } return &UserHandler{ - c: uc, + user: uc, }, nil } type UserHandler struct { - c component.UserComponent + user component.UserComponent } // GetUserDatasets godoc @@ -53,7 +53,7 @@ func (h *UserHandler) Datasets(ctx *gin.Context) { req.CurrentUser = httpbase.GetCurrentUser(ctx) req.Page = page req.PageSize = per - ds, total, err := h.c.Datasets(ctx, &req) + ds, total, err := h.user.Datasets(ctx, &req) if err != nil { slog.Error("Failed to gat user datasets", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -94,7 +94,7 @@ func (h *UserHandler) Models(ctx *gin.Context) { req.CurrentUser = httpbase.GetCurrentUser(ctx) req.Page = page req.PageSize = per - ms, total, err := h.c.Models(ctx, &req) + ms, total, err := h.user.Models(ctx, &req) if err != nil { slog.Error("Failed to gat user models", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -136,7 +136,7 @@ func (h *UserHandler) Codes(ctx *gin.Context) { req.CurrentUser = httpbase.GetCurrentUser(ctx) req.Page = page req.PageSize = per - ms, total, err := h.c.Codes(ctx, &req) + ms, total, err := h.user.Codes(ctx, &req) if err != nil { slog.Error("Failed to gat user codes", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -177,7 +177,7 @@ func (h *UserHandler) Spaces(ctx *gin.Context) { req.CurrentUser = httpbase.GetCurrentUser(ctx) req.Page = page req.PageSize = per - ms, total, err := h.c.Spaces(ctx, &req) + ms, total, err := h.user.Spaces(ctx, &req) if err != nil { slog.Error("Failed to gat user space", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -219,8 +219,8 @@ func (h *UserHandler) LikesAdd(ctx *gin.Context) { httpbase.ServerError(ctx, err) return } - req.Repo_id = repo_id - err = h.c.AddLikes(ctx, &req) + req.RepoID = repo_id + err = h.user.AddLikes(ctx, &req) if err != nil { httpbase.ServerError(ctx, err) return @@ -257,7 +257,7 @@ func (h *UserHandler) LikesCollections(ctx *gin.Context) { req.CurrentUser = httpbase.GetCurrentUser(ctx) req.Page = page req.PageSize = per - ms, total, err := h.c.LikesCollection(ctx, &req) + ms, total, err := h.user.LikesCollection(ctx, &req) if err != nil { slog.Error("Failed to get user collections", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -296,7 +296,7 @@ func (h *UserHandler) UserCollections(ctx *gin.Context) { req.CurrentUser = httpbase.GetCurrentUser(ctx) req.Page = page req.PageSize = per - ms, total, err := h.c.Collections(ctx, &req) + ms, total, err := h.user.Collections(ctx, &req) if err != nil { slog.Error("Failed to get user collections", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -331,13 +331,13 @@ func (h *UserHandler) LikeCollection(ctx *gin.Context) { } var req types.UserLikesRequest req.CurrentUser = currentUser - collection_id, err := strconv.ParseInt(ctx.Param("id"), 10, 64) + collectionID, err := strconv.ParseInt(ctx.Param("id"), 10, 64) if err != nil { httpbase.ServerError(ctx, err) return } - req.Collection_id = collection_id - err = h.c.LikeCollection(ctx, &req) + req.CollectionID = collectionID + err = h.user.LikeCollection(ctx, &req) if err != nil { httpbase.ServerError(ctx, err) return @@ -370,8 +370,8 @@ func (h *UserHandler) UnLikeCollection(ctx *gin.Context) { httpbase.ServerError(ctx, err) return } - req.Collection_id = collection_id - err = h.c.UnLikeCollection(ctx, &req) + req.CollectionID = collection_id + err = h.user.UnLikeCollection(ctx, &req) if err != nil { httpbase.ServerError(ctx, err) return @@ -406,9 +406,9 @@ func (h *UserHandler) LikesDelete(ctx *gin.Context) { httpbase.ServerError(ctx, err) return } - req.Repo_id = repo_id + req.RepoID = repo_id // slog.Info("user.likes.delete.req=%v", req) - err = h.c.DeleteLikes(ctx, &req) + err = h.user.DeleteLikes(ctx, &req) if err != nil { httpbase.ServerError(ctx, err) return @@ -446,7 +446,7 @@ func (h *UserHandler) LikesSpaces(ctx *gin.Context) { req.Page = page req.PageSize = per - ms, total, err := h.c.LikesSpaces(ctx, &req) + ms, total, err := h.user.LikesSpaces(ctx, &req) if err != nil { slog.Error("Failed to gat user space", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -492,7 +492,7 @@ func (h *UserHandler) LikesCodes(ctx *gin.Context) { req.CurrentUser = currentUser req.Page = page req.PageSize = per - ms, total, err := h.c.LikesCodes(ctx, &req) + ms, total, err := h.user.LikesCodes(ctx, &req) if err != nil { slog.Error("Failed to gat user codes", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -539,7 +539,7 @@ func (h *UserHandler) LikesModels(ctx *gin.Context) { req.CurrentUser = currentUser req.Page = page req.PageSize = per - ms, total, err := h.c.LikesModels(ctx, &req) + ms, total, err := h.user.LikesModels(ctx, &req) if err != nil { slog.Error("Failed to gat user models", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -586,7 +586,7 @@ func (h *UserHandler) LikesDatasets(ctx *gin.Context) { req.CurrentUser = currentUser req.Page = page req.PageSize = per - ds, total, err := h.c.LikesDatasets(ctx, &req) + ds, total, err := h.user.LikesDatasets(ctx, &req) if err != nil { slog.Error("Failed to gat user datasets", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -683,7 +683,7 @@ func (h *UserHandler) GetRunDeploys(ctx *gin.Context) { req.PageSize = per req.RepoType = repoType req.DeployType = deployType - ds, total, err := h.c.ListDeploys(ctx, repoType, &req) + ds, total, err := h.user.ListDeploys(ctx, repoType, &req) if err != nil { slog.Error("Failed to get deploy repo list", slog.Any("error", err), slog.Any("req", req)) httpbase.ServerError(ctx, err) @@ -743,7 +743,7 @@ func (h *UserHandler) GetFinetuneInstances(ctx *gin.Context) { req.CurrentUser = currentUser req.Page = page req.PageSize = per - ds, total, err := h.c.ListInstances(ctx, &req) + ds, total, err := h.user.ListInstances(ctx, &req) if err != nil { slog.Error("Failed to get instance list", slog.Any("error", err), slog.Any("req", req)) httpbase.ServerError(ctx, err) @@ -799,7 +799,7 @@ func (h *UserHandler) GetRunServerless(ctx *gin.Context) { req.PageSize = per req.RepoType = types.ModelRepo req.DeployType = types.ServerlessType - ds, total, err := h.c.ListServerless(ctx, req) + ds, total, err := h.user.ListServerless(ctx, req) if err != nil { slog.Error("Failed to get serverless list", slog.Any("error", err), slog.Any("req", req)) httpbase.ServerError(ctx, err) @@ -840,7 +840,7 @@ func (h *UserHandler) Prompts(ctx *gin.Context) { req.CurrentUser = httpbase.GetCurrentUser(ctx) req.Page = page req.PageSize = per - ds, total, err := h.c.Prompts(ctx, &req) + ds, total, err := h.user.Prompts(ctx, &req) if err != nil { slog.Error("Failed to get user prompts", slog.Any("error", err)) httpbase.ServerError(ctx, err) @@ -887,7 +887,7 @@ func (h *UserHandler) GetEvaluations(ctx *gin.Context) { req.CurrentUser = httpbase.GetCurrentUser(ctx) req.Page = page req.PageSize = per - ds, total, err := h.c.Evaluations(ctx, &req) + ds, total, err := h.user.Evaluations(ctx, &req) if err != nil { slog.Error("Failed to get user evaluations", slog.Any("error", err)) httpbase.ServerError(ctx, err) diff --git a/api/handler/user_test.go b/api/handler/user_test.go new file mode 100644 index 00000000..d8544d54 --- /dev/null +++ b/api/handler/user_test.go @@ -0,0 +1,439 @@ +package handler + +import ( + "testing" + + "github.com/gin-gonic/gin" + mockcomponent "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/component" + "opencsg.com/csghub-server/common/types" +) + +type UserTester struct { + *GinTester + handler *UserHandler + mocks struct { + user *mockcomponent.MockUserComponent + } +} + +func NewUserTester(t *testing.T) *UserTester { + tester := &UserTester{GinTester: NewGinTester()} + tester.mocks.user = mockcomponent.NewMockUserComponent(t) + + tester.handler = &UserHandler{ + user: tester.mocks.user, + } + tester.WithParam("name", "u") + tester.WithParam("namespace", "r") + return tester +} + +func (t *UserTester) WithHandleFunc(fn func(h *UserHandler) gin.HandlerFunc) *UserTester { + t.ginHandler = fn(t.handler) + return t +} + +func TestUserHandler_Datasets(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.Datasets + }) + + tester.mocks.user.EXPECT().Datasets(tester.ctx, &types.UserDatasetsReq{ + Owner: "go", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Dataset{{Name: "ds"}}, 100, nil) + tester.AddPagination(1, 10).WithUser().WithParam("username", "go").Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.Dataset{{Name: "ds"}}, + "total": 100, + }) +} + +func TestUserHandler_Models(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.Models + }) + + tester.mocks.user.EXPECT().Models(tester.ctx, &types.UserDatasetsReq{ + Owner: "go", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Model{{Name: "ds"}}, 100, nil) + tester.AddPagination(1, 10).WithUser().WithParam("username", "go").Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.Model{{Name: "ds"}}, + "total": 100, + }) +} + +func TestUserHandler_Codes(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.Codes + }) + + tester.mocks.user.EXPECT().Codes(tester.ctx, &types.UserDatasetsReq{ + Owner: "go", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Code{{Name: "ds"}}, 100, nil) + tester.AddPagination(1, 10).WithUser().WithParam("username", "go").Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.Code{{Name: "ds"}}, + "total": 100, + }) +} + +func TestUserHandler_Spaces(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.Spaces + }) + + tester.mocks.user.EXPECT().Spaces(tester.ctx, &types.UserDatasetsReq{ + Owner: "go", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Space{{Name: "ds"}}, 100, nil) + tester.AddPagination(1, 10).WithUser().WithParam("username", "go").Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.Space{{Name: "ds"}}, + "total": 100, + }) +} + +func TestUserHandler_LikesAdd(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.LikesAdd + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().AddLikes(tester.ctx, &types.UserLikesRequest{ + Username: "go", + CurrentUser: "u", + RepoID: 123, + }).Return(nil) + tester.WithParam("username", "go").WithParam("repo_id", "123").Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestUserHandler_LikesCollections(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.LikesCollections + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().LikesCollection(tester.ctx, &types.UserCollectionReq{ + Owner: "go", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Collection{{ID: 1}}, 100, nil) + tester.WithParam("username", "go").AddPagination(1, 10).Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "data": []types.Collection{{ID: 1}}, + "total": 100, + }) +} + +func TestUserHandler_UserCollections(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.UserCollections + }) + + tester.mocks.user.EXPECT().Collections(tester.ctx, &types.UserCollectionReq{ + Owner: "go", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Collection{{ID: 1}}, 100, nil) + tester.WithParam("username", "go").WithUser().AddPagination(1, 10).Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "data": []types.Collection{{ID: 1}}, + "total": 100, + }) +} + +func TestUserHandler_LikeCollection(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.LikeCollection + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().LikeCollection(tester.ctx, &types.UserLikesRequest{ + CurrentUser: "u", + CollectionID: 123, + }).Return(nil) + tester.WithParam("id", "123").Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestUserHandler_UnLikeCollection(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.UnLikeCollection + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().UnLikeCollection(tester.ctx, &types.UserLikesRequest{ + CurrentUser: "u", + CollectionID: 123, + }).Return(nil) + tester.WithParam("id", "123").Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestUserHandler_LikesDelete(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.LikesDelete + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().DeleteLikes(tester.ctx, &types.UserLikesRequest{ + CurrentUser: "u", + RepoID: 123, + }).Return(nil) + tester.WithParam("repo_id", "123").Execute() + tester.ResponseEq(t, 200, tester.OKText, nil) +} + +func TestUserHandler_LikesSpaces(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.LikesSpaces + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().LikesSpaces(tester.ctx, &types.UserSpacesReq{ + Owner: "foo", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Space{{Name: "sp"}}, 100, nil) + tester.WithParam("username", "foo").AddPagination(1, 10).Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.Space{{Name: "sp"}}, + "total": 100, + }) +} + +func TestUserHandler_LikesCodes(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.LikesCodes + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().LikesCodes(tester.ctx, &types.UserDatasetsReq{ + Owner: "foo", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Code{{Name: "sp"}}, 100, nil) + tester.WithParam("username", "foo").AddPagination(1, 10).Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.Code{{Name: "sp"}}, + "total": 100, + }) +} + +func TestUserHandler_LikesModels(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.LikesModels + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().LikesModels(tester.ctx, &types.UserDatasetsReq{ + Owner: "foo", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Model{{Name: "sp"}}, 100, nil) + tester.WithParam("username", "foo").AddPagination(1, 10).Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.Model{{Name: "sp"}}, + "total": 100, + }) +} + +func TestUserHandler_LikesDatasets(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.LikesDatasets + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().LikesDatasets(tester.ctx, &types.UserDatasetsReq{ + Owner: "foo", + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.Dataset{{Name: "sp"}}, 100, nil) + tester.WithParam("username", "foo").AddPagination(1, 10).Execute() + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.Dataset{{Name: "sp"}}, + "total": 100, + }) +} + +func TestUserHandler_UserPermission(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.UserPermission + }) + tester.RequireUser(t) + + tester.Execute() + tester.ResponseEqSimple(t, 200, types.WhoamiResponse{ + Name: "u", + Auth: types.Auth{ + AccessToken: types.AccessToken{ + DisplayName: "u", + Role: "write", + }, + Type: "Bearer", + }, + }) +} + +func TestUserHandler_GetRunDeploys(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.GetRunDeploys + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().ListDeploys(tester.ctx, types.ModelRepo, &types.DeployReq{ + CurrentUser: "u", + RepoType: types.ModelRepo, + DeployType: 1, + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.DeployRepo{{DeployID: 1}}, 100, nil) + tester.WithParam("username", "u").WithQuery("deploy_type", "").AddPagination(1, 10) + tester.WithParam("repo_type", "model").Execute() + + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.DeployRepo{{DeployID: 1}}, + "total": 100, + }) +} + +func TestUserHandler_GetFinetuneInstances(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.GetFinetuneInstances + }) + + tester.mocks.user.EXPECT().ListInstances(tester.ctx, &types.UserRepoReq{ + CurrentUser: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.DeployRepo{{DeployID: 1}}, 100, nil) + tester.WithUser().WithParam("username", "u").WithQuery("deploy_type", "").AddPagination(1, 10) + tester.Execute() + + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.DeployRepo{{DeployID: 1}}, + "total": 100, + }) +} + +func TestUserHandler_GetRunServerless(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.GetRunServerless + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().ListServerless(tester.ctx, types.DeployReq{ + CurrentUser: "u", + RepoType: types.ModelRepo, + DeployType: 3, + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.DeployRepo{{DeployID: 1}}, 100, nil) + tester.WithParam("username", "u").WithQuery("deploy_type", "").AddPagination(1, 10) + tester.WithParam("repo_type", "model").Execute() + + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.DeployRepo{{DeployID: 1}}, + "total": 100, + }) +} + +func TestUserHandler_Prompts(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.Prompts + }) + + tester.mocks.user.EXPECT().Prompts(tester.ctx, &types.UserPromptsReq{ + CurrentUser: "u", + Owner: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.PromptRes{{ID: 123}}, 100, nil) + tester.WithUser().WithParam("username", "u").AddPagination(1, 10).Execute() + + tester.ResponseEqSimple(t, 200, gin.H{ + "message": "OK", + "data": []types.PromptRes{{ID: 123}}, + "total": 100, + }) +} + +func TestUserHandler_GetEvaluations(t *testing.T) { + tester := NewUserTester(t).WithHandleFunc(func(h *UserHandler) gin.HandlerFunc { + return h.GetEvaluations + }) + tester.RequireUser(t) + + tester.mocks.user.EXPECT().Evaluations(tester.ctx, &types.UserEvaluationReq{ + CurrentUser: "u", + Owner: "u", + PageOpts: types.PageOpts{ + Page: 1, + PageSize: 10, + }, + }).Return([]types.ArgoWorkFlowRes{{ID: 123}}, 100, nil) + tester.WithParam("username", "u").AddPagination(1, 10).Execute() + + tester.ResponseEqSimple(t, 200, gin.H{ + "data": []types.ArgoWorkFlowRes{{ID: 123}}, + "total": 100, + }) +} diff --git a/common/types/user.go b/common/types/user.go index 18d4ab65..4459713f 100644 --- a/common/types/user.go +++ b/common/types/user.go @@ -166,10 +166,10 @@ type User struct { } type UserLikesRequest struct { - Username string `json:"username"` - Repo_id int64 `json:"repo_id"` - Collection_id int64 `json:"collection_id"` - CurrentUser string `json:"current_user"` + Username string `json:"username"` + RepoID int64 `json:"repo_id"` + CollectionID int64 `json:"collection_id"` + CurrentUser string `json:"current_user"` } /* for HF compatible apis */ diff --git a/component/user.go b/component/user.go index 86cc31e9..85426990 100644 --- a/component/user.go +++ b/component/user.go @@ -296,7 +296,7 @@ func (c *userComponentImpl) AddLikes(ctx context.Context, req *types.UserLikesRe return newError } var likesRepoIDs []int64 - likesRepoIDs = append(likesRepoIDs, req.Repo_id) + likesRepoIDs = append(likesRepoIDs, req.RepoID) var opts []database.SelectOption opts = append(opts, database.Columns("id", "repository_type", "path", "user_id", "private")) @@ -314,7 +314,7 @@ func (c *userComponentImpl) AddLikes(ctx context.Context, req *types.UserLikesRe return fmt.Errorf("do not found likes repositories visiable to user:%s, %w", req.CurrentUser, err) } - err = c.userLikeStore.Add(ctx, user.ID, req.Repo_id) + err = c.userLikeStore.Add(ctx, user.ID, req.RepoID) return err } @@ -391,7 +391,7 @@ func (c *userComponentImpl) LikeCollection(ctx context.Context, req *types.UserL return newError } - collection, err := c.collectionStore.FindById(ctx, req.Collection_id) + collection, err := c.collectionStore.FindById(ctx, req.CollectionID) if err != nil { return fmt.Errorf("failed to get likes collection by id, error: %w", err) } @@ -400,7 +400,7 @@ func (c *userComponentImpl) LikeCollection(ctx context.Context, req *types.UserL return fmt.Errorf("no permission to like this collection for user:%s", req.CurrentUser) } - err = c.userLikeStore.LikeCollection(ctx, user.ID, req.Collection_id) + err = c.userLikeStore.LikeCollection(ctx, user.ID, req.CollectionID) return err } @@ -410,7 +410,7 @@ func (c *userComponentImpl) UnLikeCollection(ctx context.Context, req *types.Use newError := fmt.Errorf("failed to check for the presence of the user,error:%w", err) return newError } - err = c.userLikeStore.UnLikeCollection(ctx, user.ID, req.Collection_id) + err = c.userLikeStore.UnLikeCollection(ctx, user.ID, req.CollectionID) return err } @@ -420,7 +420,7 @@ func (c *userComponentImpl) DeleteLikes(ctx context.Context, req *types.UserLike newError := fmt.Errorf("failed to check for the presence of the user,error:%w", err) return newError } - err = c.userLikeStore.Delete(ctx, user.ID, req.Repo_id) + err = c.userLikeStore.Delete(ctx, user.ID, req.RepoID) return err } diff --git a/component/user_test.go b/component/user_test.go index efc2b3fb..0052ab62 100644 --- a/component/user_test.go +++ b/component/user_test.go @@ -136,10 +136,10 @@ func TestUserComponent_AddLikes(t *testing.T) { uc.mocks.stores.UserLikesMock().EXPECT().Add(ctx, int64(1), int64(123)).Return(nil) err := uc.AddLikes(ctx, &types.UserLikesRequest{ - Username: "user", - Repo_id: 123, - Collection_id: 456, - CurrentUser: "user", + Username: "user", + RepoID: 123, + CollectionID: 456, + CurrentUser: "user", }) require.Nil(t, err) } @@ -201,10 +201,10 @@ func TestUserComponent_LikeCollection(t *testing.T) { }, nil) uc.mocks.stores.UserLikesMock().EXPECT().LikeCollection(ctx, int64(1), int64(456)).Return(nil) err := uc.LikeCollection(ctx, &types.UserLikesRequest{ - Username: "user", - Repo_id: 123, - Collection_id: 456, - CurrentUser: "user", + Username: "user", + RepoID: 123, + CollectionID: 456, + CurrentUser: "user", }) require.Nil(t, err) } @@ -216,10 +216,10 @@ func TestUserComponent_UnLikeCollection(t *testing.T) { uc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ID: 1}, nil) uc.mocks.stores.UserLikesMock().EXPECT().UnLikeCollection(ctx, int64(1), int64(456)).Return(nil) err := uc.UnLikeCollection(ctx, &types.UserLikesRequest{ - Username: "user", - Repo_id: 123, - Collection_id: 456, - CurrentUser: "user", + Username: "user", + RepoID: 123, + CollectionID: 456, + CurrentUser: "user", }) require.Nil(t, err) } @@ -231,10 +231,10 @@ func TestUserComponent_DeleteLikes(t *testing.T) { uc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ID: 1}, nil) uc.mocks.stores.UserLikesMock().EXPECT().Delete(ctx, int64(1), int64(123)).Return(nil) err := uc.DeleteLikes(ctx, &types.UserLikesRequest{ - Username: "user", - Repo_id: 123, - Collection_id: 456, - CurrentUser: "user", + Username: "user", + RepoID: 123, + CollectionID: 456, + CurrentUser: "user", }) require.Nil(t, err) } From 3c6dd17e4a99e8777f18d7692ff977aaff5b2eb7 Mon Sep 17 00:00:00 2001 From: yiling Date: Tue, 24 Dec 2024 17:53:53 +0800 Subject: [PATCH 5/5] Add opencsg check back to git http handler --- api/handler/git_http.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/handler/git_http.go b/api/handler/git_http.go index a9bcb86b..2c22bf57 100644 --- a/api/handler/git_http.go +++ b/api/handler/git_http.go @@ -175,6 +175,11 @@ func (h *GitHTTPHandler) LfsBatch(ctx *gin.Context) { return } + s3Internal := ctx.GetHeader("X-OPENCSG-S3-Internal") + if s3Internal == "true" { + ctx.Set("X-OPENCSG-S3-Internal", true) + } + objectResponse, err := h.gitHttp.BuildObjectResponse(ctx, batchRequest, isUpload) if err != nil { if errors.Is(err, component.ErrUnauthorized) { @@ -235,6 +240,11 @@ func (h *GitHTTPHandler) LfsDownload(ctx *gin.Context) { downloadRequest.CurrentUser = httpbase.GetCurrentUser(ctx) downloadRequest.SaveAs = ctx.Query("save_as") + s3Internal := ctx.GetHeader("X-OPENCSG-S3-Internal") + if s3Internal == "true" { + ctx.Set("X-OPENCSG-S3-Internal", true) + } + url, err := h.gitHttp.LfsDownload(ctx, downloadRequest) if err != nil { httpbase.ServerError(ctx, err)