@@ -18,6 +18,7 @@ import (
1818 repo_mocks "github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/repo/mocks"
1919
2020 idgenmock "github.com/coze-dev/coze-loop/backend/infra/idgen/mocks"
21+ "github.com/coze-dev/coze-loop/backend/infra/middleware/session"
2122 "github.com/coze-dev/coze-loop/backend/kitex_gen/base"
2223 "github.com/coze-dev/coze-loop/backend/kitex_gen/coze/loop/data/domain/tag"
2324 "github.com/coze-dev/coze-loop/backend/kitex_gen/coze/loop/evaluation/domain/common"
@@ -1814,11 +1815,13 @@ func TestExperimentApplication_KillExperiment(t *testing.T) {
18141815 // Create mock objects
18151816 mockManager := servicemocks .NewMockIExptManager (ctrl )
18161817 mockAuth := rpcmocks .NewMockIAuthProvider (ctrl )
1818+ mockConfiger := componentMocks .NewMockIConfiger (ctrl )
18171819
18181820 // Test data
18191821 validWorkspaceID := int64 (123 )
18201822 validExptID := int64 (456 )
18211823 validUserID := int64 (789 )
1824+ validRunID := int64 (999 )
18221825
18231826 tests := []struct {
18241827 name string
@@ -1828,19 +1831,59 @@ func TestExperimentApplication_KillExperiment(t *testing.T) {
18281831 wantErr bool
18291832 }{
18301833 {
1831- name : "successfully terminate experiment" ,
1834+ name : "successfully terminate experiment with maintainer permission " ,
18321835 req : & exptpb.KillExperimentRequest {
18331836 WorkspaceID : gptr .Of (validWorkspaceID ),
18341837 ExptID : gptr .Of (validExptID ),
18351838 },
18361839 mockSetup : func () {
18371840 // 获取实验信息
18381841 mockManager .EXPECT ().Get (gomock .Any (), validExptID , validWorkspaceID , gomock .Any ()).Return (& entity.Experiment {
1839- ID : validExptID ,
1840- SpaceID : validWorkspaceID ,
1841- CreatedBy : strconv .FormatInt (validUserID , 10 ),
1842+ ID : validExptID ,
1843+ SpaceID : validWorkspaceID ,
1844+ CreatedBy : strconv .FormatInt (validUserID , 10 ),
1845+ LatestRunID : validRunID ,
1846+ Status : entity .ExptStatus_Processing ,
1847+ }, nil )
1848+
1849+ // Maintainer权限检查 - 用户是maintainer
1850+ mockConfiger .EXPECT ().GetMaintainerUserIDs (gomock .Any ()).Return (map [string ]bool {
1851+ strconv .FormatInt (validUserID , 10 ): true ,
1852+ })
1853+
1854+ // 设置终止中状态(实现中同步执行)
1855+ mockManager .EXPECT ().SetExptTerminating (gomock .Any (), validExptID , validRunID , validWorkspaceID , gomock .Any ()).Return (nil )
1856+
1857+ // 异步终止:允许在后台调用,不校验调用次数
1858+ mockManager .EXPECT ().CompleteRun (gomock .Any (), validExptID , validRunID , validWorkspaceID , gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
1859+ mockManager .EXPECT ().CompleteExpt (gomock .Any (), validExptID , validWorkspaceID , gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
1860+ },
1861+ wantResp : & exptpb.KillExperimentResponse {
1862+ BaseResp : base .NewBaseResp (),
1863+ },
1864+ wantErr : false ,
1865+ },
1866+ {
1867+ name : "successfully terminate experiment with regular permission" ,
1868+ req : & exptpb.KillExperimentRequest {
1869+ WorkspaceID : gptr .Of (validWorkspaceID ),
1870+ ExptID : gptr .Of (validExptID ),
1871+ },
1872+ mockSetup : func () {
1873+ // 获取实验信息
1874+ mockManager .EXPECT ().Get (gomock .Any (), validExptID , validWorkspaceID , gomock .Any ()).Return (& entity.Experiment {
1875+ ID : validExptID ,
1876+ SpaceID : validWorkspaceID ,
1877+ CreatedBy : strconv .FormatInt (validUserID , 10 ),
1878+ LatestRunID : validRunID ,
1879+ Status : entity .ExptStatus_Processing ,
18421880 }, nil )
18431881
1882+ // Maintainer权限检查 - 用户不是maintainer
1883+ mockConfiger .EXPECT ().GetMaintainerUserIDs (gomock .Any ()).Return (map [string ]bool {
1884+ "other_user" : true ,
1885+ })
1886+
18441887 // 权限验证
18451888 mockAuth .EXPECT ().AuthorizationWithoutSPI (gomock .Any (), & rpc.AuthorizationWithoutSPIParam {
18461889 ObjectID : strconv .FormatInt (validExptID , 10 ),
@@ -1850,19 +1893,12 @@ func TestExperimentApplication_KillExperiment(t *testing.T) {
18501893 ResourceSpaceID : validWorkspaceID ,
18511894 }).Return (nil )
18521895
1853- // 终止实验
1854- mockManager .EXPECT ().CompleteExpt (gomock .Any (), validExptID , validWorkspaceID , gomock .Any (), gomock .Any ()).DoAndReturn (
1855- func (ctx context.Context , exptID , spaceID int64 , session * entity.Session , opts ... entity.CompleteExptOptionFn ) error {
1856- // 验证传入的 opts 是否包含正确的状态设置
1857- opt := & entity.CompleteExptOption {}
1858- for _ , fn := range opts {
1859- fn (opt )
1860- }
1861- if opt .Status != entity .ExptStatus_Terminated {
1862- t .Errorf ("expected status %v, got %v" , entity .ExptStatus_Terminated , opt .Status )
1863- }
1864- return nil
1865- })
1896+ // 设置终止中状态(实现中同步执行)
1897+ mockManager .EXPECT ().SetExptTerminating (gomock .Any (), validExptID , validRunID , validWorkspaceID , gomock .Any ()).Return (nil )
1898+
1899+ // 异步终止:允许在后台调用,不校验调用次数
1900+ mockManager .EXPECT ().CompleteRun (gomock .Any (), validExptID , validRunID , validWorkspaceID , gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
1901+ mockManager .EXPECT ().CompleteExpt (gomock .Any (), validExptID , validWorkspaceID , gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
18661902 },
18671903 wantResp : & exptpb.KillExperimentResponse {
18681904 BaseResp : base .NewBaseResp (),
@@ -1881,6 +1917,102 @@ func TestExperimentApplication_KillExperiment(t *testing.T) {
18811917 wantResp : nil ,
18821918 wantErr : true ,
18831919 },
1920+ {
1921+ name : "permission validation failed for regular user" ,
1922+ req : & exptpb.KillExperimentRequest {
1923+ WorkspaceID : gptr .Of (validWorkspaceID ),
1924+ ExptID : gptr .Of (validExptID ),
1925+ },
1926+ mockSetup : func () {
1927+ // 获取实验信息
1928+ mockManager .EXPECT ().Get (gomock .Any (), validExptID , validWorkspaceID , gomock .Any ()).Return (& entity.Experiment {
1929+ ID : validExptID ,
1930+ SpaceID : validWorkspaceID ,
1931+ CreatedBy : strconv .FormatInt (validUserID , 10 ),
1932+ LatestRunID : validRunID ,
1933+ Status : entity .ExptStatus_Processing ,
1934+ }, nil )
1935+
1936+ // Maintainer权限检查 - 用户不是maintainer
1937+ mockConfiger .EXPECT ().GetMaintainerUserIDs (gomock .Any ()).Return (map [string ]bool {
1938+ "other_user" : true ,
1939+ })
1940+
1941+ // 权限验证失败
1942+ mockAuth .EXPECT ().AuthorizationWithoutSPI (gomock .Any (), & rpc.AuthorizationWithoutSPIParam {
1943+ ObjectID : strconv .FormatInt (validExptID , 10 ),
1944+ SpaceID : validWorkspaceID ,
1945+ ActionObjects : []* rpc.ActionObject {{Action : gptr .Of (consts .Run ), EntityType : gptr .Of (rpc .AuthEntityType_EvaluationExperiment )}},
1946+ OwnerID : gptr .Of (strconv .FormatInt (validUserID , 10 )),
1947+ ResourceSpaceID : validWorkspaceID ,
1948+ }).Return (errorx .NewByCode (errno .CommonNoPermissionCode ))
1949+ },
1950+ wantResp : nil ,
1951+ wantErr : true ,
1952+ },
1953+ {
1954+ name : "complete run failed" ,
1955+ req : & exptpb.KillExperimentRequest {
1956+ WorkspaceID : gptr .Of (validWorkspaceID ),
1957+ ExptID : gptr .Of (validExptID ),
1958+ },
1959+ mockSetup : func () {
1960+ // 获取实验信息
1961+ mockManager .EXPECT ().Get (gomock .Any (), validExptID , validWorkspaceID , gomock .Any ()).Return (& entity.Experiment {
1962+ ID : validExptID ,
1963+ SpaceID : validWorkspaceID ,
1964+ CreatedBy : strconv .FormatInt (validUserID , 10 ),
1965+ LatestRunID : validRunID ,
1966+ Status : entity .ExptStatus_Processing ,
1967+ }, nil )
1968+
1969+ // Maintainer权限检查 - 用户是maintainer
1970+ mockConfiger .EXPECT ().GetMaintainerUserIDs (gomock .Any ()).Return (map [string ]bool {
1971+ strconv .FormatInt (validUserID , 10 ): true ,
1972+ })
1973+
1974+ // 设置终止中状态
1975+ mockManager .EXPECT ().SetExptTerminating (gomock .Any (), validExptID , validRunID , validWorkspaceID , gomock .Any ()).Return (nil )
1976+
1977+ // 异步终止运行失败:允许后台调用
1978+ mockManager .EXPECT ().CompleteRun (gomock .Any (), validExptID , validRunID , validWorkspaceID , gomock .Any (), gomock .Any ()).Return (
1979+ errorx .NewByCode (errno .CommonInternalErrorCode )).AnyTimes ()
1980+ },
1981+ wantResp : & exptpb.KillExperimentResponse {BaseResp : base .NewBaseResp ()},
1982+ wantErr : false ,
1983+ },
1984+ {
1985+ name : "complete experiment failed" ,
1986+ req : & exptpb.KillExperimentRequest {
1987+ WorkspaceID : gptr .Of (validWorkspaceID ),
1988+ ExptID : gptr .Of (validExptID ),
1989+ },
1990+ mockSetup : func () {
1991+ // 获取实验信息
1992+ mockManager .EXPECT ().Get (gomock .Any (), validExptID , validWorkspaceID , gomock .Any ()).Return (& entity.Experiment {
1993+ ID : validExptID ,
1994+ SpaceID : validWorkspaceID ,
1995+ CreatedBy : strconv .FormatInt (validUserID , 10 ),
1996+ LatestRunID : validRunID ,
1997+ Status : entity .ExptStatus_Processing ,
1998+ }, nil )
1999+
2000+ // Maintainer权限检查 - 用户是maintainer
2001+ mockConfiger .EXPECT ().GetMaintainerUserIDs (gomock .Any ()).Return (map [string ]bool {
2002+ strconv .FormatInt (validUserID , 10 ): true ,
2003+ })
2004+
2005+ // 设置终止中状态
2006+ mockManager .EXPECT ().SetExptTerminating (gomock .Any (), validExptID , validRunID , validWorkspaceID , gomock .Any ()).Return (nil )
2007+
2008+ // 异步终止
2009+ mockManager .EXPECT ().CompleteRun (gomock .Any (), validExptID , validRunID , validWorkspaceID , gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
2010+ mockManager .EXPECT ().CompleteExpt (gomock .Any (), validExptID , validWorkspaceID , gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (
2011+ errorx .NewByCode (errno .CommonInternalErrorCode )).AnyTimes ()
2012+ },
2013+ wantResp : & exptpb.KillExperimentResponse {BaseResp : base .NewBaseResp ()},
2014+ wantErr : false ,
2015+ },
18842016 }
18852017
18862018 for _ , tt := range tests {
@@ -1896,7 +2028,7 @@ func TestExperimentApplication_KillExperiment(t *testing.T) {
18962028 nil , // scheduler
18972029 nil , // recordEval
18982030 nil ,
1899- nil , // configer
2031+ mockConfiger , // configer
19002032 mockAuth ,
19012033 nil , // userInfoService
19022034 nil , // evalTargetService
@@ -1907,8 +2039,13 @@ func TestExperimentApplication_KillExperiment(t *testing.T) {
19072039 nil ,
19082040 )
19092041
2042+ // 设置 context 中的 UserID,这样 entity.NewSession 才能获取到 UserID
2043+ ctx := session .WithCtxUser (context .Background (), & session.User {
2044+ ID : strconv .FormatInt (validUserID , 10 ),
2045+ })
2046+
19102047 // 执行测试
1911- gotResp , err := app .KillExperiment (context . Background () , tt .req )
2048+ gotResp , err := app .KillExperiment (ctx , tt .req )
19122049
19132050 // 验证结果
19142051 if tt .wantErr {
0 commit comments