diff --git a/_mocks/opencsg.com/csghub-server/component/mock_TagComponent.go b/_mocks/opencsg.com/csghub-server/component/mock_TagComponent.go index af076cd7..05021f20 100644 --- a/_mocks/opencsg.com/csghub-server/component/mock_TagComponent.go +++ b/_mocks/opencsg.com/csghub-server/component/mock_TagComponent.go @@ -24,64 +24,6 @@ func (_m *MockTagComponent) EXPECT() *MockTagComponent_Expecter { return &MockTagComponent_Expecter{mock: &_m.Mock} } -// AllTags provides a mock function with given fields: ctx -func (_m *MockTagComponent) AllTags(ctx context.Context) ([]database.Tag, error) { - ret := _m.Called(ctx) - - if len(ret) == 0 { - panic("no return value specified for AllTags") - } - - var r0 []database.Tag - var r1 error - if rf, ok := ret.Get(0).(func(context.Context) ([]database.Tag, error)); ok { - return rf(ctx) - } - if rf, ok := ret.Get(0).(func(context.Context) []database.Tag); ok { - r0 = rf(ctx) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]database.Tag) - } - } - - if rf, ok := ret.Get(1).(func(context.Context) error); ok { - r1 = rf(ctx) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// MockTagComponent_AllTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AllTags' -type MockTagComponent_AllTags_Call struct { - *mock.Call -} - -// AllTags is a helper method to define mock.On call -// - ctx context.Context -func (_e *MockTagComponent_Expecter) AllTags(ctx interface{}) *MockTagComponent_AllTags_Call { - return &MockTagComponent_AllTags_Call{Call: _e.mock.On("AllTags", ctx)} -} - -func (_c *MockTagComponent_AllTags_Call) Run(run func(ctx context.Context)) *MockTagComponent_AllTags_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context)) - }) - return _c -} - -func (_c *MockTagComponent_AllTags_Call) Return(_a0 []database.Tag, _a1 error) *MockTagComponent_AllTags_Call { - _c.Call.Return(_a0, _a1) - return _c -} - -func (_c *MockTagComponent_AllTags_Call) RunAndReturn(run func(context.Context) ([]database.Tag, error)) *MockTagComponent_AllTags_Call { - _c.Call.Return(run) - return _c -} - // AllTagsByScopeAndCategory provides a mock function with given fields: ctx, scope, category func (_m *MockTagComponent) AllTagsByScopeAndCategory(ctx context.Context, scope string, category string) ([]*database.Tag, error) { ret := _m.Called(ctx, scope, category) diff --git a/common/tests/stores.go b/common/tests/stores.go index a6817517..2197113f 100644 --- a/common/tests/stores.go +++ b/common/tests/stores.go @@ -43,6 +43,7 @@ type MockStores struct { Org database.OrgStore MultiSync database.MultiSyncStore File database.FileStore + SSH database.SSHKeyStore } func NewMockStores(t interface { @@ -86,6 +87,7 @@ func NewMockStores(t interface { Org: mockdb.NewMockOrgStore(t), MultiSync: mockdb.NewMockMultiSyncStore(t), File: mockdb.NewMockFileStore(t), + SSH: mockdb.NewMockSSHKeyStore(t), } } @@ -232,3 +234,7 @@ func (s *MockStores) MultiSyncMock() *mockdb.MockMultiSyncStore { func (s *MockStores) FileMock() *mockdb.MockFileStore { return s.File.(*mockdb.MockFileStore) } + +func (s *MockStores) SSHMock() *mockdb.MockSSHKeyStore { + return s.SSH.(*mockdb.MockSSHKeyStore) +} diff --git a/component/internal.go b/component/internal.go index d23f28ec..07af29ca 100644 --- a/component/internal.go +++ b/component/internal.go @@ -8,6 +8,7 @@ import ( "strconv" pb "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb" + "opencsg.com/csghub-server/builder/git" "opencsg.com/csghub-server/builder/git/gitserver" "opencsg.com/csghub-server/builder/git/gitserver/gitaly" "opencsg.com/csghub-server/builder/store/database" @@ -17,10 +18,13 @@ import ( ) type internalComponentImpl struct { - config *config.Config - sshKeyStore database.SSHKeyStore - repoStore database.RepoStore - *repoComponentImpl + config *config.Config + sshKeyStore database.SSHKeyStore + repoStore database.RepoStore + tokenStore database.AccessTokenStore + namespaceStore database.NamespaceStore + repoComponent RepoComponent + gitServer gitserver.GitServer } type InternalComponent interface { @@ -37,11 +41,17 @@ func NewInternalComponent(config *config.Config) (InternalComponent, error) { c.config = config c.sshKeyStore = database.NewSSHKeyStore() c.repoStore = database.NewRepoStore() - c.repoComponentImpl, err = NewRepoComponentImpl(config) + c.repoComponent, err = NewRepoComponentImpl(config) c.tokenStore = database.NewAccessTokenStore() + c.namespaceStore = database.NewNamespaceStore() if err != nil { return nil, err } + git, err := git.NewGitServer(config) + if err != nil { + return nil, fmt.Errorf("failed to create git server: %w", err) + } + c.gitServer = git return c, nil } @@ -70,7 +80,7 @@ func (c *internalComponentImpl) SSHAllowed(ctx context.Context, req types.SSHAll return nil, fmt.Errorf("failed to find ssh key by id, err: %v", err) } if req.Action == "git-receive-pack" { - allowed, err := c.AllowWriteAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username) + allowed, err := c.repoComponent.AllowWriteAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username) if err != nil { return nil, ErrUnauthorized } @@ -79,7 +89,7 @@ func (c *internalComponentImpl) SSHAllowed(ctx context.Context, req types.SSHAll } } else if req.Action == "git-upload-pack" { if repo.Private { - allowed, err := c.AllowReadAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username) + allowed, err := c.repoComponent.AllowReadAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username) if err != nil { return nil, ErrUnauthorized } @@ -133,7 +143,7 @@ func (c *internalComponentImpl) GetCommitDiff(ctx context.Context, req types.Get if repo == nil { return nil, errors.New("repo not found") } - diffs, err := c.git.GetDiffBetweenTwoCommits(ctx, gitserver.GetDiffBetweenTwoCommitsReq{ + diffs, err := c.gitServer.GetDiffBetweenTwoCommits(ctx, gitserver.GetDiffBetweenTwoCommitsReq{ Namespace: req.Namespace, Name: req.Name, RepoType: req.RepoType, @@ -165,7 +175,7 @@ func (c *internalComponentImpl) LfsAuthenticate(ctx context.Context, req types.L return nil, fmt.Errorf("failed to find ssh key by id, err: %v", err) } if repo.Private { - allowed, err := c.AllowReadAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username) + allowed, err := c.repoComponent.AllowReadAccess(ctx, req.RepoType, req.Namespace, req.Name, sshKey.User.Username) if err != nil { return nil, ErrUnauthorized } diff --git a/component/internal_test.go b/component/internal_test.go new file mode 100644 index 00000000..4935fac9 --- /dev/null +++ b/component/internal_test.go @@ -0,0 +1,153 @@ +package component + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + pb "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb" + "opencsg.com/csghub-server/builder/git/gitserver" + "opencsg.com/csghub-server/builder/store/database" + "opencsg.com/csghub-server/common/types" +) + +func TestInternalComponent_Allowed(t *testing.T) { + ctx := context.TODO() + ic := initializeTestInternalComponent(ctx, t) + + allowed, err := ic.Allowed(ctx) + require.Nil(t, err) + require.True(t, allowed) +} + +func TestInternalComponent_SSHAllowed(t *testing.T) { + ctx := context.TODO() + ic := initializeTestInternalComponent(ctx, t) + + ic.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, "ns").Return(database.Namespace{ + ID: 321, + }, nil) + ic.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "ns", "n").Return( + &database.Repository{ID: 123, Private: true}, nil, + ) + ic.mocks.stores.SSHMock().EXPECT().FindByID(ctx, int64(1)).Return(&database.SSHKey{ + ID: 111, + User: &database.User{ID: 11, Username: "user"}, + }, nil) + ic.mocks.components.repo.EXPECT().AllowWriteAccess( + ctx, types.ModelRepo, "ns", "n", "user", + ).Return(true, nil) + ic.mocks.components.repo.EXPECT().AllowReadAccess( + ctx, types.ModelRepo, "ns", "n", "user", + ).Return(true, nil) + + req := types.SSHAllowedReq{ + RepoType: types.ModelRepo, + Namespace: "ns", + Name: "n", + KeyID: "1", + Action: "git-receive-pack", + } + resp, err := ic.SSHAllowed(ctx, req) + require.Nil(t, err) + expected := &types.SSHAllowedResp{ + Success: true, + Message: "allowed", + Repo: req.Repo, + UserID: "11", + KeyType: "ssh", + KeyID: 111, + ProjectID: 123, + RootNamespaceID: 321, + GitConfigOptions: []string{"uploadpack.allowFilter=true", "uploadpack.allowAnySHA1InWant=true"}, + Gitaly: types.Gitaly{ + Repo: pb.Repository{ + RelativePath: "models_ns/n.git", + GlRepository: "models/ns/n", + }, + }, + StatusCode: 200, + } + + require.Equal(t, expected, resp) + + req.Action = "git-upload-pack" + resp, err = ic.SSHAllowed(ctx, req) + require.Nil(t, err) + require.Equal(t, expected, resp) + +} + +func TestInternalComponent_GetAuthorizedKeys(t *testing.T) { + ctx := context.TODO() + ic := initializeTestInternalComponent(ctx, t) + + ic.mocks.stores.SSHMock().EXPECT().FindByFingerpringSHA256( + ctx, "dUQ5GwtKsCPC8Scv1OLnOEvIW0QWULVSWyj5bZwQHwM", + ).Return(&database.SSHKey{}, nil) + key, err := ic.GetAuthorizedKeys(ctx, "foobar") + require.Nil(t, err) + require.Equal(t, &database.SSHKey{}, key) +} + +func TestInternalComponent_GetCommitDiff(t *testing.T) { + ctx := context.TODO() + ic := initializeTestInternalComponent(ctx, t) + + req := types.GetDiffBetweenTwoCommitsReq{ + Namespace: "ns", + Name: "n", + RepoType: types.ModelRepo, + Ref: "main", + LeftCommitId: "l", + RightCommitId: "r", + } + ic.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "ns", "n").Return( + &database.Repository{}, nil, + ) + ic.mocks.gitServer.EXPECT().GetDiffBetweenTwoCommits(ctx, gitserver.GetDiffBetweenTwoCommitsReq{ + Namespace: req.Namespace, + Name: req.Name, + RepoType: req.RepoType, + Ref: req.Ref, + LeftCommitId: req.LeftCommitId, + RightCommitId: req.RightCommitId, + }).Return(&types.GiteaCallbackPushReq{Ref: "main"}, nil) + + resp, err := ic.GetCommitDiff(ctx, req) + require.Nil(t, err) + require.Equal(t, &types.GiteaCallbackPushReq{Ref: "main"}, resp) +} + +func TestInternalComponent_LfsAuthenticate(t *testing.T) { + ctx := context.TODO() + ic := initializeTestInternalComponent(ctx, t) + + ic.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.ModelRepo, "ns", "n").Return( + &database.Repository{Private: true}, nil, + ) + ic.mocks.stores.SSHMock().EXPECT().FindByID(ctx, int64(1)).Return(&database.SSHKey{ + ID: 111, + User: &database.User{ID: 11, Username: "user"}, + }, nil) + ic.mocks.components.repo.EXPECT().AllowReadAccess( + ctx, types.ModelRepo, "ns", "n", "user", + ).Return(true, nil) + ic.mocks.stores.AccessTokenMock().EXPECT().GetUserGitToken(ctx, "user").Return( + &database.AccessToken{Token: "token"}, nil, + ) + + resp, err := ic.LfsAuthenticate(ctx, types.LfsAuthenticateReq{ + Namespace: "ns", + Name: "n", + RepoType: types.ModelRepo, + KeyID: "1", + }) + require.Nil(t, err) + require.Equal(t, &types.LfsAuthenticateResp{ + Username: "user", + LfsToken: "token", + RepoPath: "/models/ns/n.git", + }, resp) + +} diff --git a/component/mirror_source.go b/component/mirror_source.go index 136c16a0..b136e29d 100644 --- a/component/mirror_source.go +++ b/component/mirror_source.go @@ -11,8 +11,8 @@ import ( ) type mirrorSourceComponentImpl struct { - msStore database.MirrorSourceStore - userStore database.UserStore + mirrorSourceStore database.MirrorSourceStore + userStore database.UserStore } type MirrorSourceComponent interface { @@ -25,8 +25,8 @@ type MirrorSourceComponent interface { func NewMirrorSourceComponent(config *config.Config) (MirrorSourceComponent, error) { return &mirrorSourceComponentImpl{ - msStore: database.NewMirrorSourceStore(), - userStore: database.NewUserStore(), + mirrorSourceStore: database.NewMirrorSourceStore(), + userStore: database.NewUserStore(), }, nil } @@ -41,7 +41,7 @@ func (c *mirrorSourceComponentImpl) Create(ctx context.Context, req types.Create } ms.SourceName = req.SourceName ms.InfoAPIUrl = req.InfoAPiUrl - res, err := c.msStore.Create(ctx, &ms) + res, err := c.mirrorSourceStore.Create(ctx, &ms) if err != nil { return nil, fmt.Errorf("failed to create mirror source, error: %w", err) } @@ -56,7 +56,7 @@ func (c *mirrorSourceComponentImpl) Get(ctx context.Context, id int64, currentUs if !user.CanAdmin() { return nil, errors.New("user does not have admin permission") } - ms, err := c.msStore.Get(ctx, id) + ms, err := c.mirrorSourceStore.Get(ctx, id) if err != nil { return nil, fmt.Errorf("failed to get mirror source, error: %w", err) } @@ -71,7 +71,7 @@ func (c *mirrorSourceComponentImpl) Index(ctx context.Context, currentUser strin if !user.CanAdmin() { return nil, errors.New("user does not have admin permission") } - ms, err := c.msStore.Index(ctx) + ms, err := c.mirrorSourceStore.Index(ctx) if err != nil { return nil, fmt.Errorf("failed to get mirror source, error: %w", err) } @@ -89,7 +89,7 @@ func (c *mirrorSourceComponentImpl) Update(ctx context.Context, req types.Update ms.ID = req.ID ms.SourceName = req.SourceName ms.InfoAPIUrl = req.InfoAPiUrl - err = c.msStore.Update(ctx, &ms) + err = c.mirrorSourceStore.Update(ctx, &ms) if err != nil { return nil, fmt.Errorf("failed to update mirror source, error: %w", err) } @@ -104,11 +104,11 @@ func (c *mirrorSourceComponentImpl) Delete(ctx context.Context, id int64, curren if !user.CanAdmin() { return errors.New("user does not have admin permission") } - ms, err := c.msStore.Get(ctx, id) + ms, err := c.mirrorSourceStore.Get(ctx, id) if err != nil { return fmt.Errorf("failed to find mirror source, error: %w", err) } - err = c.msStore.Delete(ctx, ms) + err = c.mirrorSourceStore.Delete(ctx, ms) if err != nil { return fmt.Errorf("failed to delete mirror source, error: %w", err) } diff --git a/component/mirror_source_test.go b/component/mirror_source_test.go new file mode 100644 index 00000000..ccc3ec02 --- /dev/null +++ b/component/mirror_source_test.go @@ -0,0 +1,104 @@ +package component + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "opencsg.com/csghub-server/builder/store/database" + "opencsg.com/csghub-server/common/types" +) + +func TestMirrorSourceComponent_Create(t *testing.T) { + ctx := context.TODO() + mc := initializeTestMirrorSourceComponent(ctx, t) + + mc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ + RoleMask: "admin", + }, nil) + mc.mocks.stores.MirrorSourceMock().EXPECT().Create(ctx, &database.MirrorSource{ + SourceName: "sn", + InfoAPIUrl: "url", + }).Return(&database.MirrorSource{ID: 1}, nil) + + data, err := mc.Create(ctx, types.CreateMirrorSourceReq{ + SourceName: "sn", + InfoAPiUrl: "url", + CurrentUser: "user", + }) + require.Nil(t, err) + require.Equal(t, &database.MirrorSource{ID: 1}, data) +} + +func TestMirrorSourceComponent_Get(t *testing.T) { + ctx := context.TODO() + mc := initializeTestMirrorSourceComponent(ctx, t) + + mc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ + RoleMask: "admin", + }, nil) + mc.mocks.stores.MirrorSourceMock().EXPECT().Get(ctx, int64(1)).Return(&database.MirrorSource{ID: 1}, nil) + + data, err := mc.Get(ctx, 1, "user") + require.Nil(t, err) + require.Equal(t, &database.MirrorSource{ID: 1}, data) +} + +func TestMirrorSourceComponent_Index(t *testing.T) { + ctx := context.TODO() + mc := initializeTestMirrorSourceComponent(ctx, t) + + mc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ + RoleMask: "admin", + }, nil) + mc.mocks.stores.MirrorSourceMock().EXPECT().Index(ctx).Return([]database.MirrorSource{ + {ID: 1}, + }, nil) + + data, err := mc.Index(ctx, "user") + require.Nil(t, err) + require.Equal(t, []database.MirrorSource{ + {ID: 1}, + }, data) +} + +func TestMirrorSourceComponent_Update(t *testing.T) { + ctx := context.TODO() + mc := initializeTestMirrorSourceComponent(ctx, t) + + mc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ + RoleMask: "admin", + }, nil) + mc.mocks.stores.MirrorSourceMock().EXPECT().Update(ctx, &database.MirrorSource{ + ID: 1, + SourceName: "sn", + InfoAPIUrl: "url", + }).Return(nil) + + data, err := mc.Update(ctx, types.UpdateMirrorSourceReq{ + ID: 1, + SourceName: "sn", + InfoAPiUrl: "url", + CurrentUser: "user", + }) + require.Nil(t, err) + require.Equal(t, &database.MirrorSource{ + ID: 1, + SourceName: "sn", + InfoAPIUrl: "url", + }, data) +} + +func TestMirrorSourceComponent_Delete(t *testing.T) { + ctx := context.TODO() + mc := initializeTestMirrorSourceComponent(ctx, t) + + mc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ + RoleMask: "admin", + }, nil) + mc.mocks.stores.MirrorSourceMock().EXPECT().Get(ctx, int64(1)).Return(&database.MirrorSource{ID: 1}, nil) + mc.mocks.stores.MirrorSourceMock().EXPECT().Delete(ctx, &database.MirrorSource{ID: 1}).Return(nil) + + err := mc.Delete(ctx, 1, "user") + require.Nil(t, err) +} diff --git a/component/model_test.go b/component/model_test.go index ea557131..3f49f915 100644 --- a/component/model_test.go +++ b/component/model_test.go @@ -9,7 +9,6 @@ import ( "github.com/stretchr/testify/require" "opencsg.com/csghub-server/builder/git/gitserver" "opencsg.com/csghub-server/builder/git/membership" - "opencsg.com/csghub-server/builder/inference" "opencsg.com/csghub-server/builder/store/database" "opencsg.com/csghub-server/common/types" ) @@ -458,31 +457,22 @@ func TestModelComponent_DeleteRelationDataset(t *testing.T) { require.Nil(t, err) } -func TestModelComponent_Predict(t *testing.T) { - ctx := context.TODO() - mc := initializeTestModelComponent(ctx, t) - - mc.mocks.inferenceClient.EXPECT().Predict(inference.ModelID{ - Owner: "ns", - Name: "n", - }, &inference.PredictRequest{ - Prompt: "foo", - }).Return(&inference.PredictResponse{ - GeneratedText: "abcd", - }, nil) +// func TestModelComponent_Predict(t *testing.T) { +// ctx := context.TODO() +// mc := initializeTestModelComponent(ctx, t) - resp, err := mc.Predict(ctx, &types.ModelPredictReq{ - Namespace: "ns", - Name: "n", - Input: "foo", - CurrentUser: "user", - }) - require.Nil(t, err) - require.Equal(t, &types.ModelPredictResp{ - Content: "abcd", - }, resp) +// resp, err := mc.Predict(ctx, &types.ModelPredictReq{ +// Namespace: "ns", +// Name: "n", +// Input: "foo", +// CurrentUser: "user", +// }) +// require.Nil(t, err) +// require.Equal(t, &types.ModelPredictResp{ +// Content: "abcd", +// }, resp) -} +// } // func TestModelComponent_Deploy(t *testing.T) { // ctx := context.TODO() diff --git a/component/recom.go b/component/recom.go index c3661891..0e3183d8 100644 --- a/component/recom.go +++ b/component/recom.go @@ -14,9 +14,9 @@ import ( ) type recomComponentImpl struct { - rs database.RecomStore - repos database.RepoStore - gs gitserver.GitServer + recomStore database.RecomStore + repoStore database.RepoStore + gitServer gitserver.GitServer } type RecomComponent interface { @@ -33,18 +33,18 @@ func NewRecomComponent(cfg *config.Config) (RecomComponent, error) { } return &recomComponentImpl{ - rs: database.NewRecomStore(), - repos: database.NewRepoStore(), - gs: gs, + recomStore: database.NewRecomStore(), + repoStore: database.NewRepoStore(), + gitServer: gs, }, nil } func (rc *recomComponentImpl) SetOpWeight(ctx context.Context, repoID, weight int64) error { - _, err := rc.repos.FindById(ctx, repoID) + _, err := rc.repoStore.FindById(ctx, repoID) if err != nil { return fmt.Errorf("failed to find repository with id %d, err:%w", repoID, err) } - return rc.rs.UpsetOpWeights(ctx, repoID, weight) + return rc.recomStore.UpsetOpWeights(ctx, repoID, weight) } // loop through repositories and calculate the recom score of the repository @@ -54,7 +54,7 @@ func (rc *recomComponentImpl) CalculateRecomScore(ctx context.Context) { slog.Error("Error loading weights", "error", err) return } - repos, err := rc.repos.All(ctx) + repos, err := rc.repoStore.All(ctx) if err != nil { slog.Error("Error fetching repositories", "error", err) return @@ -62,7 +62,7 @@ func (rc *recomComponentImpl) CalculateRecomScore(ctx context.Context) { for _, repo := range repos { repoID := repo.ID score := rc.CalcTotalScore(ctx, repo, weights) - err := rc.rs.UpsertScore(ctx, repoID, score) + err := rc.recomStore.UpsertScore(ctx, repoID, score) if err != nil { slog.Error("Error updating recom score", slog.Int64("repo_id", repoID), slog.Float64("score", score), slog.String("error", err.Error())) @@ -132,7 +132,7 @@ func (rc *recomComponentImpl) calcQualityScore(ctx context.Context, repo *databa score := 0.0 // get file counts from git server namespace, name := repo.NamespaceAndName() - files, err := getFilePaths(namespace, name, "", repo.RepositoryType, "", rc.gs.GetRepoFileTree) + files, err := getFilePaths(namespace, name, "", repo.RepositoryType, "", rc.gitServer.GetRepoFileTree) if err != nil { return 0, fmt.Errorf("failed to get repo file tree,%w", err) } @@ -157,7 +157,7 @@ func (rc *recomComponentImpl) calcQualityScore(ctx context.Context, repo *databa func (rc *recomComponentImpl) loadWeights() (map[string]string, error) { ctx := context.Background() - items, err := rc.rs.LoadWeights(ctx) + items, err := rc.recomStore.LoadWeights(ctx) if err != nil { return nil, err } diff --git a/component/recom_test.go b/component/recom_test.go index 139e9eac..bba569e3 100644 --- a/component/recom_test.go +++ b/component/recom_test.go @@ -6,25 +6,48 @@ import ( "time" "github.com/stretchr/testify/mock" - gsmock "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/git/gitserver" "opencsg.com/csghub-server/builder/git/gitserver" "opencsg.com/csghub-server/builder/store/database" - "opencsg.com/csghub-server/common/tests" ) -func NewTestRecomComponent(stores *tests.MockStores, gitServer gitserver.GitServer) *recomComponentImpl { - return &recomComponentImpl{ - repos: stores.Repo, - gs: gitServer, - } +// func TestRecomComponent_SetOpWeight(t *testing.T) { +// ctx := context.TODO() +// rc := initializeTestRecomComponent(ctx, t) + +// rc.mocks.stores.RepoMock().EXPECT().FindById(ctx, int64(1)).Return(&database.Repository{}, nil) +// rc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ +// RoleMask: "admin", +// }, nil) +// rc.mocks.stores.RecomMock().EXPECT().UpsetOpWeights(ctx, int64(1), int64(100)).Return(nil) + +// err := rc.SetOpWeight(ctx, 1, 100) +// require.Nil(t, err) +// } + +func TestRecomComponent_CalculateRecomScore(t *testing.T) { + ctx := context.TODO() + rc := initializeTestRecomComponent(ctx, t) + + rc.mocks.stores.RecomMock().EXPECT().LoadWeights(mock.Anything).Return( + []*database.RecomWeight{{Name: "freshness", WeightExp: "score = 12.34"}}, nil, + ) + rc.mocks.stores.RepoMock().EXPECT().All(ctx).Return([]*database.Repository{ + {ID: 1, Path: "foo/bar"}, + }, nil) + rc.mocks.stores.RecomMock().EXPECT().UpsertScore(ctx, int64(1), 12.34).Return(nil) + rc.mocks.gitServer.EXPECT().GetRepoFileTree(mock.Anything, gitserver.GetRepoInfoByPathReq{ + Namespace: "foo", + Name: "bar", + }).Return(nil, nil) + + rc.CalculateRecomScore(ctx) } func TestRecomComponent_CalculateTotalScore(t *testing.T) { - gitServer := gsmock.NewMockGitServer(t) - rc := &recomComponentImpl{gs: gitServer} ctx := context.TODO() + rc := initializeTestRecomComponent(ctx, t) - gitServer.EXPECT().GetRepoFileTree(mock.Anything, gitserver.GetRepoInfoByPathReq{ + rc.mocks.gitServer.EXPECT().GetRepoFileTree(mock.Anything, gitserver.GetRepoInfoByPathReq{ Namespace: "foo", Name: "bar", }).Return(nil, nil) diff --git a/component/space_resource_test.go b/component/space_resource_test.go new file mode 100644 index 00000000..024cc1e9 --- /dev/null +++ b/component/space_resource_test.go @@ -0,0 +1,94 @@ +package component + +// func TestSpaceResourceComponent_Index(t *testing.T) { +// ctx := context.TODO() +// sc := initializeTestSpaceResourceComponent(ctx, t) + +// sc.mocks.deployer.EXPECT().ListCluster(ctx).Return([]types.ClusterRes{ +// {ClusterID: "c1"}, +// }, nil) +// sc.mocks.stores.SpaceResourceMock().EXPECT().Index(ctx, "c1").Return( +// []database.SpaceResource{ +// {ID: 1, Name: "sr", Resources: `{"memory": "1000"}`}, +// }, nil, +// ) +// sc.mocks.deployer.EXPECT().GetClusterById(ctx, "c1").Return(&types.ClusterRes{}, nil) +// sc.mocks.stores.UserMock().EXPECT().FindByUsername(ctx, "user").Return(database.User{ +// UUID: "uid", +// }, nil) + +// data, err := sc.Index(ctx, "", 1) +// require.Nil(t, err) +// require.Equal(t, []types.SpaceResource{ +// { +// ID: 1, Name: "sr", Resources: "{\"memory\": \"1000\"}", +// IsAvailable: false, Type: "cpu", +// }, +// { +// ID: 0, Name: "", Resources: "{\"memory\": \"2000\"}", IsAvailable: true, +// Type: "cpu", +// }, +// }, data) + +// } + +// func TestSpaceResourceComponent_Update(t *testing.T) { +// ctx := context.TODO() +// sc := initializeTestSpaceResourceComponent(ctx, t) + +// sc.mocks.stores.SpaceResourceMock().EXPECT().FindByID(ctx, int64(1)).Return( +// &database.SpaceResource{}, nil, +// ) +// sc.mocks.stores.SpaceResourceMock().EXPECT().Update(ctx, database.SpaceResource{ +// Name: "n", +// Resources: "r", +// }).Return(&database.SpaceResource{ID: 1, Name: "n", Resources: "r"}, nil) + +// data, err := sc.Update(ctx, &types.UpdateSpaceResourceReq{ +// ID: 1, +// Name: "n", +// Resources: "r", +// }) +// require.Nil(t, err) +// require.Equal(t, &types.SpaceResource{ +// ID: 1, +// Name: "n", +// Resources: "r", +// }, data) +// } + +// func TestSpaceResourceComponent_Create(t *testing.T) { +// ctx := context.TODO() +// sc := initializeTestSpaceResourceComponent(ctx, t) + +// sc.mocks.stores.SpaceResourceMock().EXPECT().Create(ctx, database.SpaceResource{ +// Name: "n", +// Resources: "r", +// ClusterID: "c", +// }).Return(&database.SpaceResource{ID: 1, Name: "n", Resources: "r"}, nil) + +// data, err := sc.Create(ctx, &types.CreateSpaceResourceReq{ +// Name: "n", +// Resources: "r", +// ClusterID: "c", +// }) +// require.Nil(t, err) +// require.Equal(t, &types.SpaceResource{ +// ID: 1, +// Name: "n", +// Resources: "r", +// }, data) +// } + +// func TestSpaceResourceComponent_Delete(t *testing.T) { +// ctx := context.TODO() +// sc := initializeTestSpaceResourceComponent(ctx, t) + +// sc.mocks.stores.SpaceResourceMock().EXPECT().FindByID(ctx, int64(1)).Return( +// &database.SpaceResource{}, nil, +// ) +// sc.mocks.stores.SpaceResourceMock().EXPECT().Delete(ctx, database.SpaceResource{}).Return(nil) + +// err := sc.Delete(ctx, 1) +// require.Nil(t, err) +// } diff --git a/component/space_sdk.go b/component/space_sdk.go index 1c0eca17..497dfede 100644 --- a/component/space_sdk.go +++ b/component/space_sdk.go @@ -18,18 +18,18 @@ type SpaceSdkComponent interface { func NewSpaceSdkComponent(config *config.Config) (SpaceSdkComponent, error) { c := &spaceSdkComponentImpl{} - c.sss = database.NewSpaceSdkStore() + c.spaceSdkStore = database.NewSpaceSdkStore() return c, nil } type spaceSdkComponentImpl struct { - sss database.SpaceSdkStore + spaceSdkStore database.SpaceSdkStore } func (c *spaceSdkComponentImpl) Index(ctx context.Context) ([]types.SpaceSdk, error) { var result []types.SpaceSdk - databaseSpaceSdks, err := c.sss.Index(ctx) + databaseSpaceSdks, err := c.spaceSdkStore.Index(ctx) if err != nil { return nil, err } @@ -45,7 +45,7 @@ func (c *spaceSdkComponentImpl) Index(ctx context.Context) ([]types.SpaceSdk, er } func (c *spaceSdkComponentImpl) Update(ctx context.Context, req *types.UpdateSpaceSdkReq) (*types.SpaceSdk, error) { - ss, err := c.sss.FindByID(ctx, req.ID) + ss, err := c.spaceSdkStore.FindByID(ctx, req.ID) if err != nil { slog.Error("error getting space sdk", slog.Any("error", err)) return nil, err @@ -53,7 +53,7 @@ func (c *spaceSdkComponentImpl) Update(ctx context.Context, req *types.UpdateSpa ss.Name = req.Name ss.Version = req.Version - ss, err = c.sss.Update(ctx, *ss) + ss, err = c.spaceSdkStore.Update(ctx, *ss) if err != nil { slog.Error("error getting space sdk", slog.Any("error", err)) return nil, err @@ -73,7 +73,7 @@ func (c *spaceSdkComponentImpl) Create(ctx context.Context, req *types.CreateSpa Name: req.Name, Version: req.Version, } - res, err := c.sss.Create(ctx, ss) + res, err := c.spaceSdkStore.Create(ctx, ss) if err != nil { slog.Error("error creating space sdk", slog.Any("error", err)) return nil, err @@ -89,13 +89,13 @@ func (c *spaceSdkComponentImpl) Create(ctx context.Context, req *types.CreateSpa } func (c *spaceSdkComponentImpl) Delete(ctx context.Context, id int64) error { - ss, err := c.sss.FindByID(ctx, id) + ss, err := c.spaceSdkStore.FindByID(ctx, id) if err != nil { slog.Error("error finding space sdk", slog.Any("error", err)) return err } - err = c.sss.Delete(ctx, *ss) + err = c.spaceSdkStore.Delete(ctx, *ss) if err != nil { slog.Error("error deleting space sdk", slog.Any("error", err)) return err diff --git a/component/space_sdk_test.go b/component/space_sdk_test.go new file mode 100644 index 00000000..641f00d4 --- /dev/null +++ b/component/space_sdk_test.go @@ -0,0 +1,64 @@ +package component + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "opencsg.com/csghub-server/builder/store/database" + "opencsg.com/csghub-server/common/types" +) + +func TestSpaceSdkComponent_Index(t *testing.T) { + ctx := context.TODO() + sc := initializeTestSpaceSdkComponent(ctx, t) + + sc.mocks.stores.SpaceSdkMock().EXPECT().Index(ctx).Return([]database.SpaceSdk{ + {ID: 1, Name: "s", Version: "1"}, + }, nil) + + data, err := sc.Index(ctx) + require.Nil(t, err) + require.Equal(t, []types.SpaceSdk{{ID: 1, Name: "s", Version: "1"}}, data) +} + +func TestSpaceSdkComponent_Update(t *testing.T) { + ctx := context.TODO() + sc := initializeTestSpaceSdkComponent(ctx, t) + + s := &database.SpaceSdk{ID: 1} + sc.mocks.stores.SpaceSdkMock().EXPECT().FindByID(ctx, int64(1)).Return(s, nil) + s2 := *s + s2.Name = "n" + s2.Version = "v1" + sc.mocks.stores.SpaceSdkMock().EXPECT().Update(ctx, s2).Return(s, nil) + + data, err := sc.Update(ctx, &types.UpdateSpaceSdkReq{ID: 1, Name: "n", Version: "v1"}) + require.Nil(t, err) + require.Equal(t, &types.SpaceSdk{ID: 1, Name: "n", Version: "v1"}, data) +} + +func TestSpaceSdkComponent_Create(t *testing.T) { + ctx := context.TODO() + sc := initializeTestSpaceSdkComponent(ctx, t) + + s := database.SpaceSdk{Name: "n", Version: "v1"} + sc.mocks.stores.SpaceSdkMock().EXPECT().Create(ctx, s).Return(&s, nil) + s.ID = 1 + + data, err := sc.Create(ctx, &types.CreateSpaceSdkReq{Name: "n", Version: "v1"}) + require.Nil(t, err) + require.Equal(t, &types.SpaceSdk{ID: 1, Name: "n", Version: "v1"}, data) +} + +func TestSpaceSdkComponent_Delete(t *testing.T) { + ctx := context.TODO() + sc := initializeTestSpaceSdkComponent(ctx, t) + + s := &database.SpaceSdk{} + sc.mocks.stores.SpaceSdkMock().EXPECT().FindByID(ctx, int64(1)).Return(s, nil) + sc.mocks.stores.SpaceSdkMock().EXPECT().Delete(ctx, *s).Return(nil) + + err := sc.Delete(ctx, int64(1)) + require.Nil(t, err) +} diff --git a/component/tag.go b/component/tag.go index d428b895..2c95e4f3 100644 --- a/component/tag.go +++ b/component/tag.go @@ -16,7 +16,6 @@ import ( type TagComponent interface { AllTagsByScopeAndCategory(ctx context.Context, scope string, category string) ([]*database.Tag, error) - AllTags(ctx context.Context) ([]database.Tag, error) ClearMetaTags(ctx context.Context, repoType types.RepositoryType, namespace, name string) error UpdateMetaTags(ctx context.Context, tagScope database.TagScope, namespace, name, content string) ([]*database.RepositoryTag, error) UpdateLibraryTags(ctx context.Context, tagScope database.TagScope, namespace, name, oldFilePath, newFilePath string) error @@ -25,8 +24,8 @@ type TagComponent interface { func NewTagComponent(config *config.Config) (TagComponent, error) { tc := &tagComponentImpl{} - tc.ts = database.NewTagStore() - tc.rs = database.NewRepoStore() + tc.tagStore = database.NewTagStore() + tc.repoStore = database.NewRepoStore() if config.SensitiveCheck.Enable { tc.sensitiveChecker = rpc.NewModerationSvcHttpClient(fmt.Sprintf("%s:%d", config.Moderation.Host, config.Moderation.Port)) } @@ -34,21 +33,18 @@ func NewTagComponent(config *config.Config) (TagComponent, error) { } type tagComponentImpl struct { - ts database.TagStore - rs database.RepoStore + tagStore database.TagStore + repoStore database.RepoStore sensitiveChecker rpc.ModerationSvcClient } -func (c *tagComponentImpl) AllTags(ctx context.Context) ([]database.Tag, error) { - // TODO: query cache for tags at first - return c.ts.AllTags(ctx) -} -func (c *tagComponentImpl) AllTagsByScopeAndCategory(ctx context.Context, scope string, category string) ([]*database.Tag, error) { - return c.ts.AllTagsByScopeAndCategory(ctx, database.TagScope(scope), category) +func (tc *tagComponentImpl) AllTagsByScopeAndCategory(ctx context.Context, scope string, category string) ([]*database.Tag, error) { + return tc.tagStore.AllTagsByScopeAndCategory(ctx, database.TagScope(scope), category) } func (c *tagComponentImpl) ClearMetaTags(ctx context.Context, repoType types.RepositoryType, namespace, name string) error { - _, err := c.ts.SetMetaTags(ctx, repoType, namespace, name, nil) + + _, err := c.tagStore.SetMetaTags(ctx, repoType, namespace, name, nil) return err } @@ -60,13 +56,13 @@ func (c *tagComponentImpl) UpdateMetaTags(ctx context.Context, tagScope database // TODO:load from cache if tagScope == database.DatasetTagScope { - tp = tagparser.NewDatasetTagProcessor(c.ts) + tp = tagparser.NewDatasetTagProcessor(c.tagStore) repoType = types.DatasetRepo } else if tagScope == database.ModelTagScope { - tp = tagparser.NewModelTagProcessor(c.ts) + tp = tagparser.NewModelTagProcessor(c.tagStore) repoType = types.ModelRepo } else if tagScope == database.PromptTagScope { - tp = tagparser.NewPromptTagProcessor(c.ts) + tp = tagparser.NewPromptTagProcessor(c.tagStore) repoType = types.PromptRepo } else { // skip tag process for code and space now @@ -91,13 +87,13 @@ func (c *tagComponentImpl) UpdateMetaTags(ctx context.Context, tagScope database }) } - err = c.ts.SaveTags(ctx, tagToCreate) + err = c.tagStore.SaveTags(ctx, tagToCreate) if err != nil { slog.Error("Failed to save tags", slog.Any("error", err)) return nil, fmt.Errorf("failed to save tags, cause: %w", err) } - repo, err := c.rs.FindByPath(ctx, repoType, namespace, name) + repo, err := c.repoStore.FindByPath(ctx, repoType, namespace, name) if err != nil { slog.Error("failed to find repo", slog.Any("error", err)) return nil, fmt.Errorf("failed to find repo, cause: %w", err) @@ -105,14 +101,14 @@ func (c *tagComponentImpl) UpdateMetaTags(ctx context.Context, tagScope database metaTags := append(tagsMatched, tagToCreate...) var repoTags []*database.RepositoryTag - repoTags, err = c.ts.SetMetaTags(ctx, repoType, namespace, name, metaTags) + repoTags, err = c.tagStore.SetMetaTags(ctx, repoType, namespace, name, metaTags) if err != nil { slog.Error("failed to set dataset's tags", slog.String("namespace", namespace), slog.String("name", name), slog.Any("error", err)) return nil, fmt.Errorf("failed to set dataset's tags, cause: %w", err) } - err = c.rs.UpdateLicenseByTag(ctx, repo.ID) + err = c.repoStore.UpdateLicenseByTag(ctx, repo.ID) if err != nil { slog.Error("failed to update repo license tags", slog.Any("error", err)) } @@ -130,13 +126,13 @@ func (c *tagComponentImpl) UpdateLibraryTags(ctx context.Context, tagScope datab repoType types.RepositoryType ) if tagScope == database.DatasetTagScope { - allTags, err = c.ts.AllDatasetTags(ctx) + allTags, err = c.tagStore.AllDatasetTags(ctx) repoType = types.DatasetRepo } else if tagScope == database.ModelTagScope { - allTags, err = c.ts.AllModelTags(ctx) + allTags, err = c.tagStore.AllModelTags(ctx) repoType = types.ModelRepo } else if tagScope == database.PromptTagScope { - allTags, err = c.ts.AllPromptTags(ctx) + allTags, err = c.tagStore.AllPromptTags(ctx) repoType = types.PromptRepo } else { return nil @@ -156,7 +152,7 @@ func (c *tagComponentImpl) UpdateLibraryTags(ctx context.Context, tagScope datab oldLibTag = t } } - err = c.ts.SetLibraryTag(ctx, repoType, namespace, name, newLibTag, oldLibTag) + err = c.tagStore.SetLibraryTag(ctx, repoType, namespace, name, newLibTag, oldLibTag) if err != nil { slog.Error("failed to set %s's tags", string(repoType), slog.String("namespace", namespace), slog.String("name", name), slog.Any("error", err)) @@ -166,7 +162,7 @@ func (c *tagComponentImpl) UpdateLibraryTags(ctx context.Context, tagScope datab } func (c *tagComponentImpl) UpdateRepoTagsByCategory(ctx context.Context, tagScope database.TagScope, repoID int64, category string, tagNames []string) error { - allTags, err := c.ts.AllTagsByScopeAndCategory(ctx, tagScope, category) + allTags, err := c.tagStore.AllTagsByScopeAndCategory(ctx, tagScope, category) if err != nil { return fmt.Errorf("failed to get all tags of scope `%s`, error: %w", tagScope, err) } @@ -185,9 +181,9 @@ func (c *tagComponentImpl) UpdateRepoTagsByCategory(ctx context.Context, tagScop } var oldTagIDs []int64 - oldTagIDs, err = c.rs.TagIDs(ctx, repoID, category) + oldTagIDs, err = c.repoStore.TagIDs(ctx, repoID, category) if err != nil { return fmt.Errorf("failed to get old tag ids, error: %w", err) } - return c.ts.UpsertRepoTags(ctx, repoID, oldTagIDs, tagIDs) + return c.tagStore.UpsertRepoTags(ctx, repoID, oldTagIDs, tagIDs) } diff --git a/component/tag_test.go b/component/tag_test.go new file mode 100644 index 00000000..2f53aa30 --- /dev/null +++ b/component/tag_test.go @@ -0,0 +1,90 @@ +package component + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "opencsg.com/csghub-server/builder/store/database" + "opencsg.com/csghub-server/common/types" +) + +func TestTagComponent_AllTagsByScopeAndCategory(t *testing.T) { + ctx := context.TODO() + tc := initializeTestTagComponent(ctx, t) + + tc.mocks.stores.TagMock().EXPECT().AllTagsByScopeAndCategory(ctx, database.CodeTagScope, "cat").Return( + []*database.Tag{{Name: "t"}}, nil, + ) + + data, err := tc.AllTagsByScopeAndCategory(ctx, "code", "cat") + require.Nil(t, err) + require.Equal(t, []*database.Tag{{Name: "t"}}, data) +} + +func TestTagComponent_ClearMetaTags(t *testing.T) { + ctx := context.TODO() + tc := initializeTestTagComponent(ctx, t) + + tc.mocks.stores.TagMock().EXPECT().SetMetaTags( + ctx, types.ModelRepo, "ns", "n", []*database.Tag(nil), + ).Return(nil, nil) + + err := tc.ClearMetaTags(ctx, types.ModelRepo, "ns", "n") + require.Nil(t, err) +} + +func TestTagComponent_UpdateMetaTags(t *testing.T) { + ctx := context.TODO() + tc := initializeTestTagComponent(ctx, t) + + tc.mocks.stores.TagMock().EXPECT().AllDatasetTags(ctx).Return([]*database.Tag{}, nil) + tc.mocks.stores.TagMock().EXPECT().SaveTags(ctx, []*database.Tag(nil)).Return(nil) + tc.mocks.stores.RepoMock().EXPECT().FindByPath(ctx, types.DatasetRepo, "ns", "n").Return( + &database.Repository{ID: 1}, nil, + ) + tc.mocks.stores.TagMock().EXPECT().SetMetaTags( + ctx, types.DatasetRepo, "ns", "n", []*database.Tag(nil), + ).Return(nil, nil) + tc.mocks.stores.RepoMock().EXPECT().UpdateLicenseByTag(ctx, int64(1)).Return(nil) + + data, err := tc.UpdateMetaTags(ctx, database.DatasetTagScope, "ns", "n", "") + require.Nil(t, err) + require.Equal(t, []*database.RepositoryTag(nil), data) +} + +func TestTagComponent_UpdateLibraryTags(t *testing.T) { + ctx := context.TODO() + tc := initializeTestTagComponent(ctx, t) + + tags := []*database.Tag{ + {Category: "framework", Name: "pytorch", ID: 1}, + {Category: "framework", Name: "tensorflow", ID: 2}, + } + tc.mocks.stores.TagMock().EXPECT().AllDatasetTags(ctx).Return(tags, nil) + tc.mocks.stores.TagMock().EXPECT().SetLibraryTag( + ctx, types.DatasetRepo, "ns", "n", tags[1], tags[0], + ).Return(nil) + + err := tc.UpdateLibraryTags( + ctx, database.DatasetTagScope, "ns", "n", "pytorch_model_old.bin", "tf_model_new.h5", + ) + require.Nil(t, err) + +} + +func TestTagComponent_UpdateRepoTagsByCategory(t *testing.T) { + ctx := context.TODO() + tc := initializeTestTagComponent(ctx, t) + + tc.mocks.stores.TagMock().EXPECT().AllTagsByScopeAndCategory(ctx, database.DatasetTagScope, "c").Return( + []*database.Tag{ + {Name: "t1", ID: 2}, + }, nil, + ) + tc.mocks.stores.RepoMock().EXPECT().TagIDs(ctx, int64(1), "c").Return([]int64{1}, nil) + tc.mocks.stores.TagMock().EXPECT().UpsertRepoTags(ctx, int64(1), []int64{1}, []int64{2}).Return(nil) + + err := tc.UpdateRepoTagsByCategory(ctx, database.DatasetTagScope, 1, "c", []string{"t1"}) + require.Nil(t, err) +} diff --git a/component/wire.go b/component/wire.go index e2bc9b85..89b3ab39 100644 --- a/component/wire.go +++ b/component/wire.go @@ -249,3 +249,99 @@ func initializeTestMultiSyncComponent(ctx context.Context, t interface { ) return &testMultiSyncWithMocks{} } + +type testInternalWithMocks struct { + *internalComponentImpl + mocks *Mocks +} + +func initializeTestInternalComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testInternalWithMocks { + wire.Build( + MockSuperSet, InternalComponentSet, + wire.Struct(new(testInternalWithMocks), "*"), + ) + return &testInternalWithMocks{} +} + +type testMirrorSourceWithMocks struct { + *mirrorSourceComponentImpl + mocks *Mocks +} + +func initializeTestMirrorSourceComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testMirrorSourceWithMocks { + wire.Build( + MockSuperSet, MirrorSourceComponentSet, + wire.Struct(new(testMirrorSourceWithMocks), "*"), + ) + return &testMirrorSourceWithMocks{} +} + +type testSpaceResourceWithMocks struct { + *spaceResourceComponentImpl + mocks *Mocks +} + +func initializeTestSpaceResourceComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testSpaceResourceWithMocks { + wire.Build( + MockSuperSet, SpaceResourceComponentSet, + wire.Struct(new(testSpaceResourceWithMocks), "*"), + ) + return &testSpaceResourceWithMocks{} +} + +type testTagWithMocks struct { + *tagComponentImpl + mocks *Mocks +} + +func initializeTestTagComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testTagWithMocks { + wire.Build( + MockSuperSet, TagComponentSet, + wire.Struct(new(testTagWithMocks), "*"), + ) + return &testTagWithMocks{} +} + +type testRecomWithMocks struct { + *recomComponentImpl + mocks *Mocks +} + +func initializeTestRecomComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testRecomWithMocks { + wire.Build( + MockSuperSet, RecomComponentSet, + wire.Struct(new(testRecomWithMocks), "*"), + ) + return &testRecomWithMocks{} +} + +type testSpaceSdkWithMocks struct { + *spaceSdkComponentImpl + mocks *Mocks +} + +func initializeTestSpaceSdkComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testSpaceSdkWithMocks { + wire.Build( + MockSuperSet, SpaceSdkComponentSet, + wire.Struct(new(testSpaceSdkWithMocks), "*"), + ) + return &testSpaceSdkWithMocks{} +} diff --git a/component/wire_gen_test.go b/component/wire_gen_test.go index 9f17ccf2..2fac5ee9 100644 --- a/component/wire_gen_test.go +++ b/component/wire_gen_test.go @@ -13,7 +13,6 @@ import ( "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/deploy" "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/git/gitserver" "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/git/mirrorserver" - "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/inference" "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/parquet" "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/rpc" "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/store/s3" @@ -51,9 +50,9 @@ func initializeTestRepoComponent(ctx context.Context, t interface { runtimeArchitecture: mockRuntimeArchitectureComponent, sensitive: mockSensitiveComponent, } - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -63,9 +62,9 @@ func initializeTestRepoComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestRepoWithMocks := &testRepoWithMocks{ repoComponentImpl: componentRepoComponentImpl, @@ -101,9 +100,9 @@ func initializeTestPromptComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -113,9 +112,9 @@ func initializeTestPromptComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestPromptWithMocks := &testPromptWithMocks{ promptComponentImpl: componentPromptComponentImpl, @@ -150,9 +149,9 @@ func initializeTestUserComponent(ctx context.Context, t interface { mockClient := s3.NewMockClient(t) mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -162,9 +161,9 @@ func initializeTestUserComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestUserWithMocks := &testUserWithMocks{ userComponentImpl: componentUserComponentImpl, @@ -200,9 +199,9 @@ func initializeTestSpaceComponent(ctx context.Context, t interface { mockClient := s3.NewMockClient(t) mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -212,9 +211,9 @@ func initializeTestSpaceComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestSpaceWithMocks := &testSpaceWithMocks{ spaceComponentImpl: componentSpaceComponentImpl, @@ -231,13 +230,12 @@ func initializeTestModelComponent(ctx context.Context, t interface { mockStores := tests.NewMockStores(t) mockRepoComponent := component.NewMockRepoComponent(t) mockSpaceComponent := component.NewMockSpaceComponent(t) - mockClient := inference.NewMockClient(t) mockDeployer := deploy.NewMockDeployer(t) mockAccountingComponent := component.NewMockAccountingComponent(t) mockRuntimeArchitectureComponent := component.NewMockRuntimeArchitectureComponent(t) mockGitServer := gitserver.NewMockGitServer(t) mockUserSvcClient := rpc.NewMockUserSvcClient(t) - componentModelComponentImpl := NewTestModelComponent(config, mockStores, mockRepoComponent, mockSpaceComponent, mockClient, mockDeployer, mockAccountingComponent, mockRuntimeArchitectureComponent, mockGitServer, mockUserSvcClient) + componentModelComponentImpl := NewTestModelComponent(config, mockStores, mockRepoComponent, mockSpaceComponent, mockDeployer, mockAccountingComponent, mockRuntimeArchitectureComponent, mockGitServer, mockUserSvcClient) mockTagComponent := component.NewMockTagComponent(t) mockSensitiveComponent := component.NewMockSensitiveComponent(t) componentMockedComponents := &mockedComponents{ @@ -248,23 +246,24 @@ func initializeTestModelComponent(ctx context.Context, t interface { runtimeArchitecture: mockRuntimeArchitectureComponent, sensitive: mockSensitiveComponent, } - s3MockClient := s3.NewMockClient(t) + mockClient := s3.NewMockClient(t) mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, gitServer: mockGitServer, userSvcClient: mockUserSvcClient, - s3Client: s3MockClient, + s3Client: mockClient, mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: mockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestModelWithMocks := &testModelWithMocks{ modelComponentImpl: componentModelComponentImpl, @@ -300,8 +299,8 @@ func initializeTestAccountingComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -311,9 +310,9 @@ func initializeTestAccountingComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestAccountingWithMocks := &testAccountingWithMocks{ accountingComponentImpl: componentAccountingComponentImpl, @@ -350,8 +349,8 @@ func initializeTestDatasetViewerComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -361,9 +360,9 @@ func initializeTestDatasetViewerComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestDatasetViewerWithMocks := &testDatasetViewerWithMocks{ datasetViewerComponentImpl: componentDatasetViewerComponentImpl, @@ -399,9 +398,9 @@ func initializeTestGitHTTPComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -411,9 +410,9 @@ func initializeTestGitHTTPComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestGitHTTPWithMocks := &testGitHTTPWithMocks{ gitHTTPComponentImpl: componentGitHTTPComponentImpl, @@ -448,9 +447,9 @@ func initializeTestDiscussionComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -460,9 +459,9 @@ func initializeTestDiscussionComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestDiscussionWithMocks := &testDiscussionWithMocks{ discussionComponentImpl: componentDiscussionComponentImpl, @@ -497,9 +496,9 @@ func initializeTestRuntimeArchComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -509,9 +508,9 @@ func initializeTestRuntimeArchComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestRuntimeArchWithMocks := &testRuntimeArchWithMocks{ runtimeArchitectureComponentImpl: componentRuntimeArchitectureComponentImpl, @@ -547,9 +546,9 @@ func initializeTestMirrorComponent(ctx context.Context, t interface { } mockUserSvcClient := rpc.NewMockUserSvcClient(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -559,9 +558,9 @@ func initializeTestMirrorComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestMirrorWithMocks := &testMirrorWithMocks{ mirrorComponentImpl: componentMirrorComponentImpl, @@ -596,9 +595,9 @@ func initializeTestCollectionComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -608,9 +607,9 @@ func initializeTestCollectionComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestCollectionWithMocks := &testCollectionWithMocks{ collectionComponentImpl: componentCollectionComponentImpl, @@ -646,9 +645,9 @@ func initializeTestDatasetComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -658,9 +657,9 @@ func initializeTestDatasetComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestDatasetWithMocks := &testDatasetWithMocks{ datasetComponentImpl: componentDatasetComponentImpl, @@ -696,9 +695,9 @@ func initializeTestCodeComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -708,9 +707,9 @@ func initializeTestCodeComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestCodeWithMocks := &testCodeWithMocks{ codeComponentImpl: componentCodeComponentImpl, @@ -746,9 +745,9 @@ func initializeTestMultiSyncComponent(ctx context.Context, t interface { mockMirrorServer := mirrorserver.NewMockMirrorServer(t) mockPriorityQueue := queue.NewMockPriorityQueue(t) mockDeployer := deploy.NewMockDeployer(t) - inferenceMockClient := inference.NewMockClient(t) mockAccountingClient := accounting.NewMockAccountingClient(t) mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) mocks := &Mocks{ stores: mockStores, components: componentMockedComponents, @@ -758,9 +757,9 @@ func initializeTestMultiSyncComponent(ctx context.Context, t interface { mirrorServer: mockMirrorServer, mirrorQueue: mockPriorityQueue, deployer: mockDeployer, - inferenceClient: inferenceMockClient, accountingClient: mockAccountingClient, preader: mockReader, + moderationClient: mockModerationSvcClient, } componentTestMultiSyncWithMocks := &testMultiSyncWithMocks{ multiSyncComponentImpl: componentMultiSyncComponentImpl, @@ -769,6 +768,306 @@ func initializeTestMultiSyncComponent(ctx context.Context, t interface { return componentTestMultiSyncWithMocks } +func initializeTestInternalComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testInternalWithMocks { + config := ProvideTestConfig() + mockStores := tests.NewMockStores(t) + mockRepoComponent := component.NewMockRepoComponent(t) + mockGitServer := gitserver.NewMockGitServer(t) + componentInternalComponentImpl := NewTestInternalComponent(config, mockStores, mockRepoComponent, mockGitServer) + mockAccountingComponent := component.NewMockAccountingComponent(t) + mockTagComponent := component.NewMockTagComponent(t) + mockSpaceComponent := component.NewMockSpaceComponent(t) + mockRuntimeArchitectureComponent := component.NewMockRuntimeArchitectureComponent(t) + mockSensitiveComponent := component.NewMockSensitiveComponent(t) + componentMockedComponents := &mockedComponents{ + accounting: mockAccountingComponent, + repo: mockRepoComponent, + tag: mockTagComponent, + space: mockSpaceComponent, + runtimeArchitecture: mockRuntimeArchitectureComponent, + sensitive: mockSensitiveComponent, + } + mockUserSvcClient := rpc.NewMockUserSvcClient(t) + mockClient := s3.NewMockClient(t) + mockMirrorServer := mirrorserver.NewMockMirrorServer(t) + mockPriorityQueue := queue.NewMockPriorityQueue(t) + mockDeployer := deploy.NewMockDeployer(t) + mockAccountingClient := accounting.NewMockAccountingClient(t) + mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) + mocks := &Mocks{ + stores: mockStores, + components: componentMockedComponents, + gitServer: mockGitServer, + userSvcClient: mockUserSvcClient, + s3Client: mockClient, + mirrorServer: mockMirrorServer, + mirrorQueue: mockPriorityQueue, + deployer: mockDeployer, + accountingClient: mockAccountingClient, + preader: mockReader, + moderationClient: mockModerationSvcClient, + } + componentTestInternalWithMocks := &testInternalWithMocks{ + internalComponentImpl: componentInternalComponentImpl, + mocks: mocks, + } + return componentTestInternalWithMocks +} + +func initializeTestMirrorSourceComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testMirrorSourceWithMocks { + config := ProvideTestConfig() + mockStores := tests.NewMockStores(t) + componentMirrorSourceComponentImpl := NewTestMirrorSourceComponent(config, mockStores) + mockAccountingComponent := component.NewMockAccountingComponent(t) + mockRepoComponent := component.NewMockRepoComponent(t) + mockTagComponent := component.NewMockTagComponent(t) + mockSpaceComponent := component.NewMockSpaceComponent(t) + mockRuntimeArchitectureComponent := component.NewMockRuntimeArchitectureComponent(t) + mockSensitiveComponent := component.NewMockSensitiveComponent(t) + componentMockedComponents := &mockedComponents{ + accounting: mockAccountingComponent, + repo: mockRepoComponent, + tag: mockTagComponent, + space: mockSpaceComponent, + runtimeArchitecture: mockRuntimeArchitectureComponent, + sensitive: mockSensitiveComponent, + } + mockGitServer := gitserver.NewMockGitServer(t) + mockUserSvcClient := rpc.NewMockUserSvcClient(t) + mockClient := s3.NewMockClient(t) + mockMirrorServer := mirrorserver.NewMockMirrorServer(t) + mockPriorityQueue := queue.NewMockPriorityQueue(t) + mockDeployer := deploy.NewMockDeployer(t) + mockAccountingClient := accounting.NewMockAccountingClient(t) + mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) + mocks := &Mocks{ + stores: mockStores, + components: componentMockedComponents, + gitServer: mockGitServer, + userSvcClient: mockUserSvcClient, + s3Client: mockClient, + mirrorServer: mockMirrorServer, + mirrorQueue: mockPriorityQueue, + deployer: mockDeployer, + accountingClient: mockAccountingClient, + preader: mockReader, + moderationClient: mockModerationSvcClient, + } + componentTestMirrorSourceWithMocks := &testMirrorSourceWithMocks{ + mirrorSourceComponentImpl: componentMirrorSourceComponentImpl, + mocks: mocks, + } + return componentTestMirrorSourceWithMocks +} + +func initializeTestSpaceResourceComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testSpaceResourceWithMocks { + config := ProvideTestConfig() + mockStores := tests.NewMockStores(t) + mockDeployer := deploy.NewMockDeployer(t) + mockAccountingComponent := component.NewMockAccountingComponent(t) + componentSpaceResourceComponentImpl := NewTestSpaceResourceComponent(config, mockStores, mockDeployer, mockAccountingComponent) + mockRepoComponent := component.NewMockRepoComponent(t) + mockTagComponent := component.NewMockTagComponent(t) + mockSpaceComponent := component.NewMockSpaceComponent(t) + mockRuntimeArchitectureComponent := component.NewMockRuntimeArchitectureComponent(t) + mockSensitiveComponent := component.NewMockSensitiveComponent(t) + componentMockedComponents := &mockedComponents{ + accounting: mockAccountingComponent, + repo: mockRepoComponent, + tag: mockTagComponent, + space: mockSpaceComponent, + runtimeArchitecture: mockRuntimeArchitectureComponent, + sensitive: mockSensitiveComponent, + } + mockGitServer := gitserver.NewMockGitServer(t) + mockUserSvcClient := rpc.NewMockUserSvcClient(t) + mockClient := s3.NewMockClient(t) + mockMirrorServer := mirrorserver.NewMockMirrorServer(t) + mockPriorityQueue := queue.NewMockPriorityQueue(t) + mockAccountingClient := accounting.NewMockAccountingClient(t) + mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) + mocks := &Mocks{ + stores: mockStores, + components: componentMockedComponents, + gitServer: mockGitServer, + userSvcClient: mockUserSvcClient, + s3Client: mockClient, + mirrorServer: mockMirrorServer, + mirrorQueue: mockPriorityQueue, + deployer: mockDeployer, + accountingClient: mockAccountingClient, + preader: mockReader, + moderationClient: mockModerationSvcClient, + } + componentTestSpaceResourceWithMocks := &testSpaceResourceWithMocks{ + spaceResourceComponentImpl: componentSpaceResourceComponentImpl, + mocks: mocks, + } + return componentTestSpaceResourceWithMocks +} + +func initializeTestTagComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testTagWithMocks { + config := ProvideTestConfig() + mockStores := tests.NewMockStores(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) + componentTagComponentImpl := NewTestTagComponent(config, mockStores, mockModerationSvcClient) + mockAccountingComponent := component.NewMockAccountingComponent(t) + mockRepoComponent := component.NewMockRepoComponent(t) + mockTagComponent := component.NewMockTagComponent(t) + mockSpaceComponent := component.NewMockSpaceComponent(t) + mockRuntimeArchitectureComponent := component.NewMockRuntimeArchitectureComponent(t) + mockSensitiveComponent := component.NewMockSensitiveComponent(t) + componentMockedComponents := &mockedComponents{ + accounting: mockAccountingComponent, + repo: mockRepoComponent, + tag: mockTagComponent, + space: mockSpaceComponent, + runtimeArchitecture: mockRuntimeArchitectureComponent, + sensitive: mockSensitiveComponent, + } + mockGitServer := gitserver.NewMockGitServer(t) + mockUserSvcClient := rpc.NewMockUserSvcClient(t) + mockClient := s3.NewMockClient(t) + mockMirrorServer := mirrorserver.NewMockMirrorServer(t) + mockPriorityQueue := queue.NewMockPriorityQueue(t) + mockDeployer := deploy.NewMockDeployer(t) + mockAccountingClient := accounting.NewMockAccountingClient(t) + mockReader := parquet.NewMockReader(t) + mocks := &Mocks{ + stores: mockStores, + components: componentMockedComponents, + gitServer: mockGitServer, + userSvcClient: mockUserSvcClient, + s3Client: mockClient, + mirrorServer: mockMirrorServer, + mirrorQueue: mockPriorityQueue, + deployer: mockDeployer, + accountingClient: mockAccountingClient, + preader: mockReader, + moderationClient: mockModerationSvcClient, + } + componentTestTagWithMocks := &testTagWithMocks{ + tagComponentImpl: componentTagComponentImpl, + mocks: mocks, + } + return componentTestTagWithMocks +} + +func initializeTestRecomComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testRecomWithMocks { + config := ProvideTestConfig() + mockStores := tests.NewMockStores(t) + mockGitServer := gitserver.NewMockGitServer(t) + componentRecomComponentImpl := NewTestRecomComponent(config, mockStores, mockGitServer) + mockAccountingComponent := component.NewMockAccountingComponent(t) + mockRepoComponent := component.NewMockRepoComponent(t) + mockTagComponent := component.NewMockTagComponent(t) + mockSpaceComponent := component.NewMockSpaceComponent(t) + mockRuntimeArchitectureComponent := component.NewMockRuntimeArchitectureComponent(t) + mockSensitiveComponent := component.NewMockSensitiveComponent(t) + componentMockedComponents := &mockedComponents{ + accounting: mockAccountingComponent, + repo: mockRepoComponent, + tag: mockTagComponent, + space: mockSpaceComponent, + runtimeArchitecture: mockRuntimeArchitectureComponent, + sensitive: mockSensitiveComponent, + } + mockUserSvcClient := rpc.NewMockUserSvcClient(t) + mockClient := s3.NewMockClient(t) + mockMirrorServer := mirrorserver.NewMockMirrorServer(t) + mockPriorityQueue := queue.NewMockPriorityQueue(t) + mockDeployer := deploy.NewMockDeployer(t) + mockAccountingClient := accounting.NewMockAccountingClient(t) + mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) + mocks := &Mocks{ + stores: mockStores, + components: componentMockedComponents, + gitServer: mockGitServer, + userSvcClient: mockUserSvcClient, + s3Client: mockClient, + mirrorServer: mockMirrorServer, + mirrorQueue: mockPriorityQueue, + deployer: mockDeployer, + accountingClient: mockAccountingClient, + preader: mockReader, + moderationClient: mockModerationSvcClient, + } + componentTestRecomWithMocks := &testRecomWithMocks{ + recomComponentImpl: componentRecomComponentImpl, + mocks: mocks, + } + return componentTestRecomWithMocks +} + +func initializeTestSpaceSdkComponent(ctx context.Context, t interface { + Cleanup(func()) + mock.TestingT +}) *testSpaceSdkWithMocks { + config := ProvideTestConfig() + mockStores := tests.NewMockStores(t) + componentSpaceSdkComponentImpl := NewTestSpaceSdkComponent(config, mockStores) + mockAccountingComponent := component.NewMockAccountingComponent(t) + mockRepoComponent := component.NewMockRepoComponent(t) + mockTagComponent := component.NewMockTagComponent(t) + mockSpaceComponent := component.NewMockSpaceComponent(t) + mockRuntimeArchitectureComponent := component.NewMockRuntimeArchitectureComponent(t) + mockSensitiveComponent := component.NewMockSensitiveComponent(t) + componentMockedComponents := &mockedComponents{ + accounting: mockAccountingComponent, + repo: mockRepoComponent, + tag: mockTagComponent, + space: mockSpaceComponent, + runtimeArchitecture: mockRuntimeArchitectureComponent, + sensitive: mockSensitiveComponent, + } + mockGitServer := gitserver.NewMockGitServer(t) + mockUserSvcClient := rpc.NewMockUserSvcClient(t) + mockClient := s3.NewMockClient(t) + mockMirrorServer := mirrorserver.NewMockMirrorServer(t) + mockPriorityQueue := queue.NewMockPriorityQueue(t) + mockDeployer := deploy.NewMockDeployer(t) + mockAccountingClient := accounting.NewMockAccountingClient(t) + mockReader := parquet.NewMockReader(t) + mockModerationSvcClient := rpc.NewMockModerationSvcClient(t) + mocks := &Mocks{ + stores: mockStores, + components: componentMockedComponents, + gitServer: mockGitServer, + userSvcClient: mockUserSvcClient, + s3Client: mockClient, + mirrorServer: mockMirrorServer, + mirrorQueue: mockPriorityQueue, + deployer: mockDeployer, + accountingClient: mockAccountingClient, + preader: mockReader, + moderationClient: mockModerationSvcClient, + } + componentTestSpaceSdkWithMocks := &testSpaceSdkWithMocks{ + spaceSdkComponentImpl: componentSpaceSdkComponentImpl, + mocks: mocks, + } + return componentTestSpaceSdkWithMocks +} + // wire.go: type testRepoWithMocks struct { @@ -845,3 +1144,33 @@ type testMultiSyncWithMocks struct { *multiSyncComponentImpl mocks *Mocks } + +type testInternalWithMocks struct { + *internalComponentImpl + mocks *Mocks +} + +type testMirrorSourceWithMocks struct { + *mirrorSourceComponentImpl + mocks *Mocks +} + +type testSpaceResourceWithMocks struct { + *spaceResourceComponentImpl + mocks *Mocks +} + +type testTagWithMocks struct { + *tagComponentImpl + mocks *Mocks +} + +type testRecomWithMocks struct { + *recomComponentImpl + mocks *Mocks +} + +type testSpaceSdkWithMocks struct { + *spaceSdkComponentImpl + mocks *Mocks +} diff --git a/component/wireset.go b/component/wireset.go index 9e84a2e8..b25cc075 100644 --- a/component/wireset.go +++ b/component/wireset.go @@ -6,7 +6,6 @@ import ( mock_deploy "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/deploy" mock_git "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/git/gitserver" mock_mirror "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/git/mirrorserver" - mock_inference "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/inference" mock_preader "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/parquet" mock_rpc "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/rpc" mock_s3 "opencsg.com/csghub-server/_mocks/opencsg.com/csghub-server/builder/store/s3" @@ -16,7 +15,6 @@ import ( "opencsg.com/csghub-server/builder/deploy" "opencsg.com/csghub-server/builder/git/gitserver" "opencsg.com/csghub-server/builder/git/mirrorserver" - "opencsg.com/csghub-server/builder/inference" "opencsg.com/csghub-server/builder/llm" "opencsg.com/csghub-server/builder/parquet" "opencsg.com/csghub-server/builder/rpc" @@ -84,11 +82,6 @@ var MockedMirrorQueueSet = wire.NewSet( wire.Bind(new(queue.PriorityQueue), new(*mock_mirror_queue.MockPriorityQueue)), ) -var MockedInferenceClientSet = wire.NewSet( - mock_inference.NewMockClient, - wire.Bind(new(inference.Client), new(*mock_inference.MockClient)), -) - var MockedAccountingClientSet = wire.NewSet( mock_accounting.NewMockAccountingClient, wire.Bind(new(accounting.AccountingClient), new(*mock_accounting.MockAccountingClient)), @@ -99,6 +92,11 @@ var MockedParquetReaderSet = wire.NewSet( wire.Bind(new(parquet.Reader), new(*mock_preader.MockReader)), ) +var MockedModerationSvcClientSet = wire.NewSet( + mock_rpc.NewMockModerationSvcClient, + wire.Bind(new(rpc.ModerationSvcClient), new(*mock_rpc.MockModerationSvcClient)), +) + type Mocks struct { stores *tests.MockStores components *mockedComponents @@ -108,9 +106,9 @@ type Mocks struct { mirrorServer *mock_mirror.MockMirrorServer mirrorQueue *mock_mirror_queue.MockPriorityQueue deployer *mock_deploy.MockDeployer - inferenceClient *mock_inference.MockClient accountingClient *mock_accounting.MockAccountingClient preader *mock_preader.MockReader + moderationClient *mock_rpc.MockModerationSvcClient } var AllMockSet = wire.NewSet( @@ -125,8 +123,8 @@ func ProvideTestConfig() *config.Config { var MockSuperSet = wire.NewSet( MockedComponentSet, AllMockSet, MockedStoreSet, MockedGitServerSet, MockedUserSvcSet, MockedS3Set, MockedDeployerSet, ProvideTestConfig, MockedMirrorServerSet, - MockedMirrorQueueSet, MockedInferenceClientSet, MockedAccountingClientSet, - MockedParquetReaderSet, + MockedMirrorQueueSet, MockedAccountingClientSet, MockedParquetReaderSet, + MockedModerationSvcClientSet, ) func NewTestRepoComponent(config *config.Config, stores *tests.MockStores, rpcUser rpc.UserSvcClient, gitServer gitserver.GitServer, tagComponent TagComponent, s3Client s3.Client, deployer deploy.Deployer, accountingComponent AccountingComponent, mq queue.PriorityQueue, mirrorServer mirrorserver.MirrorServer) *repoComponentImpl { @@ -244,7 +242,6 @@ func NewTestModelComponent( stores *tests.MockStores, repoComponent RepoComponent, spaceComponent SpaceComponent, - inferClient inference.Client, deployer deploy.Deployer, accountingComponent AccountingComponent, runtimeArchComponent RuntimeArchitectureComponent, @@ -260,7 +257,6 @@ func NewTestModelComponent( modelStore: stores.Model, repoStore: stores.Repo, spaceResourceStore: stores.SpaceResource, - inferClient: inferClient, userStore: stores.User, deployer: deployer, accountingComponent: accountingComponent, @@ -432,3 +428,62 @@ func NewTestMultiSyncComponent(config *config.Config, stores *tests.MockStores, } var MultiSyncComponentSet = wire.NewSet(NewTestMultiSyncComponent) + +func NewTestInternalComponent(config *config.Config, stores *tests.MockStores, repoComponent RepoComponent, gitServer gitserver.GitServer) *internalComponentImpl { + return &internalComponentImpl{ + config: config, + sshKeyStore: stores.SSH, + repoStore: stores.Repo, + tokenStore: stores.AccessToken, + namespaceStore: stores.Namespace, + repoComponent: repoComponent, + gitServer: gitServer, + } +} + +var InternalComponentSet = wire.NewSet(NewTestInternalComponent) + +func NewTestMirrorSourceComponent(config *config.Config, stores *tests.MockStores) *mirrorSourceComponentImpl { + return &mirrorSourceComponentImpl{ + mirrorSourceStore: stores.MirrorSource, + userStore: stores.User, + } +} + +var MirrorSourceComponentSet = wire.NewSet(NewTestMirrorSourceComponent) + +func NewTestSpaceResourceComponent(config *config.Config, stores *tests.MockStores, deployer deploy.Deployer, accountComponent AccountingComponent) *spaceResourceComponentImpl { + return &spaceResourceComponentImpl{ + deployer: deployer, + } +} + +var SpaceResourceComponentSet = wire.NewSet(NewTestSpaceResourceComponent) + +func NewTestTagComponent(config *config.Config, stores *tests.MockStores, sensitiveChecker rpc.ModerationSvcClient) *tagComponentImpl { + return &tagComponentImpl{ + tagStore: stores.Tag, + repoStore: stores.Repo, + sensitiveChecker: sensitiveChecker, + } +} + +var TagComponentSet = wire.NewSet(NewTestTagComponent) + +func NewTestRecomComponent(config *config.Config, stores *tests.MockStores, gitServer gitserver.GitServer) *recomComponentImpl { + return &recomComponentImpl{ + recomStore: stores.Recom, + repoStore: stores.Repo, + gitServer: gitServer, + } +} + +var RecomComponentSet = wire.NewSet(NewTestRecomComponent) + +func NewTestSpaceSdkComponent(config *config.Config, stores *tests.MockStores) *spaceSdkComponentImpl { + return &spaceSdkComponentImpl{ + spaceSdkStore: stores.SpaceSdk, + } +} + +var SpaceSdkComponentSet = wire.NewSet(NewTestSpaceSdkComponent)