1+ // Copyright (c) 2025 coze-dev Authors
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ package experiment
5+
6+ import (
7+ "testing"
8+
9+ "github.com/bytedance/gg/gptr"
10+ "github.com/stretchr/testify/assert"
11+
12+ "github.com/coze-dev/coze-loop/backend/kitex_gen/coze/loop/evaluation/domain/common"
13+ domain_expt "github.com/coze-dev/coze-loop/backend/kitex_gen/coze/loop/evaluation/domain/expt"
14+ "github.com/coze-dev/coze-loop/backend/kitex_gen/coze/loop/evaluation/expt"
15+ "github.com/coze-dev/coze-loop/backend/modules/evaluation/consts"
16+ )
17+
18+ func TestEvalConfConvert_ConvertToEntity_TargetConfAlwaysCreated (t * testing.T ) {
19+ tests := []struct {
20+ name string
21+ request * expt.CreateExperimentRequest
22+ expectedTargetConf bool
23+ expectedVersionID int64
24+ expectedEvalSetConf bool
25+ }{
26+ {
27+ name : "nil_target_field_mapping_should_create_target_conf" ,
28+ request : & expt.CreateExperimentRequest {
29+ TargetVersionID : gptr .Of (int64 (123 )),
30+ TargetFieldMapping : nil ,
31+ EvaluatorFieldMapping : nil ,
32+ },
33+ expectedTargetConf : true ,
34+ expectedVersionID : 123 ,
35+ expectedEvalSetConf : false ,
36+ },
37+ {
38+ name : "valid_target_field_mapping_should_create_target_conf" ,
39+ request : & expt.CreateExperimentRequest {
40+ TargetVersionID : gptr .Of (int64 (456 )),
41+ TargetFieldMapping : & domain_expt.TargetFieldMapping {
42+ FromEvalSet : []* domain_expt.FieldMapping {
43+ {
44+ FieldName : gptr .Of ("input" ),
45+ FromFieldName : gptr .Of ("question" ),
46+ ConstValue : gptr .Of ("" ),
47+ },
48+ },
49+ },
50+ EvaluatorFieldMapping : nil ,
51+ },
52+ expectedTargetConf : true ,
53+ expectedVersionID : 456 ,
54+ expectedEvalSetConf : false ,
55+ },
56+ {
57+ name : "with_evaluator_field_mapping_should_create_both_confs" ,
58+ request : & expt.CreateExperimentRequest {
59+ TargetVersionID : gptr .Of (int64 (789 )),
60+ TargetFieldMapping : & domain_expt.TargetFieldMapping {
61+ FromEvalSet : []* domain_expt.FieldMapping {
62+ {
63+ FieldName : gptr .Of ("input" ),
64+ FromFieldName : gptr .Of ("question" ),
65+ },
66+ },
67+ },
68+ EvaluatorFieldMapping : []* domain_expt.EvaluatorFieldMapping {
69+ {
70+ EvaluatorVersionID : 999 ,
71+ FromEvalSet : []* domain_expt.FieldMapping {
72+ {
73+ FieldName : gptr .Of ("eval_input" ),
74+ FromFieldName : gptr .Of ("question" ),
75+ },
76+ },
77+ },
78+ },
79+ },
80+ expectedTargetConf : true ,
81+ expectedVersionID : 789 ,
82+ expectedEvalSetConf : true ,
83+ },
84+ }
85+
86+ converter := & EvalConfConvert {}
87+
88+ for _ , tt := range tests {
89+ t .Run (tt .name , func (t * testing.T ) {
90+ result , err := converter .ConvertToEntity (tt .request )
91+
92+ assert .NoError (t , err )
93+ assert .NotNil (t , result )
94+
95+ // TargetConf should always be created
96+ if tt .expectedTargetConf {
97+ assert .NotNil (t , result .ConnectorConf .TargetConf )
98+ assert .Equal (t , tt .expectedVersionID , result .ConnectorConf .TargetConf .TargetVersionID )
99+ assert .NotNil (t , result .ConnectorConf .TargetConf .IngressConf )
100+ } else {
101+ assert .Nil (t , result .ConnectorConf .TargetConf )
102+ }
103+
104+ // EvaluatorsConf should only be created when evaluator mapping exists
105+ if tt .expectedEvalSetConf {
106+ assert .NotNil (t , result .ConnectorConf .EvaluatorsConf )
107+ } else {
108+ assert .Nil (t , result .ConnectorConf .EvaluatorsConf )
109+ }
110+ })
111+ }
112+ }
113+
114+ func TestToTargetFieldMappingDO_AlwaysReturnsValidConf (t * testing.T ) {
115+ tests := []struct {
116+ name string
117+ mapping * domain_expt.TargetFieldMapping
118+ runtimeParam * common.RuntimeParam
119+ expectedEvalSetFields int
120+ expectedCustomConf bool
121+ }{
122+ {
123+ name : "nil_mapping_should_return_valid_conf_with_empty_adapter" ,
124+ mapping : nil ,
125+ runtimeParam : nil ,
126+ expectedEvalSetFields : 0 ,
127+ expectedCustomConf : false ,
128+ },
129+ {
130+ name : "valid_mapping_should_populate_field_configs" ,
131+ mapping : & domain_expt.TargetFieldMapping {
132+ FromEvalSet : []* domain_expt.FieldMapping {
133+ {
134+ FieldName : gptr .Of ("input" ),
135+ FromFieldName : gptr .Of ("question" ),
136+ ConstValue : gptr .Of ("" ),
137+ },
138+ {
139+ FieldName : gptr .Of ("role" ),
140+ FromFieldName : gptr .Of ("user_role" ),
141+ ConstValue : gptr .Of ("user" ),
142+ },
143+ },
144+ },
145+ runtimeParam : nil ,
146+ expectedEvalSetFields : 2 ,
147+ expectedCustomConf : false ,
148+ },
149+ {
150+ name : "nil_mapping_with_runtime_param_should_create_custom_conf" ,
151+ mapping : nil ,
152+ runtimeParam : & common.RuntimeParam {
153+ JSONValue : gptr .Of (`{"model":"test"}` ),
154+ },
155+ expectedEvalSetFields : 0 ,
156+ expectedCustomConf : true ,
157+ },
158+ {
159+ name : "valid_mapping_with_runtime_param_should_create_both" ,
160+ mapping : & domain_expt.TargetFieldMapping {
161+ FromEvalSet : []* domain_expt.FieldMapping {
162+ {
163+ FieldName : gptr .Of ("input" ),
164+ FromFieldName : gptr .Of ("question" ),
165+ },
166+ },
167+ },
168+ runtimeParam : & common.RuntimeParam {
169+ JSONValue : gptr .Of (`{"temperature":0.7}` ),
170+ },
171+ expectedEvalSetFields : 1 ,
172+ expectedCustomConf : true ,
173+ },
174+ {
175+ name : "runtime_param_with_empty_json_should_not_create_custom_conf" ,
176+ mapping : & domain_expt.TargetFieldMapping {
177+ FromEvalSet : []* domain_expt.FieldMapping {
178+ {
179+ FieldName : gptr .Of ("input" ),
180+ FromFieldName : gptr .Of ("question" ),
181+ },
182+ },
183+ },
184+ runtimeParam : & common.RuntimeParam {
185+ JSONValue : gptr .Of ("" ),
186+ },
187+ expectedEvalSetFields : 1 ,
188+ expectedCustomConf : false ,
189+ },
190+ }
191+
192+ for _ , tt := range tests {
193+ t .Run (tt .name , func (t * testing.T ) {
194+ result := toTargetFieldMappingDO (tt .mapping , tt .runtimeParam )
195+
196+ // Should always return a valid TargetIngressConf
197+ assert .NotNil (t , result )
198+ assert .NotNil (t , result .EvalSetAdapter )
199+
200+ // Check EvalSetAdapter field configurations
201+ assert .Equal (t , tt .expectedEvalSetFields , len (result .EvalSetAdapter .FieldConfs ))
202+
203+ // Check CustomConf creation
204+ if tt .expectedCustomConf {
205+ assert .NotNil (t , result .CustomConf )
206+ assert .Equal (t , 1 , len (result .CustomConf .FieldConfs ))
207+ assert .Equal (t , consts .FieldAdapterBuiltinFieldNameRuntimeParam , result .CustomConf .FieldConfs [0 ].FieldName )
208+ } else {
209+ assert .Nil (t , result .CustomConf )
210+ }
211+
212+ // Verify field mapping content when mapping is provided
213+ if tt .mapping != nil && len (tt .mapping .FromEvalSet ) > 0 {
214+ for i , expectedMapping := range tt .mapping .FromEvalSet {
215+ actualField := result .EvalSetAdapter .FieldConfs [i ]
216+ assert .Equal (t , gptr .Indirect (expectedMapping .FieldName ), actualField .FieldName )
217+ assert .Equal (t , gptr .Indirect (expectedMapping .FromFieldName ), actualField .FromField )
218+ assert .Equal (t , gptr .Indirect (expectedMapping .ConstValue ), actualField .Value )
219+ }
220+ }
221+ })
222+ }
223+ }
0 commit comments