1+ // Copyright (c) 2025 coze-dev Authors
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ package application
5+
6+ import (
7+ "context"
8+ "testing"
9+
10+ "go.uber.org/mock/gomock"
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+ exptpb "github.com/coze-dev/coze-loop/backend/kitex_gen/coze/loop/evaluation/expt"
15+ "github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/entity"
16+ repo_mocks "github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/repo/mocks"
17+ servicemocks "github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/service/mocks"
18+ rpcmocks "github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/component/rpc/mocks"
19+ )
20+
21+ func setupTestApp (t * testing.T ) (context.Context , * experimentApplication , * servicemocks.MockIExptManager , * repo_mocks.MockIExperimentRepo , * servicemocks.MockIExptInsightAnalysisService , * rpcmocks.MockIAuthProvider ) {
22+ ctrl := gomock .NewController (t )
23+ mockManager := servicemocks .NewMockIExptManager (ctrl )
24+ mockRepo := repo_mocks .NewMockIExperimentRepo (ctrl )
25+ mockInsightService := servicemocks .NewMockIExptInsightAnalysisService (ctrl )
26+ mockAuth := rpcmocks .NewMockIAuthProvider (ctrl )
27+
28+ app := & experimentApplication {
29+ manager : mockManager ,
30+ IExptInsightAnalysisService : mockInsightService ,
31+ auth : mockAuth ,
32+ }
33+
34+ return context .Background (), app , mockManager , mockRepo , mockInsightService , mockAuth
35+ }
36+
37+ func TestInsightAnalysisExperiment (t * testing.T ) {
38+ ctx , app , mockManager , _ , mockInsightService , mockAuth := setupTestApp (t )
39+
40+ req := & exptpb.InsightAnalysisExperimentRequest {
41+ WorkspaceID : 123 ,
42+ ExptID : 456 ,
43+ Session : & common.Session {
44+ UserID : & []int64 {789 }[0 ],
45+ },
46+ }
47+
48+ // Mock the manager.Get call
49+ mockManager .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (& entity.Experiment {CreatedBy : "test-user" }, nil )
50+ // Mock the auth.AuthorizationWithoutSPI call
51+ mockAuth .EXPECT ().AuthorizationWithoutSPI (gomock .Any (), gomock .Any ()).Return (nil )
52+ // Mock the CreateAnalysisRecord call
53+ mockInsightService .EXPECT ().CreateAnalysisRecord (gomock .Any (), gomock .Any (), gomock .Any ()).Return (int64 (123 ), nil )
54+
55+ _ , err := app .InsightAnalysisExperiment (ctx , req )
56+ if err != nil {
57+ t .Errorf ("InsightAnalysisExperiment failed: %v" , err )
58+ }
59+ }
60+
61+ func TestListExptInsightAnalysisRecord (t * testing.T ) {
62+ ctx , app , mockManager , _ , mockInsightService , mockAuth := setupTestApp (t )
63+
64+ req := & exptpb.ListExptInsightAnalysisRecordRequest {
65+ WorkspaceID : 123 ,
66+ ExptID : 456 ,
67+ PageNumber : & []int32 {1 }[0 ],
68+ PageSize : & []int32 {10 }[0 ],
69+ Session : & common.Session {
70+ UserID : & []int64 {789 }[0 ],
71+ },
72+ }
73+
74+ // Mock the manager.Get call
75+ mockManager .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (& entity.Experiment {CreatedBy : "test-user" }, nil )
76+ // Mock the auth.AuthorizationWithoutSPI call
77+ mockAuth .EXPECT ().AuthorizationWithoutSPI (gomock .Any (), gomock .Any ()).Return (nil )
78+ mockInsightService .EXPECT ().ListAnalysisRecord (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return ([]* entity.ExptInsightAnalysisRecord {}, int64 (0 ), nil )
79+
80+ _ , err := app .ListExptInsightAnalysisRecord (ctx , req )
81+ if err != nil {
82+ t .Errorf ("ListExptInsightAnalysisRecord failed: %v" , err )
83+ }
84+ }
85+
86+ func TestGetExptInsightAnalysisRecord (t * testing.T ) {
87+ ctx , app , _ , _ , mockInsightService , mockAuth := setupTestApp (t )
88+
89+ userID := int64 (789 )
90+ req := & exptpb.GetExptInsightAnalysisRecordRequest {
91+ WorkspaceID : 123 ,
92+ ExptID : 456 ,
93+ InsightAnalysisRecordID : 789 ,
94+ Session : & common.Session {
95+ UserID : & userID ,
96+ },
97+ }
98+
99+ // Mock the auth.Authorization call
100+ mockAuth .EXPECT ().Authorization (gomock .Any (), gomock .Any ()).Return (nil )
101+ // Mock the service call
102+ mockInsightService .EXPECT ().GetAnalysisRecordByID (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (& entity.ExptInsightAnalysisRecord {
103+ ID : 789 ,
104+ ExptID : 456 ,
105+ SpaceID : 123 ,
106+ Status : entity .InsightAnalysisStatus_Running ,
107+ CreatedBy : "test-user" ,
108+ }, nil )
109+
110+ resp , err := app .GetExptInsightAnalysisRecord (ctx , req )
111+ if err != nil {
112+ t .Errorf ("GetExptInsightAnalysisRecord failed: %v" , err )
113+ }
114+ if resp == nil {
115+ t .Error ("Expected non-nil response" )
116+ }
117+ }
118+
119+ func TestDeleteExptInsightAnalysisRecord (t * testing.T ) {
120+ ctx , app , mockManager , _ , mockInsightService , mockAuth := setupTestApp (t )
121+
122+ req := & exptpb.DeleteExptInsightAnalysisRecordRequest {
123+ WorkspaceID : 123 ,
124+ ExptID : 456 ,
125+ InsightAnalysisRecordID : 789 ,
126+ Session : & common.Session {
127+ UserID : & []int64 {789 }[0 ],
128+ },
129+ }
130+
131+ // Mock the manager.Get call
132+ mockManager .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (& entity.Experiment {CreatedBy : "test-user" }, nil )
133+ // Mock the auth.AuthorizationWithoutSPI call
134+ mockAuth .EXPECT ().AuthorizationWithoutSPI (gomock .Any (), gomock .Any ()).Return (nil )
135+ mockInsightService .EXPECT ().DeleteAnalysisRecord (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil )
136+
137+ _ , err := app .DeleteExptInsightAnalysisRecord (ctx , req )
138+ if err != nil {
139+ t .Errorf ("DeleteExptInsightAnalysisRecord failed: %v" , err )
140+ }
141+ }
142+
143+ func TestFeedbackExptInsightAnalysisReport (t * testing.T ) {
144+ ctx , app , mockManager , _ , mockInsightService , mockAuth := setupTestApp (t )
145+
146+ req := & exptpb.FeedbackExptInsightAnalysisReportRequest {
147+ WorkspaceID : 123 ,
148+ ExptID : 456 ,
149+ InsightAnalysisRecordID : 789 ,
150+ FeedbackActionType : domain_expt .FeedbackActionTypeUpvote ,
151+ Session : & common.Session {
152+ UserID : & []int64 {789 }[0 ],
153+ },
154+ }
155+
156+ // Mock the manager.Get call
157+ mockManager .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (& entity.Experiment {CreatedBy : "test-user" }, nil )
158+ // Mock the auth.AuthorizationWithoutSPI call
159+ mockAuth .EXPECT ().AuthorizationWithoutSPI (gomock .Any (), gomock .Any ()).Return (nil )
160+ mockInsightService .EXPECT ().FeedbackExptInsightAnalysis (gomock .Any (), gomock .Any ()).Return (nil )
161+
162+ _ , err := app .FeedbackExptInsightAnalysisReport (ctx , req )
163+ if err != nil {
164+ t .Errorf ("FeedbackExptInsightAnalysisReport failed: %v" , err )
165+ }
166+ }
167+
168+ func TestListExptInsightAnalysisComment (t * testing.T ) {
169+ ctx , app , _ , _ , mockInsightService , mockAuth := setupTestApp (t )
170+
171+ req := & exptpb.ListExptInsightAnalysisCommentRequest {
172+ WorkspaceID : 123 ,
173+ ExptID : 456 ,
174+ InsightAnalysisRecordID : 789 ,
175+ PageNumber : & []int32 {1 }[0 ],
176+ PageSize : & []int32 {10 }[0 ],
177+ Session : & common.Session {
178+ UserID : & []int64 {789 }[0 ],
179+ },
180+ }
181+
182+ // Mock the auth.Authorization call
183+ mockAuth .EXPECT ().Authorization (gomock .Any (), gomock .Any ()).Return (nil )
184+ mockInsightService .EXPECT ().ListExptInsightAnalysisFeedbackComment (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return ([]* entity.ExptInsightAnalysisFeedbackComment {}, int64 (0 ), nil )
185+
186+ _ , err := app .ListExptInsightAnalysisComment (ctx , req )
187+ if err != nil {
188+ t .Errorf ("ListExptInsightAnalysisComment failed: %v" , err )
189+ }
190+ }
0 commit comments