@ -12,6 +12,7 @@ import com.sztzjy.linkCommerce.mapper.TeachingClassStudentMapper;
import com.sztzjy.linkCommerce.mapper.TrainingTaskMapper ;
import com.sztzjy.linkCommerce.config.exception.handler.ServiceException ;
import com.sztzjy.linkCommerce.config.security.JwtUser ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import org.junit.jupiter.api.Test ;
import org.mockito.ArgumentCaptor ;
import org.springframework.transaction.PlatformTransactionManager ;
@ -127,6 +128,32 @@ class AiTrainingEvaluationServiceImplTest {
verify ( service . studentTrainingAnswerMapper ) . updateAiAssessmentScore ( eq ( "answer-1" ) , eq ( 86 ) , any ( Date . class ) ) ;
}
@Test
void assessmentAcceptsJsonFenceAndWholeNumberStrings ( ) {
AiTrainingEvaluationServiceImpl service = serviceWithContext ( answer ( "answer-1" , "step one" , null , null , null ) ) ;
AiTrainingEvaluation evaluation = evaluation ( "NOT_STARTED" , "NOT_STARTED" ) ;
String fencedResponse = "```json\n" + assessmentJson ( )
. replace ( "\"score\":86" , "\"score\":\"86\"" )
. replace ( "\"maxScore\":25" , "\"maxScore\":\"25\"" )
. replace ( "\"score\":22" , "\"score\":\"22\"" )
. replace ( "\"score\":21" , "\"score\":\"21\"" ) + "\n```" ;
when ( service . aiTrainingEvaluationMapper . findByStudentClassAndTask ( "stu-1" , "class-1" , "task-1" ) )
. thenReturn ( null , evaluation ) ;
when ( service . aiTrainingEvaluationMapper . claimAssessment ( eq ( "evaluation-1" ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
when ( service . qwenChatClient . completeJson ( any ( ) , any ( ) ) ) . thenReturn ( fencedResponse ) ;
when ( service . aiTrainingEvaluationMapper . completeAssessment ( any ( AiTrainingEvaluation . class ) ) ) . thenReturn ( 1 ) ;
when ( service . studentTrainingAnswerMapper . updateAiAssessmentScore ( eq ( "answer-1" ) , eq ( 86 ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
AiTrainingEvaluationView view = service . generateAssessment ( "new-product-survey" , student ( ) ) ;
ArgumentCaptor < AiTrainingEvaluation > saved = ArgumentCaptor . forClass ( AiTrainingEvaluation . class ) ;
verify ( service . aiTrainingEvaluationMapper ) . completeAssessment ( saved . capture ( ) ) ;
assertEquals ( Integer . valueOf ( 86 ) , view . getAssessmentScore ( ) ) ;
assertEquals ( fencedResponse , saved . getValue ( ) . getAssessmentRawResponse ( ) ) ;
assertTrue ( saved . getValue ( ) . getAssessmentReportJson ( ) . startsWith ( "{" ) ) ;
assertTrue ( ! saved . getValue ( ) . getAssessmentReportJson ( ) . contains ( "```" ) ) ;
}
@Test
void assessmentScoreWriteFailureRollsBackCompletionAndLeavesEvaluationRetryable ( ) throws Exception {
AiTrainingEvaluationServiceImpl service = serviceWithContext ( answer ( "answer-1" , "step one" , null , null , null ) ) ;
@ -149,7 +176,7 @@ class AiTrainingEvaluationServiceImplTest {
} ) ;
when ( service . studentTrainingAnswerMapper . updateAiAssessmentScore ( eq ( "answer-1" ) , eq ( 86 ) , any ( Date . class ) ) )
. thenThrow ( new IllegalStateException ( "score write failed" ) ) ;
when ( service . aiTrainingEvaluationMapper . failAssessment ( eq ( "evaluation-1" ) , any ( ) , any ( Date . class ) ) ) . thenAnswer ( invocation - > {
when ( service . aiTrainingEvaluationMapper . failAssessment ( eq ( "evaluation-1" ) , any ( ) , any ( ) , any ( Date . class ) ) ) . thenAnswer ( invocation - > {
if ( "PROCESSING" . equals ( evaluation . getAssessmentStatus ( ) ) ) {
evaluation . setAssessmentStatus ( "FAILED" ) ;
return 1 ;
@ -175,10 +202,116 @@ class AiTrainingEvaluationServiceImplTest {
assertThrows ( ServiceException . class , ( ) - > service . generateAssessment ( "new-product-survey" , student ( ) ) ) ;
verify ( service . aiTrainingEvaluationMapper ) . failAssessment ( eq ( "evaluation-1" ) , any ( ) , any ( Date . class ) ) ;
verify ( service . aiTrainingEvaluationMapper ) . failAssessment ( eq ( "evaluation-1" ) , any ( ) , any ( ) , any ( Date . class ) ) ;
verify ( service . studentTrainingAnswerMapper , never ( ) ) . updateAiAssessmentScore ( any ( ) , any ( ) , any ( ) ) ;
}
@Test
void invalidAssessmentKeepsRawModelResponseForDiagnosis ( ) {
AiTrainingEvaluationServiceImpl service = serviceWithContext ( answer ( "answer-1" , "step one" , null , null , null ) ) ;
AiTrainingEvaluation evaluation = evaluation ( "NOT_STARTED" , "NOT_STARTED" ) ;
String invalidResponse = assessmentJson ( ) . replace ( "\"maxScore\":25" , "\"maxScore\":24" ) ;
when ( service . aiTrainingEvaluationMapper . findByStudentClassAndTask ( "stu-1" , "class-1" , "task-1" ) )
. thenReturn ( null , evaluation ) ;
when ( service . aiTrainingEvaluationMapper . claimAssessment ( eq ( "evaluation-1" ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
when ( service . qwenChatClient . completeJson ( any ( ) , any ( ) ) ) . thenReturn ( invalidResponse ) ;
assertThrows ( ServiceException . class , ( ) - > service . generateAssessment ( "new-product-survey" , student ( ) ) ) ;
verify ( service . aiTrainingEvaluationMapper ) . failAssessment ( eq ( "evaluation-1" ) , any ( ) , eq ( invalidResponse ) , any ( Date . class ) ) ;
}
@Test
void assessmentUsesEachOfFiveConfiguredStepsWithAMaximumOfTwentyPoints ( ) {
AiTrainingEvaluationServiceImpl service = serviceWithContext ( answer ( "answer-1" , "step one" , null , null , null ) ) ;
AiTrainingEvaluation evaluation = evaluation ( "NOT_STARTED" , "NOT_STARTED" ) ;
TrainingTask task = task ( ) ;
task . setSteps ( "[{\"name\":\"步骤一\"},{\"name\":\"步骤二\"},{\"name\":\"步骤三\"},{\"name\":\"步骤四\"},{\"name\":\"步骤五\"}]" ) ;
when ( service . trainingTaskMapper . selectByTaskKey ( "new-product-survey" ) ) . thenReturn ( task ) ;
when ( service . aiTrainingEvaluationMapper . findByStudentClassAndTask ( "stu-1" , "class-1" , "task-1" ) )
. thenReturn ( null , evaluation ) ;
when ( service . aiTrainingEvaluationMapper . claimAssessment ( eq ( "evaluation-1" ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
when ( service . qwenChatClient . completeJson ( any ( ) , any ( ) ) ) . thenReturn ( fiveStepAssessmentJson ( ) ) ;
when ( service . aiTrainingEvaluationMapper . completeAssessment ( any ( AiTrainingEvaluation . class ) ) ) . thenReturn ( 1 ) ;
when ( service . studentTrainingAnswerMapper . updateAiAssessmentScore ( eq ( "answer-1" ) , eq ( 90 ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
AiTrainingEvaluationView view = service . generateAssessment ( "new-product-survey" , student ( ) ) ;
ArgumentCaptor < String > systemPrompt = ArgumentCaptor . forClass ( String . class ) ;
verify ( service . qwenChatClient ) . completeJson ( systemPrompt . capture ( ) , any ( ) ) ;
assertEquals ( Integer . valueOf ( 90 ) , view . getAssessmentScore ( ) ) ;
assertTrue ( systemPrompt . getValue ( ) . contains ( "There are exactly 5 training steps and the total score is exactly 100" ) ) ;
assertTrue ( systemPrompt . getValue ( ) . contains ( "步骤五 (max 20)" ) ) ;
}
@Test
void assessmentRejectsFiveStepCriteriaThatExceedTwentyPoints ( ) {
AiTrainingEvaluationServiceImpl service = serviceWithContext ( answer ( "answer-1" , "step one" , null , null , null ) ) ;
AiTrainingEvaluation evaluation = evaluation ( "NOT_STARTED" , "NOT_STARTED" ) ;
TrainingTask task = task ( ) ;
task . setSteps ( "[\"步骤一\",\"步骤二\",\"步骤三\",\"步骤四\",\"步骤五\"]" ) ;
when ( service . trainingTaskMapper . selectByTaskKey ( "new-product-survey" ) ) . thenReturn ( task ) ;
when ( service . aiTrainingEvaluationMapper . findByStudentClassAndTask ( "stu-1" , "class-1" , "task-1" ) )
. thenReturn ( null , evaluation ) ;
when ( service . aiTrainingEvaluationMapper . claimAssessment ( eq ( "evaluation-1" ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
when ( service . qwenChatClient . completeJson ( any ( ) , any ( ) ) ) . thenReturn ( fiveStepAssessmentJson ( ) . replaceFirst ( "\"maxScore\":20" , "\"maxScore\":21" ) ) ;
assertThrows ( ServiceException . class , ( ) - > service . generateAssessment ( "new-product-survey" , student ( ) ) ) ;
verify ( service . aiTrainingEvaluationMapper ) . failAssessment ( eq ( "evaluation-1" ) , any ( ) , any ( ) , any ( Date . class ) ) ;
}
@Test
void assessmentLimitsAllStepRationalesToTwoHundredCharacters ( ) throws Exception {
AiTrainingEvaluationServiceImpl service = serviceWithContext ( answer ( "answer-1" , "step one" , null , null , null ) ) ;
AiTrainingEvaluation evaluation = evaluation ( "NOT_STARTED" , "NOT_STARTED" ) ;
String longRationale = "评分依据" . repeat ( 80 ) ;
String response = assessmentJson ( )
. replace ( "evidence" , longRationale )
. replace ( "logic" , longRationale )
. replace ( "clarity" , longRationale )
. replace ( "coverage" , longRationale ) ;
when ( service . aiTrainingEvaluationMapper . findByStudentClassAndTask ( "stu-1" , "class-1" , "task-1" ) )
. thenReturn ( null , evaluation ) ;
when ( service . aiTrainingEvaluationMapper . claimAssessment ( eq ( "evaluation-1" ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
when ( service . qwenChatClient . completeJson ( any ( ) , any ( ) ) ) . thenReturn ( response ) ;
when ( service . aiTrainingEvaluationMapper . completeAssessment ( any ( AiTrainingEvaluation . class ) ) ) . thenReturn ( 1 ) ;
when ( service . studentTrainingAnswerMapper . updateAiAssessmentScore ( eq ( "answer-1" ) , eq ( 86 ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
service . generateAssessment ( "new-product-survey" , student ( ) ) ;
ArgumentCaptor < AiTrainingEvaluation > saved = ArgumentCaptor . forClass ( AiTrainingEvaluation . class ) ;
verify ( service . aiTrainingEvaluationMapper ) . completeAssessment ( saved . capture ( ) ) ;
int totalCharacters = 0 ;
for ( com . fasterxml . jackson . databind . JsonNode criterion : new ObjectMapper ( )
. readTree ( saved . getValue ( ) . getAssessmentReportJson ( ) ) . path ( "criteria" ) ) {
String rationale = criterion . path ( "rationale" ) . asText ( ) ;
totalCharacters + = rationale . codePointCount ( 0 , rationale . length ( ) ) ;
}
assertTrue ( totalCharacters < = 200 ) ;
}
@Test
void assessmentTruncationUsesASentenceBoundaryWhenAvailable ( ) throws Exception {
AiTrainingEvaluationServiceImpl service = serviceWithContext ( answer ( "answer-1" , "step one" , null , null , null ) ) ;
AiTrainingEvaluation evaluation = evaluation ( "NOT_STARTED" , "NOT_STARTED" ) ;
String response = assessmentJson ( ) . replace ( "evidence" , "第一句说明问题。第二句非常长" + "内容" . repeat ( 80 ) ) ;
when ( service . aiTrainingEvaluationMapper . findByStudentClassAndTask ( "stu-1" , "class-1" , "task-1" ) )
. thenReturn ( null , evaluation ) ;
when ( service . aiTrainingEvaluationMapper . claimAssessment ( eq ( "evaluation-1" ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
when ( service . qwenChatClient . completeJson ( any ( ) , any ( ) ) ) . thenReturn ( response ) ;
when ( service . aiTrainingEvaluationMapper . completeAssessment ( any ( AiTrainingEvaluation . class ) ) ) . thenReturn ( 1 ) ;
when ( service . studentTrainingAnswerMapper . updateAiAssessmentScore ( eq ( "answer-1" ) , eq ( 86 ) , any ( Date . class ) ) ) . thenReturn ( 1 ) ;
service . generateAssessment ( "new-product-survey" , student ( ) ) ;
ArgumentCaptor < AiTrainingEvaluation > saved = ArgumentCaptor . forClass ( AiTrainingEvaluation . class ) ;
verify ( service . aiTrainingEvaluationMapper ) . completeAssessment ( saved . capture ( ) ) ;
String rationale = new ObjectMapper ( ) . readTree ( saved . getValue ( ) . getAssessmentReportJson ( ) )
. path ( "criteria" ) . get ( 0 ) . path ( "rationale" ) . asText ( ) ;
assertEquals ( "第一句说明问题。" , rationale ) ;
}
@Test
void succeededHelpCanBeRegeneratedForANewQuestion ( ) {
AiTrainingEvaluationServiceImpl service = serviceWithContext ( answer ( "answer-1" , "step one" , null , null , null ) ) ;
@ -289,7 +422,7 @@ class AiTrainingEvaluationServiceImplTest {
assertNotNull ( AiTrainingEvaluationMapper . class . getDeclaredMethod ( "completeHelp" , AiTrainingEvaluation . class ) ) ;
assertNotNull ( AiTrainingEvaluationMapper . class . getDeclaredMethod ( "completeAssessment" , AiTrainingEvaluation . class ) ) ;
assertNotNull ( AiTrainingEvaluationMapper . class . getDeclaredMethod ( "failHelp" , String . class , String . class , Date . class ) ) ;
assertNotNull ( AiTrainingEvaluationMapper . class . getDeclaredMethod ( "failAssessment" , String . class , String . class , Date. class ) ) ;
assertNotNull ( AiTrainingEvaluationMapper . class . getDeclaredMethod ( "failAssessment" , String . class , String . class , String. class , Date. class ) ) ;
String xml = readResource ( "src/main/resources/mappers/AiTrainingEvaluationMapper.xml" ) ;
assertTrue ( xml . contains ( "INSERT IGNORE INTO ai_training_evaluation" ) ) ;
@ -364,6 +497,7 @@ class AiTrainingEvaluationServiceImplTest {
task . setBackground ( "task background" ) ;
task . setObjectives ( "[\"objective one\"]" ) ;
task . setRequirements ( "task requirement" ) ;
task . setSteps ( "[{\"name\":\"步骤一\"},{\"name\":\"步骤二\"},{\"name\":\"步骤三\"},{\"name\":\"步骤四\"}]" ) ;
return task ;
}
@ -398,9 +532,20 @@ class AiTrainingEvaluationServiceImplTest {
private String assessmentJson ( ) {
return "{\"score\":86,\"overallComment\":\"good\",\"criteria\":["
+ "{\"name\":\"research\",\"maxScore\":40,\"score\":34,\"rationale\":\"evidence\"},"
+ "{\"name\":\"analysis\",\"maxScore\":35,\"score\":30,\"rationale\":\"logic\"},"
+ "{\"name\":\"presentation\",\"maxScore\":25,\"score\":22,\"rationale\":\"clarity\"}],"
+ "{\"name\":\"步骤一\",\"maxScore\":25,\"score\":22,\"rationale\":\"evidence\"},"
+ "{\"name\":\"步骤二\",\"maxScore\":25,\"score\":22,\"rationale\":\"logic\"},"
+ "{\"name\":\"步骤三\",\"maxScore\":25,\"score\":21,\"rationale\":\"clarity\"},"
+ "{\"name\":\"步骤四\",\"maxScore\":25,\"score\":21,\"rationale\":\"coverage\"}],"
+ "\"strengths\":[\"clear\"],\"improvements\":[\"more data\"]}" ;
}
private String fiveStepAssessmentJson ( ) {
return "{\"score\":90,\"overallComment\":\"good\",\"criteria\":["
+ "{\"name\":\"步骤一\",\"maxScore\":20,\"score\":18,\"rationale\":\"one\"},"
+ "{\"name\":\"步骤二\",\"maxScore\":20,\"score\":18,\"rationale\":\"two\"},"
+ "{\"name\":\"步骤三\",\"maxScore\":20,\"score\":18,\"rationale\":\"three\"},"
+ "{\"name\":\"步骤四\",\"maxScore\":20,\"score\":18,\"rationale\":\"four\"},"
+ "{\"name\":\"步骤五\",\"maxScore\":20,\"score\":18,\"rationale\":\"five\"}],"
+ "\"strengths\":[\"clear\"],\"improvements\":[\"more data\"]}" ;
}
}