Repository navigation
refactor(rules): extract duplicated stubbings in InputMatcherTest into 3 helpers - #1204
Conversation
|
@mia-li-001 is attempting to deploy a commit to the eddi Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Shared test setup src/test/java/ai/labs/eddi/modules/rules/impl/conditions/InputMatcherTest.java |
The current-step, last-step, any-step, and never-occurrence tests use shared helpers for stubbing step data and provider responses. Nonmatching expressions continue to receive an empty result. |
Estimated code review effort: 2 (Simple) | ~8 minutes
Change: Refactor
Merge Risk: 
Minimal · up to cae11
-
The helper extraction preserves the existing test behavior and changes no production code. No actionable merge-blocking risk remains, subject to normal checks.
Pre-merge checks: 
5
-
Full details: Docstring Coverage
-
Explanation
No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 1 files.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
|
@CodeRabbit review |
Action performed
|
Summary
I am a researcher at the university. In my analysis of the project, I observed that
In the test class
InputMatcherTest, 3 groups of stubbings are repeated across tests:execute_occurrence_anyStepandexecute_occurrence_neverdefine the same stubbings:when(previousConversationStep1.getLatestData(eq(EXPRESSIONS_PARSED.key()))).thenAnswer(invocation -> new Data<>("expressions", "someNonMatchingExpression"))when(previousConversationStep2.getLatestData(eq(EXPRESSIONS_PARSED.key()))).thenAnswer(invocation -> new Data<>("expressions", inputExpressions))when(memory.getAllSteps()).thenAnswer(invocation -> new ConversationMemory.ConversationStepStack(Arrays.asList(previousConversationStep1, previousConversationStep2)))execute_occurrence_currentStepandexecute_occurrence_lastStepdefine the same stubbings:when(conversationStep.getLatestData(eq(EXPRESSIONS_PARSED))).thenAnswer(invocation -> new Data<>("expressions", inputExpressions))when(expressionProvider.parseExpressions(eq(inputExpressions))).thenAnswer(invocation -> expectedInputExpressions)execute_occurrence_anyStepandexecute_occurrence_neverdefine the same stubbings:when(expressionProvider.parseExpressions(eq(inputExpressions))).thenAnswer(invocation -> expectedInputExpressions)when(expressionProvider.parseExpressions(not(eq(inputExpressions)))).thenAnswer(invocation -> new Expressions())Here
previousConversationStep1,previousConversationStep2,memory,conversationStepandexpectedInputExpressionsstand for the mocks and values that differ from test to test.These duplicated stubbings repeat the setup in each test, so a change to that setup has to be made in 2 places for each group. I propose extracting them into 3 private helper methods:
setupIConversationMemoryGetAllStepsAndGetLatestDataStubs, which takes 3 parameters (previousConversationStep1,previousConversationStep2andmemory) and is called from 2 tests.setupConversationStepGetLatestDataAndExpressionProviderParseExpressionsStubs, which takes 2 parameters (conversationStepandexpectedInputExpressions) and is called from 2 tests.setupExpressionProviderParseExpressionsStubs, which takes 1 parameter (expectedInputExpressions) and is called from 2 tests.In
execute_occurrence_currentStepandexecute_occurrence_lastStep, thegetLatestDatastubbing now comes after thegetCurrentStep/getPreviousStepsstubbing (inside the helper call). All stubbings are still made before the matcher runs, so the behavior is the same.This refactoring also reduces the cognitive complexity of
InputMatcherTestby 13.3% (from 309.1 to 268.1, measured with Genese). Cognitive complexity, introduced by SonarSource, estimates how hard code is for a human to understand.The change only moves the existing stubbings into the helpers. The stubbed calls, arguments and returned values are the same as before, and the test results are unchanged.
I propose a solution below to remove the duplicated stubbings. Happy to modify the pull request based on your feedback.
Type of Change
Related Issue
N/A
Closes #
Changes Made
setupIConversationMemoryGetAllStepsAndGetLatestDataStubs(final IConversationStep previousConversationStep1, final IConversationStep previousConversationStep2, final IConversationMemory memory), called fromexecute_occurrence_anyStepandexecute_occurrence_never.setupConversationStepGetLatestDataAndExpressionProviderParseExpressionsStubs(final IConversationStep conversationStep, final Expressions expectedInputExpressions), called fromexecute_occurrence_currentStepandexecute_occurrence_lastStep.setupExpressionProviderParseExpressionsStubs(final Expressions expectedInputExpressions), called fromexecute_occurrence_anyStepandexecute_occurrence_never.execute_occurrence_currentStepandexecute_occurrence_lastStep, thegetLatestDatastubbing now comes after thegetCurrentStep/getPreviousStepsstubbing (inside the helper call); all stubbings are still made before the matcher runs.How to Test
Run the target test class using Maven wrapper:
Verify that all 13 tests pass without regressions:
(Optional) Run the full test suite locally:
Checklist
./mvnw clean verify -DskipITs)docs/changelog.d/— not an edit todocs/changelog.md, which every open PR would conflict overSummary by CodeRabbit